blob: bc022f5721b59c7ab872679afd4c4c292e3f6ea8 [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;
501 SelectorTable &SelTable = Reader.getContext()->Selectors;
502 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.
635 if (Reader.getContext() == 0) return II;
636 if (DataLen > 0) {
637 SmallVector<uint32_t, 4> DeclIDs;
638 for (; DataLen > 0; DataLen -= 4)
639 DeclIDs.push_back(Reader.getGlobalDeclID(F, ReadUnalignedLE32(d)));
640 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000641 }
642
Douglas Gregord44252e2011-08-25 20:47:51 +0000643 II->setIsFromAST();
644 return II;
645}
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000646
Douglas Gregord44252e2011-08-25 20:47:51 +0000647unsigned
648ASTDeclContextNameLookupTrait::ComputeHash(const DeclNameKey &Key) const {
649 llvm::FoldingSetNodeID ID;
650 ID.AddInteger(Key.Kind);
651
652 switch (Key.Kind) {
653 case DeclarationName::Identifier:
654 case DeclarationName::CXXLiteralOperatorName:
655 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
656 break;
657 case DeclarationName::ObjCZeroArgSelector:
658 case DeclarationName::ObjCOneArgSelector:
659 case DeclarationName::ObjCMultiArgSelector:
660 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
661 break;
662 case DeclarationName::CXXOperatorName:
663 ID.AddInteger((OverloadedOperatorKind)Key.Data);
664 break;
665 case DeclarationName::CXXConstructorName:
666 case DeclarationName::CXXDestructorName:
667 case DeclarationName::CXXConversionFunctionName:
668 case DeclarationName::CXXUsingDirective:
669 break;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000670 }
671
Douglas Gregord44252e2011-08-25 20:47:51 +0000672 return ID.ComputeHash();
673}
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +0000674
Douglas Gregord44252e2011-08-25 20:47:51 +0000675ASTDeclContextNameLookupTrait::internal_key_type
676ASTDeclContextNameLookupTrait::GetInternalKey(
677 const external_key_type& Name) const {
678 DeclNameKey Key;
679 Key.Kind = Name.getNameKind();
680 switch (Name.getNameKind()) {
681 case DeclarationName::Identifier:
682 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
683 break;
684 case DeclarationName::ObjCZeroArgSelector:
685 case DeclarationName::ObjCOneArgSelector:
686 case DeclarationName::ObjCMultiArgSelector:
687 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
688 break;
689 case DeclarationName::CXXOperatorName:
690 Key.Data = Name.getCXXOverloadedOperator();
691 break;
692 case DeclarationName::CXXLiteralOperatorName:
693 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
694 break;
695 case DeclarationName::CXXConstructorName:
696 case DeclarationName::CXXDestructorName:
697 case DeclarationName::CXXConversionFunctionName:
698 case DeclarationName::CXXUsingDirective:
699 Key.Data = 0;
700 break;
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +0000701 }
702
Douglas Gregord44252e2011-08-25 20:47:51 +0000703 return Key;
704}
705
706ASTDeclContextNameLookupTrait::external_key_type
707ASTDeclContextNameLookupTrait::GetExternalKey(
708 const internal_key_type& Key) const {
709 ASTContext *Context = Reader.getContext();
710 switch (Key.Kind) {
711 case DeclarationName::Identifier:
712 return DeclarationName((IdentifierInfo*)Key.Data);
713
714 case DeclarationName::ObjCZeroArgSelector:
715 case DeclarationName::ObjCOneArgSelector:
716 case DeclarationName::ObjCMultiArgSelector:
717 return DeclarationName(Selector(Key.Data));
718
719 case DeclarationName::CXXConstructorName:
720 return Context->DeclarationNames.getCXXConstructorName(
721 Context->getCanonicalType(Reader.getLocalType(F, Key.Data)));
722
723 case DeclarationName::CXXDestructorName:
724 return Context->DeclarationNames.getCXXDestructorName(
725 Context->getCanonicalType(Reader.getLocalType(F, Key.Data)));
726
727 case DeclarationName::CXXConversionFunctionName:
728 return Context->DeclarationNames.getCXXConversionFunctionName(
729 Context->getCanonicalType(Reader.getLocalType(F, Key.Data)));
730
731 case DeclarationName::CXXOperatorName:
732 return Context->DeclarationNames.getCXXOperatorName(
733 (OverloadedOperatorKind)Key.Data);
734
735 case DeclarationName::CXXLiteralOperatorName:
736 return Context->DeclarationNames.getCXXLiteralOperatorName(
737 (IdentifierInfo*)Key.Data);
738
739 case DeclarationName::CXXUsingDirective:
740 return DeclarationName::getUsingDirectiveName();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000741 }
742
Douglas Gregord44252e2011-08-25 20:47:51 +0000743 llvm_unreachable("Invalid Name Kind ?");
744}
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000745
Douglas Gregord44252e2011-08-25 20:47:51 +0000746std::pair<unsigned, unsigned>
747ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
748 using namespace clang::io;
749 unsigned KeyLen = ReadUnalignedLE16(d);
750 unsigned DataLen = ReadUnalignedLE16(d);
751 return std::make_pair(KeyLen, DataLen);
752}
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000753
Douglas Gregord44252e2011-08-25 20:47:51 +0000754ASTDeclContextNameLookupTrait::internal_key_type
755ASTDeclContextNameLookupTrait::ReadKey(const unsigned char* d, unsigned) {
756 using namespace clang::io;
757
758 DeclNameKey Key;
759 Key.Kind = (DeclarationName::NameKind)*d++;
760 switch (Key.Kind) {
761 case DeclarationName::Identifier:
762 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
763 break;
764 case DeclarationName::ObjCZeroArgSelector:
765 case DeclarationName::ObjCOneArgSelector:
766 case DeclarationName::ObjCMultiArgSelector:
767 Key.Data =
768 (uint64_t)Reader.getLocalSelector(F, ReadUnalignedLE32(d))
769 .getAsOpaquePtr();
770 break;
771 case DeclarationName::CXXOperatorName:
772 Key.Data = *d++; // OverloadedOperatorKind
773 break;
774 case DeclarationName::CXXLiteralOperatorName:
775 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
776 break;
777 case DeclarationName::CXXConstructorName:
778 case DeclarationName::CXXDestructorName:
779 case DeclarationName::CXXConversionFunctionName:
780 case DeclarationName::CXXUsingDirective:
781 Key.Data = 0;
782 break;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000783 }
784
Douglas Gregord44252e2011-08-25 20:47:51 +0000785 return Key;
786}
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000787
Douglas Gregord44252e2011-08-25 20:47:51 +0000788ASTDeclContextNameLookupTrait::data_type
789ASTDeclContextNameLookupTrait::ReadData(internal_key_type,
790 const unsigned char* d,
791 unsigned DataLen) {
792 using namespace clang::io;
793 unsigned NumDecls = ReadUnalignedLE16(d);
794 DeclID *Start = (DeclID *)d;
795 return std::make_pair(Start, Start + NumDecls);
796}
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000797
Douglas Gregor94619c82011-08-24 19:03:07 +0000798bool ASTReader::ReadDeclContextStorage(Module &M,
799 llvm::BitstreamCursor &Cursor,
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000800 const std::pair<uint64_t, uint64_t> &Offsets,
801 DeclContextInfo &Info) {
802 SavedStreamPosition SavedPosition(Cursor);
803 // First the lexical decls.
804 if (Offsets.first != 0) {
805 Cursor.JumpToBit(Offsets.first);
806
807 RecordData Record;
808 const char *Blob;
809 unsigned BlobLen;
810 unsigned Code = Cursor.ReadCode();
811 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
812 if (RecCode != DECL_CONTEXT_LEXICAL) {
813 Error("Expected lexical block");
814 return true;
815 }
816
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000817 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
818 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000819 }
820
821 // Now the lookup table.
822 if (Offsets.second != 0) {
823 Cursor.JumpToBit(Offsets.second);
824
825 RecordData Record;
826 const char *Blob;
827 unsigned BlobLen;
828 unsigned Code = Cursor.ReadCode();
829 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
830 if (RecCode != DECL_CONTEXT_VISIBLE) {
831 Error("Expected visible lookup table block");
832 return true;
833 }
834 Info.NameLookupTableData
835 = ASTDeclContextNameLookupTable::Create(
836 (const unsigned char *)Blob + Record[0],
837 (const unsigned char *)Blob,
Douglas Gregor94619c82011-08-24 19:03:07 +0000838 ASTDeclContextNameLookupTrait(*this, M));
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000839 }
840
841 return false;
842}
843
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000844void ASTReader::Error(StringRef Msg) {
Argyrios Kyrtzidisdaa41f52011-04-25 22:23:56 +0000845 Error(diag::err_fe_pch_malformed, Msg);
846}
847
848void ASTReader::Error(unsigned DiagID,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000849 StringRef Arg1, StringRef Arg2) {
Argyrios Kyrtzidisdaa41f52011-04-25 22:23:56 +0000850 if (Diags.isDiagnosticInFlight())
851 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
852 else
853 Diag(DiagID) << Arg1 << Arg2;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000854}
855
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000856/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redl2c499f62010-08-18 23:56:43 +0000857bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000858 if (Listener)
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000859 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000860 ActualOriginalFileName,
Nick Lewycky36079892011-02-23 21:16:44 +0000861 SuggestedPredefines,
862 FileMgr);
Douglas Gregorc379c072009-04-28 18:58:38 +0000863 return false;
Douglas Gregor92863e42009-04-10 23:10:45 +0000864}
865
Douglas Gregorc5046832009-04-27 18:38:38 +0000866//===----------------------------------------------------------------------===//
867// Source Manager Deserialization
868//===----------------------------------------------------------------------===//
869
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000870/// \brief Read the line table in the source manager block.
Sebastian Redl2c373b92010-10-05 15:59:54 +0000871/// \returns true if there was an error.
Douglas Gregora6895d82011-07-22 16:00:58 +0000872bool ASTReader::ParseLineTable(Module &F,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000873 SmallVectorImpl<uint64_t> &Record) {
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000874 unsigned Idx = 0;
875 LineTableInfo &LineTable = SourceMgr.getLineTable();
876
877 // Parse the file names
Douglas Gregora8854652009-04-13 17:12:42 +0000878 std::map<int, int> FileIDs;
879 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000880 // Extract the file name
881 unsigned FilenameLen = Record[Idx++];
882 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
883 Idx += FilenameLen;
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000884 MaybeAddSystemRootToFilename(Filename);
Jay Foad9a6b0982011-06-21 15:13:30 +0000885 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000886 }
887
888 // Parse the line entries
889 std::vector<LineEntry> Entries;
890 while (Idx < Record.size()) {
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000891 int FID = Record[Idx++];
Douglas Gregor925296b2011-07-19 16:10:42 +0000892 assert(FID >= 0 && "Serialized line entries for non-local file.");
893 // Remap FileID from 1-based old view.
894 FID += F.SLocEntryBaseID - 1;
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000895
896 // Extract the line entries
897 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000898 assert(NumEntries && "Numentries is 00000");
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000899 Entries.clear();
900 Entries.reserve(NumEntries);
901 for (unsigned I = 0; I != NumEntries; ++I) {
902 unsigned FileOffset = Record[Idx++];
903 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000904 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump11289f42009-09-09 15:08:12 +0000905 SrcMgr::CharacteristicKind FileKind
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000906 = (SrcMgr::CharacteristicKind)Record[Idx++];
907 unsigned IncludeOffset = Record[Idx++];
908 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
909 FileKind, IncludeOffset));
910 }
911 LineTable.AddEntry(FID, Entries);
912 }
913
914 return false;
915}
916
Douglas Gregorc5046832009-04-27 18:38:38 +0000917namespace {
918
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000919class ASTStatData {
Douglas Gregorc5046832009-04-27 18:38:38 +0000920public:
Douglas Gregorc5046832009-04-27 18:38:38 +0000921 const ino_t ino;
922 const dev_t dev;
923 const mode_t mode;
924 const time_t mtime;
925 const off_t size;
Mike Stump11289f42009-09-09 15:08:12 +0000926
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000927 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Chris Lattner2a6fa472010-11-23 19:28:12 +0000928 : ino(i), dev(d), mode(mo), mtime(m), size(s) {}
Douglas Gregorc5046832009-04-27 18:38:38 +0000929};
930
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000931class ASTStatLookupTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +0000932 public:
933 typedef const char *external_key_type;
934 typedef const char *internal_key_type;
935
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000936 typedef ASTStatData data_type;
Douglas Gregorc5046832009-04-27 18:38:38 +0000937
938 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000939 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +0000940 }
941
942 static internal_key_type GetInternalKey(const char *path) { return path; }
943
944 static bool EqualKey(internal_key_type a, internal_key_type b) {
945 return strcmp(a, b) == 0;
946 }
947
948 static std::pair<unsigned, unsigned>
949 ReadKeyDataLength(const unsigned char*& d) {
950 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
951 unsigned DataLen = (unsigned) *d++;
952 return std::make_pair(KeyLen + 1, DataLen);
953 }
954
955 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
956 return (const char *)d;
957 }
958
959 static data_type ReadData(const internal_key_type, const unsigned char *d,
960 unsigned /*DataLen*/) {
961 using namespace clang::io;
962
Douglas Gregorc5046832009-04-27 18:38:38 +0000963 ino_t ino = (ino_t) ReadUnalignedLE32(d);
964 dev_t dev = (dev_t) ReadUnalignedLE32(d);
965 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump11289f42009-09-09 15:08:12 +0000966 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregorc5046832009-04-27 18:38:38 +0000967 off_t size = (off_t) ReadUnalignedLE64(d);
968 return data_type(ino, dev, mode, mtime, size);
969 }
970};
971
972/// \brief stat() cache for precompiled headers.
973///
974/// This cache is very similar to the stat cache used by pretokenized
975/// headers.
Chris Lattner226efd32010-11-23 19:19:34 +0000976class ASTStatCache : public FileSystemStatCache {
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000977 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregorc5046832009-04-27 18:38:38 +0000978 CacheTy *Cache;
979
980 unsigned &NumStatHits, &NumStatMisses;
Mike Stump11289f42009-09-09 15:08:12 +0000981public:
Chris Lattner2a6fa472010-11-23 19:28:12 +0000982 ASTStatCache(const unsigned char *Buckets, const unsigned char *Base,
983 unsigned &NumStatHits, unsigned &NumStatMisses)
Douglas Gregorc5046832009-04-27 18:38:38 +0000984 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
985 Cache = CacheTy::Create(Buckets, Base);
986 }
987
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000988 ~ASTStatCache() { delete Cache; }
Mike Stump11289f42009-09-09 15:08:12 +0000989
Chris Lattnerdd278432010-11-23 21:17:56 +0000990 LookupResult getStat(const char *Path, struct stat &StatBuf,
991 int *FileDescriptor) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000992 // Do the lookup for the file's data in the AST file.
Chris Lattner226efd32010-11-23 19:19:34 +0000993 CacheTy::iterator I = Cache->find(Path);
Douglas Gregorc5046832009-04-27 18:38:38 +0000994
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000995 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregorc5046832009-04-27 18:38:38 +0000996 if (I == Cache->end()) {
997 ++NumStatMisses;
Chris Lattnerdd278432010-11-23 21:17:56 +0000998 return statChained(Path, StatBuf, FileDescriptor);
Douglas Gregorc5046832009-04-27 18:38:38 +0000999 }
Mike Stump11289f42009-09-09 15:08:12 +00001000
Douglas Gregorc5046832009-04-27 18:38:38 +00001001 ++NumStatHits;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001002 ASTStatData Data = *I;
Mike Stump11289f42009-09-09 15:08:12 +00001003
Chris Lattner226efd32010-11-23 19:19:34 +00001004 StatBuf.st_ino = Data.ino;
1005 StatBuf.st_dev = Data.dev;
1006 StatBuf.st_mtime = Data.mtime;
1007 StatBuf.st_mode = Data.mode;
1008 StatBuf.st_size = Data.size;
Chris Lattner8f0583d2010-11-23 20:05:15 +00001009 return CacheExists;
Douglas Gregorc5046832009-04-27 18:38:38 +00001010 }
1011};
1012} // end anonymous namespace
1013
1014
Sebastian Redl393f8b72010-07-19 20:52:06 +00001015/// \brief Read a source manager block
Douglas Gregora6895d82011-07-22 16:00:58 +00001016ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(Module &F) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001017 using namespace SrcMgr;
Douglas Gregor258ae542009-04-27 06:38:32 +00001018
Sebastian Redl393f8b72010-07-19 20:52:06 +00001019 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001020
Douglas Gregor258ae542009-04-27 06:38:32 +00001021 // Set the source-location entry cursor to the current position in
1022 // the stream. This cursor will be used to read the contents of the
1023 // source manager block initially, and then lazily read
1024 // source-location entries as needed.
Sebastian Redl393f8b72010-07-19 20:52:06 +00001025 SLocEntryCursor = F.Stream;
Douglas Gregor258ae542009-04-27 06:38:32 +00001026
1027 // The stream itself is going to skip over the source manager block.
Sebastian Redl393f8b72010-07-19 20:52:06 +00001028 if (F.Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001029 Error("malformed block record in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001030 return Failure;
1031 }
1032
1033 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001034 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001035 Error("malformed source manager block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001036 return Failure;
1037 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001038
Douglas Gregora7f71a92009-04-10 03:52:48 +00001039 RecordData Record;
1040 while (true) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001041 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregora7f71a92009-04-10 03:52:48 +00001042 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001043 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001044 Error("error at end of Source Manager block in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001045 return Failure;
1046 }
Douglas Gregor92863e42009-04-10 23:10:45 +00001047 return Success;
Douglas Gregora7f71a92009-04-10 03:52:48 +00001048 }
Mike Stump11289f42009-09-09 15:08:12 +00001049
Douglas Gregora7f71a92009-04-10 03:52:48 +00001050 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1051 // No known subblocks, always skip them.
Douglas Gregor258ae542009-04-27 06:38:32 +00001052 SLocEntryCursor.ReadSubBlockID();
1053 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001054 Error("malformed block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001055 return Failure;
1056 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001057 continue;
1058 }
Mike Stump11289f42009-09-09 15:08:12 +00001059
Douglas Gregora7f71a92009-04-10 03:52:48 +00001060 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001061 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregora7f71a92009-04-10 03:52:48 +00001062 continue;
1063 }
Mike Stump11289f42009-09-09 15:08:12 +00001064
Douglas Gregora7f71a92009-04-10 03:52:48 +00001065 // Read a record.
1066 const char *BlobStart;
1067 unsigned BlobLen;
1068 Record.clear();
Douglas Gregor258ae542009-04-27 06:38:32 +00001069 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001070 default: // Default behavior: ignore.
1071 break;
1072
Sebastian Redl539c5062010-08-18 23:57:32 +00001073 case SM_SLOC_FILE_ENTRY:
1074 case SM_SLOC_BUFFER_ENTRY:
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001075 case SM_SLOC_EXPANSION_ENTRY:
Douglas Gregor258ae542009-04-27 06:38:32 +00001076 // Once we hit one of the source location entries, we're done.
1077 return Success;
Douglas Gregora7f71a92009-04-10 03:52:48 +00001078 }
1079 }
1080}
1081
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001082/// \brief If a header file is not found at the path that we expect it to be
1083/// and the PCH file was moved from its original location, try to resolve the
1084/// file by assuming that header+PCH were moved together and the header is in
1085/// the same place relative to the PCH.
1086static std::string
1087resolveFileRelativeToOriginalDir(const std::string &Filename,
1088 const std::string &OriginalDir,
1089 const std::string &CurrDir) {
1090 assert(OriginalDir != CurrDir &&
1091 "No point trying to resolve the file if the PCH dir didn't change");
1092 using namespace llvm::sys;
1093 llvm::SmallString<128> filePath(Filename);
1094 fs::make_absolute(filePath);
1095 assert(path::is_absolute(OriginalDir));
1096 llvm::SmallString<128> currPCHPath(CurrDir);
1097
1098 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1099 fileDirE = path::end(path::parent_path(filePath));
1100 path::const_iterator origDirI = path::begin(OriginalDir),
1101 origDirE = path::end(OriginalDir);
1102 // Skip the common path components from filePath and OriginalDir.
1103 while (fileDirI != fileDirE && origDirI != origDirE &&
1104 *fileDirI == *origDirI) {
1105 ++fileDirI;
1106 ++origDirI;
1107 }
1108 for (; origDirI != origDirE; ++origDirI)
1109 path::append(currPCHPath, "..");
1110 path::append(currPCHPath, fileDirI, fileDirE);
1111 path::append(currPCHPath, path::filename(Filename));
1112 return currPCHPath.str();
1113}
1114
Douglas Gregor258ae542009-04-27 06:38:32 +00001115/// \brief Read in the source location entry with the given ID.
Douglas Gregor925296b2011-07-19 16:10:42 +00001116ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(int ID) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001117 if (ID == 0)
1118 return Success;
1119
Douglas Gregor49bf76b2011-07-21 18:46:38 +00001120 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001121 Error("source location entry ID out-of-range for AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001122 return Failure;
1123 }
1124
Douglas Gregora6895d82011-07-22 16:00:58 +00001125 Module *F = GlobalSLocEntryMap.find(-ID)->second;
Douglas Gregor925296b2011-07-19 16:10:42 +00001126 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00001127 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Douglas Gregor925296b2011-07-19 16:10:42 +00001128 unsigned BaseOffset = F->SLocEntryBaseOffset;
Sebastian Redl34522812010-07-16 17:50:48 +00001129
Douglas Gregor258ae542009-04-27 06:38:32 +00001130 ++NumSLocEntriesRead;
Douglas Gregor258ae542009-04-27 06:38:32 +00001131 unsigned Code = SLocEntryCursor.ReadCode();
1132 if (Code == llvm::bitc::END_BLOCK ||
1133 Code == llvm::bitc::ENTER_SUBBLOCK ||
1134 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001135 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001136 return Failure;
1137 }
1138
Douglas Gregor258ae542009-04-27 06:38:32 +00001139 RecordData Record;
1140 const char *BlobStart;
1141 unsigned BlobLen;
1142 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1143 default:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001144 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001145 return Failure;
1146
Sebastian Redl539c5062010-08-18 23:57:32 +00001147 case SM_SLOC_FILE_ENTRY: {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001148 std::string Filename(BlobStart, BlobStart + BlobLen);
1149 MaybeAddSystemRootToFilename(Filename);
Chris Lattner5159f612010-11-23 08:35:12 +00001150 const FileEntry *File = FileMgr.getFile(Filename);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001151 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1152 OriginalDir != CurrentDir) {
1153 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1154 OriginalDir,
1155 CurrentDir);
1156 if (!resolved.empty())
1157 File = FileMgr.getFile(resolved);
1158 }
Axel Naumann63fbaed2011-01-27 10:55:51 +00001159 if (File == 0)
1160 File = FileMgr.getVirtualFile(Filename, (off_t)Record[4],
1161 (time_t)Record[5]);
Chris Lattnerd20dc872009-06-15 04:35:16 +00001162 if (File == 0) {
1163 std::string ErrorStr = "could not find file '";
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001164 ErrorStr += Filename;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001165 ErrorStr += "' referenced by AST file";
Chris Lattnerd20dc872009-06-15 04:35:16 +00001166 Error(ErrorStr.c_str());
1167 return Failure;
1168 }
Mike Stump11289f42009-09-09 15:08:12 +00001169
Douglas Gregor09b69892011-02-10 17:09:37 +00001170 if (Record.size() < 6) {
Ted Kremenekabb1ddd2010-03-18 21:23:05 +00001171 Error("source location entry is incorrect");
1172 return Failure;
1173 }
1174
Douglas Gregorce3a8292010-07-27 00:27:13 +00001175 if (!DisableValidation &&
1176 ((off_t)Record[4] != File->getSize()
Douglas Gregor08288f22010-04-09 15:54:22 +00001177#if !defined(LLVM_ON_WIN32)
1178 // In our regression testing, the Windows file system seems to
1179 // have inconsistent modification times that sometimes
1180 // erroneously trigger this error-handling path.
Douglas Gregorce3a8292010-07-27 00:27:13 +00001181 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor08288f22010-04-09 15:54:22 +00001182#endif
Douglas Gregorce3a8292010-07-27 00:27:13 +00001183 )) {
Argyrios Kyrtzidisdaa41f52011-04-25 22:23:56 +00001184 Error(diag::err_fe_pch_file_modified, Filename);
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001185 return Failure;
1186 }
1187
Douglas Gregor925296b2011-07-19 16:10:42 +00001188 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregora6895d82011-07-22 16:00:58 +00001189 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
Douglas Gregor925296b2011-07-19 16:10:42 +00001190 // This is the module's main file.
1191 IncludeLoc = getImportLocation(F);
1192 }
1193 FileID FID = SourceMgr.createFileID(File, IncludeLoc,
Chris Lattner26b5c192010-11-23 09:19:42 +00001194 (SrcMgr::CharacteristicKind)Record[2],
Douglas Gregor925296b2011-07-19 16:10:42 +00001195 ID, BaseOffset + Record[0]);
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001196 SrcMgr::FileInfo &FileInfo =
1197 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1198 FileInfo.NumCreatedFIDs = Record[6];
Douglas Gregor258ae542009-04-27 06:38:32 +00001199 if (Record[3])
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001200 FileInfo.setHasLineDirectives();
Douglas Gregor09b69892011-02-10 17:09:37 +00001201
Douglas Gregor258ae542009-04-27 06:38:32 +00001202 break;
1203 }
1204
Sebastian Redl539c5062010-08-18 23:57:32 +00001205 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor258ae542009-04-27 06:38:32 +00001206 const char *Name = BlobStart;
1207 unsigned Offset = Record[0];
1208 unsigned Code = SLocEntryCursor.ReadCode();
1209 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00001210 unsigned RecCode
Douglas Gregor258ae542009-04-27 06:38:32 +00001211 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001212
Sebastian Redl539c5062010-08-18 23:57:32 +00001213 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001214 Error("AST record has invalid code");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001215 return Failure;
1216 }
1217
Douglas Gregor258ae542009-04-27 06:38:32 +00001218 llvm::MemoryBuffer *Buffer
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001219 = llvm::MemoryBuffer::getMemBuffer(StringRef(BlobStart, BlobLen - 1),
Chris Lattner58c79342010-04-05 22:42:27 +00001220 Name);
Douglas Gregor925296b2011-07-19 16:10:42 +00001221 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID,
1222 BaseOffset + Offset);
Mike Stump11289f42009-09-09 15:08:12 +00001223
Douglas Gregore6648fb2009-04-28 20:33:11 +00001224 if (strcmp(Name, "<built-in>") == 0) {
Sebastian Redl75fbb3b2010-07-14 17:49:11 +00001225 PCHPredefinesBlock Block = {
1226 BufferID,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001227 StringRef(BlobStart, BlobLen - 1)
Sebastian Redl75fbb3b2010-07-14 17:49:11 +00001228 };
1229 PCHPredefinesBuffers.push_back(Block);
Douglas Gregore6648fb2009-04-28 20:33:11 +00001230 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001231
1232 break;
1233 }
1234
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001235 case SM_SLOC_EXPANSION_ENTRY: {
Sebastian Redl2c373b92010-10-05 15:59:54 +00001236 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Chandler Carruth115b0772011-07-26 03:03:05 +00001237 SourceMgr.createExpansionLoc(SpellingLoc,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001238 ReadSourceLocation(*F, Record[2]),
1239 ReadSourceLocation(*F, Record[3]),
Douglas Gregor258ae542009-04-27 06:38:32 +00001240 Record[4],
1241 ID,
Douglas Gregor925296b2011-07-19 16:10:42 +00001242 BaseOffset + Record[0]);
Douglas Gregor258ae542009-04-27 06:38:32 +00001243 break;
Mike Stump11289f42009-09-09 15:08:12 +00001244 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001245 }
1246
1247 return Success;
1248}
1249
Douglas Gregor925296b2011-07-19 16:10:42 +00001250/// \brief Find the location where the module F is imported.
Douglas Gregora6895d82011-07-22 16:00:58 +00001251SourceLocation ASTReader::getImportLocation(Module *F) {
Douglas Gregor925296b2011-07-19 16:10:42 +00001252 if (F->ImportLoc.isValid())
1253 return F->ImportLoc;
Jonathan D. Turner10d52012011-07-29 18:09:09 +00001254
Douglas Gregor925296b2011-07-19 16:10:42 +00001255 // Otherwise we have a PCH. It's considered to be "imported" at the first
1256 // location of its includer.
Jonathan D. Turner10d52012011-07-29 18:09:09 +00001257 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
Douglas Gregor925296b2011-07-19 16:10:42 +00001258 // Main file is the importer. We assume that it is the first entry in the
1259 // entry table. We can't ask the manager, because at the time of PCH loading
1260 // the main file entry doesn't exist yet.
1261 // The very first entry is the invalid instantiation loc, which takes up
1262 // offsets 0 and 1.
1263 return SourceLocation::getFromRawEncoding(2U);
1264 }
Jonathan D. Turner10d52012011-07-29 18:09:09 +00001265 //return F->Loaders[0]->FirstLoc;
1266 return F->ImportedBy[0]->FirstLoc;
Douglas Gregor925296b2011-07-19 16:10:42 +00001267}
1268
Chris Lattnere78a6be2009-04-27 01:05:14 +00001269/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1270/// specified cursor. Read the abbreviations that are at the top of the block
1271/// and then leave the cursor pointing into the block.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001272bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattnere78a6be2009-04-27 01:05:14 +00001273 unsigned BlockID) {
1274 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001275 Error("malformed block record in AST file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001276 return Failure;
1277 }
Mike Stump11289f42009-09-09 15:08:12 +00001278
Chris Lattnere78a6be2009-04-27 01:05:14 +00001279 while (true) {
Douglas Gregor796d76a2010-10-20 22:00:55 +00001280 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattnere78a6be2009-04-27 01:05:14 +00001281 unsigned Code = Cursor.ReadCode();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001282
Chris Lattnere78a6be2009-04-27 01:05:14 +00001283 // We expect all abbrevs to be at the start of the block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001284 if (Code != llvm::bitc::DEFINE_ABBREV) {
1285 Cursor.JumpToBit(Offset);
Chris Lattnere78a6be2009-04-27 01:05:14 +00001286 return false;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001287 }
Chris Lattnere78a6be2009-04-27 01:05:14 +00001288 Cursor.ReadAbbrevRecord();
1289 }
1290}
1291
Douglas Gregor7cb0d012011-08-04 18:09:14 +00001292void ASTReader::ReadMacroRecord(Module &F, uint64_t Offset) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001293 assert(PP && "Forgot to set Preprocessor ?");
Douglas Gregor796d76a2010-10-20 22:00:55 +00001294 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump11289f42009-09-09 15:08:12 +00001295
Douglas Gregorc3366a52009-04-21 23:56:24 +00001296 // Keep track of where we are in the stream, then jump back there
1297 // after reading this macro.
1298 SavedStreamPosition SavedPosition(Stream);
1299
1300 Stream.JumpToBit(Offset);
1301 RecordData Record;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001302 SmallVector<IdentifierInfo*, 16> MacroArgs;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001303 MacroInfo *Macro = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001304
Douglas Gregorc3366a52009-04-21 23:56:24 +00001305 while (true) {
1306 unsigned Code = Stream.ReadCode();
1307 switch (Code) {
1308 case llvm::bitc::END_BLOCK:
Douglas Gregor7cb0d012011-08-04 18:09:14 +00001309 return;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001310
1311 case llvm::bitc::ENTER_SUBBLOCK:
1312 // No known subblocks, always skip them.
1313 Stream.ReadSubBlockID();
1314 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001315 Error("malformed block record in AST file");
Douglas Gregor7cb0d012011-08-04 18:09:14 +00001316 return;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001317 }
1318 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001319
Douglas Gregorc3366a52009-04-21 23:56:24 +00001320 case llvm::bitc::DEFINE_ABBREV:
1321 Stream.ReadAbbrevRecord();
1322 continue;
1323 default: break;
1324 }
1325
1326 // Read a record.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001327 const char *BlobStart = 0;
1328 unsigned BlobLen = 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001329 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001330 PreprocessorRecordTypes RecType =
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001331 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregor796d76a2010-10-20 22:00:55 +00001332 BlobLen);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001333 switch (RecType) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001334 case PP_MACRO_OBJECT_LIKE:
1335 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001336 // If we already have a macro, that means that we've hit the end
1337 // of the definition of the macro we were looking for. We're
1338 // done.
1339 if (Macro)
Douglas Gregor7cb0d012011-08-04 18:09:14 +00001340 return;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001341
Douglas Gregora3e41532011-07-28 20:55:49 +00001342 IdentifierInfo *II = getLocalIdentifier(F, Record[0]);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001343 if (II == 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001344 Error("macro must have a name in AST file");
Douglas Gregor7cb0d012011-08-04 18:09:14 +00001345 return;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001346 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00001347 SourceLocation Loc = ReadSourceLocation(F, Record[1]);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001348 bool isUsed = Record[2];
Mike Stump11289f42009-09-09 15:08:12 +00001349
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001350 MacroInfo *MI = PP->AllocateMacroInfo(Loc);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001351 MI->setIsUsed(isUsed);
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001352 MI->setIsFromAST();
Mike Stump11289f42009-09-09 15:08:12 +00001353
Douglas Gregoraae92242010-03-19 21:51:54 +00001354 unsigned NextIndex = 3;
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.
1357 bool isC99VarArgs = Record[3];
1358 bool isGNUVarArgs = Record[4];
1359 MacroArgs.clear();
1360 unsigned NumArgs = Record[5];
Douglas Gregoraae92242010-03-19 21:51:54 +00001361 NextIndex = 6 + NumArgs;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001362 for (unsigned i = 0; i != NumArgs; ++i)
Douglas Gregora3e41532011-07-28 20:55:49 +00001363 MacroArgs.push_back(getLocalIdentifier(F, Record[6+i]));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001364
1365 // Install function-like macro info.
1366 MI->setIsFunctionLike();
1367 if (isC99VarArgs) MI->setIsC99Varargs();
1368 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor038c3382009-05-22 22:45:36 +00001369 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001370 PP->getPreprocessorAllocator());
Douglas Gregorc3366a52009-04-21 23:56:24 +00001371 }
1372
1373 // Finally, install the macro.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001374 PP->setMacroInfo(II, MI);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001375
1376 // Remember that we saw this macro last so that we add the tokens that
1377 // form its body to it.
1378 Macro = MI;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001379
Douglas Gregoraae92242010-03-19 21:51:54 +00001380 if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) {
1381 // We have a macro definition. Load it now.
1382 PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro,
Douglas Gregor035611e2011-07-28 22:16:57 +00001383 getLocalMacroDefinition(F, Record[NextIndex]));
Douglas Gregoraae92242010-03-19 21:51:54 +00001384 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001385
Douglas Gregorc3366a52009-04-21 23:56:24 +00001386 ++NumMacrosRead;
1387 break;
1388 }
Mike Stump11289f42009-09-09 15:08:12 +00001389
Sebastian Redl539c5062010-08-18 23:57:32 +00001390 case PP_TOKEN: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001391 // If we see a TOKEN before a PP_MACRO_*, then the file is
1392 // erroneous, just pretend we didn't see this.
1393 if (Macro == 0) break;
Mike Stump11289f42009-09-09 15:08:12 +00001394
Douglas Gregorc3366a52009-04-21 23:56:24 +00001395 Token Tok;
1396 Tok.startToken();
Sebastian Redl2c373b92010-10-05 15:59:54 +00001397 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001398 Tok.setLength(Record[1]);
Douglas Gregora3e41532011-07-28 20:55:49 +00001399 if (IdentifierInfo *II = getLocalIdentifier(F, Record[2]))
Douglas Gregorc3366a52009-04-21 23:56:24 +00001400 Tok.setIdentifierInfo(II);
1401 Tok.setKind((tok::TokenKind)Record[3]);
1402 Tok.setFlag((Token::TokenFlags)Record[4]);
1403 Macro->AddTokenToBody(Tok);
1404 break;
1405 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00001406 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001407 }
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001408
Douglas Gregor7cb0d012011-08-04 18:09:14 +00001409 return;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001410}
1411
Douglas Gregora6895d82011-07-22 16:00:58 +00001412PreprocessedEntity *ASTReader::LoadPreprocessedEntity(Module &F) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00001413 assert(PP && "Forgot to set Preprocessor ?");
1414 unsigned Code = F.PreprocessorDetailCursor.ReadCode();
1415 switch (Code) {
1416 case llvm::bitc::END_BLOCK:
1417 return 0;
1418
1419 case llvm::bitc::ENTER_SUBBLOCK:
1420 Error("unexpected subblock record in preprocessor detail block");
1421 return 0;
1422
1423 case llvm::bitc::DEFINE_ABBREV:
1424 Error("unexpected abbrevation record in preprocessor detail block");
1425 return 0;
1426
1427 default:
1428 break;
1429 }
1430
1431 if (!PP->getPreprocessingRecord()) {
1432 Error("no preprocessing record");
1433 return 0;
1434 }
1435
1436 // Read the record.
1437 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1438 const char *BlobStart = 0;
1439 unsigned BlobLen = 0;
1440 RecordData Record;
1441 PreprocessorDetailRecordTypes RecType =
1442 (PreprocessorDetailRecordTypes)F.PreprocessorDetailCursor.ReadRecord(
1443 Code, Record, BlobStart, BlobLen);
1444 switch (RecType) {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001445 case PPD_MACRO_EXPANSION: {
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001446 PreprocessedEntityID GlobalID = getGlobalPreprocessedEntityID(F, Record[0]);
Douglas Gregor0d4b4312011-08-04 17:06:18 +00001447 if (PreprocessedEntity *PE = PPRec.getLoadedPreprocessedEntity(GlobalID-1))
Douglas Gregor92a96f52011-02-08 21:58:10 +00001448 return PE;
1449
Chandler Carrutha88a22182011-07-14 08:20:46 +00001450 MacroExpansion *ME =
Douglas Gregora3e41532011-07-28 20:55:49 +00001451 new (PPRec) MacroExpansion(getLocalIdentifier(F, Record[3]),
Douglas Gregor92a96f52011-02-08 21:58:10 +00001452 SourceRange(ReadSourceLocation(F, Record[1]),
1453 ReadSourceLocation(F, Record[2])),
Douglas Gregor035611e2011-07-28 22:16:57 +00001454 getLocalMacroDefinition(F, Record[4]));
Douglas Gregor0d4b4312011-08-04 17:06:18 +00001455 PPRec.setLoadedPreallocatedEntity(GlobalID - 1, ME);
Chandler Carrutha88a22182011-07-14 08:20:46 +00001456 return ME;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001457 }
1458
1459 case PPD_MACRO_DEFINITION: {
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001460 PreprocessedEntityID GlobalID = getGlobalPreprocessedEntityID(F, Record[0]);
Douglas Gregor0d4b4312011-08-04 17:06:18 +00001461 if (PreprocessedEntity *PE = PPRec.getLoadedPreprocessedEntity(GlobalID-1))
Douglas Gregor92a96f52011-02-08 21:58:10 +00001462 return PE;
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001463
1464 unsigned MacroDefID = getGlobalMacroDefinitionID(F, Record[1]);
1465 if (MacroDefID > MacroDefinitionsLoaded.size()) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00001466 Error("out-of-bounds macro definition record");
1467 return 0;
1468 }
1469
1470 // Decode the identifier info and then check again; if the macro is
1471 // still defined and associated with the identifier,
Douglas Gregora3e41532011-07-28 20:55:49 +00001472 IdentifierInfo *II = getLocalIdentifier(F, Record[4]);
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001473 if (!MacroDefinitionsLoaded[MacroDefID - 1]) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00001474 MacroDefinition *MD
1475 = new (PPRec) MacroDefinition(II,
1476 ReadSourceLocation(F, Record[5]),
1477 SourceRange(
1478 ReadSourceLocation(F, Record[2]),
1479 ReadSourceLocation(F, Record[3])));
1480
Douglas Gregor0d4b4312011-08-04 17:06:18 +00001481 PPRec.setLoadedPreallocatedEntity(GlobalID - 1, MD);
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001482 MacroDefinitionsLoaded[MacroDefID - 1] = MD;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001483
1484 if (DeserializationListener)
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001485 DeserializationListener->MacroDefinitionRead(MacroDefID, MD);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001486 }
1487
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001488 return MacroDefinitionsLoaded[MacroDefID - 1];
Douglas Gregor92a96f52011-02-08 21:58:10 +00001489 }
1490
1491 case PPD_INCLUSION_DIRECTIVE: {
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001492 PreprocessedEntityID GlobalID = getGlobalPreprocessedEntityID(F, Record[0]);
Douglas Gregor0d4b4312011-08-04 17:06:18 +00001493 if (PreprocessedEntity *PE = PPRec.getLoadedPreprocessedEntity(GlobalID-1))
Douglas Gregor92a96f52011-02-08 21:58:10 +00001494 return PE;
1495
1496 const char *FullFileNameStart = BlobStart + Record[3];
1497 const FileEntry *File
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001498 = PP->getFileManager().getFile(StringRef(FullFileNameStart,
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001499 BlobLen - Record[3]));
Douglas Gregor92a96f52011-02-08 21:58:10 +00001500
1501 // FIXME: Stable encoding
1502 InclusionDirective::InclusionKind Kind
1503 = static_cast<InclusionDirective::InclusionKind>(Record[5]);
1504 InclusionDirective *ID
1505 = new (PPRec) InclusionDirective(PPRec, Kind,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001506 StringRef(BlobStart, Record[3]),
Douglas Gregor92a96f52011-02-08 21:58:10 +00001507 Record[4],
1508 File,
1509 SourceRange(ReadSourceLocation(F, Record[1]),
1510 ReadSourceLocation(F, Record[2])));
Douglas Gregor0d4b4312011-08-04 17:06:18 +00001511 PPRec.setLoadedPreallocatedEntity(GlobalID - 1, ID);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001512 return ID;
1513 }
1514 }
1515
1516 Error("invalid offset in preprocessor detail block");
1517 return 0;
1518}
1519
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001520PreprocessedEntityID
1521ASTReader::getGlobalPreprocessedEntityID(Module &M, unsigned LocalID) {
Douglas Gregor2f555fc2011-08-04 18:56:47 +00001522 ContinuousRangeMap<uint32_t, int, 2>::iterator
1523 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1524 assert(I != M.PreprocessedEntityRemap.end()
1525 && "Invalid index into preprocessed entity index remap");
1526
1527 return LocalID + I->second;
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001528}
1529
Douglas Gregord44252e2011-08-25 20:47:51 +00001530unsigned HeaderFileInfoTrait::ComputeHash(const char *path) {
1531 return llvm::HashString(llvm::sys::path::filename(path));
Douglas Gregor09b69892011-02-10 17:09:37 +00001532}
Douglas Gregord44252e2011-08-25 20:47:51 +00001533
1534HeaderFileInfoTrait::internal_key_type
1535HeaderFileInfoTrait::GetInternalKey(const char *path) { return path; }
1536
1537bool HeaderFileInfoTrait::EqualKey(internal_key_type a, internal_key_type b) {
1538 if (strcmp(a, b) == 0)
1539 return true;
1540
1541 if (llvm::sys::path::filename(a) != llvm::sys::path::filename(b))
1542 return false;
1543
1544 // The file names match, but the path names don't. stat() the files to
1545 // see if they are the same.
1546 struct stat StatBufA, StatBufB;
1547 if (StatSimpleCache(a, &StatBufA) || StatSimpleCache(b, &StatBufB))
1548 return false;
1549
1550 return StatBufA.st_ino == StatBufB.st_ino;
1551}
1552
1553std::pair<unsigned, unsigned>
1554HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1555 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1556 unsigned DataLen = (unsigned) *d++;
1557 return std::make_pair(KeyLen + 1, DataLen);
1558}
1559
1560HeaderFileInfoTrait::data_type
1561HeaderFileInfoTrait::ReadData(const internal_key_type, const unsigned char *d,
1562 unsigned DataLen) {
1563 const unsigned char *End = d + DataLen;
1564 using namespace clang::io;
1565 HeaderFileInfo HFI;
1566 unsigned Flags = *d++;
1567 HFI.isImport = (Flags >> 5) & 0x01;
1568 HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1569 HFI.DirInfo = (Flags >> 2) & 0x03;
1570 HFI.Resolved = (Flags >> 1) & 0x01;
1571 HFI.IndexHeaderMapHeader = Flags & 0x01;
1572 HFI.NumIncludes = ReadUnalignedLE16(d);
1573 HFI.ControllingMacroID = Reader.getGlobalDeclID(M, ReadUnalignedLE32(d));
1574 if (unsigned FrameworkOffset = ReadUnalignedLE32(d)) {
1575 // The framework offset is 1 greater than the actual offset,
1576 // since 0 is used as an indicator for "no framework name".
1577 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1578 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1579 }
1580
1581 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1582 (void)End;
1583
1584 // This HeaderFileInfo was externally loaded.
1585 HFI.External = true;
1586 return HFI;
1587}
Douglas Gregor09b69892011-02-10 17:09:37 +00001588
Douglas Gregora6895d82011-07-22 16:00:58 +00001589void ASTReader::SetIdentifierIsMacro(IdentifierInfo *II, Module &F,
Douglas Gregor074fdc52011-07-28 21:16:51 +00001590 uint64_t LocalOffset) {
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001591 // Note that this identifier has a macro definition.
1592 II->setHasMacroDefinition(true);
1593
Douglas Gregord32f0352011-07-22 06:10:01 +00001594 // Adjust the offset to a global offset.
Douglas Gregor074fdc52011-07-28 21:16:51 +00001595 UnreadMacroRecordOffsets[II] = F.GlobalBitOffset + LocalOffset;
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001596}
1597
Sebastian Redl2c499f62010-08-18 23:56:43 +00001598void ASTReader::ReadDefinedMacros() {
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00001599 for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1600 E = ModuleMgr.rend(); I != E; ++I) {
1601 llvm::BitstreamCursor &MacroCursor = (*I)->MacroCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001602
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001603 // If there was no preprocessor block, skip this file.
1604 if (!MacroCursor.getBitStreamReader())
1605 continue;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001606
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001607 llvm::BitstreamCursor Cursor = MacroCursor;
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00001608 Cursor.JumpToBit((*I)->MacroStartOffset);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001609
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001610 RecordData Record;
1611 while (true) {
1612 unsigned Code = Cursor.ReadCode();
Douglas Gregor796d76a2010-10-20 22:00:55 +00001613 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001614 break;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001615
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001616 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1617 // No known subblocks, always skip them.
1618 Cursor.ReadSubBlockID();
1619 if (Cursor.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001620 Error("malformed block record in AST file");
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001621 return;
1622 }
1623 continue;
1624 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001625
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001626 if (Code == llvm::bitc::DEFINE_ABBREV) {
1627 Cursor.ReadAbbrevRecord();
1628 continue;
1629 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001630
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001631 // Read a record.
1632 const char *BlobStart;
1633 unsigned BlobLen;
1634 Record.clear();
1635 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1636 default: // Default behavior: ignore.
1637 break;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001638
Sebastian Redl539c5062010-08-18 23:57:32 +00001639 case PP_MACRO_OBJECT_LIKE:
1640 case PP_MACRO_FUNCTION_LIKE:
Douglas Gregora3e41532011-07-28 20:55:49 +00001641 getLocalIdentifier(**I, Record[0]);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001642 break;
1643
Sebastian Redl539c5062010-08-18 23:57:32 +00001644 case PP_TOKEN:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001645 // Ignore tokens.
1646 break;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001647 }
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001648 }
1649 }
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001650
1651 // Drain the unread macro-record offsets map.
1652 while (!UnreadMacroRecordOffsets.empty())
1653 LoadMacroDefinition(UnreadMacroRecordOffsets.begin());
1654}
1655
1656void ASTReader::LoadMacroDefinition(
1657 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos) {
1658 assert(Pos != UnreadMacroRecordOffsets.end() && "Unknown macro definition");
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001659 uint64_t Offset = Pos->second;
1660 UnreadMacroRecordOffsets.erase(Pos);
1661
Douglas Gregord32f0352011-07-22 06:10:01 +00001662 RecordLocation Loc = getLocalBitOffset(Offset);
1663 ReadMacroRecord(*Loc.F, Loc.Offset);
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001664}
1665
1666void ASTReader::LoadMacroDefinition(IdentifierInfo *II) {
1667 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos
1668 = UnreadMacroRecordOffsets.find(II);
1669 LoadMacroDefinition(Pos);
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001670}
1671
Sebastian Redl50e26582010-09-15 19:54:06 +00001672MacroDefinition *ASTReader::getMacroDefinition(MacroID ID) {
Douglas Gregor91096292010-10-02 19:29:26 +00001673 if (ID == 0 || ID > MacroDefinitionsLoaded.size())
Douglas Gregoraae92242010-03-19 21:51:54 +00001674 return 0;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001675
Douglas Gregor91096292010-10-02 19:29:26 +00001676 if (!MacroDefinitionsLoaded[ID - 1]) {
Douglas Gregor270e0142011-07-20 01:29:15 +00001677 GlobalMacroDefinitionMapType::iterator I =GlobalMacroDefinitionMap.find(ID);
1678 assert(I != GlobalMacroDefinitionMap.end() &&
1679 "Corrupted global macro definition map");
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00001680 Module &M = *I->second;
1681 unsigned Index = ID - 1 - M.BaseMacroDefinitionID;
1682 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
1683 M.PreprocessorDetailCursor.JumpToBit(M.MacroDefinitionOffsets[Index]);
1684 LoadPreprocessedEntity(M);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001685 }
1686
Douglas Gregor91096292010-10-02 19:29:26 +00001687 return MacroDefinitionsLoaded[ID - 1];
Douglas Gregoraae92242010-03-19 21:51:54 +00001688}
1689
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001690const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) {
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00001691 std::string Filename = filenameStrRef;
1692 MaybeAddSystemRootToFilename(Filename);
1693 const FileEntry *File = FileMgr.getFile(Filename);
1694 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1695 OriginalDir != CurrentDir) {
1696 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1697 OriginalDir,
1698 CurrentDir);
1699 if (!resolved.empty())
1700 File = FileMgr.getFile(resolved);
1701 }
1702
1703 return File;
1704}
1705
Douglas Gregor035611e2011-07-28 22:16:57 +00001706MacroID ASTReader::getGlobalMacroDefinitionID(Module &M, unsigned LocalID) {
Douglas Gregora863b4b2011-08-04 16:36:56 +00001707 if (LocalID < NUM_PREDEF_MACRO_IDS)
1708 return LocalID;
1709
1710 ContinuousRangeMap<uint32_t, int, 2>::iterator I
1711 = M.MacroDefinitionRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
1712 assert(I != M.MacroDefinitionRemap.end() &&
1713 "Invalid index into macro definition ID remap");
1714
1715 return LocalID + I->second;
Douglas Gregor035611e2011-07-28 22:16:57 +00001716}
1717
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001718/// \brief If we are loading a relocatable PCH file, and the filename is
1719/// not an absolute path, add the system root to the beginning of the file
1720/// name.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001721void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001722 // If this is not a relocatable PCH file, there's nothing to do.
1723 if (!RelocatablePCH)
1724 return;
Mike Stump11289f42009-09-09 15:08:12 +00001725
Michael J. Spencerf28df4c2010-12-17 21:22:22 +00001726 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001727 return;
1728
Douglas Gregorc567ba22011-07-22 16:35:34 +00001729 if (isysroot.empty()) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001730 // If no system root was given, default to '/'
1731 Filename.insert(Filename.begin(), '/');
1732 return;
1733 }
Mike Stump11289f42009-09-09 15:08:12 +00001734
Douglas Gregorc567ba22011-07-22 16:35:34 +00001735 unsigned Length = isysroot.size();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001736 if (isysroot[Length - 1] != '/')
1737 Filename.insert(Filename.begin(), '/');
Mike Stump11289f42009-09-09 15:08:12 +00001738
Douglas Gregorc567ba22011-07-22 16:35:34 +00001739 Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001740}
1741
Sebastian Redl2c499f62010-08-18 23:56:43 +00001742ASTReader::ASTReadResult
Douglas Gregora6895d82011-07-22 16:00:58 +00001743ASTReader::ReadASTBlock(Module &F) {
Sebastian Redl34522812010-07-16 17:50:48 +00001744 llvm::BitstreamCursor &Stream = F.Stream;
1745
Sebastian Redl539c5062010-08-18 23:57:32 +00001746 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001747 Error("malformed block record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001748 return Failure;
1749 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001750
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001751 // Read all of the records and blocks for the ASt file.
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001752 RecordData Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001753 while (!Stream.AtEndOfStream()) {
1754 unsigned Code = Stream.ReadCode();
1755 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor55abb232009-04-10 20:39:37 +00001756 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001757 Error("error at end of module block in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001758 return Failure;
1759 }
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001760
Douglas Gregor55abb232009-04-10 20:39:37 +00001761 return Success;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001762 }
1763
1764 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1765 switch (Stream.ReadSubBlockID()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001766 case DECLTYPES_BLOCK_ID:
Chris Lattnere78a6be2009-04-27 01:05:14 +00001767 // We lazily load the decls block, but we want to set up the
1768 // DeclsCursor cursor to point into it. Clone our current bitcode
1769 // cursor to it, enter the block and read the abbrevs in that block.
1770 // With the main cursor, we just skip over it.
Sebastian Redl34522812010-07-16 17:50:48 +00001771 F.DeclsCursor = Stream;
Chris Lattnere78a6be2009-04-27 01:05:14 +00001772 if (Stream.SkipBlock() || // Skip with the main cursor.
1773 // Read the abbrevs.
Sebastian Redl539c5062010-08-18 23:57:32 +00001774 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001775 Error("malformed block record in AST file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001776 return Failure;
1777 }
1778 break;
Mike Stump11289f42009-09-09 15:08:12 +00001779
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00001780 case DECL_UPDATES_BLOCK_ID:
1781 if (Stream.SkipBlock()) {
1782 Error("malformed block record in AST file");
1783 return Failure;
1784 }
1785 break;
1786
Sebastian Redl539c5062010-08-18 23:57:32 +00001787 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl34522812010-07-16 17:50:48 +00001788 F.MacroCursor = Stream;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001789 if (PP)
1790 PP->setExternalSource(this);
1791
Douglas Gregor796d76a2010-10-20 22:00:55 +00001792 if (Stream.SkipBlock() ||
1793 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001794 Error("malformed block record in AST file");
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001795 return Failure;
1796 }
Douglas Gregor796d76a2010-10-20 22:00:55 +00001797 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001798 break;
Steve Naroff2ddea052009-04-23 10:39:46 +00001799
Douglas Gregor92a96f52011-02-08 21:58:10 +00001800 case PREPROCESSOR_DETAIL_BLOCK_ID:
1801 F.PreprocessorDetailCursor = Stream;
1802 if (Stream.SkipBlock() ||
1803 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
1804 PREPROCESSOR_DETAIL_BLOCK_ID)) {
1805 Error("malformed preprocessor detail record in AST file");
1806 return Failure;
1807 }
1808 F.PreprocessorDetailStartOffset
1809 = F.PreprocessorDetailCursor.GetCurrentBitNo();
1810 break;
1811
Sebastian Redl539c5062010-08-18 23:57:32 +00001812 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl393f8b72010-07-19 20:52:06 +00001813 switch (ReadSourceManagerBlock(F)) {
Douglas Gregor92863e42009-04-10 23:10:45 +00001814 case Success:
1815 break;
1816
1817 case Failure:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001818 Error("malformed source manager block in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001819 return Failure;
Douglas Gregor92863e42009-04-10 23:10:45 +00001820
1821 case IgnorePCH:
1822 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00001823 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001824 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001825 }
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001826 continue;
1827 }
1828
1829 if (Code == llvm::bitc::DEFINE_ABBREV) {
1830 Stream.ReadAbbrevRecord();
1831 continue;
1832 }
1833
1834 // Read and process a record.
1835 Record.clear();
Douglas Gregorbfbde532009-04-10 21:16:55 +00001836 const char *BlobStart = 0;
1837 unsigned BlobLen = 0;
Sebastian Redl539c5062010-08-18 23:57:32 +00001838 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001839 &BlobStart, &BlobLen)) {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001840 default: // Default behavior: ignore.
1841 break;
1842
Sebastian Redl539c5062010-08-18 23:57:32 +00001843 case METADATA: {
1844 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1845 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001846 : diag::warn_pch_version_too_new);
1847 return IgnorePCH;
1848 }
1849
1850 RelocatablePCH = Record[4];
1851 if (Listener) {
1852 std::string TargetTriple(BlobStart, BlobLen);
1853 if (Listener->ReadTargetTriple(TargetTriple))
1854 return IgnorePCH;
1855 }
1856 break;
1857 }
1858
Douglas Gregor29cc6422011-08-17 21:07:30 +00001859 case IMPORTS: {
1860 // Load each of the imported PCH files.
1861 unsigned Idx = 0, N = Record.size();
1862 while (Idx < N) {
1863 // Read information about the AST file.
1864 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
1865 unsigned Length = Record[Idx++];
1866 llvm::SmallString<128> ImportedFile(Record.begin() + Idx,
1867 Record.begin() + Idx + Length);
1868 Idx += Length;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001869
Douglas Gregor29cc6422011-08-17 21:07:30 +00001870 // Load the AST file.
Douglas Gregordf0c1512011-08-18 04:12:04 +00001871 switch(ReadASTCore(ImportedFile, ImportedKind, &F)) {
Douglas Gregor29cc6422011-08-17 21:07:30 +00001872 case Failure: return Failure;
1873 // If we have to ignore the dependency, we'll have to ignore this too.
1874 case IgnorePCH: return IgnorePCH;
1875 case Success: break;
1876 }
1877 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001878 break;
1879 }
1880
Douglas Gregor5204bde2011-08-02 16:26:37 +00001881 case TYPE_OFFSET: {
Sebastian Redl9e687992010-07-19 22:06:55 +00001882 if (F.LocalNumTypes != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001883 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001884 return Failure;
1885 }
Sebastian Redl9e687992010-07-19 22:06:55 +00001886 F.TypeOffsets = (const uint32_t *)BlobStart;
1887 F.LocalNumTypes = Record[0];
Douglas Gregor3b65ed02011-08-02 18:32:54 +00001888 unsigned LocalBaseTypeIndex = Record[1];
1889 F.BaseTypeIndex = getTotalNumTypes();
Douglas Gregor8ab4ea82011-07-29 00:21:44 +00001890
Douglas Gregor5204bde2011-08-02 16:26:37 +00001891 if (F.LocalNumTypes > 0) {
1892 // Introduce the global -> local mapping for types within this module.
1893 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
1894
1895 // Introduce the local -> global mapping for types within this module.
Douglas Gregor3b65ed02011-08-02 18:32:54 +00001896 F.TypeRemap.insert(std::make_pair(LocalBaseTypeIndex,
1897 F.BaseTypeIndex - LocalBaseTypeIndex));
Douglas Gregor5204bde2011-08-02 16:26:37 +00001898
1899 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
1900 }
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001901 break;
Douglas Gregor5204bde2011-08-02 16:26:37 +00001902 }
1903
Douglas Gregorf7180622011-08-03 15:48:04 +00001904 case DECL_OFFSET: {
Sebastian Redl9e687992010-07-19 22:06:55 +00001905 if (F.LocalNumDecls != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001906 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001907 return Failure;
1908 }
Sebastian Redl9e687992010-07-19 22:06:55 +00001909 F.DeclOffsets = (const uint32_t *)BlobStart;
1910 F.LocalNumDecls = Record[0];
Douglas Gregorf7180622011-08-03 15:48:04 +00001911 unsigned LocalBaseDeclID = Record[1];
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00001912 F.BaseDeclID = getTotalNumDecls();
Douglas Gregor047d2ef2011-07-20 00:27:43 +00001913
Douglas Gregorf7180622011-08-03 15:48:04 +00001914 if (F.LocalNumDecls > 0) {
1915 // Introduce the global -> local mapping for declarations within this
1916 // module.
Douglas Gregordab42432011-08-12 00:15:20 +00001917 GlobalDeclMap.insert(
1918 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
Douglas Gregorf7180622011-08-03 15:48:04 +00001919
1920 // Introduce the local -> global mapping for declarations within this
1921 // module.
1922 F.DeclRemap.insert(std::make_pair(LocalBaseDeclID,
1923 F.BaseDeclID - LocalBaseDeclID));
1924
1925 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
1926 }
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001927 break;
Douglas Gregorf7180622011-08-03 15:48:04 +00001928 }
1929
Sebastian Redl539c5062010-08-18 23:57:32 +00001930 case TU_UPDATE_LEXICAL: {
Douglas Gregordab42432011-08-12 00:15:20 +00001931 DeclContext *TU = Context ? Context->getTranslationUnitDecl() : 0;
Douglas Gregor94619c82011-08-24 19:03:07 +00001932 DeclContextInfo &Info = F.DeclContextInfos[TU];
1933 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(BlobStart);
1934 Info.NumLexicalDecls
1935 = static_cast<unsigned int>(BlobLen / sizeof(KindDeclIDPair));
Douglas Gregordab42432011-08-12 00:15:20 +00001936 if (TU)
1937 TU->setHasExternalLexicalStorage(true);
1938
Sebastian Redl4b1f4902010-07-27 18:24:41 +00001939 break;
1940 }
1941
Sebastian Redld7dce0a2010-08-24 00:50:04 +00001942 case UPDATE_VISIBLE: {
Douglas Gregorf7180622011-08-03 15:48:04 +00001943 unsigned Idx = 0;
1944 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
Sebastian Redld7dce0a2010-08-24 00:50:04 +00001945 void *Table = ASTDeclContextNameLookupTable::Create(
Douglas Gregorf7180622011-08-03 15:48:04 +00001946 (const unsigned char *)BlobStart + Record[Idx++],
Sebastian Redld7dce0a2010-08-24 00:50:04 +00001947 (const unsigned char *)BlobStart,
Douglas Gregor903b7e92011-07-22 00:38:23 +00001948 ASTDeclContextNameLookupTrait(*this, F));
Douglas Gregordab42432011-08-12 00:15:20 +00001949 if (ID == PREDEF_DECL_TRANSLATION_UNIT_ID && Context) { // Is it the TU?
Douglas Gregordab42432011-08-12 00:15:20 +00001950 DeclContext *TU = Context->getTranslationUnitDecl();
Douglas Gregor94619c82011-08-24 19:03:07 +00001951 F.DeclContextInfos[TU].NameLookupTableData = Table;
Douglas Gregordab42432011-08-12 00:15:20 +00001952 TU->setHasExternalVisibleStorage(true);
Sebastian Redld7dce0a2010-08-24 00:50:04 +00001953 } else
Douglas Gregorf7180622011-08-03 15:48:04 +00001954 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
Sebastian Redld7dce0a2010-08-24 00:50:04 +00001955 break;
1956 }
1957
Sebastian Redl539c5062010-08-18 23:57:32 +00001958 case REDECLS_UPDATE_LATEST: {
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00001959 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
Douglas Gregorf7180622011-08-03 15:48:04 +00001960 for (unsigned i = 0, e = Record.size(); i < e; /* in loop */) {
1961 DeclID First = ReadDeclID(F, Record, i);
1962 DeclID Latest = ReadDeclID(F, Record, i);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00001963 FirstLatestDeclIDs[First] = Latest;
1964 }
1965 break;
1966 }
1967
Sebastian Redl539c5062010-08-18 23:57:32 +00001968 case LANGUAGE_OPTIONS:
Douglas Gregorce3a8292010-07-27 00:27:13 +00001969 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor55abb232009-04-10 20:39:37 +00001970 return IgnorePCH;
1971 break;
Douglas Gregorbfbde532009-04-10 21:16:55 +00001972
Sebastian Redl539c5062010-08-18 23:57:32 +00001973 case IDENTIFIER_TABLE:
Sebastian Redl393f8b72010-07-19 20:52:06 +00001974 F.IdentifierTableData = BlobStart;
Douglas Gregor0e149972009-04-25 19:10:14 +00001975 if (Record[0]) {
Sebastian Redl393f8b72010-07-19 20:52:06 +00001976 F.IdentifierLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001977 = ASTIdentifierLookupTable::Create(
Sebastian Redl393f8b72010-07-19 20:52:06 +00001978 (const unsigned char *)F.IdentifierTableData + Record[0],
1979 (const unsigned char *)F.IdentifierTableData,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001980 ASTIdentifierLookupTrait(*this, F));
Douglas Gregor49bf76b2011-07-21 18:46:38 +00001981 if (PP) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001982 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor49bf76b2011-07-21 18:46:38 +00001983 PP->getHeaderSearchInfo().SetExternalLookup(this);
1984 }
Douglas Gregor0e149972009-04-25 19:10:14 +00001985 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001986 break;
1987
Douglas Gregor1ab036c2011-08-03 21:49:18 +00001988 case IDENTIFIER_OFFSET: {
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00001989 if (F.LocalNumIdentifiers != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001990 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001991 return Failure;
1992 }
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00001993 F.IdentifierOffsets = (const uint32_t *)BlobStart;
1994 F.LocalNumIdentifiers = Record[0];
Douglas Gregor1ab036c2011-08-03 21:49:18 +00001995 unsigned LocalBaseIdentifierID = Record[1];
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00001996 F.BaseIdentifierID = getTotalNumIdentifiers();
Douglas Gregor19d26352011-07-20 00:59:32 +00001997
Douglas Gregor1ab036c2011-08-03 21:49:18 +00001998 if (F.LocalNumIdentifiers > 0) {
1999 // Introduce the global -> local mapping for identifiers within this
2000 // module.
2001 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
2002 &F));
2003
2004 // Introduce the local -> global mapping for identifiers within this
2005 // module.
2006 F.IdentifierRemap.insert(
2007 std::make_pair(LocalBaseIdentifierID,
2008 F.BaseIdentifierID - LocalBaseIdentifierID));
2009
2010 IdentifiersLoaded.resize(IdentifiersLoaded.size()
2011 + F.LocalNumIdentifiers);
2012 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002013 break;
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002014 }
2015
Sebastian Redl539c5062010-08-18 23:57:32 +00002016 case EXTERNAL_DEFINITIONS:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002017 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2018 ExternalDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002019 break;
Douglas Gregor08f01292009-04-17 22:13:46 +00002020
Sebastian Redl539c5062010-08-18 23:57:32 +00002021 case SPECIAL_TYPES:
Douglas Gregor903b7e92011-07-22 00:38:23 +00002022 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2023 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
Douglas Gregor652d82a2009-04-18 05:55:16 +00002024 break;
2025
Sebastian Redl539c5062010-08-18 23:57:32 +00002026 case STATISTICS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002027 TotalNumStatements += Record[0];
2028 TotalNumMacros += Record[1];
2029 TotalLexicalDeclContexts += Record[2];
2030 TotalVisibleDeclContexts += Record[3];
Douglas Gregor08f01292009-04-17 22:13:46 +00002031 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00002032
Sebastian Redl539c5062010-08-18 23:57:32 +00002033 case UNUSED_FILESCOPED_DECLS:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002034 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2035 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
Tanya Lattner90073802010-02-12 00:07:30 +00002036 break;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00002037
Alexis Hunt27a761d2011-05-04 23:29:54 +00002038 case DELEGATING_CTORS:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002039 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2040 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
Alexis Hunt27a761d2011-05-04 23:29:54 +00002041 break;
2042
Sebastian Redl539c5062010-08-18 23:57:32 +00002043 case WEAK_UNDECLARED_IDENTIFIERS:
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00002044 if (Record.size() % 4 != 0) {
2045 Error("invalid weak identifiers record");
2046 return Failure;
2047 }
2048
2049 // FIXME: Ignore weak undeclared identifiers from non-original PCH
2050 // files. This isn't the way to do it :)
2051 WeakUndeclaredIdentifiers.clear();
2052
2053 // Translate the weak, undeclared identifiers into global IDs.
2054 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2055 WeakUndeclaredIdentifiers.push_back(
2056 getGlobalIdentifierID(F, Record[I++]));
2057 WeakUndeclaredIdentifiers.push_back(
2058 getGlobalIdentifierID(F, Record[I++]));
2059 WeakUndeclaredIdentifiers.push_back(
2060 ReadSourceLocation(F, Record, I).getRawEncoding());
2061 WeakUndeclaredIdentifiers.push_back(Record[I++]);
2062 }
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002063 break;
2064
Sebastian Redl539c5062010-08-18 23:57:32 +00002065 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002066 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2067 LocallyScopedExternalDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002068 break;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002069
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002070 case SELECTOR_OFFSETS: {
Sebastian Redla19a67f2010-08-03 21:58:15 +00002071 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redlada023c2010-08-04 20:40:17 +00002072 F.LocalNumSelectors = Record[0];
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002073 unsigned LocalBaseSelectorID = Record[1];
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00002074 F.BaseSelectorID = getTotalNumSelectors();
Douglas Gregor2262d282011-07-20 01:10:58 +00002075
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002076 if (F.LocalNumSelectors > 0) {
2077 // Introduce the global -> local mapping for selectors within this
2078 // module.
2079 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2080
2081 // Introduce the local -> global mapping for selectors within this
2082 // module.
2083 F.SelectorRemap.insert(std::make_pair(LocalBaseSelectorID,
2084 F.BaseSelectorID - LocalBaseSelectorID));
Douglas Gregor95c13f52009-04-25 17:48:32 +00002085
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002086 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
2087 }
2088 break;
2089 }
2090
Sebastian Redl539c5062010-08-18 23:57:32 +00002091 case METHOD_POOL:
Sebastian Redlada023c2010-08-04 20:40:17 +00002092 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002093 if (Record[0])
Sebastian Redlada023c2010-08-04 20:40:17 +00002094 F.SelectorLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002095 = ASTSelectorLookupTable::Create(
Sebastian Redlada023c2010-08-04 20:40:17 +00002096 F.SelectorLookupTableData + Record[0],
2097 F.SelectorLookupTableData,
Douglas Gregor7fb09192011-07-21 22:35:25 +00002098 ASTSelectorLookupTrait(*this, F));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002099 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorc78d3462009-04-24 21:10:55 +00002100 break;
Douglas Gregoreda6a892009-04-26 00:07:37 +00002101
Sebastian Redl96371b42010-09-22 00:42:30 +00002102 case REFERENCED_SELECTOR_POOL:
Douglas Gregor3f8f04f2011-07-28 14:41:43 +00002103 if (!Record.empty()) {
2104 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2105 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2106 Record[Idx++]));
2107 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2108 getRawEncoding());
2109 }
2110 }
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002111 break;
2112
Sebastian Redl539c5062010-08-18 23:57:32 +00002113 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002114 if (!Record.empty() && Listener)
2115 Listener->ReadCounter(Record[0]);
Douglas Gregoreda6a892009-04-26 00:07:37 +00002116 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00002117
Douglas Gregor925296b2011-07-19 16:10:42 +00002118 case SOURCE_LOCATION_OFFSETS: {
2119 F.SLocEntryOffsets = (const uint32_t *)BlobStart;
Sebastian Redlb293a452010-07-20 21:20:32 +00002120 F.LocalNumSLocEntries = Record[0];
Douglas Gregor925296b2011-07-19 16:10:42 +00002121 llvm::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
2122 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries, Record[1]);
2123 // Make our entry in the range map. BaseID is negative and growing, so
2124 // we invert it. Because we invert it, though, we need the other end of
2125 // the range.
2126 unsigned RangeStart =
2127 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2128 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2129 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2130
2131 // Initialize the remapping table.
2132 // Invalid stays invalid.
2133 F.SLocRemap.insert(std::make_pair(0U, 0));
2134 // This module. Base was 2 when being compiled.
2135 F.SLocRemap.insert(std::make_pair(2U,
2136 static_cast<int>(F.SLocEntryBaseOffset - 2)));
Douglas Gregor49bf76b2011-07-21 18:46:38 +00002137
2138 TotalNumSLocEntries += F.LocalNumSLocEntries;
Douglas Gregor925296b2011-07-19 16:10:42 +00002139 break;
2140 }
2141
Douglas Gregor5a1797c2011-08-01 16:01:55 +00002142 case MODULE_OFFSET_MAP: {
Douglas Gregor925296b2011-07-19 16:10:42 +00002143 // Additional remapping information.
2144 const unsigned char *Data = (const unsigned char*)BlobStart;
2145 const unsigned char *DataEnd = Data + BlobLen;
Douglas Gregor00659902011-08-02 10:56:51 +00002146
2147 // Continuous range maps we may be updating in our module.
2148 ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap);
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002149 ContinuousRangeMap<uint32_t, int, 2>::Builder
2150 IdentifierRemap(F.IdentifierRemap);
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002151 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002152 PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2153 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregora863b4b2011-08-04 16:36:56 +00002154 MacroDefinitionRemap(F.MacroDefinitionRemap);
2155 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002156 SelectorRemap(F.SelectorRemap);
Douglas Gregorf7180622011-08-03 15:48:04 +00002157 ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap);
Douglas Gregor5204bde2011-08-02 16:26:37 +00002158 ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap);
2159
Douglas Gregor925296b2011-07-19 16:10:42 +00002160 while(Data < DataEnd) {
Douglas Gregor925296b2011-07-19 16:10:42 +00002161 uint16_t Len = io::ReadUnalignedLE16(Data);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002162 StringRef Name = StringRef((const char*)Data, Len);
Douglas Gregor00659902011-08-02 10:56:51 +00002163 Data += Len;
Jonathan D. Turnerb2b08232011-07-26 18:21:30 +00002164 Module *OM = ModuleMgr.lookup(Name);
Douglas Gregor925296b2011-07-19 16:10:42 +00002165 if (!OM) {
2166 Error("SourceLocation remap refers to unknown module");
2167 return Failure;
2168 }
Douglas Gregor00659902011-08-02 10:56:51 +00002169
2170 uint32_t SLocOffset = io::ReadUnalignedLE32(Data);
2171 uint32_t IdentifierIDOffset = io::ReadUnalignedLE32(Data);
2172 uint32_t PreprocessedEntityIDOffset = io::ReadUnalignedLE32(Data);
2173 uint32_t MacroDefinitionIDOffset = io::ReadUnalignedLE32(Data);
2174 uint32_t SelectorIDOffset = io::ReadUnalignedLE32(Data);
2175 uint32_t DeclIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregor5204bde2011-08-02 16:26:37 +00002176 uint32_t TypeIndexOffset = io::ReadUnalignedLE32(Data);
Douglas Gregor00659902011-08-02 10:56:51 +00002177
2178 // Source location offset is mapped to OM->SLocEntryBaseOffset.
2179 SLocRemap.insert(std::make_pair(SLocOffset,
2180 static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset)));
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002181 IdentifierRemap.insert(
2182 std::make_pair(IdentifierIDOffset,
2183 OM->BaseIdentifierID - IdentifierIDOffset));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002184 PreprocessedEntityRemap.insert(
2185 std::make_pair(PreprocessedEntityIDOffset,
2186 OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset));
Douglas Gregora863b4b2011-08-04 16:36:56 +00002187 MacroDefinitionRemap.insert(
2188 std::make_pair(MacroDefinitionIDOffset,
2189 OM->BaseMacroDefinitionID - MacroDefinitionIDOffset));
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002190 SelectorRemap.insert(std::make_pair(SelectorIDOffset,
2191 OM->BaseSelectorID - SelectorIDOffset));
Douglas Gregorf7180622011-08-03 15:48:04 +00002192 DeclRemap.insert(std::make_pair(DeclIDOffset,
2193 OM->BaseDeclID - DeclIDOffset));
2194
Douglas Gregor5204bde2011-08-02 16:26:37 +00002195 TypeRemap.insert(std::make_pair(TypeIndexOffset,
Douglas Gregor3b65ed02011-08-02 18:32:54 +00002196 OM->BaseTypeIndex - TypeIndexOffset));
Douglas Gregor925296b2011-07-19 16:10:42 +00002197 }
2198 break;
2199 }
2200
Douglas Gregor925296b2011-07-19 16:10:42 +00002201 case SOURCE_MANAGER_LINE_TABLE:
2202 if (ParseLineTable(F, Record))
2203 return Failure;
Douglas Gregor258ae542009-04-27 06:38:32 +00002204 break;
2205
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00002206 case FILE_SOURCE_LOCATION_OFFSETS:
2207 F.SLocFileOffsets = (const uint32_t *)BlobStart;
2208 F.LocalNumSLocFileEntries = Record[0];
2209 break;
2210
Douglas Gregor925296b2011-07-19 16:10:42 +00002211 case SOURCE_LOCATION_PRELOADS: {
2212 // Need to transform from the local view (1-based IDs) to the global view,
2213 // which is based off F.SLocEntryBaseID.
Douglas Gregora918bab2011-08-25 21:09:44 +00002214 if (!F.PreloadSLocEntries.empty()) {
2215 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
2216 return Failure;
2217 }
2218
2219 F.PreloadSLocEntries.swap(Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002220 break;
Douglas Gregor925296b2011-07-19 16:10:42 +00002221 }
Douglas Gregorc5046832009-04-27 18:38:38 +00002222
Sebastian Redl539c5062010-08-18 23:57:32 +00002223 case STAT_CACHE: {
Douglas Gregor606c4ac2011-02-05 19:42:43 +00002224 if (!DisableStatCache) {
2225 ASTStatCache *MyStatCache =
2226 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
2227 (const unsigned char *)BlobStart,
2228 NumStatHits, NumStatMisses);
2229 FileMgr.addStatCache(MyStatCache);
2230 F.StatCache = MyStatCache;
2231 }
Douglas Gregorc5046832009-04-27 18:38:38 +00002232 break;
Douglas Gregord2eb58a2009-10-16 18:18:30 +00002233 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00002234
Sebastian Redl539c5062010-08-18 23:57:32 +00002235 case EXT_VECTOR_DECLS:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002236 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2237 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002238 break;
2239
Sebastian Redl539c5062010-08-18 23:57:32 +00002240 case VTABLE_USES:
Douglas Gregor4daf6a32011-07-28 19:11:31 +00002241 if (Record.size() % 3 != 0) {
2242 Error("Invalid VTABLE_USES record");
2243 return Failure;
2244 }
2245
Sebastian Redl08aca90252010-08-05 18:21:25 +00002246 // Later tables overwrite earlier ones.
Douglas Gregor4daf6a32011-07-28 19:11:31 +00002247 // FIXME: Modules will have some trouble with this. This is clearly not
2248 // the right way to do this.
Douglas Gregor7fb09192011-07-21 22:35:25 +00002249 VTableUses.clear();
Douglas Gregor4daf6a32011-07-28 19:11:31 +00002250
2251 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
2252 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
2253 VTableUses.push_back(
2254 ReadSourceLocation(F, Record, Idx).getRawEncoding());
2255 VTableUses.push_back(Record[Idx++]);
2256 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002257 break;
2258
Sebastian Redl539c5062010-08-18 23:57:32 +00002259 case DYNAMIC_CLASSES:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002260 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2261 DynamicClasses.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002262 break;
2263
Sebastian Redl539c5062010-08-18 23:57:32 +00002264 case PENDING_IMPLICIT_INSTANTIATIONS:
Douglas Gregorbbbc3672011-07-28 19:26:52 +00002265 if (PendingInstantiations.size() % 2 != 0) {
2266 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
2267 return Failure;
2268 }
2269
2270 // Later lists of pending instantiations overwrite earlier ones.
2271 // FIXME: This is most certainly wrong for modules.
2272 PendingInstantiations.clear();
2273 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2274 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
2275 PendingInstantiations.push_back(
2276 ReadSourceLocation(F, Record, I).getRawEncoding());
2277 }
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002278 break;
2279
Sebastian Redl539c5062010-08-18 23:57:32 +00002280 case SEMA_DECL_REFS:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002281 // Later tables overwrite earlier ones.
Douglas Gregor7fb09192011-07-21 22:35:25 +00002282 // FIXME: Modules will have some trouble with this.
2283 SemaDeclRefs.clear();
2284 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2285 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002286 break;
2287
Sebastian Redl539c5062010-08-18 23:57:32 +00002288 case ORIGINAL_FILE_NAME:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002289 // The primary AST will be the last to get here, so it will be the one
Sebastian Redlb293a452010-07-20 21:20:32 +00002290 // that's used.
Daniel Dunbar000c4ff2009-11-11 05:29:04 +00002291 ActualOriginalFileName.assign(BlobStart, BlobLen);
2292 OriginalFileName = ActualOriginalFileName;
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002293 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregor45fe0362009-05-12 01:31:05 +00002294 break;
Mike Stump11289f42009-09-09 15:08:12 +00002295
Douglas Gregora3b20262011-05-06 21:43:30 +00002296 case ORIGINAL_FILE_ID:
2297 OriginalFileID = FileID::get(Record[0]);
2298 break;
2299
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002300 case ORIGINAL_PCH_DIR:
2301 // The primary AST will be the last to get here, so it will be the one
2302 // that's used.
2303 OriginalDir.assign(BlobStart, BlobLen);
2304 break;
2305
Sebastian Redl539c5062010-08-18 23:57:32 +00002306 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek8bd09292010-02-12 23:31:14 +00002307 const std::string &CurBranch = getClangFullRepositoryVersion();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002308 StringRef ASTBranch(BlobStart, BlobLen);
2309 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002310 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregord54f3a12009-10-05 21:07:28 +00002311 return IgnorePCH;
2312 }
2313 break;
2314 }
Sebastian Redlfa061442010-07-21 20:07:32 +00002315
Douglas Gregor4a9c39a2011-07-21 00:47:40 +00002316 case MACRO_DEFINITION_OFFSETS: {
Sebastian Redlfa061442010-07-21 20:07:32 +00002317 F.MacroDefinitionOffsets = (const uint32_t *)BlobStart;
2318 F.NumPreallocatedPreprocessingEntities = Record[0];
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002319 unsigned LocalBasePreprocessedEntityID = Record[1];
2320 F.LocalNumMacroDefinitions = Record[2];
2321 unsigned LocalBaseMacroID = Record[3];
Douglas Gregora863b4b2011-08-04 16:36:56 +00002322
Douglas Gregor4a9c39a2011-07-21 00:47:40 +00002323 unsigned StartingID;
2324 if (PP) {
2325 if (!PP->getPreprocessingRecord())
2326 PP->createPreprocessingRecord(true);
2327 if (!PP->getPreprocessingRecord()->getExternalSource())
2328 PP->getPreprocessingRecord()->SetExternalSource(*this);
2329 StartingID
2330 = PP->getPreprocessingRecord()
2331 ->allocateLoadedEntities(F.NumPreallocatedPreprocessingEntities);
2332 } else {
2333 // FIXME: We'll eventually want to kill this path, since it assumes
2334 // a particular allocation strategy in the preprocessing record.
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002335 StartingID = getTotalNumPreprocessedEntities()
2336 - F.NumPreallocatedPreprocessingEntities;
Douglas Gregor4a9c39a2011-07-21 00:47:40 +00002337 }
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00002338 F.BaseMacroDefinitionID = getTotalNumMacroDefinitions();
2339 F.BasePreprocessedEntityID = StartingID;
Douglas Gregor4a9c39a2011-07-21 00:47:40 +00002340
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002341 if (F.NumPreallocatedPreprocessingEntities > 0) {
2342 // Introduce the global -> local mapping for preprocessed entities in
2343 // this module.
2344 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
2345
2346 // Introduce the local -> global mapping for preprocessed entities in
2347 // this module.
2348 F.PreprocessedEntityRemap.insert(
2349 std::make_pair(LocalBasePreprocessedEntityID,
2350 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
2351 }
2352
2353
Douglas Gregora863b4b2011-08-04 16:36:56 +00002354 if (F.LocalNumMacroDefinitions > 0) {
2355 // Introduce the global -> local mapping for macro definitions within
2356 // this module.
2357 GlobalMacroDefinitionMap.insert(
2358 std::make_pair(getTotalNumMacroDefinitions() + 1, &F));
2359
2360 // Introduce the local -> global mapping for macro definitions within
2361 // this module.
2362 F.MacroDefinitionRemap.insert(
2363 std::make_pair(LocalBaseMacroID,
2364 F.BaseMacroDefinitionID - LocalBaseMacroID));
2365
2366 MacroDefinitionsLoaded.resize(
Douglas Gregor270e0142011-07-20 01:29:15 +00002367 MacroDefinitionsLoaded.size() + F.LocalNumMacroDefinitions);
Douglas Gregora863b4b2011-08-04 16:36:56 +00002368 }
2369
Douglas Gregoraae92242010-03-19 21:51:54 +00002370 break;
Douglas Gregor4a9c39a2011-07-21 00:47:40 +00002371 }
2372
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002373 case DECL_UPDATE_OFFSETS: {
2374 if (Record.size() % 2 != 0) {
2375 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2376 return Failure;
2377 }
2378 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Douglas Gregorf7180622011-08-03 15:48:04 +00002379 DeclUpdateOffsets[getGlobalDeclID(F, Record[I])]
2380 .push_back(std::make_pair(&F, Record[I+1]));
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002381 break;
2382 }
2383
Sebastian Redl539c5062010-08-18 23:57:32 +00002384 case DECL_REPLACEMENTS: {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002385 if (Record.size() % 2 != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002386 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002387 return Failure;
2388 }
2389 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Douglas Gregorf7180622011-08-03 15:48:04 +00002390 ReplacedDecls[getGlobalDeclID(F, Record[I])]
2391 = std::make_pair(&F, Record[I+1]);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002392 break;
2393 }
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00002394
2395 case OBJC_CHAINED_CATEGORIES: {
2396 if (Record.size() % 3 != 0) {
2397 Error("invalid OBJC_CHAINED_CATEGORIES block in AST file");
2398 return Failure;
2399 }
2400 for (unsigned I = 0, N = Record.size(); I != N; I += 3) {
2401 serialization::GlobalDeclID GlobID = getGlobalDeclID(F, Record[I]);
2402 F.ChainedObjCCategories[GlobID] = std::make_pair(Record[I+1],
2403 Record[I+2]);
2404 ObjCChainedCategoriesInterfaces.insert(GlobID);
2405 }
2406 break;
2407 }
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002408
2409 case CXX_BASE_SPECIFIER_OFFSETS: {
2410 if (F.LocalNumCXXBaseSpecifiers != 0) {
2411 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2412 return Failure;
2413 }
2414
2415 F.LocalNumCXXBaseSpecifiers = Record[0];
2416 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
Jonathan D. Turner3766fdb2011-07-21 21:15:19 +00002417 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002418 break;
2419 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002420
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002421 case DIAG_PRAGMA_MAPPINGS:
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002422 if (Record.size() % 2 != 0) {
2423 Error("invalid DIAG_USER_MAPPINGS block in AST file");
2424 return Failure;
2425 }
Douglas Gregor925296b2011-07-19 16:10:42 +00002426
2427 if (F.PragmaDiagMappings.empty())
2428 F.PragmaDiagMappings.swap(Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002429 else
Douglas Gregor925296b2011-07-19 16:10:42 +00002430 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
2431 Record.begin(), Record.end());
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002432 break;
Douglas Gregor09b69892011-02-10 17:09:37 +00002433
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002434 case CUDA_SPECIAL_DECL_REFS:
2435 // Later tables overwrite earlier ones.
Douglas Gregor7fb09192011-07-21 22:35:25 +00002436 // FIXME: Modules will have trouble with this.
2437 CUDASpecialDeclRefs.clear();
2438 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2439 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002440 break;
Douglas Gregor09b69892011-02-10 17:09:37 +00002441
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002442 case HEADER_SEARCH_TABLE: {
Douglas Gregor09b69892011-02-10 17:09:37 +00002443 F.HeaderFileInfoTableData = BlobStart;
2444 F.LocalNumHeaderFileInfos = Record[1];
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002445 F.HeaderFileFrameworkStrings = BlobStart + Record[2];
Douglas Gregor09b69892011-02-10 17:09:37 +00002446 if (Record[0]) {
2447 F.HeaderFileInfoTable
2448 = HeaderFileInfoLookupTable::Create(
2449 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002450 (const unsigned char *)F.HeaderFileInfoTableData,
Douglas Gregora3e41532011-07-28 20:55:49 +00002451 HeaderFileInfoTrait(*this, F,
2452 PP? &PP->getHeaderSearchInfo() : 0,
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002453 BlobStart + Record[2]));
Douglas Gregor09b69892011-02-10 17:09:37 +00002454 if (PP)
2455 PP->getHeaderSearchInfo().SetExternalSource(this);
2456 }
2457 break;
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002458 }
2459
Peter Collingbourne5df20e02011-02-15 19:46:30 +00002460 case FP_PRAGMA_OPTIONS:
2461 // Later tables overwrite earlier ones.
2462 FPPragmaOptions.swap(Record);
2463 break;
2464
2465 case OPENCL_EXTENSIONS:
2466 // Later tables overwrite earlier ones.
2467 OpenCLExtensions.swap(Record);
2468 break;
Alexis Hunt27a761d2011-05-04 23:29:54 +00002469
2470 case TENTATIVE_DEFINITIONS:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002471 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2472 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Alexis Hunt27a761d2011-05-04 23:29:54 +00002473 break;
Douglas Gregorc2fa1692011-06-28 16:20:02 +00002474
2475 case KNOWN_NAMESPACES:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002476 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2477 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregorc2fa1692011-06-28 16:20:02 +00002478 break;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002479 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002480 }
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002481 Error("premature end of bitstream in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00002482 return Failure;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002483}
2484
Douglas Gregor2fdb6b52011-08-25 20:58:51 +00002485ASTReader::ASTReadResult ASTReader::validateFileEntries(Module &M) {
2486 llvm::BitstreamCursor &SLocEntryCursor = M.SLocEntryCursor;
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00002487
Douglas Gregor2fdb6b52011-08-25 20:58:51 +00002488 for (unsigned i = 0, e = M.LocalNumSLocFileEntries; i != e; ++i) {
2489 SLocEntryCursor.JumpToBit(M.SLocFileOffsets[i]);
2490 unsigned Code = SLocEntryCursor.ReadCode();
2491 if (Code == llvm::bitc::END_BLOCK ||
2492 Code == llvm::bitc::ENTER_SUBBLOCK ||
2493 Code == llvm::bitc::DEFINE_ABBREV) {
2494 Error("incorrectly-formatted source location entry in AST file");
2495 return Failure;
2496 }
2497
2498 RecordData Record;
2499 const char *BlobStart;
2500 unsigned BlobLen;
2501 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
2502 default:
2503 Error("incorrectly-formatted source location entry in AST file");
2504 return Failure;
2505
2506 case SM_SLOC_FILE_ENTRY: {
2507 StringRef Filename(BlobStart, BlobLen);
2508 const FileEntry *File = getFileEntry(Filename);
2509
2510 if (File == 0) {
2511 std::string ErrorStr = "could not find file '";
2512 ErrorStr += Filename;
2513 ErrorStr += "' referenced by AST file";
2514 Error(ErrorStr.c_str());
2515 return IgnorePCH;
2516 }
2517
2518 if (Record.size() < 6) {
2519 Error("source location entry is incorrect");
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00002520 return Failure;
2521 }
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00002522
Douglas Gregor2fdb6b52011-08-25 20:58:51 +00002523 // The stat info from the FileEntry came from the cached stat
2524 // info of the PCH, so we cannot trust it.
2525 struct stat StatBuf;
2526 if (::stat(File->getName(), &StatBuf) != 0) {
2527 StatBuf.st_size = File->getSize();
2528 StatBuf.st_mtime = File->getModificationTime();
2529 }
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00002530
Douglas Gregor2fdb6b52011-08-25 20:58:51 +00002531 if (((off_t)Record[4] != StatBuf.st_size
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00002532#if !defined(LLVM_ON_WIN32)
Douglas Gregor2fdb6b52011-08-25 20:58:51 +00002533 // In our regression testing, the Windows file system seems to
2534 // have inconsistent modification times that sometimes
2535 // erroneously trigger this error-handling path.
2536 || (time_t)Record[5] != StatBuf.st_mtime
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00002537#endif
Douglas Gregor2fdb6b52011-08-25 20:58:51 +00002538 )) {
2539 Error(diag::err_fe_pch_file_modified, Filename);
2540 return IgnorePCH;
2541 }
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00002542
Douglas Gregor2fdb6b52011-08-25 20:58:51 +00002543 break;
2544 }
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00002545 }
2546 }
2547
2548 return Success;
2549}
2550
Douglas Gregord09937f2011-08-25 21:19:59 +00002551namespace {
2552 /// \brief Visitor class used to look up identifirs in an AST file.
2553 class IdentifierLookupVisitor {
2554 StringRef Name;
2555 IdentifierInfo *Found;
2556 public:
2557 explicit IdentifierLookupVisitor(StringRef Name) : Name(Name), Found() { }
2558
2559 static bool visit(Module &M, void *UserData) {
2560 IdentifierLookupVisitor *This
2561 = static_cast<IdentifierLookupVisitor *>(UserData);
2562
2563 ASTIdentifierLookupTable *IdTable
Douglas Gregor4e4c83e2011-08-26 22:04:51 +00002564 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
Douglas Gregord09937f2011-08-25 21:19:59 +00002565 if (!IdTable)
2566 return false;
2567
2568 std::pair<const char*, unsigned> Key(This->Name.begin(),
2569 This->Name.size());
2570 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
2571 if (Pos == IdTable->end())
2572 return false;
2573
2574 // Dereferencing the iterator has the effect of building the
2575 // IdentifierInfo node and populating it with the various
2576 // declarations it needs.
2577 This->Found = *Pos;
2578 return true;
2579 }
2580
2581 // \brief Retrieve the identifier info found within the module
2582 // files.
2583 IdentifierInfo *getIdentifierInfo() const { return Found; }
2584 };
2585}
2586
2587
Sebastian Redl009e7f22010-10-05 16:15:19 +00002588ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
Douglas Gregora6895d82011-07-22 16:00:58 +00002589 ModuleKind Type) {
Douglas Gregordf0c1512011-08-18 04:12:04 +00002590 switch(ReadASTCore(FileName, Type, /*ImportedBy=*/0)) {
Sebastian Redl2abc0382010-07-16 20:41:52 +00002591 case Failure: return Failure;
2592 case IgnorePCH: return IgnorePCH;
2593 case Success: break;
2594 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002595
2596 // Here comes stuff that we only do once the entire chain is loaded.
Douglas Gregor49bf76b2011-07-21 18:46:38 +00002597
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002598 // Check the predefines buffers.
Douglas Gregor9125bd62011-07-27 16:30:06 +00002599 if (!DisableValidation && Type != MK_Module && CheckPredefinesBuffers())
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002600 return IgnorePCH;
2601
2602 if (PP) {
2603 // Initialization of keywords and pragmas occurs before the
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002604 // AST file is read, so there may be some identifiers that were
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002605 // loaded into the IdentifierTable before we intercepted the
2606 // creation of identifiers. Iterate through the list of known
2607 // identifiers and determine whether we have to establish
2608 // preprocessor definitions or top-level identifier declaration
2609 // chains for those identifiers.
2610 //
2611 // We copy the IdentifierInfo pointers to a small vector first,
2612 // since de-serializing declarations or macro definitions can add
2613 // new entries into the identifier table, invalidating the
2614 // iterators.
Douglas Gregor9125bd62011-07-27 16:30:06 +00002615 //
2616 // FIXME: We need a lazier way to load this information, e.g., by marking
2617 // the identifier data as 'dirty', so that it will be looked up in the
2618 // AST file(s) if it is uttered in the source. This could save us some
2619 // module load time.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002620 SmallVector<IdentifierInfo *, 128> Identifiers;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002621 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
2622 IdEnd = PP->getIdentifierTable().end();
2623 Id != IdEnd; ++Id)
2624 Identifiers.push_back(Id->second);
Douglas Gregord09937f2011-08-25 21:19:59 +00002625
2626 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2627 IdentifierLookupVisitor Visitor(Identifiers[I]->getName());
2628 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002629 }
2630 }
2631
2632 if (Context)
2633 InitializeContext(*Context);
2634
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002635 if (DeserializationListener)
2636 DeserializationListener->ReaderInitialized(this);
2637
Douglas Gregor936a5b42010-11-30 05:23:00 +00002638 // If this AST file is a precompiled preamble, then set the main file ID of
2639 // the source manager to the file source file from which the preamble was
2640 // built. This is the only valid way to use a precompiled preamble.
Douglas Gregora6895d82011-07-22 16:00:58 +00002641 if (Type == MK_Preamble) {
Douglas Gregora3b20262011-05-06 21:43:30 +00002642 if (OriginalFileID.isInvalid()) {
2643 SourceLocation Loc
2644 = SourceMgr.getLocation(FileMgr.getFile(getOriginalSourceFile()), 1, 1);
2645 if (Loc.isValid())
2646 OriginalFileID = SourceMgr.getDecomposedLoc(Loc).first;
Douglas Gregor936a5b42010-11-30 05:23:00 +00002647 }
Douglas Gregor925296b2011-07-19 16:10:42 +00002648 else {
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00002649 OriginalFileID = FileID::get(ModuleMgr.getPrimaryModule().SLocEntryBaseID
Douglas Gregor925296b2011-07-19 16:10:42 +00002650 + OriginalFileID.getOpaqueValue() - 1);
2651 }
2652
Douglas Gregora3b20262011-05-06 21:43:30 +00002653 if (!OriginalFileID.isInvalid())
2654 SourceMgr.SetPreambleFileID(OriginalFileID);
Douglas Gregor936a5b42010-11-30 05:23:00 +00002655 }
2656
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002657 return Success;
2658}
2659
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002660ASTReader::ASTReadResult ASTReader::ReadASTCore(StringRef FileName,
Douglas Gregordf0c1512011-08-18 04:12:04 +00002661 ModuleKind Type,
2662 Module *ImportedBy) {
Douglas Gregor4dd3e942011-08-19 02:29:29 +00002663 Module *M;
2664 bool NewModule;
2665 std::string ErrorStr;
2666 llvm::tie(M, NewModule) = ModuleMgr.addModule(FileName, Type, ImportedBy,
2667 ErrorStr);
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002668
Douglas Gregor4dd3e942011-08-19 02:29:29 +00002669 if (!M) {
2670 // We couldn't load the module.
2671 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
2672 + ErrorStr;
2673 Error(Msg);
2674 return Failure;
2675 }
2676
2677 if (!NewModule) {
2678 // We've already loaded this module.
2679 return Success;
2680 }
2681
2682 // FIXME: This seems rather a hack. Should CurrentDir be part of the
2683 // module?
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002684 if (FileName != "-") {
2685 CurrentDir = llvm::sys::path::parent_path(FileName);
2686 if (CurrentDir.empty()) CurrentDir = ".";
2687 }
2688
Douglas Gregor4dd3e942011-08-19 02:29:29 +00002689 Module &F = *M;
Sebastian Redl34522812010-07-16 17:50:48 +00002690 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002691 Stream.init(F.StreamFile);
Sebastian Redlfa061442010-07-21 20:07:32 +00002692 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Douglas Gregord32f0352011-07-22 06:10:01 +00002693
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002694 // Sniff for the signature.
2695 if (Stream.Read(8) != 'C' ||
2696 Stream.Read(8) != 'P' ||
2697 Stream.Read(8) != 'C' ||
2698 Stream.Read(8) != 'H') {
2699 Diag(diag::err_not_a_pch_file) << FileName;
2700 return Failure;
2701 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002702
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002703 while (!Stream.AtEndOfStream()) {
2704 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00002705
Douglas Gregor92863e42009-04-10 23:10:45 +00002706 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002707 Error("invalid record at top-level of AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002708 return Failure;
2709 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002710
2711 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregora868bbd2009-04-21 22:25:48 +00002712
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002713 // We only know the AST subblock ID.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002714 switch (BlockID) {
2715 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregor92863e42009-04-10 23:10:45 +00002716 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002717 Error("malformed BlockInfoBlock in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002718 return Failure;
2719 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002720 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00002721 case AST_BLOCK_ID:
Sebastian Redl3e31c722010-08-18 23:56:56 +00002722 switch (ReadASTBlock(F)) {
Douglas Gregor55abb232009-04-10 20:39:37 +00002723 case Success:
2724 break;
2725
2726 case Failure:
Douglas Gregor92863e42009-04-10 23:10:45 +00002727 return Failure;
Douglas Gregor55abb232009-04-10 20:39:37 +00002728
2729 case IgnorePCH:
Douglas Gregorbfbde532009-04-10 21:16:55 +00002730 // FIXME: We could consider reading through to the end of this
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002731 // AST block, skipping subblocks, to see if there are other
2732 // AST blocks elsewhere.
Douglas Gregor0bc12932009-04-27 21:28:04 +00002733
Douglas Gregor925296b2011-07-19 16:10:42 +00002734 // FIXME: We can't clear loaded slocentries anymore.
2735 //SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor0bc12932009-04-27 21:28:04 +00002736
2737 // Remove the stat cache.
Sebastian Redl34522812010-07-16 17:50:48 +00002738 if (F.StatCache)
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002739 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor0bc12932009-04-27 21:28:04 +00002740
Douglas Gregor92863e42009-04-10 23:10:45 +00002741 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00002742 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002743 break;
2744 default:
Douglas Gregor92863e42009-04-10 23:10:45 +00002745 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002746 Error("malformed block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002747 return Failure;
2748 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002749 break;
2750 }
Mike Stump11289f42009-09-09 15:08:12 +00002751 }
Douglas Gregord32f0352011-07-22 06:10:01 +00002752
Douglas Gregora6895d82011-07-22 16:00:58 +00002753 // Once read, set the Module bit base offset and update the size in
Douglas Gregord32f0352011-07-22 06:10:01 +00002754 // bits of all files we've seen.
2755 F.GlobalBitOffset = TotalModulesSizeInBits;
2756 TotalModulesSizeInBits += F.SizeInBits;
2757 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
Douglas Gregor2fdb6b52011-08-25 20:58:51 +00002758
2759 // Make sure that the files this module was built against are still available.
2760 if (!DisableValidation) {
2761 switch(validateFileEntries(*M)) {
2762 case Failure: return Failure;
2763 case IgnorePCH: return IgnorePCH;
2764 case Success: break;
2765 }
2766 }
Douglas Gregora918bab2011-08-25 21:09:44 +00002767
2768 // Preload SLocEntries.
2769 for (unsigned I = 0, N = M->PreloadSLocEntries.size(); I != N; ++I) {
2770 int Index = int(M->PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
2771 ASTReadResult Result = ReadSLocEntryRecord(Index);
2772 if (Result != Success)
2773 return Failure;
2774 }
2775
Douglas Gregor2fdb6b52011-08-25 20:58:51 +00002776
Sebastian Redl2abc0382010-07-16 20:41:52 +00002777 return Success;
2778}
2779
Sebastian Redl2c499f62010-08-18 23:56:43 +00002780void ASTReader::setPreprocessor(Preprocessor &pp) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002781 PP = &pp;
Douglas Gregor4a9c39a2011-07-21 00:47:40 +00002782
2783 if (unsigned N = getTotalNumPreprocessedEntities()) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002784 if (!PP->getPreprocessingRecord())
Douglas Gregor998caea2011-05-06 16:33:08 +00002785 PP->createPreprocessingRecord(true);
Douglas Gregor4a9c39a2011-07-21 00:47:40 +00002786 PP->getPreprocessingRecord()->SetExternalSource(*this);
2787 PP->getPreprocessingRecord()->allocateLoadedEntities(N);
Douglas Gregoraae92242010-03-19 21:51:54 +00002788 }
Douglas Gregor4a9c39a2011-07-21 00:47:40 +00002789
2790 PP->getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor49bf76b2011-07-21 18:46:38 +00002791 PP->getHeaderSearchInfo().SetExternalSource(this);
Douglas Gregoraae92242010-03-19 21:51:54 +00002792}
2793
Sebastian Redl2c499f62010-08-18 23:56:43 +00002794void ASTReader::InitializeContext(ASTContext &Ctx) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002795 Context = &Ctx;
2796 assert(Context && "Passed null context!");
Douglas Gregor4e4c83e2011-08-26 22:04:51 +00002797
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002798 assert(PP && "Forgot to set Preprocessor ?");
2799 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor9882a5a2010-01-04 19:18:44 +00002800 PP->setExternalSource(this);
Douglas Gregor09b69892011-02-10 17:09:37 +00002801
Douglas Gregor94619c82011-08-24 19:03:07 +00002802 // If we have any update blocks for the TU waiting, we have to add
2803 // them before we deserialize anything.
Douglas Gregordab42432011-08-12 00:15:20 +00002804 TranslationUnitDecl *TU = Ctx.getTranslationUnitDecl();
Douglas Gregor94619c82011-08-24 19:03:07 +00002805 for (ModuleIterator M = ModuleMgr.begin(), MEnd = ModuleMgr.end();
2806 M != MEnd; ++M) {
2807 Module::DeclContextInfosMap::iterator DCU
2808 = (*M)->DeclContextInfos.find(0);
2809 if (DCU != (*M)->DeclContextInfos.end()) {
2810 // Insertion could invalidate map, so grab value first.
2811 DeclContextInfo Info = DCU->second;
2812 (*M)->DeclContextInfos.erase(DCU);
2813 (*M)->DeclContextInfos[TU] = Info;
2814 }
Douglas Gregoraa433012010-10-01 01:18:02 +00002815 }
Douglas Gregordab42432011-08-12 00:15:20 +00002816
2817 // If there's a listener, notify them that we "read" the translation unit.
2818 if (DeserializationListener)
2819 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID, TU);
Douglas Gregoraa433012010-10-01 01:18:02 +00002820
Douglas Gregordab42432011-08-12 00:15:20 +00002821 // Make sure we load the declaration update records for the translation unit,
2822 // if there are any.
2823 loadDeclUpdateRecords(PREDEF_DECL_TRANSLATION_UNIT_ID, TU);
2824
2825 // Note that the translation unit has external lexical and visible storage.
2826 TU->setHasExternalLexicalStorage(true);
2827 TU->setHasExternalVisibleStorage(true);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002828
Douglas Gregoraa8a8272011-08-11 22:18:49 +00002829 // FIXME: Find a better way to deal with collisions between these
2830 // built-in types. Right now, we just ignore the problem.
2831
2832 // Load the special types.
Jonathan D. Turnerf07f1312011-08-05 23:07:10 +00002833 if (Context->getBuiltinVaListType().isNull()) {
Jonathan D. Turnerf07f1312011-08-05 23:07:10 +00002834 Context->setBuiltinVaListType(
2835 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
Douglas Gregoraa8a8272011-08-11 22:18:49 +00002836 }
2837
Douglas Gregoraa8a8272011-08-11 22:18:49 +00002838 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL]) {
2839 if (Context->ObjCProtoType.isNull())
Douglas Gregor09c4aa82011-08-11 22:04:35 +00002840 Context->ObjCProtoType = GetType(Proto);
Douglas Gregoraa8a8272011-08-11 22:18:49 +00002841 }
2842
Douglas Gregoraa8a8272011-08-11 22:18:49 +00002843 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
2844 if (!Context->CFConstantStringTypeDecl)
Jonathan D. Turnerf07f1312011-08-05 23:07:10 +00002845 Context->setCFConstantStringType(GetType(String));
Douglas Gregoraa8a8272011-08-11 22:18:49 +00002846 }
2847
2848 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
2849 QualType FileType = GetType(File);
2850 if (FileType.isNull()) {
2851 Error("FILE type is NULL");
2852 return;
2853 }
2854
2855 if (!Context->FILEDecl) {
Jonathan D. Turnerf07f1312011-08-05 23:07:10 +00002856 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
2857 Context->setFILEDecl(Typedef->getDecl());
2858 else {
2859 const TagType *Tag = FileType->getAs<TagType>();
2860 if (!Tag) {
2861 Error("Invalid FILE type in AST file");
2862 return;
2863 }
2864 Context->setFILEDecl(Tag->getDecl());
2865 }
Douglas Gregor27821ce2009-07-07 16:35:42 +00002866 }
Douglas Gregoraa8a8272011-08-11 22:18:49 +00002867 }
2868
2869 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
2870 QualType Jmp_bufType = GetType(Jmp_buf);
2871 if (Jmp_bufType.isNull()) {
2872 Error("jmp_buf type is NULL");
2873 return;
2874 }
2875
2876 if (!Context->jmp_bufDecl) {
Jonathan D. Turnerf07f1312011-08-05 23:07:10 +00002877 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
2878 Context->setjmp_bufDecl(Typedef->getDecl());
2879 else {
2880 const TagType *Tag = Jmp_bufType->getAs<TagType>();
2881 if (!Tag) {
2882 Error("Invalid jmp_buf type in AST file");
2883 return;
2884 }
2885 Context->setjmp_bufDecl(Tag->getDecl());
2886 }
Mike Stumpa4de80b2009-07-28 02:25:19 +00002887 }
Douglas Gregoraa8a8272011-08-11 22:18:49 +00002888 }
2889
2890 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
2891 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
2892 if (Sigjmp_bufType.isNull()) {
2893 Error("sigjmp_buf type is NULL");
2894 return;
2895 }
2896
2897 if (!Context->sigjmp_bufDecl) {
Jonathan D. Turnerf07f1312011-08-05 23:07:10 +00002898 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
2899 Context->setsigjmp_bufDecl(Typedef->getDecl());
2900 else {
2901 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
2902 assert(Tag && "Invalid sigjmp_buf type in AST file");
2903 Context->setsigjmp_bufDecl(Tag->getDecl());
2904 }
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002905 }
Jonathan D. Turnerf07f1312011-08-05 23:07:10 +00002906 }
Richard Smith02e85f32011-04-14 22:09:26 +00002907
Douglas Gregoraa8a8272011-08-11 22:18:49 +00002908 if (unsigned ObjCIdRedef
2909 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
2910 if (Context->ObjCIdRedefinitionType.isNull())
2911 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
2912 }
2913
2914 if (unsigned ObjCClassRedef
2915 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
2916 if (Context->ObjCClassRedefinitionType.isNull())
2917 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
2918 }
2919
2920 if (unsigned ObjCSelRedef
2921 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
2922 if (Context->ObjCSelRedefinitionType.isNull())
2923 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
2924 }
2925
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002926 ReadPragmaDiagnosticMappings(Context->getDiagnostics());
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002927
2928 // If there were any CUDA special declarations, deserialize them.
2929 if (!CUDASpecialDeclRefs.empty()) {
2930 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
2931 Context->setcudaConfigureCallDecl(
2932 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
2933 }
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002934}
2935
Douglas Gregor45fe0362009-05-12 01:31:05 +00002936/// \brief Retrieve the name of the original source file name
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002937/// directly from the AST file, without actually loading the AST
Douglas Gregor45fe0362009-05-12 01:31:05 +00002938/// file.
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002939std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +00002940 FileManager &FileMgr,
Daniel Dunbar3b951482009-12-03 09:13:06 +00002941 Diagnostic &Diags) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002942 // Open the AST file.
Douglas Gregor45fe0362009-05-12 01:31:05 +00002943 std::string ErrStr;
2944 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner5159f612010-11-23 08:35:12 +00002945 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregor45fe0362009-05-12 01:31:05 +00002946 if (!Buffer) {
Daniel Dunbar3b951482009-12-03 09:13:06 +00002947 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002948 return std::string();
2949 }
2950
2951 // Initialize the stream
2952 llvm::BitstreamReader StreamFile;
2953 llvm::BitstreamCursor Stream;
Mike Stump11289f42009-09-09 15:08:12 +00002954 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregor45fe0362009-05-12 01:31:05 +00002955 (const unsigned char *)Buffer->getBufferEnd());
2956 Stream.init(StreamFile);
2957
2958 // Sniff for the signature.
2959 if (Stream.Read(8) != 'C' ||
2960 Stream.Read(8) != 'P' ||
2961 Stream.Read(8) != 'C' ||
2962 Stream.Read(8) != 'H') {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002963 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002964 return std::string();
2965 }
2966
2967 RecordData Record;
2968 while (!Stream.AtEndOfStream()) {
2969 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00002970
Douglas Gregor45fe0362009-05-12 01:31:05 +00002971 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2972 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump11289f42009-09-09 15:08:12 +00002973
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002974 // We only know the AST subblock ID.
Douglas Gregor45fe0362009-05-12 01:31:05 +00002975 switch (BlockID) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002976 case AST_BLOCK_ID:
2977 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002978 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002979 return std::string();
2980 }
2981 break;
Mike Stump11289f42009-09-09 15:08:12 +00002982
Douglas Gregor45fe0362009-05-12 01:31:05 +00002983 default:
2984 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002985 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002986 return std::string();
2987 }
2988 break;
2989 }
2990 continue;
2991 }
2992
2993 if (Code == llvm::bitc::END_BLOCK) {
2994 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002995 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002996 return std::string();
2997 }
2998 continue;
2999 }
3000
3001 if (Code == llvm::bitc::DEFINE_ABBREV) {
3002 Stream.ReadAbbrevRecord();
3003 continue;
3004 }
3005
3006 Record.clear();
3007 const char *BlobStart = 0;
3008 unsigned BlobLen = 0;
Mike Stump11289f42009-09-09 15:08:12 +00003009 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl539c5062010-08-18 23:57:32 +00003010 == ORIGINAL_FILE_NAME)
Douglas Gregor45fe0362009-05-12 01:31:05 +00003011 return std::string(BlobStart, BlobLen);
Mike Stump11289f42009-09-09 15:08:12 +00003012 }
Douglas Gregor45fe0362009-05-12 01:31:05 +00003013
3014 return std::string();
3015}
3016
Douglas Gregor55abb232009-04-10 20:39:37 +00003017/// \brief Parse the record that corresponds to a LangOptions data
3018/// structure.
3019///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003020/// This routine parses the language options from the AST file and then gives
3021/// them to the AST listener if one is set.
Douglas Gregor55abb232009-04-10 20:39:37 +00003022///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003023/// \returns true if the listener deems the file unacceptable, false otherwise.
Sebastian Redl2c499f62010-08-18 23:56:43 +00003024bool ASTReader::ParseLanguageOptions(
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003025 const SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003026 if (Listener) {
3027 LangOptions LangOpts;
Mike Stump11289f42009-09-09 15:08:12 +00003028
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003029 #define PARSE_LANGOPT(Option) \
3030 LangOpts.Option = Record[Idx]; \
3031 ++Idx
Mike Stump11289f42009-09-09 15:08:12 +00003032
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003033 unsigned Idx = 0;
3034 PARSE_LANGOPT(Trigraphs);
3035 PARSE_LANGOPT(BCPLComment);
3036 PARSE_LANGOPT(DollarIdents);
3037 PARSE_LANGOPT(AsmPreprocessor);
3038 PARSE_LANGOPT(GNUMode);
Chandler Carruthe03aa552010-04-17 20:17:31 +00003039 PARSE_LANGOPT(GNUKeywords);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003040 PARSE_LANGOPT(ImplicitInt);
3041 PARSE_LANGOPT(Digraphs);
3042 PARSE_LANGOPT(HexFloats);
3043 PARSE_LANGOPT(C99);
Peter Collingbournea686b5f2011-04-15 00:35:23 +00003044 PARSE_LANGOPT(C1X);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003045 PARSE_LANGOPT(Microsoft);
3046 PARSE_LANGOPT(CPlusPlus);
3047 PARSE_LANGOPT(CPlusPlus0x);
3048 PARSE_LANGOPT(CXXOperatorNames);
3049 PARSE_LANGOPT(ObjC1);
3050 PARSE_LANGOPT(ObjC2);
3051 PARSE_LANGOPT(ObjCNonFragileABI);
Fariborz Jahanian45878032010-02-09 19:31:38 +00003052 PARSE_LANGOPT(ObjCNonFragileABI2);
Fariborz Jahanian13f3b2f2011-01-07 18:59:25 +00003053 PARSE_LANGOPT(AppleKext);
Ted Kremenek1d56c9e2010-12-23 21:35:43 +00003054 PARSE_LANGOPT(ObjCDefaultSynthProperties);
Douglas Gregora860e6a2011-06-14 23:20:43 +00003055 PARSE_LANGOPT(ObjCInferRelatedResultType);
Fariborz Jahanian62c56022010-04-22 21:01:59 +00003056 PARSE_LANGOPT(NoConstantCFStrings);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003057 PARSE_LANGOPT(PascalStrings);
3058 PARSE_LANGOPT(WritableStrings);
3059 PARSE_LANGOPT(LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +00003060 PARSE_LANGOPT(AltiVec);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003061 PARSE_LANGOPT(Exceptions);
Anders Carlssonce8dd3a2011-02-19 23:53:54 +00003062 PARSE_LANGOPT(ObjCExceptions);
Anders Carlsson6bbd2682011-02-23 03:04:54 +00003063 PARSE_LANGOPT(CXXExceptions);
3064 PARSE_LANGOPT(SjLjExceptions);
Douglas Gregordbe39272011-02-01 15:15:22 +00003065 PARSE_LANGOPT(MSBitfields);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003066 PARSE_LANGOPT(NeXTRuntime);
3067 PARSE_LANGOPT(Freestanding);
3068 PARSE_LANGOPT(NoBuiltin);
3069 PARSE_LANGOPT(ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +00003070 PARSE_LANGOPT(POSIXThreads);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003071 PARSE_LANGOPT(Blocks);
3072 PARSE_LANGOPT(EmitAllDecls);
3073 PARSE_LANGOPT(MathErrno);
Chris Lattner51924e512010-06-26 21:25:03 +00003074 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
3075 Record[Idx++]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003076 PARSE_LANGOPT(HeinousExtensions);
3077 PARSE_LANGOPT(Optimize);
3078 PARSE_LANGOPT(OptimizeSize);
3079 PARSE_LANGOPT(Static);
3080 PARSE_LANGOPT(PICLevel);
3081 PARSE_LANGOPT(GNUInline);
3082 PARSE_LANGOPT(NoInline);
Chandler Carruth7ffce732011-04-23 20:05:38 +00003083 PARSE_LANGOPT(Deprecated);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003084 PARSE_LANGOPT(AccessControl);
3085 PARSE_LANGOPT(CharIsSigned);
John Thompsoned4e2952009-11-05 20:14:16 +00003086 PARSE_LANGOPT(ShortWChar);
Argyrios Kyrtzidisa88942a2011-01-15 02:56:16 +00003087 PARSE_LANGOPT(ShortEnums);
Chris Lattner51924e512010-06-26 21:25:03 +00003088 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
John McCall457a04e2010-10-22 21:05:15 +00003089 LangOpts.setVisibilityMode((Visibility)Record[Idx++]);
Daniel Dunbar143021e2009-09-21 04:16:19 +00003090 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
Chris Lattner51924e512010-06-26 21:25:03 +00003091 Record[Idx++]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003092 PARSE_LANGOPT(InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +00003093 PARSE_LANGOPT(OpenCL);
Peter Collingbourne546d0792010-12-01 19:14:57 +00003094 PARSE_LANGOPT(CUDA);
Mike Stumpd9546382009-12-12 01:27:46 +00003095 PARSE_LANGOPT(CatchUndefined);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003096 PARSE_LANGOPT(DefaultFPContract);
Roman Divackydc1f68d2011-03-01 17:36:40 +00003097 PARSE_LANGOPT(ElideConstructors);
3098 PARSE_LANGOPT(SpellChecking);
Roman Divacky65b88cd2011-03-01 17:40:53 +00003099 PARSE_LANGOPT(MRTD);
John McCall31168b02011-06-15 23:02:42 +00003100 PARSE_LANGOPT(ObjCAutoRefCount);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003101 #undef PARSE_LANGOPT
Douglas Gregor55abb232009-04-10 20:39:37 +00003102
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003103 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor55abb232009-04-10 20:39:37 +00003104 }
Douglas Gregor55abb232009-04-10 20:39:37 +00003105
3106 return false;
3107}
3108
Douglas Gregor14f77162011-08-25 18:03:05 +00003109namespace {
3110 /// \brief Visitor used by ASTReader::ReadPreprocessedEntities() to load
3111 /// all of the preprocessed entities within a module.
3112 class ReadPreprocessedEntitiesVisitor {
3113 ASTReader &Reader;
3114
3115 public:
3116 explicit ReadPreprocessedEntitiesVisitor(ASTReader &Reader)
3117 : Reader(Reader) { }
3118
3119 static bool visit(Module &M, bool Preorder, void *UserData) {
3120 if (Preorder)
3121 return false;
3122
3123 ReadPreprocessedEntitiesVisitor *This
3124 = static_cast<ReadPreprocessedEntitiesVisitor *>(UserData);
3125
3126 if (!M.PreprocessorDetailCursor.getBitStreamReader())
3127 return false;
3128
3129 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
3130 M.PreprocessorDetailCursor.JumpToBit(M.PreprocessorDetailStartOffset);
3131 while (This->Reader.LoadPreprocessedEntity(M)) { }
3132 return false;
3133 }
3134 };
3135}
Douglas Gregor92a96f52011-02-08 21:58:10 +00003136
Douglas Gregor14f77162011-08-25 18:03:05 +00003137void ASTReader::ReadPreprocessedEntities() {
3138 ReadPreprocessedEntitiesVisitor Visitor(*this);
3139 ModuleMgr.visitDepthFirst(&ReadPreprocessedEntitiesVisitor::visit, &Visitor);
Douglas Gregoraae92242010-03-19 21:51:54 +00003140}
3141
Douglas Gregor46c50012011-02-11 19:46:30 +00003142PreprocessedEntity *ASTReader::ReadPreprocessedEntityAtOffset(uint64_t Offset) {
Douglas Gregord32f0352011-07-22 06:10:01 +00003143 RecordLocation Loc = getLocalBitOffset(Offset);
Douglas Gregorf88e35b2010-11-30 06:16:57 +00003144
Douglas Gregor92a96f52011-02-08 21:58:10 +00003145 // Keep track of where we are in the stream, then jump back there
3146 // after reading this entity.
Douglas Gregord32f0352011-07-22 06:10:01 +00003147 SavedStreamPosition SavedPosition(Loc.F->PreprocessorDetailCursor);
3148 Loc.F->PreprocessorDetailCursor.JumpToBit(Loc.Offset);
3149 return LoadPreprocessedEntity(*Loc.F);
Douglas Gregorf88e35b2010-11-30 06:16:57 +00003150}
3151
Douglas Gregor69e94642011-08-25 18:14:34 +00003152namespace {
3153 /// \brief Visitor used to search for information about a header file.
3154 class HeaderFileInfoVisitor {
3155 ASTReader &Reader;
3156 const FileEntry *FE;
3157
3158 llvm::Optional<HeaderFileInfo> HFI;
3159
3160 public:
3161 HeaderFileInfoVisitor(ASTReader &Reader, const FileEntry *FE)
3162 : Reader(Reader), FE(FE) { }
3163
3164 static bool visit(Module &M, void *UserData) {
3165 HeaderFileInfoVisitor *This
3166 = static_cast<HeaderFileInfoVisitor *>(UserData);
3167
3168 HeaderFileInfoTrait Trait(This->Reader, M,
3169 &This->Reader.getPreprocessor().getHeaderSearchInfo(),
3170 M.HeaderFileFrameworkStrings,
3171 This->FE->getName());
3172
3173 HeaderFileInfoLookupTable *Table
3174 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
3175 if (!Table)
3176 return false;
3177
3178 // Look in the on-disk hash table for an entry for this file name.
3179 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE->getName(),
3180 &Trait);
3181 if (Pos == Table->end())
3182 return false;
3183
3184 This->HFI = *Pos;
3185 return true;
3186 }
3187
3188 llvm::Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
3189 };
3190}
3191
Douglas Gregor09b69892011-02-10 17:09:37 +00003192HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Douglas Gregor69e94642011-08-25 18:14:34 +00003193 HeaderFileInfoVisitor Visitor(*this, FE);
3194 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
3195 if (llvm::Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) {
Douglas Gregor09b69892011-02-10 17:09:37 +00003196 if (Listener)
Douglas Gregor69e94642011-08-25 18:14:34 +00003197 Listener->ReadHeaderFileInfo(*HFI, FE->getUID());
3198 return *HFI;
Douglas Gregor09b69892011-02-10 17:09:37 +00003199 }
3200
3201 return HeaderFileInfo();
3202}
3203
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00003204void ASTReader::ReadPragmaDiagnosticMappings(Diagnostic &Diag) {
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00003205 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
3206 Module &F = *(*I);
Douglas Gregor925296b2011-07-19 16:10:42 +00003207 unsigned Idx = 0;
3208 while (Idx < F.PragmaDiagMappings.size()) {
3209 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
3210 while (1) {
3211 assert(Idx < F.PragmaDiagMappings.size() &&
3212 "Invalid data, didn't find '-1' marking end of diag/map pairs");
3213 if (Idx >= F.PragmaDiagMappings.size()) {
3214 break; // Something is messed up but at least avoid infinite loop in
3215 // release build.
3216 }
3217 unsigned DiagID = F.PragmaDiagMappings[Idx++];
3218 if (DiagID == (unsigned)-1) {
3219 break; // no more diag/map pairs for this location.
3220 }
3221 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++];
3222 Diag.setDiagnosticMapping(DiagID, Map, Loc);
3223 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00003224 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00003225 }
3226}
3227
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003228/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redl2c499f62010-08-18 23:56:43 +00003229ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Douglas Gregor5204bde2011-08-02 16:26:37 +00003230 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
Jonathan D. Turner35005682011-07-20 21:31:32 +00003231 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
Douglas Gregor8ab4ea82011-07-29 00:21:44 +00003232 Module *M = I->second;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003233 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003234}
3235
3236/// \brief Read and return the type with the given index..
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003237///
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003238/// The index is the type ID, shifted and minus the number of predefs. This
3239/// routine actually reads the record corresponding to the type at the given
3240/// location. It is a helper routine for GetType, which deals with reading type
3241/// IDs.
Douglas Gregor903b7e92011-07-22 00:38:23 +00003242QualType ASTReader::readTypeRecord(unsigned Index) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003243 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003244 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00003245
Douglas Gregorfeb84b02009-04-14 21:18:50 +00003246 // Keep track of where we are in the stream, then jump back there
3247 // after reading this type.
Douglas Gregor12bfa382009-10-17 00:13:19 +00003248 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00003249
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003250 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redleaa4ade2010-08-11 18:52:41 +00003251
Douglas Gregor1342e842009-07-06 18:54:52 +00003252 // Note that we are loading a type record.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00003253 Deserializing AType(this);
Mike Stump11289f42009-09-09 15:08:12 +00003254
Douglas Gregor903b7e92011-07-22 00:38:23 +00003255 unsigned Idx = 0;
Sebastian Redl2c373b92010-10-05 15:59:54 +00003256 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003257 RecordData Record;
Douglas Gregor12bfa382009-10-17 00:13:19 +00003258 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl539c5062010-08-18 23:57:32 +00003259 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
3260 case TYPE_EXT_QUAL: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003261 if (Record.size() != 2) {
3262 Error("Incorrect encoding of extended qualifier type");
3263 return QualType();
3264 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003265 QualType Base = readType(*Loc.F, Record, Idx);
3266 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
John McCall8ccfcb52009-09-24 19:53:00 +00003267 return Context->getQualifiedType(Base, Quals);
Douglas Gregor455b8f42009-04-15 22:00:08 +00003268 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003269
Sebastian Redl539c5062010-08-18 23:57:32 +00003270 case TYPE_COMPLEX: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003271 if (Record.size() != 1) {
3272 Error("Incorrect encoding of complex type");
3273 return QualType();
3274 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003275 QualType ElemType = readType(*Loc.F, Record, Idx);
Chris Lattner8575daa2009-04-27 21:45:14 +00003276 return Context->getComplexType(ElemType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003277 }
3278
Sebastian Redl539c5062010-08-18 23:57:32 +00003279 case TYPE_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003280 if (Record.size() != 1) {
3281 Error("Incorrect encoding of pointer type");
3282 return QualType();
3283 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003284 QualType PointeeType = readType(*Loc.F, Record, Idx);
Chris Lattner8575daa2009-04-27 21:45:14 +00003285 return Context->getPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003286 }
3287
Sebastian Redl539c5062010-08-18 23:57:32 +00003288 case TYPE_BLOCK_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003289 if (Record.size() != 1) {
3290 Error("Incorrect encoding of block pointer type");
3291 return QualType();
3292 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003293 QualType PointeeType = readType(*Loc.F, Record, Idx);
Chris Lattner8575daa2009-04-27 21:45:14 +00003294 return Context->getBlockPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003295 }
3296
Sebastian Redl539c5062010-08-18 23:57:32 +00003297 case TYPE_LVALUE_REFERENCE: {
Richard Smith0f538462011-04-12 10:38:03 +00003298 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003299 Error("Incorrect encoding of lvalue reference type");
3300 return QualType();
3301 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003302 QualType PointeeType = readType(*Loc.F, Record, Idx);
Richard Smith0f538462011-04-12 10:38:03 +00003303 return Context->getLValueReferenceType(PointeeType, Record[1]);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003304 }
3305
Sebastian Redl539c5062010-08-18 23:57:32 +00003306 case TYPE_RVALUE_REFERENCE: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003307 if (Record.size() != 1) {
3308 Error("Incorrect encoding of rvalue reference type");
3309 return QualType();
3310 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003311 QualType PointeeType = readType(*Loc.F, Record, Idx);
Chris Lattner8575daa2009-04-27 21:45:14 +00003312 return Context->getRValueReferenceType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003313 }
3314
Sebastian Redl539c5062010-08-18 23:57:32 +00003315 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidisee776bc2010-07-02 11:55:15 +00003316 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003317 Error("Incorrect encoding of member pointer type");
3318 return QualType();
3319 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003320 QualType PointeeType = readType(*Loc.F, Record, Idx);
3321 QualType ClassType = readType(*Loc.F, Record, Idx);
Douglas Gregor0cdc8322010-12-10 17:03:06 +00003322 if (PointeeType.isNull() || ClassType.isNull())
3323 return QualType();
3324
Chris Lattner8575daa2009-04-27 21:45:14 +00003325 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003326 }
3327
Sebastian Redl539c5062010-08-18 23:57:32 +00003328 case TYPE_CONSTANT_ARRAY: {
Douglas Gregor903b7e92011-07-22 00:38:23 +00003329 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003330 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3331 unsigned IndexTypeQuals = Record[2];
3332 unsigned Idx = 3;
3333 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor04318252009-07-06 15:59:29 +00003334 return Context->getConstantArrayType(ElementType, Size,
3335 ASM, IndexTypeQuals);
3336 }
3337
Sebastian Redl539c5062010-08-18 23:57:32 +00003338 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregor903b7e92011-07-22 00:38:23 +00003339 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003340 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3341 unsigned IndexTypeQuals = Record[2];
Chris Lattner8575daa2009-04-27 21:45:14 +00003342 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003343 }
3344
Sebastian Redl539c5062010-08-18 23:57:32 +00003345 case TYPE_VARIABLE_ARRAY: {
Douglas Gregor903b7e92011-07-22 00:38:23 +00003346 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00003347 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3348 unsigned IndexTypeQuals = Record[2];
Sebastian Redl2c373b92010-10-05 15:59:54 +00003349 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
3350 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
3351 return Context->getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor04318252009-07-06 15:59:29 +00003352 ASM, IndexTypeQuals,
3353 SourceRange(LBLoc, RBLoc));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003354 }
3355
Sebastian Redl539c5062010-08-18 23:57:32 +00003356 case TYPE_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00003357 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003358 Error("incorrect encoding of vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003359 return QualType();
3360 }
3361
Douglas Gregor903b7e92011-07-22 00:38:23 +00003362 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003363 unsigned NumElements = Record[1];
Bob Wilsonaeb56442010-11-10 21:56:12 +00003364 unsigned VecKind = Record[2];
Chris Lattner37141f42010-06-23 06:00:24 +00003365 return Context->getVectorType(ElementType, NumElements,
Bob Wilsonaeb56442010-11-10 21:56:12 +00003366 (VectorType::VectorKind)VecKind);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003367 }
3368
Sebastian Redl539c5062010-08-18 23:57:32 +00003369 case TYPE_EXT_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00003370 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003371 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003372 return QualType();
3373 }
3374
Douglas Gregor903b7e92011-07-22 00:38:23 +00003375 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003376 unsigned NumElements = Record[1];
Chris Lattner8575daa2009-04-27 21:45:14 +00003377 return Context->getExtVectorType(ElementType, NumElements);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003378 }
3379
Sebastian Redl539c5062010-08-18 23:57:32 +00003380 case TYPE_FUNCTION_NO_PROTO: {
John McCall31168b02011-06-15 23:02:42 +00003381 if (Record.size() != 6) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00003382 Error("incorrect encoding of no-proto function type");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003383 return QualType();
3384 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003385 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCall31168b02011-06-15 23:02:42 +00003386 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
3387 (CallingConv)Record[4], Record[5]);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00003388 return Context->getFunctionNoProtoType(ResultType, Info);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003389 }
3390
Sebastian Redl539c5062010-08-18 23:57:32 +00003391 case TYPE_FUNCTION_PROTO: {
Douglas Gregor903b7e92011-07-22 00:38:23 +00003392 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCalldb40c7f2010-12-14 08:05:40 +00003393
3394 FunctionProtoType::ExtProtoInfo EPI;
3395 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
Eli Friedmanc5b20b52011-04-09 08:18:08 +00003396 /*hasregparm*/ Record[2],
3397 /*regparm*/ Record[3],
John McCall31168b02011-06-15 23:02:42 +00003398 static_cast<CallingConv>(Record[4]),
3399 /*produces*/ Record[5]);
John McCalldb40c7f2010-12-14 08:05:40 +00003400
John McCall31168b02011-06-15 23:02:42 +00003401 unsigned Idx = 6;
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003402 unsigned NumParams = Record[Idx++];
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003403 SmallVector<QualType, 16> ParamTypes;
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003404 for (unsigned I = 0; I != NumParams; ++I)
Douglas Gregor903b7e92011-07-22 00:38:23 +00003405 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
John McCalldb40c7f2010-12-14 08:05:40 +00003406
3407 EPI.Variadic = Record[Idx++];
3408 EPI.TypeQuals = Record[Idx++];
Douglas Gregordb9d6642011-01-26 05:01:58 +00003409 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003410 ExceptionSpecificationType EST =
3411 static_cast<ExceptionSpecificationType>(Record[Idx++]);
3412 EPI.ExceptionSpecType = EST;
3413 if (EST == EST_Dynamic) {
3414 EPI.NumExceptions = Record[Idx++];
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003415 SmallVector<QualType, 2> Exceptions;
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003416 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
Douglas Gregor903b7e92011-07-22 00:38:23 +00003417 Exceptions.push_back(readType(*Loc.F, Record, Idx));
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003418 EPI.Exceptions = Exceptions.data();
3419 } else if (EST == EST_ComputedNoexcept) {
3420 EPI.NoexceptExpr = ReadExpr(*Loc.F);
3421 }
Jay Foad7d0479f2009-05-21 09:52:38 +00003422 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
John McCalldb40c7f2010-12-14 08:05:40 +00003423 EPI);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003424 }
3425
Douglas Gregor7fb09192011-07-21 22:35:25 +00003426 case TYPE_UNRESOLVED_USING: {
3427 unsigned Idx = 0;
John McCallb96ec562009-12-04 22:46:56 +00003428 return Context->getTypeDeclType(
Douglas Gregor7fb09192011-07-21 22:35:25 +00003429 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
3430 }
3431
Sebastian Redl539c5062010-08-18 23:57:32 +00003432 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003433 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003434 Error("incorrect encoding of typedef type");
3435 return QualType();
3436 }
Douglas Gregor7fb09192011-07-21 22:35:25 +00003437 unsigned Idx = 0;
3438 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
Douglas Gregor903b7e92011-07-22 00:38:23 +00003439 QualType Canonical = readType(*Loc.F, Record, Idx);
Douglas Gregorf86c9392010-10-26 00:51:02 +00003440 if (!Canonical.isNull())
3441 Canonical = Context->getCanonicalType(Canonical);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003442 return Context->getTypedefType(Decl, Canonical);
3443 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003444
Sebastian Redl539c5062010-08-18 23:57:32 +00003445 case TYPE_TYPEOF_EXPR:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003446 return Context->getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003447
Sebastian Redl539c5062010-08-18 23:57:32 +00003448 case TYPE_TYPEOF: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003449 if (Record.size() != 1) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003450 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003451 return QualType();
3452 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003453 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Chris Lattner8575daa2009-04-27 21:45:14 +00003454 return Context->getTypeOfType(UnderlyingType);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003455 }
Mike Stump11289f42009-09-09 15:08:12 +00003456
Sebastian Redl539c5062010-08-18 23:57:32 +00003457 case TYPE_DECLTYPE:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003458 return Context->getDecltypeType(ReadExpr(*Loc.F));
Anders Carlsson81df7b82009-06-24 19:06:50 +00003459
Alexis Hunte852b102011-05-24 22:41:36 +00003460 case TYPE_UNARY_TRANSFORM: {
Douglas Gregor903b7e92011-07-22 00:38:23 +00003461 QualType BaseType = readType(*Loc.F, Record, Idx);
3462 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Alexis Hunte852b102011-05-24 22:41:36 +00003463 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
3464 return Context->getUnaryTransformType(BaseType, UnderlyingType, UKind);
3465 }
3466
Richard Smith30482bc2011-02-20 03:19:35 +00003467 case TYPE_AUTO:
Douglas Gregor903b7e92011-07-22 00:38:23 +00003468 return Context->getAutoType(readType(*Loc.F, Record, Idx));
Richard Smith30482bc2011-02-20 03:19:35 +00003469
Sebastian Redl539c5062010-08-18 23:57:32 +00003470 case TYPE_RECORD: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003471 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003472 Error("incorrect encoding of record type");
3473 return QualType();
3474 }
Douglas Gregor7fb09192011-07-21 22:35:25 +00003475 unsigned Idx = 0;
3476 bool IsDependent = Record[Idx++];
3477 QualType T
3478 = Context->getRecordType(ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx));
John McCall424cec92011-01-19 06:33:43 +00003479 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003480 return T;
3481 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003482
Sebastian Redl539c5062010-08-18 23:57:32 +00003483 case TYPE_ENUM: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003484 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003485 Error("incorrect encoding of enum type");
3486 return QualType();
3487 }
Douglas Gregor7fb09192011-07-21 22:35:25 +00003488 unsigned Idx = 0;
3489 bool IsDependent = Record[Idx++];
3490 QualType T
3491 = Context->getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
John McCall424cec92011-01-19 06:33:43 +00003492 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003493 return T;
3494 }
Douglas Gregor1daeb692009-04-13 18:14:40 +00003495
John McCall81904512011-01-06 01:58:22 +00003496 case TYPE_ATTRIBUTED: {
3497 if (Record.size() != 3) {
3498 Error("incorrect encoding of attributed type");
3499 return QualType();
3500 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003501 QualType modifiedType = readType(*Loc.F, Record, Idx);
3502 QualType equivalentType = readType(*Loc.F, Record, Idx);
John McCall81904512011-01-06 01:58:22 +00003503 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
3504 return Context->getAttributedType(kind, modifiedType, equivalentType);
3505 }
3506
Abramo Bagnara924a8f32010-12-10 16:29:40 +00003507 case TYPE_PAREN: {
3508 if (Record.size() != 1) {
3509 Error("incorrect encoding of paren type");
3510 return QualType();
3511 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003512 QualType InnerType = readType(*Loc.F, Record, Idx);
Abramo Bagnara924a8f32010-12-10 16:29:40 +00003513 return Context->getParenType(InnerType);
3514 }
3515
Douglas Gregord2fa7662010-12-20 02:24:11 +00003516 case TYPE_PACK_EXPANSION: {
Douglas Gregor17328502011-02-01 15:24:58 +00003517 if (Record.size() != 2) {
Douglas Gregord2fa7662010-12-20 02:24:11 +00003518 Error("incorrect encoding of pack expansion type");
3519 return QualType();
3520 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003521 QualType Pattern = readType(*Loc.F, Record, Idx);
Douglas Gregord2fa7662010-12-20 02:24:11 +00003522 if (Pattern.isNull())
3523 return QualType();
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00003524 llvm::Optional<unsigned> NumExpansions;
3525 if (Record[1])
3526 NumExpansions = Record[1] - 1;
3527 return Context->getPackExpansionType(Pattern, NumExpansions);
Douglas Gregord2fa7662010-12-20 02:24:11 +00003528 }
3529
Sebastian Redl539c5062010-08-18 23:57:32 +00003530 case TYPE_ELABORATED: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003531 unsigned Idx = 0;
3532 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor7fb09192011-07-21 22:35:25 +00003533 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor903b7e92011-07-22 00:38:23 +00003534 QualType NamedType = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003535 return Context->getElaboratedType(Keyword, NNS, NamedType);
John McCallfcc33b02009-09-05 00:15:47 +00003536 }
3537
Sebastian Redl539c5062010-08-18 23:57:32 +00003538 case TYPE_OBJC_INTERFACE: {
Chris Lattner587cbe12009-04-22 06:45:28 +00003539 unsigned Idx = 0;
Douglas Gregor7fb09192011-07-21 22:35:25 +00003540 ObjCInterfaceDecl *ItfD
3541 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
John McCall8b07ec22010-05-15 11:32:37 +00003542 return Context->getObjCInterfaceType(ItfD);
3543 }
3544
Sebastian Redl539c5062010-08-18 23:57:32 +00003545 case TYPE_OBJC_OBJECT: {
John McCall8b07ec22010-05-15 11:32:37 +00003546 unsigned Idx = 0;
Douglas Gregor903b7e92011-07-22 00:38:23 +00003547 QualType Base = readType(*Loc.F, Record, Idx);
Chris Lattner587cbe12009-04-22 06:45:28 +00003548 unsigned NumProtos = Record[Idx++];
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003549 SmallVector<ObjCProtocolDecl*, 4> Protos;
Chris Lattner587cbe12009-04-22 06:45:28 +00003550 for (unsigned I = 0; I != NumProtos; ++I)
Douglas Gregor7fb09192011-07-21 22:35:25 +00003551 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003552 return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattner587cbe12009-04-22 06:45:28 +00003553 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003554
Sebastian Redl539c5062010-08-18 23:57:32 +00003555 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattner6e054af2009-04-22 06:40:03 +00003556 unsigned Idx = 0;
Douglas Gregor903b7e92011-07-22 00:38:23 +00003557 QualType Pointee = readType(*Loc.F, Record, Idx);
John McCall8b07ec22010-05-15 11:32:37 +00003558 return Context->getObjCObjectPointerType(Pointee);
Chris Lattner6e054af2009-04-22 06:40:03 +00003559 }
Argyrios Kyrtzidisa7a36df2009-09-29 19:42:55 +00003560
Sebastian Redl539c5062010-08-18 23:57:32 +00003561 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCallcebee162009-10-18 09:09:24 +00003562 unsigned Idx = 0;
Douglas Gregor903b7e92011-07-22 00:38:23 +00003563 QualType Parm = readType(*Loc.F, Record, Idx);
3564 QualType Replacement = readType(*Loc.F, Record, Idx);
John McCallcebee162009-10-18 09:09:24 +00003565 return
3566 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
3567 Replacement);
3568 }
John McCalle78aac42010-03-10 03:28:59 +00003569
Douglas Gregorada4b792011-01-14 02:55:32 +00003570 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
3571 unsigned Idx = 0;
Douglas Gregor903b7e92011-07-22 00:38:23 +00003572 QualType Parm = readType(*Loc.F, Record, Idx);
Douglas Gregorada4b792011-01-14 02:55:32 +00003573 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
3574 return Context->getSubstTemplateTypeParmPackType(
3575 cast<TemplateTypeParmType>(Parm),
3576 ArgPack);
3577 }
3578
Sebastian Redl539c5062010-08-18 23:57:32 +00003579 case TYPE_INJECTED_CLASS_NAME: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00003580 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
Douglas Gregor903b7e92011-07-22 00:38:23 +00003581 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00003582 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003583 // for AST reading, too much interdependencies.
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00003584 return
3585 QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCalle78aac42010-03-10 03:28:59 +00003586 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003587
Sebastian Redl539c5062010-08-18 23:57:32 +00003588 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003589 unsigned Idx = 0;
3590 unsigned Depth = Record[Idx++];
3591 unsigned Index = Record[Idx++];
3592 bool Pack = Record[Idx++];
Douglas Gregor7fb09192011-07-21 22:35:25 +00003593 TemplateTypeParmDecl *D
3594 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
Chandler Carruth08836322011-05-01 00:51:33 +00003595 return Context->getTemplateTypeParmType(Depth, Index, Pack, D);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003596 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003597
Sebastian Redl539c5062010-08-18 23:57:32 +00003598 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00003599 unsigned Idx = 0;
3600 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor7fb09192011-07-21 22:35:25 +00003601 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregora3e41532011-07-28 20:55:49 +00003602 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Douglas Gregor903b7e92011-07-22 00:38:23 +00003603 QualType Canon = readType(*Loc.F, Record, Idx);
Douglas Gregorf86c9392010-10-26 00:51:02 +00003604 if (!Canon.isNull())
3605 Canon = Context->getCanonicalType(Canon);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +00003606 return Context->getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00003607 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003608
Sebastian Redl539c5062010-08-18 23:57:32 +00003609 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003610 unsigned Idx = 0;
3611 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor7fb09192011-07-21 22:35:25 +00003612 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregora3e41532011-07-28 20:55:49 +00003613 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003614 unsigned NumArgs = Record[Idx++];
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003615 SmallVector<TemplateArgument, 8> Args;
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003616 Args.reserve(NumArgs);
3617 while (NumArgs--)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003618 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003619 return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
3620 Args.size(), Args.data());
3621 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003622
Sebastian Redl539c5062010-08-18 23:57:32 +00003623 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00003624 unsigned Idx = 0;
3625
3626 // ArrayType
Douglas Gregor903b7e92011-07-22 00:38:23 +00003627 QualType ElementType = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00003628 ArrayType::ArraySizeModifier ASM
3629 = (ArrayType::ArraySizeModifier)Record[Idx++];
3630 unsigned IndexTypeQuals = Record[Idx++];
3631
3632 // DependentSizedArrayType
Sebastian Redl2c373b92010-10-05 15:59:54 +00003633 Expr *NumElts = ReadExpr(*Loc.F);
3634 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00003635
3636 return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
3637 IndexTypeQuals, Brackets);
3638 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003639
Sebastian Redl539c5062010-08-18 23:57:32 +00003640 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003641 unsigned Idx = 0;
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003642 bool IsDependent = Record[Idx++];
Douglas Gregor5590be02011-01-15 06:45:20 +00003643 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003644 SmallVector<TemplateArgument, 8> Args;
Sebastian Redl2c373b92010-10-05 15:59:54 +00003645 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Douglas Gregor903b7e92011-07-22 00:38:23 +00003646 QualType Underlying = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003647 QualType T;
Richard Smith3f1b5d02011-05-05 21:57:07 +00003648 if (Underlying.isNull())
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003649 T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(),
3650 Args.size());
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003651 else
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003652 T = Context->getTemplateSpecializationType(Name, Args.data(),
Richard Smith3f1b5d02011-05-05 21:57:07 +00003653 Args.size(), Underlying);
John McCall424cec92011-01-19 06:33:43 +00003654 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003655 return T;
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003656 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003657 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003658 // Suppress a GCC warning
3659 return QualType();
3660}
3661
Sebastian Redl2c373b92010-10-05 15:59:54 +00003662class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redl2c499f62010-08-18 23:56:43 +00003663 ASTReader &Reader;
Douglas Gregora6895d82011-07-22 16:00:58 +00003664 Module &F;
Sebastian Redlc67764e2010-07-22 22:43:28 +00003665 llvm::BitstreamCursor &DeclsCursor;
Sebastian Redl2c499f62010-08-18 23:56:43 +00003666 const ASTReader::RecordData &Record;
John McCall8f115c62009-10-16 21:56:05 +00003667 unsigned &Idx;
3668
Sebastian Redl2c373b92010-10-05 15:59:54 +00003669 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
3670 unsigned &I) {
3671 return Reader.ReadSourceLocation(F, R, I);
3672 }
3673
Douglas Gregor7fb09192011-07-21 22:35:25 +00003674 template<typename T>
3675 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
3676 return Reader.ReadDeclAs<T>(F, Record, Idx);
3677 }
3678
John McCall8f115c62009-10-16 21:56:05 +00003679public:
Douglas Gregora6895d82011-07-22 16:00:58 +00003680 TypeLocReader(ASTReader &Reader, Module &F,
Sebastian Redl2c499f62010-08-18 23:56:43 +00003681 const ASTReader::RecordData &Record, unsigned &Idx)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003682 : Reader(Reader), F(F), DeclsCursor(F.DeclsCursor), Record(Record), Idx(Idx)
3683 { }
John McCall8f115c62009-10-16 21:56:05 +00003684
John McCall17001972009-10-18 01:05:36 +00003685 // We want compile-time assurance that we've enumerated all of
3686 // these, so unfortunately we have to declare them first, then
3687 // define them out-of-line.
3688#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +00003689#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +00003690 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +00003691#include "clang/AST/TypeLocNodes.def"
3692
John McCall17001972009-10-18 01:05:36 +00003693 void VisitFunctionTypeLoc(FunctionTypeLoc);
3694 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCall8f115c62009-10-16 21:56:05 +00003695};
3696
John McCall17001972009-10-18 01:05:36 +00003697void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCall8f115c62009-10-16 21:56:05 +00003698 // nothing to do
3699}
John McCall17001972009-10-18 01:05:36 +00003700void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003701 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorc9b7a592010-01-18 18:04:31 +00003702 if (TL.needsExtraLocalData()) {
3703 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
3704 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
3705 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
3706 TL.setModeAttr(Record[Idx++]);
3707 }
John McCall8f115c62009-10-16 21:56:05 +00003708}
John McCall17001972009-10-18 01:05:36 +00003709void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003710 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003711}
John McCall17001972009-10-18 01:05:36 +00003712void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003713 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003714}
John McCall17001972009-10-18 01:05:36 +00003715void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003716 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003717}
John McCall17001972009-10-18 01:05:36 +00003718void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003719 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003720}
John McCall17001972009-10-18 01:05:36 +00003721void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003722 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003723}
John McCall17001972009-10-18 01:05:36 +00003724void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003725 TL.setStarLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnara509357842011-03-05 14:42:21 +00003726 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003727}
John McCall17001972009-10-18 01:05:36 +00003728void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003729 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
3730 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003731 if (Record[Idx++])
Sebastian Redl2c373b92010-10-05 15:59:54 +00003732 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor12bfa382009-10-17 00:13:19 +00003733 else
John McCall17001972009-10-18 01:05:36 +00003734 TL.setSizeExpr(0);
3735}
3736void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
3737 VisitArrayTypeLoc(TL);
3738}
3739void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
3740 VisitArrayTypeLoc(TL);
3741}
3742void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
3743 VisitArrayTypeLoc(TL);
3744}
3745void TypeLocReader::VisitDependentSizedArrayTypeLoc(
3746 DependentSizedArrayTypeLoc TL) {
3747 VisitArrayTypeLoc(TL);
3748}
3749void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
3750 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003751 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003752}
3753void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003754 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003755}
3756void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003757 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003758}
3759void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003760 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
3761 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
Douglas Gregor7fb25412010-10-01 18:44:50 +00003762 TL.setTrailingReturn(Record[Idx++]);
John McCall17001972009-10-18 01:05:36 +00003763 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
Douglas Gregor7fb09192011-07-21 22:35:25 +00003764 TL.setArg(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003765 }
3766}
3767void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
3768 VisitFunctionTypeLoc(TL);
3769}
3770void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
3771 VisitFunctionTypeLoc(TL);
3772}
John McCallb96ec562009-12-04 22:46:56 +00003773void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003774 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallb96ec562009-12-04 22:46:56 +00003775}
John McCall17001972009-10-18 01:05:36 +00003776void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003777 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003778}
3779void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003780 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3781 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3782 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003783}
3784void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003785 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3786 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3787 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3788 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003789}
3790void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003791 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003792}
Alexis Hunte852b102011-05-24 22:41:36 +00003793void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
3794 TL.setKWLoc(ReadSourceLocation(Record, Idx));
3795 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3796 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3797 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
3798}
Richard Smith30482bc2011-02-20 03:19:35 +00003799void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
3800 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3801}
John McCall17001972009-10-18 01:05:36 +00003802void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003803 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003804}
3805void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003806 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003807}
John McCall81904512011-01-06 01:58:22 +00003808void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
3809 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
3810 if (TL.hasAttrOperand()) {
3811 SourceRange range;
3812 range.setBegin(ReadSourceLocation(Record, Idx));
3813 range.setEnd(ReadSourceLocation(Record, Idx));
3814 TL.setAttrOperandParensRange(range);
3815 }
3816 if (TL.hasAttrExprOperand()) {
3817 if (Record[Idx++])
3818 TL.setAttrExprOperand(Reader.ReadExpr(F));
3819 else
3820 TL.setAttrExprOperand(0);
3821 } else if (TL.hasAttrEnumOperand())
3822 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
3823}
John McCall17001972009-10-18 01:05:36 +00003824void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003825 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003826}
John McCallcebee162009-10-18 09:09:24 +00003827void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
3828 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003829 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallcebee162009-10-18 09:09:24 +00003830}
Douglas Gregorada4b792011-01-14 02:55:32 +00003831void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
3832 SubstTemplateTypeParmPackTypeLoc TL) {
3833 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3834}
John McCall17001972009-10-18 01:05:36 +00003835void TypeLocReader::VisitTemplateSpecializationTypeLoc(
3836 TemplateSpecializationTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003837 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
3838 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3839 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall0ad16662009-10-29 08:12:44 +00003840 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
3841 TL.setArgLocInfo(i,
Sebastian Redl2c373b92010-10-05 15:59:54 +00003842 Reader.GetTemplateArgumentLocInfo(F,
3843 TL.getTypePtr()->getArg(i).getKind(),
3844 Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003845}
Abramo Bagnara924a8f32010-12-10 16:29:40 +00003846void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
3847 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3848 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3849}
Abramo Bagnara6150c882010-05-11 21:36:43 +00003850void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003851 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor844cb502011-03-01 18:12:44 +00003852 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003853}
John McCalle78aac42010-03-10 03:28:59 +00003854void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003855 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalle78aac42010-03-10 03:28:59 +00003856}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00003857void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003858 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor3d0da5f2011-03-01 01:34:45 +00003859 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redl2c373b92010-10-05 15:59:54 +00003860 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003861}
John McCallc392f372010-06-11 00:33:02 +00003862void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
3863 DependentTemplateSpecializationTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003864 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregora7a795b2011-03-01 20:11:18 +00003865 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redl2c373b92010-10-05 15:59:54 +00003866 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3867 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3868 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCallc392f372010-06-11 00:33:02 +00003869 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
3870 TL.setArgLocInfo(I,
Sebastian Redl2c373b92010-10-05 15:59:54 +00003871 Reader.GetTemplateArgumentLocInfo(F,
3872 TL.getTypePtr()->getArg(I).getKind(),
3873 Record, Idx));
John McCallc392f372010-06-11 00:33:02 +00003874}
Douglas Gregord2fa7662010-12-20 02:24:11 +00003875void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
3876 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
3877}
John McCall17001972009-10-18 01:05:36 +00003878void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003879 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall8b07ec22010-05-15 11:32:37 +00003880}
3881void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3882 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003883 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3884 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003885 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003886 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003887}
John McCallfc93cf92009-10-22 22:37:11 +00003888void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003889 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCallfc93cf92009-10-22 22:37:11 +00003890}
John McCall8f115c62009-10-16 21:56:05 +00003891
Douglas Gregora6895d82011-07-22 16:00:58 +00003892TypeSourceInfo *ASTReader::GetTypeSourceInfo(Module &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003893 const RecordData &Record,
John McCall8f115c62009-10-16 21:56:05 +00003894 unsigned &Idx) {
Douglas Gregor903b7e92011-07-22 00:38:23 +00003895 QualType InfoTy = readType(F, Record, Idx);
John McCall8f115c62009-10-16 21:56:05 +00003896 if (InfoTy.isNull())
3897 return 0;
3898
John McCallbcd03502009-12-07 02:54:59 +00003899 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003900 TypeLocReader TLR(*this, F, Record, Idx);
John McCallbcd03502009-12-07 02:54:59 +00003901 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCall8f115c62009-10-16 21:56:05 +00003902 TLR.Visit(TL);
John McCallbcd03502009-12-07 02:54:59 +00003903 return TInfo;
John McCall8f115c62009-10-16 21:56:05 +00003904}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003905
Sebastian Redl539c5062010-08-18 23:57:32 +00003906QualType ASTReader::GetType(TypeID ID) {
John McCall8ccfcb52009-09-24 19:53:00 +00003907 unsigned FastQuals = ID & Qualifiers::FastMask;
3908 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003909
Sebastian Redl539c5062010-08-18 23:57:32 +00003910 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003911 QualType T;
Sebastian Redl539c5062010-08-18 23:57:32 +00003912 switch ((PredefinedTypeIDs)Index) {
3913 case PREDEF_TYPE_NULL_ID: return QualType();
3914 case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
3915 case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003916
Sebastian Redl539c5062010-08-18 23:57:32 +00003917 case PREDEF_TYPE_CHAR_U_ID:
3918 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003919 // FIXME: Check that the signedness of CharTy is correct!
Chris Lattner8575daa2009-04-27 21:45:14 +00003920 T = Context->CharTy;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003921 break;
3922
Sebastian Redl539c5062010-08-18 23:57:32 +00003923 case PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break;
3924 case PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break;
3925 case PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break;
3926 case PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break;
3927 case PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break;
3928 case PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break;
3929 case PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break;
3930 case PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break;
3931 case PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break;
3932 case PREDEF_TYPE_INT_ID: T = Context->IntTy; break;
3933 case PREDEF_TYPE_LONG_ID: T = Context->LongTy; break;
3934 case PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break;
3935 case PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break;
3936 case PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break;
3937 case PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break;
3938 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break;
3939 case PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break;
John McCall0009fcc2011-04-26 20:42:42 +00003940 case PREDEF_TYPE_BOUND_MEMBER: T = Context->BoundMemberTy; break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003941 case PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break;
John McCall31996342011-04-07 08:22:57 +00003942 case PREDEF_TYPE_UNKNOWN_ANY: T = Context->UnknownAnyTy; break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003943 case PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break;
3944 case PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break;
3945 case PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break;
3946 case PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break;
3947 case PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break;
3948 case PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break;
Douglas Gregoreda8e122011-08-09 15:13:55 +00003949 case PREDEF_TYPE_AUTO_DEDUCT: T = Context->getAutoDeductType(); break;
3950
3951 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
3952 T = Context->getAutoRRefDeductType();
3953 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003954 }
3955
3956 assert(!T.isNull() && "Unknown predefined type");
John McCall8ccfcb52009-09-24 19:53:00 +00003957 return T.withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003958 }
3959
Sebastian Redl539c5062010-08-18 23:57:32 +00003960 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003961 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl409183f2010-07-14 20:26:45 +00003962 if (TypesLoaded[Index].isNull()) {
Douglas Gregor903b7e92011-07-22 00:38:23 +00003963 TypesLoaded[Index] = readTypeRecord(Index);
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003964 if (TypesLoaded[Index].isNull())
3965 return QualType();
3966
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003967 TypesLoaded[Index]->setFromAST();
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003968 if (DeserializationListener)
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003969 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003970 TypesLoaded[Index]);
Sebastian Redl409183f2010-07-14 20:26:45 +00003971 }
Mike Stump11289f42009-09-09 15:08:12 +00003972
John McCall8ccfcb52009-09-24 19:53:00 +00003973 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003974}
3975
Douglas Gregora6895d82011-07-22 16:00:58 +00003976QualType ASTReader::getLocalType(Module &F, unsigned LocalID) {
Douglas Gregor903b7e92011-07-22 00:38:23 +00003977 return GetType(getGlobalTypeID(F, LocalID));
3978}
3979
3980serialization::TypeID
Douglas Gregora6895d82011-07-22 16:00:58 +00003981ASTReader::getGlobalTypeID(Module &F, unsigned LocalID) const {
Douglas Gregor5204bde2011-08-02 16:26:37 +00003982 unsigned FastQuals = LocalID & Qualifiers::FastMask;
3983 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
3984
3985 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
3986 return LocalID;
3987
3988 ContinuousRangeMap<uint32_t, int, 2>::iterator I
3989 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
3990 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
3991
3992 unsigned GlobalIndex = LocalIndex + I->second;
3993 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
3994}
3995
John McCall0ad16662009-10-29 08:12:44 +00003996TemplateArgumentLocInfo
Douglas Gregora6895d82011-07-22 16:00:58 +00003997ASTReader::GetTemplateArgumentLocInfo(Module &F,
Sebastian Redl2c373b92010-10-05 15:59:54 +00003998 TemplateArgument::ArgKind Kind,
John McCall0ad16662009-10-29 08:12:44 +00003999 const RecordData &Record,
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00004000 unsigned &Index) {
John McCall0ad16662009-10-29 08:12:44 +00004001 switch (Kind) {
4002 case TemplateArgument::Expression:
Sebastian Redl2c373b92010-10-05 15:59:54 +00004003 return ReadExpr(F);
John McCall0ad16662009-10-29 08:12:44 +00004004 case TemplateArgument::Type:
Sebastian Redl2c373b92010-10-05 15:59:54 +00004005 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004006 case TemplateArgument::Template: {
Douglas Gregor9d802122011-03-02 17:09:35 +00004007 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
4008 Index);
Sebastian Redl2c373b92010-10-05 15:59:54 +00004009 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregor9d802122011-03-02 17:09:35 +00004010 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004011 SourceLocation());
4012 }
4013 case TemplateArgument::TemplateExpansion: {
Douglas Gregor9d802122011-03-02 17:09:35 +00004014 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
4015 Index);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004016 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregoreb29d182011-01-05 17:40:24 +00004017 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregor9d802122011-03-02 17:09:35 +00004018 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregoreb29d182011-01-05 17:40:24 +00004019 EllipsisLoc);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004020 }
John McCall0ad16662009-10-29 08:12:44 +00004021 case TemplateArgument::Null:
4022 case TemplateArgument::Integral:
4023 case TemplateArgument::Declaration:
4024 case TemplateArgument::Pack:
4025 return TemplateArgumentLocInfo();
4026 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +00004027 llvm_unreachable("unexpected template argument loc");
John McCall0ad16662009-10-29 08:12:44 +00004028 return TemplateArgumentLocInfo();
4029}
4030
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004031TemplateArgumentLoc
Douglas Gregora6895d82011-07-22 16:00:58 +00004032ASTReader::ReadTemplateArgumentLoc(Module &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00004033 const RecordData &Record, unsigned &Index) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004034 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004035
4036 if (Arg.getKind() == TemplateArgument::Expression) {
4037 if (Record[Index++]) // bool InfoHasSameExpr.
4038 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
4039 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00004040 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00004041 Record, Index));
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004042}
4043
Sebastian Redl2c499f62010-08-18 23:56:43 +00004044Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall75b960e2010-06-01 09:23:16 +00004045 return GetDecl(ID);
4046}
4047
Douglas Gregorc27b2872011-08-04 00:01:48 +00004048uint64_t ASTReader::readCXXBaseSpecifiers(Module &M, const RecordData &Record,
4049 unsigned &Idx){
4050 if (Idx >= Record.size())
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004051 return 0;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004052
Douglas Gregorc27b2872011-08-04 00:01:48 +00004053 unsigned LocalID = Record[Idx++];
4054 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004055}
4056
4057CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
Douglas Gregord32f0352011-07-22 06:10:01 +00004058 RecordLocation Loc = getLocalBitOffset(Offset);
4059 llvm::BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004060 SavedStreamPosition SavedPosition(Cursor);
Douglas Gregord32f0352011-07-22 06:10:01 +00004061 Cursor.JumpToBit(Loc.Offset);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004062 ReadingKindTracker ReadingKind(Read_Decl, *this);
4063 RecordData Record;
4064 unsigned Code = Cursor.ReadCode();
4065 unsigned RecCode = Cursor.ReadRecord(Code, Record);
4066 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
4067 Error("Malformed AST file: missing C++ base specifiers");
4068 return 0;
4069 }
4070
4071 unsigned Idx = 0;
4072 unsigned NumBases = Record[Idx++];
4073 void *Mem = Context->Allocate(sizeof(CXXBaseSpecifier) * NumBases);
4074 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
4075 for (unsigned I = 0; I != NumBases; ++I)
Douglas Gregord32f0352011-07-22 06:10:01 +00004076 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004077 return Bases;
4078}
4079
Douglas Gregor7fb09192011-07-21 22:35:25 +00004080serialization::DeclID
Douglas Gregora6895d82011-07-22 16:00:58 +00004081ASTReader::getGlobalDeclID(Module &F, unsigned LocalID) const {
Douglas Gregor6f8912e2011-08-03 16:05:40 +00004082 if (LocalID < NUM_PREDEF_DECL_IDS)
Douglas Gregorf7180622011-08-03 15:48:04 +00004083 return LocalID;
4084
4085 ContinuousRangeMap<uint32_t, int, 2>::iterator I
Douglas Gregor6f8912e2011-08-03 16:05:40 +00004086 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
Douglas Gregorf7180622011-08-03 15:48:04 +00004087 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
4088
4089 return LocalID + I->second;
Douglas Gregor7fb09192011-07-21 22:35:25 +00004090}
4091
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004092bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
4093 Module &M) const {
4094 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
4095 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
4096 return &M == I->second;
4097}
4098
Sebastian Redl539c5062010-08-18 23:57:32 +00004099Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregor6f8912e2011-08-03 16:05:40 +00004100 if (ID < NUM_PREDEF_DECL_IDS) {
4101 switch ((PredefinedDeclIDs)ID) {
Douglas Gregordab42432011-08-12 00:15:20 +00004102 case PREDEF_DECL_NULL_ID:
Douglas Gregor6f8912e2011-08-03 16:05:40 +00004103 return 0;
Douglas Gregordab42432011-08-12 00:15:20 +00004104
4105 case PREDEF_DECL_TRANSLATION_UNIT_ID:
4106 assert(Context && "No context available?");
4107 return Context->getTranslationUnitDecl();
Douglas Gregor3ea72692011-08-12 05:46:01 +00004108
4109 case PREDEF_DECL_OBJC_ID_ID:
4110 assert(Context && "No context available?");
4111 return Context->getObjCIdDecl();
Douglas Gregor0a586182011-08-12 05:59:41 +00004112
Douglas Gregor52e02802011-08-12 06:17:30 +00004113 case PREDEF_DECL_OBJC_SEL_ID:
4114 assert(Context && "No context available?");
4115 return Context->getObjCSelDecl();
4116
Douglas Gregor0a586182011-08-12 05:59:41 +00004117 case PREDEF_DECL_OBJC_CLASS_ID:
4118 assert(Context && "No context available?");
4119 return Context->getObjCClassDecl();
Douglas Gregor801c99d2011-08-12 06:49:56 +00004120
4121 case PREDEF_DECL_INT_128_ID:
4122 assert(Context && "No context available?");
4123 return Context->getInt128Decl();
4124
4125 case PREDEF_DECL_UNSIGNED_INT_128_ID:
4126 assert(Context && "No context available?");
4127 return Context->getUInt128Decl();
Douglas Gregor6f8912e2011-08-03 16:05:40 +00004128 }
4129
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004130 return 0;
Douglas Gregor6f8912e2011-08-03 16:05:40 +00004131 }
4132
Douglas Gregordab42432011-08-12 00:15:20 +00004133 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
4134
4135 if (Index > DeclsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004136 Error("declaration ID out-of-range for AST file");
Douglas Gregor745ed142009-04-25 18:35:21 +00004137 return 0;
4138 }
Douglas Gregordab42432011-08-12 00:15:20 +00004139
4140if (!DeclsLoaded[Index]) {
Douglas Gregorf7180622011-08-03 15:48:04 +00004141 ReadDeclRecord(ID);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00004142 if (DeserializationListener)
4143 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
4144 }
Douglas Gregor745ed142009-04-25 18:35:21 +00004145
4146 return DeclsLoaded[Index];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004147}
4148
Douglas Gregora6895d82011-07-22 16:00:58 +00004149serialization::DeclID ASTReader::ReadDeclID(Module &F,
Douglas Gregor7fb09192011-07-21 22:35:25 +00004150 const RecordData &Record,
4151 unsigned &Idx) {
4152 if (Idx >= Record.size()) {
4153 Error("Corrupted AST file");
4154 return 0;
4155 }
4156
4157 return getGlobalDeclID(F, Record[Idx++]);
4158}
4159
Chris Lattner9c28af02009-04-27 05:46:25 +00004160/// \brief Resolve the offset of a statement into a statement.
4161///
4162/// This operation will read a new statement from the external
4163/// source each time it is called, and is meant to be used via a
4164/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redl2c499f62010-08-18 23:56:43 +00004165Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidisd9f526f2010-10-28 09:29:32 +00004166 // Switch case IDs are per Decl.
4167 ClearSwitchCaseIDs();
4168
Sebastian Redl5c415f32010-07-22 17:01:13 +00004169 // Offset here is a global offset across the entire chain.
Douglas Gregord32f0352011-07-22 06:10:01 +00004170 RecordLocation Loc = getLocalBitOffset(Offset);
4171 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
4172 return ReadStmtFromStream(*Loc.F);
Douglas Gregor3c3aa612009-04-18 00:07:54 +00004173}
4174
Douglas Gregor1257f972011-08-24 21:27:34 +00004175namespace {
4176 class FindExternalLexicalDeclsVisitor {
4177 ASTReader &Reader;
4178 const DeclContext *DC;
4179 bool (*isKindWeWant)(Decl::Kind);
Douglas Gregor4e4c83e2011-08-26 22:04:51 +00004180
Douglas Gregor1257f972011-08-24 21:27:34 +00004181 SmallVectorImpl<Decl*> &Decls;
4182 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
4183
4184 public:
4185 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
4186 bool (*isKindWeWant)(Decl::Kind),
4187 SmallVectorImpl<Decl*> &Decls)
4188 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
4189 {
4190 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
4191 PredefsVisited[I] = false;
4192 }
4193
4194 static bool visit(Module &M, bool Preorder, void *UserData) {
4195 if (Preorder)
4196 return false;
4197
4198 FindExternalLexicalDeclsVisitor *This
4199 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
4200
4201 Module::DeclContextInfosMap::iterator Info
4202 = M.DeclContextInfos.find(This->DC);
4203 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
4204 return false;
4205
4206 // Load all of the declaration IDs
4207 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
4208 *IDE = ID + Info->second.NumLexicalDecls;
4209 ID != IDE; ++ID) {
4210 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
4211 continue;
4212
4213 // Don't add predefined declarations to the lexical context more
4214 // than once.
4215 if (ID->second < NUM_PREDEF_DECL_IDS) {
4216 if (This->PredefsVisited[ID->second])
4217 continue;
4218
4219 This->PredefsVisited[ID->second] = true;
4220 }
4221
Douglas Gregor4e4c83e2011-08-26 22:04:51 +00004222 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
4223 if (!This->DC->isDeclInLexicalTraversal(D))
4224 This->Decls.push_back(D);
4225 }
Douglas Gregor1257f972011-08-24 21:27:34 +00004226 }
4227
4228 return false;
4229 }
4230 };
4231}
4232
Douglas Gregor3d0adb32011-07-15 21:46:17 +00004233ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00004234 bool (*isKindWeWant)(Decl::Kind),
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004235 SmallVectorImpl<Decl*> &Decls) {
Douglas Gregor94619c82011-08-24 19:03:07 +00004236 // There might be lexical decls in multiple modules, for the TU at
Douglas Gregor1257f972011-08-24 21:27:34 +00004237 // least. Walk all of the modules in the order they were loaded.
4238 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
4239 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00004240 ++NumLexicalDeclContextsRead;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00004241 return ELR_Success;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004242}
4243
Douglas Gregor94619c82011-08-24 19:03:07 +00004244namespace {
4245 /// \brief Module visitor used to perform name lookup into a
4246 /// declaration context.
4247 class DeclContextNameLookupVisitor {
4248 ASTReader &Reader;
4249 const DeclContext *DC;
4250 DeclarationName Name;
4251 SmallVectorImpl<NamedDecl *> &Decls;
4252
4253 public:
4254 DeclContextNameLookupVisitor(ASTReader &Reader,
4255 const DeclContext *DC, DeclarationName Name,
4256 SmallVectorImpl<NamedDecl *> &Decls)
4257 : Reader(Reader), DC(DC), Name(Name), Decls(Decls) { }
4258
4259 static bool visit(Module &M, void *UserData) {
4260 DeclContextNameLookupVisitor *This
4261 = static_cast<DeclContextNameLookupVisitor *>(UserData);
4262
4263 // Check whether we have any visible declaration information for
4264 // this context in this module.
4265 Module::DeclContextInfosMap::iterator Info
4266 = M.DeclContextInfos.find(This->DC);
4267 if (Info == M.DeclContextInfos.end() || !Info->second.NameLookupTableData)
4268 return false;
4269
4270 // Look for this name within this module.
4271 ASTDeclContextNameLookupTable *LookupTable =
4272 (ASTDeclContextNameLookupTable*)Info->second.NameLookupTableData;
4273 ASTDeclContextNameLookupTable::iterator Pos
4274 = LookupTable->find(This->Name);
4275 if (Pos == LookupTable->end())
4276 return false;
4277
4278 bool FoundAnything = false;
4279 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
4280 for (; Data.first != Data.second; ++Data.first) {
4281 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
4282 if (!ND)
4283 continue;
4284
4285 if (ND->getDeclName() != This->Name) {
4286 assert(!This->Name.getCXXNameType().isNull() &&
4287 "Name mismatch without a type");
4288 continue;
4289 }
4290
4291 // Record this declaration.
4292 FoundAnything = true;
4293 This->Decls.push_back(ND);
4294 }
4295
4296 return FoundAnything;
4297 }
4298 };
4299}
4300
John McCall75b960e2010-06-01 09:23:16 +00004301DeclContext::lookup_result
Sebastian Redl2c499f62010-08-18 23:56:43 +00004302ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall75b960e2010-06-01 09:23:16 +00004303 DeclarationName Name) {
Mike Stump11289f42009-09-09 15:08:12 +00004304 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004305 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004306 if (!Name)
4307 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
4308 DeclContext::lookup_iterator(0));
Ted Kremenek1ff615c2010-03-18 00:56:54 +00004309
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004310 SmallVector<NamedDecl *, 64> Decls;
Douglas Gregor94619c82011-08-24 19:03:07 +00004311 DeclContextNameLookupVisitor Visitor(*this, DC, Name, Decls);
4312 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00004313 ++NumVisibleDeclContextsRead;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004314 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall75b960e2010-06-01 09:23:16 +00004315 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004316}
4317
Sebastian Redl2c499f62010-08-18 23:56:43 +00004318void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004319 assert(Consumer);
4320 while (!InterestingDecls.empty()) {
4321 DeclGroupRef DG(InterestingDecls.front());
4322 InterestingDecls.pop_front();
Sebastian Redleaa4ade2010-08-11 18:52:41 +00004323 Consumer->HandleInterestingDecl(DG);
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004324 }
4325}
4326
Sebastian Redl2c499f62010-08-18 23:56:43 +00004327void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregorb985eeb2009-04-22 19:09:20 +00004328 this->Consumer = Consumer;
4329
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00004330 if (!Consumer)
4331 return;
4332
4333 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004334 // Force deserialization of this decl, which will cause it to be queued for
4335 // passing to the consumer.
Daniel Dunbar865c2a72009-09-17 03:06:44 +00004336 GetDecl(ExternalDefinitions[I]);
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00004337 }
Douglas Gregorf005eac2009-04-25 00:41:30 +00004338
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004339 PassInterestingDeclsToConsumer();
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00004340}
4341
Sebastian Redl2c499f62010-08-18 23:56:43 +00004342void ASTReader::PrintStats() {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004343 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004344
Mike Stump11289f42009-09-09 15:08:12 +00004345 unsigned NumTypesLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00004346 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall8ccfcb52009-09-24 19:53:00 +00004347 QualType());
Douglas Gregor0e149972009-04-25 19:10:14 +00004348 unsigned NumDeclsLoaded
4349 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
4350 (Decl *)0);
4351 unsigned NumIdentifiersLoaded
4352 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
4353 IdentifiersLoaded.end(),
4354 (IdentifierInfo *)0);
Mike Stump11289f42009-09-09 15:08:12 +00004355 unsigned NumSelectorsLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00004356 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
4357 SelectorsLoaded.end(),
4358 Selector());
Douglas Gregorc3b1dd12009-04-13 20:50:16 +00004359
Douglas Gregorc5046832009-04-27 18:38:38 +00004360 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
4361 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor49bf76b2011-07-21 18:46:38 +00004362 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
Douglas Gregor258ae542009-04-27 06:38:32 +00004363 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
4364 NumSLocEntriesRead, TotalNumSLocEntries,
4365 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor745ed142009-04-25 18:35:21 +00004366 if (!TypesLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00004367 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00004368 NumTypesLoaded, (unsigned)TypesLoaded.size(),
4369 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
4370 if (!DeclsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00004371 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00004372 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
4373 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor0e149972009-04-25 19:10:14 +00004374 if (!IdentifiersLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00004375 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor0e149972009-04-25 19:10:14 +00004376 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
4377 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redlada023c2010-08-04 20:40:17 +00004378 if (!SelectorsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00004379 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redlada023c2010-08-04 20:40:17 +00004380 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
4381 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor95c13f52009-04-25 17:48:32 +00004382 if (TotalNumStatements)
4383 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
4384 NumStatementsRead, TotalNumStatements,
4385 ((float)NumStatementsRead/TotalNumStatements * 100));
4386 if (TotalNumMacros)
4387 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
4388 NumMacrosRead, TotalNumMacros,
4389 ((float)NumMacrosRead/TotalNumMacros * 100));
4390 if (TotalLexicalDeclContexts)
4391 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
4392 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
4393 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
4394 * 100));
4395 if (TotalVisibleDeclContexts)
4396 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
4397 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
4398 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
4399 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004400 if (TotalNumMethodPoolEntries) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004401 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004402 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
4403 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor95c13f52009-04-25 17:48:32 +00004404 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004405 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor95c13f52009-04-25 17:48:32 +00004406 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004407 std::fprintf(stderr, "\n");
Douglas Gregor204b8712011-07-21 19:50:14 +00004408 dump();
4409 std::fprintf(stderr, "\n");
4410}
4411
Douglas Gregora6895d82011-07-22 16:00:58 +00004412template<typename Key, typename Module, unsigned InitialCapacity>
Douglas Gregor204b8712011-07-21 19:50:14 +00004413static void
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004414dumpModuleIDMap(StringRef Name,
Douglas Gregora6895d82011-07-22 16:00:58 +00004415 const ContinuousRangeMap<Key, Module *,
Douglas Gregor204b8712011-07-21 19:50:14 +00004416 InitialCapacity> &Map) {
4417 if (Map.begin() == Map.end())
4418 return;
4419
Douglas Gregora6895d82011-07-22 16:00:58 +00004420 typedef ContinuousRangeMap<Key, Module *, InitialCapacity> MapType;
Douglas Gregor204b8712011-07-21 19:50:14 +00004421 llvm::errs() << Name << ":\n";
4422 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
4423 I != IEnd; ++I) {
4424 llvm::errs() << " " << I->first << " -> " << I->second->FileName
4425 << "\n";
4426 }
4427}
4428
Douglas Gregor204b8712011-07-21 19:50:14 +00004429void ASTReader::dump() {
Douglas Gregor1cc9c062011-08-02 11:12:41 +00004430 llvm::errs() << "*** PCH/Module Remappings:\n";
Douglas Gregord32f0352011-07-22 06:10:01 +00004431 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
Douglas Gregor204b8712011-07-21 19:50:14 +00004432 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
Douglas Gregor8ab4ea82011-07-29 00:21:44 +00004433 dumpModuleIDMap("Global type map", GlobalTypeMap);
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00004434 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00004435 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
4436 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
4437 dumpModuleIDMap("Global macro definition map", GlobalMacroDefinitionMap);
4438 dumpModuleIDMap("Global preprocessed entity map",
4439 GlobalPreprocessedEntityMap);
Douglas Gregor1cc9c062011-08-02 11:12:41 +00004440
4441 llvm::errs() << "\n*** PCH/Modules Loaded:";
4442 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
4443 MEnd = ModuleMgr.end();
4444 M != MEnd; ++M)
4445 (*M)->dump();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004446}
4447
Ted Kremenek5e1ed7b2011-04-28 23:46:20 +00004448/// Return the amount of memory used by memory buffers, breaking down
4449/// by heap-backed versus mmap'ed memory.
4450void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00004451 for (ModuleConstIterator I = ModuleMgr.begin(),
4452 E = ModuleMgr.end(); I != E; ++I) {
4453 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
Ted Kremenek5e1ed7b2011-04-28 23:46:20 +00004454 size_t bytes = buf->getBufferSize();
4455 switch (buf->getBufferKind()) {
4456 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
4457 sizes.malloc_bytes += bytes;
4458 break;
4459 case llvm::MemoryBuffer::MemoryBuffer_MMap:
4460 sizes.mmap_bytes += bytes;
4461 break;
4462 }
4463 }
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00004464 }
Ted Kremenek5e1ed7b2011-04-28 23:46:20 +00004465}
4466
Sebastian Redl2c499f62010-08-18 23:56:43 +00004467void ASTReader::InitializeSema(Sema &S) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00004468 SemaObj = &S;
Douglas Gregorc78d3462009-04-24 21:10:55 +00004469 S.ExternalSource = this;
4470
Douglas Gregor7cd60f72009-04-22 21:15:06 +00004471 // Makes sure any declarations that were deserialized "too early"
4472 // still get added to the identifier's declaration chains.
Douglas Gregor2fb99df2010-09-24 23:29:12 +00004473 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
4474 if (SemaObj->TUScope)
John McCall48871652010-08-21 09:40:31 +00004475 SemaObj->TUScope->AddDecl(PreloadedDecls[I]);
Douglas Gregor2fb99df2010-09-24 23:29:12 +00004476
4477 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregora868bbd2009-04-21 22:25:48 +00004478 }
Douglas Gregor7cd60f72009-04-22 21:15:06 +00004479 PreloadedDecls.clear();
Douglas Gregord4df8652009-04-22 22:02:47 +00004480
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004481 // Load the offsets of the declarations that Sema references.
4482 // They will be lazily deserialized when needed.
4483 if (!SemaDeclRefs.empty()) {
4484 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
Douglas Gregorb0f3ae62011-07-28 00:57:24 +00004485 if (!SemaObj->StdNamespace)
4486 SemaObj->StdNamespace = SemaDeclRefs[0];
4487 if (!SemaObj->StdBadAlloc)
4488 SemaObj->StdBadAlloc = SemaDeclRefs[1];
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004489 }
4490
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004491 if (!FPPragmaOptions.empty()) {
4492 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
4493 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
4494 }
4495
4496 if (!OpenCLExtensions.empty()) {
4497 unsigned I = 0;
4498#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
4499#include "clang/Basic/OpenCLExtensions.def"
4500
4501 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
4502 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00004503}
4504
Douglas Gregorab443b92011-08-20 04:39:52 +00004505IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Douglas Gregorab443b92011-08-20 04:39:52 +00004506 IdentifierLookupVisitor Visitor(StringRef(NameStart, NameEnd - NameStart));
4507 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
4508 return Visitor.getIdentifierInfo();
Douglas Gregora868bbd2009-04-21 22:25:48 +00004509}
4510
Douglas Gregor57756ea2010-10-14 22:11:03 +00004511namespace clang {
4512 /// \brief An identifier-lookup iterator that enumerates all of the
4513 /// identifiers stored within a set of AST files.
4514 class ASTIdentifierIterator : public IdentifierIterator {
4515 /// \brief The AST reader whose identifiers are being enumerated.
4516 const ASTReader &Reader;
4517
4518 /// \brief The current index into the chain of AST files stored in
4519 /// the AST reader.
4520 unsigned Index;
4521
4522 /// \brief The current position within the identifier lookup table
4523 /// of the current AST file.
4524 ASTIdentifierLookupTable::key_iterator Current;
4525
4526 /// \brief The end position within the identifier lookup table of
4527 /// the current AST file.
4528 ASTIdentifierLookupTable::key_iterator End;
4529
4530 public:
4531 explicit ASTIdentifierIterator(const ASTReader &Reader);
4532
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004533 virtual StringRef Next();
Douglas Gregor57756ea2010-10-14 22:11:03 +00004534 };
4535}
4536
4537ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00004538 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
Douglas Gregor57756ea2010-10-14 22:11:03 +00004539 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00004540 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
Douglas Gregor57756ea2010-10-14 22:11:03 +00004541 Current = IdTable->key_begin();
4542 End = IdTable->key_end();
4543}
4544
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004545StringRef ASTIdentifierIterator::Next() {
Douglas Gregor57756ea2010-10-14 22:11:03 +00004546 while (Current == End) {
4547 // If we have exhausted all of our AST files, we're done.
4548 if (Index == 0)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004549 return StringRef();
Douglas Gregor57756ea2010-10-14 22:11:03 +00004550
4551 --Index;
4552 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00004553 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
4554 IdentifierLookupTable;
Douglas Gregor57756ea2010-10-14 22:11:03 +00004555 Current = IdTable->key_begin();
4556 End = IdTable->key_end();
4557 }
4558
4559 // We have any identifiers remaining in the current AST file; return
4560 // the next one.
4561 std::pair<const char*, unsigned> Key = *Current;
4562 ++Current;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004563 return StringRef(Key.first, Key.second);
Douglas Gregor57756ea2010-10-14 22:11:03 +00004564}
4565
4566IdentifierIterator *ASTReader::getIdentifiers() const {
4567 return new ASTIdentifierIterator(*this);
4568}
4569
Douglas Gregorc10edd62011-08-25 14:51:20 +00004570namespace clang { namespace serialization {
4571 class ReadMethodPoolVisitor {
4572 ASTReader &Reader;
4573 Selector Sel;
4574 llvm::SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
4575 llvm::SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Douglas Gregorc78d3462009-04-24 21:10:55 +00004576
Douglas Gregorc10edd62011-08-25 14:51:20 +00004577 /// \brief Build an ObjCMethodList from a vector of Objective-C method
4578 /// declarations.
4579 ObjCMethodList
4580 buildObjCMethodList(const SmallVectorImpl<ObjCMethodDecl *> &Vec) const
4581 {
4582 ObjCMethodList List;
4583 ObjCMethodList *Prev = 0;
4584 for (unsigned I = 0, N = Vec.size(); I != N; ++I) {
4585 if (!List.Method) {
4586 // This is the first method, which is the easy case.
4587 List.Method = Vec[I];
4588 Prev = &List;
4589 continue;
4590 }
4591
4592 ObjCMethodList *Mem =
4593 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
4594 Prev->Next = new (Mem) ObjCMethodList(Vec[I], 0);
4595 Prev = Prev->Next;
4596 }
4597
4598 return List;
4599 }
4600
4601 public:
4602 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel)
4603 : Reader(Reader), Sel(Sel) { }
4604
4605 static bool visit(Module &M, void *UserData) {
4606 ReadMethodPoolVisitor *This
4607 = static_cast<ReadMethodPoolVisitor *>(UserData);
4608
4609 if (!M.SelectorLookupTable)
4610 return false;
4611
4612 ASTSelectorLookupTable *PoolTable
4613 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
4614 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
4615 if (Pos == PoolTable->end())
4616 return false;
4617
4618 ++This->Reader.NumSelectorsRead;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004619 // FIXME: Not quite happy with the statistics here. We probably should
4620 // disable this tracking when called via LoadSelector.
4621 // Also, should entries without methods count as misses?
Douglas Gregorc10edd62011-08-25 14:51:20 +00004622 ++This->Reader.NumMethodPoolEntriesRead;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004623 ASTSelectorLookupTrait::data_type Data = *Pos;
Douglas Gregorc10edd62011-08-25 14:51:20 +00004624 if (This->Reader.DeserializationListener)
4625 This->Reader.DeserializationListener->SelectorRead(Data.ID,
4626 This->Sel);
4627
4628 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
4629 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
4630 return true;
Sebastian Redlada023c2010-08-04 20:40:17 +00004631 }
Douglas Gregorc10edd62011-08-25 14:51:20 +00004632
4633 /// \brief Retrieve the instance methods found by this visitor.
4634 ObjCMethodList getInstanceMethods() const {
4635 return buildObjCMethodList(InstanceMethods);
4636 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00004637
Douglas Gregorc10edd62011-08-25 14:51:20 +00004638 /// \brief Retrieve the instance methods found by this visitor.
4639 ObjCMethodList getFactoryMethods() const {
4640 return buildObjCMethodList(FactoryMethods);
4641 }
4642 };
4643} } // end namespace clang::serialization
4644
4645std::pair<ObjCMethodList, ObjCMethodList>
4646ASTReader::ReadMethodPool(Selector Sel) {
4647 ReadMethodPoolVisitor Visitor(*this, Sel);
4648 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
4649 std::pair<ObjCMethodList, ObjCMethodList> Result;
4650 Result.first = Visitor.getInstanceMethods();
4651 Result.second = Visitor.getFactoryMethods();
4652
4653 if (!Result.first.Method && !Result.second.Method)
4654 ++NumMethodPoolMisses;
4655 return Result;
Douglas Gregorc78d3462009-04-24 21:10:55 +00004656}
4657
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004658void ASTReader::ReadKnownNamespaces(
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004659 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004660 Namespaces.clear();
4661
4662 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
4663 if (NamespaceDecl *Namespace
4664 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
4665 Namespaces.push_back(Namespace);
4666 }
4667}
4668
Douglas Gregoreb08bd42011-07-27 20:58:46 +00004669void ASTReader::ReadTentativeDefinitions(
4670 SmallVectorImpl<VarDecl *> &TentativeDefs) {
4671 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
4672 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
4673 if (Var)
4674 TentativeDefs.push_back(Var);
4675 }
4676 TentativeDefinitions.clear();
4677}
4678
Douglas Gregora94a1542011-07-27 21:45:57 +00004679void ASTReader::ReadUnusedFileScopedDecls(
4680 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
4681 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
4682 DeclaratorDecl *D
4683 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
4684 if (D)
4685 Decls.push_back(D);
4686 }
4687 UnusedFileScopedDecls.clear();
4688}
4689
Douglas Gregorbae31202011-07-27 21:57:17 +00004690void ASTReader::ReadDelegatingConstructors(
4691 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
4692 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
4693 CXXConstructorDecl *D
4694 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
4695 if (D)
4696 Decls.push_back(D);
4697 }
4698 DelegatingCtorDecls.clear();
4699}
4700
Douglas Gregorb7098a32011-07-28 00:39:29 +00004701void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
4702 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
4703 TypedefNameDecl *D
4704 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
4705 if (D)
4706 Decls.push_back(D);
4707 }
4708 ExtVectorDecls.clear();
4709}
4710
Douglas Gregor32002192011-07-28 00:53:40 +00004711void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
4712 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
4713 CXXRecordDecl *D
4714 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
4715 if (D)
4716 Decls.push_back(D);
4717 }
4718 DynamicClasses.clear();
4719}
4720
Douglas Gregordc5c9582011-07-28 14:20:37 +00004721void
4722ASTReader::ReadLocallyScopedExternalDecls(SmallVectorImpl<NamedDecl *> &Decls) {
4723 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
4724 NamedDecl *D
4725 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
4726 if (D)
4727 Decls.push_back(D);
4728 }
4729 LocallyScopedExternalDecls.clear();
4730}
4731
Douglas Gregor72e357f2011-07-28 14:54:22 +00004732void ASTReader::ReadReferencedSelectors(
4733 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
4734 if (ReferencedSelectorsData.empty())
4735 return;
4736
4737 // If there are @selector references added them to its pool. This is for
4738 // implementation of -Wselector.
4739 unsigned int DataSize = ReferencedSelectorsData.size()-1;
4740 unsigned I = 0;
4741 while (I < DataSize) {
4742 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
4743 SourceLocation SelLoc
4744 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
4745 Sels.push_back(std::make_pair(Sel, SelLoc));
4746 }
4747 ReferencedSelectorsData.clear();
4748}
4749
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00004750void ASTReader::ReadWeakUndeclaredIdentifiers(
4751 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
4752 if (WeakUndeclaredIdentifiers.empty())
4753 return;
4754
4755 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
4756 IdentifierInfo *WeakId
4757 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
4758 IdentifierInfo *AliasId
4759 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
4760 SourceLocation Loc
4761 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
4762 bool Used = WeakUndeclaredIdentifiers[I++];
4763 WeakInfo WI(AliasId, Loc);
4764 WI.setUsed(Used);
4765 WeakIDs.push_back(std::make_pair(WeakId, WI));
4766 }
4767 WeakUndeclaredIdentifiers.clear();
4768}
4769
Douglas Gregor4daf6a32011-07-28 19:11:31 +00004770void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
4771 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
4772 ExternalVTableUse VT;
4773 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
4774 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
4775 VT.DefinitionRequired = VTableUses[Idx++];
4776 VTables.push_back(VT);
4777 }
4778
4779 VTableUses.clear();
4780}
4781
Douglas Gregore39f97c2011-07-28 19:49:54 +00004782void ASTReader::ReadPendingInstantiations(
4783 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
4784 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
4785 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
4786 SourceLocation Loc
4787 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
4788 Pending.push_back(std::make_pair(D, Loc));
4789 }
4790 PendingInstantiations.clear();
4791}
4792
Sebastian Redl2c499f62010-08-18 23:56:43 +00004793void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00004794 // It would be complicated to avoid reading the methods anyway. So don't.
4795 ReadMethodPool(Sel);
4796}
4797
Douglas Gregora3e41532011-07-28 20:55:49 +00004798void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00004799 assert(ID && "Non-zero identifier ID required");
Douglas Gregor6f00bf82009-04-28 21:53:25 +00004800 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor0e149972009-04-25 19:10:14 +00004801 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlff4a2952010-07-23 23:49:55 +00004802 if (DeserializationListener)
4803 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregora868bbd2009-04-21 22:25:48 +00004804}
4805
Douglas Gregor1342e842009-07-06 18:54:52 +00004806/// \brief Set the globally-visible declarations associated with the given
4807/// identifier.
4808///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004809/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump11289f42009-09-09 15:08:12 +00004810/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregor1342e842009-07-06 18:54:52 +00004811/// them.
4812///
4813/// \param II an IdentifierInfo that refers to one or more globally-visible
4814/// declarations.
4815///
4816/// \param DeclIDs the set of declaration IDs with the name @p II that are
4817/// visible at global scope.
4818///
4819/// \param Nonrecursive should be true to indicate that the caller knows that
4820/// this call is non-recursive, and therefore the globally-visible declarations
4821/// will not be placed onto the pending queue.
Mike Stump11289f42009-09-09 15:08:12 +00004822void
Sebastian Redl2c499f62010-08-18 23:56:43 +00004823ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004824 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregor1342e842009-07-06 18:54:52 +00004825 bool Nonrecursive) {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004826 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregor1342e842009-07-06 18:54:52 +00004827 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
4828 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
4829 PII.II = II;
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00004830 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregor1342e842009-07-06 18:54:52 +00004831 return;
4832 }
Mike Stump11289f42009-09-09 15:08:12 +00004833
Douglas Gregor1342e842009-07-06 18:54:52 +00004834 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
4835 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
4836 if (SemaObj) {
Douglas Gregor6fd55e02010-08-13 03:15:25 +00004837 if (SemaObj->TUScope) {
4838 // Introduce this declaration into the translation-unit scope
4839 // and add it to the declaration chain for this identifier, so
4840 // that (unqualified) name lookup will find it.
John McCall48871652010-08-21 09:40:31 +00004841 SemaObj->TUScope->AddDecl(D);
Douglas Gregor6fd55e02010-08-13 03:15:25 +00004842 }
Douglas Gregor2fb99df2010-09-24 23:29:12 +00004843 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
Douglas Gregor1342e842009-07-06 18:54:52 +00004844 } else {
4845 // Queue this declaration so that it will be added to the
4846 // translation unit scope and identifier's declaration chain
4847 // once a Sema object is known.
4848 PreloadedDecls.push_back(D);
4849 }
4850 }
4851}
4852
Douglas Gregora3e41532011-07-28 20:55:49 +00004853IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004854 if (ID == 0)
4855 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00004856
Sebastian Redlc713b962010-07-21 00:46:22 +00004857 if (IdentifiersLoaded.empty()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004858 Error("no identifier table in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004859 return 0;
4860 }
Mike Stump11289f42009-09-09 15:08:12 +00004861
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00004862 assert(PP && "Forgot to set Preprocessor ?");
Sebastian Redlc713b962010-07-21 00:46:22 +00004863 ID -= 1;
4864 if (!IdentifiersLoaded[ID]) {
Douglas Gregor19d26352011-07-20 00:59:32 +00004865 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
4866 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00004867 Module *M = I->second;
4868 unsigned Index = ID - M->BaseIdentifierID;
4869 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
Douglas Gregor5287b4e2009-04-25 21:04:17 +00004870
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004871 // All of the strings in the AST file are preceded by a 16-bit length.
4872 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenekca42a512009-10-23 04:45:31 +00004873 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
4874 // unsigned integers. This is important to avoid integer overflow when
4875 // we cast them to 'unsigned'.
Ted Kremenek49c52322009-10-23 03:57:22 +00004876 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregorab4df582009-04-28 20:01:51 +00004877 unsigned StrLen = (((unsigned) StrLenPtr[0])
4878 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redlc713b962010-07-21 00:46:22 +00004879 IdentifiersLoaded[ID]
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004880 = &PP->getIdentifierTable().get(StringRef(Str, StrLen));
Sebastian Redlff4a2952010-07-23 23:49:55 +00004881 if (DeserializationListener)
4882 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004883 }
Mike Stump11289f42009-09-09 15:08:12 +00004884
Sebastian Redlc713b962010-07-21 00:46:22 +00004885 return IdentifiersLoaded[ID];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004886}
4887
Douglas Gregora3e41532011-07-28 20:55:49 +00004888IdentifierInfo *ASTReader::getLocalIdentifier(Module &M, unsigned LocalID) {
4889 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
4890}
4891
4892IdentifierID ASTReader::getGlobalIdentifierID(Module &M, unsigned LocalID) {
Douglas Gregor1ab036c2011-08-03 21:49:18 +00004893 if (LocalID < NUM_PREDEF_IDENT_IDS)
4894 return LocalID;
4895
4896 ContinuousRangeMap<uint32_t, int, 2>::iterator I
4897 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
4898 assert(I != M.IdentifierRemap.end()
4899 && "Invalid index into identifier index remap");
4900
4901 return LocalID + I->second;
Douglas Gregora3e41532011-07-28 20:55:49 +00004902}
4903
Douglas Gregor925296b2011-07-19 16:10:42 +00004904bool ASTReader::ReadSLocEntry(int ID) {
Douglas Gregor49f754f2011-04-20 00:21:03 +00004905 return ReadSLocEntryRecord(ID) != Success;
Douglas Gregor258ae542009-04-27 06:38:32 +00004906}
4907
Douglas Gregor074fdc52011-07-28 21:16:51 +00004908Selector ASTReader::getLocalSelector(Module &M, unsigned LocalID) {
4909 return DecodeSelector(getGlobalSelectorID(M, LocalID));
4910}
4911
4912Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
Steve Naroff2ddea052009-04-23 10:39:46 +00004913 if (ID == 0)
4914 return Selector();
Mike Stump11289f42009-09-09 15:08:12 +00004915
Sebastian Redlada023c2010-08-04 20:40:17 +00004916 if (ID > SelectorsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004917 Error("selector ID out of range in AST file");
Steve Naroff2ddea052009-04-23 10:39:46 +00004918 return Selector();
4919 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00004920
Sebastian Redlada023c2010-08-04 20:40:17 +00004921 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004922 // Load this selector from the selector table.
Douglas Gregor2262d282011-07-20 01:10:58 +00004923 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
4924 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00004925 Module &M = *I->second;
4926 ASTSelectorLookupTrait Trait(*this, M);
Douglas Gregor8f364fb2011-08-03 23:28:44 +00004927 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
Douglas Gregor2262d282011-07-20 01:10:58 +00004928 SelectorsLoaded[ID - 1] =
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00004929 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
Douglas Gregor2262d282011-07-20 01:10:58 +00004930 if (DeserializationListener)
4931 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
Douglas Gregor95c13f52009-04-25 17:48:32 +00004932 }
4933
Sebastian Redlada023c2010-08-04 20:40:17 +00004934 return SelectorsLoaded[ID - 1];
Steve Naroff2ddea052009-04-23 10:39:46 +00004935}
4936
Douglas Gregor3f8f04f2011-07-28 14:41:43 +00004937Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
Douglas Gregord720daf2010-04-06 17:30:22 +00004938 return DecodeSelector(ID);
4939}
4940
Sebastian Redl2c499f62010-08-18 23:56:43 +00004941uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redlada023c2010-08-04 20:40:17 +00004942 // ID 0 (the null selector) is considered an external selector.
4943 return getTotalNumSelectors() + 1;
Douglas Gregord720daf2010-04-06 17:30:22 +00004944}
4945
Douglas Gregor8f364fb2011-08-03 23:28:44 +00004946serialization::SelectorID
4947ASTReader::getGlobalSelectorID(Module &M, unsigned LocalID) const {
4948 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
4949 return LocalID;
4950
4951 ContinuousRangeMap<uint32_t, int, 2>::iterator I
4952 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
4953 assert(I != M.SelectorRemap.end()
4954 && "Invalid index into identifier index remap");
4955
4956 return LocalID + I->second;
Douglas Gregor3f8f04f2011-07-28 14:41:43 +00004957}
4958
Mike Stump11289f42009-09-09 15:08:12 +00004959DeclarationName
Douglas Gregora6895d82011-07-22 16:00:58 +00004960ASTReader::ReadDeclarationName(Module &F,
Douglas Gregor903b7e92011-07-22 00:38:23 +00004961 const RecordData &Record, unsigned &Idx) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004962 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
4963 switch (Kind) {
4964 case DeclarationName::Identifier:
Douglas Gregora3e41532011-07-28 20:55:49 +00004965 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004966
4967 case DeclarationName::ObjCZeroArgSelector:
4968 case DeclarationName::ObjCOneArgSelector:
4969 case DeclarationName::ObjCMultiArgSelector:
Douglas Gregor074fdc52011-07-28 21:16:51 +00004970 return DeclarationName(ReadSelector(F, Record, Idx));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004971
4972 case DeclarationName::CXXConstructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004973 return Context->DeclarationNames.getCXXConstructorName(
Douglas Gregor903b7e92011-07-22 00:38:23 +00004974 Context->getCanonicalType(readType(F, Record, Idx)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004975
4976 case DeclarationName::CXXDestructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004977 return Context->DeclarationNames.getCXXDestructorName(
Douglas Gregor903b7e92011-07-22 00:38:23 +00004978 Context->getCanonicalType(readType(F, Record, Idx)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004979
4980 case DeclarationName::CXXConversionFunctionName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004981 return Context->DeclarationNames.getCXXConversionFunctionName(
Douglas Gregor903b7e92011-07-22 00:38:23 +00004982 Context->getCanonicalType(readType(F, Record, Idx)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004983
4984 case DeclarationName::CXXOperatorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004985 return Context->DeclarationNames.getCXXOperatorName(
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004986 (OverloadedOperatorKind)Record[Idx++]);
4987
Alexis Hunt3d221f22009-11-29 07:34:05 +00004988 case DeclarationName::CXXLiteralOperatorName:
4989 return Context->DeclarationNames.getCXXLiteralOperatorName(
Douglas Gregora3e41532011-07-28 20:55:49 +00004990 GetIdentifierInfo(F, Record, Idx));
Alexis Hunt3d221f22009-11-29 07:34:05 +00004991
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004992 case DeclarationName::CXXUsingDirective:
4993 return DeclarationName::getUsingDirectiveName();
4994 }
4995
4996 // Required to silence GCC warning
4997 return DeclarationName();
4998}
Douglas Gregor55abb232009-04-10 20:39:37 +00004999
Douglas Gregora6895d82011-07-22 16:00:58 +00005000void ASTReader::ReadDeclarationNameLoc(Module &F,
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005001 DeclarationNameLoc &DNLoc,
5002 DeclarationName Name,
5003 const RecordData &Record, unsigned &Idx) {
5004 switch (Name.getNameKind()) {
5005 case DeclarationName::CXXConstructorName:
5006 case DeclarationName::CXXDestructorName:
5007 case DeclarationName::CXXConversionFunctionName:
5008 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
5009 break;
5010
5011 case DeclarationName::CXXOperatorName:
5012 DNLoc.CXXOperatorName.BeginOpNameLoc
5013 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5014 DNLoc.CXXOperatorName.EndOpNameLoc
5015 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5016 break;
5017
5018 case DeclarationName::CXXLiteralOperatorName:
5019 DNLoc.CXXLiteralOperatorName.OpNameLoc
5020 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5021 break;
5022
5023 case DeclarationName::Identifier:
5024 case DeclarationName::ObjCZeroArgSelector:
5025 case DeclarationName::ObjCOneArgSelector:
5026 case DeclarationName::ObjCMultiArgSelector:
5027 case DeclarationName::CXXUsingDirective:
5028 break;
5029 }
5030}
5031
Douglas Gregora6895d82011-07-22 16:00:58 +00005032void ASTReader::ReadDeclarationNameInfo(Module &F,
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005033 DeclarationNameInfo &NameInfo,
5034 const RecordData &Record, unsigned &Idx) {
Douglas Gregor903b7e92011-07-22 00:38:23 +00005035 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005036 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
5037 DeclarationNameLoc DNLoc;
5038 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
5039 NameInfo.setInfo(DNLoc);
5040}
5041
Douglas Gregora6895d82011-07-22 16:00:58 +00005042void ASTReader::ReadQualifierInfo(Module &F, QualifierInfo &Info,
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005043 const RecordData &Record, unsigned &Idx) {
Douglas Gregor14454802011-02-25 02:25:35 +00005044 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005045 unsigned NumTPLists = Record[Idx++];
5046 Info.NumTemplParamLists = NumTPLists;
5047 if (NumTPLists) {
5048 Info.TemplParamLists = new (*Context) TemplateParameterList*[NumTPLists];
5049 for (unsigned i=0; i != NumTPLists; ++i)
5050 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
5051 }
5052}
5053
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005054TemplateName
Douglas Gregora6895d82011-07-22 16:00:58 +00005055ASTReader::ReadTemplateName(Module &F, const RecordData &Record,
Douglas Gregor5590be02011-01-15 06:45:20 +00005056 unsigned &Idx) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005057 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005058 switch (Kind) {
5059 case TemplateName::Template:
Douglas Gregor7fb09192011-07-21 22:35:25 +00005060 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005061
5062 case TemplateName::OverloadedTemplate: {
5063 unsigned size = Record[Idx++];
5064 UnresolvedSet<8> Decls;
5065 while (size--)
Douglas Gregor7fb09192011-07-21 22:35:25 +00005066 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005067
5068 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
5069 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005070
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005071 case TemplateName::QualifiedTemplate: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00005072 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005073 bool hasTemplKeyword = Record[Idx++];
Douglas Gregor7fb09192011-07-21 22:35:25 +00005074 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005075 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
5076 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005077
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005078 case TemplateName::DependentTemplate: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00005079 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005080 if (Record[Idx++]) // isIdentifier
5081 return Context->getDependentTemplateName(NNS,
Douglas Gregora3e41532011-07-28 20:55:49 +00005082 GetIdentifierInfo(F, Record,
5083 Idx));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005084 return Context->getDependentTemplateName(NNS,
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00005085 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005086 }
John McCalld9dfe3a2011-06-30 08:33:18 +00005087
5088 case TemplateName::SubstTemplateTemplateParm: {
5089 TemplateTemplateParmDecl *param
Douglas Gregor7fb09192011-07-21 22:35:25 +00005090 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
John McCalld9dfe3a2011-06-30 08:33:18 +00005091 if (!param) return TemplateName();
5092 TemplateName replacement = ReadTemplateName(F, Record, Idx);
5093 return Context->getSubstTemplateTemplateParm(param, replacement);
5094 }
Douglas Gregor5590be02011-01-15 06:45:20 +00005095
5096 case TemplateName::SubstTemplateTemplateParmPack: {
5097 TemplateTemplateParmDecl *Param
Douglas Gregor7fb09192011-07-21 22:35:25 +00005098 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
Douglas Gregor5590be02011-01-15 06:45:20 +00005099 if (!Param)
5100 return TemplateName();
5101
5102 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
5103 if (ArgPack.getKind() != TemplateArgument::Pack)
5104 return TemplateName();
5105
5106 return Context->getSubstTemplateTemplateParmPack(Param, ArgPack);
5107 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005108 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005109
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005110 assert(0 && "Unhandled template name kind!");
5111 return TemplateName();
5112}
5113
5114TemplateArgument
Douglas Gregora6895d82011-07-22 16:00:58 +00005115ASTReader::ReadTemplateArgument(Module &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00005116 const RecordData &Record, unsigned &Idx) {
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005117 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
5118 switch (Kind) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005119 case TemplateArgument::Null:
5120 return TemplateArgument();
5121 case TemplateArgument::Type:
Douglas Gregor903b7e92011-07-22 00:38:23 +00005122 return TemplateArgument(readType(F, Record, Idx));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005123 case TemplateArgument::Declaration:
Douglas Gregor7fb09192011-07-21 22:35:25 +00005124 return TemplateArgument(ReadDecl(F, Record, Idx));
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +00005125 case TemplateArgument::Integral: {
5126 llvm::APSInt Value = ReadAPSInt(Record, Idx);
Douglas Gregor903b7e92011-07-22 00:38:23 +00005127 QualType T = readType(F, Record, Idx);
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +00005128 return TemplateArgument(Value, T);
5129 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005130 case TemplateArgument::Template:
Douglas Gregor5590be02011-01-15 06:45:20 +00005131 return TemplateArgument(ReadTemplateName(F, Record, Idx));
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005132 case TemplateArgument::TemplateExpansion: {
Douglas Gregor5590be02011-01-15 06:45:20 +00005133 TemplateName Name = ReadTemplateName(F, Record, Idx);
Douglas Gregore1d60df2011-01-14 23:41:42 +00005134 llvm::Optional<unsigned> NumTemplateExpansions;
5135 if (unsigned NumExpansions = Record[Idx++])
5136 NumTemplateExpansions = NumExpansions - 1;
5137 return TemplateArgument(Name, NumTemplateExpansions);
Douglas Gregoreb29d182011-01-05 17:40:24 +00005138 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005139 case TemplateArgument::Expression:
Sebastian Redl2c373b92010-10-05 15:59:54 +00005140 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005141 case TemplateArgument::Pack: {
5142 unsigned NumArgs = Record[Idx++];
Douglas Gregor1ccc8412010-11-07 23:05:16 +00005143 TemplateArgument *Args = new (*Context) TemplateArgument[NumArgs];
5144 for (unsigned I = 0; I != NumArgs; ++I)
5145 Args[I] = ReadTemplateArgument(F, Record, Idx);
5146 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005147 }
5148 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005149
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005150 assert(0 && "Unhandled template argument kind!");
5151 return TemplateArgument();
5152}
5153
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005154TemplateParameterList *
Douglas Gregora6895d82011-07-22 16:00:58 +00005155ASTReader::ReadTemplateParameterList(Module &F,
Sebastian Redl2c373b92010-10-05 15:59:54 +00005156 const RecordData &Record, unsigned &Idx) {
5157 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
5158 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
5159 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005160
5161 unsigned NumParams = Record[Idx++];
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005162 SmallVector<NamedDecl *, 16> Params;
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005163 Params.reserve(NumParams);
5164 while (NumParams--)
Douglas Gregor7fb09192011-07-21 22:35:25 +00005165 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005166
5167 TemplateParameterList* TemplateParams =
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005168 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
5169 Params.data(), Params.size(), RAngleLoc);
5170 return TemplateParams;
5171}
5172
5173void
Sebastian Redl2c499f62010-08-18 23:56:43 +00005174ASTReader::
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005175ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs,
Douglas Gregora6895d82011-07-22 16:00:58 +00005176 Module &F, const RecordData &Record,
Sebastian Redl2c373b92010-10-05 15:59:54 +00005177 unsigned &Idx) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005178 unsigned NumTemplateArgs = Record[Idx++];
5179 TemplArgs.reserve(NumTemplateArgs);
5180 while (NumTemplateArgs--)
Sebastian Redl2c373b92010-10-05 15:59:54 +00005181 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005182}
5183
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005184/// \brief Read a UnresolvedSet structure.
Douglas Gregora6895d82011-07-22 16:00:58 +00005185void ASTReader::ReadUnresolvedSet(Module &F, UnresolvedSetImpl &Set,
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005186 const RecordData &Record, unsigned &Idx) {
5187 unsigned NumDecls = Record[Idx++];
5188 while (NumDecls--) {
Douglas Gregor7fb09192011-07-21 22:35:25 +00005189 NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx);
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005190 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
5191 Set.addDecl(D, AS);
5192 }
5193}
5194
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005195CXXBaseSpecifier
Douglas Gregora6895d82011-07-22 16:00:58 +00005196ASTReader::ReadCXXBaseSpecifier(Module &F,
Nick Lewycky19b9f952010-07-26 16:56:01 +00005197 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005198 bool isVirtual = static_cast<bool>(Record[Idx++]);
5199 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
5200 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redl08905022011-02-05 19:23:19 +00005201 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00005202 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
5203 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor752a5952011-01-03 22:36:02 +00005204 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redl08905022011-02-05 19:23:19 +00005205 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
Douglas Gregor752a5952011-01-03 22:36:02 +00005206 EllipsisLoc);
Sebastian Redl08905022011-02-05 19:23:19 +00005207 Result.setInheritConstructors(inheritConstructors);
5208 return Result;
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005209}
5210
Alexis Hunt1d792652011-01-08 20:30:50 +00005211std::pair<CXXCtorInitializer **, unsigned>
Douglas Gregora6895d82011-07-22 16:00:58 +00005212ASTReader::ReadCXXCtorInitializers(Module &F, const RecordData &Record,
Alexis Hunt1d792652011-01-08 20:30:50 +00005213 unsigned &Idx) {
5214 CXXCtorInitializer **CtorInitializers = 0;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005215 unsigned NumInitializers = Record[Idx++];
5216 if (NumInitializers) {
5217 ASTContext &C = *getContext();
5218
Alexis Hunt1d792652011-01-08 20:30:50 +00005219 CtorInitializers
5220 = new (C) CXXCtorInitializer*[NumInitializers];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005221 for (unsigned i=0; i != NumInitializers; ++i) {
5222 TypeSourceInfo *BaseClassInfo = 0;
5223 bool IsBaseVirtual = false;
5224 FieldDecl *Member = 0;
Francois Pichetd583da02010-12-04 09:14:42 +00005225 IndirectFieldDecl *IndirectMember = 0;
Alexis Hunt37a477f2011-05-04 01:19:08 +00005226 CXXConstructorDecl *Target = 0;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005227
Alexis Hunt37a477f2011-05-04 01:19:08 +00005228 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
5229 switch (Type) {
5230 case CTOR_INITIALIZER_BASE:
Sebastian Redl2c373b92010-10-05 15:59:54 +00005231 BaseClassInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005232 IsBaseVirtual = Record[Idx++];
Alexis Hunt37a477f2011-05-04 01:19:08 +00005233 break;
5234
5235 case CTOR_INITIALIZER_DELEGATING:
Douglas Gregor7fb09192011-07-21 22:35:25 +00005236 Target = ReadDeclAs<CXXConstructorDecl>(F, Record, Idx);
Alexis Hunt37a477f2011-05-04 01:19:08 +00005237 break;
5238
5239 case CTOR_INITIALIZER_MEMBER:
Douglas Gregor7fb09192011-07-21 22:35:25 +00005240 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
Alexis Hunt37a477f2011-05-04 01:19:08 +00005241 break;
5242
5243 case CTOR_INITIALIZER_INDIRECT_MEMBER:
Douglas Gregor7fb09192011-07-21 22:35:25 +00005244 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
Alexis Hunt37a477f2011-05-04 01:19:08 +00005245 break;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005246 }
Alexis Hunt37a477f2011-05-04 01:19:08 +00005247
Douglas Gregor44e7df62011-01-04 00:32:56 +00005248 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redl2c373b92010-10-05 15:59:54 +00005249 Expr *Init = ReadExpr(F);
Sebastian Redl2c373b92010-10-05 15:59:54 +00005250 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
5251 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005252 bool IsWritten = Record[Idx++];
5253 unsigned SourceOrderOrNumArrayIndices;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005254 SmallVector<VarDecl *, 8> Indices;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005255 if (IsWritten) {
5256 SourceOrderOrNumArrayIndices = Record[Idx++];
5257 } else {
5258 SourceOrderOrNumArrayIndices = Record[Idx++];
5259 Indices.reserve(SourceOrderOrNumArrayIndices);
5260 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
Douglas Gregor7fb09192011-07-21 22:35:25 +00005261 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005262 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005263
Alexis Hunt1d792652011-01-08 20:30:50 +00005264 CXXCtorInitializer *BOMInit;
Alexis Hunt37a477f2011-05-04 01:19:08 +00005265 if (Type == CTOR_INITIALIZER_BASE) {
Alexis Hunt1d792652011-01-08 20:30:50 +00005266 BOMInit = new (C) CXXCtorInitializer(C, BaseClassInfo, IsBaseVirtual,
5267 LParenLoc, Init, RParenLoc,
5268 MemberOrEllipsisLoc);
Alexis Hunt37a477f2011-05-04 01:19:08 +00005269 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
5270 BOMInit = new (C) CXXCtorInitializer(C, MemberOrEllipsisLoc, LParenLoc,
5271 Target, Init, RParenLoc);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005272 } else if (IsWritten) {
Francois Pichetd583da02010-12-04 09:14:42 +00005273 if (Member)
Alexis Hunt1d792652011-01-08 20:30:50 +00005274 BOMInit = new (C) CXXCtorInitializer(C, Member, MemberOrEllipsisLoc,
5275 LParenLoc, Init, RParenLoc);
Francois Pichetd583da02010-12-04 09:14:42 +00005276 else
Alexis Hunt1d792652011-01-08 20:30:50 +00005277 BOMInit = new (C) CXXCtorInitializer(C, IndirectMember,
5278 MemberOrEllipsisLoc, LParenLoc,
5279 Init, RParenLoc);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005280 } else {
Alexis Hunt1d792652011-01-08 20:30:50 +00005281 BOMInit = CXXCtorInitializer::Create(C, Member, MemberOrEllipsisLoc,
5282 LParenLoc, Init, RParenLoc,
5283 Indices.data(), Indices.size());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005284 }
5285
Argyrios Kyrtzidisd05f3e32010-09-06 19:04:27 +00005286 if (IsWritten)
5287 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Alexis Hunt1d792652011-01-08 20:30:50 +00005288 CtorInitializers[i] = BOMInit;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005289 }
5290 }
5291
Alexis Hunt1d792652011-01-08 20:30:50 +00005292 return std::make_pair(CtorInitializers, NumInitializers);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005293}
5294
Chris Lattnerca025db2010-05-07 21:43:38 +00005295NestedNameSpecifier *
Douglas Gregora6895d82011-07-22 16:00:58 +00005296ASTReader::ReadNestedNameSpecifier(Module &F,
Douglas Gregor7fb09192011-07-21 22:35:25 +00005297 const RecordData &Record, unsigned &Idx) {
Chris Lattnerca025db2010-05-07 21:43:38 +00005298 unsigned N = Record[Idx++];
5299 NestedNameSpecifier *NNS = 0, *Prev = 0;
5300 for (unsigned I = 0; I != N; ++I) {
5301 NestedNameSpecifier::SpecifierKind Kind
5302 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
5303 switch (Kind) {
5304 case NestedNameSpecifier::Identifier: {
Douglas Gregora3e41532011-07-28 20:55:49 +00005305 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Chris Lattnerca025db2010-05-07 21:43:38 +00005306 NNS = NestedNameSpecifier::Create(*Context, Prev, II);
5307 break;
5308 }
5309
5310 case NestedNameSpecifier::Namespace: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00005311 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Chris Lattnerca025db2010-05-07 21:43:38 +00005312 NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
5313 break;
5314 }
5315
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005316 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00005317 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005318 NNS = NestedNameSpecifier::Create(*Context, Prev, Alias);
5319 break;
5320 }
5321
Chris Lattnerca025db2010-05-07 21:43:38 +00005322 case NestedNameSpecifier::TypeSpec:
5323 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregor903b7e92011-07-22 00:38:23 +00005324 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
Douglas Gregor0cdc8322010-12-10 17:03:06 +00005325 if (!T)
5326 return 0;
5327
Chris Lattnerca025db2010-05-07 21:43:38 +00005328 bool Template = Record[Idx++];
5329 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
5330 break;
5331 }
5332
5333 case NestedNameSpecifier::Global: {
5334 NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
5335 // No associated value, and there can't be a prefix.
5336 break;
5337 }
Chris Lattnerca025db2010-05-07 21:43:38 +00005338 }
Argyrios Kyrtzidisad65c692010-07-07 15:46:30 +00005339 Prev = NNS;
Chris Lattnerca025db2010-05-07 21:43:38 +00005340 }
5341 return NNS;
5342}
5343
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005344NestedNameSpecifierLoc
Douglas Gregora6895d82011-07-22 16:00:58 +00005345ASTReader::ReadNestedNameSpecifierLoc(Module &F, const RecordData &Record,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005346 unsigned &Idx) {
5347 unsigned N = Record[Idx++];
Douglas Gregor9b272512011-02-28 23:58:31 +00005348 NestedNameSpecifierLocBuilder Builder;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005349 for (unsigned I = 0; I != N; ++I) {
5350 NestedNameSpecifier::SpecifierKind Kind
5351 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
5352 switch (Kind) {
5353 case NestedNameSpecifier::Identifier: {
Douglas Gregora3e41532011-07-28 20:55:49 +00005354 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005355 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00005356 Builder.Extend(*Context, II, Range.getBegin(), Range.getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005357 break;
5358 }
5359
5360 case NestedNameSpecifier::Namespace: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00005361 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005362 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00005363 Builder.Extend(*Context, NS, Range.getBegin(), Range.getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005364 break;
5365 }
5366
5367 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00005368 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005369 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00005370 Builder.Extend(*Context, Alias, Range.getBegin(), Range.getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005371 break;
5372 }
5373
5374 case NestedNameSpecifier::TypeSpec:
5375 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005376 bool Template = Record[Idx++];
5377 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
5378 if (!T)
5379 return NestedNameSpecifierLoc();
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005380 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00005381
5382 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
5383 Builder.Extend(*Context,
5384 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
5385 T->getTypeLoc(), ColonColonLoc);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005386 break;
5387 }
5388
5389 case NestedNameSpecifier::Global: {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005390 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00005391 Builder.MakeGlobal(*Context, ColonColonLoc);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005392 break;
5393 }
5394 }
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005395 }
5396
Douglas Gregor9b272512011-02-28 23:58:31 +00005397 return Builder.getWithLocInContext(*Context);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005398}
5399
Chris Lattnerca025db2010-05-07 21:43:38 +00005400SourceRange
Douglas Gregora6895d82011-07-22 16:00:58 +00005401ASTReader::ReadSourceRange(Module &F, const RecordData &Record,
Sebastian Redl2c373b92010-10-05 15:59:54 +00005402 unsigned &Idx) {
5403 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
5404 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar6d3bc082010-06-02 15:47:10 +00005405 return SourceRange(beg, end);
Chris Lattnerca025db2010-05-07 21:43:38 +00005406}
5407
Douglas Gregor1daeb692009-04-13 18:14:40 +00005408/// \brief Read an integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00005409llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00005410 unsigned BitWidth = Record[Idx++];
5411 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
5412 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
5413 Idx += NumWords;
5414 return Result;
5415}
5416
5417/// \brief Read a signed integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00005418llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00005419 bool isUnsigned = Record[Idx++];
5420 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
5421}
5422
Douglas Gregore0a3a512009-04-14 21:55:33 +00005423/// \brief Read a floating-point value
Sebastian Redl2c499f62010-08-18 23:56:43 +00005424llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00005425 return llvm::APFloat(ReadAPInt(Record, Idx));
5426}
5427
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00005428// \brief Read a string
Sebastian Redl2c499f62010-08-18 23:56:43 +00005429std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00005430 unsigned Len = Record[Idx++];
Jay Foad7d0479f2009-05-21 09:52:38 +00005431 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00005432 Idx += Len;
5433 return Result;
5434}
5435
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005436VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
5437 unsigned &Idx) {
5438 unsigned Major = Record[Idx++];
5439 unsigned Minor = Record[Idx++];
5440 unsigned Subminor = Record[Idx++];
5441 if (Minor == 0)
5442 return VersionTuple(Major);
5443 if (Subminor == 0)
5444 return VersionTuple(Major, Minor - 1);
5445 return VersionTuple(Major, Minor - 1, Subminor - 1);
5446}
5447
Douglas Gregora6895d82011-07-22 16:00:58 +00005448CXXTemporary *ASTReader::ReadCXXTemporary(Module &F,
Douglas Gregor7fb09192011-07-21 22:35:25 +00005449 const RecordData &Record,
Chris Lattnercba86142010-05-10 00:25:06 +00005450 unsigned &Idx) {
Douglas Gregor7fb09192011-07-21 22:35:25 +00005451 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
Chris Lattnercba86142010-05-10 00:25:06 +00005452 return CXXTemporary::Create(*Context, Decl);
5453}
5454
Sebastian Redl2c499f62010-08-18 23:56:43 +00005455DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregor92863e42009-04-10 23:10:45 +00005456 return Diag(SourceLocation(), DiagID);
5457}
5458
Sebastian Redl2c499f62010-08-18 23:56:43 +00005459DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00005460 return Diags.Report(Loc, DiagID);
Douglas Gregor55abb232009-04-10 20:39:37 +00005461}
Douglas Gregora9af1d12009-04-17 00:04:06 +00005462
Douglas Gregora868bbd2009-04-21 22:25:48 +00005463/// \brief Retrieve the identifier table associated with the
5464/// preprocessor.
Sebastian Redl2c499f62010-08-18 23:56:43 +00005465IdentifierTable &ASTReader::getIdentifierTable() {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00005466 assert(PP && "Forgot to set Preprocessor ?");
5467 return PP->getIdentifierTable();
Douglas Gregora868bbd2009-04-21 22:25:48 +00005468}
5469
Douglas Gregora9af1d12009-04-17 00:04:06 +00005470/// \brief Record that the given ID maps to the given switch-case
5471/// statement.
Sebastian Redl2c499f62010-08-18 23:56:43 +00005472void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Douglas Gregora9af1d12009-04-17 00:04:06 +00005473 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
5474 SwitchCaseStmts[ID] = SC;
5475}
5476
5477/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00005478SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Douglas Gregora9af1d12009-04-17 00:04:06 +00005479 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
5480 return SwitchCaseStmts[ID];
5481}
Douglas Gregor6cc68a42009-04-17 18:18:49 +00005482
Argyrios Kyrtzidisd9f526f2010-10-28 09:29:32 +00005483void ASTReader::ClearSwitchCaseIDs() {
5484 SwitchCaseStmts.clear();
5485}
5486
Sebastian Redl2c499f62010-08-18 23:56:43 +00005487void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00005488 assert(NumCurrentElementsDeserializing &&
5489 "FinishedDeserializing not paired with StartedDeserializing");
5490 if (NumCurrentElementsDeserializing == 1) {
Douglas Gregor1342e842009-07-06 18:54:52 +00005491 // If any identifiers with corresponding top-level declarations have
5492 // been loaded, load those declarations now.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00005493 while (!PendingIdentifierInfos.empty()) {
5494 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
5495 PendingIdentifierInfos.front().DeclIDs, true);
5496 PendingIdentifierInfos.pop_front();
Douglas Gregor1342e842009-07-06 18:54:52 +00005497 }
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00005498
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00005499 // Ready to load previous declarations of Decls that were delayed.
5500 while (!PendingPreviousDecls.empty()) {
5501 loadAndAttachPreviousDecl(PendingPreviousDecls.front().first,
5502 PendingPreviousDecls.front().second);
5503 PendingPreviousDecls.pop_front();
5504 }
5505
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00005506 // We are not in recursive loading, so it's safe to pass the "interesting"
5507 // decls to the consumer.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00005508 if (Consumer)
5509 PassInterestingDeclsToConsumer();
Argyrios Kyrtzidisad5f95c2010-10-24 17:26:31 +00005510
5511 assert(PendingForwardRefs.size() == 0 &&
5512 "Some forward refs did not get linked to the definition!");
Douglas Gregor1342e842009-07-06 18:54:52 +00005513 }
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00005514 --NumCurrentElementsDeserializing;
Douglas Gregor1342e842009-07-06 18:54:52 +00005515}
Douglas Gregorb473b072010-08-19 00:28:17 +00005516
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005517ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
Douglas Gregorc567ba22011-07-22 16:35:34 +00005518 StringRef isysroot, bool DisableValidation,
Douglas Gregor606c4ac2011-02-05 19:42:43 +00005519 bool DisableStatCache)
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005520 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
5521 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
5522 Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
Jonathan D. Turnerecc27402011-07-28 17:20:23 +00005523 Consumer(0), ModuleMgr(FileMgr.getFileSystemOptions()),
5524 RelocatablePCH(false), isysroot(isysroot),
Douglas Gregor925296b2011-07-19 16:10:42 +00005525 DisableValidation(DisableValidation),
Douglas Gregor606c4ac2011-02-05 19:42:43 +00005526 DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0),
Douglas Gregor925296b2011-07-19 16:10:42 +00005527 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Douglas Gregor606c4ac2011-02-05 19:42:43 +00005528 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
5529 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
5530 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
5531 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
Jonathan D. Turner3766fdb2011-07-21 21:15:19 +00005532 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
5533 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
5534 NumCXXBaseSpecifiersLoaded(0)
Douglas Gregor606c4ac2011-02-05 19:42:43 +00005535{
Douglas Gregor925296b2011-07-19 16:10:42 +00005536 SourceMgr.setExternalSLocEntrySource(this);
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005537}
5538
5539ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
Douglas Gregorc567ba22011-07-22 16:35:34 +00005540 Diagnostic &Diags, StringRef isysroot,
Douglas Gregor606c4ac2011-02-05 19:42:43 +00005541 bool DisableValidation, bool DisableStatCache)
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005542 : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
Jonathan D. Turnerecc27402011-07-28 17:20:23 +00005543 Diags(Diags), SemaObj(0), PP(0), Context(0),
5544 Consumer(0), ModuleMgr(FileMgr.getFileSystemOptions()),
Douglas Gregor925296b2011-07-19 16:10:42 +00005545 RelocatablePCH(false), isysroot(isysroot),
5546 DisableValidation(DisableValidation), DisableStatCache(DisableStatCache),
5547 NumStatHits(0), NumStatMisses(0), NumSLocEntriesRead(0),
5548 TotalNumSLocEntries(0), NumStatementsRead(0),
5549 TotalNumStatements(0), NumMacrosRead(0), TotalNumMacros(0),
5550 NumSelectorsRead(0), NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
Sebastian Redlc1d035f2010-09-22 20:19:08 +00005551 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
5552 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
Jonathan D. Turner3766fdb2011-07-21 21:15:19 +00005553 TotalVisibleDeclContexts(0), TotalModulesSizeInBits(0),
5554 NumCurrentElementsDeserializing(0), NumCXXBaseSpecifiersLoaded(0)
Douglas Gregor925296b2011-07-19 16:10:42 +00005555{
5556 SourceMgr.setExternalSLocEntrySource(this);
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005557}
5558
5559ASTReader::~ASTReader() {
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005560 for (DeclContextVisibleUpdatesPending::iterator
5561 I = PendingVisibleUpdates.begin(),
5562 E = PendingVisibleUpdates.end();
5563 I != E; ++I) {
5564 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
5565 F = I->second.end();
5566 J != F; ++J)
Douglas Gregorf7180622011-08-03 15:48:04 +00005567 delete static_cast<ASTDeclContextNameLookupTable*>(J->first);
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005568 }
5569}