blob: 03efd5bc645c73154cc8d5a87c93e5877edae64b [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"
Douglas Gregord44252e2011-08-25 20:47:51 +000016#include "clang/Serialization/ModuleManager.h"
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +000017#include "ASTCommon.h"
Douglas Gregord44252e2011-08-25 20:47:51 +000018#include "ASTReaderInternals.h"
Douglas Gregor55abb232009-04-10 20:39:37 +000019#include "clang/Frontend/FrontendDiagnostic.h"
Daniel Dunbar732ef8a2009-11-11 23:58:53 +000020#include "clang/Frontend/Utils.h"
Douglas Gregorc3a6ade2010-08-12 20:07:10 +000021#include "clang/Sema/Sema.h"
John McCallcc14d1f2010-08-24 08:50:51 +000022#include "clang/Sema/Scope.h"
Douglas Gregor1a0d0b92009-04-14 00:24:19 +000023#include "clang/AST/ASTConsumer.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000024#include "clang/AST/ASTContext.h"
John McCall19c1bfd2010-08-25 05:32:35 +000025#include "clang/AST/DeclTemplate.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000026#include "clang/AST/Expr.h"
John McCallbfd822c2010-08-24 07:32:53 +000027#include "clang/AST/ExprCXX.h"
Douglas Gregor9b272512011-02-28 23:58:31 +000028#include "clang/AST/NestedNameSpecifier.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000029#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000030#include "clang/AST/TypeLocVisitor.h"
Chris Lattner34321bc2009-04-10 21:41:48 +000031#include "clang/Lex/MacroInfo.h"
Douglas Gregoraae92242010-03-19 21:51:54 +000032#include "clang/Lex/PreprocessingRecord.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000033#include "clang/Lex/Preprocessor.h"
Steve Naroff3fa455a2009-04-24 20:03:17 +000034#include "clang/Lex/HeaderSearch.h"
Douglas Gregora868bbd2009-04-21 22:25:48 +000035#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000036#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000037#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000038#include "clang/Basic/FileManager.h"
Chris Lattner226efd32010-11-23 19:19:34 +000039#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000040#include "clang/Basic/TargetInfo.h"
Douglas Gregord54f3a12009-10-05 21:07:28 +000041#include "clang/Basic/Version.h"
Douglas Gregor20b2ebd2011-03-23 00:50:03 +000042#include "clang/Basic/VersionTuple.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000043#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000044#include "llvm/Bitcode/BitstreamReader.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000045#include "llvm/Support/MemoryBuffer.h"
John McCall0ad16662009-10-29 08:12:44 +000046#include "llvm/Support/ErrorHandling.h"
Douglas Gregor09b69892011-02-10 17:09:37 +000047#include "llvm/Support/FileSystem.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000048#include "llvm/Support/Path.h"
Michael J. Spencerf25faaa2010-12-09 17:36:38 +000049#include "llvm/Support/system_error.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000050#include <algorithm>
Douglas Gregorc379c072009-04-28 18:58:38 +000051#include <iterator>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000052#include <cstdio>
Douglas Gregorc5046832009-04-27 18:38:38 +000053#include <sys/stat.h>
Douglas Gregor09b69892011-02-10 17:09:37 +000054
Douglas Gregoref84c4b2009-04-09 22:27:44 +000055using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000056using namespace clang::serialization;
Douglas Gregord44252e2011-08-25 20:47:51 +000057using namespace clang::serialization::reader;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000058
59//===----------------------------------------------------------------------===//
Sebastian Redld44cd6a2010-08-18 23:57:06 +000060// PCH validator implementation
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000061//===----------------------------------------------------------------------===//
62
Sebastian Redl3e31c722010-08-18 23:56:56 +000063ASTReaderListener::~ASTReaderListener() {}
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000064
65bool
66PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts) {
67 const LangOptions &PPLangOpts = PP.getLangOptions();
68#define PARSE_LANGOPT_BENIGN(Option)
69#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
70 if (PPLangOpts.Option != LangOpts.Option) { \
71 Reader.Diag(DiagID) << LangOpts.Option << PPLangOpts.Option; \
72 return true; \
73 }
74
75 PARSE_LANGOPT_BENIGN(Trigraphs);
76 PARSE_LANGOPT_BENIGN(BCPLComment);
77 PARSE_LANGOPT_BENIGN(DollarIdents);
78 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
79 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
Chandler Carruthe03aa552010-04-17 20:17:31 +000080 PARSE_LANGOPT_IMPORTANT(GNUKeywords, diag::warn_pch_gnu_keywords);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000081 PARSE_LANGOPT_BENIGN(ImplicitInt);
82 PARSE_LANGOPT_BENIGN(Digraphs);
83 PARSE_LANGOPT_BENIGN(HexFloats);
84 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
Peter Collingbournea686b5f2011-04-15 00:35:23 +000085 PARSE_LANGOPT_IMPORTANT(C1X, diag::warn_pch_c1x);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000086 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
Michael J. Spencer4992ca4b2010-10-21 05:21:48 +000087 PARSE_LANGOPT_BENIGN(MSCVersion);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000088 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
89 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
90 PARSE_LANGOPT_BENIGN(CXXOperatorName);
91 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
92 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
93 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
Fariborz Jahanian45878032010-02-09 19:31:38 +000094 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI2, diag::warn_pch_nonfragile_abi2);
Fariborz Jahanian13f3b2f2011-01-07 18:59:25 +000095 PARSE_LANGOPT_IMPORTANT(AppleKext, diag::warn_pch_apple_kext);
Ted Kremenek1d56c9e2010-12-23 21:35:43 +000096 PARSE_LANGOPT_IMPORTANT(ObjCDefaultSynthProperties,
97 diag::warn_pch_objc_auto_properties);
Douglas Gregora860e6a2011-06-14 23:20:43 +000098 PARSE_LANGOPT_BENIGN(ObjCInferRelatedResultType)
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +000099 PARSE_LANGOPT_IMPORTANT(NoConstantCFStrings,
Fariborz Jahanian62c56022010-04-22 21:01:59 +0000100 diag::warn_pch_no_constant_cfstrings);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000101 PARSE_LANGOPT_BENIGN(PascalStrings);
102 PARSE_LANGOPT_BENIGN(WritableStrings);
Mike Stump11289f42009-09-09 15:08:12 +0000103 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000104 diag::warn_pch_lax_vector_conversions);
Nate Begeman9d905792009-06-25 22:57:40 +0000105 PARSE_LANGOPT_IMPORTANT(AltiVec, diag::warn_pch_altivec);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000106 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
Anders Carlssonce8dd3a2011-02-19 23:53:54 +0000107 PARSE_LANGOPT_IMPORTANT(ObjCExceptions, diag::warn_pch_objc_exceptions);
Anders Carlsson6bbd2682011-02-23 03:04:54 +0000108 PARSE_LANGOPT_IMPORTANT(CXXExceptions, diag::warn_pch_cxx_exceptions);
109 PARSE_LANGOPT_IMPORTANT(SjLjExceptions, diag::warn_pch_sjlj_exceptions);
Douglas Gregordbe39272011-02-01 15:15:22 +0000110 PARSE_LANGOPT_IMPORTANT(MSBitfields, diag::warn_pch_ms_bitfields);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000111 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
112 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
113 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
Mike Stump11289f42009-09-09 15:08:12 +0000114 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000115 diag::warn_pch_thread_safe_statics);
Daniel Dunbara77eaeb2009-09-03 04:54:28 +0000116 PARSE_LANGOPT_IMPORTANT(POSIXThreads, diag::warn_pch_posix_threads);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000117 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
118 PARSE_LANGOPT_BENIGN(EmitAllDecls);
119 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
Chris Lattner51924e512010-06-26 21:25:03 +0000120 PARSE_LANGOPT_BENIGN(getSignedOverflowBehavior());
Mike Stump11289f42009-09-09 15:08:12 +0000121 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000122 diag::warn_pch_heinous_extensions);
123 // FIXME: Most of the options below are benign if the macro wasn't
124 // used. Unfortunately, this means that a PCH compiled without
125 // optimization can't be used with optimization turned on, even
126 // though the only thing that changes is whether __OPTIMIZE__ was
127 // defined... but if __OPTIMIZE__ never showed up in the header, it
128 // doesn't matter. We could consider making this some special kind
129 // of check.
130 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
131 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
132 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
133 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
134 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
135 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
Chandler Carruth7ffce732011-04-23 20:05:38 +0000136 PARSE_LANGOPT_IMPORTANT(Deprecated, diag::warn_pch_deprecated);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000137 PARSE_LANGOPT_IMPORTANT(AccessControl, diag::warn_pch_access_control);
138 PARSE_LANGOPT_IMPORTANT(CharIsSigned, diag::warn_pch_char_signed);
John Thompsoned4e2952009-11-05 20:14:16 +0000139 PARSE_LANGOPT_IMPORTANT(ShortWChar, diag::warn_pch_short_wchar);
Argyrios Kyrtzidis74825bc2010-10-08 00:25:19 +0000140 PARSE_LANGOPT_IMPORTANT(ShortEnums, diag::warn_pch_short_enums);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000141 if ((PPLangOpts.getGCMode() != 0) != (LangOpts.getGCMode() != 0)) {
Mike Stump11289f42009-09-09 15:08:12 +0000142 Reader.Diag(diag::warn_pch_gc_mode)
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000143 << LangOpts.getGCMode() << PPLangOpts.getGCMode();
144 return true;
145 }
146 PARSE_LANGOPT_BENIGN(getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +0000147 PARSE_LANGOPT_IMPORTANT(getStackProtectorMode(),
148 diag::warn_pch_stack_protector);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000149 PARSE_LANGOPT_BENIGN(InstantiationDepth);
Nate Begeman9d905792009-06-25 22:57:40 +0000150 PARSE_LANGOPT_IMPORTANT(OpenCL, diag::warn_pch_opencl);
Peter Collingbourne546d0792010-12-01 19:14:57 +0000151 PARSE_LANGOPT_IMPORTANT(CUDA, diag::warn_pch_cuda);
Mike Stumpd9546382009-12-12 01:27:46 +0000152 PARSE_LANGOPT_BENIGN(CatchUndefined);
John McCall31168b02011-06-15 23:02:42 +0000153 PARSE_LANGOPT_BENIGN(DefaultFPContract);
Daniel Dunbar143021e2009-09-21 04:16:19 +0000154 PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors);
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +0000155 PARSE_LANGOPT_BENIGN(SpellChecking);
John McCall31168b02011-06-15 23:02:42 +0000156 PARSE_LANGOPT_IMPORTANT(ObjCAutoRefCount, diag::warn_pch_auto_ref_count);
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +0000157#undef PARSE_LANGOPT_IMPORTANT
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000158#undef PARSE_LANGOPT_BENIGN
159
160 return false;
161}
162
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000163bool PCHValidator::ReadTargetTriple(StringRef Triple) {
Daniel Dunbar20a682d2009-11-11 00:52:11 +0000164 if (Triple == PP.getTargetInfo().getTriple().str())
165 return false;
166
167 Reader.Diag(diag::warn_pch_target_triple)
168 << Triple << PP.getTargetInfo().getTriple().str();
169 return true;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000170}
171
Benjamin Kramer90b5b682010-11-25 18:29:30 +0000172namespace {
173 struct EmptyStringRef {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000174 bool operator ()(StringRef r) const { return r.empty(); }
Benjamin Kramer90b5b682010-11-25 18:29:30 +0000175 };
176 struct EmptyBlock {
177 bool operator ()(const PCHPredefinesBlock &r) const {return r.Data.empty();}
178 };
179}
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000180
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000181static bool EqualConcatenations(SmallVector<StringRef, 2> L,
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000182 PCHPredefinesBlocks R) {
183 // First, sum up the lengths.
184 unsigned LL = 0, RL = 0;
185 for (unsigned I = 0, N = L.size(); I != N; ++I) {
186 LL += L[I].size();
187 }
188 for (unsigned I = 0, N = R.size(); I != N; ++I) {
189 RL += R[I].Data.size();
190 }
191 if (LL != RL)
192 return false;
193 if (LL == 0 && RL == 0)
194 return true;
195
196 // Kick out empty parts, they confuse the algorithm below.
197 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
198 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
199
200 // Do it the hard way. At this point, both vectors must be non-empty.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000201 StringRef LR = L[0], RR = R[0].Data;
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000202 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
Daniel Dunbar01ad0a72010-07-16 00:00:11 +0000203 (void) RN;
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000204 for (;;) {
205 // Compare the current pieces.
206 if (LR.size() == RR.size()) {
207 // If they're the same length, it's pretty easy.
208 if (LR != RR)
209 return false;
210 // Both pieces are done, advance.
211 ++LI;
212 ++RI;
213 // If either string is done, they're both done, since they're the same
214 // length.
215 if (LI == LN) {
216 assert(RI == RN && "Strings not the same length after all?");
217 return true;
218 }
219 LR = L[LI];
220 RR = R[RI].Data;
221 } else if (LR.size() < RR.size()) {
222 // Right piece is longer.
223 if (!RR.startswith(LR))
224 return false;
225 ++LI;
226 assert(LI != LN && "Strings not the same length after all?");
227 RR = RR.substr(LR.size());
228 LR = L[LI];
229 } else {
230 // Left piece is longer.
231 if (!LR.startswith(RR))
232 return false;
233 ++RI;
234 assert(RI != RN && "Strings not the same length after all?");
235 LR = LR.substr(RR.size());
236 RR = R[RI].Data;
237 }
238 }
239}
240
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000241static std::pair<FileID, StringRef::size_type>
242FindMacro(const PCHPredefinesBlocks &Buffers, StringRef MacroDef) {
243 std::pair<FileID, StringRef::size_type> Res;
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000244 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
245 Res.second = Buffers[I].Data.find(MacroDef);
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000246 if (Res.second != StringRef::npos) {
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000247 Res.first = Buffers[I].BufferID;
248 break;
249 }
250 }
251 return Res;
252}
253
254bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000255 StringRef OriginalFileName,
Nick Lewycky36079892011-02-23 21:16:44 +0000256 std::string &SuggestedPredefines,
257 FileManager &FileMgr) {
Daniel Dunbar732ef8a2009-11-11 23:58:53 +0000258 // We are in the context of an implicit include, so the predefines buffer will
259 // have a #include entry for the PCH file itself (as normalized by the
260 // preprocessor initialization). Find it and skip over it in the checking
261 // below.
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000262 llvm::SmallString<256> PCHInclude;
263 PCHInclude += "#include \"";
Nick Lewycky36079892011-02-23 21:16:44 +0000264 PCHInclude += NormalizeDashIncludePath(OriginalFileName, FileMgr);
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000265 PCHInclude += "\"\n";
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000266 std::pair<StringRef,StringRef> Split =
267 StringRef(PP.getPredefines()).split(PCHInclude.str());
268 StringRef Left = Split.first, Right = Split.second;
Ted Kremenek1ff615c2010-03-18 00:56:54 +0000269 if (Left == PP.getPredefines()) {
270 Error("Missing PCH include entry!");
271 return true;
272 }
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000273
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000274 // If the concatenation of all the PCH buffers is equal to the adjusted
275 // command line, we're done.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000276 SmallVector<StringRef, 2> CommandLine;
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000277 CommandLine.push_back(Left);
278 CommandLine.push_back(Right);
279 if (EqualConcatenations(CommandLine, Buffers))
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000280 return false;
281
282 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump11289f42009-09-09 15:08:12 +0000283
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000284 // The predefines buffers are different. Determine what the differences are,
285 // and whether they require us to reject the PCH file.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000286 SmallVector<StringRef, 8> PCHLines;
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000287 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
288 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000289
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000290 SmallVector<StringRef, 8> CmdLineLines;
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000291 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis58c65412010-09-30 16:53:50 +0000292
293 // Pick out implicit #includes after the PCH and don't consider them for
294 // validation; we will insert them into SuggestedPredefines so that the
295 // preprocessor includes them.
296 std::string IncludesAfterPCH;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000297 SmallVector<StringRef, 8> AfterPCHLines;
Argyrios Kyrtzidis58c65412010-09-30 16:53:50 +0000298 Right.split(AfterPCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
299 for (unsigned i = 0, e = AfterPCHLines.size(); i != e; ++i) {
300 if (AfterPCHLines[i].startswith("#include ")) {
301 IncludesAfterPCH += AfterPCHLines[i];
302 IncludesAfterPCH += '\n';
303 } else {
304 CmdLineLines.push_back(AfterPCHLines[i]);
305 }
306 }
307
308 // Make sure we add the includes last into SuggestedPredefines before we
309 // exit this function.
310 struct AddIncludesRAII {
311 std::string &SuggestedPredefines;
312 std::string &IncludesAfterPCH;
313
314 AddIncludesRAII(std::string &SuggestedPredefines,
315 std::string &IncludesAfterPCH)
316 : SuggestedPredefines(SuggestedPredefines),
317 IncludesAfterPCH(IncludesAfterPCH) { }
318 ~AddIncludesRAII() {
319 SuggestedPredefines += IncludesAfterPCH;
320 }
321 } AddIncludes(SuggestedPredefines, IncludesAfterPCH);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000322
Daniel Dunbar499baed2009-11-11 05:26:28 +0000323 // Sort both sets of predefined buffer lines, since we allow some extra
324 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000325 std::sort(CmdLineLines.begin(), CmdLineLines.end());
326 std::sort(PCHLines.begin(), PCHLines.end());
327
Daniel Dunbar499baed2009-11-11 05:26:28 +0000328 // Determine which predefines that were used to build the PCH file are missing
329 // from the command line.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000330 std::vector<StringRef> MissingPredefines;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000331 std::set_difference(PCHLines.begin(), PCHLines.end(),
332 CmdLineLines.begin(), CmdLineLines.end(),
333 std::back_inserter(MissingPredefines));
334
335 bool MissingDefines = false;
336 bool ConflictingDefines = false;
337 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000338 StringRef Missing = MissingPredefines[I];
Argyrios Kyrtzidis58c65412010-09-30 16:53:50 +0000339 if (Missing.startswith("#include ")) {
340 // An -include was specified when generating the PCH; it is included in
341 // the PCH, just ignore it.
342 continue;
343 }
Daniel Dunbar499baed2009-11-11 05:26:28 +0000344 if (!Missing.startswith("#define ")) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000345 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
346 return true;
347 }
Mike Stump11289f42009-09-09 15:08:12 +0000348
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000349 // This is a macro definition. Determine the name of the macro we're
350 // defining.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000351 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump11289f42009-09-09 15:08:12 +0000352 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000353 = Missing.find_first_of("( \n\r", StartOfMacroName);
354 assert(EndOfMacroName != std::string::npos &&
355 "Couldn't find the end of the macro name");
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000356 StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000357
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000358 // Determine whether this macro was given a different definition on the
359 // command line.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000360 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000361 std::string::size_type MacroDefLen = MacroDefStart.size();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000362 SmallVector<StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000363 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
364 MacroDefStart);
365 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000366 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000367 // Different macro; we're done.
368 ConflictPos = CmdLineLines.end();
Mike Stump11289f42009-09-09 15:08:12 +0000369 break;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000370 }
Mike Stump11289f42009-09-09 15:08:12 +0000371
372 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000373 "Invalid #define in predefines buffer?");
Mike Stump11289f42009-09-09 15:08:12 +0000374 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000375 (*ConflictPos)[MacroDefLen] != '(')
376 continue; // Longer macro name; keep trying.
Mike Stump11289f42009-09-09 15:08:12 +0000377
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000378 // We found a conflicting macro definition.
379 break;
380 }
Mike Stump11289f42009-09-09 15:08:12 +0000381
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000382 if (ConflictPos != CmdLineLines.end()) {
383 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
384 << MacroName;
385
386 // Show the definition of this macro within the PCH file.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000387 std::pair<FileID, StringRef::size_type> MacroLoc =
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000388 FindMacro(Buffers, Missing);
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000389 assert(MacroLoc.second!=StringRef::npos && "Unable to find macro!");
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000390 SourceLocation PCHMissingLoc =
391 SourceMgr.getLocForStartOfFile(MacroLoc.first)
392 .getFileLocWithOffset(MacroLoc.second);
Daniel Dunbar499baed2009-11-11 05:26:28 +0000393 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000394
395 ConflictingDefines = true;
396 continue;
397 }
Mike Stump11289f42009-09-09 15:08:12 +0000398
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000399 // If the macro doesn't conflict, then we'll just pick up the macro
400 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000401 if (ConflictingDefines)
402 continue; // Don't complain if there are already conflicting defs
Mike Stump11289f42009-09-09 15:08:12 +0000403
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000404 if (!MissingDefines) {
405 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
406 MissingDefines = true;
407 }
408
409 // Show the definition of this macro within the PCH file.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000410 std::pair<FileID, StringRef::size_type> MacroLoc =
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000411 FindMacro(Buffers, Missing);
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000412 assert(MacroLoc.second!=StringRef::npos && "Unable to find macro!");
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000413 SourceLocation PCHMissingLoc =
414 SourceMgr.getLocForStartOfFile(MacroLoc.first)
415 .getFileLocWithOffset(MacroLoc.second);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000416 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
417 }
Mike Stump11289f42009-09-09 15:08:12 +0000418
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000419 if (ConflictingDefines)
420 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000421
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000422 // Determine what predefines were introduced based on command-line
423 // parameters that were not present when building the PCH
424 // file. Extra #defines are okay, so long as the identifiers being
425 // defined were not used within the precompiled header.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000426 std::vector<StringRef> ExtraPredefines;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000427 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
428 PCHLines.begin(), PCHLines.end(),
Mike Stump11289f42009-09-09 15:08:12 +0000429 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000430 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000431 StringRef &Extra = ExtraPredefines[I];
Daniel Dunbar499baed2009-11-11 05:26:28 +0000432 if (!Extra.startswith("#define ")) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000433 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
434 return true;
435 }
436
437 // This is an extra macro definition. Determine the name of the
438 // macro we're defining.
439 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump11289f42009-09-09 15:08:12 +0000440 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000441 = Extra.find_first_of("( \n\r", StartOfMacroName);
442 assert(EndOfMacroName != std::string::npos &&
443 "Couldn't find the end of the macro name");
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000444 StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000445
446 // Check whether this name was used somewhere in the PCH file. If
447 // so, defining it as a macro could change behavior, so we reject
448 // the PCH file.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000449 if (IdentifierInfo *II = Reader.get(MacroName)) {
Daniel Dunbar045c92f2009-11-11 00:52:00 +0000450 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000451 return true;
452 }
453
454 // Add this definition to the suggested predefines buffer.
455 SuggestedPredefines += Extra;
456 SuggestedPredefines += '\n';
457 }
458
459 // If we get here, it's because the predefines buffer had compatible
460 // contents. Accept the PCH file.
461 return false;
462}
463
Douglas Gregor5712ebc2010-03-16 16:35:32 +0000464void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
465 unsigned ID) {
466 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
467 ++NumHeaderInfos;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000468}
469
470void PCHValidator::ReadCounter(unsigned Value) {
471 PP.setCounterValue(Value);
472}
473
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000474//===----------------------------------------------------------------------===//
Sebastian Redl2c499f62010-08-18 23:56:43 +0000475// AST reader implementation
Douglas Gregora868bbd2009-04-21 22:25:48 +0000476//===----------------------------------------------------------------------===//
477
Sebastian Redl07a89a82010-07-30 00:29:29 +0000478void
Sebastian Redl3e31c722010-08-18 23:56:56 +0000479ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redl07a89a82010-07-30 00:29:29 +0000480 DeserializationListener = Listener;
Sebastian Redl07a89a82010-07-30 00:29:29 +0000481}
482
Chris Lattner92ba5ff2009-04-27 05:14:47 +0000483
Douglas Gregorc78d3462009-04-24 21:10:55 +0000484
Douglas Gregord44252e2011-08-25 20:47:51 +0000485unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
486 return serialization::ComputeHash(Sel);
487}
Douglas Gregorc78d3462009-04-24 21:10:55 +0000488
Mike Stump11289f42009-09-09 15:08:12 +0000489
Douglas Gregord44252e2011-08-25 20:47:51 +0000490std::pair<unsigned, unsigned>
491ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
492 using namespace clang::io;
493 unsigned KeyLen = ReadUnalignedLE16(d);
494 unsigned DataLen = ReadUnalignedLE16(d);
495 return std::make_pair(KeyLen, DataLen);
496}
497
498ASTSelectorLookupTrait::internal_key_type
499ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
500 using namespace clang::io;
Douglas Gregor4163aca2011-09-09 21:34:22 +0000501 SelectorTable &SelTable = Reader.getContext().Selectors;
Douglas Gregord44252e2011-08-25 20:47:51 +0000502 unsigned N = ReadUnalignedLE16(d);
503 IdentifierInfo *FirstII
504 = Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
505 if (N == 0)
506 return SelTable.getNullarySelector(FirstII);
507 else if (N == 1)
508 return SelTable.getUnarySelector(FirstII);
509
510 SmallVector<IdentifierInfo *, 16> Args;
511 Args.push_back(FirstII);
512 for (unsigned I = 1; I != N; ++I)
513 Args.push_back(Reader.getLocalIdentifier(F, ReadUnalignedLE32(d)));
514
515 return SelTable.getSelector(N, Args.data());
516}
517
518ASTSelectorLookupTrait::data_type
519ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
520 unsigned DataLen) {
521 using namespace clang::io;
522
523 data_type Result;
524
525 Result.ID = Reader.getGlobalSelectorID(F, ReadUnalignedLE32(d));
526 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
527 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
528
529 // Load instance methods
Douglas Gregord44252e2011-08-25 20:47:51 +0000530 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
531 if (ObjCMethodDecl *Method
532 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
533 Result.Instance.push_back(Method);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000534 }
Mike Stump11289f42009-09-09 15:08:12 +0000535
Douglas Gregord44252e2011-08-25 20:47:51 +0000536 // Load factory methods
Douglas Gregord44252e2011-08-25 20:47:51 +0000537 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
538 if (ObjCMethodDecl *Method
539 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
540 Result.Factory.push_back(Method);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000541 }
Mike Stump11289f42009-09-09 15:08:12 +0000542
Douglas Gregord44252e2011-08-25 20:47:51 +0000543 return Result;
544}
Mike Stump11289f42009-09-09 15:08:12 +0000545
Douglas Gregord44252e2011-08-25 20:47:51 +0000546unsigned ASTIdentifierLookupTrait::ComputeHash(const internal_key_type& a) {
547 return llvm::HashString(StringRef(a.first, a.second));
548}
Mike Stump11289f42009-09-09 15:08:12 +0000549
Douglas Gregord44252e2011-08-25 20:47:51 +0000550std::pair<unsigned, unsigned>
551ASTIdentifierLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
552 using namespace clang::io;
553 unsigned DataLen = ReadUnalignedLE16(d);
554 unsigned KeyLen = ReadUnalignedLE16(d);
555 return std::make_pair(KeyLen, DataLen);
556}
Douglas Gregorc78d3462009-04-24 21:10:55 +0000557
Douglas Gregord44252e2011-08-25 20:47:51 +0000558std::pair<const char*, unsigned>
559ASTIdentifierLookupTrait::ReadKey(const unsigned char* d, unsigned n) {
560 assert(n >= 2 && d[n-1] == '\0');
561 return std::make_pair((const char*) d, n-1);
562}
Douglas Gregorc78d3462009-04-24 21:10:55 +0000563
Douglas Gregord44252e2011-08-25 20:47:51 +0000564IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
565 const unsigned char* d,
566 unsigned DataLen) {
567 using namespace clang::io;
568 unsigned RawID = ReadUnalignedLE32(d);
569 bool IsInteresting = RawID & 0x01;
Mike Stump11289f42009-09-09 15:08:12 +0000570
Douglas Gregord44252e2011-08-25 20:47:51 +0000571 // Wipe out the "is interesting" bit.
572 RawID = RawID >> 1;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000573
Douglas Gregord44252e2011-08-25 20:47:51 +0000574 IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
575 if (!IsInteresting) {
576 // For uninteresting identifiers, just build the IdentifierInfo
577 // and associate it with the persistent ID.
Douglas Gregora868bbd2009-04-21 22:25:48 +0000578 IdentifierInfo *II = KnownII;
579 if (!II)
Douglas Gregor1ab036c2011-08-03 21:49:18 +0000580 II = &Reader.getIdentifierTable().getOwn(StringRef(k.first, k.second));
Douglas Gregora868bbd2009-04-21 22:25:48 +0000581 Reader.SetIdentifierInfo(ID, II);
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000582 II->setIsFromAST();
Douglas Gregora868bbd2009-04-21 22:25:48 +0000583 return II;
584 }
Mike Stump11289f42009-09-09 15:08:12 +0000585
Douglas Gregord44252e2011-08-25 20:47:51 +0000586 unsigned Bits = ReadUnalignedLE16(d);
587 bool CPlusPlusOperatorKeyword = Bits & 0x01;
588 Bits >>= 1;
589 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
590 Bits >>= 1;
591 bool Poisoned = Bits & 0x01;
592 Bits >>= 1;
593 bool ExtensionToken = Bits & 0x01;
594 Bits >>= 1;
595 bool hasMacroDefinition = Bits & 0x01;
596 Bits >>= 1;
597 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
598 Bits >>= 10;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000599
Douglas Gregord44252e2011-08-25 20:47:51 +0000600 assert(Bits == 0 && "Extra bits in the identifier?");
601 DataLen -= 6;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000602
Douglas Gregord44252e2011-08-25 20:47:51 +0000603 // Build the IdentifierInfo itself and link the identifier ID with
604 // the new IdentifierInfo.
605 IdentifierInfo *II = KnownII;
606 if (!II)
607 II = &Reader.getIdentifierTable().getOwn(StringRef(k.first, k.second));
608 Reader.SetIdentifierInfo(ID, II);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000609
Douglas Gregord44252e2011-08-25 20:47:51 +0000610 // Set or check the various bits in the IdentifierInfo structure.
611 // Token IDs are read-only.
612 if (HasRevertedTokenIDToIdentifier)
613 II->RevertTokenIDToIdentifier();
614 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
615 assert(II->isExtensionToken() == ExtensionToken &&
616 "Incorrect extension token flag");
617 (void)ExtensionToken;
618 if (Poisoned)
619 II->setIsPoisoned(true);
620 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
621 "Incorrect C++ operator keyword flag");
622 (void)CPlusPlusOperatorKeyword;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000623
Douglas Gregord44252e2011-08-25 20:47:51 +0000624 // If this identifier is a macro, deserialize the macro
625 // definition.
626 if (hasMacroDefinition) {
627 // FIXME: Check for conflicts?
628 uint32_t Offset = ReadUnalignedLE32(d);
629 Reader.SetIdentifierIsMacro(II, F, Offset);
630 DataLen -= 4;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000631 }
632
Douglas Gregord44252e2011-08-25 20:47:51 +0000633 // Read all of the declarations visible at global scope with this
634 // name.
Douglas Gregord44252e2011-08-25 20:47:51 +0000635 if (DataLen > 0) {
636 SmallVector<uint32_t, 4> DeclIDs;
637 for (; DataLen > 0; DataLen -= 4)
638 DeclIDs.push_back(Reader.getGlobalDeclID(F, ReadUnalignedLE32(d)));
639 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000640 }
641
Douglas Gregord44252e2011-08-25 20:47:51 +0000642 II->setIsFromAST();
643 return II;
644}
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000645
Douglas Gregord44252e2011-08-25 20:47:51 +0000646unsigned
647ASTDeclContextNameLookupTrait::ComputeHash(const DeclNameKey &Key) const {
648 llvm::FoldingSetNodeID ID;
649 ID.AddInteger(Key.Kind);
650
651 switch (Key.Kind) {
652 case DeclarationName::Identifier:
653 case DeclarationName::CXXLiteralOperatorName:
654 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
655 break;
656 case DeclarationName::ObjCZeroArgSelector:
657 case DeclarationName::ObjCOneArgSelector:
658 case DeclarationName::ObjCMultiArgSelector:
659 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
660 break;
661 case DeclarationName::CXXOperatorName:
662 ID.AddInteger((OverloadedOperatorKind)Key.Data);
663 break;
664 case DeclarationName::CXXConstructorName:
665 case DeclarationName::CXXDestructorName:
666 case DeclarationName::CXXConversionFunctionName:
667 case DeclarationName::CXXUsingDirective:
668 break;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000669 }
670
Douglas Gregord44252e2011-08-25 20:47:51 +0000671 return ID.ComputeHash();
672}
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +0000673
Douglas Gregord44252e2011-08-25 20:47:51 +0000674ASTDeclContextNameLookupTrait::internal_key_type
675ASTDeclContextNameLookupTrait::GetInternalKey(
676 const external_key_type& Name) const {
677 DeclNameKey Key;
678 Key.Kind = Name.getNameKind();
679 switch (Name.getNameKind()) {
680 case DeclarationName::Identifier:
681 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
682 break;
683 case DeclarationName::ObjCZeroArgSelector:
684 case DeclarationName::ObjCOneArgSelector:
685 case DeclarationName::ObjCMultiArgSelector:
686 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
687 break;
688 case DeclarationName::CXXOperatorName:
689 Key.Data = Name.getCXXOverloadedOperator();
690 break;
691 case DeclarationName::CXXLiteralOperatorName:
692 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
693 break;
694 case DeclarationName::CXXConstructorName:
695 case DeclarationName::CXXDestructorName:
696 case DeclarationName::CXXConversionFunctionName:
697 case DeclarationName::CXXUsingDirective:
698 Key.Data = 0;
699 break;
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +0000700 }
701
Douglas Gregord44252e2011-08-25 20:47:51 +0000702 return Key;
703}
704
705ASTDeclContextNameLookupTrait::external_key_type
706ASTDeclContextNameLookupTrait::GetExternalKey(
707 const internal_key_type& Key) const {
Douglas Gregor4163aca2011-09-09 21:34:22 +0000708 ASTContext &Context = Reader.getContext();
Douglas Gregord44252e2011-08-25 20:47:51 +0000709 switch (Key.Kind) {
710 case DeclarationName::Identifier:
711 return DeclarationName((IdentifierInfo*)Key.Data);
712
713 case DeclarationName::ObjCZeroArgSelector:
714 case DeclarationName::ObjCOneArgSelector:
715 case DeclarationName::ObjCMultiArgSelector:
716 return DeclarationName(Selector(Key.Data));
717
718 case DeclarationName::CXXConstructorName:
Douglas Gregor4163aca2011-09-09 21:34:22 +0000719 return Context.DeclarationNames.getCXXConstructorName(
720 Context.getCanonicalType(Reader.getLocalType(F, Key.Data)));
Douglas Gregord44252e2011-08-25 20:47:51 +0000721
722 case DeclarationName::CXXDestructorName:
Douglas Gregor4163aca2011-09-09 21:34:22 +0000723 return Context.DeclarationNames.getCXXDestructorName(
724 Context.getCanonicalType(Reader.getLocalType(F, Key.Data)));
Douglas Gregord44252e2011-08-25 20:47:51 +0000725
726 case DeclarationName::CXXConversionFunctionName:
Douglas Gregor4163aca2011-09-09 21:34:22 +0000727 return Context.DeclarationNames.getCXXConversionFunctionName(
728 Context.getCanonicalType(Reader.getLocalType(F, Key.Data)));
Douglas Gregord44252e2011-08-25 20:47:51 +0000729
730 case DeclarationName::CXXOperatorName:
Douglas Gregor4163aca2011-09-09 21:34:22 +0000731 return Context.DeclarationNames.getCXXOperatorName(
Douglas Gregord44252e2011-08-25 20:47:51 +0000732 (OverloadedOperatorKind)Key.Data);
733
734 case DeclarationName::CXXLiteralOperatorName:
Douglas Gregor4163aca2011-09-09 21:34:22 +0000735 return Context.DeclarationNames.getCXXLiteralOperatorName(
Douglas Gregord44252e2011-08-25 20:47:51 +0000736 (IdentifierInfo*)Key.Data);
737
738 case DeclarationName::CXXUsingDirective:
739 return DeclarationName::getUsingDirectiveName();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000740 }
741
Douglas Gregord44252e2011-08-25 20:47:51 +0000742 llvm_unreachable("Invalid Name Kind ?");
743}
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000744
Douglas Gregord44252e2011-08-25 20:47:51 +0000745std::pair<unsigned, unsigned>
746ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
747 using namespace clang::io;
748 unsigned KeyLen = ReadUnalignedLE16(d);
749 unsigned DataLen = ReadUnalignedLE16(d);
750 return std::make_pair(KeyLen, DataLen);
751}
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000752
Douglas Gregord44252e2011-08-25 20:47:51 +0000753ASTDeclContextNameLookupTrait::internal_key_type
754ASTDeclContextNameLookupTrait::ReadKey(const unsigned char* d, unsigned) {
755 using namespace clang::io;
756
757 DeclNameKey Key;
758 Key.Kind = (DeclarationName::NameKind)*d++;
759 switch (Key.Kind) {
760 case DeclarationName::Identifier:
761 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
762 break;
763 case DeclarationName::ObjCZeroArgSelector:
764 case DeclarationName::ObjCOneArgSelector:
765 case DeclarationName::ObjCMultiArgSelector:
766 Key.Data =
767 (uint64_t)Reader.getLocalSelector(F, ReadUnalignedLE32(d))
768 .getAsOpaquePtr();
769 break;
770 case DeclarationName::CXXOperatorName:
771 Key.Data = *d++; // OverloadedOperatorKind
772 break;
773 case DeclarationName::CXXLiteralOperatorName:
774 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
775 break;
776 case DeclarationName::CXXConstructorName:
777 case DeclarationName::CXXDestructorName:
778 case DeclarationName::CXXConversionFunctionName:
779 case DeclarationName::CXXUsingDirective:
780 Key.Data = 0;
781 break;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000782 }
783
Douglas Gregord44252e2011-08-25 20:47:51 +0000784 return Key;
785}
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000786
Douglas Gregord44252e2011-08-25 20:47:51 +0000787ASTDeclContextNameLookupTrait::data_type
788ASTDeclContextNameLookupTrait::ReadData(internal_key_type,
789 const unsigned char* d,
790 unsigned DataLen) {
791 using namespace clang::io;
792 unsigned NumDecls = ReadUnalignedLE16(d);
793 DeclID *Start = (DeclID *)d;
794 return std::make_pair(Start, Start + NumDecls);
795}
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000796
Douglas Gregor94619c82011-08-24 19:03:07 +0000797bool ASTReader::ReadDeclContextStorage(Module &M,
798 llvm::BitstreamCursor &Cursor,
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000799 const std::pair<uint64_t, uint64_t> &Offsets,
800 DeclContextInfo &Info) {
801 SavedStreamPosition SavedPosition(Cursor);
802 // First the lexical decls.
803 if (Offsets.first != 0) {
804 Cursor.JumpToBit(Offsets.first);
805
806 RecordData Record;
807 const char *Blob;
808 unsigned BlobLen;
809 unsigned Code = Cursor.ReadCode();
810 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
811 if (RecCode != DECL_CONTEXT_LEXICAL) {
812 Error("Expected lexical block");
813 return true;
814 }
815
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000816 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
817 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000818 }
819
820 // Now the lookup table.
821 if (Offsets.second != 0) {
822 Cursor.JumpToBit(Offsets.second);
823
824 RecordData Record;
825 const char *Blob;
826 unsigned BlobLen;
827 unsigned Code = Cursor.ReadCode();
828 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
829 if (RecCode != DECL_CONTEXT_VISIBLE) {
830 Error("Expected visible lookup table block");
831 return true;
832 }
833 Info.NameLookupTableData
834 = ASTDeclContextNameLookupTable::Create(
835 (const unsigned char *)Blob + Record[0],
836 (const unsigned char *)Blob,
Douglas Gregor94619c82011-08-24 19:03:07 +0000837 ASTDeclContextNameLookupTrait(*this, M));
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000838 }
839
840 return false;
841}
842
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000843void ASTReader::Error(StringRef Msg) {
Argyrios Kyrtzidisdaa41f52011-04-25 22:23:56 +0000844 Error(diag::err_fe_pch_malformed, Msg);
845}
846
847void ASTReader::Error(unsigned DiagID,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000848 StringRef Arg1, StringRef Arg2) {
Argyrios Kyrtzidisdaa41f52011-04-25 22:23:56 +0000849 if (Diags.isDiagnosticInFlight())
850 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
851 else
852 Diag(DiagID) << Arg1 << Arg2;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000853}
854
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000855/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redl2c499f62010-08-18 23:56:43 +0000856bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000857 if (Listener)
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000858 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000859 ActualOriginalFileName,
Nick Lewycky36079892011-02-23 21:16:44 +0000860 SuggestedPredefines,
861 FileMgr);
Douglas Gregorc379c072009-04-28 18:58:38 +0000862 return false;
Douglas Gregor92863e42009-04-10 23:10:45 +0000863}
864
Douglas Gregorc5046832009-04-27 18:38:38 +0000865//===----------------------------------------------------------------------===//
866// Source Manager Deserialization
867//===----------------------------------------------------------------------===//
868
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000869/// \brief Read the line table in the source manager block.
Sebastian Redl2c373b92010-10-05 15:59:54 +0000870/// \returns true if there was an error.
Douglas Gregora6895d82011-07-22 16:00:58 +0000871bool ASTReader::ParseLineTable(Module &F,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000872 SmallVectorImpl<uint64_t> &Record) {
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000873 unsigned Idx = 0;
874 LineTableInfo &LineTable = SourceMgr.getLineTable();
875
876 // Parse the file names
Douglas Gregora8854652009-04-13 17:12:42 +0000877 std::map<int, int> FileIDs;
878 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000879 // Extract the file name
880 unsigned FilenameLen = Record[Idx++];
881 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
882 Idx += FilenameLen;
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000883 MaybeAddSystemRootToFilename(Filename);
Jay Foad9a6b0982011-06-21 15:13:30 +0000884 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000885 }
886
887 // Parse the line entries
888 std::vector<LineEntry> Entries;
889 while (Idx < Record.size()) {
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000890 int FID = Record[Idx++];
Douglas Gregor925296b2011-07-19 16:10:42 +0000891 assert(FID >= 0 && "Serialized line entries for non-local file.");
892 // Remap FileID from 1-based old view.
893 FID += F.SLocEntryBaseID - 1;
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000894
895 // Extract the line entries
896 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000897 assert(NumEntries && "Numentries is 00000");
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000898 Entries.clear();
899 Entries.reserve(NumEntries);
900 for (unsigned I = 0; I != NumEntries; ++I) {
901 unsigned FileOffset = Record[Idx++];
902 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000903 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump11289f42009-09-09 15:08:12 +0000904 SrcMgr::CharacteristicKind FileKind
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000905 = (SrcMgr::CharacteristicKind)Record[Idx++];
906 unsigned IncludeOffset = Record[Idx++];
907 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
908 FileKind, IncludeOffset));
909 }
910 LineTable.AddEntry(FID, Entries);
911 }
912
913 return false;
914}
915
Douglas Gregorc5046832009-04-27 18:38:38 +0000916namespace {
917
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000918class ASTStatData {
Douglas Gregorc5046832009-04-27 18:38:38 +0000919public:
Douglas Gregorc5046832009-04-27 18:38:38 +0000920 const ino_t ino;
921 const dev_t dev;
922 const mode_t mode;
923 const time_t mtime;
924 const off_t size;
Mike Stump11289f42009-09-09 15:08:12 +0000925
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000926 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Chris Lattner2a6fa472010-11-23 19:28:12 +0000927 : ino(i), dev(d), mode(mo), mtime(m), size(s) {}
Douglas Gregorc5046832009-04-27 18:38:38 +0000928};
929
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000930class ASTStatLookupTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +0000931 public:
932 typedef const char *external_key_type;
933 typedef const char *internal_key_type;
934
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000935 typedef ASTStatData data_type;
Douglas Gregorc5046832009-04-27 18:38:38 +0000936
937 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000938 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +0000939 }
940
941 static internal_key_type GetInternalKey(const char *path) { return path; }
942
943 static bool EqualKey(internal_key_type a, internal_key_type b) {
944 return strcmp(a, b) == 0;
945 }
946
947 static std::pair<unsigned, unsigned>
948 ReadKeyDataLength(const unsigned char*& d) {
949 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
950 unsigned DataLen = (unsigned) *d++;
951 return std::make_pair(KeyLen + 1, DataLen);
952 }
953
954 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
955 return (const char *)d;
956 }
957
958 static data_type ReadData(const internal_key_type, const unsigned char *d,
959 unsigned /*DataLen*/) {
960 using namespace clang::io;
961
Douglas Gregorc5046832009-04-27 18:38:38 +0000962 ino_t ino = (ino_t) ReadUnalignedLE32(d);
963 dev_t dev = (dev_t) ReadUnalignedLE32(d);
964 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump11289f42009-09-09 15:08:12 +0000965 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregorc5046832009-04-27 18:38:38 +0000966 off_t size = (off_t) ReadUnalignedLE64(d);
967 return data_type(ino, dev, mode, mtime, size);
968 }
969};
970
971/// \brief stat() cache for precompiled headers.
972///
973/// This cache is very similar to the stat cache used by pretokenized
974/// headers.
Chris Lattner226efd32010-11-23 19:19:34 +0000975class ASTStatCache : public FileSystemStatCache {
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000976 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregorc5046832009-04-27 18:38:38 +0000977 CacheTy *Cache;
978
979 unsigned &NumStatHits, &NumStatMisses;
Mike Stump11289f42009-09-09 15:08:12 +0000980public:
Chris Lattner2a6fa472010-11-23 19:28:12 +0000981 ASTStatCache(const unsigned char *Buckets, const unsigned char *Base,
982 unsigned &NumStatHits, unsigned &NumStatMisses)
Douglas Gregorc5046832009-04-27 18:38:38 +0000983 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
984 Cache = CacheTy::Create(Buckets, Base);
985 }
986
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000987 ~ASTStatCache() { delete Cache; }
Mike Stump11289f42009-09-09 15:08:12 +0000988
Chris Lattnerdd278432010-11-23 21:17:56 +0000989 LookupResult getStat(const char *Path, struct stat &StatBuf,
990 int *FileDescriptor) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000991 // Do the lookup for the file's data in the AST file.
Chris Lattner226efd32010-11-23 19:19:34 +0000992 CacheTy::iterator I = Cache->find(Path);
Douglas Gregorc5046832009-04-27 18:38:38 +0000993
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000994 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregorc5046832009-04-27 18:38:38 +0000995 if (I == Cache->end()) {
996 ++NumStatMisses;
Chris Lattnerdd278432010-11-23 21:17:56 +0000997 return statChained(Path, StatBuf, FileDescriptor);
Douglas Gregorc5046832009-04-27 18:38:38 +0000998 }
Mike Stump11289f42009-09-09 15:08:12 +0000999
Douglas Gregorc5046832009-04-27 18:38:38 +00001000 ++NumStatHits;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001001 ASTStatData Data = *I;
Mike Stump11289f42009-09-09 15:08:12 +00001002
Chris Lattner226efd32010-11-23 19:19:34 +00001003 StatBuf.st_ino = Data.ino;
1004 StatBuf.st_dev = Data.dev;
1005 StatBuf.st_mtime = Data.mtime;
1006 StatBuf.st_mode = Data.mode;
1007 StatBuf.st_size = Data.size;
Chris Lattner8f0583d2010-11-23 20:05:15 +00001008 return CacheExists;
Douglas Gregorc5046832009-04-27 18:38:38 +00001009 }
1010};
1011} // end anonymous namespace
1012
1013
Sebastian Redl393f8b72010-07-19 20:52:06 +00001014/// \brief Read a source manager block
Douglas Gregora6895d82011-07-22 16:00:58 +00001015ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(Module &F) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001016 using namespace SrcMgr;
Douglas Gregor258ae542009-04-27 06:38:32 +00001017
Sebastian Redl393f8b72010-07-19 20:52:06 +00001018 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001019
Douglas Gregor258ae542009-04-27 06:38:32 +00001020 // Set the source-location entry cursor to the current position in
1021 // the stream. This cursor will be used to read the contents of the
1022 // source manager block initially, and then lazily read
1023 // source-location entries as needed.
Sebastian Redl393f8b72010-07-19 20:52:06 +00001024 SLocEntryCursor = F.Stream;
Douglas Gregor258ae542009-04-27 06:38:32 +00001025
1026 // The stream itself is going to skip over the source manager block.
Sebastian Redl393f8b72010-07-19 20:52:06 +00001027 if (F.Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001028 Error("malformed block record in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001029 return Failure;
1030 }
1031
1032 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001033 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001034 Error("malformed source manager block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001035 return Failure;
1036 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001037
Douglas Gregora7f71a92009-04-10 03:52:48 +00001038 RecordData Record;
1039 while (true) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001040 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregora7f71a92009-04-10 03:52:48 +00001041 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001042 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001043 Error("error at end of Source Manager block in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001044 return Failure;
1045 }
Douglas Gregor92863e42009-04-10 23:10:45 +00001046 return Success;
Douglas Gregora7f71a92009-04-10 03:52:48 +00001047 }
Mike Stump11289f42009-09-09 15:08:12 +00001048
Douglas Gregora7f71a92009-04-10 03:52:48 +00001049 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1050 // No known subblocks, always skip them.
Douglas Gregor258ae542009-04-27 06:38:32 +00001051 SLocEntryCursor.ReadSubBlockID();
1052 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001053 Error("malformed block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001054 return Failure;
1055 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001056 continue;
1057 }
Mike Stump11289f42009-09-09 15:08:12 +00001058
Douglas Gregora7f71a92009-04-10 03:52:48 +00001059 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001060 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregora7f71a92009-04-10 03:52:48 +00001061 continue;
1062 }
Mike Stump11289f42009-09-09 15:08:12 +00001063
Douglas Gregora7f71a92009-04-10 03:52:48 +00001064 // Read a record.
1065 const char *BlobStart;
1066 unsigned BlobLen;
1067 Record.clear();
Douglas Gregor258ae542009-04-27 06:38:32 +00001068 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001069 default: // Default behavior: ignore.
1070 break;
1071
Sebastian Redl539c5062010-08-18 23:57:32 +00001072 case SM_SLOC_FILE_ENTRY:
1073 case SM_SLOC_BUFFER_ENTRY:
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001074 case SM_SLOC_EXPANSION_ENTRY:
Douglas Gregor258ae542009-04-27 06:38:32 +00001075 // Once we hit one of the source location entries, we're done.
1076 return Success;
Douglas Gregora7f71a92009-04-10 03:52:48 +00001077 }
1078 }
1079}
1080
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001081/// \brief If a header file is not found at the path that we expect it to be
1082/// and the PCH file was moved from its original location, try to resolve the
1083/// file by assuming that header+PCH were moved together and the header is in
1084/// the same place relative to the PCH.
1085static std::string
1086resolveFileRelativeToOriginalDir(const std::string &Filename,
1087 const std::string &OriginalDir,
1088 const std::string &CurrDir) {
1089 assert(OriginalDir != CurrDir &&
1090 "No point trying to resolve the file if the PCH dir didn't change");
1091 using namespace llvm::sys;
1092 llvm::SmallString<128> filePath(Filename);
1093 fs::make_absolute(filePath);
1094 assert(path::is_absolute(OriginalDir));
1095 llvm::SmallString<128> currPCHPath(CurrDir);
1096
1097 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1098 fileDirE = path::end(path::parent_path(filePath));
1099 path::const_iterator origDirI = path::begin(OriginalDir),
1100 origDirE = path::end(OriginalDir);
1101 // Skip the common path components from filePath and OriginalDir.
1102 while (fileDirI != fileDirE && origDirI != origDirE &&
1103 *fileDirI == *origDirI) {
1104 ++fileDirI;
1105 ++origDirI;
1106 }
1107 for (; origDirI != origDirE; ++origDirI)
1108 path::append(currPCHPath, "..");
1109 path::append(currPCHPath, fileDirI, fileDirE);
1110 path::append(currPCHPath, path::filename(Filename));
1111 return currPCHPath.str();
1112}
1113
Douglas Gregor258ae542009-04-27 06:38:32 +00001114/// \brief Read in the source location entry with the given ID.
Douglas Gregor925296b2011-07-19 16:10:42 +00001115ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(int ID) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001116 if (ID == 0)
1117 return Success;
1118
Douglas Gregor49bf76b2011-07-21 18:46:38 +00001119 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001120 Error("source location entry ID out-of-range for AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001121 return Failure;
1122 }
1123
Douglas Gregora6895d82011-07-22 16:00:58 +00001124 Module *F = GlobalSLocEntryMap.find(-ID)->second;
Douglas Gregor925296b2011-07-19 16:10:42 +00001125 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00001126 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Douglas Gregor925296b2011-07-19 16:10:42 +00001127 unsigned BaseOffset = F->SLocEntryBaseOffset;
Sebastian Redl34522812010-07-16 17:50:48 +00001128
Douglas Gregor258ae542009-04-27 06:38:32 +00001129 ++NumSLocEntriesRead;
Douglas Gregor258ae542009-04-27 06:38:32 +00001130 unsigned Code = SLocEntryCursor.ReadCode();
1131 if (Code == llvm::bitc::END_BLOCK ||
1132 Code == llvm::bitc::ENTER_SUBBLOCK ||
1133 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001134 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001135 return Failure;
1136 }
1137
Douglas Gregor258ae542009-04-27 06:38:32 +00001138 RecordData Record;
1139 const char *BlobStart;
1140 unsigned BlobLen;
1141 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1142 default:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001143 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001144 return Failure;
1145
Sebastian Redl539c5062010-08-18 23:57:32 +00001146 case SM_SLOC_FILE_ENTRY: {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001147 std::string Filename(BlobStart, BlobStart + BlobLen);
1148 MaybeAddSystemRootToFilename(Filename);
Chris Lattner5159f612010-11-23 08:35:12 +00001149 const FileEntry *File = FileMgr.getFile(Filename);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001150 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1151 OriginalDir != CurrentDir) {
1152 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1153 OriginalDir,
1154 CurrentDir);
1155 if (!resolved.empty())
1156 File = FileMgr.getFile(resolved);
1157 }
Axel Naumann63fbaed2011-01-27 10:55:51 +00001158 if (File == 0)
1159 File = FileMgr.getVirtualFile(Filename, (off_t)Record[4],
1160 (time_t)Record[5]);
Chris Lattnerd20dc872009-06-15 04:35:16 +00001161 if (File == 0) {
1162 std::string ErrorStr = "could not find file '";
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001163 ErrorStr += Filename;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001164 ErrorStr += "' referenced by AST file";
Chris Lattnerd20dc872009-06-15 04:35:16 +00001165 Error(ErrorStr.c_str());
1166 return Failure;
1167 }
Mike Stump11289f42009-09-09 15:08:12 +00001168
Douglas Gregor09b69892011-02-10 17:09:37 +00001169 if (Record.size() < 6) {
Ted Kremenekabb1ddd2010-03-18 21:23:05 +00001170 Error("source location entry is incorrect");
1171 return Failure;
1172 }
1173
Douglas Gregorce3a8292010-07-27 00:27:13 +00001174 if (!DisableValidation &&
1175 ((off_t)Record[4] != File->getSize()
Douglas Gregor08288f22010-04-09 15:54:22 +00001176#if !defined(LLVM_ON_WIN32)
1177 // In our regression testing, the Windows file system seems to
1178 // have inconsistent modification times that sometimes
1179 // erroneously trigger this error-handling path.
Douglas Gregorce3a8292010-07-27 00:27:13 +00001180 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor08288f22010-04-09 15:54:22 +00001181#endif
Douglas Gregorce3a8292010-07-27 00:27:13 +00001182 )) {
Argyrios Kyrtzidisdaa41f52011-04-25 22:23:56 +00001183 Error(diag::err_fe_pch_file_modified, Filename);
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001184 return Failure;
1185 }
1186
Douglas Gregor925296b2011-07-19 16:10:42 +00001187 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregora6895d82011-07-22 16:00:58 +00001188 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
Douglas Gregor925296b2011-07-19 16:10:42 +00001189 // This is the module's main file.
1190 IncludeLoc = getImportLocation(F);
1191 }
1192 FileID FID = SourceMgr.createFileID(File, IncludeLoc,
Chris Lattner26b5c192010-11-23 09:19:42 +00001193 (SrcMgr::CharacteristicKind)Record[2],
Douglas Gregor925296b2011-07-19 16:10:42 +00001194 ID, BaseOffset + Record[0]);
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001195 SrcMgr::FileInfo &FileInfo =
1196 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1197 FileInfo.NumCreatedFIDs = Record[6];
Douglas Gregor258ae542009-04-27 06:38:32 +00001198 if (Record[3])
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001199 FileInfo.setHasLineDirectives();
Douglas Gregor09b69892011-02-10 17:09:37 +00001200
Douglas Gregor258ae542009-04-27 06:38:32 +00001201 break;
1202 }
1203
Sebastian Redl539c5062010-08-18 23:57:32 +00001204 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor258ae542009-04-27 06:38:32 +00001205 const char *Name = BlobStart;
1206 unsigned Offset = Record[0];
1207 unsigned Code = SLocEntryCursor.ReadCode();
1208 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00001209 unsigned RecCode
Douglas Gregor258ae542009-04-27 06:38:32 +00001210 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001211
Sebastian Redl539c5062010-08-18 23:57:32 +00001212 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001213 Error("AST record has invalid code");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001214 return Failure;
1215 }
1216
Douglas Gregor258ae542009-04-27 06:38:32 +00001217 llvm::MemoryBuffer *Buffer
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001218 = llvm::MemoryBuffer::getMemBuffer(StringRef(BlobStart, BlobLen - 1),
Chris Lattner58c79342010-04-05 22:42:27 +00001219 Name);
Douglas Gregor925296b2011-07-19 16:10:42 +00001220 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID,
1221 BaseOffset + Offset);
Mike Stump11289f42009-09-09 15:08:12 +00001222
Douglas Gregore6648fb2009-04-28 20:33:11 +00001223 if (strcmp(Name, "<built-in>") == 0) {
Sebastian Redl75fbb3b2010-07-14 17:49:11 +00001224 PCHPredefinesBlock Block = {
1225 BufferID,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001226 StringRef(BlobStart, BlobLen - 1)
Sebastian Redl75fbb3b2010-07-14 17:49:11 +00001227 };
1228 PCHPredefinesBuffers.push_back(Block);
Douglas Gregore6648fb2009-04-28 20:33:11 +00001229 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001230
1231 break;
1232 }
1233
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001234 case SM_SLOC_EXPANSION_ENTRY: {
Sebastian Redl2c373b92010-10-05 15:59:54 +00001235 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Chandler Carruth115b0772011-07-26 03:03:05 +00001236 SourceMgr.createExpansionLoc(SpellingLoc,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001237 ReadSourceLocation(*F, Record[2]),
1238 ReadSourceLocation(*F, Record[3]),
Douglas Gregor258ae542009-04-27 06:38:32 +00001239 Record[4],
1240 ID,
Douglas Gregor925296b2011-07-19 16:10:42 +00001241 BaseOffset + Record[0]);
Douglas Gregor258ae542009-04-27 06:38:32 +00001242 break;
Mike Stump11289f42009-09-09 15:08:12 +00001243 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001244 }
1245
1246 return Success;
1247}
1248
Douglas Gregor925296b2011-07-19 16:10:42 +00001249/// \brief Find the location where the module F is imported.
Douglas Gregora6895d82011-07-22 16:00:58 +00001250SourceLocation ASTReader::getImportLocation(Module *F) {
Douglas Gregor925296b2011-07-19 16:10:42 +00001251 if (F->ImportLoc.isValid())
1252 return F->ImportLoc;
Jonathan D. Turner10d52012011-07-29 18:09:09 +00001253
Douglas Gregor925296b2011-07-19 16:10:42 +00001254 // Otherwise we have a PCH. It's considered to be "imported" at the first
1255 // location of its includer.
Jonathan D. Turner10d52012011-07-29 18:09:09 +00001256 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
Douglas Gregor925296b2011-07-19 16:10:42 +00001257 // Main file is the importer. We assume that it is the first entry in the
1258 // entry table. We can't ask the manager, because at the time of PCH loading
1259 // the main file entry doesn't exist yet.
1260 // The very first entry is the invalid instantiation loc, which takes up
1261 // offsets 0 and 1.
1262 return SourceLocation::getFromRawEncoding(2U);
1263 }
Jonathan D. Turner10d52012011-07-29 18:09:09 +00001264 //return F->Loaders[0]->FirstLoc;
1265 return F->ImportedBy[0]->FirstLoc;
Douglas Gregor925296b2011-07-19 16:10:42 +00001266}
1267
Chris Lattnere78a6be2009-04-27 01:05:14 +00001268/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1269/// specified cursor. Read the abbreviations that are at the top of the block
1270/// and then leave the cursor pointing into the block.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001271bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattnere78a6be2009-04-27 01:05:14 +00001272 unsigned BlockID) {
1273 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001274 Error("malformed block record in AST file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001275 return Failure;
1276 }
Mike Stump11289f42009-09-09 15:08:12 +00001277
Chris Lattnere78a6be2009-04-27 01:05:14 +00001278 while (true) {
Douglas Gregor796d76a2010-10-20 22:00:55 +00001279 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattnere78a6be2009-04-27 01:05:14 +00001280 unsigned Code = Cursor.ReadCode();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001281
Chris Lattnere78a6be2009-04-27 01:05:14 +00001282 // We expect all abbrevs to be at the start of the block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001283 if (Code != llvm::bitc::DEFINE_ABBREV) {
1284 Cursor.JumpToBit(Offset);
Chris Lattnere78a6be2009-04-27 01:05:14 +00001285 return false;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001286 }
Chris Lattnere78a6be2009-04-27 01:05:14 +00001287 Cursor.ReadAbbrevRecord();
1288 }
1289}
1290
Douglas Gregor7cb0d012011-08-04 18:09:14 +00001291void ASTReader::ReadMacroRecord(Module &F, uint64_t Offset) {
Douglas Gregor796d76a2010-10-20 22:00:55 +00001292 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump11289f42009-09-09 15:08:12 +00001293
Douglas Gregorc3366a52009-04-21 23:56:24 +00001294 // Keep track of where we are in the stream, then jump back there
1295 // after reading this macro.
1296 SavedStreamPosition SavedPosition(Stream);
1297
1298 Stream.JumpToBit(Offset);
1299 RecordData Record;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001300 SmallVector<IdentifierInfo*, 16> MacroArgs;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001301 MacroInfo *Macro = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001302
Douglas Gregorc3366a52009-04-21 23:56:24 +00001303 while (true) {
1304 unsigned Code = Stream.ReadCode();
1305 switch (Code) {
1306 case llvm::bitc::END_BLOCK:
Douglas Gregor7cb0d012011-08-04 18:09:14 +00001307 return;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001308
1309 case llvm::bitc::ENTER_SUBBLOCK:
1310 // No known subblocks, always skip them.
1311 Stream.ReadSubBlockID();
1312 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001313 Error("malformed block record in AST file");
Douglas Gregor7cb0d012011-08-04 18:09:14 +00001314 return;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001315 }
1316 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001317
Douglas Gregorc3366a52009-04-21 23:56:24 +00001318 case llvm::bitc::DEFINE_ABBREV:
1319 Stream.ReadAbbrevRecord();
1320 continue;
1321 default: break;
1322 }
1323
1324 // Read a record.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001325 const char *BlobStart = 0;
1326 unsigned BlobLen = 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001327 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001328 PreprocessorRecordTypes RecType =
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001329 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregor796d76a2010-10-20 22:00:55 +00001330 BlobLen);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001331 switch (RecType) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001332 case PP_MACRO_OBJECT_LIKE:
1333 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001334 // If we already have a macro, that means that we've hit the end
1335 // of the definition of the macro we were looking for. We're
1336 // done.
1337 if (Macro)
Douglas Gregor7cb0d012011-08-04 18:09:14 +00001338 return;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001339
Douglas Gregora3e41532011-07-28 20:55:49 +00001340 IdentifierInfo *II = getLocalIdentifier(F, Record[0]);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001341 if (II == 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001342 Error("macro must have a name in AST file");
Douglas Gregor7cb0d012011-08-04 18:09:14 +00001343 return;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001344 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00001345 SourceLocation Loc = ReadSourceLocation(F, Record[1]);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001346 bool isUsed = Record[2];
Mike Stump11289f42009-09-09 15:08:12 +00001347
Douglas Gregor51825b42011-09-09 22:02:16 +00001348 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001349 MI->setIsUsed(isUsed);
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001350 MI->setIsFromAST();
Mike Stump11289f42009-09-09 15:08:12 +00001351
Douglas Gregoraae92242010-03-19 21:51:54 +00001352 unsigned NextIndex = 3;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001353 MI->setExportLocation(ReadSourceLocation(F, Record, NextIndex));
1354
Sebastian Redl539c5062010-08-18 23:57:32 +00001355 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001356 // Decode function-like macro info.
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001357 bool isC99VarArgs = Record[NextIndex++];
1358 bool isGNUVarArgs = Record[NextIndex++];
Douglas Gregorc3366a52009-04-21 23:56:24 +00001359 MacroArgs.clear();
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001360 unsigned NumArgs = Record[NextIndex++];
Douglas Gregorc3366a52009-04-21 23:56:24 +00001361 for (unsigned i = 0; i != NumArgs; ++i)
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001362 MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++]));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001363
1364 // Install function-like macro info.
1365 MI->setIsFunctionLike();
1366 if (isC99VarArgs) MI->setIsC99Varargs();
1367 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor038c3382009-05-22 22:45:36 +00001368 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Douglas Gregor51825b42011-09-09 22:02:16 +00001369 PP.getPreprocessorAllocator());
Douglas Gregorc3366a52009-04-21 23:56:24 +00001370 }
1371
1372 // Finally, install the macro.
Douglas Gregor51825b42011-09-09 22:02:16 +00001373 PP.setMacroInfo(II, MI);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001374
1375 // Remember that we saw this macro last so that we add the tokens that
1376 // form its body to it.
1377 Macro = MI;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001378
Douglas Gregor51825b42011-09-09 22:02:16 +00001379 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord()) {
Douglas Gregoraae92242010-03-19 21:51:54 +00001380 // We have a macro definition. Load it now.
Douglas Gregor51825b42011-09-09 22:02:16 +00001381 PP.getPreprocessingRecord()->RegisterMacroDefinition(Macro,
Douglas Gregor035611e2011-07-28 22:16:57 +00001382 getLocalMacroDefinition(F, Record[NextIndex]));
Douglas Gregoraae92242010-03-19 21:51:54 +00001383 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001384
Douglas Gregorc3366a52009-04-21 23:56:24 +00001385 ++NumMacrosRead;
1386 break;
1387 }
Mike Stump11289f42009-09-09 15:08:12 +00001388
Sebastian Redl539c5062010-08-18 23:57:32 +00001389 case PP_TOKEN: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001390 // If we see a TOKEN before a PP_MACRO_*, then the file is
1391 // erroneous, just pretend we didn't see this.
1392 if (Macro == 0) break;
Mike Stump11289f42009-09-09 15:08:12 +00001393
Douglas Gregorc3366a52009-04-21 23:56:24 +00001394 Token Tok;
1395 Tok.startToken();
Sebastian Redl2c373b92010-10-05 15:59:54 +00001396 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001397 Tok.setLength(Record[1]);
Douglas Gregora3e41532011-07-28 20:55:49 +00001398 if (IdentifierInfo *II = getLocalIdentifier(F, Record[2]))
Douglas Gregorc3366a52009-04-21 23:56:24 +00001399 Tok.setIdentifierInfo(II);
1400 Tok.setKind((tok::TokenKind)Record[3]);
1401 Tok.setFlag((Token::TokenFlags)Record[4]);
1402 Macro->AddTokenToBody(Tok);
1403 break;
1404 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00001405 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001406 }
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001407
Douglas Gregor7cb0d012011-08-04 18:09:14 +00001408 return;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001409}
1410
Douglas Gregora6895d82011-07-22 16:00:58 +00001411PreprocessedEntity *ASTReader::LoadPreprocessedEntity(Module &F) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00001412 unsigned Code = F.PreprocessorDetailCursor.ReadCode();
1413 switch (Code) {
1414 case llvm::bitc::END_BLOCK:
1415 return 0;
1416
1417 case llvm::bitc::ENTER_SUBBLOCK:
1418 Error("unexpected subblock record in preprocessor detail block");
1419 return 0;
1420
1421 case llvm::bitc::DEFINE_ABBREV:
1422 Error("unexpected abbrevation record in preprocessor detail block");
1423 return 0;
1424
1425 default:
1426 break;
1427 }
1428
Douglas Gregor51825b42011-09-09 22:02:16 +00001429 if (!PP.getPreprocessingRecord()) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00001430 Error("no preprocessing record");
1431 return 0;
1432 }
1433
1434 // Read the record.
Douglas Gregor51825b42011-09-09 22:02:16 +00001435 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
Douglas Gregor92a96f52011-02-08 21:58:10 +00001436 const char *BlobStart = 0;
1437 unsigned BlobLen = 0;
1438 RecordData Record;
1439 PreprocessorDetailRecordTypes RecType =
1440 (PreprocessorDetailRecordTypes)F.PreprocessorDetailCursor.ReadRecord(
1441 Code, Record, BlobStart, BlobLen);
1442 switch (RecType) {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001443 case PPD_MACRO_EXPANSION: {
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001444 PreprocessedEntityID GlobalID = getGlobalPreprocessedEntityID(F, Record[0]);
Douglas Gregor0d4b4312011-08-04 17:06:18 +00001445 if (PreprocessedEntity *PE = PPRec.getLoadedPreprocessedEntity(GlobalID-1))
Douglas Gregor92a96f52011-02-08 21:58:10 +00001446 return PE;
1447
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +00001448 bool isBuiltin = Record[3];
1449 MacroExpansion *ME;
1450 if (isBuiltin)
1451 ME = new (PPRec) MacroExpansion(getLocalIdentifier(F, Record[4]),
Douglas Gregor92a96f52011-02-08 21:58:10 +00001452 SourceRange(ReadSourceLocation(F, Record[1]),
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +00001453 ReadSourceLocation(F, Record[2])));
1454 else
1455 ME = new (PPRec) MacroExpansion(getLocalMacroDefinition(F, Record[4]),
1456 SourceRange(ReadSourceLocation(F, Record[1]),
1457 ReadSourceLocation(F, Record[2])));
Douglas Gregor0d4b4312011-08-04 17:06:18 +00001458 PPRec.setLoadedPreallocatedEntity(GlobalID - 1, ME);
Chandler Carrutha88a22182011-07-14 08:20:46 +00001459 return ME;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001460 }
1461
1462 case PPD_MACRO_DEFINITION: {
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001463 PreprocessedEntityID GlobalID = getGlobalPreprocessedEntityID(F, Record[0]);
Douglas Gregor0d4b4312011-08-04 17:06:18 +00001464 if (PreprocessedEntity *PE = PPRec.getLoadedPreprocessedEntity(GlobalID-1))
Douglas Gregor92a96f52011-02-08 21:58:10 +00001465 return PE;
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001466
1467 unsigned MacroDefID = getGlobalMacroDefinitionID(F, Record[1]);
1468 if (MacroDefID > MacroDefinitionsLoaded.size()) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00001469 Error("out-of-bounds macro definition record");
1470 return 0;
1471 }
1472
1473 // Decode the identifier info and then check again; if the macro is
1474 // still defined and associated with the identifier,
Douglas Gregora3e41532011-07-28 20:55:49 +00001475 IdentifierInfo *II = getLocalIdentifier(F, Record[4]);
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001476 if (!MacroDefinitionsLoaded[MacroDefID - 1]) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00001477 MacroDefinition *MD
1478 = new (PPRec) MacroDefinition(II,
1479 ReadSourceLocation(F, Record[5]),
1480 SourceRange(
1481 ReadSourceLocation(F, Record[2]),
1482 ReadSourceLocation(F, Record[3])));
1483
Douglas Gregor0d4b4312011-08-04 17:06:18 +00001484 PPRec.setLoadedPreallocatedEntity(GlobalID - 1, MD);
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001485 MacroDefinitionsLoaded[MacroDefID - 1] = MD;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001486
1487 if (DeserializationListener)
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001488 DeserializationListener->MacroDefinitionRead(MacroDefID, MD);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001489 }
1490
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001491 return MacroDefinitionsLoaded[MacroDefID - 1];
Douglas Gregor92a96f52011-02-08 21:58:10 +00001492 }
1493
1494 case PPD_INCLUSION_DIRECTIVE: {
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001495 PreprocessedEntityID GlobalID = getGlobalPreprocessedEntityID(F, Record[0]);
Douglas Gregor0d4b4312011-08-04 17:06:18 +00001496 if (PreprocessedEntity *PE = PPRec.getLoadedPreprocessedEntity(GlobalID-1))
Douglas Gregor92a96f52011-02-08 21:58:10 +00001497 return PE;
1498
1499 const char *FullFileNameStart = BlobStart + Record[3];
1500 const FileEntry *File
Douglas Gregor51825b42011-09-09 22:02:16 +00001501 = PP.getFileManager().getFile(StringRef(FullFileNameStart,
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001502 BlobLen - Record[3]));
Douglas Gregor92a96f52011-02-08 21:58:10 +00001503
1504 // FIXME: Stable encoding
1505 InclusionDirective::InclusionKind Kind
1506 = static_cast<InclusionDirective::InclusionKind>(Record[5]);
1507 InclusionDirective *ID
1508 = new (PPRec) InclusionDirective(PPRec, Kind,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001509 StringRef(BlobStart, Record[3]),
Douglas Gregor92a96f52011-02-08 21:58:10 +00001510 Record[4],
1511 File,
1512 SourceRange(ReadSourceLocation(F, Record[1]),
1513 ReadSourceLocation(F, Record[2])));
Douglas Gregor0d4b4312011-08-04 17:06:18 +00001514 PPRec.setLoadedPreallocatedEntity(GlobalID - 1, ID);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001515 return ID;
1516 }
1517 }
1518
1519 Error("invalid offset in preprocessor detail block");
1520 return 0;
1521}
1522
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001523PreprocessedEntityID
1524ASTReader::getGlobalPreprocessedEntityID(Module &M, unsigned LocalID) {
Douglas Gregor2f555fc2011-08-04 18:56:47 +00001525 ContinuousRangeMap<uint32_t, int, 2>::iterator
1526 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1527 assert(I != M.PreprocessedEntityRemap.end()
1528 && "Invalid index into preprocessed entity index remap");
1529
1530 return LocalID + I->second;
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001531}
1532
Douglas Gregord44252e2011-08-25 20:47:51 +00001533unsigned HeaderFileInfoTrait::ComputeHash(const char *path) {
1534 return llvm::HashString(llvm::sys::path::filename(path));
Douglas Gregor09b69892011-02-10 17:09:37 +00001535}
Douglas Gregord44252e2011-08-25 20:47:51 +00001536
1537HeaderFileInfoTrait::internal_key_type
1538HeaderFileInfoTrait::GetInternalKey(const char *path) { return path; }
1539
1540bool HeaderFileInfoTrait::EqualKey(internal_key_type a, internal_key_type b) {
1541 if (strcmp(a, b) == 0)
1542 return true;
1543
1544 if (llvm::sys::path::filename(a) != llvm::sys::path::filename(b))
1545 return false;
1546
1547 // The file names match, but the path names don't. stat() the files to
1548 // see if they are the same.
1549 struct stat StatBufA, StatBufB;
1550 if (StatSimpleCache(a, &StatBufA) || StatSimpleCache(b, &StatBufB))
1551 return false;
1552
1553 return StatBufA.st_ino == StatBufB.st_ino;
1554}
1555
1556std::pair<unsigned, unsigned>
1557HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1558 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1559 unsigned DataLen = (unsigned) *d++;
1560 return std::make_pair(KeyLen + 1, DataLen);
1561}
1562
1563HeaderFileInfoTrait::data_type
1564HeaderFileInfoTrait::ReadData(const internal_key_type, const unsigned char *d,
1565 unsigned DataLen) {
1566 const unsigned char *End = d + DataLen;
1567 using namespace clang::io;
1568 HeaderFileInfo HFI;
1569 unsigned Flags = *d++;
1570 HFI.isImport = (Flags >> 5) & 0x01;
1571 HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1572 HFI.DirInfo = (Flags >> 2) & 0x03;
1573 HFI.Resolved = (Flags >> 1) & 0x01;
1574 HFI.IndexHeaderMapHeader = Flags & 0x01;
1575 HFI.NumIncludes = ReadUnalignedLE16(d);
1576 HFI.ControllingMacroID = Reader.getGlobalDeclID(M, ReadUnalignedLE32(d));
1577 if (unsigned FrameworkOffset = ReadUnalignedLE32(d)) {
1578 // The framework offset is 1 greater than the actual offset,
1579 // since 0 is used as an indicator for "no framework name".
1580 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1581 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1582 }
1583
1584 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1585 (void)End;
1586
1587 // This HeaderFileInfo was externally loaded.
1588 HFI.External = true;
1589 return HFI;
1590}
Douglas Gregor09b69892011-02-10 17:09:37 +00001591
Douglas Gregora6895d82011-07-22 16:00:58 +00001592void ASTReader::SetIdentifierIsMacro(IdentifierInfo *II, Module &F,
Douglas Gregor074fdc52011-07-28 21:16:51 +00001593 uint64_t LocalOffset) {
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001594 // Note that this identifier has a macro definition.
1595 II->setHasMacroDefinition(true);
1596
Douglas Gregord32f0352011-07-22 06:10:01 +00001597 // Adjust the offset to a global offset.
Douglas Gregor074fdc52011-07-28 21:16:51 +00001598 UnreadMacroRecordOffsets[II] = F.GlobalBitOffset + LocalOffset;
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001599}
1600
Sebastian Redl2c499f62010-08-18 23:56:43 +00001601void ASTReader::ReadDefinedMacros() {
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00001602 for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1603 E = ModuleMgr.rend(); I != E; ++I) {
1604 llvm::BitstreamCursor &MacroCursor = (*I)->MacroCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001605
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001606 // If there was no preprocessor block, skip this file.
1607 if (!MacroCursor.getBitStreamReader())
1608 continue;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001609
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001610 llvm::BitstreamCursor Cursor = MacroCursor;
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00001611 Cursor.JumpToBit((*I)->MacroStartOffset);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001612
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001613 RecordData Record;
1614 while (true) {
1615 unsigned Code = Cursor.ReadCode();
Douglas Gregor796d76a2010-10-20 22:00:55 +00001616 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001617 break;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001618
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001619 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1620 // No known subblocks, always skip them.
1621 Cursor.ReadSubBlockID();
1622 if (Cursor.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001623 Error("malformed block record in AST file");
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001624 return;
1625 }
1626 continue;
1627 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001628
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001629 if (Code == llvm::bitc::DEFINE_ABBREV) {
1630 Cursor.ReadAbbrevRecord();
1631 continue;
1632 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001633
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001634 // Read a record.
1635 const char *BlobStart;
1636 unsigned BlobLen;
1637 Record.clear();
1638 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1639 default: // Default behavior: ignore.
1640 break;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001641
Sebastian Redl539c5062010-08-18 23:57:32 +00001642 case PP_MACRO_OBJECT_LIKE:
1643 case PP_MACRO_FUNCTION_LIKE:
Douglas Gregora3e41532011-07-28 20:55:49 +00001644 getLocalIdentifier(**I, Record[0]);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001645 break;
1646
Sebastian Redl539c5062010-08-18 23:57:32 +00001647 case PP_TOKEN:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001648 // Ignore tokens.
1649 break;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001650 }
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001651 }
1652 }
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001653
1654 // Drain the unread macro-record offsets map.
1655 while (!UnreadMacroRecordOffsets.empty())
1656 LoadMacroDefinition(UnreadMacroRecordOffsets.begin());
1657}
1658
1659void ASTReader::LoadMacroDefinition(
1660 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos) {
1661 assert(Pos != UnreadMacroRecordOffsets.end() && "Unknown macro definition");
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001662 uint64_t Offset = Pos->second;
1663 UnreadMacroRecordOffsets.erase(Pos);
1664
Douglas Gregord32f0352011-07-22 06:10:01 +00001665 RecordLocation Loc = getLocalBitOffset(Offset);
1666 ReadMacroRecord(*Loc.F, Loc.Offset);
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001667}
1668
1669void ASTReader::LoadMacroDefinition(IdentifierInfo *II) {
1670 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos
1671 = UnreadMacroRecordOffsets.find(II);
1672 LoadMacroDefinition(Pos);
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001673}
1674
Sebastian Redl50e26582010-09-15 19:54:06 +00001675MacroDefinition *ASTReader::getMacroDefinition(MacroID ID) {
Douglas Gregor91096292010-10-02 19:29:26 +00001676 if (ID == 0 || ID > MacroDefinitionsLoaded.size())
Douglas Gregoraae92242010-03-19 21:51:54 +00001677 return 0;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001678
Douglas Gregor91096292010-10-02 19:29:26 +00001679 if (!MacroDefinitionsLoaded[ID - 1]) {
Douglas Gregor270e0142011-07-20 01:29:15 +00001680 GlobalMacroDefinitionMapType::iterator I =GlobalMacroDefinitionMap.find(ID);
1681 assert(I != GlobalMacroDefinitionMap.end() &&
1682 "Corrupted global macro definition map");
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00001683 Module &M = *I->second;
1684 unsigned Index = ID - 1 - M.BaseMacroDefinitionID;
1685 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
1686 M.PreprocessorDetailCursor.JumpToBit(M.MacroDefinitionOffsets[Index]);
1687 LoadPreprocessedEntity(M);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001688 }
1689
Douglas Gregor91096292010-10-02 19:29:26 +00001690 return MacroDefinitionsLoaded[ID - 1];
Douglas Gregoraae92242010-03-19 21:51:54 +00001691}
1692
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001693const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) {
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00001694 std::string Filename = filenameStrRef;
1695 MaybeAddSystemRootToFilename(Filename);
1696 const FileEntry *File = FileMgr.getFile(Filename);
1697 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1698 OriginalDir != CurrentDir) {
1699 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1700 OriginalDir,
1701 CurrentDir);
1702 if (!resolved.empty())
1703 File = FileMgr.getFile(resolved);
1704 }
1705
1706 return File;
1707}
1708
Douglas Gregor035611e2011-07-28 22:16:57 +00001709MacroID ASTReader::getGlobalMacroDefinitionID(Module &M, unsigned LocalID) {
Douglas Gregora863b4b2011-08-04 16:36:56 +00001710 if (LocalID < NUM_PREDEF_MACRO_IDS)
1711 return LocalID;
1712
1713 ContinuousRangeMap<uint32_t, int, 2>::iterator I
1714 = M.MacroDefinitionRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
1715 assert(I != M.MacroDefinitionRemap.end() &&
1716 "Invalid index into macro definition ID remap");
1717
1718 return LocalID + I->second;
Douglas Gregor035611e2011-07-28 22:16:57 +00001719}
1720
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001721/// \brief If we are loading a relocatable PCH file, and the filename is
1722/// not an absolute path, add the system root to the beginning of the file
1723/// name.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001724void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001725 // If this is not a relocatable PCH file, there's nothing to do.
1726 if (!RelocatablePCH)
1727 return;
Mike Stump11289f42009-09-09 15:08:12 +00001728
Michael J. Spencerf28df4c2010-12-17 21:22:22 +00001729 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001730 return;
1731
Douglas Gregorc567ba22011-07-22 16:35:34 +00001732 if (isysroot.empty()) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001733 // If no system root was given, default to '/'
1734 Filename.insert(Filename.begin(), '/');
1735 return;
1736 }
Mike Stump11289f42009-09-09 15:08:12 +00001737
Douglas Gregorc567ba22011-07-22 16:35:34 +00001738 unsigned Length = isysroot.size();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001739 if (isysroot[Length - 1] != '/')
1740 Filename.insert(Filename.begin(), '/');
Mike Stump11289f42009-09-09 15:08:12 +00001741
Douglas Gregorc567ba22011-07-22 16:35:34 +00001742 Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001743}
1744
Sebastian Redl2c499f62010-08-18 23:56:43 +00001745ASTReader::ASTReadResult
Douglas Gregora6895d82011-07-22 16:00:58 +00001746ASTReader::ReadASTBlock(Module &F) {
Sebastian Redl34522812010-07-16 17:50:48 +00001747 llvm::BitstreamCursor &Stream = F.Stream;
1748
Sebastian Redl539c5062010-08-18 23:57:32 +00001749 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001750 Error("malformed block record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001751 return Failure;
1752 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001753
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001754 // Read all of the records and blocks for the ASt file.
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001755 RecordData Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001756 while (!Stream.AtEndOfStream()) {
1757 unsigned Code = Stream.ReadCode();
1758 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor55abb232009-04-10 20:39:37 +00001759 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001760 Error("error at end of module block in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001761 return Failure;
1762 }
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001763
Douglas Gregor55abb232009-04-10 20:39:37 +00001764 return Success;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001765 }
1766
1767 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1768 switch (Stream.ReadSubBlockID()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001769 case DECLTYPES_BLOCK_ID:
Chris Lattnere78a6be2009-04-27 01:05:14 +00001770 // We lazily load the decls block, but we want to set up the
1771 // DeclsCursor cursor to point into it. Clone our current bitcode
1772 // cursor to it, enter the block and read the abbrevs in that block.
1773 // With the main cursor, we just skip over it.
Sebastian Redl34522812010-07-16 17:50:48 +00001774 F.DeclsCursor = Stream;
Chris Lattnere78a6be2009-04-27 01:05:14 +00001775 if (Stream.SkipBlock() || // Skip with the main cursor.
1776 // Read the abbrevs.
Sebastian Redl539c5062010-08-18 23:57:32 +00001777 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001778 Error("malformed block record in AST file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001779 return Failure;
1780 }
1781 break;
Mike Stump11289f42009-09-09 15:08:12 +00001782
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00001783 case DECL_UPDATES_BLOCK_ID:
1784 if (Stream.SkipBlock()) {
1785 Error("malformed block record in AST file");
1786 return Failure;
1787 }
1788 break;
1789
Sebastian Redl539c5062010-08-18 23:57:32 +00001790 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl34522812010-07-16 17:50:48 +00001791 F.MacroCursor = Stream;
Douglas Gregor51825b42011-09-09 22:02:16 +00001792 if (!PP.getExternalSource())
1793 PP.setExternalSource(this);
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001794
Douglas Gregor796d76a2010-10-20 22:00:55 +00001795 if (Stream.SkipBlock() ||
1796 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001797 Error("malformed block record in AST file");
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001798 return Failure;
1799 }
Douglas Gregor796d76a2010-10-20 22:00:55 +00001800 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001801 break;
Steve Naroff2ddea052009-04-23 10:39:46 +00001802
Douglas Gregor92a96f52011-02-08 21:58:10 +00001803 case PREPROCESSOR_DETAIL_BLOCK_ID:
1804 F.PreprocessorDetailCursor = Stream;
1805 if (Stream.SkipBlock() ||
1806 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
1807 PREPROCESSOR_DETAIL_BLOCK_ID)) {
1808 Error("malformed preprocessor detail record in AST file");
1809 return Failure;
1810 }
1811 F.PreprocessorDetailStartOffset
1812 = F.PreprocessorDetailCursor.GetCurrentBitNo();
Douglas Gregor51825b42011-09-09 22:02:16 +00001813
1814 if (!PP.getPreprocessingRecord())
1815 PP.createPreprocessingRecord(true);
1816 if (!PP.getPreprocessingRecord()->getExternalSource())
1817 PP.getPreprocessingRecord()->SetExternalSource(*this);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001818 break;
1819
Sebastian Redl539c5062010-08-18 23:57:32 +00001820 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl393f8b72010-07-19 20:52:06 +00001821 switch (ReadSourceManagerBlock(F)) {
Douglas Gregor92863e42009-04-10 23:10:45 +00001822 case Success:
1823 break;
1824
1825 case Failure:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001826 Error("malformed source manager block in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001827 return Failure;
Douglas Gregor92863e42009-04-10 23:10:45 +00001828
1829 case IgnorePCH:
1830 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00001831 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001832 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001833 }
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001834 continue;
1835 }
1836
1837 if (Code == llvm::bitc::DEFINE_ABBREV) {
1838 Stream.ReadAbbrevRecord();
1839 continue;
1840 }
1841
1842 // Read and process a record.
1843 Record.clear();
Douglas Gregorbfbde532009-04-10 21:16:55 +00001844 const char *BlobStart = 0;
1845 unsigned BlobLen = 0;
Sebastian Redl539c5062010-08-18 23:57:32 +00001846 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001847 &BlobStart, &BlobLen)) {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001848 default: // Default behavior: ignore.
1849 break;
1850
Sebastian Redl539c5062010-08-18 23:57:32 +00001851 case METADATA: {
1852 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1853 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001854 : diag::warn_pch_version_too_new);
1855 return IgnorePCH;
1856 }
1857
1858 RelocatablePCH = Record[4];
1859 if (Listener) {
1860 std::string TargetTriple(BlobStart, BlobLen);
1861 if (Listener->ReadTargetTriple(TargetTriple))
1862 return IgnorePCH;
1863 }
1864 break;
1865 }
1866
Douglas Gregor29cc6422011-08-17 21:07:30 +00001867 case IMPORTS: {
1868 // Load each of the imported PCH files.
1869 unsigned Idx = 0, N = Record.size();
1870 while (Idx < N) {
1871 // Read information about the AST file.
1872 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
1873 unsigned Length = Record[Idx++];
1874 llvm::SmallString<128> ImportedFile(Record.begin() + Idx,
1875 Record.begin() + Idx + Length);
1876 Idx += Length;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001877
Douglas Gregor29cc6422011-08-17 21:07:30 +00001878 // Load the AST file.
Douglas Gregordf0c1512011-08-18 04:12:04 +00001879 switch(ReadASTCore(ImportedFile, ImportedKind, &F)) {
Douglas Gregor29cc6422011-08-17 21:07:30 +00001880 case Failure: return Failure;
1881 // If we have to ignore the dependency, we'll have to ignore this too.
1882 case IgnorePCH: return IgnorePCH;
1883 case Success: break;
1884 }
1885 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001886 break;
1887 }
1888
Douglas Gregor5204bde2011-08-02 16:26:37 +00001889 case TYPE_OFFSET: {
Sebastian Redl9e687992010-07-19 22:06:55 +00001890 if (F.LocalNumTypes != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001891 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001892 return Failure;
1893 }
Sebastian Redl9e687992010-07-19 22:06:55 +00001894 F.TypeOffsets = (const uint32_t *)BlobStart;
1895 F.LocalNumTypes = Record[0];
Douglas Gregor3b65ed02011-08-02 18:32:54 +00001896 unsigned LocalBaseTypeIndex = Record[1];
1897 F.BaseTypeIndex = getTotalNumTypes();
Douglas Gregor8ab4ea82011-07-29 00:21:44 +00001898
Douglas Gregor5204bde2011-08-02 16:26:37 +00001899 if (F.LocalNumTypes > 0) {
1900 // Introduce the global -> local mapping for types within this module.
1901 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
1902
1903 // Introduce the local -> global mapping for types within this module.
Douglas Gregor3b65ed02011-08-02 18:32:54 +00001904 F.TypeRemap.insert(std::make_pair(LocalBaseTypeIndex,
1905 F.BaseTypeIndex - LocalBaseTypeIndex));
Douglas Gregor5204bde2011-08-02 16:26:37 +00001906
1907 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
1908 }
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001909 break;
Douglas Gregor5204bde2011-08-02 16:26:37 +00001910 }
1911
Douglas Gregorf7180622011-08-03 15:48:04 +00001912 case DECL_OFFSET: {
Sebastian Redl9e687992010-07-19 22:06:55 +00001913 if (F.LocalNumDecls != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001914 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001915 return Failure;
1916 }
Sebastian Redl9e687992010-07-19 22:06:55 +00001917 F.DeclOffsets = (const uint32_t *)BlobStart;
1918 F.LocalNumDecls = Record[0];
Douglas Gregorf7180622011-08-03 15:48:04 +00001919 unsigned LocalBaseDeclID = Record[1];
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00001920 F.BaseDeclID = getTotalNumDecls();
Douglas Gregor047d2ef2011-07-20 00:27:43 +00001921
Douglas Gregorf7180622011-08-03 15:48:04 +00001922 if (F.LocalNumDecls > 0) {
1923 // Introduce the global -> local mapping for declarations within this
1924 // module.
Douglas Gregordab42432011-08-12 00:15:20 +00001925 GlobalDeclMap.insert(
1926 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
Douglas Gregorf7180622011-08-03 15:48:04 +00001927
1928 // Introduce the local -> global mapping for declarations within this
1929 // module.
1930 F.DeclRemap.insert(std::make_pair(LocalBaseDeclID,
1931 F.BaseDeclID - LocalBaseDeclID));
1932
1933 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
1934 }
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001935 break;
Douglas Gregorf7180622011-08-03 15:48:04 +00001936 }
1937
Sebastian Redl539c5062010-08-18 23:57:32 +00001938 case TU_UPDATE_LEXICAL: {
Douglas Gregor4163aca2011-09-09 21:34:22 +00001939 DeclContext *TU = Context.getTranslationUnitDecl();
Douglas Gregor94619c82011-08-24 19:03:07 +00001940 DeclContextInfo &Info = F.DeclContextInfos[TU];
1941 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(BlobStart);
1942 Info.NumLexicalDecls
1943 = static_cast<unsigned int>(BlobLen / sizeof(KindDeclIDPair));
Douglas Gregor4163aca2011-09-09 21:34:22 +00001944 TU->setHasExternalLexicalStorage(true);
Sebastian Redl4b1f4902010-07-27 18:24:41 +00001945 break;
1946 }
1947
Sebastian Redld7dce0a2010-08-24 00:50:04 +00001948 case UPDATE_VISIBLE: {
Douglas Gregorf7180622011-08-03 15:48:04 +00001949 unsigned Idx = 0;
1950 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
Sebastian Redld7dce0a2010-08-24 00:50:04 +00001951 void *Table = ASTDeclContextNameLookupTable::Create(
Douglas Gregorf7180622011-08-03 15:48:04 +00001952 (const unsigned char *)BlobStart + Record[Idx++],
Sebastian Redld7dce0a2010-08-24 00:50:04 +00001953 (const unsigned char *)BlobStart,
Douglas Gregor903b7e92011-07-22 00:38:23 +00001954 ASTDeclContextNameLookupTrait(*this, F));
Douglas Gregor4163aca2011-09-09 21:34:22 +00001955 if (ID == PREDEF_DECL_TRANSLATION_UNIT_ID) { // Is it the TU?
1956 DeclContext *TU = Context.getTranslationUnitDecl();
Douglas Gregor94619c82011-08-24 19:03:07 +00001957 F.DeclContextInfos[TU].NameLookupTableData = Table;
Douglas Gregordab42432011-08-12 00:15:20 +00001958 TU->setHasExternalVisibleStorage(true);
Sebastian Redld7dce0a2010-08-24 00:50:04 +00001959 } else
Douglas Gregorf7180622011-08-03 15:48:04 +00001960 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
Sebastian Redld7dce0a2010-08-24 00:50:04 +00001961 break;
1962 }
1963
Sebastian Redl539c5062010-08-18 23:57:32 +00001964 case REDECLS_UPDATE_LATEST: {
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00001965 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
Douglas Gregorf7180622011-08-03 15:48:04 +00001966 for (unsigned i = 0, e = Record.size(); i < e; /* in loop */) {
1967 DeclID First = ReadDeclID(F, Record, i);
1968 DeclID Latest = ReadDeclID(F, Record, i);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00001969 FirstLatestDeclIDs[First] = Latest;
1970 }
1971 break;
1972 }
1973
Sebastian Redl539c5062010-08-18 23:57:32 +00001974 case LANGUAGE_OPTIONS:
Douglas Gregorce3a8292010-07-27 00:27:13 +00001975 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor55abb232009-04-10 20:39:37 +00001976 return IgnorePCH;
1977 break;
Douglas Gregorbfbde532009-04-10 21:16:55 +00001978
Sebastian Redl539c5062010-08-18 23:57:32 +00001979 case IDENTIFIER_TABLE:
Sebastian Redl393f8b72010-07-19 20:52:06 +00001980 F.IdentifierTableData = BlobStart;
Douglas Gregor0e149972009-04-25 19:10:14 +00001981 if (Record[0]) {
Sebastian Redl393f8b72010-07-19 20:52:06 +00001982 F.IdentifierLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001983 = ASTIdentifierLookupTable::Create(
Sebastian Redl393f8b72010-07-19 20:52:06 +00001984 (const unsigned char *)F.IdentifierTableData + Record[0],
1985 (const unsigned char *)F.IdentifierTableData,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001986 ASTIdentifierLookupTrait(*this, F));
Douglas Gregor51825b42011-09-09 22:02:16 +00001987
1988 PP.getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor0e149972009-04-25 19:10:14 +00001989 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001990 break;
1991
Douglas Gregor1ab036c2011-08-03 21:49:18 +00001992 case IDENTIFIER_OFFSET: {
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00001993 if (F.LocalNumIdentifiers != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001994 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001995 return Failure;
1996 }
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00001997 F.IdentifierOffsets = (const uint32_t *)BlobStart;
1998 F.LocalNumIdentifiers = Record[0];
Douglas Gregor1ab036c2011-08-03 21:49:18 +00001999 unsigned LocalBaseIdentifierID = Record[1];
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00002000 F.BaseIdentifierID = getTotalNumIdentifiers();
Douglas Gregor19d26352011-07-20 00:59:32 +00002001
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002002 if (F.LocalNumIdentifiers > 0) {
2003 // Introduce the global -> local mapping for identifiers within this
2004 // module.
2005 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
2006 &F));
2007
2008 // Introduce the local -> global mapping for identifiers within this
2009 // module.
2010 F.IdentifierRemap.insert(
2011 std::make_pair(LocalBaseIdentifierID,
2012 F.BaseIdentifierID - LocalBaseIdentifierID));
2013
2014 IdentifiersLoaded.resize(IdentifiersLoaded.size()
2015 + F.LocalNumIdentifiers);
2016 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002017 break;
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002018 }
2019
Sebastian Redl539c5062010-08-18 23:57:32 +00002020 case EXTERNAL_DEFINITIONS:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002021 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2022 ExternalDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002023 break;
Douglas Gregor08f01292009-04-17 22:13:46 +00002024
Sebastian Redl539c5062010-08-18 23:57:32 +00002025 case SPECIAL_TYPES:
Douglas Gregor903b7e92011-07-22 00:38:23 +00002026 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2027 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
Douglas Gregor652d82a2009-04-18 05:55:16 +00002028 break;
2029
Sebastian Redl539c5062010-08-18 23:57:32 +00002030 case STATISTICS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002031 TotalNumStatements += Record[0];
2032 TotalNumMacros += Record[1];
2033 TotalLexicalDeclContexts += Record[2];
2034 TotalVisibleDeclContexts += Record[3];
Douglas Gregor08f01292009-04-17 22:13:46 +00002035 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00002036
Sebastian Redl539c5062010-08-18 23:57:32 +00002037 case UNUSED_FILESCOPED_DECLS:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002038 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2039 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
Tanya Lattner90073802010-02-12 00:07:30 +00002040 break;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00002041
Alexis Hunt27a761d2011-05-04 23:29:54 +00002042 case DELEGATING_CTORS:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002043 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2044 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
Alexis Hunt27a761d2011-05-04 23:29:54 +00002045 break;
2046
Sebastian Redl539c5062010-08-18 23:57:32 +00002047 case WEAK_UNDECLARED_IDENTIFIERS:
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00002048 if (Record.size() % 4 != 0) {
2049 Error("invalid weak identifiers record");
2050 return Failure;
2051 }
2052
2053 // FIXME: Ignore weak undeclared identifiers from non-original PCH
2054 // files. This isn't the way to do it :)
2055 WeakUndeclaredIdentifiers.clear();
2056
2057 // Translate the weak, undeclared identifiers into global IDs.
2058 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2059 WeakUndeclaredIdentifiers.push_back(
2060 getGlobalIdentifierID(F, Record[I++]));
2061 WeakUndeclaredIdentifiers.push_back(
2062 getGlobalIdentifierID(F, Record[I++]));
2063 WeakUndeclaredIdentifiers.push_back(
2064 ReadSourceLocation(F, Record, I).getRawEncoding());
2065 WeakUndeclaredIdentifiers.push_back(Record[I++]);
2066 }
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002067 break;
2068
Sebastian Redl539c5062010-08-18 23:57:32 +00002069 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002070 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2071 LocallyScopedExternalDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002072 break;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002073
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002074 case SELECTOR_OFFSETS: {
Sebastian Redla19a67f2010-08-03 21:58:15 +00002075 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redlada023c2010-08-04 20:40:17 +00002076 F.LocalNumSelectors = Record[0];
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002077 unsigned LocalBaseSelectorID = Record[1];
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00002078 F.BaseSelectorID = getTotalNumSelectors();
Douglas Gregor2262d282011-07-20 01:10:58 +00002079
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002080 if (F.LocalNumSelectors > 0) {
2081 // Introduce the global -> local mapping for selectors within this
2082 // module.
2083 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2084
2085 // Introduce the local -> global mapping for selectors within this
2086 // module.
2087 F.SelectorRemap.insert(std::make_pair(LocalBaseSelectorID,
2088 F.BaseSelectorID - LocalBaseSelectorID));
Douglas Gregor95c13f52009-04-25 17:48:32 +00002089
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002090 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
2091 }
2092 break;
2093 }
2094
Sebastian Redl539c5062010-08-18 23:57:32 +00002095 case METHOD_POOL:
Sebastian Redlada023c2010-08-04 20:40:17 +00002096 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002097 if (Record[0])
Sebastian Redlada023c2010-08-04 20:40:17 +00002098 F.SelectorLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002099 = ASTSelectorLookupTable::Create(
Sebastian Redlada023c2010-08-04 20:40:17 +00002100 F.SelectorLookupTableData + Record[0],
2101 F.SelectorLookupTableData,
Douglas Gregor7fb09192011-07-21 22:35:25 +00002102 ASTSelectorLookupTrait(*this, F));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002103 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorc78d3462009-04-24 21:10:55 +00002104 break;
Douglas Gregoreda6a892009-04-26 00:07:37 +00002105
Sebastian Redl96371b42010-09-22 00:42:30 +00002106 case REFERENCED_SELECTOR_POOL:
Douglas Gregor3f8f04f2011-07-28 14:41:43 +00002107 if (!Record.empty()) {
2108 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2109 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2110 Record[Idx++]));
2111 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2112 getRawEncoding());
2113 }
2114 }
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002115 break;
2116
Sebastian Redl539c5062010-08-18 23:57:32 +00002117 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002118 if (!Record.empty() && Listener)
2119 Listener->ReadCounter(Record[0]);
Douglas Gregoreda6a892009-04-26 00:07:37 +00002120 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00002121
Douglas Gregor925296b2011-07-19 16:10:42 +00002122 case SOURCE_LOCATION_OFFSETS: {
2123 F.SLocEntryOffsets = (const uint32_t *)BlobStart;
Sebastian Redlb293a452010-07-20 21:20:32 +00002124 F.LocalNumSLocEntries = Record[0];
Douglas Gregor925296b2011-07-19 16:10:42 +00002125 llvm::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
2126 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries, Record[1]);
2127 // Make our entry in the range map. BaseID is negative and growing, so
2128 // we invert it. Because we invert it, though, we need the other end of
2129 // the range.
2130 unsigned RangeStart =
2131 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2132 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2133 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2134
2135 // Initialize the remapping table.
2136 // Invalid stays invalid.
2137 F.SLocRemap.insert(std::make_pair(0U, 0));
2138 // This module. Base was 2 when being compiled.
2139 F.SLocRemap.insert(std::make_pair(2U,
2140 static_cast<int>(F.SLocEntryBaseOffset - 2)));
Douglas Gregor49bf76b2011-07-21 18:46:38 +00002141
2142 TotalNumSLocEntries += F.LocalNumSLocEntries;
Douglas Gregor925296b2011-07-19 16:10:42 +00002143 break;
2144 }
2145
Douglas Gregor5a1797c2011-08-01 16:01:55 +00002146 case MODULE_OFFSET_MAP: {
Douglas Gregor925296b2011-07-19 16:10:42 +00002147 // Additional remapping information.
2148 const unsigned char *Data = (const unsigned char*)BlobStart;
2149 const unsigned char *DataEnd = Data + BlobLen;
Douglas Gregor00659902011-08-02 10:56:51 +00002150
2151 // Continuous range maps we may be updating in our module.
2152 ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap);
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002153 ContinuousRangeMap<uint32_t, int, 2>::Builder
2154 IdentifierRemap(F.IdentifierRemap);
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002155 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002156 PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2157 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregora863b4b2011-08-04 16:36:56 +00002158 MacroDefinitionRemap(F.MacroDefinitionRemap);
2159 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002160 SelectorRemap(F.SelectorRemap);
Douglas Gregorf7180622011-08-03 15:48:04 +00002161 ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap);
Douglas Gregor5204bde2011-08-02 16:26:37 +00002162 ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap);
2163
Douglas Gregor925296b2011-07-19 16:10:42 +00002164 while(Data < DataEnd) {
Douglas Gregor925296b2011-07-19 16:10:42 +00002165 uint16_t Len = io::ReadUnalignedLE16(Data);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002166 StringRef Name = StringRef((const char*)Data, Len);
Douglas Gregor00659902011-08-02 10:56:51 +00002167 Data += Len;
Jonathan D. Turnerb2b08232011-07-26 18:21:30 +00002168 Module *OM = ModuleMgr.lookup(Name);
Douglas Gregor925296b2011-07-19 16:10:42 +00002169 if (!OM) {
2170 Error("SourceLocation remap refers to unknown module");
2171 return Failure;
2172 }
Douglas Gregor00659902011-08-02 10:56:51 +00002173
2174 uint32_t SLocOffset = io::ReadUnalignedLE32(Data);
2175 uint32_t IdentifierIDOffset = io::ReadUnalignedLE32(Data);
2176 uint32_t PreprocessedEntityIDOffset = io::ReadUnalignedLE32(Data);
2177 uint32_t MacroDefinitionIDOffset = io::ReadUnalignedLE32(Data);
2178 uint32_t SelectorIDOffset = io::ReadUnalignedLE32(Data);
2179 uint32_t DeclIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregor5204bde2011-08-02 16:26:37 +00002180 uint32_t TypeIndexOffset = io::ReadUnalignedLE32(Data);
Douglas Gregor00659902011-08-02 10:56:51 +00002181
2182 // Source location offset is mapped to OM->SLocEntryBaseOffset.
2183 SLocRemap.insert(std::make_pair(SLocOffset,
2184 static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset)));
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002185 IdentifierRemap.insert(
2186 std::make_pair(IdentifierIDOffset,
2187 OM->BaseIdentifierID - IdentifierIDOffset));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002188 PreprocessedEntityRemap.insert(
2189 std::make_pair(PreprocessedEntityIDOffset,
2190 OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset));
Douglas Gregora863b4b2011-08-04 16:36:56 +00002191 MacroDefinitionRemap.insert(
2192 std::make_pair(MacroDefinitionIDOffset,
2193 OM->BaseMacroDefinitionID - MacroDefinitionIDOffset));
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002194 SelectorRemap.insert(std::make_pair(SelectorIDOffset,
2195 OM->BaseSelectorID - SelectorIDOffset));
Douglas Gregorf7180622011-08-03 15:48:04 +00002196 DeclRemap.insert(std::make_pair(DeclIDOffset,
2197 OM->BaseDeclID - DeclIDOffset));
2198
Douglas Gregor5204bde2011-08-02 16:26:37 +00002199 TypeRemap.insert(std::make_pair(TypeIndexOffset,
Douglas Gregor3b65ed02011-08-02 18:32:54 +00002200 OM->BaseTypeIndex - TypeIndexOffset));
Douglas Gregor925296b2011-07-19 16:10:42 +00002201 }
2202 break;
2203 }
2204
Douglas Gregor925296b2011-07-19 16:10:42 +00002205 case SOURCE_MANAGER_LINE_TABLE:
2206 if (ParseLineTable(F, Record))
2207 return Failure;
Douglas Gregor258ae542009-04-27 06:38:32 +00002208 break;
2209
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00002210 case FILE_SOURCE_LOCATION_OFFSETS:
2211 F.SLocFileOffsets = (const uint32_t *)BlobStart;
2212 F.LocalNumSLocFileEntries = Record[0];
2213 break;
2214
Douglas Gregor925296b2011-07-19 16:10:42 +00002215 case SOURCE_LOCATION_PRELOADS: {
2216 // Need to transform from the local view (1-based IDs) to the global view,
2217 // which is based off F.SLocEntryBaseID.
Douglas Gregora918bab2011-08-25 21:09:44 +00002218 if (!F.PreloadSLocEntries.empty()) {
2219 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
2220 return Failure;
2221 }
2222
2223 F.PreloadSLocEntries.swap(Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002224 break;
Douglas Gregor925296b2011-07-19 16:10:42 +00002225 }
Douglas Gregorc5046832009-04-27 18:38:38 +00002226
Sebastian Redl539c5062010-08-18 23:57:32 +00002227 case STAT_CACHE: {
Douglas Gregor606c4ac2011-02-05 19:42:43 +00002228 if (!DisableStatCache) {
2229 ASTStatCache *MyStatCache =
2230 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
2231 (const unsigned char *)BlobStart,
2232 NumStatHits, NumStatMisses);
2233 FileMgr.addStatCache(MyStatCache);
2234 F.StatCache = MyStatCache;
2235 }
Douglas Gregorc5046832009-04-27 18:38:38 +00002236 break;
Douglas Gregord2eb58a2009-10-16 18:18:30 +00002237 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00002238
Sebastian Redl539c5062010-08-18 23:57:32 +00002239 case EXT_VECTOR_DECLS:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002240 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2241 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002242 break;
2243
Sebastian Redl539c5062010-08-18 23:57:32 +00002244 case VTABLE_USES:
Douglas Gregor4daf6a32011-07-28 19:11:31 +00002245 if (Record.size() % 3 != 0) {
2246 Error("Invalid VTABLE_USES record");
2247 return Failure;
2248 }
2249
Sebastian Redl08aca90252010-08-05 18:21:25 +00002250 // Later tables overwrite earlier ones.
Douglas Gregor4daf6a32011-07-28 19:11:31 +00002251 // FIXME: Modules will have some trouble with this. This is clearly not
2252 // the right way to do this.
Douglas Gregor7fb09192011-07-21 22:35:25 +00002253 VTableUses.clear();
Douglas Gregor4daf6a32011-07-28 19:11:31 +00002254
2255 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
2256 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
2257 VTableUses.push_back(
2258 ReadSourceLocation(F, Record, Idx).getRawEncoding());
2259 VTableUses.push_back(Record[Idx++]);
2260 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002261 break;
2262
Sebastian Redl539c5062010-08-18 23:57:32 +00002263 case DYNAMIC_CLASSES:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002264 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2265 DynamicClasses.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002266 break;
2267
Sebastian Redl539c5062010-08-18 23:57:32 +00002268 case PENDING_IMPLICIT_INSTANTIATIONS:
Douglas Gregorbbbc3672011-07-28 19:26:52 +00002269 if (PendingInstantiations.size() % 2 != 0) {
2270 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
2271 return Failure;
2272 }
2273
2274 // Later lists of pending instantiations overwrite earlier ones.
2275 // FIXME: This is most certainly wrong for modules.
2276 PendingInstantiations.clear();
2277 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2278 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
2279 PendingInstantiations.push_back(
2280 ReadSourceLocation(F, Record, I).getRawEncoding());
2281 }
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002282 break;
2283
Sebastian Redl539c5062010-08-18 23:57:32 +00002284 case SEMA_DECL_REFS:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002285 // Later tables overwrite earlier ones.
Douglas Gregor7fb09192011-07-21 22:35:25 +00002286 // FIXME: Modules will have some trouble with this.
2287 SemaDeclRefs.clear();
2288 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2289 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002290 break;
2291
Sebastian Redl539c5062010-08-18 23:57:32 +00002292 case ORIGINAL_FILE_NAME:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002293 // The primary AST will be the last to get here, so it will be the one
Sebastian Redlb293a452010-07-20 21:20:32 +00002294 // that's used.
Daniel Dunbar000c4ff2009-11-11 05:29:04 +00002295 ActualOriginalFileName.assign(BlobStart, BlobLen);
2296 OriginalFileName = ActualOriginalFileName;
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002297 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregor45fe0362009-05-12 01:31:05 +00002298 break;
Mike Stump11289f42009-09-09 15:08:12 +00002299
Douglas Gregora3b20262011-05-06 21:43:30 +00002300 case ORIGINAL_FILE_ID:
2301 OriginalFileID = FileID::get(Record[0]);
2302 break;
2303
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002304 case ORIGINAL_PCH_DIR:
2305 // The primary AST will be the last to get here, so it will be the one
2306 // that's used.
2307 OriginalDir.assign(BlobStart, BlobLen);
2308 break;
2309
Sebastian Redl539c5062010-08-18 23:57:32 +00002310 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek8bd09292010-02-12 23:31:14 +00002311 const std::string &CurBranch = getClangFullRepositoryVersion();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002312 StringRef ASTBranch(BlobStart, BlobLen);
2313 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002314 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregord54f3a12009-10-05 21:07:28 +00002315 return IgnorePCH;
2316 }
2317 break;
2318 }
Sebastian Redlfa061442010-07-21 20:07:32 +00002319
Douglas Gregor4a9c39a2011-07-21 00:47:40 +00002320 case MACRO_DEFINITION_OFFSETS: {
Sebastian Redlfa061442010-07-21 20:07:32 +00002321 F.MacroDefinitionOffsets = (const uint32_t *)BlobStart;
2322 F.NumPreallocatedPreprocessingEntities = Record[0];
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002323 unsigned LocalBasePreprocessedEntityID = Record[1];
2324 F.LocalNumMacroDefinitions = Record[2];
2325 unsigned LocalBaseMacroID = Record[3];
Douglas Gregora863b4b2011-08-04 16:36:56 +00002326
Douglas Gregor4a9c39a2011-07-21 00:47:40 +00002327 unsigned StartingID;
Douglas Gregor51825b42011-09-09 22:02:16 +00002328 if (!PP.getPreprocessingRecord())
2329 PP.createPreprocessingRecord(true);
2330 if (!PP.getPreprocessingRecord()->getExternalSource())
2331 PP.getPreprocessingRecord()->SetExternalSource(*this);
2332 StartingID
2333 = PP.getPreprocessingRecord()
2334 ->allocateLoadedEntities(F.NumPreallocatedPreprocessingEntities);
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00002335 F.BaseMacroDefinitionID = getTotalNumMacroDefinitions();
2336 F.BasePreprocessedEntityID = StartingID;
Douglas Gregor4a9c39a2011-07-21 00:47:40 +00002337
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002338 if (F.NumPreallocatedPreprocessingEntities > 0) {
2339 // Introduce the global -> local mapping for preprocessed entities in
2340 // this module.
2341 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
2342
2343 // Introduce the local -> global mapping for preprocessed entities in
2344 // this module.
2345 F.PreprocessedEntityRemap.insert(
2346 std::make_pair(LocalBasePreprocessedEntityID,
2347 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
2348 }
2349
2350
Douglas Gregora863b4b2011-08-04 16:36:56 +00002351 if (F.LocalNumMacroDefinitions > 0) {
2352 // Introduce the global -> local mapping for macro definitions within
2353 // this module.
2354 GlobalMacroDefinitionMap.insert(
2355 std::make_pair(getTotalNumMacroDefinitions() + 1, &F));
2356
2357 // Introduce the local -> global mapping for macro definitions within
2358 // this module.
2359 F.MacroDefinitionRemap.insert(
2360 std::make_pair(LocalBaseMacroID,
2361 F.BaseMacroDefinitionID - LocalBaseMacroID));
2362
2363 MacroDefinitionsLoaded.resize(
Douglas Gregor270e0142011-07-20 01:29:15 +00002364 MacroDefinitionsLoaded.size() + F.LocalNumMacroDefinitions);
Douglas Gregora863b4b2011-08-04 16:36:56 +00002365 }
2366
Douglas Gregoraae92242010-03-19 21:51:54 +00002367 break;
Douglas Gregor4a9c39a2011-07-21 00:47:40 +00002368 }
2369
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002370 case DECL_UPDATE_OFFSETS: {
2371 if (Record.size() % 2 != 0) {
2372 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2373 return Failure;
2374 }
2375 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Douglas Gregorf7180622011-08-03 15:48:04 +00002376 DeclUpdateOffsets[getGlobalDeclID(F, Record[I])]
2377 .push_back(std::make_pair(&F, Record[I+1]));
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002378 break;
2379 }
2380
Sebastian Redl539c5062010-08-18 23:57:32 +00002381 case DECL_REPLACEMENTS: {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002382 if (Record.size() % 2 != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002383 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002384 return Failure;
2385 }
2386 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Douglas Gregorf7180622011-08-03 15:48:04 +00002387 ReplacedDecls[getGlobalDeclID(F, Record[I])]
2388 = std::make_pair(&F, Record[I+1]);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002389 break;
2390 }
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00002391
2392 case OBJC_CHAINED_CATEGORIES: {
2393 if (Record.size() % 3 != 0) {
2394 Error("invalid OBJC_CHAINED_CATEGORIES block in AST file");
2395 return Failure;
2396 }
2397 for (unsigned I = 0, N = Record.size(); I != N; I += 3) {
2398 serialization::GlobalDeclID GlobID = getGlobalDeclID(F, Record[I]);
2399 F.ChainedObjCCategories[GlobID] = std::make_pair(Record[I+1],
2400 Record[I+2]);
2401 ObjCChainedCategoriesInterfaces.insert(GlobID);
2402 }
2403 break;
2404 }
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002405
2406 case CXX_BASE_SPECIFIER_OFFSETS: {
2407 if (F.LocalNumCXXBaseSpecifiers != 0) {
2408 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2409 return Failure;
2410 }
2411
2412 F.LocalNumCXXBaseSpecifiers = Record[0];
2413 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
Jonathan D. Turner3766fdb2011-07-21 21:15:19 +00002414 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002415 break;
2416 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002417
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002418 case DIAG_PRAGMA_MAPPINGS:
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002419 if (Record.size() % 2 != 0) {
2420 Error("invalid DIAG_USER_MAPPINGS block in AST file");
2421 return Failure;
2422 }
Douglas Gregor925296b2011-07-19 16:10:42 +00002423
2424 if (F.PragmaDiagMappings.empty())
2425 F.PragmaDiagMappings.swap(Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002426 else
Douglas Gregor925296b2011-07-19 16:10:42 +00002427 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
2428 Record.begin(), Record.end());
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002429 break;
Douglas Gregor09b69892011-02-10 17:09:37 +00002430
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002431 case CUDA_SPECIAL_DECL_REFS:
2432 // Later tables overwrite earlier ones.
Douglas Gregor7fb09192011-07-21 22:35:25 +00002433 // FIXME: Modules will have trouble with this.
2434 CUDASpecialDeclRefs.clear();
2435 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2436 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002437 break;
Douglas Gregor09b69892011-02-10 17:09:37 +00002438
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002439 case HEADER_SEARCH_TABLE: {
Douglas Gregor09b69892011-02-10 17:09:37 +00002440 F.HeaderFileInfoTableData = BlobStart;
2441 F.LocalNumHeaderFileInfos = Record[1];
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002442 F.HeaderFileFrameworkStrings = BlobStart + Record[2];
Douglas Gregor09b69892011-02-10 17:09:37 +00002443 if (Record[0]) {
2444 F.HeaderFileInfoTable
2445 = HeaderFileInfoLookupTable::Create(
2446 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002447 (const unsigned char *)F.HeaderFileInfoTableData,
Douglas Gregora3e41532011-07-28 20:55:49 +00002448 HeaderFileInfoTrait(*this, F,
Douglas Gregor51825b42011-09-09 22:02:16 +00002449 &PP.getHeaderSearchInfo(),
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002450 BlobStart + Record[2]));
Douglas Gregor51825b42011-09-09 22:02:16 +00002451
2452 PP.getHeaderSearchInfo().SetExternalSource(this);
2453 if (!PP.getHeaderSearchInfo().getExternalLookup())
2454 PP.getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor09b69892011-02-10 17:09:37 +00002455 }
2456 break;
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002457 }
2458
Peter Collingbourne5df20e02011-02-15 19:46:30 +00002459 case FP_PRAGMA_OPTIONS:
2460 // Later tables overwrite earlier ones.
2461 FPPragmaOptions.swap(Record);
2462 break;
2463
2464 case OPENCL_EXTENSIONS:
2465 // Later tables overwrite earlier ones.
2466 OpenCLExtensions.swap(Record);
2467 break;
Alexis Hunt27a761d2011-05-04 23:29:54 +00002468
2469 case TENTATIVE_DEFINITIONS:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002470 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2471 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Alexis Hunt27a761d2011-05-04 23:29:54 +00002472 break;
Douglas Gregorc2fa1692011-06-28 16:20:02 +00002473
2474 case KNOWN_NAMESPACES:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002475 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2476 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregorc2fa1692011-06-28 16:20:02 +00002477 break;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002478 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002479 }
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002480 Error("premature end of bitstream in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00002481 return Failure;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002482}
2483
Douglas Gregor2fdb6b52011-08-25 20:58:51 +00002484ASTReader::ASTReadResult ASTReader::validateFileEntries(Module &M) {
2485 llvm::BitstreamCursor &SLocEntryCursor = M.SLocEntryCursor;
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00002486
Douglas Gregor2fdb6b52011-08-25 20:58:51 +00002487 for (unsigned i = 0, e = M.LocalNumSLocFileEntries; i != e; ++i) {
2488 SLocEntryCursor.JumpToBit(M.SLocFileOffsets[i]);
2489 unsigned Code = SLocEntryCursor.ReadCode();
2490 if (Code == llvm::bitc::END_BLOCK ||
2491 Code == llvm::bitc::ENTER_SUBBLOCK ||
2492 Code == llvm::bitc::DEFINE_ABBREV) {
2493 Error("incorrectly-formatted source location entry in AST file");
2494 return Failure;
2495 }
2496
2497 RecordData Record;
2498 const char *BlobStart;
2499 unsigned BlobLen;
2500 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
2501 default:
2502 Error("incorrectly-formatted source location entry in AST file");
2503 return Failure;
2504
2505 case SM_SLOC_FILE_ENTRY: {
2506 StringRef Filename(BlobStart, BlobLen);
2507 const FileEntry *File = getFileEntry(Filename);
2508
2509 if (File == 0) {
2510 std::string ErrorStr = "could not find file '";
2511 ErrorStr += Filename;
2512 ErrorStr += "' referenced by AST file";
2513 Error(ErrorStr.c_str());
2514 return IgnorePCH;
2515 }
2516
2517 if (Record.size() < 6) {
2518 Error("source location entry is incorrect");
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00002519 return Failure;
2520 }
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00002521
Douglas Gregor2fdb6b52011-08-25 20:58:51 +00002522 // The stat info from the FileEntry came from the cached stat
2523 // info of the PCH, so we cannot trust it.
2524 struct stat StatBuf;
2525 if (::stat(File->getName(), &StatBuf) != 0) {
2526 StatBuf.st_size = File->getSize();
2527 StatBuf.st_mtime = File->getModificationTime();
2528 }
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00002529
Douglas Gregor2fdb6b52011-08-25 20:58:51 +00002530 if (((off_t)Record[4] != StatBuf.st_size
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00002531#if !defined(LLVM_ON_WIN32)
Douglas Gregor2fdb6b52011-08-25 20:58:51 +00002532 // In our regression testing, the Windows file system seems to
2533 // have inconsistent modification times that sometimes
2534 // erroneously trigger this error-handling path.
2535 || (time_t)Record[5] != StatBuf.st_mtime
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00002536#endif
Douglas Gregor2fdb6b52011-08-25 20:58:51 +00002537 )) {
2538 Error(diag::err_fe_pch_file_modified, Filename);
2539 return IgnorePCH;
2540 }
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00002541
Douglas Gregor2fdb6b52011-08-25 20:58:51 +00002542 break;
2543 }
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00002544 }
2545 }
2546
2547 return Success;
2548}
2549
Douglas Gregord09937f2011-08-25 21:19:59 +00002550namespace {
2551 /// \brief Visitor class used to look up identifirs in an AST file.
2552 class IdentifierLookupVisitor {
2553 StringRef Name;
2554 IdentifierInfo *Found;
2555 public:
2556 explicit IdentifierLookupVisitor(StringRef Name) : Name(Name), Found() { }
2557
2558 static bool visit(Module &M, void *UserData) {
2559 IdentifierLookupVisitor *This
2560 = static_cast<IdentifierLookupVisitor *>(UserData);
2561
2562 ASTIdentifierLookupTable *IdTable
Douglas Gregor4e4c83e2011-08-26 22:04:51 +00002563 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
Douglas Gregord09937f2011-08-25 21:19:59 +00002564 if (!IdTable)
2565 return false;
2566
2567 std::pair<const char*, unsigned> Key(This->Name.begin(),
2568 This->Name.size());
2569 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
2570 if (Pos == IdTable->end())
2571 return false;
2572
2573 // Dereferencing the iterator has the effect of building the
2574 // IdentifierInfo node and populating it with the various
2575 // declarations it needs.
2576 This->Found = *Pos;
2577 return true;
2578 }
2579
2580 // \brief Retrieve the identifier info found within the module
2581 // files.
2582 IdentifierInfo *getIdentifierInfo() const { return Found; }
2583 };
2584}
2585
2586
Sebastian Redl009e7f22010-10-05 16:15:19 +00002587ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
Douglas Gregora6895d82011-07-22 16:00:58 +00002588 ModuleKind Type) {
Douglas Gregordf0c1512011-08-18 04:12:04 +00002589 switch(ReadASTCore(FileName, Type, /*ImportedBy=*/0)) {
Sebastian Redl2abc0382010-07-16 20:41:52 +00002590 case Failure: return Failure;
2591 case IgnorePCH: return IgnorePCH;
2592 case Success: break;
2593 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002594
2595 // Here comes stuff that we only do once the entire chain is loaded.
Douglas Gregor49bf76b2011-07-21 18:46:38 +00002596
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002597 // Check the predefines buffers.
Douglas Gregor9125bd62011-07-27 16:30:06 +00002598 if (!DisableValidation && Type != MK_Module && CheckPredefinesBuffers())
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002599 return IgnorePCH;
2600
Douglas Gregor51825b42011-09-09 22:02:16 +00002601 // Initialization of keywords and pragmas occurs before the
2602 // AST file is read, so there may be some identifiers that were
2603 // loaded into the IdentifierTable before we intercepted the
2604 // creation of identifiers. Iterate through the list of known
2605 // identifiers and determine whether we have to establish
2606 // preprocessor definitions or top-level identifier declaration
2607 // chains for those identifiers.
2608 //
2609 // We copy the IdentifierInfo pointers to a small vector first,
2610 // since de-serializing declarations or macro definitions can add
2611 // new entries into the identifier table, invalidating the
2612 // iterators.
2613 //
2614 // FIXME: We need a lazier way to load this information, e.g., by marking
2615 // the identifier data as 'dirty', so that it will be looked up in the
2616 // AST file(s) if it is uttered in the source. This could save us some
2617 // module load time.
2618 SmallVector<IdentifierInfo *, 128> Identifiers;
2619 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
2620 IdEnd = PP.getIdentifierTable().end();
2621 Id != IdEnd; ++Id)
2622 Identifiers.push_back(Id->second);
2623
2624 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2625 IdentifierLookupVisitor Visitor(Identifiers[I]->getName());
2626 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002627 }
2628
Douglas Gregor4163aca2011-09-09 21:34:22 +00002629 InitializeContext();
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002630
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002631 if (DeserializationListener)
2632 DeserializationListener->ReaderInitialized(this);
2633
Douglas Gregor936a5b42010-11-30 05:23:00 +00002634 // If this AST file is a precompiled preamble, then set the main file ID of
2635 // the source manager to the file source file from which the preamble was
2636 // built. This is the only valid way to use a precompiled preamble.
Douglas Gregora6895d82011-07-22 16:00:58 +00002637 if (Type == MK_Preamble) {
Douglas Gregora3b20262011-05-06 21:43:30 +00002638 if (OriginalFileID.isInvalid()) {
2639 SourceLocation Loc
2640 = SourceMgr.getLocation(FileMgr.getFile(getOriginalSourceFile()), 1, 1);
2641 if (Loc.isValid())
2642 OriginalFileID = SourceMgr.getDecomposedLoc(Loc).first;
Douglas Gregor936a5b42010-11-30 05:23:00 +00002643 }
Douglas Gregor925296b2011-07-19 16:10:42 +00002644 else {
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00002645 OriginalFileID = FileID::get(ModuleMgr.getPrimaryModule().SLocEntryBaseID
Douglas Gregor925296b2011-07-19 16:10:42 +00002646 + OriginalFileID.getOpaqueValue() - 1);
2647 }
2648
Douglas Gregora3b20262011-05-06 21:43:30 +00002649 if (!OriginalFileID.isInvalid())
2650 SourceMgr.SetPreambleFileID(OriginalFileID);
Douglas Gregor936a5b42010-11-30 05:23:00 +00002651 }
2652
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002653 return Success;
2654}
2655
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002656ASTReader::ASTReadResult ASTReader::ReadASTCore(StringRef FileName,
Douglas Gregordf0c1512011-08-18 04:12:04 +00002657 ModuleKind Type,
2658 Module *ImportedBy) {
Douglas Gregor4dd3e942011-08-19 02:29:29 +00002659 Module *M;
2660 bool NewModule;
2661 std::string ErrorStr;
2662 llvm::tie(M, NewModule) = ModuleMgr.addModule(FileName, Type, ImportedBy,
2663 ErrorStr);
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002664
Douglas Gregor4dd3e942011-08-19 02:29:29 +00002665 if (!M) {
2666 // We couldn't load the module.
2667 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
2668 + ErrorStr;
2669 Error(Msg);
2670 return Failure;
2671 }
2672
2673 if (!NewModule) {
2674 // We've already loaded this module.
2675 return Success;
2676 }
2677
2678 // FIXME: This seems rather a hack. Should CurrentDir be part of the
2679 // module?
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002680 if (FileName != "-") {
2681 CurrentDir = llvm::sys::path::parent_path(FileName);
2682 if (CurrentDir.empty()) CurrentDir = ".";
2683 }
2684
Douglas Gregor4dd3e942011-08-19 02:29:29 +00002685 Module &F = *M;
Sebastian Redl34522812010-07-16 17:50:48 +00002686 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002687 Stream.init(F.StreamFile);
Sebastian Redlfa061442010-07-21 20:07:32 +00002688 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Douglas Gregord32f0352011-07-22 06:10:01 +00002689
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002690 // Sniff for the signature.
2691 if (Stream.Read(8) != 'C' ||
2692 Stream.Read(8) != 'P' ||
2693 Stream.Read(8) != 'C' ||
2694 Stream.Read(8) != 'H') {
2695 Diag(diag::err_not_a_pch_file) << FileName;
2696 return Failure;
2697 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002698
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002699 while (!Stream.AtEndOfStream()) {
2700 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00002701
Douglas Gregor92863e42009-04-10 23:10:45 +00002702 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002703 Error("invalid record at top-level of AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002704 return Failure;
2705 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002706
2707 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregora868bbd2009-04-21 22:25:48 +00002708
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002709 // We only know the AST subblock ID.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002710 switch (BlockID) {
2711 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregor92863e42009-04-10 23:10:45 +00002712 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002713 Error("malformed BlockInfoBlock in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002714 return Failure;
2715 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002716 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00002717 case AST_BLOCK_ID:
Sebastian Redl3e31c722010-08-18 23:56:56 +00002718 switch (ReadASTBlock(F)) {
Douglas Gregor55abb232009-04-10 20:39:37 +00002719 case Success:
2720 break;
2721
2722 case Failure:
Douglas Gregor92863e42009-04-10 23:10:45 +00002723 return Failure;
Douglas Gregor55abb232009-04-10 20:39:37 +00002724
2725 case IgnorePCH:
Douglas Gregorbfbde532009-04-10 21:16:55 +00002726 // FIXME: We could consider reading through to the end of this
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002727 // AST block, skipping subblocks, to see if there are other
2728 // AST blocks elsewhere.
Douglas Gregor0bc12932009-04-27 21:28:04 +00002729
Douglas Gregor925296b2011-07-19 16:10:42 +00002730 // FIXME: We can't clear loaded slocentries anymore.
2731 //SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor0bc12932009-04-27 21:28:04 +00002732
2733 // Remove the stat cache.
Sebastian Redl34522812010-07-16 17:50:48 +00002734 if (F.StatCache)
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002735 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor0bc12932009-04-27 21:28:04 +00002736
Douglas Gregor92863e42009-04-10 23:10:45 +00002737 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00002738 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002739 break;
2740 default:
Douglas Gregor92863e42009-04-10 23:10:45 +00002741 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002742 Error("malformed block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002743 return Failure;
2744 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002745 break;
2746 }
Mike Stump11289f42009-09-09 15:08:12 +00002747 }
Douglas Gregord32f0352011-07-22 06:10:01 +00002748
Douglas Gregora6895d82011-07-22 16:00:58 +00002749 // Once read, set the Module bit base offset and update the size in
Douglas Gregord32f0352011-07-22 06:10:01 +00002750 // bits of all files we've seen.
2751 F.GlobalBitOffset = TotalModulesSizeInBits;
2752 TotalModulesSizeInBits += F.SizeInBits;
2753 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
Douglas Gregor2fdb6b52011-08-25 20:58:51 +00002754
2755 // Make sure that the files this module was built against are still available.
2756 if (!DisableValidation) {
2757 switch(validateFileEntries(*M)) {
2758 case Failure: return Failure;
2759 case IgnorePCH: return IgnorePCH;
2760 case Success: break;
2761 }
2762 }
Douglas Gregora918bab2011-08-25 21:09:44 +00002763
2764 // Preload SLocEntries.
2765 for (unsigned I = 0, N = M->PreloadSLocEntries.size(); I != N; ++I) {
2766 int Index = int(M->PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
2767 ASTReadResult Result = ReadSLocEntryRecord(Index);
2768 if (Result != Success)
2769 return Failure;
2770 }
2771
Douglas Gregor2fdb6b52011-08-25 20:58:51 +00002772
Sebastian Redl2abc0382010-07-16 20:41:52 +00002773 return Success;
2774}
2775
Douglas Gregor51825b42011-09-09 22:02:16 +00002776void ASTReader::InitializeContext() {
Douglas Gregordab42432011-08-12 00:15:20 +00002777 // If there's a listener, notify them that we "read" the translation unit.
2778 if (DeserializationListener)
Douglas Gregor4163aca2011-09-09 21:34:22 +00002779 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
2780 Context.getTranslationUnitDecl());
Douglas Gregoraa433012010-10-01 01:18:02 +00002781
Douglas Gregordab42432011-08-12 00:15:20 +00002782 // Make sure we load the declaration update records for the translation unit,
2783 // if there are any.
Douglas Gregor4163aca2011-09-09 21:34:22 +00002784 loadDeclUpdateRecords(PREDEF_DECL_TRANSLATION_UNIT_ID,
2785 Context.getTranslationUnitDecl());
Douglas Gregordab42432011-08-12 00:15:20 +00002786
Douglas Gregoraa8a8272011-08-11 22:18:49 +00002787 // FIXME: Find a better way to deal with collisions between these
2788 // built-in types. Right now, we just ignore the problem.
2789
2790 // Load the special types.
Douglas Gregorabc5fbe2011-09-10 00:30:18 +00002791 if (SpecialTypes.size() > NumSpecialTypeIDs) {
2792 if (Context.getBuiltinVaListType().isNull()) {
2793 Context.setBuiltinVaListType(
2794 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
Douglas Gregoraa8a8272011-08-11 22:18:49 +00002795 }
2796
Douglas Gregorabc5fbe2011-09-10 00:30:18 +00002797 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL]) {
2798 if (Context.ObjCProtoType.isNull())
2799 Context.ObjCProtoType = GetType(Proto);
2800 }
2801
2802 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
2803 if (!Context.CFConstantStringTypeDecl)
2804 Context.setCFConstantStringType(GetType(String));
2805 }
2806
2807 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
2808 QualType FileType = GetType(File);
2809 if (FileType.isNull()) {
2810 Error("FILE type is NULL");
2811 return;
2812 }
2813
2814 if (!Context.FILEDecl) {
2815 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
2816 Context.setFILEDecl(Typedef->getDecl());
2817 else {
2818 const TagType *Tag = FileType->getAs<TagType>();
2819 if (!Tag) {
2820 Error("Invalid FILE type in AST file");
2821 return;
2822 }
2823 Context.setFILEDecl(Tag->getDecl());
Jonathan D. Turnerf07f1312011-08-05 23:07:10 +00002824 }
Jonathan D. Turnerf07f1312011-08-05 23:07:10 +00002825 }
Douglas Gregor27821ce2009-07-07 16:35:42 +00002826 }
Douglas Gregoraa8a8272011-08-11 22:18:49 +00002827
Douglas Gregorabc5fbe2011-09-10 00:30:18 +00002828 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
2829 QualType Jmp_bufType = GetType(Jmp_buf);
2830 if (Jmp_bufType.isNull()) {
2831 Error("jmp_buf type is NULL");
2832 return;
2833 }
2834
2835 if (!Context.jmp_bufDecl) {
2836 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
2837 Context.setjmp_bufDecl(Typedef->getDecl());
2838 else {
2839 const TagType *Tag = Jmp_bufType->getAs<TagType>();
2840 if (!Tag) {
2841 Error("Invalid jmp_buf type in AST file");
2842 return;
2843 }
2844 Context.setjmp_bufDecl(Tag->getDecl());
Jonathan D. Turnerf07f1312011-08-05 23:07:10 +00002845 }
Jonathan D. Turnerf07f1312011-08-05 23:07:10 +00002846 }
Mike Stumpa4de80b2009-07-28 02:25:19 +00002847 }
Douglas Gregorabc5fbe2011-09-10 00:30:18 +00002848
2849 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
2850 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
2851 if (Sigjmp_bufType.isNull()) {
2852 Error("sigjmp_buf type is NULL");
2853 return;
2854 }
2855
2856 if (!Context.sigjmp_bufDecl) {
2857 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
2858 Context.setsigjmp_bufDecl(Typedef->getDecl());
2859 else {
2860 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
2861 assert(Tag && "Invalid sigjmp_buf type in AST file");
2862 Context.setsigjmp_bufDecl(Tag->getDecl());
2863 }
2864 }
2865 }
2866
2867 if (unsigned ObjCIdRedef
2868 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
2869 if (Context.ObjCIdRedefinitionType.isNull())
2870 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
2871 }
2872
2873 if (unsigned ObjCClassRedef
2874 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
2875 if (Context.ObjCClassRedefinitionType.isNull())
2876 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
2877 }
2878
2879 if (unsigned ObjCSelRedef
2880 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
2881 if (Context.ObjCSelRedefinitionType.isNull())
2882 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
2883 }
Douglas Gregoraa8a8272011-08-11 22:18:49 +00002884 }
2885
Douglas Gregor4163aca2011-09-09 21:34:22 +00002886 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002887
2888 // If there were any CUDA special declarations, deserialize them.
2889 if (!CUDASpecialDeclRefs.empty()) {
2890 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
Douglas Gregor4163aca2011-09-09 21:34:22 +00002891 Context.setcudaConfigureCallDecl(
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002892 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
2893 }
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002894}
2895
Douglas Gregor45fe0362009-05-12 01:31:05 +00002896/// \brief Retrieve the name of the original source file name
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002897/// directly from the AST file, without actually loading the AST
Douglas Gregor45fe0362009-05-12 01:31:05 +00002898/// file.
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002899std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +00002900 FileManager &FileMgr,
Daniel Dunbar3b951482009-12-03 09:13:06 +00002901 Diagnostic &Diags) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002902 // Open the AST file.
Douglas Gregor45fe0362009-05-12 01:31:05 +00002903 std::string ErrStr;
2904 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner5159f612010-11-23 08:35:12 +00002905 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregor45fe0362009-05-12 01:31:05 +00002906 if (!Buffer) {
Daniel Dunbar3b951482009-12-03 09:13:06 +00002907 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002908 return std::string();
2909 }
2910
2911 // Initialize the stream
2912 llvm::BitstreamReader StreamFile;
2913 llvm::BitstreamCursor Stream;
Mike Stump11289f42009-09-09 15:08:12 +00002914 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregor45fe0362009-05-12 01:31:05 +00002915 (const unsigned char *)Buffer->getBufferEnd());
2916 Stream.init(StreamFile);
2917
2918 // Sniff for the signature.
2919 if (Stream.Read(8) != 'C' ||
2920 Stream.Read(8) != 'P' ||
2921 Stream.Read(8) != 'C' ||
2922 Stream.Read(8) != 'H') {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002923 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002924 return std::string();
2925 }
2926
2927 RecordData Record;
2928 while (!Stream.AtEndOfStream()) {
2929 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00002930
Douglas Gregor45fe0362009-05-12 01:31:05 +00002931 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2932 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump11289f42009-09-09 15:08:12 +00002933
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002934 // We only know the AST subblock ID.
Douglas Gregor45fe0362009-05-12 01:31:05 +00002935 switch (BlockID) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002936 case AST_BLOCK_ID:
2937 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002938 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002939 return std::string();
2940 }
2941 break;
Mike Stump11289f42009-09-09 15:08:12 +00002942
Douglas Gregor45fe0362009-05-12 01:31:05 +00002943 default:
2944 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002945 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002946 return std::string();
2947 }
2948 break;
2949 }
2950 continue;
2951 }
2952
2953 if (Code == llvm::bitc::END_BLOCK) {
2954 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002955 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002956 return std::string();
2957 }
2958 continue;
2959 }
2960
2961 if (Code == llvm::bitc::DEFINE_ABBREV) {
2962 Stream.ReadAbbrevRecord();
2963 continue;
2964 }
2965
2966 Record.clear();
2967 const char *BlobStart = 0;
2968 unsigned BlobLen = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002969 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl539c5062010-08-18 23:57:32 +00002970 == ORIGINAL_FILE_NAME)
Douglas Gregor45fe0362009-05-12 01:31:05 +00002971 return std::string(BlobStart, BlobLen);
Mike Stump11289f42009-09-09 15:08:12 +00002972 }
Douglas Gregor45fe0362009-05-12 01:31:05 +00002973
2974 return std::string();
2975}
2976
Douglas Gregor55abb232009-04-10 20:39:37 +00002977/// \brief Parse the record that corresponds to a LangOptions data
2978/// structure.
2979///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002980/// This routine parses the language options from the AST file and then gives
2981/// them to the AST listener if one is set.
Douglas Gregor55abb232009-04-10 20:39:37 +00002982///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002983/// \returns true if the listener deems the file unacceptable, false otherwise.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002984bool ASTReader::ParseLanguageOptions(
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002985 const SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002986 if (Listener) {
2987 LangOptions LangOpts;
Mike Stump11289f42009-09-09 15:08:12 +00002988
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002989 #define PARSE_LANGOPT(Option) \
2990 LangOpts.Option = Record[Idx]; \
2991 ++Idx
Mike Stump11289f42009-09-09 15:08:12 +00002992
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002993 unsigned Idx = 0;
2994 PARSE_LANGOPT(Trigraphs);
2995 PARSE_LANGOPT(BCPLComment);
2996 PARSE_LANGOPT(DollarIdents);
2997 PARSE_LANGOPT(AsmPreprocessor);
2998 PARSE_LANGOPT(GNUMode);
Chandler Carruthe03aa552010-04-17 20:17:31 +00002999 PARSE_LANGOPT(GNUKeywords);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003000 PARSE_LANGOPT(ImplicitInt);
3001 PARSE_LANGOPT(Digraphs);
3002 PARSE_LANGOPT(HexFloats);
3003 PARSE_LANGOPT(C99);
Peter Collingbournea686b5f2011-04-15 00:35:23 +00003004 PARSE_LANGOPT(C1X);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003005 PARSE_LANGOPT(Microsoft);
3006 PARSE_LANGOPT(CPlusPlus);
3007 PARSE_LANGOPT(CPlusPlus0x);
3008 PARSE_LANGOPT(CXXOperatorNames);
3009 PARSE_LANGOPT(ObjC1);
3010 PARSE_LANGOPT(ObjC2);
3011 PARSE_LANGOPT(ObjCNonFragileABI);
Fariborz Jahanian45878032010-02-09 19:31:38 +00003012 PARSE_LANGOPT(ObjCNonFragileABI2);
Fariborz Jahanian13f3b2f2011-01-07 18:59:25 +00003013 PARSE_LANGOPT(AppleKext);
Ted Kremenek1d56c9e2010-12-23 21:35:43 +00003014 PARSE_LANGOPT(ObjCDefaultSynthProperties);
Douglas Gregora860e6a2011-06-14 23:20:43 +00003015 PARSE_LANGOPT(ObjCInferRelatedResultType);
Fariborz Jahanian62c56022010-04-22 21:01:59 +00003016 PARSE_LANGOPT(NoConstantCFStrings);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003017 PARSE_LANGOPT(PascalStrings);
3018 PARSE_LANGOPT(WritableStrings);
3019 PARSE_LANGOPT(LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +00003020 PARSE_LANGOPT(AltiVec);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003021 PARSE_LANGOPT(Exceptions);
Anders Carlssonce8dd3a2011-02-19 23:53:54 +00003022 PARSE_LANGOPT(ObjCExceptions);
Anders Carlsson6bbd2682011-02-23 03:04:54 +00003023 PARSE_LANGOPT(CXXExceptions);
3024 PARSE_LANGOPT(SjLjExceptions);
Douglas Gregordbe39272011-02-01 15:15:22 +00003025 PARSE_LANGOPT(MSBitfields);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003026 PARSE_LANGOPT(NeXTRuntime);
3027 PARSE_LANGOPT(Freestanding);
3028 PARSE_LANGOPT(NoBuiltin);
3029 PARSE_LANGOPT(ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +00003030 PARSE_LANGOPT(POSIXThreads);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003031 PARSE_LANGOPT(Blocks);
3032 PARSE_LANGOPT(EmitAllDecls);
3033 PARSE_LANGOPT(MathErrno);
Chris Lattner51924e512010-06-26 21:25:03 +00003034 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
3035 Record[Idx++]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003036 PARSE_LANGOPT(HeinousExtensions);
3037 PARSE_LANGOPT(Optimize);
3038 PARSE_LANGOPT(OptimizeSize);
3039 PARSE_LANGOPT(Static);
3040 PARSE_LANGOPT(PICLevel);
3041 PARSE_LANGOPT(GNUInline);
3042 PARSE_LANGOPT(NoInline);
Chandler Carruth7ffce732011-04-23 20:05:38 +00003043 PARSE_LANGOPT(Deprecated);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003044 PARSE_LANGOPT(AccessControl);
3045 PARSE_LANGOPT(CharIsSigned);
John Thompsoned4e2952009-11-05 20:14:16 +00003046 PARSE_LANGOPT(ShortWChar);
Argyrios Kyrtzidisa88942a2011-01-15 02:56:16 +00003047 PARSE_LANGOPT(ShortEnums);
Chris Lattner51924e512010-06-26 21:25:03 +00003048 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
John McCall457a04e2010-10-22 21:05:15 +00003049 LangOpts.setVisibilityMode((Visibility)Record[Idx++]);
Daniel Dunbar143021e2009-09-21 04:16:19 +00003050 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
Chris Lattner51924e512010-06-26 21:25:03 +00003051 Record[Idx++]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003052 PARSE_LANGOPT(InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +00003053 PARSE_LANGOPT(OpenCL);
Peter Collingbourne546d0792010-12-01 19:14:57 +00003054 PARSE_LANGOPT(CUDA);
Mike Stumpd9546382009-12-12 01:27:46 +00003055 PARSE_LANGOPT(CatchUndefined);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003056 PARSE_LANGOPT(DefaultFPContract);
Roman Divackydc1f68d2011-03-01 17:36:40 +00003057 PARSE_LANGOPT(ElideConstructors);
3058 PARSE_LANGOPT(SpellChecking);
Roman Divacky65b88cd2011-03-01 17:40:53 +00003059 PARSE_LANGOPT(MRTD);
John McCall31168b02011-06-15 23:02:42 +00003060 PARSE_LANGOPT(ObjCAutoRefCount);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003061 #undef PARSE_LANGOPT
Douglas Gregor55abb232009-04-10 20:39:37 +00003062
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003063 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor55abb232009-04-10 20:39:37 +00003064 }
Douglas Gregor55abb232009-04-10 20:39:37 +00003065
3066 return false;
3067}
3068
Douglas Gregor14f77162011-08-25 18:03:05 +00003069namespace {
3070 /// \brief Visitor used by ASTReader::ReadPreprocessedEntities() to load
3071 /// all of the preprocessed entities within a module.
3072 class ReadPreprocessedEntitiesVisitor {
3073 ASTReader &Reader;
3074
3075 public:
3076 explicit ReadPreprocessedEntitiesVisitor(ASTReader &Reader)
3077 : Reader(Reader) { }
3078
3079 static bool visit(Module &M, bool Preorder, void *UserData) {
3080 if (Preorder)
3081 return false;
3082
3083 ReadPreprocessedEntitiesVisitor *This
3084 = static_cast<ReadPreprocessedEntitiesVisitor *>(UserData);
3085
3086 if (!M.PreprocessorDetailCursor.getBitStreamReader())
3087 return false;
3088
3089 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
3090 M.PreprocessorDetailCursor.JumpToBit(M.PreprocessorDetailStartOffset);
3091 while (This->Reader.LoadPreprocessedEntity(M)) { }
3092 return false;
3093 }
3094 };
3095}
Douglas Gregor92a96f52011-02-08 21:58:10 +00003096
Douglas Gregor14f77162011-08-25 18:03:05 +00003097void ASTReader::ReadPreprocessedEntities() {
3098 ReadPreprocessedEntitiesVisitor Visitor(*this);
3099 ModuleMgr.visitDepthFirst(&ReadPreprocessedEntitiesVisitor::visit, &Visitor);
Douglas Gregoraae92242010-03-19 21:51:54 +00003100}
3101
Douglas Gregor46c50012011-02-11 19:46:30 +00003102PreprocessedEntity *ASTReader::ReadPreprocessedEntityAtOffset(uint64_t Offset) {
Douglas Gregord32f0352011-07-22 06:10:01 +00003103 RecordLocation Loc = getLocalBitOffset(Offset);
Douglas Gregorf88e35b2010-11-30 06:16:57 +00003104
Douglas Gregor92a96f52011-02-08 21:58:10 +00003105 // Keep track of where we are in the stream, then jump back there
3106 // after reading this entity.
Douglas Gregord32f0352011-07-22 06:10:01 +00003107 SavedStreamPosition SavedPosition(Loc.F->PreprocessorDetailCursor);
3108 Loc.F->PreprocessorDetailCursor.JumpToBit(Loc.Offset);
3109 return LoadPreprocessedEntity(*Loc.F);
Douglas Gregorf88e35b2010-11-30 06:16:57 +00003110}
3111
Douglas Gregor69e94642011-08-25 18:14:34 +00003112namespace {
3113 /// \brief Visitor used to search for information about a header file.
3114 class HeaderFileInfoVisitor {
3115 ASTReader &Reader;
3116 const FileEntry *FE;
3117
3118 llvm::Optional<HeaderFileInfo> HFI;
3119
3120 public:
3121 HeaderFileInfoVisitor(ASTReader &Reader, const FileEntry *FE)
3122 : Reader(Reader), FE(FE) { }
3123
3124 static bool visit(Module &M, void *UserData) {
3125 HeaderFileInfoVisitor *This
3126 = static_cast<HeaderFileInfoVisitor *>(UserData);
3127
3128 HeaderFileInfoTrait Trait(This->Reader, M,
3129 &This->Reader.getPreprocessor().getHeaderSearchInfo(),
3130 M.HeaderFileFrameworkStrings,
3131 This->FE->getName());
3132
3133 HeaderFileInfoLookupTable *Table
3134 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
3135 if (!Table)
3136 return false;
3137
3138 // Look in the on-disk hash table for an entry for this file name.
3139 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE->getName(),
3140 &Trait);
3141 if (Pos == Table->end())
3142 return false;
3143
3144 This->HFI = *Pos;
3145 return true;
3146 }
3147
3148 llvm::Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
3149 };
3150}
3151
Douglas Gregor09b69892011-02-10 17:09:37 +00003152HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Douglas Gregor69e94642011-08-25 18:14:34 +00003153 HeaderFileInfoVisitor Visitor(*this, FE);
3154 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
3155 if (llvm::Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) {
Douglas Gregor09b69892011-02-10 17:09:37 +00003156 if (Listener)
Douglas Gregor69e94642011-08-25 18:14:34 +00003157 Listener->ReadHeaderFileInfo(*HFI, FE->getUID());
3158 return *HFI;
Douglas Gregor09b69892011-02-10 17:09:37 +00003159 }
3160
3161 return HeaderFileInfo();
3162}
3163
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00003164void ASTReader::ReadPragmaDiagnosticMappings(Diagnostic &Diag) {
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00003165 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
3166 Module &F = *(*I);
Douglas Gregor925296b2011-07-19 16:10:42 +00003167 unsigned Idx = 0;
3168 while (Idx < F.PragmaDiagMappings.size()) {
3169 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
3170 while (1) {
3171 assert(Idx < F.PragmaDiagMappings.size() &&
3172 "Invalid data, didn't find '-1' marking end of diag/map pairs");
3173 if (Idx >= F.PragmaDiagMappings.size()) {
3174 break; // Something is messed up but at least avoid infinite loop in
3175 // release build.
3176 }
3177 unsigned DiagID = F.PragmaDiagMappings[Idx++];
3178 if (DiagID == (unsigned)-1) {
3179 break; // no more diag/map pairs for this location.
3180 }
3181 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++];
3182 Diag.setDiagnosticMapping(DiagID, Map, Loc);
3183 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00003184 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00003185 }
3186}
3187
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003188/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redl2c499f62010-08-18 23:56:43 +00003189ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Douglas Gregor5204bde2011-08-02 16:26:37 +00003190 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
Jonathan D. Turner35005682011-07-20 21:31:32 +00003191 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
Douglas Gregor8ab4ea82011-07-29 00:21:44 +00003192 Module *M = I->second;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003193 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003194}
3195
3196/// \brief Read and return the type with the given index..
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003197///
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003198/// The index is the type ID, shifted and minus the number of predefs. This
3199/// routine actually reads the record corresponding to the type at the given
3200/// location. It is a helper routine for GetType, which deals with reading type
3201/// IDs.
Douglas Gregor903b7e92011-07-22 00:38:23 +00003202QualType ASTReader::readTypeRecord(unsigned Index) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003203 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003204 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00003205
Douglas Gregorfeb84b02009-04-14 21:18:50 +00003206 // Keep track of where we are in the stream, then jump back there
3207 // after reading this type.
Douglas Gregor12bfa382009-10-17 00:13:19 +00003208 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00003209
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003210 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redleaa4ade2010-08-11 18:52:41 +00003211
Douglas Gregor1342e842009-07-06 18:54:52 +00003212 // Note that we are loading a type record.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00003213 Deserializing AType(this);
Mike Stump11289f42009-09-09 15:08:12 +00003214
Douglas Gregor903b7e92011-07-22 00:38:23 +00003215 unsigned Idx = 0;
Sebastian Redl2c373b92010-10-05 15:59:54 +00003216 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003217 RecordData Record;
Douglas Gregor12bfa382009-10-17 00:13:19 +00003218 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl539c5062010-08-18 23:57:32 +00003219 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
3220 case TYPE_EXT_QUAL: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003221 if (Record.size() != 2) {
3222 Error("Incorrect encoding of extended qualifier type");
3223 return QualType();
3224 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003225 QualType Base = readType(*Loc.F, Record, Idx);
3226 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
Douglas Gregor4163aca2011-09-09 21:34:22 +00003227 return Context.getQualifiedType(Base, Quals);
Douglas Gregor455b8f42009-04-15 22:00:08 +00003228 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003229
Sebastian Redl539c5062010-08-18 23:57:32 +00003230 case TYPE_COMPLEX: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003231 if (Record.size() != 1) {
3232 Error("Incorrect encoding of complex type");
3233 return QualType();
3234 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003235 QualType ElemType = readType(*Loc.F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00003236 return Context.getComplexType(ElemType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003237 }
3238
Sebastian Redl539c5062010-08-18 23:57:32 +00003239 case TYPE_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003240 if (Record.size() != 1) {
3241 Error("Incorrect encoding of pointer type");
3242 return QualType();
3243 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003244 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00003245 return Context.getPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003246 }
3247
Sebastian Redl539c5062010-08-18 23:57:32 +00003248 case TYPE_BLOCK_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003249 if (Record.size() != 1) {
3250 Error("Incorrect encoding of block pointer type");
3251 return QualType();
3252 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003253 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00003254 return Context.getBlockPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003255 }
3256
Sebastian Redl539c5062010-08-18 23:57:32 +00003257 case TYPE_LVALUE_REFERENCE: {
Richard Smith0f538462011-04-12 10:38:03 +00003258 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003259 Error("Incorrect encoding of lvalue reference type");
3260 return QualType();
3261 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003262 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00003263 return Context.getLValueReferenceType(PointeeType, Record[1]);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003264 }
3265
Sebastian Redl539c5062010-08-18 23:57:32 +00003266 case TYPE_RVALUE_REFERENCE: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003267 if (Record.size() != 1) {
3268 Error("Incorrect encoding of rvalue reference type");
3269 return QualType();
3270 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003271 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00003272 return Context.getRValueReferenceType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003273 }
3274
Sebastian Redl539c5062010-08-18 23:57:32 +00003275 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidisee776bc2010-07-02 11:55:15 +00003276 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003277 Error("Incorrect encoding of member pointer type");
3278 return QualType();
3279 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003280 QualType PointeeType = readType(*Loc.F, Record, Idx);
3281 QualType ClassType = readType(*Loc.F, Record, Idx);
Douglas Gregor0cdc8322010-12-10 17:03:06 +00003282 if (PointeeType.isNull() || ClassType.isNull())
3283 return QualType();
3284
Douglas Gregor4163aca2011-09-09 21:34:22 +00003285 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003286 }
3287
Sebastian Redl539c5062010-08-18 23:57:32 +00003288 case TYPE_CONSTANT_ARRAY: {
Douglas Gregor903b7e92011-07-22 00:38:23 +00003289 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003290 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3291 unsigned IndexTypeQuals = Record[2];
3292 unsigned Idx = 3;
3293 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00003294 return Context.getConstantArrayType(ElementType, Size,
Douglas Gregor04318252009-07-06 15:59:29 +00003295 ASM, IndexTypeQuals);
3296 }
3297
Sebastian Redl539c5062010-08-18 23:57:32 +00003298 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregor903b7e92011-07-22 00:38:23 +00003299 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003300 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3301 unsigned IndexTypeQuals = Record[2];
Douglas Gregor4163aca2011-09-09 21:34:22 +00003302 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003303 }
3304
Sebastian Redl539c5062010-08-18 23:57:32 +00003305 case TYPE_VARIABLE_ARRAY: {
Douglas Gregor903b7e92011-07-22 00:38:23 +00003306 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00003307 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3308 unsigned IndexTypeQuals = Record[2];
Sebastian Redl2c373b92010-10-05 15:59:54 +00003309 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
3310 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
Douglas Gregor4163aca2011-09-09 21:34:22 +00003311 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor04318252009-07-06 15:59:29 +00003312 ASM, IndexTypeQuals,
3313 SourceRange(LBLoc, RBLoc));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003314 }
3315
Sebastian Redl539c5062010-08-18 23:57:32 +00003316 case TYPE_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00003317 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003318 Error("incorrect encoding of vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003319 return QualType();
3320 }
3321
Douglas Gregor903b7e92011-07-22 00:38:23 +00003322 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003323 unsigned NumElements = Record[1];
Bob Wilsonaeb56442010-11-10 21:56:12 +00003324 unsigned VecKind = Record[2];
Douglas Gregor4163aca2011-09-09 21:34:22 +00003325 return Context.getVectorType(ElementType, NumElements,
Bob Wilsonaeb56442010-11-10 21:56:12 +00003326 (VectorType::VectorKind)VecKind);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003327 }
3328
Sebastian Redl539c5062010-08-18 23:57:32 +00003329 case TYPE_EXT_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00003330 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003331 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003332 return QualType();
3333 }
3334
Douglas Gregor903b7e92011-07-22 00:38:23 +00003335 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003336 unsigned NumElements = Record[1];
Douglas Gregor4163aca2011-09-09 21:34:22 +00003337 return Context.getExtVectorType(ElementType, NumElements);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003338 }
3339
Sebastian Redl539c5062010-08-18 23:57:32 +00003340 case TYPE_FUNCTION_NO_PROTO: {
John McCall31168b02011-06-15 23:02:42 +00003341 if (Record.size() != 6) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00003342 Error("incorrect encoding of no-proto function type");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003343 return QualType();
3344 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003345 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCall31168b02011-06-15 23:02:42 +00003346 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
3347 (CallingConv)Record[4], Record[5]);
Douglas Gregor4163aca2011-09-09 21:34:22 +00003348 return Context.getFunctionNoProtoType(ResultType, Info);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003349 }
3350
Sebastian Redl539c5062010-08-18 23:57:32 +00003351 case TYPE_FUNCTION_PROTO: {
Douglas Gregor903b7e92011-07-22 00:38:23 +00003352 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCalldb40c7f2010-12-14 08:05:40 +00003353
3354 FunctionProtoType::ExtProtoInfo EPI;
3355 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
Eli Friedmanc5b20b52011-04-09 08:18:08 +00003356 /*hasregparm*/ Record[2],
3357 /*regparm*/ Record[3],
John McCall31168b02011-06-15 23:02:42 +00003358 static_cast<CallingConv>(Record[4]),
3359 /*produces*/ Record[5]);
John McCalldb40c7f2010-12-14 08:05:40 +00003360
John McCall31168b02011-06-15 23:02:42 +00003361 unsigned Idx = 6;
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003362 unsigned NumParams = Record[Idx++];
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003363 SmallVector<QualType, 16> ParamTypes;
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003364 for (unsigned I = 0; I != NumParams; ++I)
Douglas Gregor903b7e92011-07-22 00:38:23 +00003365 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
John McCalldb40c7f2010-12-14 08:05:40 +00003366
3367 EPI.Variadic = Record[Idx++];
3368 EPI.TypeQuals = Record[Idx++];
Douglas Gregordb9d6642011-01-26 05:01:58 +00003369 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003370 ExceptionSpecificationType EST =
3371 static_cast<ExceptionSpecificationType>(Record[Idx++]);
3372 EPI.ExceptionSpecType = EST;
3373 if (EST == EST_Dynamic) {
3374 EPI.NumExceptions = Record[Idx++];
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003375 SmallVector<QualType, 2> Exceptions;
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003376 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
Douglas Gregor903b7e92011-07-22 00:38:23 +00003377 Exceptions.push_back(readType(*Loc.F, Record, Idx));
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003378 EPI.Exceptions = Exceptions.data();
3379 } else if (EST == EST_ComputedNoexcept) {
3380 EPI.NoexceptExpr = ReadExpr(*Loc.F);
3381 }
Douglas Gregor4163aca2011-09-09 21:34:22 +00003382 return Context.getFunctionType(ResultType, ParamTypes.data(), NumParams,
John McCalldb40c7f2010-12-14 08:05:40 +00003383 EPI);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003384 }
3385
Douglas Gregor7fb09192011-07-21 22:35:25 +00003386 case TYPE_UNRESOLVED_USING: {
3387 unsigned Idx = 0;
Douglas Gregor4163aca2011-09-09 21:34:22 +00003388 return Context.getTypeDeclType(
Douglas Gregor7fb09192011-07-21 22:35:25 +00003389 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
3390 }
3391
Sebastian Redl539c5062010-08-18 23:57:32 +00003392 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003393 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003394 Error("incorrect encoding of typedef type");
3395 return QualType();
3396 }
Douglas Gregor7fb09192011-07-21 22:35:25 +00003397 unsigned Idx = 0;
3398 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
Douglas Gregor903b7e92011-07-22 00:38:23 +00003399 QualType Canonical = readType(*Loc.F, Record, Idx);
Douglas Gregorf86c9392010-10-26 00:51:02 +00003400 if (!Canonical.isNull())
Douglas Gregor4163aca2011-09-09 21:34:22 +00003401 Canonical = Context.getCanonicalType(Canonical);
3402 return Context.getTypedefType(Decl, Canonical);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003403 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003404
Sebastian Redl539c5062010-08-18 23:57:32 +00003405 case TYPE_TYPEOF_EXPR:
Douglas Gregor4163aca2011-09-09 21:34:22 +00003406 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003407
Sebastian Redl539c5062010-08-18 23:57:32 +00003408 case TYPE_TYPEOF: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003409 if (Record.size() != 1) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003410 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003411 return QualType();
3412 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003413 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00003414 return Context.getTypeOfType(UnderlyingType);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003415 }
Mike Stump11289f42009-09-09 15:08:12 +00003416
Sebastian Redl539c5062010-08-18 23:57:32 +00003417 case TYPE_DECLTYPE:
Douglas Gregor4163aca2011-09-09 21:34:22 +00003418 return Context.getDecltypeType(ReadExpr(*Loc.F));
Anders Carlsson81df7b82009-06-24 19:06:50 +00003419
Alexis Hunte852b102011-05-24 22:41:36 +00003420 case TYPE_UNARY_TRANSFORM: {
Douglas Gregor903b7e92011-07-22 00:38:23 +00003421 QualType BaseType = readType(*Loc.F, Record, Idx);
3422 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Alexis Hunte852b102011-05-24 22:41:36 +00003423 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
Douglas Gregor4163aca2011-09-09 21:34:22 +00003424 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
Alexis Hunte852b102011-05-24 22:41:36 +00003425 }
3426
Richard Smith30482bc2011-02-20 03:19:35 +00003427 case TYPE_AUTO:
Douglas Gregor4163aca2011-09-09 21:34:22 +00003428 return Context.getAutoType(readType(*Loc.F, Record, Idx));
Richard Smith30482bc2011-02-20 03:19:35 +00003429
Sebastian Redl539c5062010-08-18 23:57:32 +00003430 case TYPE_RECORD: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003431 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003432 Error("incorrect encoding of record type");
3433 return QualType();
3434 }
Douglas Gregor7fb09192011-07-21 22:35:25 +00003435 unsigned Idx = 0;
3436 bool IsDependent = Record[Idx++];
3437 QualType T
Douglas Gregor4163aca2011-09-09 21:34:22 +00003438 = Context.getRecordType(ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx));
John McCall424cec92011-01-19 06:33:43 +00003439 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003440 return T;
3441 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003442
Sebastian Redl539c5062010-08-18 23:57:32 +00003443 case TYPE_ENUM: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003444 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003445 Error("incorrect encoding of enum type");
3446 return QualType();
3447 }
Douglas Gregor7fb09192011-07-21 22:35:25 +00003448 unsigned Idx = 0;
3449 bool IsDependent = Record[Idx++];
3450 QualType T
Douglas Gregor4163aca2011-09-09 21:34:22 +00003451 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
John McCall424cec92011-01-19 06:33:43 +00003452 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003453 return T;
3454 }
Douglas Gregor1daeb692009-04-13 18:14:40 +00003455
John McCall81904512011-01-06 01:58:22 +00003456 case TYPE_ATTRIBUTED: {
3457 if (Record.size() != 3) {
3458 Error("incorrect encoding of attributed type");
3459 return QualType();
3460 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003461 QualType modifiedType = readType(*Loc.F, Record, Idx);
3462 QualType equivalentType = readType(*Loc.F, Record, Idx);
John McCall81904512011-01-06 01:58:22 +00003463 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
Douglas Gregor4163aca2011-09-09 21:34:22 +00003464 return Context.getAttributedType(kind, modifiedType, equivalentType);
John McCall81904512011-01-06 01:58:22 +00003465 }
3466
Abramo Bagnara924a8f32010-12-10 16:29:40 +00003467 case TYPE_PAREN: {
3468 if (Record.size() != 1) {
3469 Error("incorrect encoding of paren type");
3470 return QualType();
3471 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003472 QualType InnerType = readType(*Loc.F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00003473 return Context.getParenType(InnerType);
Abramo Bagnara924a8f32010-12-10 16:29:40 +00003474 }
3475
Douglas Gregord2fa7662010-12-20 02:24:11 +00003476 case TYPE_PACK_EXPANSION: {
Douglas Gregor17328502011-02-01 15:24:58 +00003477 if (Record.size() != 2) {
Douglas Gregord2fa7662010-12-20 02:24:11 +00003478 Error("incorrect encoding of pack expansion type");
3479 return QualType();
3480 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003481 QualType Pattern = readType(*Loc.F, Record, Idx);
Douglas Gregord2fa7662010-12-20 02:24:11 +00003482 if (Pattern.isNull())
3483 return QualType();
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00003484 llvm::Optional<unsigned> NumExpansions;
3485 if (Record[1])
3486 NumExpansions = Record[1] - 1;
Douglas Gregor4163aca2011-09-09 21:34:22 +00003487 return Context.getPackExpansionType(Pattern, NumExpansions);
Douglas Gregord2fa7662010-12-20 02:24:11 +00003488 }
3489
Sebastian Redl539c5062010-08-18 23:57:32 +00003490 case TYPE_ELABORATED: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003491 unsigned Idx = 0;
3492 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor7fb09192011-07-21 22:35:25 +00003493 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor903b7e92011-07-22 00:38:23 +00003494 QualType NamedType = readType(*Loc.F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00003495 return Context.getElaboratedType(Keyword, NNS, NamedType);
John McCallfcc33b02009-09-05 00:15:47 +00003496 }
3497
Sebastian Redl539c5062010-08-18 23:57:32 +00003498 case TYPE_OBJC_INTERFACE: {
Chris Lattner587cbe12009-04-22 06:45:28 +00003499 unsigned Idx = 0;
Douglas Gregor7fb09192011-07-21 22:35:25 +00003500 ObjCInterfaceDecl *ItfD
3501 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00003502 return Context.getObjCInterfaceType(ItfD);
John McCall8b07ec22010-05-15 11:32:37 +00003503 }
3504
Sebastian Redl539c5062010-08-18 23:57:32 +00003505 case TYPE_OBJC_OBJECT: {
John McCall8b07ec22010-05-15 11:32:37 +00003506 unsigned Idx = 0;
Douglas Gregor903b7e92011-07-22 00:38:23 +00003507 QualType Base = readType(*Loc.F, Record, Idx);
Chris Lattner587cbe12009-04-22 06:45:28 +00003508 unsigned NumProtos = Record[Idx++];
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003509 SmallVector<ObjCProtocolDecl*, 4> Protos;
Chris Lattner587cbe12009-04-22 06:45:28 +00003510 for (unsigned I = 0; I != NumProtos; ++I)
Douglas Gregor7fb09192011-07-21 22:35:25 +00003511 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
Douglas Gregor4163aca2011-09-09 21:34:22 +00003512 return Context.getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattner587cbe12009-04-22 06:45:28 +00003513 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003514
Sebastian Redl539c5062010-08-18 23:57:32 +00003515 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattner6e054af2009-04-22 06:40:03 +00003516 unsigned Idx = 0;
Douglas Gregor903b7e92011-07-22 00:38:23 +00003517 QualType Pointee = readType(*Loc.F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00003518 return Context.getObjCObjectPointerType(Pointee);
Chris Lattner6e054af2009-04-22 06:40:03 +00003519 }
Argyrios Kyrtzidisa7a36df2009-09-29 19:42:55 +00003520
Sebastian Redl539c5062010-08-18 23:57:32 +00003521 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCallcebee162009-10-18 09:09:24 +00003522 unsigned Idx = 0;
Douglas Gregor903b7e92011-07-22 00:38:23 +00003523 QualType Parm = readType(*Loc.F, Record, Idx);
3524 QualType Replacement = readType(*Loc.F, Record, Idx);
John McCallcebee162009-10-18 09:09:24 +00003525 return
Douglas Gregor4163aca2011-09-09 21:34:22 +00003526 Context.getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
John McCallcebee162009-10-18 09:09:24 +00003527 Replacement);
3528 }
John McCalle78aac42010-03-10 03:28:59 +00003529
Douglas Gregorada4b792011-01-14 02:55:32 +00003530 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
3531 unsigned Idx = 0;
Douglas Gregor903b7e92011-07-22 00:38:23 +00003532 QualType Parm = readType(*Loc.F, Record, Idx);
Douglas Gregorada4b792011-01-14 02:55:32 +00003533 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00003534 return Context.getSubstTemplateTypeParmPackType(
Douglas Gregorada4b792011-01-14 02:55:32 +00003535 cast<TemplateTypeParmType>(Parm),
3536 ArgPack);
3537 }
3538
Sebastian Redl539c5062010-08-18 23:57:32 +00003539 case TYPE_INJECTED_CLASS_NAME: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00003540 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
Douglas Gregor903b7e92011-07-22 00:38:23 +00003541 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00003542 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003543 // for AST reading, too much interdependencies.
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00003544 return
Douglas Gregor4163aca2011-09-09 21:34:22 +00003545 QualType(new (Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCalle78aac42010-03-10 03:28:59 +00003546 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003547
Sebastian Redl539c5062010-08-18 23:57:32 +00003548 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003549 unsigned Idx = 0;
3550 unsigned Depth = Record[Idx++];
3551 unsigned Index = Record[Idx++];
3552 bool Pack = Record[Idx++];
Douglas Gregor7fb09192011-07-21 22:35:25 +00003553 TemplateTypeParmDecl *D
3554 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00003555 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003556 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003557
Sebastian Redl539c5062010-08-18 23:57:32 +00003558 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00003559 unsigned Idx = 0;
3560 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor7fb09192011-07-21 22:35:25 +00003561 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregora3e41532011-07-28 20:55:49 +00003562 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Douglas Gregor903b7e92011-07-22 00:38:23 +00003563 QualType Canon = readType(*Loc.F, Record, Idx);
Douglas Gregorf86c9392010-10-26 00:51:02 +00003564 if (!Canon.isNull())
Douglas Gregor4163aca2011-09-09 21:34:22 +00003565 Canon = Context.getCanonicalType(Canon);
3566 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00003567 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003568
Sebastian Redl539c5062010-08-18 23:57:32 +00003569 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003570 unsigned Idx = 0;
3571 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor7fb09192011-07-21 22:35:25 +00003572 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregora3e41532011-07-28 20:55:49 +00003573 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003574 unsigned NumArgs = Record[Idx++];
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003575 SmallVector<TemplateArgument, 8> Args;
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003576 Args.reserve(NumArgs);
3577 while (NumArgs--)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003578 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Douglas Gregor4163aca2011-09-09 21:34:22 +00003579 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003580 Args.size(), Args.data());
3581 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003582
Sebastian Redl539c5062010-08-18 23:57:32 +00003583 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00003584 unsigned Idx = 0;
3585
3586 // ArrayType
Douglas Gregor903b7e92011-07-22 00:38:23 +00003587 QualType ElementType = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00003588 ArrayType::ArraySizeModifier ASM
3589 = (ArrayType::ArraySizeModifier)Record[Idx++];
3590 unsigned IndexTypeQuals = Record[Idx++];
3591
3592 // DependentSizedArrayType
Sebastian Redl2c373b92010-10-05 15:59:54 +00003593 Expr *NumElts = ReadExpr(*Loc.F);
3594 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00003595
Douglas Gregor4163aca2011-09-09 21:34:22 +00003596 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00003597 IndexTypeQuals, Brackets);
3598 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003599
Sebastian Redl539c5062010-08-18 23:57:32 +00003600 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003601 unsigned Idx = 0;
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003602 bool IsDependent = Record[Idx++];
Douglas Gregor5590be02011-01-15 06:45:20 +00003603 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003604 SmallVector<TemplateArgument, 8> Args;
Sebastian Redl2c373b92010-10-05 15:59:54 +00003605 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Douglas Gregor903b7e92011-07-22 00:38:23 +00003606 QualType Underlying = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003607 QualType T;
Richard Smith3f1b5d02011-05-05 21:57:07 +00003608 if (Underlying.isNull())
Douglas Gregor4163aca2011-09-09 21:34:22 +00003609 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003610 Args.size());
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003611 else
Douglas Gregor4163aca2011-09-09 21:34:22 +00003612 T = Context.getTemplateSpecializationType(Name, Args.data(),
Richard Smith3f1b5d02011-05-05 21:57:07 +00003613 Args.size(), Underlying);
John McCall424cec92011-01-19 06:33:43 +00003614 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003615 return T;
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003616 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003617 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003618 // Suppress a GCC warning
3619 return QualType();
3620}
3621
Sebastian Redl2c373b92010-10-05 15:59:54 +00003622class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redl2c499f62010-08-18 23:56:43 +00003623 ASTReader &Reader;
Douglas Gregora6895d82011-07-22 16:00:58 +00003624 Module &F;
Sebastian Redlc67764e2010-07-22 22:43:28 +00003625 llvm::BitstreamCursor &DeclsCursor;
Sebastian Redl2c499f62010-08-18 23:56:43 +00003626 const ASTReader::RecordData &Record;
John McCall8f115c62009-10-16 21:56:05 +00003627 unsigned &Idx;
3628
Sebastian Redl2c373b92010-10-05 15:59:54 +00003629 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
3630 unsigned &I) {
3631 return Reader.ReadSourceLocation(F, R, I);
3632 }
3633
Douglas Gregor7fb09192011-07-21 22:35:25 +00003634 template<typename T>
3635 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
3636 return Reader.ReadDeclAs<T>(F, Record, Idx);
3637 }
3638
John McCall8f115c62009-10-16 21:56:05 +00003639public:
Douglas Gregora6895d82011-07-22 16:00:58 +00003640 TypeLocReader(ASTReader &Reader, Module &F,
Sebastian Redl2c499f62010-08-18 23:56:43 +00003641 const ASTReader::RecordData &Record, unsigned &Idx)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003642 : Reader(Reader), F(F), DeclsCursor(F.DeclsCursor), Record(Record), Idx(Idx)
3643 { }
John McCall8f115c62009-10-16 21:56:05 +00003644
John McCall17001972009-10-18 01:05:36 +00003645 // We want compile-time assurance that we've enumerated all of
3646 // these, so unfortunately we have to declare them first, then
3647 // define them out-of-line.
3648#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +00003649#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +00003650 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +00003651#include "clang/AST/TypeLocNodes.def"
3652
John McCall17001972009-10-18 01:05:36 +00003653 void VisitFunctionTypeLoc(FunctionTypeLoc);
3654 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCall8f115c62009-10-16 21:56:05 +00003655};
3656
John McCall17001972009-10-18 01:05:36 +00003657void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCall8f115c62009-10-16 21:56:05 +00003658 // nothing to do
3659}
John McCall17001972009-10-18 01:05:36 +00003660void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003661 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorc9b7a592010-01-18 18:04:31 +00003662 if (TL.needsExtraLocalData()) {
3663 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
3664 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
3665 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
3666 TL.setModeAttr(Record[Idx++]);
3667 }
John McCall8f115c62009-10-16 21:56:05 +00003668}
John McCall17001972009-10-18 01:05:36 +00003669void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003670 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003671}
John McCall17001972009-10-18 01:05:36 +00003672void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003673 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003674}
John McCall17001972009-10-18 01:05:36 +00003675void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003676 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003677}
John McCall17001972009-10-18 01:05:36 +00003678void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003679 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003680}
John McCall17001972009-10-18 01:05:36 +00003681void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003682 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003683}
John McCall17001972009-10-18 01:05:36 +00003684void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003685 TL.setStarLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnara509357842011-03-05 14:42:21 +00003686 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003687}
John McCall17001972009-10-18 01:05:36 +00003688void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003689 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
3690 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003691 if (Record[Idx++])
Sebastian Redl2c373b92010-10-05 15:59:54 +00003692 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor12bfa382009-10-17 00:13:19 +00003693 else
John McCall17001972009-10-18 01:05:36 +00003694 TL.setSizeExpr(0);
3695}
3696void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
3697 VisitArrayTypeLoc(TL);
3698}
3699void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
3700 VisitArrayTypeLoc(TL);
3701}
3702void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
3703 VisitArrayTypeLoc(TL);
3704}
3705void TypeLocReader::VisitDependentSizedArrayTypeLoc(
3706 DependentSizedArrayTypeLoc TL) {
3707 VisitArrayTypeLoc(TL);
3708}
3709void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
3710 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003711 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003712}
3713void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003714 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003715}
3716void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003717 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003718}
3719void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003720 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
3721 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
Douglas Gregor7fb25412010-10-01 18:44:50 +00003722 TL.setTrailingReturn(Record[Idx++]);
John McCall17001972009-10-18 01:05:36 +00003723 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
Douglas Gregor7fb09192011-07-21 22:35:25 +00003724 TL.setArg(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003725 }
3726}
3727void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
3728 VisitFunctionTypeLoc(TL);
3729}
3730void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
3731 VisitFunctionTypeLoc(TL);
3732}
John McCallb96ec562009-12-04 22:46:56 +00003733void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003734 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallb96ec562009-12-04 22:46:56 +00003735}
John McCall17001972009-10-18 01:05:36 +00003736void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003737 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003738}
3739void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003740 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3741 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3742 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003743}
3744void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003745 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3746 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3747 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3748 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003749}
3750void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003751 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003752}
Alexis Hunte852b102011-05-24 22:41:36 +00003753void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
3754 TL.setKWLoc(ReadSourceLocation(Record, Idx));
3755 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3756 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3757 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
3758}
Richard Smith30482bc2011-02-20 03:19:35 +00003759void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
3760 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3761}
John McCall17001972009-10-18 01:05:36 +00003762void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003763 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003764}
3765void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003766 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003767}
John McCall81904512011-01-06 01:58:22 +00003768void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
3769 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
3770 if (TL.hasAttrOperand()) {
3771 SourceRange range;
3772 range.setBegin(ReadSourceLocation(Record, Idx));
3773 range.setEnd(ReadSourceLocation(Record, Idx));
3774 TL.setAttrOperandParensRange(range);
3775 }
3776 if (TL.hasAttrExprOperand()) {
3777 if (Record[Idx++])
3778 TL.setAttrExprOperand(Reader.ReadExpr(F));
3779 else
3780 TL.setAttrExprOperand(0);
3781 } else if (TL.hasAttrEnumOperand())
3782 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
3783}
John McCall17001972009-10-18 01:05:36 +00003784void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003785 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003786}
John McCallcebee162009-10-18 09:09:24 +00003787void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
3788 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003789 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallcebee162009-10-18 09:09:24 +00003790}
Douglas Gregorada4b792011-01-14 02:55:32 +00003791void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
3792 SubstTemplateTypeParmPackTypeLoc TL) {
3793 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3794}
John McCall17001972009-10-18 01:05:36 +00003795void TypeLocReader::VisitTemplateSpecializationTypeLoc(
3796 TemplateSpecializationTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003797 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
3798 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3799 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall0ad16662009-10-29 08:12:44 +00003800 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
3801 TL.setArgLocInfo(i,
Sebastian Redl2c373b92010-10-05 15:59:54 +00003802 Reader.GetTemplateArgumentLocInfo(F,
3803 TL.getTypePtr()->getArg(i).getKind(),
3804 Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003805}
Abramo Bagnara924a8f32010-12-10 16:29:40 +00003806void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
3807 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3808 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3809}
Abramo Bagnara6150c882010-05-11 21:36:43 +00003810void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003811 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor844cb502011-03-01 18:12:44 +00003812 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003813}
John McCalle78aac42010-03-10 03:28:59 +00003814void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003815 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalle78aac42010-03-10 03:28:59 +00003816}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00003817void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003818 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor3d0da5f2011-03-01 01:34:45 +00003819 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redl2c373b92010-10-05 15:59:54 +00003820 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003821}
John McCallc392f372010-06-11 00:33:02 +00003822void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
3823 DependentTemplateSpecializationTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003824 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregora7a795b2011-03-01 20:11:18 +00003825 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redl2c373b92010-10-05 15:59:54 +00003826 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3827 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3828 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCallc392f372010-06-11 00:33:02 +00003829 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
3830 TL.setArgLocInfo(I,
Sebastian Redl2c373b92010-10-05 15:59:54 +00003831 Reader.GetTemplateArgumentLocInfo(F,
3832 TL.getTypePtr()->getArg(I).getKind(),
3833 Record, Idx));
John McCallc392f372010-06-11 00:33:02 +00003834}
Douglas Gregord2fa7662010-12-20 02:24:11 +00003835void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
3836 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
3837}
John McCall17001972009-10-18 01:05:36 +00003838void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003839 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall8b07ec22010-05-15 11:32:37 +00003840}
3841void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3842 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003843 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3844 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003845 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003846 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003847}
John McCallfc93cf92009-10-22 22:37:11 +00003848void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003849 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCallfc93cf92009-10-22 22:37:11 +00003850}
John McCall8f115c62009-10-16 21:56:05 +00003851
Douglas Gregora6895d82011-07-22 16:00:58 +00003852TypeSourceInfo *ASTReader::GetTypeSourceInfo(Module &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003853 const RecordData &Record,
John McCall8f115c62009-10-16 21:56:05 +00003854 unsigned &Idx) {
Douglas Gregor903b7e92011-07-22 00:38:23 +00003855 QualType InfoTy = readType(F, Record, Idx);
John McCall8f115c62009-10-16 21:56:05 +00003856 if (InfoTy.isNull())
3857 return 0;
3858
Douglas Gregor4163aca2011-09-09 21:34:22 +00003859 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003860 TypeLocReader TLR(*this, F, Record, Idx);
John McCallbcd03502009-12-07 02:54:59 +00003861 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCall8f115c62009-10-16 21:56:05 +00003862 TLR.Visit(TL);
John McCallbcd03502009-12-07 02:54:59 +00003863 return TInfo;
John McCall8f115c62009-10-16 21:56:05 +00003864}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003865
Sebastian Redl539c5062010-08-18 23:57:32 +00003866QualType ASTReader::GetType(TypeID ID) {
John McCall8ccfcb52009-09-24 19:53:00 +00003867 unsigned FastQuals = ID & Qualifiers::FastMask;
3868 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003869
Sebastian Redl539c5062010-08-18 23:57:32 +00003870 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003871 QualType T;
Sebastian Redl539c5062010-08-18 23:57:32 +00003872 switch ((PredefinedTypeIDs)Index) {
3873 case PREDEF_TYPE_NULL_ID: return QualType();
Douglas Gregor4163aca2011-09-09 21:34:22 +00003874 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
3875 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003876
Sebastian Redl539c5062010-08-18 23:57:32 +00003877 case PREDEF_TYPE_CHAR_U_ID:
3878 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003879 // FIXME: Check that the signedness of CharTy is correct!
Douglas Gregor4163aca2011-09-09 21:34:22 +00003880 T = Context.CharTy;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003881 break;
3882
Douglas Gregor4163aca2011-09-09 21:34:22 +00003883 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
3884 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
3885 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
3886 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
3887 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
3888 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break;
3889 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
3890 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
3891 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
3892 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
3893 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
3894 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
3895 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break;
3896 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
3897 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
3898 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
3899 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
3900 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break;
3901 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
3902 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break;
3903 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break;
3904 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break;
3905 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break;
3906 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break;
3907 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break;
3908 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break;
3909 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break;
Douglas Gregoreda8e122011-08-09 15:13:55 +00003910
3911 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
Douglas Gregor4163aca2011-09-09 21:34:22 +00003912 T = Context.getAutoRRefDeductType();
Douglas Gregoreda8e122011-08-09 15:13:55 +00003913 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003914 }
3915
3916 assert(!T.isNull() && "Unknown predefined type");
John McCall8ccfcb52009-09-24 19:53:00 +00003917 return T.withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003918 }
3919
Sebastian Redl539c5062010-08-18 23:57:32 +00003920 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003921 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl409183f2010-07-14 20:26:45 +00003922 if (TypesLoaded[Index].isNull()) {
Douglas Gregor903b7e92011-07-22 00:38:23 +00003923 TypesLoaded[Index] = readTypeRecord(Index);
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003924 if (TypesLoaded[Index].isNull())
3925 return QualType();
3926
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003927 TypesLoaded[Index]->setFromAST();
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003928 if (DeserializationListener)
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003929 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003930 TypesLoaded[Index]);
Sebastian Redl409183f2010-07-14 20:26:45 +00003931 }
Mike Stump11289f42009-09-09 15:08:12 +00003932
John McCall8ccfcb52009-09-24 19:53:00 +00003933 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003934}
3935
Douglas Gregora6895d82011-07-22 16:00:58 +00003936QualType ASTReader::getLocalType(Module &F, unsigned LocalID) {
Douglas Gregor903b7e92011-07-22 00:38:23 +00003937 return GetType(getGlobalTypeID(F, LocalID));
3938}
3939
3940serialization::TypeID
Douglas Gregora6895d82011-07-22 16:00:58 +00003941ASTReader::getGlobalTypeID(Module &F, unsigned LocalID) const {
Douglas Gregor5204bde2011-08-02 16:26:37 +00003942 unsigned FastQuals = LocalID & Qualifiers::FastMask;
3943 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
3944
3945 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
3946 return LocalID;
3947
3948 ContinuousRangeMap<uint32_t, int, 2>::iterator I
3949 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
3950 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
3951
3952 unsigned GlobalIndex = LocalIndex + I->second;
3953 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
3954}
3955
John McCall0ad16662009-10-29 08:12:44 +00003956TemplateArgumentLocInfo
Douglas Gregora6895d82011-07-22 16:00:58 +00003957ASTReader::GetTemplateArgumentLocInfo(Module &F,
Sebastian Redl2c373b92010-10-05 15:59:54 +00003958 TemplateArgument::ArgKind Kind,
John McCall0ad16662009-10-29 08:12:44 +00003959 const RecordData &Record,
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003960 unsigned &Index) {
John McCall0ad16662009-10-29 08:12:44 +00003961 switch (Kind) {
3962 case TemplateArgument::Expression:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003963 return ReadExpr(F);
John McCall0ad16662009-10-29 08:12:44 +00003964 case TemplateArgument::Type:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003965 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003966 case TemplateArgument::Template: {
Douglas Gregor9d802122011-03-02 17:09:35 +00003967 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
3968 Index);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003969 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregor9d802122011-03-02 17:09:35 +00003970 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003971 SourceLocation());
3972 }
3973 case TemplateArgument::TemplateExpansion: {
Douglas Gregor9d802122011-03-02 17:09:35 +00003974 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
3975 Index);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003976 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregoreb29d182011-01-05 17:40:24 +00003977 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregor9d802122011-03-02 17:09:35 +00003978 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregoreb29d182011-01-05 17:40:24 +00003979 EllipsisLoc);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003980 }
John McCall0ad16662009-10-29 08:12:44 +00003981 case TemplateArgument::Null:
3982 case TemplateArgument::Integral:
3983 case TemplateArgument::Declaration:
3984 case TemplateArgument::Pack:
3985 return TemplateArgumentLocInfo();
3986 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +00003987 llvm_unreachable("unexpected template argument loc");
John McCall0ad16662009-10-29 08:12:44 +00003988 return TemplateArgumentLocInfo();
3989}
3990
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003991TemplateArgumentLoc
Douglas Gregora6895d82011-07-22 16:00:58 +00003992ASTReader::ReadTemplateArgumentLoc(Module &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003993 const RecordData &Record, unsigned &Index) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003994 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003995
3996 if (Arg.getKind() == TemplateArgument::Expression) {
3997 if (Record[Index++]) // bool InfoHasSameExpr.
3998 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
3999 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00004000 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00004001 Record, Index));
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004002}
4003
Sebastian Redl2c499f62010-08-18 23:56:43 +00004004Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall75b960e2010-06-01 09:23:16 +00004005 return GetDecl(ID);
4006}
4007
Douglas Gregorc27b2872011-08-04 00:01:48 +00004008uint64_t ASTReader::readCXXBaseSpecifiers(Module &M, const RecordData &Record,
4009 unsigned &Idx){
4010 if (Idx >= Record.size())
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004011 return 0;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004012
Douglas Gregorc27b2872011-08-04 00:01:48 +00004013 unsigned LocalID = Record[Idx++];
4014 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004015}
4016
4017CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
Douglas Gregord32f0352011-07-22 06:10:01 +00004018 RecordLocation Loc = getLocalBitOffset(Offset);
4019 llvm::BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004020 SavedStreamPosition SavedPosition(Cursor);
Douglas Gregord32f0352011-07-22 06:10:01 +00004021 Cursor.JumpToBit(Loc.Offset);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004022 ReadingKindTracker ReadingKind(Read_Decl, *this);
4023 RecordData Record;
4024 unsigned Code = Cursor.ReadCode();
4025 unsigned RecCode = Cursor.ReadRecord(Code, Record);
4026 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
4027 Error("Malformed AST file: missing C++ base specifiers");
4028 return 0;
4029 }
4030
4031 unsigned Idx = 0;
4032 unsigned NumBases = Record[Idx++];
Douglas Gregor4163aca2011-09-09 21:34:22 +00004033 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004034 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
4035 for (unsigned I = 0; I != NumBases; ++I)
Douglas Gregord32f0352011-07-22 06:10:01 +00004036 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004037 return Bases;
4038}
4039
Douglas Gregor7fb09192011-07-21 22:35:25 +00004040serialization::DeclID
Douglas Gregora6895d82011-07-22 16:00:58 +00004041ASTReader::getGlobalDeclID(Module &F, unsigned LocalID) const {
Douglas Gregor6f8912e2011-08-03 16:05:40 +00004042 if (LocalID < NUM_PREDEF_DECL_IDS)
Douglas Gregorf7180622011-08-03 15:48:04 +00004043 return LocalID;
4044
4045 ContinuousRangeMap<uint32_t, int, 2>::iterator I
Douglas Gregor6f8912e2011-08-03 16:05:40 +00004046 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
Douglas Gregorf7180622011-08-03 15:48:04 +00004047 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
4048
4049 return LocalID + I->second;
Douglas Gregor7fb09192011-07-21 22:35:25 +00004050}
4051
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004052bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
4053 Module &M) const {
4054 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
4055 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
4056 return &M == I->second;
4057}
4058
Sebastian Redl539c5062010-08-18 23:57:32 +00004059Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregor6f8912e2011-08-03 16:05:40 +00004060 if (ID < NUM_PREDEF_DECL_IDS) {
4061 switch ((PredefinedDeclIDs)ID) {
Douglas Gregordab42432011-08-12 00:15:20 +00004062 case PREDEF_DECL_NULL_ID:
Douglas Gregor6f8912e2011-08-03 16:05:40 +00004063 return 0;
Douglas Gregordab42432011-08-12 00:15:20 +00004064
4065 case PREDEF_DECL_TRANSLATION_UNIT_ID:
Douglas Gregor4163aca2011-09-09 21:34:22 +00004066 return Context.getTranslationUnitDecl();
Douglas Gregor3ea72692011-08-12 05:46:01 +00004067
4068 case PREDEF_DECL_OBJC_ID_ID:
Douglas Gregor4163aca2011-09-09 21:34:22 +00004069 return Context.getObjCIdDecl();
Douglas Gregor0a586182011-08-12 05:59:41 +00004070
Douglas Gregor52e02802011-08-12 06:17:30 +00004071 case PREDEF_DECL_OBJC_SEL_ID:
Douglas Gregor4163aca2011-09-09 21:34:22 +00004072 return Context.getObjCSelDecl();
Douglas Gregor52e02802011-08-12 06:17:30 +00004073
Douglas Gregor0a586182011-08-12 05:59:41 +00004074 case PREDEF_DECL_OBJC_CLASS_ID:
Douglas Gregor4163aca2011-09-09 21:34:22 +00004075 return Context.getObjCClassDecl();
Douglas Gregor801c99d2011-08-12 06:49:56 +00004076
4077 case PREDEF_DECL_INT_128_ID:
Douglas Gregor4163aca2011-09-09 21:34:22 +00004078 return Context.getInt128Decl();
Douglas Gregor801c99d2011-08-12 06:49:56 +00004079
4080 case PREDEF_DECL_UNSIGNED_INT_128_ID:
Douglas Gregor4163aca2011-09-09 21:34:22 +00004081 return Context.getUInt128Decl();
Douglas Gregorbab8a962011-09-08 01:46:34 +00004082
4083 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
Douglas Gregor4163aca2011-09-09 21:34:22 +00004084 return Context.getObjCInstanceTypeDecl();
Douglas Gregor6f8912e2011-08-03 16:05:40 +00004085 }
4086
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004087 return 0;
Douglas Gregor6f8912e2011-08-03 16:05:40 +00004088 }
4089
Douglas Gregordab42432011-08-12 00:15:20 +00004090 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
4091
4092 if (Index > DeclsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004093 Error("declaration ID out-of-range for AST file");
Douglas Gregor745ed142009-04-25 18:35:21 +00004094 return 0;
4095 }
Douglas Gregordab42432011-08-12 00:15:20 +00004096
4097if (!DeclsLoaded[Index]) {
Douglas Gregorf7180622011-08-03 15:48:04 +00004098 ReadDeclRecord(ID);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00004099 if (DeserializationListener)
4100 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
4101 }
Douglas Gregor745ed142009-04-25 18:35:21 +00004102
4103 return DeclsLoaded[Index];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004104}
4105
Douglas Gregora6895d82011-07-22 16:00:58 +00004106serialization::DeclID ASTReader::ReadDeclID(Module &F,
Douglas Gregor7fb09192011-07-21 22:35:25 +00004107 const RecordData &Record,
4108 unsigned &Idx) {
4109 if (Idx >= Record.size()) {
4110 Error("Corrupted AST file");
4111 return 0;
4112 }
4113
4114 return getGlobalDeclID(F, Record[Idx++]);
4115}
4116
Chris Lattner9c28af02009-04-27 05:46:25 +00004117/// \brief Resolve the offset of a statement into a statement.
4118///
4119/// This operation will read a new statement from the external
4120/// source each time it is called, and is meant to be used via a
4121/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redl2c499f62010-08-18 23:56:43 +00004122Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidisd9f526f2010-10-28 09:29:32 +00004123 // Switch case IDs are per Decl.
4124 ClearSwitchCaseIDs();
4125
Sebastian Redl5c415f32010-07-22 17:01:13 +00004126 // Offset here is a global offset across the entire chain.
Douglas Gregord32f0352011-07-22 06:10:01 +00004127 RecordLocation Loc = getLocalBitOffset(Offset);
4128 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
4129 return ReadStmtFromStream(*Loc.F);
Douglas Gregor3c3aa612009-04-18 00:07:54 +00004130}
4131
Douglas Gregor1257f972011-08-24 21:27:34 +00004132namespace {
4133 class FindExternalLexicalDeclsVisitor {
4134 ASTReader &Reader;
4135 const DeclContext *DC;
4136 bool (*isKindWeWant)(Decl::Kind);
Douglas Gregor4e4c83e2011-08-26 22:04:51 +00004137
Douglas Gregor1257f972011-08-24 21:27:34 +00004138 SmallVectorImpl<Decl*> &Decls;
4139 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
4140
4141 public:
4142 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
4143 bool (*isKindWeWant)(Decl::Kind),
4144 SmallVectorImpl<Decl*> &Decls)
4145 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
4146 {
4147 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
4148 PredefsVisited[I] = false;
4149 }
4150
4151 static bool visit(Module &M, bool Preorder, void *UserData) {
4152 if (Preorder)
4153 return false;
4154
4155 FindExternalLexicalDeclsVisitor *This
4156 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
4157
4158 Module::DeclContextInfosMap::iterator Info
4159 = M.DeclContextInfos.find(This->DC);
4160 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
4161 return false;
4162
4163 // Load all of the declaration IDs
4164 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
4165 *IDE = ID + Info->second.NumLexicalDecls;
4166 ID != IDE; ++ID) {
4167 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
4168 continue;
4169
4170 // Don't add predefined declarations to the lexical context more
4171 // than once.
4172 if (ID->second < NUM_PREDEF_DECL_IDS) {
4173 if (This->PredefsVisited[ID->second])
4174 continue;
4175
4176 This->PredefsVisited[ID->second] = true;
4177 }
4178
Douglas Gregor4e4c83e2011-08-26 22:04:51 +00004179 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
4180 if (!This->DC->isDeclInLexicalTraversal(D))
4181 This->Decls.push_back(D);
4182 }
Douglas Gregor1257f972011-08-24 21:27:34 +00004183 }
4184
4185 return false;
4186 }
4187 };
4188}
4189
Douglas Gregor3d0adb32011-07-15 21:46:17 +00004190ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00004191 bool (*isKindWeWant)(Decl::Kind),
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004192 SmallVectorImpl<Decl*> &Decls) {
Douglas Gregor94619c82011-08-24 19:03:07 +00004193 // There might be lexical decls in multiple modules, for the TU at
Douglas Gregor1257f972011-08-24 21:27:34 +00004194 // least. Walk all of the modules in the order they were loaded.
4195 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
4196 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00004197 ++NumLexicalDeclContextsRead;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00004198 return ELR_Success;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004199}
4200
Douglas Gregor94619c82011-08-24 19:03:07 +00004201namespace {
4202 /// \brief Module visitor used to perform name lookup into a
4203 /// declaration context.
4204 class DeclContextNameLookupVisitor {
4205 ASTReader &Reader;
4206 const DeclContext *DC;
4207 DeclarationName Name;
4208 SmallVectorImpl<NamedDecl *> &Decls;
4209
4210 public:
4211 DeclContextNameLookupVisitor(ASTReader &Reader,
4212 const DeclContext *DC, DeclarationName Name,
4213 SmallVectorImpl<NamedDecl *> &Decls)
4214 : Reader(Reader), DC(DC), Name(Name), Decls(Decls) { }
4215
4216 static bool visit(Module &M, void *UserData) {
4217 DeclContextNameLookupVisitor *This
4218 = static_cast<DeclContextNameLookupVisitor *>(UserData);
4219
4220 // Check whether we have any visible declaration information for
4221 // this context in this module.
4222 Module::DeclContextInfosMap::iterator Info
4223 = M.DeclContextInfos.find(This->DC);
4224 if (Info == M.DeclContextInfos.end() || !Info->second.NameLookupTableData)
4225 return false;
4226
4227 // Look for this name within this module.
4228 ASTDeclContextNameLookupTable *LookupTable =
4229 (ASTDeclContextNameLookupTable*)Info->second.NameLookupTableData;
4230 ASTDeclContextNameLookupTable::iterator Pos
4231 = LookupTable->find(This->Name);
4232 if (Pos == LookupTable->end())
4233 return false;
4234
4235 bool FoundAnything = false;
4236 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
4237 for (; Data.first != Data.second; ++Data.first) {
4238 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
4239 if (!ND)
4240 continue;
4241
4242 if (ND->getDeclName() != This->Name) {
4243 assert(!This->Name.getCXXNameType().isNull() &&
4244 "Name mismatch without a type");
4245 continue;
4246 }
4247
4248 // Record this declaration.
4249 FoundAnything = true;
4250 This->Decls.push_back(ND);
4251 }
4252
4253 return FoundAnything;
4254 }
4255 };
4256}
4257
John McCall75b960e2010-06-01 09:23:16 +00004258DeclContext::lookup_result
Sebastian Redl2c499f62010-08-18 23:56:43 +00004259ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall75b960e2010-06-01 09:23:16 +00004260 DeclarationName Name) {
Mike Stump11289f42009-09-09 15:08:12 +00004261 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004262 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004263 if (!Name)
4264 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
4265 DeclContext::lookup_iterator(0));
Ted Kremenek1ff615c2010-03-18 00:56:54 +00004266
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004267 SmallVector<NamedDecl *, 64> Decls;
Douglas Gregor94619c82011-08-24 19:03:07 +00004268 DeclContextNameLookupVisitor Visitor(*this, DC, Name, Decls);
4269 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00004270 ++NumVisibleDeclContextsRead;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004271 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall75b960e2010-06-01 09:23:16 +00004272 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004273}
4274
Sebastian Redl2c499f62010-08-18 23:56:43 +00004275void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004276 assert(Consumer);
4277 while (!InterestingDecls.empty()) {
4278 DeclGroupRef DG(InterestingDecls.front());
4279 InterestingDecls.pop_front();
Sebastian Redleaa4ade2010-08-11 18:52:41 +00004280 Consumer->HandleInterestingDecl(DG);
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004281 }
4282}
4283
Sebastian Redl2c499f62010-08-18 23:56:43 +00004284void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregorb985eeb2009-04-22 19:09:20 +00004285 this->Consumer = Consumer;
4286
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00004287 if (!Consumer)
4288 return;
4289
4290 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004291 // Force deserialization of this decl, which will cause it to be queued for
4292 // passing to the consumer.
Daniel Dunbar865c2a72009-09-17 03:06:44 +00004293 GetDecl(ExternalDefinitions[I]);
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00004294 }
Douglas Gregorf005eac2009-04-25 00:41:30 +00004295
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004296 PassInterestingDeclsToConsumer();
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00004297}
4298
Sebastian Redl2c499f62010-08-18 23:56:43 +00004299void ASTReader::PrintStats() {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004300 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004301
Mike Stump11289f42009-09-09 15:08:12 +00004302 unsigned NumTypesLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00004303 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall8ccfcb52009-09-24 19:53:00 +00004304 QualType());
Douglas Gregor0e149972009-04-25 19:10:14 +00004305 unsigned NumDeclsLoaded
4306 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
4307 (Decl *)0);
4308 unsigned NumIdentifiersLoaded
4309 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
4310 IdentifiersLoaded.end(),
4311 (IdentifierInfo *)0);
Mike Stump11289f42009-09-09 15:08:12 +00004312 unsigned NumSelectorsLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00004313 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
4314 SelectorsLoaded.end(),
4315 Selector());
Douglas Gregorc3b1dd12009-04-13 20:50:16 +00004316
Douglas Gregorc5046832009-04-27 18:38:38 +00004317 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
4318 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor49bf76b2011-07-21 18:46:38 +00004319 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
Douglas Gregor258ae542009-04-27 06:38:32 +00004320 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
4321 NumSLocEntriesRead, TotalNumSLocEntries,
4322 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor745ed142009-04-25 18:35:21 +00004323 if (!TypesLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00004324 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00004325 NumTypesLoaded, (unsigned)TypesLoaded.size(),
4326 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
4327 if (!DeclsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00004328 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00004329 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
4330 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor0e149972009-04-25 19:10:14 +00004331 if (!IdentifiersLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00004332 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor0e149972009-04-25 19:10:14 +00004333 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
4334 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redlada023c2010-08-04 20:40:17 +00004335 if (!SelectorsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00004336 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redlada023c2010-08-04 20:40:17 +00004337 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
4338 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor95c13f52009-04-25 17:48:32 +00004339 if (TotalNumStatements)
4340 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
4341 NumStatementsRead, TotalNumStatements,
4342 ((float)NumStatementsRead/TotalNumStatements * 100));
4343 if (TotalNumMacros)
4344 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
4345 NumMacrosRead, TotalNumMacros,
4346 ((float)NumMacrosRead/TotalNumMacros * 100));
4347 if (TotalLexicalDeclContexts)
4348 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
4349 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
4350 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
4351 * 100));
4352 if (TotalVisibleDeclContexts)
4353 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
4354 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
4355 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
4356 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004357 if (TotalNumMethodPoolEntries) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004358 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004359 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
4360 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor95c13f52009-04-25 17:48:32 +00004361 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004362 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor95c13f52009-04-25 17:48:32 +00004363 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004364 std::fprintf(stderr, "\n");
Douglas Gregor204b8712011-07-21 19:50:14 +00004365 dump();
4366 std::fprintf(stderr, "\n");
4367}
4368
Douglas Gregora6895d82011-07-22 16:00:58 +00004369template<typename Key, typename Module, unsigned InitialCapacity>
Douglas Gregor204b8712011-07-21 19:50:14 +00004370static void
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004371dumpModuleIDMap(StringRef Name,
Douglas Gregora6895d82011-07-22 16:00:58 +00004372 const ContinuousRangeMap<Key, Module *,
Douglas Gregor204b8712011-07-21 19:50:14 +00004373 InitialCapacity> &Map) {
4374 if (Map.begin() == Map.end())
4375 return;
4376
Douglas Gregora6895d82011-07-22 16:00:58 +00004377 typedef ContinuousRangeMap<Key, Module *, InitialCapacity> MapType;
Douglas Gregor204b8712011-07-21 19:50:14 +00004378 llvm::errs() << Name << ":\n";
4379 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
4380 I != IEnd; ++I) {
4381 llvm::errs() << " " << I->first << " -> " << I->second->FileName
4382 << "\n";
4383 }
4384}
4385
Douglas Gregor204b8712011-07-21 19:50:14 +00004386void ASTReader::dump() {
Douglas Gregor1cc9c062011-08-02 11:12:41 +00004387 llvm::errs() << "*** PCH/Module Remappings:\n";
Douglas Gregord32f0352011-07-22 06:10:01 +00004388 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
Douglas Gregor204b8712011-07-21 19:50:14 +00004389 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
Douglas Gregor8ab4ea82011-07-29 00:21:44 +00004390 dumpModuleIDMap("Global type map", GlobalTypeMap);
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00004391 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00004392 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
4393 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
4394 dumpModuleIDMap("Global macro definition map", GlobalMacroDefinitionMap);
4395 dumpModuleIDMap("Global preprocessed entity map",
4396 GlobalPreprocessedEntityMap);
Douglas Gregor1cc9c062011-08-02 11:12:41 +00004397
4398 llvm::errs() << "\n*** PCH/Modules Loaded:";
4399 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
4400 MEnd = ModuleMgr.end();
4401 M != MEnd; ++M)
4402 (*M)->dump();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004403}
4404
Ted Kremenek5e1ed7b2011-04-28 23:46:20 +00004405/// Return the amount of memory used by memory buffers, breaking down
4406/// by heap-backed versus mmap'ed memory.
4407void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00004408 for (ModuleConstIterator I = ModuleMgr.begin(),
4409 E = ModuleMgr.end(); I != E; ++I) {
4410 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
Ted Kremenek5e1ed7b2011-04-28 23:46:20 +00004411 size_t bytes = buf->getBufferSize();
4412 switch (buf->getBufferKind()) {
4413 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
4414 sizes.malloc_bytes += bytes;
4415 break;
4416 case llvm::MemoryBuffer::MemoryBuffer_MMap:
4417 sizes.mmap_bytes += bytes;
4418 break;
4419 }
4420 }
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00004421 }
Ted Kremenek5e1ed7b2011-04-28 23:46:20 +00004422}
4423
Sebastian Redl2c499f62010-08-18 23:56:43 +00004424void ASTReader::InitializeSema(Sema &S) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00004425 SemaObj = &S;
Douglas Gregorc78d3462009-04-24 21:10:55 +00004426 S.ExternalSource = this;
4427
Douglas Gregor7cd60f72009-04-22 21:15:06 +00004428 // Makes sure any declarations that were deserialized "too early"
4429 // still get added to the identifier's declaration chains.
Douglas Gregor2fb99df2010-09-24 23:29:12 +00004430 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
4431 if (SemaObj->TUScope)
John McCall48871652010-08-21 09:40:31 +00004432 SemaObj->TUScope->AddDecl(PreloadedDecls[I]);
Douglas Gregor2fb99df2010-09-24 23:29:12 +00004433
4434 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregora868bbd2009-04-21 22:25:48 +00004435 }
Douglas Gregor7cd60f72009-04-22 21:15:06 +00004436 PreloadedDecls.clear();
Douglas Gregord4df8652009-04-22 22:02:47 +00004437
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004438 // Load the offsets of the declarations that Sema references.
4439 // They will be lazily deserialized when needed.
4440 if (!SemaDeclRefs.empty()) {
4441 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
Douglas Gregorb0f3ae62011-07-28 00:57:24 +00004442 if (!SemaObj->StdNamespace)
4443 SemaObj->StdNamespace = SemaDeclRefs[0];
4444 if (!SemaObj->StdBadAlloc)
4445 SemaObj->StdBadAlloc = SemaDeclRefs[1];
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004446 }
4447
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004448 if (!FPPragmaOptions.empty()) {
4449 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
4450 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
4451 }
4452
4453 if (!OpenCLExtensions.empty()) {
4454 unsigned I = 0;
4455#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
4456#include "clang/Basic/OpenCLExtensions.def"
4457
4458 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
4459 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00004460}
4461
Douglas Gregorab443b92011-08-20 04:39:52 +00004462IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Douglas Gregorab443b92011-08-20 04:39:52 +00004463 IdentifierLookupVisitor Visitor(StringRef(NameStart, NameEnd - NameStart));
4464 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
4465 return Visitor.getIdentifierInfo();
Douglas Gregora868bbd2009-04-21 22:25:48 +00004466}
4467
Douglas Gregor57756ea2010-10-14 22:11:03 +00004468namespace clang {
4469 /// \brief An identifier-lookup iterator that enumerates all of the
4470 /// identifiers stored within a set of AST files.
4471 class ASTIdentifierIterator : public IdentifierIterator {
4472 /// \brief The AST reader whose identifiers are being enumerated.
4473 const ASTReader &Reader;
4474
4475 /// \brief The current index into the chain of AST files stored in
4476 /// the AST reader.
4477 unsigned Index;
4478
4479 /// \brief The current position within the identifier lookup table
4480 /// of the current AST file.
4481 ASTIdentifierLookupTable::key_iterator Current;
4482
4483 /// \brief The end position within the identifier lookup table of
4484 /// the current AST file.
4485 ASTIdentifierLookupTable::key_iterator End;
4486
4487 public:
4488 explicit ASTIdentifierIterator(const ASTReader &Reader);
4489
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004490 virtual StringRef Next();
Douglas Gregor57756ea2010-10-14 22:11:03 +00004491 };
4492}
4493
4494ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00004495 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
Douglas Gregor57756ea2010-10-14 22:11:03 +00004496 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00004497 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
Douglas Gregor57756ea2010-10-14 22:11:03 +00004498 Current = IdTable->key_begin();
4499 End = IdTable->key_end();
4500}
4501
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004502StringRef ASTIdentifierIterator::Next() {
Douglas Gregor57756ea2010-10-14 22:11:03 +00004503 while (Current == End) {
4504 // If we have exhausted all of our AST files, we're done.
4505 if (Index == 0)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004506 return StringRef();
Douglas Gregor57756ea2010-10-14 22:11:03 +00004507
4508 --Index;
4509 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00004510 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
4511 IdentifierLookupTable;
Douglas Gregor57756ea2010-10-14 22:11:03 +00004512 Current = IdTable->key_begin();
4513 End = IdTable->key_end();
4514 }
4515
4516 // We have any identifiers remaining in the current AST file; return
4517 // the next one.
4518 std::pair<const char*, unsigned> Key = *Current;
4519 ++Current;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004520 return StringRef(Key.first, Key.second);
Douglas Gregor57756ea2010-10-14 22:11:03 +00004521}
4522
4523IdentifierIterator *ASTReader::getIdentifiers() const {
4524 return new ASTIdentifierIterator(*this);
4525}
4526
Douglas Gregorc10edd62011-08-25 14:51:20 +00004527namespace clang { namespace serialization {
4528 class ReadMethodPoolVisitor {
4529 ASTReader &Reader;
4530 Selector Sel;
4531 llvm::SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
4532 llvm::SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Douglas Gregorc78d3462009-04-24 21:10:55 +00004533
Douglas Gregorc10edd62011-08-25 14:51:20 +00004534 /// \brief Build an ObjCMethodList from a vector of Objective-C method
4535 /// declarations.
4536 ObjCMethodList
4537 buildObjCMethodList(const SmallVectorImpl<ObjCMethodDecl *> &Vec) const
4538 {
4539 ObjCMethodList List;
4540 ObjCMethodList *Prev = 0;
4541 for (unsigned I = 0, N = Vec.size(); I != N; ++I) {
4542 if (!List.Method) {
4543 // This is the first method, which is the easy case.
4544 List.Method = Vec[I];
4545 Prev = &List;
4546 continue;
4547 }
4548
4549 ObjCMethodList *Mem =
4550 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
4551 Prev->Next = new (Mem) ObjCMethodList(Vec[I], 0);
4552 Prev = Prev->Next;
4553 }
4554
4555 return List;
4556 }
4557
4558 public:
4559 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel)
4560 : Reader(Reader), Sel(Sel) { }
4561
4562 static bool visit(Module &M, void *UserData) {
4563 ReadMethodPoolVisitor *This
4564 = static_cast<ReadMethodPoolVisitor *>(UserData);
4565
4566 if (!M.SelectorLookupTable)
4567 return false;
4568
4569 ASTSelectorLookupTable *PoolTable
4570 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
4571 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
4572 if (Pos == PoolTable->end())
4573 return false;
4574
4575 ++This->Reader.NumSelectorsRead;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004576 // FIXME: Not quite happy with the statistics here. We probably should
4577 // disable this tracking when called via LoadSelector.
4578 // Also, should entries without methods count as misses?
Douglas Gregorc10edd62011-08-25 14:51:20 +00004579 ++This->Reader.NumMethodPoolEntriesRead;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004580 ASTSelectorLookupTrait::data_type Data = *Pos;
Douglas Gregorc10edd62011-08-25 14:51:20 +00004581 if (This->Reader.DeserializationListener)
4582 This->Reader.DeserializationListener->SelectorRead(Data.ID,
4583 This->Sel);
4584
4585 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
4586 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
4587 return true;
Sebastian Redlada023c2010-08-04 20:40:17 +00004588 }
Douglas Gregorc10edd62011-08-25 14:51:20 +00004589
4590 /// \brief Retrieve the instance methods found by this visitor.
4591 ObjCMethodList getInstanceMethods() const {
4592 return buildObjCMethodList(InstanceMethods);
4593 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00004594
Douglas Gregorc10edd62011-08-25 14:51:20 +00004595 /// \brief Retrieve the instance methods found by this visitor.
4596 ObjCMethodList getFactoryMethods() const {
4597 return buildObjCMethodList(FactoryMethods);
4598 }
4599 };
4600} } // end namespace clang::serialization
4601
4602std::pair<ObjCMethodList, ObjCMethodList>
4603ASTReader::ReadMethodPool(Selector Sel) {
4604 ReadMethodPoolVisitor Visitor(*this, Sel);
4605 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
4606 std::pair<ObjCMethodList, ObjCMethodList> Result;
4607 Result.first = Visitor.getInstanceMethods();
4608 Result.second = Visitor.getFactoryMethods();
4609
4610 if (!Result.first.Method && !Result.second.Method)
4611 ++NumMethodPoolMisses;
4612 return Result;
Douglas Gregorc78d3462009-04-24 21:10:55 +00004613}
4614
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004615void ASTReader::ReadKnownNamespaces(
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004616 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004617 Namespaces.clear();
4618
4619 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
4620 if (NamespaceDecl *Namespace
4621 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
4622 Namespaces.push_back(Namespace);
4623 }
4624}
4625
Douglas Gregoreb08bd42011-07-27 20:58:46 +00004626void ASTReader::ReadTentativeDefinitions(
4627 SmallVectorImpl<VarDecl *> &TentativeDefs) {
4628 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
4629 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
4630 if (Var)
4631 TentativeDefs.push_back(Var);
4632 }
4633 TentativeDefinitions.clear();
4634}
4635
Douglas Gregora94a1542011-07-27 21:45:57 +00004636void ASTReader::ReadUnusedFileScopedDecls(
4637 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
4638 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
4639 DeclaratorDecl *D
4640 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
4641 if (D)
4642 Decls.push_back(D);
4643 }
4644 UnusedFileScopedDecls.clear();
4645}
4646
Douglas Gregorbae31202011-07-27 21:57:17 +00004647void ASTReader::ReadDelegatingConstructors(
4648 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
4649 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
4650 CXXConstructorDecl *D
4651 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
4652 if (D)
4653 Decls.push_back(D);
4654 }
4655 DelegatingCtorDecls.clear();
4656}
4657
Douglas Gregorb7098a32011-07-28 00:39:29 +00004658void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
4659 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
4660 TypedefNameDecl *D
4661 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
4662 if (D)
4663 Decls.push_back(D);
4664 }
4665 ExtVectorDecls.clear();
4666}
4667
Douglas Gregor32002192011-07-28 00:53:40 +00004668void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
4669 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
4670 CXXRecordDecl *D
4671 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
4672 if (D)
4673 Decls.push_back(D);
4674 }
4675 DynamicClasses.clear();
4676}
4677
Douglas Gregordc5c9582011-07-28 14:20:37 +00004678void
4679ASTReader::ReadLocallyScopedExternalDecls(SmallVectorImpl<NamedDecl *> &Decls) {
4680 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
4681 NamedDecl *D
4682 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
4683 if (D)
4684 Decls.push_back(D);
4685 }
4686 LocallyScopedExternalDecls.clear();
4687}
4688
Douglas Gregor72e357f2011-07-28 14:54:22 +00004689void ASTReader::ReadReferencedSelectors(
4690 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
4691 if (ReferencedSelectorsData.empty())
4692 return;
4693
4694 // If there are @selector references added them to its pool. This is for
4695 // implementation of -Wselector.
4696 unsigned int DataSize = ReferencedSelectorsData.size()-1;
4697 unsigned I = 0;
4698 while (I < DataSize) {
4699 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
4700 SourceLocation SelLoc
4701 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
4702 Sels.push_back(std::make_pair(Sel, SelLoc));
4703 }
4704 ReferencedSelectorsData.clear();
4705}
4706
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00004707void ASTReader::ReadWeakUndeclaredIdentifiers(
4708 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
4709 if (WeakUndeclaredIdentifiers.empty())
4710 return;
4711
4712 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
4713 IdentifierInfo *WeakId
4714 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
4715 IdentifierInfo *AliasId
4716 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
4717 SourceLocation Loc
4718 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
4719 bool Used = WeakUndeclaredIdentifiers[I++];
4720 WeakInfo WI(AliasId, Loc);
4721 WI.setUsed(Used);
4722 WeakIDs.push_back(std::make_pair(WeakId, WI));
4723 }
4724 WeakUndeclaredIdentifiers.clear();
4725}
4726
Douglas Gregor4daf6a32011-07-28 19:11:31 +00004727void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
4728 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
4729 ExternalVTableUse VT;
4730 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
4731 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
4732 VT.DefinitionRequired = VTableUses[Idx++];
4733 VTables.push_back(VT);
4734 }
4735
4736 VTableUses.clear();
4737}
4738
Douglas Gregore39f97c2011-07-28 19:49:54 +00004739void ASTReader::ReadPendingInstantiations(
4740 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
4741 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
4742 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
4743 SourceLocation Loc
4744 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
4745 Pending.push_back(std::make_pair(D, Loc));
4746 }
4747 PendingInstantiations.clear();
4748}
4749
Sebastian Redl2c499f62010-08-18 23:56:43 +00004750void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00004751 // It would be complicated to avoid reading the methods anyway. So don't.
4752 ReadMethodPool(Sel);
4753}
4754
Douglas Gregora3e41532011-07-28 20:55:49 +00004755void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00004756 assert(ID && "Non-zero identifier ID required");
Douglas Gregor6f00bf82009-04-28 21:53:25 +00004757 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor0e149972009-04-25 19:10:14 +00004758 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlff4a2952010-07-23 23:49:55 +00004759 if (DeserializationListener)
4760 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregora868bbd2009-04-21 22:25:48 +00004761}
4762
Douglas Gregor1342e842009-07-06 18:54:52 +00004763/// \brief Set the globally-visible declarations associated with the given
4764/// identifier.
4765///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004766/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump11289f42009-09-09 15:08:12 +00004767/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregor1342e842009-07-06 18:54:52 +00004768/// them.
4769///
4770/// \param II an IdentifierInfo that refers to one or more globally-visible
4771/// declarations.
4772///
4773/// \param DeclIDs the set of declaration IDs with the name @p II that are
4774/// visible at global scope.
4775///
4776/// \param Nonrecursive should be true to indicate that the caller knows that
4777/// this call is non-recursive, and therefore the globally-visible declarations
4778/// will not be placed onto the pending queue.
Mike Stump11289f42009-09-09 15:08:12 +00004779void
Sebastian Redl2c499f62010-08-18 23:56:43 +00004780ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004781 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregor1342e842009-07-06 18:54:52 +00004782 bool Nonrecursive) {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004783 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregor1342e842009-07-06 18:54:52 +00004784 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
4785 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
4786 PII.II = II;
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00004787 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregor1342e842009-07-06 18:54:52 +00004788 return;
4789 }
Mike Stump11289f42009-09-09 15:08:12 +00004790
Douglas Gregor1342e842009-07-06 18:54:52 +00004791 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
4792 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
4793 if (SemaObj) {
Douglas Gregor6fd55e02010-08-13 03:15:25 +00004794 if (SemaObj->TUScope) {
4795 // Introduce this declaration into the translation-unit scope
4796 // and add it to the declaration chain for this identifier, so
4797 // that (unqualified) name lookup will find it.
John McCall48871652010-08-21 09:40:31 +00004798 SemaObj->TUScope->AddDecl(D);
Douglas Gregor6fd55e02010-08-13 03:15:25 +00004799 }
Douglas Gregor2fb99df2010-09-24 23:29:12 +00004800 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
Douglas Gregor1342e842009-07-06 18:54:52 +00004801 } else {
4802 // Queue this declaration so that it will be added to the
4803 // translation unit scope and identifier's declaration chain
4804 // once a Sema object is known.
4805 PreloadedDecls.push_back(D);
4806 }
4807 }
4808}
4809
Douglas Gregora3e41532011-07-28 20:55:49 +00004810IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004811 if (ID == 0)
4812 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00004813
Sebastian Redlc713b962010-07-21 00:46:22 +00004814 if (IdentifiersLoaded.empty()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004815 Error("no identifier table in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004816 return 0;
4817 }
Mike Stump11289f42009-09-09 15:08:12 +00004818
Sebastian Redlc713b962010-07-21 00:46:22 +00004819 ID -= 1;
4820 if (!IdentifiersLoaded[ID]) {
Douglas Gregor19d26352011-07-20 00:59:32 +00004821 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
4822 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00004823 Module *M = I->second;
4824 unsigned Index = ID - M->BaseIdentifierID;
4825 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
Douglas Gregor5287b4e2009-04-25 21:04:17 +00004826
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004827 // All of the strings in the AST file are preceded by a 16-bit length.
4828 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenekca42a512009-10-23 04:45:31 +00004829 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
4830 // unsigned integers. This is important to avoid integer overflow when
4831 // we cast them to 'unsigned'.
Ted Kremenek49c52322009-10-23 03:57:22 +00004832 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregorab4df582009-04-28 20:01:51 +00004833 unsigned StrLen = (((unsigned) StrLenPtr[0])
4834 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redlc713b962010-07-21 00:46:22 +00004835 IdentifiersLoaded[ID]
Douglas Gregor51825b42011-09-09 22:02:16 +00004836 = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
Sebastian Redlff4a2952010-07-23 23:49:55 +00004837 if (DeserializationListener)
4838 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004839 }
Mike Stump11289f42009-09-09 15:08:12 +00004840
Sebastian Redlc713b962010-07-21 00:46:22 +00004841 return IdentifiersLoaded[ID];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004842}
4843
Douglas Gregora3e41532011-07-28 20:55:49 +00004844IdentifierInfo *ASTReader::getLocalIdentifier(Module &M, unsigned LocalID) {
4845 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
4846}
4847
4848IdentifierID ASTReader::getGlobalIdentifierID(Module &M, unsigned LocalID) {
Douglas Gregor1ab036c2011-08-03 21:49:18 +00004849 if (LocalID < NUM_PREDEF_IDENT_IDS)
4850 return LocalID;
4851
4852 ContinuousRangeMap<uint32_t, int, 2>::iterator I
4853 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
4854 assert(I != M.IdentifierRemap.end()
4855 && "Invalid index into identifier index remap");
4856
4857 return LocalID + I->second;
Douglas Gregora3e41532011-07-28 20:55:49 +00004858}
4859
Douglas Gregor925296b2011-07-19 16:10:42 +00004860bool ASTReader::ReadSLocEntry(int ID) {
Douglas Gregor49f754f2011-04-20 00:21:03 +00004861 return ReadSLocEntryRecord(ID) != Success;
Douglas Gregor258ae542009-04-27 06:38:32 +00004862}
4863
Douglas Gregor074fdc52011-07-28 21:16:51 +00004864Selector ASTReader::getLocalSelector(Module &M, unsigned LocalID) {
4865 return DecodeSelector(getGlobalSelectorID(M, LocalID));
4866}
4867
4868Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
Steve Naroff2ddea052009-04-23 10:39:46 +00004869 if (ID == 0)
4870 return Selector();
Mike Stump11289f42009-09-09 15:08:12 +00004871
Sebastian Redlada023c2010-08-04 20:40:17 +00004872 if (ID > SelectorsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004873 Error("selector ID out of range in AST file");
Steve Naroff2ddea052009-04-23 10:39:46 +00004874 return Selector();
4875 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00004876
Sebastian Redlada023c2010-08-04 20:40:17 +00004877 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004878 // Load this selector from the selector table.
Douglas Gregor2262d282011-07-20 01:10:58 +00004879 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
4880 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00004881 Module &M = *I->second;
4882 ASTSelectorLookupTrait Trait(*this, M);
Douglas Gregor8f364fb2011-08-03 23:28:44 +00004883 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
Douglas Gregor2262d282011-07-20 01:10:58 +00004884 SelectorsLoaded[ID - 1] =
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00004885 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
Douglas Gregor2262d282011-07-20 01:10:58 +00004886 if (DeserializationListener)
4887 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
Douglas Gregor95c13f52009-04-25 17:48:32 +00004888 }
4889
Sebastian Redlada023c2010-08-04 20:40:17 +00004890 return SelectorsLoaded[ID - 1];
Steve Naroff2ddea052009-04-23 10:39:46 +00004891}
4892
Douglas Gregor3f8f04f2011-07-28 14:41:43 +00004893Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
Douglas Gregord720daf2010-04-06 17:30:22 +00004894 return DecodeSelector(ID);
4895}
4896
Sebastian Redl2c499f62010-08-18 23:56:43 +00004897uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redlada023c2010-08-04 20:40:17 +00004898 // ID 0 (the null selector) is considered an external selector.
4899 return getTotalNumSelectors() + 1;
Douglas Gregord720daf2010-04-06 17:30:22 +00004900}
4901
Douglas Gregor8f364fb2011-08-03 23:28:44 +00004902serialization::SelectorID
4903ASTReader::getGlobalSelectorID(Module &M, unsigned LocalID) const {
4904 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
4905 return LocalID;
4906
4907 ContinuousRangeMap<uint32_t, int, 2>::iterator I
4908 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
4909 assert(I != M.SelectorRemap.end()
4910 && "Invalid index into identifier index remap");
4911
4912 return LocalID + I->second;
Douglas Gregor3f8f04f2011-07-28 14:41:43 +00004913}
4914
Mike Stump11289f42009-09-09 15:08:12 +00004915DeclarationName
Douglas Gregora6895d82011-07-22 16:00:58 +00004916ASTReader::ReadDeclarationName(Module &F,
Douglas Gregor903b7e92011-07-22 00:38:23 +00004917 const RecordData &Record, unsigned &Idx) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004918 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
4919 switch (Kind) {
4920 case DeclarationName::Identifier:
Douglas Gregora3e41532011-07-28 20:55:49 +00004921 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004922
4923 case DeclarationName::ObjCZeroArgSelector:
4924 case DeclarationName::ObjCOneArgSelector:
4925 case DeclarationName::ObjCMultiArgSelector:
Douglas Gregor074fdc52011-07-28 21:16:51 +00004926 return DeclarationName(ReadSelector(F, Record, Idx));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004927
4928 case DeclarationName::CXXConstructorName:
Douglas Gregor4163aca2011-09-09 21:34:22 +00004929 return Context.DeclarationNames.getCXXConstructorName(
4930 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004931
4932 case DeclarationName::CXXDestructorName:
Douglas Gregor4163aca2011-09-09 21:34:22 +00004933 return Context.DeclarationNames.getCXXDestructorName(
4934 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004935
4936 case DeclarationName::CXXConversionFunctionName:
Douglas Gregor4163aca2011-09-09 21:34:22 +00004937 return Context.DeclarationNames.getCXXConversionFunctionName(
4938 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004939
4940 case DeclarationName::CXXOperatorName:
Douglas Gregor4163aca2011-09-09 21:34:22 +00004941 return Context.DeclarationNames.getCXXOperatorName(
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004942 (OverloadedOperatorKind)Record[Idx++]);
4943
Alexis Hunt3d221f22009-11-29 07:34:05 +00004944 case DeclarationName::CXXLiteralOperatorName:
Douglas Gregor4163aca2011-09-09 21:34:22 +00004945 return Context.DeclarationNames.getCXXLiteralOperatorName(
Douglas Gregora3e41532011-07-28 20:55:49 +00004946 GetIdentifierInfo(F, Record, Idx));
Alexis Hunt3d221f22009-11-29 07:34:05 +00004947
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004948 case DeclarationName::CXXUsingDirective:
4949 return DeclarationName::getUsingDirectiveName();
4950 }
4951
4952 // Required to silence GCC warning
4953 return DeclarationName();
4954}
Douglas Gregor55abb232009-04-10 20:39:37 +00004955
Douglas Gregora6895d82011-07-22 16:00:58 +00004956void ASTReader::ReadDeclarationNameLoc(Module &F,
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004957 DeclarationNameLoc &DNLoc,
4958 DeclarationName Name,
4959 const RecordData &Record, unsigned &Idx) {
4960 switch (Name.getNameKind()) {
4961 case DeclarationName::CXXConstructorName:
4962 case DeclarationName::CXXDestructorName:
4963 case DeclarationName::CXXConversionFunctionName:
4964 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
4965 break;
4966
4967 case DeclarationName::CXXOperatorName:
4968 DNLoc.CXXOperatorName.BeginOpNameLoc
4969 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4970 DNLoc.CXXOperatorName.EndOpNameLoc
4971 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4972 break;
4973
4974 case DeclarationName::CXXLiteralOperatorName:
4975 DNLoc.CXXLiteralOperatorName.OpNameLoc
4976 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4977 break;
4978
4979 case DeclarationName::Identifier:
4980 case DeclarationName::ObjCZeroArgSelector:
4981 case DeclarationName::ObjCOneArgSelector:
4982 case DeclarationName::ObjCMultiArgSelector:
4983 case DeclarationName::CXXUsingDirective:
4984 break;
4985 }
4986}
4987
Douglas Gregora6895d82011-07-22 16:00:58 +00004988void ASTReader::ReadDeclarationNameInfo(Module &F,
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004989 DeclarationNameInfo &NameInfo,
4990 const RecordData &Record, unsigned &Idx) {
Douglas Gregor903b7e92011-07-22 00:38:23 +00004991 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004992 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
4993 DeclarationNameLoc DNLoc;
4994 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
4995 NameInfo.setInfo(DNLoc);
4996}
4997
Douglas Gregora6895d82011-07-22 16:00:58 +00004998void ASTReader::ReadQualifierInfo(Module &F, QualifierInfo &Info,
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004999 const RecordData &Record, unsigned &Idx) {
Douglas Gregor14454802011-02-25 02:25:35 +00005000 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005001 unsigned NumTPLists = Record[Idx++];
5002 Info.NumTemplParamLists = NumTPLists;
5003 if (NumTPLists) {
Douglas Gregor4163aca2011-09-09 21:34:22 +00005004 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005005 for (unsigned i=0; i != NumTPLists; ++i)
5006 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
5007 }
5008}
5009
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005010TemplateName
Douglas Gregora6895d82011-07-22 16:00:58 +00005011ASTReader::ReadTemplateName(Module &F, const RecordData &Record,
Douglas Gregor5590be02011-01-15 06:45:20 +00005012 unsigned &Idx) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005013 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005014 switch (Kind) {
5015 case TemplateName::Template:
Douglas Gregor7fb09192011-07-21 22:35:25 +00005016 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005017
5018 case TemplateName::OverloadedTemplate: {
5019 unsigned size = Record[Idx++];
5020 UnresolvedSet<8> Decls;
5021 while (size--)
Douglas Gregor7fb09192011-07-21 22:35:25 +00005022 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005023
Douglas Gregor4163aca2011-09-09 21:34:22 +00005024 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005025 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005026
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005027 case TemplateName::QualifiedTemplate: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00005028 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005029 bool hasTemplKeyword = Record[Idx++];
Douglas Gregor7fb09192011-07-21 22:35:25 +00005030 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00005031 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005032 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005033
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005034 case TemplateName::DependentTemplate: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00005035 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005036 if (Record[Idx++]) // isIdentifier
Douglas Gregor4163aca2011-09-09 21:34:22 +00005037 return Context.getDependentTemplateName(NNS,
Douglas Gregora3e41532011-07-28 20:55:49 +00005038 GetIdentifierInfo(F, Record,
5039 Idx));
Douglas Gregor4163aca2011-09-09 21:34:22 +00005040 return Context.getDependentTemplateName(NNS,
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00005041 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005042 }
John McCalld9dfe3a2011-06-30 08:33:18 +00005043
5044 case TemplateName::SubstTemplateTemplateParm: {
5045 TemplateTemplateParmDecl *param
Douglas Gregor7fb09192011-07-21 22:35:25 +00005046 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
John McCalld9dfe3a2011-06-30 08:33:18 +00005047 if (!param) return TemplateName();
5048 TemplateName replacement = ReadTemplateName(F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00005049 return Context.getSubstTemplateTemplateParm(param, replacement);
John McCalld9dfe3a2011-06-30 08:33:18 +00005050 }
Douglas Gregor5590be02011-01-15 06:45:20 +00005051
5052 case TemplateName::SubstTemplateTemplateParmPack: {
5053 TemplateTemplateParmDecl *Param
Douglas Gregor7fb09192011-07-21 22:35:25 +00005054 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
Douglas Gregor5590be02011-01-15 06:45:20 +00005055 if (!Param)
5056 return TemplateName();
5057
5058 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
5059 if (ArgPack.getKind() != TemplateArgument::Pack)
5060 return TemplateName();
5061
Douglas Gregor4163aca2011-09-09 21:34:22 +00005062 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
Douglas Gregor5590be02011-01-15 06:45:20 +00005063 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005064 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005065
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005066 assert(0 && "Unhandled template name kind!");
5067 return TemplateName();
5068}
5069
5070TemplateArgument
Douglas Gregora6895d82011-07-22 16:00:58 +00005071ASTReader::ReadTemplateArgument(Module &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00005072 const RecordData &Record, unsigned &Idx) {
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005073 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
5074 switch (Kind) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005075 case TemplateArgument::Null:
5076 return TemplateArgument();
5077 case TemplateArgument::Type:
Douglas Gregor903b7e92011-07-22 00:38:23 +00005078 return TemplateArgument(readType(F, Record, Idx));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005079 case TemplateArgument::Declaration:
Douglas Gregor7fb09192011-07-21 22:35:25 +00005080 return TemplateArgument(ReadDecl(F, Record, Idx));
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +00005081 case TemplateArgument::Integral: {
5082 llvm::APSInt Value = ReadAPSInt(Record, Idx);
Douglas Gregor903b7e92011-07-22 00:38:23 +00005083 QualType T = readType(F, Record, Idx);
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +00005084 return TemplateArgument(Value, T);
5085 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005086 case TemplateArgument::Template:
Douglas Gregor5590be02011-01-15 06:45:20 +00005087 return TemplateArgument(ReadTemplateName(F, Record, Idx));
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005088 case TemplateArgument::TemplateExpansion: {
Douglas Gregor5590be02011-01-15 06:45:20 +00005089 TemplateName Name = ReadTemplateName(F, Record, Idx);
Douglas Gregore1d60df2011-01-14 23:41:42 +00005090 llvm::Optional<unsigned> NumTemplateExpansions;
5091 if (unsigned NumExpansions = Record[Idx++])
5092 NumTemplateExpansions = NumExpansions - 1;
5093 return TemplateArgument(Name, NumTemplateExpansions);
Douglas Gregoreb29d182011-01-05 17:40:24 +00005094 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005095 case TemplateArgument::Expression:
Sebastian Redl2c373b92010-10-05 15:59:54 +00005096 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005097 case TemplateArgument::Pack: {
5098 unsigned NumArgs = Record[Idx++];
Douglas Gregor4163aca2011-09-09 21:34:22 +00005099 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
Douglas Gregor1ccc8412010-11-07 23:05:16 +00005100 for (unsigned I = 0; I != NumArgs; ++I)
5101 Args[I] = ReadTemplateArgument(F, Record, Idx);
5102 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005103 }
5104 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005105
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005106 assert(0 && "Unhandled template argument kind!");
5107 return TemplateArgument();
5108}
5109
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005110TemplateParameterList *
Douglas Gregora6895d82011-07-22 16:00:58 +00005111ASTReader::ReadTemplateParameterList(Module &F,
Sebastian Redl2c373b92010-10-05 15:59:54 +00005112 const RecordData &Record, unsigned &Idx) {
5113 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
5114 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
5115 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005116
5117 unsigned NumParams = Record[Idx++];
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005118 SmallVector<NamedDecl *, 16> Params;
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005119 Params.reserve(NumParams);
5120 while (NumParams--)
Douglas Gregor7fb09192011-07-21 22:35:25 +00005121 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005122
5123 TemplateParameterList* TemplateParams =
Douglas Gregor4163aca2011-09-09 21:34:22 +00005124 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005125 Params.data(), Params.size(), RAngleLoc);
5126 return TemplateParams;
5127}
5128
5129void
Sebastian Redl2c499f62010-08-18 23:56:43 +00005130ASTReader::
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005131ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs,
Douglas Gregora6895d82011-07-22 16:00:58 +00005132 Module &F, const RecordData &Record,
Sebastian Redl2c373b92010-10-05 15:59:54 +00005133 unsigned &Idx) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005134 unsigned NumTemplateArgs = Record[Idx++];
5135 TemplArgs.reserve(NumTemplateArgs);
5136 while (NumTemplateArgs--)
Sebastian Redl2c373b92010-10-05 15:59:54 +00005137 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005138}
5139
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005140/// \brief Read a UnresolvedSet structure.
Douglas Gregora6895d82011-07-22 16:00:58 +00005141void ASTReader::ReadUnresolvedSet(Module &F, UnresolvedSetImpl &Set,
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005142 const RecordData &Record, unsigned &Idx) {
5143 unsigned NumDecls = Record[Idx++];
5144 while (NumDecls--) {
Douglas Gregor7fb09192011-07-21 22:35:25 +00005145 NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx);
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005146 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
5147 Set.addDecl(D, AS);
5148 }
5149}
5150
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005151CXXBaseSpecifier
Douglas Gregora6895d82011-07-22 16:00:58 +00005152ASTReader::ReadCXXBaseSpecifier(Module &F,
Nick Lewycky19b9f952010-07-26 16:56:01 +00005153 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005154 bool isVirtual = static_cast<bool>(Record[Idx++]);
5155 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
5156 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redl08905022011-02-05 19:23:19 +00005157 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00005158 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
5159 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor752a5952011-01-03 22:36:02 +00005160 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redl08905022011-02-05 19:23:19 +00005161 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
Douglas Gregor752a5952011-01-03 22:36:02 +00005162 EllipsisLoc);
Sebastian Redl08905022011-02-05 19:23:19 +00005163 Result.setInheritConstructors(inheritConstructors);
5164 return Result;
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005165}
5166
Alexis Hunt1d792652011-01-08 20:30:50 +00005167std::pair<CXXCtorInitializer **, unsigned>
Douglas Gregora6895d82011-07-22 16:00:58 +00005168ASTReader::ReadCXXCtorInitializers(Module &F, const RecordData &Record,
Alexis Hunt1d792652011-01-08 20:30:50 +00005169 unsigned &Idx) {
5170 CXXCtorInitializer **CtorInitializers = 0;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005171 unsigned NumInitializers = Record[Idx++];
5172 if (NumInitializers) {
Alexis Hunt1d792652011-01-08 20:30:50 +00005173 CtorInitializers
Douglas Gregor4163aca2011-09-09 21:34:22 +00005174 = new (Context) CXXCtorInitializer*[NumInitializers];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005175 for (unsigned i=0; i != NumInitializers; ++i) {
5176 TypeSourceInfo *BaseClassInfo = 0;
5177 bool IsBaseVirtual = false;
5178 FieldDecl *Member = 0;
Francois Pichetd583da02010-12-04 09:14:42 +00005179 IndirectFieldDecl *IndirectMember = 0;
Alexis Hunt37a477f2011-05-04 01:19:08 +00005180 CXXConstructorDecl *Target = 0;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005181
Alexis Hunt37a477f2011-05-04 01:19:08 +00005182 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
5183 switch (Type) {
5184 case CTOR_INITIALIZER_BASE:
Sebastian Redl2c373b92010-10-05 15:59:54 +00005185 BaseClassInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005186 IsBaseVirtual = Record[Idx++];
Alexis Hunt37a477f2011-05-04 01:19:08 +00005187 break;
5188
5189 case CTOR_INITIALIZER_DELEGATING:
Douglas Gregor7fb09192011-07-21 22:35:25 +00005190 Target = ReadDeclAs<CXXConstructorDecl>(F, Record, Idx);
Alexis Hunt37a477f2011-05-04 01:19:08 +00005191 break;
5192
5193 case CTOR_INITIALIZER_MEMBER:
Douglas Gregor7fb09192011-07-21 22:35:25 +00005194 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
Alexis Hunt37a477f2011-05-04 01:19:08 +00005195 break;
5196
5197 case CTOR_INITIALIZER_INDIRECT_MEMBER:
Douglas Gregor7fb09192011-07-21 22:35:25 +00005198 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
Alexis Hunt37a477f2011-05-04 01:19:08 +00005199 break;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005200 }
Alexis Hunt37a477f2011-05-04 01:19:08 +00005201
Douglas Gregor44e7df62011-01-04 00:32:56 +00005202 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redl2c373b92010-10-05 15:59:54 +00005203 Expr *Init = ReadExpr(F);
Sebastian Redl2c373b92010-10-05 15:59:54 +00005204 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
5205 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005206 bool IsWritten = Record[Idx++];
5207 unsigned SourceOrderOrNumArrayIndices;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005208 SmallVector<VarDecl *, 8> Indices;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005209 if (IsWritten) {
5210 SourceOrderOrNumArrayIndices = Record[Idx++];
5211 } else {
5212 SourceOrderOrNumArrayIndices = Record[Idx++];
5213 Indices.reserve(SourceOrderOrNumArrayIndices);
5214 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
Douglas Gregor7fb09192011-07-21 22:35:25 +00005215 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005216 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005217
Alexis Hunt1d792652011-01-08 20:30:50 +00005218 CXXCtorInitializer *BOMInit;
Alexis Hunt37a477f2011-05-04 01:19:08 +00005219 if (Type == CTOR_INITIALIZER_BASE) {
Douglas Gregor4163aca2011-09-09 21:34:22 +00005220 BOMInit = new (Context) CXXCtorInitializer(Context, BaseClassInfo, IsBaseVirtual,
Alexis Hunt1d792652011-01-08 20:30:50 +00005221 LParenLoc, Init, RParenLoc,
5222 MemberOrEllipsisLoc);
Alexis Hunt37a477f2011-05-04 01:19:08 +00005223 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
Douglas Gregor4163aca2011-09-09 21:34:22 +00005224 BOMInit = new (Context) CXXCtorInitializer(Context, MemberOrEllipsisLoc, LParenLoc,
Alexis Hunt37a477f2011-05-04 01:19:08 +00005225 Target, Init, RParenLoc);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005226 } else if (IsWritten) {
Francois Pichetd583da02010-12-04 09:14:42 +00005227 if (Member)
Douglas Gregor4163aca2011-09-09 21:34:22 +00005228 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc,
Alexis Hunt1d792652011-01-08 20:30:50 +00005229 LParenLoc, Init, RParenLoc);
Francois Pichetd583da02010-12-04 09:14:42 +00005230 else
Douglas Gregor4163aca2011-09-09 21:34:22 +00005231 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
Alexis Hunt1d792652011-01-08 20:30:50 +00005232 MemberOrEllipsisLoc, LParenLoc,
5233 Init, RParenLoc);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005234 } else {
Douglas Gregor4163aca2011-09-09 21:34:22 +00005235 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc,
Alexis Hunt1d792652011-01-08 20:30:50 +00005236 LParenLoc, Init, RParenLoc,
5237 Indices.data(), Indices.size());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005238 }
5239
Argyrios Kyrtzidisd05f3e32010-09-06 19:04:27 +00005240 if (IsWritten)
5241 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Alexis Hunt1d792652011-01-08 20:30:50 +00005242 CtorInitializers[i] = BOMInit;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005243 }
5244 }
5245
Alexis Hunt1d792652011-01-08 20:30:50 +00005246 return std::make_pair(CtorInitializers, NumInitializers);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005247}
5248
Chris Lattnerca025db2010-05-07 21:43:38 +00005249NestedNameSpecifier *
Douglas Gregora6895d82011-07-22 16:00:58 +00005250ASTReader::ReadNestedNameSpecifier(Module &F,
Douglas Gregor7fb09192011-07-21 22:35:25 +00005251 const RecordData &Record, unsigned &Idx) {
Chris Lattnerca025db2010-05-07 21:43:38 +00005252 unsigned N = Record[Idx++];
5253 NestedNameSpecifier *NNS = 0, *Prev = 0;
5254 for (unsigned I = 0; I != N; ++I) {
5255 NestedNameSpecifier::SpecifierKind Kind
5256 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
5257 switch (Kind) {
5258 case NestedNameSpecifier::Identifier: {
Douglas Gregora3e41532011-07-28 20:55:49 +00005259 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00005260 NNS = NestedNameSpecifier::Create(Context, Prev, II);
Chris Lattnerca025db2010-05-07 21:43:38 +00005261 break;
5262 }
5263
5264 case NestedNameSpecifier::Namespace: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00005265 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00005266 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
Chris Lattnerca025db2010-05-07 21:43:38 +00005267 break;
5268 }
5269
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005270 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00005271 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00005272 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005273 break;
5274 }
5275
Chris Lattnerca025db2010-05-07 21:43:38 +00005276 case NestedNameSpecifier::TypeSpec:
5277 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregor903b7e92011-07-22 00:38:23 +00005278 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
Douglas Gregor0cdc8322010-12-10 17:03:06 +00005279 if (!T)
5280 return 0;
5281
Chris Lattnerca025db2010-05-07 21:43:38 +00005282 bool Template = Record[Idx++];
Douglas Gregor4163aca2011-09-09 21:34:22 +00005283 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
Chris Lattnerca025db2010-05-07 21:43:38 +00005284 break;
5285 }
5286
5287 case NestedNameSpecifier::Global: {
Douglas Gregor4163aca2011-09-09 21:34:22 +00005288 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
Chris Lattnerca025db2010-05-07 21:43:38 +00005289 // No associated value, and there can't be a prefix.
5290 break;
5291 }
Chris Lattnerca025db2010-05-07 21:43:38 +00005292 }
Argyrios Kyrtzidisad65c692010-07-07 15:46:30 +00005293 Prev = NNS;
Chris Lattnerca025db2010-05-07 21:43:38 +00005294 }
5295 return NNS;
5296}
5297
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005298NestedNameSpecifierLoc
Douglas Gregora6895d82011-07-22 16:00:58 +00005299ASTReader::ReadNestedNameSpecifierLoc(Module &F, const RecordData &Record,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005300 unsigned &Idx) {
5301 unsigned N = Record[Idx++];
Douglas Gregor9b272512011-02-28 23:58:31 +00005302 NestedNameSpecifierLocBuilder Builder;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005303 for (unsigned I = 0; I != N; ++I) {
5304 NestedNameSpecifier::SpecifierKind Kind
5305 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
5306 switch (Kind) {
5307 case NestedNameSpecifier::Identifier: {
Douglas Gregora3e41532011-07-28 20:55:49 +00005308 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005309 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00005310 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005311 break;
5312 }
5313
5314 case NestedNameSpecifier::Namespace: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00005315 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005316 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00005317 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005318 break;
5319 }
5320
5321 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00005322 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005323 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00005324 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005325 break;
5326 }
5327
5328 case NestedNameSpecifier::TypeSpec:
5329 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005330 bool Template = Record[Idx++];
5331 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
5332 if (!T)
5333 return NestedNameSpecifierLoc();
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005334 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00005335
5336 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
Douglas Gregor4163aca2011-09-09 21:34:22 +00005337 Builder.Extend(Context,
Douglas Gregor9b272512011-02-28 23:58:31 +00005338 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
5339 T->getTypeLoc(), ColonColonLoc);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005340 break;
5341 }
5342
5343 case NestedNameSpecifier::Global: {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005344 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00005345 Builder.MakeGlobal(Context, ColonColonLoc);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005346 break;
5347 }
5348 }
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005349 }
5350
Douglas Gregor4163aca2011-09-09 21:34:22 +00005351 return Builder.getWithLocInContext(Context);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005352}
5353
Chris Lattnerca025db2010-05-07 21:43:38 +00005354SourceRange
Douglas Gregora6895d82011-07-22 16:00:58 +00005355ASTReader::ReadSourceRange(Module &F, const RecordData &Record,
Sebastian Redl2c373b92010-10-05 15:59:54 +00005356 unsigned &Idx) {
5357 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
5358 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar6d3bc082010-06-02 15:47:10 +00005359 return SourceRange(beg, end);
Chris Lattnerca025db2010-05-07 21:43:38 +00005360}
5361
Douglas Gregor1daeb692009-04-13 18:14:40 +00005362/// \brief Read an integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00005363llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00005364 unsigned BitWidth = Record[Idx++];
5365 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
5366 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
5367 Idx += NumWords;
5368 return Result;
5369}
5370
5371/// \brief Read a signed integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00005372llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00005373 bool isUnsigned = Record[Idx++];
5374 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
5375}
5376
Douglas Gregore0a3a512009-04-14 21:55:33 +00005377/// \brief Read a floating-point value
Sebastian Redl2c499f62010-08-18 23:56:43 +00005378llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00005379 return llvm::APFloat(ReadAPInt(Record, Idx));
5380}
5381
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00005382// \brief Read a string
Sebastian Redl2c499f62010-08-18 23:56:43 +00005383std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00005384 unsigned Len = Record[Idx++];
Jay Foad7d0479f2009-05-21 09:52:38 +00005385 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00005386 Idx += Len;
5387 return Result;
5388}
5389
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005390VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
5391 unsigned &Idx) {
5392 unsigned Major = Record[Idx++];
5393 unsigned Minor = Record[Idx++];
5394 unsigned Subminor = Record[Idx++];
5395 if (Minor == 0)
5396 return VersionTuple(Major);
5397 if (Subminor == 0)
5398 return VersionTuple(Major, Minor - 1);
5399 return VersionTuple(Major, Minor - 1, Subminor - 1);
5400}
5401
Douglas Gregora6895d82011-07-22 16:00:58 +00005402CXXTemporary *ASTReader::ReadCXXTemporary(Module &F,
Douglas Gregor7fb09192011-07-21 22:35:25 +00005403 const RecordData &Record,
Chris Lattnercba86142010-05-10 00:25:06 +00005404 unsigned &Idx) {
Douglas Gregor7fb09192011-07-21 22:35:25 +00005405 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00005406 return CXXTemporary::Create(Context, Decl);
Chris Lattnercba86142010-05-10 00:25:06 +00005407}
5408
Sebastian Redl2c499f62010-08-18 23:56:43 +00005409DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregor92863e42009-04-10 23:10:45 +00005410 return Diag(SourceLocation(), DiagID);
5411}
5412
Sebastian Redl2c499f62010-08-18 23:56:43 +00005413DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00005414 return Diags.Report(Loc, DiagID);
Douglas Gregor55abb232009-04-10 20:39:37 +00005415}
Douglas Gregora9af1d12009-04-17 00:04:06 +00005416
Douglas Gregora868bbd2009-04-21 22:25:48 +00005417/// \brief Retrieve the identifier table associated with the
5418/// preprocessor.
Sebastian Redl2c499f62010-08-18 23:56:43 +00005419IdentifierTable &ASTReader::getIdentifierTable() {
Douglas Gregor51825b42011-09-09 22:02:16 +00005420 return PP.getIdentifierTable();
Douglas Gregora868bbd2009-04-21 22:25:48 +00005421}
5422
Douglas Gregora9af1d12009-04-17 00:04:06 +00005423/// \brief Record that the given ID maps to the given switch-case
5424/// statement.
Sebastian Redl2c499f62010-08-18 23:56:43 +00005425void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Douglas Gregora9af1d12009-04-17 00:04:06 +00005426 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
5427 SwitchCaseStmts[ID] = SC;
5428}
5429
5430/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00005431SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Douglas Gregora9af1d12009-04-17 00:04:06 +00005432 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
5433 return SwitchCaseStmts[ID];
5434}
Douglas Gregor6cc68a42009-04-17 18:18:49 +00005435
Argyrios Kyrtzidisd9f526f2010-10-28 09:29:32 +00005436void ASTReader::ClearSwitchCaseIDs() {
5437 SwitchCaseStmts.clear();
5438}
5439
Sebastian Redl2c499f62010-08-18 23:56:43 +00005440void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00005441 assert(NumCurrentElementsDeserializing &&
5442 "FinishedDeserializing not paired with StartedDeserializing");
5443 if (NumCurrentElementsDeserializing == 1) {
Douglas Gregor1342e842009-07-06 18:54:52 +00005444 // If any identifiers with corresponding top-level declarations have
5445 // been loaded, load those declarations now.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00005446 while (!PendingIdentifierInfos.empty()) {
5447 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
5448 PendingIdentifierInfos.front().DeclIDs, true);
5449 PendingIdentifierInfos.pop_front();
Douglas Gregor1342e842009-07-06 18:54:52 +00005450 }
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00005451
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00005452 // Ready to load previous declarations of Decls that were delayed.
5453 while (!PendingPreviousDecls.empty()) {
5454 loadAndAttachPreviousDecl(PendingPreviousDecls.front().first,
5455 PendingPreviousDecls.front().second);
5456 PendingPreviousDecls.pop_front();
5457 }
5458
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00005459 // We are not in recursive loading, so it's safe to pass the "interesting"
5460 // decls to the consumer.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00005461 if (Consumer)
5462 PassInterestingDeclsToConsumer();
Argyrios Kyrtzidisad5f95c2010-10-24 17:26:31 +00005463
5464 assert(PendingForwardRefs.size() == 0 &&
5465 "Some forward refs did not get linked to the definition!");
Douglas Gregor1342e842009-07-06 18:54:52 +00005466 }
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00005467 --NumCurrentElementsDeserializing;
Douglas Gregor1342e842009-07-06 18:54:52 +00005468}
Douglas Gregorb473b072010-08-19 00:28:17 +00005469
Douglas Gregor8835e032011-09-02 00:26:20 +00005470ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context,
Douglas Gregorc567ba22011-07-22 16:35:34 +00005471 StringRef isysroot, bool DisableValidation,
Douglas Gregor606c4ac2011-02-05 19:42:43 +00005472 bool DisableStatCache)
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005473 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
5474 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
Douglas Gregor51825b42011-09-09 22:02:16 +00005475 Diags(PP.getDiagnostics()), SemaObj(0), PP(PP), Context(Context),
Jonathan D. Turnerecc27402011-07-28 17:20:23 +00005476 Consumer(0), ModuleMgr(FileMgr.getFileSystemOptions()),
5477 RelocatablePCH(false), isysroot(isysroot),
Douglas Gregor925296b2011-07-19 16:10:42 +00005478 DisableValidation(DisableValidation),
Douglas Gregor606c4ac2011-02-05 19:42:43 +00005479 DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0),
Douglas Gregor925296b2011-07-19 16:10:42 +00005480 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Douglas Gregor606c4ac2011-02-05 19:42:43 +00005481 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
5482 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
5483 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
5484 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
Jonathan D. Turner3766fdb2011-07-21 21:15:19 +00005485 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
5486 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
5487 NumCXXBaseSpecifiersLoaded(0)
Douglas Gregor606c4ac2011-02-05 19:42:43 +00005488{
Douglas Gregor925296b2011-07-19 16:10:42 +00005489 SourceMgr.setExternalSLocEntrySource(this);
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005490}
5491
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005492ASTReader::~ASTReader() {
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005493 for (DeclContextVisibleUpdatesPending::iterator
5494 I = PendingVisibleUpdates.begin(),
5495 E = PendingVisibleUpdates.end();
5496 I != E; ++I) {
5497 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
5498 F = I->second.end();
5499 J != F; ++J)
Douglas Gregorf7180622011-08-03 15:48:04 +00005500 delete static_cast<ASTDeclContextNameLookupTable*>(J->first);
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005501 }
5502}