blob: 86625b7bdf1b934d8c8bb7102c9d399d8761158a [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);
157 PARSE_LANGOPT_BENIGN(ObjCInferRelatedReturnType);
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +0000158#undef PARSE_LANGOPT_IMPORTANT
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000159#undef PARSE_LANGOPT_BENIGN
160
161 return false;
162}
163
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000164bool PCHValidator::ReadTargetTriple(StringRef Triple) {
Daniel Dunbar20a682d2009-11-11 00:52:11 +0000165 if (Triple == PP.getTargetInfo().getTriple().str())
166 return false;
167
168 Reader.Diag(diag::warn_pch_target_triple)
169 << Triple << PP.getTargetInfo().getTriple().str();
170 return true;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000171}
172
Benjamin Kramer90b5b682010-11-25 18:29:30 +0000173namespace {
174 struct EmptyStringRef {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000175 bool operator ()(StringRef r) const { return r.empty(); }
Benjamin Kramer90b5b682010-11-25 18:29:30 +0000176 };
177 struct EmptyBlock {
178 bool operator ()(const PCHPredefinesBlock &r) const {return r.Data.empty();}
179 };
180}
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000181
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000182static bool EqualConcatenations(SmallVector<StringRef, 2> L,
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000183 PCHPredefinesBlocks R) {
184 // First, sum up the lengths.
185 unsigned LL = 0, RL = 0;
186 for (unsigned I = 0, N = L.size(); I != N; ++I) {
187 LL += L[I].size();
188 }
189 for (unsigned I = 0, N = R.size(); I != N; ++I) {
190 RL += R[I].Data.size();
191 }
192 if (LL != RL)
193 return false;
194 if (LL == 0 && RL == 0)
195 return true;
196
197 // Kick out empty parts, they confuse the algorithm below.
198 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
199 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
200
201 // Do it the hard way. At this point, both vectors must be non-empty.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000202 StringRef LR = L[0], RR = R[0].Data;
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000203 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
Daniel Dunbar01ad0a72010-07-16 00:00:11 +0000204 (void) RN;
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000205 for (;;) {
206 // Compare the current pieces.
207 if (LR.size() == RR.size()) {
208 // If they're the same length, it's pretty easy.
209 if (LR != RR)
210 return false;
211 // Both pieces are done, advance.
212 ++LI;
213 ++RI;
214 // If either string is done, they're both done, since they're the same
215 // length.
216 if (LI == LN) {
217 assert(RI == RN && "Strings not the same length after all?");
218 return true;
219 }
220 LR = L[LI];
221 RR = R[RI].Data;
222 } else if (LR.size() < RR.size()) {
223 // Right piece is longer.
224 if (!RR.startswith(LR))
225 return false;
226 ++LI;
227 assert(LI != LN && "Strings not the same length after all?");
228 RR = RR.substr(LR.size());
229 LR = L[LI];
230 } else {
231 // Left piece is longer.
232 if (!LR.startswith(RR))
233 return false;
234 ++RI;
235 assert(RI != RN && "Strings not the same length after all?");
236 LR = LR.substr(RR.size());
237 RR = R[RI].Data;
238 }
239 }
240}
241
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000242static std::pair<FileID, StringRef::size_type>
243FindMacro(const PCHPredefinesBlocks &Buffers, StringRef MacroDef) {
244 std::pair<FileID, StringRef::size_type> Res;
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000245 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
246 Res.second = Buffers[I].Data.find(MacroDef);
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000247 if (Res.second != StringRef::npos) {
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000248 Res.first = Buffers[I].BufferID;
249 break;
250 }
251 }
252 return Res;
253}
254
255bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000256 StringRef OriginalFileName,
Nick Lewycky36079892011-02-23 21:16:44 +0000257 std::string &SuggestedPredefines,
258 FileManager &FileMgr) {
Daniel Dunbar732ef8a2009-11-11 23:58:53 +0000259 // We are in the context of an implicit include, so the predefines buffer will
260 // have a #include entry for the PCH file itself (as normalized by the
261 // preprocessor initialization). Find it and skip over it in the checking
262 // below.
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000263 llvm::SmallString<256> PCHInclude;
264 PCHInclude += "#include \"";
Nick Lewycky36079892011-02-23 21:16:44 +0000265 PCHInclude += NormalizeDashIncludePath(OriginalFileName, FileMgr);
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000266 PCHInclude += "\"\n";
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000267 std::pair<StringRef,StringRef> Split =
268 StringRef(PP.getPredefines()).split(PCHInclude.str());
269 StringRef Left = Split.first, Right = Split.second;
Ted Kremenek1ff615c2010-03-18 00:56:54 +0000270 if (Left == PP.getPredefines()) {
271 Error("Missing PCH include entry!");
272 return true;
273 }
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000274
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000275 // If the concatenation of all the PCH buffers is equal to the adjusted
276 // command line, we're done.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000277 SmallVector<StringRef, 2> CommandLine;
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000278 CommandLine.push_back(Left);
279 CommandLine.push_back(Right);
280 if (EqualConcatenations(CommandLine, Buffers))
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000281 return false;
282
283 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump11289f42009-09-09 15:08:12 +0000284
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000285 // The predefines buffers are different. Determine what the differences are,
286 // and whether they require us to reject the PCH file.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000287 SmallVector<StringRef, 8> PCHLines;
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000288 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
289 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000290
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000291 SmallVector<StringRef, 8> CmdLineLines;
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000292 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis58c65412010-09-30 16:53:50 +0000293
294 // Pick out implicit #includes after the PCH and don't consider them for
295 // validation; we will insert them into SuggestedPredefines so that the
296 // preprocessor includes them.
297 std::string IncludesAfterPCH;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000298 SmallVector<StringRef, 8> AfterPCHLines;
Argyrios Kyrtzidis58c65412010-09-30 16:53:50 +0000299 Right.split(AfterPCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
300 for (unsigned i = 0, e = AfterPCHLines.size(); i != e; ++i) {
301 if (AfterPCHLines[i].startswith("#include ")) {
302 IncludesAfterPCH += AfterPCHLines[i];
303 IncludesAfterPCH += '\n';
304 } else {
305 CmdLineLines.push_back(AfterPCHLines[i]);
306 }
307 }
308
309 // Make sure we add the includes last into SuggestedPredefines before we
310 // exit this function.
311 struct AddIncludesRAII {
312 std::string &SuggestedPredefines;
313 std::string &IncludesAfterPCH;
314
315 AddIncludesRAII(std::string &SuggestedPredefines,
316 std::string &IncludesAfterPCH)
317 : SuggestedPredefines(SuggestedPredefines),
318 IncludesAfterPCH(IncludesAfterPCH) { }
319 ~AddIncludesRAII() {
320 SuggestedPredefines += IncludesAfterPCH;
321 }
322 } AddIncludes(SuggestedPredefines, IncludesAfterPCH);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000323
Daniel Dunbar499baed2009-11-11 05:26:28 +0000324 // Sort both sets of predefined buffer lines, since we allow some extra
325 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000326 std::sort(CmdLineLines.begin(), CmdLineLines.end());
327 std::sort(PCHLines.begin(), PCHLines.end());
328
Daniel Dunbar499baed2009-11-11 05:26:28 +0000329 // Determine which predefines that were used to build the PCH file are missing
330 // from the command line.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000331 std::vector<StringRef> MissingPredefines;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000332 std::set_difference(PCHLines.begin(), PCHLines.end(),
333 CmdLineLines.begin(), CmdLineLines.end(),
334 std::back_inserter(MissingPredefines));
335
336 bool MissingDefines = false;
337 bool ConflictingDefines = false;
338 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000339 StringRef Missing = MissingPredefines[I];
Argyrios Kyrtzidis58c65412010-09-30 16:53:50 +0000340 if (Missing.startswith("#include ")) {
341 // An -include was specified when generating the PCH; it is included in
342 // the PCH, just ignore it.
343 continue;
344 }
Daniel Dunbar499baed2009-11-11 05:26:28 +0000345 if (!Missing.startswith("#define ")) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000346 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
347 return true;
348 }
Mike Stump11289f42009-09-09 15:08:12 +0000349
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000350 // This is a macro definition. Determine the name of the macro we're
351 // defining.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000352 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump11289f42009-09-09 15:08:12 +0000353 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000354 = Missing.find_first_of("( \n\r", StartOfMacroName);
355 assert(EndOfMacroName != std::string::npos &&
356 "Couldn't find the end of the macro name");
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000357 StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000358
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000359 // Determine whether this macro was given a different definition on the
360 // command line.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000361 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000362 std::string::size_type MacroDefLen = MacroDefStart.size();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000363 SmallVector<StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000364 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
365 MacroDefStart);
366 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000367 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000368 // Different macro; we're done.
369 ConflictPos = CmdLineLines.end();
Mike Stump11289f42009-09-09 15:08:12 +0000370 break;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000371 }
Mike Stump11289f42009-09-09 15:08:12 +0000372
373 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000374 "Invalid #define in predefines buffer?");
Mike Stump11289f42009-09-09 15:08:12 +0000375 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000376 (*ConflictPos)[MacroDefLen] != '(')
377 continue; // Longer macro name; keep trying.
Mike Stump11289f42009-09-09 15:08:12 +0000378
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000379 // We found a conflicting macro definition.
380 break;
381 }
Mike Stump11289f42009-09-09 15:08:12 +0000382
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000383 if (ConflictPos != CmdLineLines.end()) {
384 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
385 << MacroName;
386
387 // Show the definition of this macro within the PCH file.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000388 std::pair<FileID, StringRef::size_type> MacroLoc =
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000389 FindMacro(Buffers, Missing);
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000390 assert(MacroLoc.second!=StringRef::npos && "Unable to find macro!");
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000391 SourceLocation PCHMissingLoc =
392 SourceMgr.getLocForStartOfFile(MacroLoc.first)
393 .getFileLocWithOffset(MacroLoc.second);
Daniel Dunbar499baed2009-11-11 05:26:28 +0000394 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000395
396 ConflictingDefines = true;
397 continue;
398 }
Mike Stump11289f42009-09-09 15:08:12 +0000399
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000400 // If the macro doesn't conflict, then we'll just pick up the macro
401 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000402 if (ConflictingDefines)
403 continue; // Don't complain if there are already conflicting defs
Mike Stump11289f42009-09-09 15:08:12 +0000404
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000405 if (!MissingDefines) {
406 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
407 MissingDefines = true;
408 }
409
410 // Show the definition of this macro within the PCH file.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000411 std::pair<FileID, StringRef::size_type> MacroLoc =
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000412 FindMacro(Buffers, Missing);
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000413 assert(MacroLoc.second!=StringRef::npos && "Unable to find macro!");
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000414 SourceLocation PCHMissingLoc =
415 SourceMgr.getLocForStartOfFile(MacroLoc.first)
416 .getFileLocWithOffset(MacroLoc.second);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000417 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
418 }
Mike Stump11289f42009-09-09 15:08:12 +0000419
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000420 if (ConflictingDefines)
421 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000422
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000423 // Determine what predefines were introduced based on command-line
424 // parameters that were not present when building the PCH
425 // file. Extra #defines are okay, so long as the identifiers being
426 // defined were not used within the precompiled header.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000427 std::vector<StringRef> ExtraPredefines;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000428 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
429 PCHLines.begin(), PCHLines.end(),
Mike Stump11289f42009-09-09 15:08:12 +0000430 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000431 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000432 StringRef &Extra = ExtraPredefines[I];
Daniel Dunbar499baed2009-11-11 05:26:28 +0000433 if (!Extra.startswith("#define ")) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000434 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
435 return true;
436 }
437
438 // This is an extra macro definition. Determine the name of the
439 // macro we're defining.
440 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump11289f42009-09-09 15:08:12 +0000441 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000442 = Extra.find_first_of("( \n\r", StartOfMacroName);
443 assert(EndOfMacroName != std::string::npos &&
444 "Couldn't find the end of the macro name");
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000445 StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000446
447 // Check whether this name was used somewhere in the PCH file. If
448 // so, defining it as a macro could change behavior, so we reject
449 // the PCH file.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000450 if (IdentifierInfo *II = Reader.get(MacroName)) {
Daniel Dunbar045c92f2009-11-11 00:52:00 +0000451 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000452 return true;
453 }
454
455 // Add this definition to the suggested predefines buffer.
456 SuggestedPredefines += Extra;
457 SuggestedPredefines += '\n';
458 }
459
460 // If we get here, it's because the predefines buffer had compatible
461 // contents. Accept the PCH file.
462 return false;
463}
464
Douglas Gregor5712ebc2010-03-16 16:35:32 +0000465void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
466 unsigned ID) {
467 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
468 ++NumHeaderInfos;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000469}
470
471void PCHValidator::ReadCounter(unsigned Value) {
472 PP.setCounterValue(Value);
473}
474
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000475//===----------------------------------------------------------------------===//
Sebastian Redl2c499f62010-08-18 23:56:43 +0000476// AST reader implementation
Douglas Gregora868bbd2009-04-21 22:25:48 +0000477//===----------------------------------------------------------------------===//
478
Sebastian Redl07a89a82010-07-30 00:29:29 +0000479void
Sebastian Redl3e31c722010-08-18 23:56:56 +0000480ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redl07a89a82010-07-30 00:29:29 +0000481 DeserializationListener = Listener;
Sebastian Redl07a89a82010-07-30 00:29:29 +0000482}
483
Chris Lattner92ba5ff2009-04-27 05:14:47 +0000484
Douglas Gregorc78d3462009-04-24 21:10:55 +0000485
Douglas Gregord44252e2011-08-25 20:47:51 +0000486unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
487 return serialization::ComputeHash(Sel);
488}
Douglas Gregorc78d3462009-04-24 21:10:55 +0000489
Mike Stump11289f42009-09-09 15:08:12 +0000490
Douglas Gregord44252e2011-08-25 20:47:51 +0000491std::pair<unsigned, unsigned>
492ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
493 using namespace clang::io;
494 unsigned KeyLen = ReadUnalignedLE16(d);
495 unsigned DataLen = ReadUnalignedLE16(d);
496 return std::make_pair(KeyLen, DataLen);
497}
498
499ASTSelectorLookupTrait::internal_key_type
500ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
501 using namespace clang::io;
502 SelectorTable &SelTable = Reader.getContext()->Selectors;
503 unsigned N = ReadUnalignedLE16(d);
504 IdentifierInfo *FirstII
505 = Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
506 if (N == 0)
507 return SelTable.getNullarySelector(FirstII);
508 else if (N == 1)
509 return SelTable.getUnarySelector(FirstII);
510
511 SmallVector<IdentifierInfo *, 16> Args;
512 Args.push_back(FirstII);
513 for (unsigned I = 1; I != N; ++I)
514 Args.push_back(Reader.getLocalIdentifier(F, ReadUnalignedLE32(d)));
515
516 return SelTable.getSelector(N, Args.data());
517}
518
519ASTSelectorLookupTrait::data_type
520ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
521 unsigned DataLen) {
522 using namespace clang::io;
523
524 data_type Result;
525
526 Result.ID = Reader.getGlobalSelectorID(F, ReadUnalignedLE32(d));
527 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
528 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
529
530 // Load instance methods
531 ObjCMethodList *Prev = 0;
532 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
533 if (ObjCMethodDecl *Method
534 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
535 Result.Instance.push_back(Method);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000536 }
Mike Stump11289f42009-09-09 15:08:12 +0000537
Douglas Gregord44252e2011-08-25 20:47:51 +0000538 // Load factory methods
539 Prev = 0;
540 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
541 if (ObjCMethodDecl *Method
542 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
543 Result.Factory.push_back(Method);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000544 }
Mike Stump11289f42009-09-09 15:08:12 +0000545
Douglas Gregord44252e2011-08-25 20:47:51 +0000546 return Result;
547}
Mike Stump11289f42009-09-09 15:08:12 +0000548
Douglas Gregord44252e2011-08-25 20:47:51 +0000549unsigned ASTIdentifierLookupTrait::ComputeHash(const internal_key_type& a) {
550 return llvm::HashString(StringRef(a.first, a.second));
551}
Mike Stump11289f42009-09-09 15:08:12 +0000552
Douglas Gregord44252e2011-08-25 20:47:51 +0000553std::pair<unsigned, unsigned>
554ASTIdentifierLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
555 using namespace clang::io;
556 unsigned DataLen = ReadUnalignedLE16(d);
557 unsigned KeyLen = ReadUnalignedLE16(d);
558 return std::make_pair(KeyLen, DataLen);
559}
Douglas Gregorc78d3462009-04-24 21:10:55 +0000560
Douglas Gregord44252e2011-08-25 20:47:51 +0000561std::pair<const char*, unsigned>
562ASTIdentifierLookupTrait::ReadKey(const unsigned char* d, unsigned n) {
563 assert(n >= 2 && d[n-1] == '\0');
564 return std::make_pair((const char*) d, n-1);
565}
Douglas Gregorc78d3462009-04-24 21:10:55 +0000566
Douglas Gregord44252e2011-08-25 20:47:51 +0000567IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
568 const unsigned char* d,
569 unsigned DataLen) {
570 using namespace clang::io;
571 unsigned RawID = ReadUnalignedLE32(d);
572 bool IsInteresting = RawID & 0x01;
Mike Stump11289f42009-09-09 15:08:12 +0000573
Douglas Gregord44252e2011-08-25 20:47:51 +0000574 // Wipe out the "is interesting" bit.
575 RawID = RawID >> 1;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000576
Douglas Gregord44252e2011-08-25 20:47:51 +0000577 IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
578 if (!IsInteresting) {
579 // For uninteresting identifiers, just build the IdentifierInfo
580 // and associate it with the persistent ID.
Douglas Gregora868bbd2009-04-21 22:25:48 +0000581 IdentifierInfo *II = KnownII;
582 if (!II)
Douglas Gregor1ab036c2011-08-03 21:49:18 +0000583 II = &Reader.getIdentifierTable().getOwn(StringRef(k.first, k.second));
Douglas Gregora868bbd2009-04-21 22:25:48 +0000584 Reader.SetIdentifierInfo(ID, II);
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000585 II->setIsFromAST();
Douglas Gregora868bbd2009-04-21 22:25:48 +0000586 return II;
587 }
Mike Stump11289f42009-09-09 15:08:12 +0000588
Douglas Gregord44252e2011-08-25 20:47:51 +0000589 unsigned Bits = ReadUnalignedLE16(d);
590 bool CPlusPlusOperatorKeyword = Bits & 0x01;
591 Bits >>= 1;
592 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
593 Bits >>= 1;
594 bool Poisoned = Bits & 0x01;
595 Bits >>= 1;
596 bool ExtensionToken = Bits & 0x01;
597 Bits >>= 1;
598 bool hasMacroDefinition = Bits & 0x01;
599 Bits >>= 1;
600 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
601 Bits >>= 10;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000602
Douglas Gregord44252e2011-08-25 20:47:51 +0000603 assert(Bits == 0 && "Extra bits in the identifier?");
604 DataLen -= 6;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000605
Douglas Gregord44252e2011-08-25 20:47:51 +0000606 // Build the IdentifierInfo itself and link the identifier ID with
607 // the new IdentifierInfo.
608 IdentifierInfo *II = KnownII;
609 if (!II)
610 II = &Reader.getIdentifierTable().getOwn(StringRef(k.first, k.second));
611 Reader.SetIdentifierInfo(ID, II);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000612
Douglas Gregord44252e2011-08-25 20:47:51 +0000613 // Set or check the various bits in the IdentifierInfo structure.
614 // Token IDs are read-only.
615 if (HasRevertedTokenIDToIdentifier)
616 II->RevertTokenIDToIdentifier();
617 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
618 assert(II->isExtensionToken() == ExtensionToken &&
619 "Incorrect extension token flag");
620 (void)ExtensionToken;
621 if (Poisoned)
622 II->setIsPoisoned(true);
623 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
624 "Incorrect C++ operator keyword flag");
625 (void)CPlusPlusOperatorKeyword;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000626
Douglas Gregord44252e2011-08-25 20:47:51 +0000627 // If this identifier is a macro, deserialize the macro
628 // definition.
629 if (hasMacroDefinition) {
630 // FIXME: Check for conflicts?
631 uint32_t Offset = ReadUnalignedLE32(d);
632 Reader.SetIdentifierIsMacro(II, F, Offset);
633 DataLen -= 4;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000634 }
635
Douglas Gregord44252e2011-08-25 20:47:51 +0000636 // Read all of the declarations visible at global scope with this
637 // name.
638 if (Reader.getContext() == 0) return II;
639 if (DataLen > 0) {
640 SmallVector<uint32_t, 4> DeclIDs;
641 for (; DataLen > 0; DataLen -= 4)
642 DeclIDs.push_back(Reader.getGlobalDeclID(F, ReadUnalignedLE32(d)));
643 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000644 }
645
Douglas Gregord44252e2011-08-25 20:47:51 +0000646 II->setIsFromAST();
647 return II;
648}
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000649
Douglas Gregord44252e2011-08-25 20:47:51 +0000650unsigned
651ASTDeclContextNameLookupTrait::ComputeHash(const DeclNameKey &Key) const {
652 llvm::FoldingSetNodeID ID;
653 ID.AddInteger(Key.Kind);
654
655 switch (Key.Kind) {
656 case DeclarationName::Identifier:
657 case DeclarationName::CXXLiteralOperatorName:
658 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
659 break;
660 case DeclarationName::ObjCZeroArgSelector:
661 case DeclarationName::ObjCOneArgSelector:
662 case DeclarationName::ObjCMultiArgSelector:
663 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
664 break;
665 case DeclarationName::CXXOperatorName:
666 ID.AddInteger((OverloadedOperatorKind)Key.Data);
667 break;
668 case DeclarationName::CXXConstructorName:
669 case DeclarationName::CXXDestructorName:
670 case DeclarationName::CXXConversionFunctionName:
671 case DeclarationName::CXXUsingDirective:
672 break;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000673 }
674
Douglas Gregord44252e2011-08-25 20:47:51 +0000675 return ID.ComputeHash();
676}
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +0000677
Douglas Gregord44252e2011-08-25 20:47:51 +0000678ASTDeclContextNameLookupTrait::internal_key_type
679ASTDeclContextNameLookupTrait::GetInternalKey(
680 const external_key_type& Name) const {
681 DeclNameKey Key;
682 Key.Kind = Name.getNameKind();
683 switch (Name.getNameKind()) {
684 case DeclarationName::Identifier:
685 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
686 break;
687 case DeclarationName::ObjCZeroArgSelector:
688 case DeclarationName::ObjCOneArgSelector:
689 case DeclarationName::ObjCMultiArgSelector:
690 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
691 break;
692 case DeclarationName::CXXOperatorName:
693 Key.Data = Name.getCXXOverloadedOperator();
694 break;
695 case DeclarationName::CXXLiteralOperatorName:
696 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
697 break;
698 case DeclarationName::CXXConstructorName:
699 case DeclarationName::CXXDestructorName:
700 case DeclarationName::CXXConversionFunctionName:
701 case DeclarationName::CXXUsingDirective:
702 Key.Data = 0;
703 break;
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +0000704 }
705
Douglas Gregord44252e2011-08-25 20:47:51 +0000706 return Key;
707}
708
709ASTDeclContextNameLookupTrait::external_key_type
710ASTDeclContextNameLookupTrait::GetExternalKey(
711 const internal_key_type& Key) const {
712 ASTContext *Context = Reader.getContext();
713 switch (Key.Kind) {
714 case DeclarationName::Identifier:
715 return DeclarationName((IdentifierInfo*)Key.Data);
716
717 case DeclarationName::ObjCZeroArgSelector:
718 case DeclarationName::ObjCOneArgSelector:
719 case DeclarationName::ObjCMultiArgSelector:
720 return DeclarationName(Selector(Key.Data));
721
722 case DeclarationName::CXXConstructorName:
723 return Context->DeclarationNames.getCXXConstructorName(
724 Context->getCanonicalType(Reader.getLocalType(F, Key.Data)));
725
726 case DeclarationName::CXXDestructorName:
727 return Context->DeclarationNames.getCXXDestructorName(
728 Context->getCanonicalType(Reader.getLocalType(F, Key.Data)));
729
730 case DeclarationName::CXXConversionFunctionName:
731 return Context->DeclarationNames.getCXXConversionFunctionName(
732 Context->getCanonicalType(Reader.getLocalType(F, Key.Data)));
733
734 case DeclarationName::CXXOperatorName:
735 return Context->DeclarationNames.getCXXOperatorName(
736 (OverloadedOperatorKind)Key.Data);
737
738 case DeclarationName::CXXLiteralOperatorName:
739 return Context->DeclarationNames.getCXXLiteralOperatorName(
740 (IdentifierInfo*)Key.Data);
741
742 case DeclarationName::CXXUsingDirective:
743 return DeclarationName::getUsingDirectiveName();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000744 }
745
Douglas Gregord44252e2011-08-25 20:47:51 +0000746 llvm_unreachable("Invalid Name Kind ?");
747}
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000748
Douglas Gregord44252e2011-08-25 20:47:51 +0000749std::pair<unsigned, unsigned>
750ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
751 using namespace clang::io;
752 unsigned KeyLen = ReadUnalignedLE16(d);
753 unsigned DataLen = ReadUnalignedLE16(d);
754 return std::make_pair(KeyLen, DataLen);
755}
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000756
Douglas Gregord44252e2011-08-25 20:47:51 +0000757ASTDeclContextNameLookupTrait::internal_key_type
758ASTDeclContextNameLookupTrait::ReadKey(const unsigned char* d, unsigned) {
759 using namespace clang::io;
760
761 DeclNameKey Key;
762 Key.Kind = (DeclarationName::NameKind)*d++;
763 switch (Key.Kind) {
764 case DeclarationName::Identifier:
765 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
766 break;
767 case DeclarationName::ObjCZeroArgSelector:
768 case DeclarationName::ObjCOneArgSelector:
769 case DeclarationName::ObjCMultiArgSelector:
770 Key.Data =
771 (uint64_t)Reader.getLocalSelector(F, ReadUnalignedLE32(d))
772 .getAsOpaquePtr();
773 break;
774 case DeclarationName::CXXOperatorName:
775 Key.Data = *d++; // OverloadedOperatorKind
776 break;
777 case DeclarationName::CXXLiteralOperatorName:
778 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
779 break;
780 case DeclarationName::CXXConstructorName:
781 case DeclarationName::CXXDestructorName:
782 case DeclarationName::CXXConversionFunctionName:
783 case DeclarationName::CXXUsingDirective:
784 Key.Data = 0;
785 break;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000786 }
787
Douglas Gregord44252e2011-08-25 20:47:51 +0000788 return Key;
789}
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000790
Douglas Gregord44252e2011-08-25 20:47:51 +0000791ASTDeclContextNameLookupTrait::data_type
792ASTDeclContextNameLookupTrait::ReadData(internal_key_type,
793 const unsigned char* d,
794 unsigned DataLen) {
795 using namespace clang::io;
796 unsigned NumDecls = ReadUnalignedLE16(d);
797 DeclID *Start = (DeclID *)d;
798 return std::make_pair(Start, Start + NumDecls);
799}
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000800
Douglas Gregor94619c82011-08-24 19:03:07 +0000801bool ASTReader::ReadDeclContextStorage(Module &M,
802 llvm::BitstreamCursor &Cursor,
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000803 const std::pair<uint64_t, uint64_t> &Offsets,
804 DeclContextInfo &Info) {
805 SavedStreamPosition SavedPosition(Cursor);
806 // First the lexical decls.
807 if (Offsets.first != 0) {
808 Cursor.JumpToBit(Offsets.first);
809
810 RecordData Record;
811 const char *Blob;
812 unsigned BlobLen;
813 unsigned Code = Cursor.ReadCode();
814 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
815 if (RecCode != DECL_CONTEXT_LEXICAL) {
816 Error("Expected lexical block");
817 return true;
818 }
819
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000820 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
821 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000822 }
823
824 // Now the lookup table.
825 if (Offsets.second != 0) {
826 Cursor.JumpToBit(Offsets.second);
827
828 RecordData Record;
829 const char *Blob;
830 unsigned BlobLen;
831 unsigned Code = Cursor.ReadCode();
832 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
833 if (RecCode != DECL_CONTEXT_VISIBLE) {
834 Error("Expected visible lookup table block");
835 return true;
836 }
837 Info.NameLookupTableData
838 = ASTDeclContextNameLookupTable::Create(
839 (const unsigned char *)Blob + Record[0],
840 (const unsigned char *)Blob,
Douglas Gregor94619c82011-08-24 19:03:07 +0000841 ASTDeclContextNameLookupTrait(*this, M));
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000842 }
843
844 return false;
845}
846
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000847void ASTReader::Error(StringRef Msg) {
Argyrios Kyrtzidisdaa41f52011-04-25 22:23:56 +0000848 Error(diag::err_fe_pch_malformed, Msg);
849}
850
851void ASTReader::Error(unsigned DiagID,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000852 StringRef Arg1, StringRef Arg2) {
Argyrios Kyrtzidisdaa41f52011-04-25 22:23:56 +0000853 if (Diags.isDiagnosticInFlight())
854 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
855 else
856 Diag(DiagID) << Arg1 << Arg2;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000857}
858
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000859/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redl2c499f62010-08-18 23:56:43 +0000860bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000861 if (Listener)
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000862 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000863 ActualOriginalFileName,
Nick Lewycky36079892011-02-23 21:16:44 +0000864 SuggestedPredefines,
865 FileMgr);
Douglas Gregorc379c072009-04-28 18:58:38 +0000866 return false;
Douglas Gregor92863e42009-04-10 23:10:45 +0000867}
868
Douglas Gregorc5046832009-04-27 18:38:38 +0000869//===----------------------------------------------------------------------===//
870// Source Manager Deserialization
871//===----------------------------------------------------------------------===//
872
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000873/// \brief Read the line table in the source manager block.
Sebastian Redl2c373b92010-10-05 15:59:54 +0000874/// \returns true if there was an error.
Douglas Gregora6895d82011-07-22 16:00:58 +0000875bool ASTReader::ParseLineTable(Module &F,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000876 SmallVectorImpl<uint64_t> &Record) {
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000877 unsigned Idx = 0;
878 LineTableInfo &LineTable = SourceMgr.getLineTable();
879
880 // Parse the file names
Douglas Gregora8854652009-04-13 17:12:42 +0000881 std::map<int, int> FileIDs;
882 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000883 // Extract the file name
884 unsigned FilenameLen = Record[Idx++];
885 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
886 Idx += FilenameLen;
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000887 MaybeAddSystemRootToFilename(Filename);
Jay Foad9a6b0982011-06-21 15:13:30 +0000888 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000889 }
890
891 // Parse the line entries
892 std::vector<LineEntry> Entries;
893 while (Idx < Record.size()) {
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000894 int FID = Record[Idx++];
Douglas Gregor925296b2011-07-19 16:10:42 +0000895 assert(FID >= 0 && "Serialized line entries for non-local file.");
896 // Remap FileID from 1-based old view.
897 FID += F.SLocEntryBaseID - 1;
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000898
899 // Extract the line entries
900 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000901 assert(NumEntries && "Numentries is 00000");
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000902 Entries.clear();
903 Entries.reserve(NumEntries);
904 for (unsigned I = 0; I != NumEntries; ++I) {
905 unsigned FileOffset = Record[Idx++];
906 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000907 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump11289f42009-09-09 15:08:12 +0000908 SrcMgr::CharacteristicKind FileKind
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000909 = (SrcMgr::CharacteristicKind)Record[Idx++];
910 unsigned IncludeOffset = Record[Idx++];
911 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
912 FileKind, IncludeOffset));
913 }
914 LineTable.AddEntry(FID, Entries);
915 }
916
917 return false;
918}
919
Douglas Gregorc5046832009-04-27 18:38:38 +0000920namespace {
921
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000922class ASTStatData {
Douglas Gregorc5046832009-04-27 18:38:38 +0000923public:
Douglas Gregorc5046832009-04-27 18:38:38 +0000924 const ino_t ino;
925 const dev_t dev;
926 const mode_t mode;
927 const time_t mtime;
928 const off_t size;
Mike Stump11289f42009-09-09 15:08:12 +0000929
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000930 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Chris Lattner2a6fa472010-11-23 19:28:12 +0000931 : ino(i), dev(d), mode(mo), mtime(m), size(s) {}
Douglas Gregorc5046832009-04-27 18:38:38 +0000932};
933
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000934class ASTStatLookupTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +0000935 public:
936 typedef const char *external_key_type;
937 typedef const char *internal_key_type;
938
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000939 typedef ASTStatData data_type;
Douglas Gregorc5046832009-04-27 18:38:38 +0000940
941 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000942 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +0000943 }
944
945 static internal_key_type GetInternalKey(const char *path) { return path; }
946
947 static bool EqualKey(internal_key_type a, internal_key_type b) {
948 return strcmp(a, b) == 0;
949 }
950
951 static std::pair<unsigned, unsigned>
952 ReadKeyDataLength(const unsigned char*& d) {
953 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
954 unsigned DataLen = (unsigned) *d++;
955 return std::make_pair(KeyLen + 1, DataLen);
956 }
957
958 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
959 return (const char *)d;
960 }
961
962 static data_type ReadData(const internal_key_type, const unsigned char *d,
963 unsigned /*DataLen*/) {
964 using namespace clang::io;
965
Douglas Gregorc5046832009-04-27 18:38:38 +0000966 ino_t ino = (ino_t) ReadUnalignedLE32(d);
967 dev_t dev = (dev_t) ReadUnalignedLE32(d);
968 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump11289f42009-09-09 15:08:12 +0000969 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregorc5046832009-04-27 18:38:38 +0000970 off_t size = (off_t) ReadUnalignedLE64(d);
971 return data_type(ino, dev, mode, mtime, size);
972 }
973};
974
975/// \brief stat() cache for precompiled headers.
976///
977/// This cache is very similar to the stat cache used by pretokenized
978/// headers.
Chris Lattner226efd32010-11-23 19:19:34 +0000979class ASTStatCache : public FileSystemStatCache {
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000980 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregorc5046832009-04-27 18:38:38 +0000981 CacheTy *Cache;
982
983 unsigned &NumStatHits, &NumStatMisses;
Mike Stump11289f42009-09-09 15:08:12 +0000984public:
Chris Lattner2a6fa472010-11-23 19:28:12 +0000985 ASTStatCache(const unsigned char *Buckets, const unsigned char *Base,
986 unsigned &NumStatHits, unsigned &NumStatMisses)
Douglas Gregorc5046832009-04-27 18:38:38 +0000987 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
988 Cache = CacheTy::Create(Buckets, Base);
989 }
990
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000991 ~ASTStatCache() { delete Cache; }
Mike Stump11289f42009-09-09 15:08:12 +0000992
Chris Lattnerdd278432010-11-23 21:17:56 +0000993 LookupResult getStat(const char *Path, struct stat &StatBuf,
994 int *FileDescriptor) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000995 // Do the lookup for the file's data in the AST file.
Chris Lattner226efd32010-11-23 19:19:34 +0000996 CacheTy::iterator I = Cache->find(Path);
Douglas Gregorc5046832009-04-27 18:38:38 +0000997
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000998 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregorc5046832009-04-27 18:38:38 +0000999 if (I == Cache->end()) {
1000 ++NumStatMisses;
Chris Lattnerdd278432010-11-23 21:17:56 +00001001 return statChained(Path, StatBuf, FileDescriptor);
Douglas Gregorc5046832009-04-27 18:38:38 +00001002 }
Mike Stump11289f42009-09-09 15:08:12 +00001003
Douglas Gregorc5046832009-04-27 18:38:38 +00001004 ++NumStatHits;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001005 ASTStatData Data = *I;
Mike Stump11289f42009-09-09 15:08:12 +00001006
Chris Lattner226efd32010-11-23 19:19:34 +00001007 StatBuf.st_ino = Data.ino;
1008 StatBuf.st_dev = Data.dev;
1009 StatBuf.st_mtime = Data.mtime;
1010 StatBuf.st_mode = Data.mode;
1011 StatBuf.st_size = Data.size;
Chris Lattner8f0583d2010-11-23 20:05:15 +00001012 return CacheExists;
Douglas Gregorc5046832009-04-27 18:38:38 +00001013 }
1014};
1015} // end anonymous namespace
1016
1017
Sebastian Redl393f8b72010-07-19 20:52:06 +00001018/// \brief Read a source manager block
Douglas Gregora6895d82011-07-22 16:00:58 +00001019ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(Module &F) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001020 using namespace SrcMgr;
Douglas Gregor258ae542009-04-27 06:38:32 +00001021
Sebastian Redl393f8b72010-07-19 20:52:06 +00001022 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001023
Douglas Gregor258ae542009-04-27 06:38:32 +00001024 // Set the source-location entry cursor to the current position in
1025 // the stream. This cursor will be used to read the contents of the
1026 // source manager block initially, and then lazily read
1027 // source-location entries as needed.
Sebastian Redl393f8b72010-07-19 20:52:06 +00001028 SLocEntryCursor = F.Stream;
Douglas Gregor258ae542009-04-27 06:38:32 +00001029
1030 // The stream itself is going to skip over the source manager block.
Sebastian Redl393f8b72010-07-19 20:52:06 +00001031 if (F.Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001032 Error("malformed block record in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001033 return Failure;
1034 }
1035
1036 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001037 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001038 Error("malformed source manager block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001039 return Failure;
1040 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001041
Douglas Gregora7f71a92009-04-10 03:52:48 +00001042 RecordData Record;
1043 while (true) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001044 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregora7f71a92009-04-10 03:52:48 +00001045 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001046 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001047 Error("error at end of Source Manager block in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001048 return Failure;
1049 }
Douglas Gregor92863e42009-04-10 23:10:45 +00001050 return Success;
Douglas Gregora7f71a92009-04-10 03:52:48 +00001051 }
Mike Stump11289f42009-09-09 15:08:12 +00001052
Douglas Gregora7f71a92009-04-10 03:52:48 +00001053 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1054 // No known subblocks, always skip them.
Douglas Gregor258ae542009-04-27 06:38:32 +00001055 SLocEntryCursor.ReadSubBlockID();
1056 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001057 Error("malformed block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001058 return Failure;
1059 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001060 continue;
1061 }
Mike Stump11289f42009-09-09 15:08:12 +00001062
Douglas Gregora7f71a92009-04-10 03:52:48 +00001063 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001064 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregora7f71a92009-04-10 03:52:48 +00001065 continue;
1066 }
Mike Stump11289f42009-09-09 15:08:12 +00001067
Douglas Gregora7f71a92009-04-10 03:52:48 +00001068 // Read a record.
1069 const char *BlobStart;
1070 unsigned BlobLen;
1071 Record.clear();
Douglas Gregor258ae542009-04-27 06:38:32 +00001072 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001073 default: // Default behavior: ignore.
1074 break;
1075
Sebastian Redl539c5062010-08-18 23:57:32 +00001076 case SM_SLOC_FILE_ENTRY:
1077 case SM_SLOC_BUFFER_ENTRY:
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001078 case SM_SLOC_EXPANSION_ENTRY:
Douglas Gregor258ae542009-04-27 06:38:32 +00001079 // Once we hit one of the source location entries, we're done.
1080 return Success;
Douglas Gregora7f71a92009-04-10 03:52:48 +00001081 }
1082 }
1083}
1084
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001085/// \brief If a header file is not found at the path that we expect it to be
1086/// and the PCH file was moved from its original location, try to resolve the
1087/// file by assuming that header+PCH were moved together and the header is in
1088/// the same place relative to the PCH.
1089static std::string
1090resolveFileRelativeToOriginalDir(const std::string &Filename,
1091 const std::string &OriginalDir,
1092 const std::string &CurrDir) {
1093 assert(OriginalDir != CurrDir &&
1094 "No point trying to resolve the file if the PCH dir didn't change");
1095 using namespace llvm::sys;
1096 llvm::SmallString<128> filePath(Filename);
1097 fs::make_absolute(filePath);
1098 assert(path::is_absolute(OriginalDir));
1099 llvm::SmallString<128> currPCHPath(CurrDir);
1100
1101 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1102 fileDirE = path::end(path::parent_path(filePath));
1103 path::const_iterator origDirI = path::begin(OriginalDir),
1104 origDirE = path::end(OriginalDir);
1105 // Skip the common path components from filePath and OriginalDir.
1106 while (fileDirI != fileDirE && origDirI != origDirE &&
1107 *fileDirI == *origDirI) {
1108 ++fileDirI;
1109 ++origDirI;
1110 }
1111 for (; origDirI != origDirE; ++origDirI)
1112 path::append(currPCHPath, "..");
1113 path::append(currPCHPath, fileDirI, fileDirE);
1114 path::append(currPCHPath, path::filename(Filename));
1115 return currPCHPath.str();
1116}
1117
Douglas Gregor258ae542009-04-27 06:38:32 +00001118/// \brief Read in the source location entry with the given ID.
Douglas Gregor925296b2011-07-19 16:10:42 +00001119ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(int ID) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001120 if (ID == 0)
1121 return Success;
1122
Douglas Gregor49bf76b2011-07-21 18:46:38 +00001123 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001124 Error("source location entry ID out-of-range for AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001125 return Failure;
1126 }
1127
Douglas Gregora6895d82011-07-22 16:00:58 +00001128 Module *F = GlobalSLocEntryMap.find(-ID)->second;
Douglas Gregor925296b2011-07-19 16:10:42 +00001129 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00001130 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Douglas Gregor925296b2011-07-19 16:10:42 +00001131 unsigned BaseOffset = F->SLocEntryBaseOffset;
Sebastian Redl34522812010-07-16 17:50:48 +00001132
Douglas Gregor258ae542009-04-27 06:38:32 +00001133 ++NumSLocEntriesRead;
Douglas Gregor258ae542009-04-27 06:38:32 +00001134 unsigned Code = SLocEntryCursor.ReadCode();
1135 if (Code == llvm::bitc::END_BLOCK ||
1136 Code == llvm::bitc::ENTER_SUBBLOCK ||
1137 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001138 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001139 return Failure;
1140 }
1141
Douglas Gregor258ae542009-04-27 06:38:32 +00001142 RecordData Record;
1143 const char *BlobStart;
1144 unsigned BlobLen;
1145 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1146 default:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001147 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001148 return Failure;
1149
Sebastian Redl539c5062010-08-18 23:57:32 +00001150 case SM_SLOC_FILE_ENTRY: {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001151 std::string Filename(BlobStart, BlobStart + BlobLen);
1152 MaybeAddSystemRootToFilename(Filename);
Chris Lattner5159f612010-11-23 08:35:12 +00001153 const FileEntry *File = FileMgr.getFile(Filename);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001154 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1155 OriginalDir != CurrentDir) {
1156 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1157 OriginalDir,
1158 CurrentDir);
1159 if (!resolved.empty())
1160 File = FileMgr.getFile(resolved);
1161 }
Axel Naumann63fbaed2011-01-27 10:55:51 +00001162 if (File == 0)
1163 File = FileMgr.getVirtualFile(Filename, (off_t)Record[4],
1164 (time_t)Record[5]);
Chris Lattnerd20dc872009-06-15 04:35:16 +00001165 if (File == 0) {
1166 std::string ErrorStr = "could not find file '";
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001167 ErrorStr += Filename;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001168 ErrorStr += "' referenced by AST file";
Chris Lattnerd20dc872009-06-15 04:35:16 +00001169 Error(ErrorStr.c_str());
1170 return Failure;
1171 }
Mike Stump11289f42009-09-09 15:08:12 +00001172
Douglas Gregor09b69892011-02-10 17:09:37 +00001173 if (Record.size() < 6) {
Ted Kremenekabb1ddd2010-03-18 21:23:05 +00001174 Error("source location entry is incorrect");
1175 return Failure;
1176 }
1177
Douglas Gregorce3a8292010-07-27 00:27:13 +00001178 if (!DisableValidation &&
1179 ((off_t)Record[4] != File->getSize()
Douglas Gregor08288f22010-04-09 15:54:22 +00001180#if !defined(LLVM_ON_WIN32)
1181 // In our regression testing, the Windows file system seems to
1182 // have inconsistent modification times that sometimes
1183 // erroneously trigger this error-handling path.
Douglas Gregorce3a8292010-07-27 00:27:13 +00001184 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor08288f22010-04-09 15:54:22 +00001185#endif
Douglas Gregorce3a8292010-07-27 00:27:13 +00001186 )) {
Argyrios Kyrtzidisdaa41f52011-04-25 22:23:56 +00001187 Error(diag::err_fe_pch_file_modified, Filename);
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001188 return Failure;
1189 }
1190
Douglas Gregor925296b2011-07-19 16:10:42 +00001191 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregora6895d82011-07-22 16:00:58 +00001192 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
Douglas Gregor925296b2011-07-19 16:10:42 +00001193 // This is the module's main file.
1194 IncludeLoc = getImportLocation(F);
1195 }
1196 FileID FID = SourceMgr.createFileID(File, IncludeLoc,
Chris Lattner26b5c192010-11-23 09:19:42 +00001197 (SrcMgr::CharacteristicKind)Record[2],
Douglas Gregor925296b2011-07-19 16:10:42 +00001198 ID, BaseOffset + Record[0]);
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001199 SrcMgr::FileInfo &FileInfo =
1200 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1201 FileInfo.NumCreatedFIDs = Record[6];
Douglas Gregor258ae542009-04-27 06:38:32 +00001202 if (Record[3])
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001203 FileInfo.setHasLineDirectives();
Douglas Gregor09b69892011-02-10 17:09:37 +00001204
Douglas Gregor258ae542009-04-27 06:38:32 +00001205 break;
1206 }
1207
Sebastian Redl539c5062010-08-18 23:57:32 +00001208 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor258ae542009-04-27 06:38:32 +00001209 const char *Name = BlobStart;
1210 unsigned Offset = Record[0];
1211 unsigned Code = SLocEntryCursor.ReadCode();
1212 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00001213 unsigned RecCode
Douglas Gregor258ae542009-04-27 06:38:32 +00001214 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001215
Sebastian Redl539c5062010-08-18 23:57:32 +00001216 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001217 Error("AST record has invalid code");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001218 return Failure;
1219 }
1220
Douglas Gregor258ae542009-04-27 06:38:32 +00001221 llvm::MemoryBuffer *Buffer
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001222 = llvm::MemoryBuffer::getMemBuffer(StringRef(BlobStart, BlobLen - 1),
Chris Lattner58c79342010-04-05 22:42:27 +00001223 Name);
Douglas Gregor925296b2011-07-19 16:10:42 +00001224 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID,
1225 BaseOffset + Offset);
Mike Stump11289f42009-09-09 15:08:12 +00001226
Douglas Gregore6648fb2009-04-28 20:33:11 +00001227 if (strcmp(Name, "<built-in>") == 0) {
Sebastian Redl75fbb3b2010-07-14 17:49:11 +00001228 PCHPredefinesBlock Block = {
1229 BufferID,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001230 StringRef(BlobStart, BlobLen - 1)
Sebastian Redl75fbb3b2010-07-14 17:49:11 +00001231 };
1232 PCHPredefinesBuffers.push_back(Block);
Douglas Gregore6648fb2009-04-28 20:33:11 +00001233 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001234
1235 break;
1236 }
1237
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001238 case SM_SLOC_EXPANSION_ENTRY: {
Sebastian Redl2c373b92010-10-05 15:59:54 +00001239 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Chandler Carruth115b0772011-07-26 03:03:05 +00001240 SourceMgr.createExpansionLoc(SpellingLoc,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001241 ReadSourceLocation(*F, Record[2]),
1242 ReadSourceLocation(*F, Record[3]),
Douglas Gregor258ae542009-04-27 06:38:32 +00001243 Record[4],
1244 ID,
Douglas Gregor925296b2011-07-19 16:10:42 +00001245 BaseOffset + Record[0]);
Douglas Gregor258ae542009-04-27 06:38:32 +00001246 break;
Mike Stump11289f42009-09-09 15:08:12 +00001247 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001248 }
1249
1250 return Success;
1251}
1252
Douglas Gregor925296b2011-07-19 16:10:42 +00001253/// \brief Find the location where the module F is imported.
Douglas Gregora6895d82011-07-22 16:00:58 +00001254SourceLocation ASTReader::getImportLocation(Module *F) {
Douglas Gregor925296b2011-07-19 16:10:42 +00001255 if (F->ImportLoc.isValid())
1256 return F->ImportLoc;
Jonathan D. Turner10d52012011-07-29 18:09:09 +00001257
Douglas Gregor925296b2011-07-19 16:10:42 +00001258 // Otherwise we have a PCH. It's considered to be "imported" at the first
1259 // location of its includer.
Jonathan D. Turner10d52012011-07-29 18:09:09 +00001260 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
Douglas Gregor925296b2011-07-19 16:10:42 +00001261 // Main file is the importer. We assume that it is the first entry in the
1262 // entry table. We can't ask the manager, because at the time of PCH loading
1263 // the main file entry doesn't exist yet.
1264 // The very first entry is the invalid instantiation loc, which takes up
1265 // offsets 0 and 1.
1266 return SourceLocation::getFromRawEncoding(2U);
1267 }
Jonathan D. Turner10d52012011-07-29 18:09:09 +00001268 //return F->Loaders[0]->FirstLoc;
1269 return F->ImportedBy[0]->FirstLoc;
Douglas Gregor925296b2011-07-19 16:10:42 +00001270}
1271
Chris Lattnere78a6be2009-04-27 01:05:14 +00001272/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1273/// specified cursor. Read the abbreviations that are at the top of the block
1274/// and then leave the cursor pointing into the block.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001275bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattnere78a6be2009-04-27 01:05:14 +00001276 unsigned BlockID) {
1277 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001278 Error("malformed block record in AST file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001279 return Failure;
1280 }
Mike Stump11289f42009-09-09 15:08:12 +00001281
Chris Lattnere78a6be2009-04-27 01:05:14 +00001282 while (true) {
Douglas Gregor796d76a2010-10-20 22:00:55 +00001283 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattnere78a6be2009-04-27 01:05:14 +00001284 unsigned Code = Cursor.ReadCode();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001285
Chris Lattnere78a6be2009-04-27 01:05:14 +00001286 // We expect all abbrevs to be at the start of the block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001287 if (Code != llvm::bitc::DEFINE_ABBREV) {
1288 Cursor.JumpToBit(Offset);
Chris Lattnere78a6be2009-04-27 01:05:14 +00001289 return false;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001290 }
Chris Lattnere78a6be2009-04-27 01:05:14 +00001291 Cursor.ReadAbbrevRecord();
1292 }
1293}
1294
Douglas Gregor7cb0d012011-08-04 18:09:14 +00001295void ASTReader::ReadMacroRecord(Module &F, uint64_t Offset) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001296 assert(PP && "Forgot to set Preprocessor ?");
Douglas Gregor796d76a2010-10-20 22:00:55 +00001297 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump11289f42009-09-09 15:08:12 +00001298
Douglas Gregorc3366a52009-04-21 23:56:24 +00001299 // Keep track of where we are in the stream, then jump back there
1300 // after reading this macro.
1301 SavedStreamPosition SavedPosition(Stream);
1302
1303 Stream.JumpToBit(Offset);
1304 RecordData Record;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001305 SmallVector<IdentifierInfo*, 16> MacroArgs;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001306 MacroInfo *Macro = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001307
Douglas Gregorc3366a52009-04-21 23:56:24 +00001308 while (true) {
1309 unsigned Code = Stream.ReadCode();
1310 switch (Code) {
1311 case llvm::bitc::END_BLOCK:
Douglas Gregor7cb0d012011-08-04 18:09:14 +00001312 return;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001313
1314 case llvm::bitc::ENTER_SUBBLOCK:
1315 // No known subblocks, always skip them.
1316 Stream.ReadSubBlockID();
1317 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001318 Error("malformed block record in AST file");
Douglas Gregor7cb0d012011-08-04 18:09:14 +00001319 return;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001320 }
1321 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001322
Douglas Gregorc3366a52009-04-21 23:56:24 +00001323 case llvm::bitc::DEFINE_ABBREV:
1324 Stream.ReadAbbrevRecord();
1325 continue;
1326 default: break;
1327 }
1328
1329 // Read a record.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001330 const char *BlobStart = 0;
1331 unsigned BlobLen = 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001332 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001333 PreprocessorRecordTypes RecType =
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001334 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregor796d76a2010-10-20 22:00:55 +00001335 BlobLen);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001336 switch (RecType) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001337 case PP_MACRO_OBJECT_LIKE:
1338 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001339 // If we already have a macro, that means that we've hit the end
1340 // of the definition of the macro we were looking for. We're
1341 // done.
1342 if (Macro)
Douglas Gregor7cb0d012011-08-04 18:09:14 +00001343 return;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001344
Douglas Gregora3e41532011-07-28 20:55:49 +00001345 IdentifierInfo *II = getLocalIdentifier(F, Record[0]);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001346 if (II == 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001347 Error("macro must have a name in AST file");
Douglas Gregor7cb0d012011-08-04 18:09:14 +00001348 return;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001349 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00001350 SourceLocation Loc = ReadSourceLocation(F, Record[1]);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001351 bool isUsed = Record[2];
Mike Stump11289f42009-09-09 15:08:12 +00001352
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001353 MacroInfo *MI = PP->AllocateMacroInfo(Loc);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001354 MI->setIsUsed(isUsed);
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001355 MI->setIsFromAST();
Mike Stump11289f42009-09-09 15:08:12 +00001356
Douglas Gregoraae92242010-03-19 21:51:54 +00001357 unsigned NextIndex = 3;
Sebastian Redl539c5062010-08-18 23:57:32 +00001358 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001359 // Decode function-like macro info.
1360 bool isC99VarArgs = Record[3];
1361 bool isGNUVarArgs = Record[4];
1362 MacroArgs.clear();
1363 unsigned NumArgs = Record[5];
Douglas Gregoraae92242010-03-19 21:51:54 +00001364 NextIndex = 6 + NumArgs;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001365 for (unsigned i = 0; i != NumArgs; ++i)
Douglas Gregora3e41532011-07-28 20:55:49 +00001366 MacroArgs.push_back(getLocalIdentifier(F, Record[6+i]));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001367
1368 // Install function-like macro info.
1369 MI->setIsFunctionLike();
1370 if (isC99VarArgs) MI->setIsC99Varargs();
1371 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor038c3382009-05-22 22:45:36 +00001372 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001373 PP->getPreprocessorAllocator());
Douglas Gregorc3366a52009-04-21 23:56:24 +00001374 }
1375
1376 // Finally, install the macro.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001377 PP->setMacroInfo(II, MI);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001378
1379 // Remember that we saw this macro last so that we add the tokens that
1380 // form its body to it.
1381 Macro = MI;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001382
Douglas Gregoraae92242010-03-19 21:51:54 +00001383 if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) {
1384 // We have a macro definition. Load it now.
1385 PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro,
Douglas Gregor035611e2011-07-28 22:16:57 +00001386 getLocalMacroDefinition(F, Record[NextIndex]));
Douglas Gregoraae92242010-03-19 21:51:54 +00001387 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001388
Douglas Gregorc3366a52009-04-21 23:56:24 +00001389 ++NumMacrosRead;
1390 break;
1391 }
Mike Stump11289f42009-09-09 15:08:12 +00001392
Sebastian Redl539c5062010-08-18 23:57:32 +00001393 case PP_TOKEN: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001394 // If we see a TOKEN before a PP_MACRO_*, then the file is
1395 // erroneous, just pretend we didn't see this.
1396 if (Macro == 0) break;
Mike Stump11289f42009-09-09 15:08:12 +00001397
Douglas Gregorc3366a52009-04-21 23:56:24 +00001398 Token Tok;
1399 Tok.startToken();
Sebastian Redl2c373b92010-10-05 15:59:54 +00001400 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001401 Tok.setLength(Record[1]);
Douglas Gregora3e41532011-07-28 20:55:49 +00001402 if (IdentifierInfo *II = getLocalIdentifier(F, Record[2]))
Douglas Gregorc3366a52009-04-21 23:56:24 +00001403 Tok.setIdentifierInfo(II);
1404 Tok.setKind((tok::TokenKind)Record[3]);
1405 Tok.setFlag((Token::TokenFlags)Record[4]);
1406 Macro->AddTokenToBody(Tok);
1407 break;
1408 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00001409 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001410 }
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001411
Douglas Gregor7cb0d012011-08-04 18:09:14 +00001412 return;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001413}
1414
Douglas Gregora6895d82011-07-22 16:00:58 +00001415PreprocessedEntity *ASTReader::LoadPreprocessedEntity(Module &F) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00001416 assert(PP && "Forgot to set Preprocessor ?");
1417 unsigned Code = F.PreprocessorDetailCursor.ReadCode();
1418 switch (Code) {
1419 case llvm::bitc::END_BLOCK:
1420 return 0;
1421
1422 case llvm::bitc::ENTER_SUBBLOCK:
1423 Error("unexpected subblock record in preprocessor detail block");
1424 return 0;
1425
1426 case llvm::bitc::DEFINE_ABBREV:
1427 Error("unexpected abbrevation record in preprocessor detail block");
1428 return 0;
1429
1430 default:
1431 break;
1432 }
1433
1434 if (!PP->getPreprocessingRecord()) {
1435 Error("no preprocessing record");
1436 return 0;
1437 }
1438
1439 // Read the record.
1440 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1441 const char *BlobStart = 0;
1442 unsigned BlobLen = 0;
1443 RecordData Record;
1444 PreprocessorDetailRecordTypes RecType =
1445 (PreprocessorDetailRecordTypes)F.PreprocessorDetailCursor.ReadRecord(
1446 Code, Record, BlobStart, BlobLen);
1447 switch (RecType) {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001448 case PPD_MACRO_EXPANSION: {
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001449 PreprocessedEntityID GlobalID = getGlobalPreprocessedEntityID(F, Record[0]);
Douglas Gregor0d4b4312011-08-04 17:06:18 +00001450 if (PreprocessedEntity *PE = PPRec.getLoadedPreprocessedEntity(GlobalID-1))
Douglas Gregor92a96f52011-02-08 21:58:10 +00001451 return PE;
1452
Chandler Carrutha88a22182011-07-14 08:20:46 +00001453 MacroExpansion *ME =
Douglas Gregora3e41532011-07-28 20:55:49 +00001454 new (PPRec) MacroExpansion(getLocalIdentifier(F, Record[3]),
Douglas Gregor92a96f52011-02-08 21:58:10 +00001455 SourceRange(ReadSourceLocation(F, Record[1]),
1456 ReadSourceLocation(F, Record[2])),
Douglas Gregor035611e2011-07-28 22:16:57 +00001457 getLocalMacroDefinition(F, Record[4]));
Douglas Gregor0d4b4312011-08-04 17:06:18 +00001458 PPRec.setLoadedPreallocatedEntity(GlobalID - 1, ME);
Chandler Carrutha88a22182011-07-14 08:20:46 +00001459 return ME;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001460 }
1461
1462 case PPD_MACRO_DEFINITION: {
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001463 PreprocessedEntityID GlobalID = getGlobalPreprocessedEntityID(F, Record[0]);
Douglas Gregor0d4b4312011-08-04 17:06:18 +00001464 if (PreprocessedEntity *PE = PPRec.getLoadedPreprocessedEntity(GlobalID-1))
Douglas Gregor92a96f52011-02-08 21:58:10 +00001465 return PE;
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001466
1467 unsigned MacroDefID = getGlobalMacroDefinitionID(F, Record[1]);
1468 if (MacroDefID > MacroDefinitionsLoaded.size()) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00001469 Error("out-of-bounds macro definition record");
1470 return 0;
1471 }
1472
1473 // Decode the identifier info and then check again; if the macro is
1474 // still defined and associated with the identifier,
Douglas Gregora3e41532011-07-28 20:55:49 +00001475 IdentifierInfo *II = getLocalIdentifier(F, Record[4]);
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001476 if (!MacroDefinitionsLoaded[MacroDefID - 1]) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00001477 MacroDefinition *MD
1478 = new (PPRec) MacroDefinition(II,
1479 ReadSourceLocation(F, Record[5]),
1480 SourceRange(
1481 ReadSourceLocation(F, Record[2]),
1482 ReadSourceLocation(F, Record[3])));
1483
Douglas Gregor0d4b4312011-08-04 17:06:18 +00001484 PPRec.setLoadedPreallocatedEntity(GlobalID - 1, MD);
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001485 MacroDefinitionsLoaded[MacroDefID - 1] = MD;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001486
1487 if (DeserializationListener)
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001488 DeserializationListener->MacroDefinitionRead(MacroDefID, MD);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001489 }
1490
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001491 return MacroDefinitionsLoaded[MacroDefID - 1];
Douglas Gregor92a96f52011-02-08 21:58:10 +00001492 }
1493
1494 case PPD_INCLUSION_DIRECTIVE: {
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001495 PreprocessedEntityID GlobalID = getGlobalPreprocessedEntityID(F, Record[0]);
Douglas Gregor0d4b4312011-08-04 17:06:18 +00001496 if (PreprocessedEntity *PE = PPRec.getLoadedPreprocessedEntity(GlobalID-1))
Douglas Gregor92a96f52011-02-08 21:58:10 +00001497 return PE;
1498
1499 const char *FullFileNameStart = BlobStart + Record[3];
1500 const FileEntry *File
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001501 = PP->getFileManager().getFile(StringRef(FullFileNameStart,
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001502 BlobLen - Record[3]));
Douglas Gregor92a96f52011-02-08 21:58:10 +00001503
1504 // FIXME: Stable encoding
1505 InclusionDirective::InclusionKind Kind
1506 = static_cast<InclusionDirective::InclusionKind>(Record[5]);
1507 InclusionDirective *ID
1508 = new (PPRec) InclusionDirective(PPRec, Kind,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001509 StringRef(BlobStart, Record[3]),
Douglas Gregor92a96f52011-02-08 21:58:10 +00001510 Record[4],
1511 File,
1512 SourceRange(ReadSourceLocation(F, Record[1]),
1513 ReadSourceLocation(F, Record[2])));
Douglas Gregor0d4b4312011-08-04 17:06:18 +00001514 PPRec.setLoadedPreallocatedEntity(GlobalID - 1, ID);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001515 return ID;
1516 }
1517 }
1518
1519 Error("invalid offset in preprocessor detail block");
1520 return 0;
1521}
1522
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001523PreprocessedEntityID
1524ASTReader::getGlobalPreprocessedEntityID(Module &M, unsigned LocalID) {
Douglas Gregor2f555fc2011-08-04 18:56:47 +00001525 ContinuousRangeMap<uint32_t, int, 2>::iterator
1526 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1527 assert(I != M.PreprocessedEntityRemap.end()
1528 && "Invalid index into preprocessed entity index remap");
1529
1530 return LocalID + I->second;
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001531}
1532
Douglas Gregord44252e2011-08-25 20:47:51 +00001533unsigned HeaderFileInfoTrait::ComputeHash(const char *path) {
1534 return llvm::HashString(llvm::sys::path::filename(path));
Douglas Gregor09b69892011-02-10 17:09:37 +00001535}
Douglas Gregord44252e2011-08-25 20:47:51 +00001536
1537HeaderFileInfoTrait::internal_key_type
1538HeaderFileInfoTrait::GetInternalKey(const char *path) { return path; }
1539
1540bool HeaderFileInfoTrait::EqualKey(internal_key_type a, internal_key_type b) {
1541 if (strcmp(a, b) == 0)
1542 return true;
1543
1544 if (llvm::sys::path::filename(a) != llvm::sys::path::filename(b))
1545 return false;
1546
1547 // The file names match, but the path names don't. stat() the files to
1548 // see if they are the same.
1549 struct stat StatBufA, StatBufB;
1550 if (StatSimpleCache(a, &StatBufA) || StatSimpleCache(b, &StatBufB))
1551 return false;
1552
1553 return StatBufA.st_ino == StatBufB.st_ino;
1554}
1555
1556std::pair<unsigned, unsigned>
1557HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1558 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1559 unsigned DataLen = (unsigned) *d++;
1560 return std::make_pair(KeyLen + 1, DataLen);
1561}
1562
1563HeaderFileInfoTrait::data_type
1564HeaderFileInfoTrait::ReadData(const internal_key_type, const unsigned char *d,
1565 unsigned DataLen) {
1566 const unsigned char *End = d + DataLen;
1567 using namespace clang::io;
1568 HeaderFileInfo HFI;
1569 unsigned Flags = *d++;
1570 HFI.isImport = (Flags >> 5) & 0x01;
1571 HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1572 HFI.DirInfo = (Flags >> 2) & 0x03;
1573 HFI.Resolved = (Flags >> 1) & 0x01;
1574 HFI.IndexHeaderMapHeader = Flags & 0x01;
1575 HFI.NumIncludes = ReadUnalignedLE16(d);
1576 HFI.ControllingMacroID = Reader.getGlobalDeclID(M, ReadUnalignedLE32(d));
1577 if (unsigned FrameworkOffset = ReadUnalignedLE32(d)) {
1578 // The framework offset is 1 greater than the actual offset,
1579 // since 0 is used as an indicator for "no framework name".
1580 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1581 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1582 }
1583
1584 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1585 (void)End;
1586
1587 // This HeaderFileInfo was externally loaded.
1588 HFI.External = true;
1589 return HFI;
1590}
Douglas Gregor09b69892011-02-10 17:09:37 +00001591
Douglas Gregora6895d82011-07-22 16:00:58 +00001592void ASTReader::SetIdentifierIsMacro(IdentifierInfo *II, Module &F,
Douglas Gregor074fdc52011-07-28 21:16:51 +00001593 uint64_t LocalOffset) {
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001594 // Note that this identifier has a macro definition.
1595 II->setHasMacroDefinition(true);
1596
Douglas Gregord32f0352011-07-22 06:10:01 +00001597 // Adjust the offset to a global offset.
Douglas Gregor074fdc52011-07-28 21:16:51 +00001598 UnreadMacroRecordOffsets[II] = F.GlobalBitOffset + LocalOffset;
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001599}
1600
Sebastian Redl2c499f62010-08-18 23:56:43 +00001601void ASTReader::ReadDefinedMacros() {
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00001602 for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1603 E = ModuleMgr.rend(); I != E; ++I) {
1604 llvm::BitstreamCursor &MacroCursor = (*I)->MacroCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001605
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001606 // If there was no preprocessor block, skip this file.
1607 if (!MacroCursor.getBitStreamReader())
1608 continue;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001609
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001610 llvm::BitstreamCursor Cursor = MacroCursor;
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00001611 Cursor.JumpToBit((*I)->MacroStartOffset);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001612
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001613 RecordData Record;
1614 while (true) {
1615 unsigned Code = Cursor.ReadCode();
Douglas Gregor796d76a2010-10-20 22:00:55 +00001616 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001617 break;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001618
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001619 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1620 // No known subblocks, always skip them.
1621 Cursor.ReadSubBlockID();
1622 if (Cursor.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001623 Error("malformed block record in AST file");
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001624 return;
1625 }
1626 continue;
1627 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001628
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001629 if (Code == llvm::bitc::DEFINE_ABBREV) {
1630 Cursor.ReadAbbrevRecord();
1631 continue;
1632 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001633
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001634 // Read a record.
1635 const char *BlobStart;
1636 unsigned BlobLen;
1637 Record.clear();
1638 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1639 default: // Default behavior: ignore.
1640 break;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001641
Sebastian Redl539c5062010-08-18 23:57:32 +00001642 case PP_MACRO_OBJECT_LIKE:
1643 case PP_MACRO_FUNCTION_LIKE:
Douglas Gregora3e41532011-07-28 20:55:49 +00001644 getLocalIdentifier(**I, Record[0]);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001645 break;
1646
Sebastian Redl539c5062010-08-18 23:57:32 +00001647 case PP_TOKEN:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001648 // Ignore tokens.
1649 break;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001650 }
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001651 }
1652 }
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001653
1654 // Drain the unread macro-record offsets map.
1655 while (!UnreadMacroRecordOffsets.empty())
1656 LoadMacroDefinition(UnreadMacroRecordOffsets.begin());
1657}
1658
1659void ASTReader::LoadMacroDefinition(
1660 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos) {
1661 assert(Pos != UnreadMacroRecordOffsets.end() && "Unknown macro definition");
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001662 uint64_t Offset = Pos->second;
1663 UnreadMacroRecordOffsets.erase(Pos);
1664
Douglas Gregord32f0352011-07-22 06:10:01 +00001665 RecordLocation Loc = getLocalBitOffset(Offset);
1666 ReadMacroRecord(*Loc.F, Loc.Offset);
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001667}
1668
1669void ASTReader::LoadMacroDefinition(IdentifierInfo *II) {
1670 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos
1671 = UnreadMacroRecordOffsets.find(II);
1672 LoadMacroDefinition(Pos);
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001673}
1674
Sebastian Redl50e26582010-09-15 19:54:06 +00001675MacroDefinition *ASTReader::getMacroDefinition(MacroID ID) {
Douglas Gregor91096292010-10-02 19:29:26 +00001676 if (ID == 0 || ID > MacroDefinitionsLoaded.size())
Douglas Gregoraae92242010-03-19 21:51:54 +00001677 return 0;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001678
Douglas Gregor91096292010-10-02 19:29:26 +00001679 if (!MacroDefinitionsLoaded[ID - 1]) {
Douglas Gregor270e0142011-07-20 01:29:15 +00001680 GlobalMacroDefinitionMapType::iterator I =GlobalMacroDefinitionMap.find(ID);
1681 assert(I != GlobalMacroDefinitionMap.end() &&
1682 "Corrupted global macro definition map");
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00001683 Module &M = *I->second;
1684 unsigned Index = ID - 1 - M.BaseMacroDefinitionID;
1685 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
1686 M.PreprocessorDetailCursor.JumpToBit(M.MacroDefinitionOffsets[Index]);
1687 LoadPreprocessedEntity(M);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001688 }
1689
Douglas Gregor91096292010-10-02 19:29:26 +00001690 return MacroDefinitionsLoaded[ID - 1];
Douglas Gregoraae92242010-03-19 21:51:54 +00001691}
1692
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001693const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) {
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00001694 std::string Filename = filenameStrRef;
1695 MaybeAddSystemRootToFilename(Filename);
1696 const FileEntry *File = FileMgr.getFile(Filename);
1697 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1698 OriginalDir != CurrentDir) {
1699 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1700 OriginalDir,
1701 CurrentDir);
1702 if (!resolved.empty())
1703 File = FileMgr.getFile(resolved);
1704 }
1705
1706 return File;
1707}
1708
Douglas Gregor035611e2011-07-28 22:16:57 +00001709MacroID ASTReader::getGlobalMacroDefinitionID(Module &M, unsigned LocalID) {
Douglas Gregora863b4b2011-08-04 16:36:56 +00001710 if (LocalID < NUM_PREDEF_MACRO_IDS)
1711 return LocalID;
1712
1713 ContinuousRangeMap<uint32_t, int, 2>::iterator I
1714 = M.MacroDefinitionRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
1715 assert(I != M.MacroDefinitionRemap.end() &&
1716 "Invalid index into macro definition ID remap");
1717
1718 return LocalID + I->second;
Douglas Gregor035611e2011-07-28 22:16:57 +00001719}
1720
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001721/// \brief If we are loading a relocatable PCH file, and the filename is
1722/// not an absolute path, add the system root to the beginning of the file
1723/// name.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001724void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001725 // If this is not a relocatable PCH file, there's nothing to do.
1726 if (!RelocatablePCH)
1727 return;
Mike Stump11289f42009-09-09 15:08:12 +00001728
Michael J. Spencerf28df4c2010-12-17 21:22:22 +00001729 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001730 return;
1731
Douglas Gregorc567ba22011-07-22 16:35:34 +00001732 if (isysroot.empty()) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001733 // If no system root was given, default to '/'
1734 Filename.insert(Filename.begin(), '/');
1735 return;
1736 }
Mike Stump11289f42009-09-09 15:08:12 +00001737
Douglas Gregorc567ba22011-07-22 16:35:34 +00001738 unsigned Length = isysroot.size();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001739 if (isysroot[Length - 1] != '/')
1740 Filename.insert(Filename.begin(), '/');
Mike Stump11289f42009-09-09 15:08:12 +00001741
Douglas Gregorc567ba22011-07-22 16:35:34 +00001742 Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001743}
1744
Sebastian Redl2c499f62010-08-18 23:56:43 +00001745ASTReader::ASTReadResult
Douglas Gregora6895d82011-07-22 16:00:58 +00001746ASTReader::ReadASTBlock(Module &F) {
Sebastian Redl34522812010-07-16 17:50:48 +00001747 llvm::BitstreamCursor &Stream = F.Stream;
1748
Sebastian Redl539c5062010-08-18 23:57:32 +00001749 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001750 Error("malformed block record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001751 return Failure;
1752 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001753
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001754 // Read all of the records and blocks for the ASt file.
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001755 RecordData Record;
Sebastian Redl393f8b72010-07-19 20:52:06 +00001756 bool First = true;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001757 while (!Stream.AtEndOfStream()) {
1758 unsigned Code = Stream.ReadCode();
1759 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor55abb232009-04-10 20:39:37 +00001760 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001761 Error("error at end of module block in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001762 return Failure;
1763 }
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001764
Douglas Gregor55abb232009-04-10 20:39:37 +00001765 return Success;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001766 }
1767
1768 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1769 switch (Stream.ReadSubBlockID()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001770 case DECLTYPES_BLOCK_ID:
Chris Lattnere78a6be2009-04-27 01:05:14 +00001771 // We lazily load the decls block, but we want to set up the
1772 // DeclsCursor cursor to point into it. Clone our current bitcode
1773 // cursor to it, enter the block and read the abbrevs in that block.
1774 // With the main cursor, we just skip over it.
Sebastian Redl34522812010-07-16 17:50:48 +00001775 F.DeclsCursor = Stream;
Chris Lattnere78a6be2009-04-27 01:05:14 +00001776 if (Stream.SkipBlock() || // Skip with the main cursor.
1777 // Read the abbrevs.
Sebastian Redl539c5062010-08-18 23:57:32 +00001778 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001779 Error("malformed block record in AST file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001780 return Failure;
1781 }
1782 break;
Mike Stump11289f42009-09-09 15:08:12 +00001783
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00001784 case DECL_UPDATES_BLOCK_ID:
1785 if (Stream.SkipBlock()) {
1786 Error("malformed block record in AST file");
1787 return Failure;
1788 }
1789 break;
1790
Sebastian Redl539c5062010-08-18 23:57:32 +00001791 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl34522812010-07-16 17:50:48 +00001792 F.MacroCursor = Stream;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001793 if (PP)
1794 PP->setExternalSource(this);
1795
Douglas Gregor796d76a2010-10-20 22:00:55 +00001796 if (Stream.SkipBlock() ||
1797 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001798 Error("malformed block record in AST file");
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001799 return Failure;
1800 }
Douglas Gregor796d76a2010-10-20 22:00:55 +00001801 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001802 break;
Steve Naroff2ddea052009-04-23 10:39:46 +00001803
Douglas Gregor92a96f52011-02-08 21:58:10 +00001804 case PREPROCESSOR_DETAIL_BLOCK_ID:
1805 F.PreprocessorDetailCursor = Stream;
1806 if (Stream.SkipBlock() ||
1807 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
1808 PREPROCESSOR_DETAIL_BLOCK_ID)) {
1809 Error("malformed preprocessor detail record in AST file");
1810 return Failure;
1811 }
1812 F.PreprocessorDetailStartOffset
1813 = F.PreprocessorDetailCursor.GetCurrentBitNo();
1814 break;
1815
Sebastian Redl539c5062010-08-18 23:57:32 +00001816 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl393f8b72010-07-19 20:52:06 +00001817 switch (ReadSourceManagerBlock(F)) {
Douglas Gregor92863e42009-04-10 23:10:45 +00001818 case Success:
1819 break;
1820
1821 case Failure:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001822 Error("malformed source manager block in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001823 return Failure;
Douglas Gregor92863e42009-04-10 23:10:45 +00001824
1825 case IgnorePCH:
1826 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00001827 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001828 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001829 }
Sebastian Redl393f8b72010-07-19 20:52:06 +00001830 First = false;
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001831 continue;
1832 }
1833
1834 if (Code == llvm::bitc::DEFINE_ABBREV) {
1835 Stream.ReadAbbrevRecord();
1836 continue;
1837 }
1838
1839 // Read and process a record.
1840 Record.clear();
Douglas Gregorbfbde532009-04-10 21:16:55 +00001841 const char *BlobStart = 0;
1842 unsigned BlobLen = 0;
Sebastian Redl539c5062010-08-18 23:57:32 +00001843 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001844 &BlobStart, &BlobLen)) {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001845 default: // Default behavior: ignore.
1846 break;
1847
Sebastian Redl539c5062010-08-18 23:57:32 +00001848 case METADATA: {
1849 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1850 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001851 : diag::warn_pch_version_too_new);
1852 return IgnorePCH;
1853 }
1854
1855 RelocatablePCH = Record[4];
1856 if (Listener) {
1857 std::string TargetTriple(BlobStart, BlobLen);
1858 if (Listener->ReadTargetTriple(TargetTriple))
1859 return IgnorePCH;
1860 }
1861 break;
1862 }
1863
Douglas Gregor29cc6422011-08-17 21:07:30 +00001864 case IMPORTS: {
1865 // Load each of the imported PCH files.
1866 unsigned Idx = 0, N = Record.size();
1867 while (Idx < N) {
1868 // Read information about the AST file.
1869 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
1870 unsigned Length = Record[Idx++];
1871 llvm::SmallString<128> ImportedFile(Record.begin() + Idx,
1872 Record.begin() + Idx + Length);
1873 Idx += Length;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001874
Douglas Gregor29cc6422011-08-17 21:07:30 +00001875 // Load the AST file.
Douglas Gregordf0c1512011-08-18 04:12:04 +00001876 switch(ReadASTCore(ImportedFile, ImportedKind, &F)) {
Douglas Gregor29cc6422011-08-17 21:07:30 +00001877 case Failure: return Failure;
1878 // If we have to ignore the dependency, we'll have to ignore this too.
1879 case IgnorePCH: return IgnorePCH;
1880 case Success: break;
1881 }
1882 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001883 break;
1884 }
1885
Douglas Gregor5204bde2011-08-02 16:26:37 +00001886 case TYPE_OFFSET: {
Sebastian Redl9e687992010-07-19 22:06:55 +00001887 if (F.LocalNumTypes != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001888 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001889 return Failure;
1890 }
Sebastian Redl9e687992010-07-19 22:06:55 +00001891 F.TypeOffsets = (const uint32_t *)BlobStart;
1892 F.LocalNumTypes = Record[0];
Douglas Gregor3b65ed02011-08-02 18:32:54 +00001893 unsigned LocalBaseTypeIndex = Record[1];
1894 F.BaseTypeIndex = getTotalNumTypes();
Douglas Gregor8ab4ea82011-07-29 00:21:44 +00001895
Douglas Gregor5204bde2011-08-02 16:26:37 +00001896 if (F.LocalNumTypes > 0) {
1897 // Introduce the global -> local mapping for types within this module.
1898 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
1899
1900 // Introduce the local -> global mapping for types within this module.
Douglas Gregor3b65ed02011-08-02 18:32:54 +00001901 F.TypeRemap.insert(std::make_pair(LocalBaseTypeIndex,
1902 F.BaseTypeIndex - LocalBaseTypeIndex));
Douglas Gregor5204bde2011-08-02 16:26:37 +00001903
1904 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
1905 }
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001906 break;
Douglas Gregor5204bde2011-08-02 16:26:37 +00001907 }
1908
Douglas Gregorf7180622011-08-03 15:48:04 +00001909 case DECL_OFFSET: {
Sebastian Redl9e687992010-07-19 22:06:55 +00001910 if (F.LocalNumDecls != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001911 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001912 return Failure;
1913 }
Sebastian Redl9e687992010-07-19 22:06:55 +00001914 F.DeclOffsets = (const uint32_t *)BlobStart;
1915 F.LocalNumDecls = Record[0];
Douglas Gregorf7180622011-08-03 15:48:04 +00001916 unsigned LocalBaseDeclID = Record[1];
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00001917 F.BaseDeclID = getTotalNumDecls();
Douglas Gregor047d2ef2011-07-20 00:27:43 +00001918
Douglas Gregorf7180622011-08-03 15:48:04 +00001919 if (F.LocalNumDecls > 0) {
1920 // Introduce the global -> local mapping for declarations within this
1921 // module.
Douglas Gregordab42432011-08-12 00:15:20 +00001922 GlobalDeclMap.insert(
1923 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
Douglas Gregorf7180622011-08-03 15:48:04 +00001924
1925 // Introduce the local -> global mapping for declarations within this
1926 // module.
1927 F.DeclRemap.insert(std::make_pair(LocalBaseDeclID,
1928 F.BaseDeclID - LocalBaseDeclID));
1929
1930 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
1931 }
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001932 break;
Douglas Gregorf7180622011-08-03 15:48:04 +00001933 }
1934
Sebastian Redl539c5062010-08-18 23:57:32 +00001935 case TU_UPDATE_LEXICAL: {
Douglas Gregordab42432011-08-12 00:15:20 +00001936 DeclContext *TU = Context ? Context->getTranslationUnitDecl() : 0;
Douglas Gregor94619c82011-08-24 19:03:07 +00001937 DeclContextInfo &Info = F.DeclContextInfos[TU];
1938 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(BlobStart);
1939 Info.NumLexicalDecls
1940 = static_cast<unsigned int>(BlobLen / sizeof(KindDeclIDPair));
Douglas Gregordab42432011-08-12 00:15:20 +00001941 if (TU)
1942 TU->setHasExternalLexicalStorage(true);
1943
Sebastian Redl4b1f4902010-07-27 18:24:41 +00001944 break;
1945 }
1946
Sebastian Redld7dce0a2010-08-24 00:50:04 +00001947 case UPDATE_VISIBLE: {
Douglas Gregorf7180622011-08-03 15:48:04 +00001948 unsigned Idx = 0;
1949 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
Sebastian Redld7dce0a2010-08-24 00:50:04 +00001950 void *Table = ASTDeclContextNameLookupTable::Create(
Douglas Gregorf7180622011-08-03 15:48:04 +00001951 (const unsigned char *)BlobStart + Record[Idx++],
Sebastian Redld7dce0a2010-08-24 00:50:04 +00001952 (const unsigned char *)BlobStart,
Douglas Gregor903b7e92011-07-22 00:38:23 +00001953 ASTDeclContextNameLookupTrait(*this, F));
Douglas Gregordab42432011-08-12 00:15:20 +00001954 if (ID == PREDEF_DECL_TRANSLATION_UNIT_ID && Context) { // Is it the TU?
Douglas Gregordab42432011-08-12 00:15:20 +00001955 DeclContext *TU = Context->getTranslationUnitDecl();
Douglas Gregor94619c82011-08-24 19:03:07 +00001956 F.DeclContextInfos[TU].NameLookupTableData = Table;
Douglas Gregordab42432011-08-12 00:15:20 +00001957 TU->setHasExternalVisibleStorage(true);
Sebastian Redld7dce0a2010-08-24 00:50:04 +00001958 } else
Douglas Gregorf7180622011-08-03 15:48:04 +00001959 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
Sebastian Redld7dce0a2010-08-24 00:50:04 +00001960 break;
1961 }
1962
Sebastian Redl539c5062010-08-18 23:57:32 +00001963 case REDECLS_UPDATE_LATEST: {
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00001964 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
Douglas Gregorf7180622011-08-03 15:48:04 +00001965 for (unsigned i = 0, e = Record.size(); i < e; /* in loop */) {
1966 DeclID First = ReadDeclID(F, Record, i);
1967 DeclID Latest = ReadDeclID(F, Record, i);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00001968 FirstLatestDeclIDs[First] = Latest;
1969 }
1970 break;
1971 }
1972
Sebastian Redl539c5062010-08-18 23:57:32 +00001973 case LANGUAGE_OPTIONS:
Douglas Gregorce3a8292010-07-27 00:27:13 +00001974 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor55abb232009-04-10 20:39:37 +00001975 return IgnorePCH;
1976 break;
Douglas Gregorbfbde532009-04-10 21:16:55 +00001977
Sebastian Redl539c5062010-08-18 23:57:32 +00001978 case IDENTIFIER_TABLE:
Sebastian Redl393f8b72010-07-19 20:52:06 +00001979 F.IdentifierTableData = BlobStart;
Douglas Gregor0e149972009-04-25 19:10:14 +00001980 if (Record[0]) {
Sebastian Redl393f8b72010-07-19 20:52:06 +00001981 F.IdentifierLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001982 = ASTIdentifierLookupTable::Create(
Sebastian Redl393f8b72010-07-19 20:52:06 +00001983 (const unsigned char *)F.IdentifierTableData + Record[0],
1984 (const unsigned char *)F.IdentifierTableData,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001985 ASTIdentifierLookupTrait(*this, F));
Douglas Gregor49bf76b2011-07-21 18:46:38 +00001986 if (PP) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001987 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor49bf76b2011-07-21 18:46:38 +00001988 PP->getHeaderSearchInfo().SetExternalLookup(this);
1989 }
Douglas Gregor0e149972009-04-25 19:10:14 +00001990 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001991 break;
1992
Douglas Gregor1ab036c2011-08-03 21:49:18 +00001993 case IDENTIFIER_OFFSET: {
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00001994 if (F.LocalNumIdentifiers != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001995 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001996 return Failure;
1997 }
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00001998 F.IdentifierOffsets = (const uint32_t *)BlobStart;
1999 F.LocalNumIdentifiers = Record[0];
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002000 unsigned LocalBaseIdentifierID = Record[1];
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00002001 F.BaseIdentifierID = getTotalNumIdentifiers();
Douglas Gregor19d26352011-07-20 00:59:32 +00002002
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002003 if (F.LocalNumIdentifiers > 0) {
2004 // Introduce the global -> local mapping for identifiers within this
2005 // module.
2006 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
2007 &F));
2008
2009 // Introduce the local -> global mapping for identifiers within this
2010 // module.
2011 F.IdentifierRemap.insert(
2012 std::make_pair(LocalBaseIdentifierID,
2013 F.BaseIdentifierID - LocalBaseIdentifierID));
2014
2015 IdentifiersLoaded.resize(IdentifiersLoaded.size()
2016 + F.LocalNumIdentifiers);
2017 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002018 break;
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002019 }
2020
Sebastian Redl539c5062010-08-18 23:57:32 +00002021 case EXTERNAL_DEFINITIONS:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002022 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2023 ExternalDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002024 break;
Douglas Gregor08f01292009-04-17 22:13:46 +00002025
Sebastian Redl539c5062010-08-18 23:57:32 +00002026 case SPECIAL_TYPES:
Douglas Gregor903b7e92011-07-22 00:38:23 +00002027 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2028 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
Douglas Gregor652d82a2009-04-18 05:55:16 +00002029 break;
2030
Sebastian Redl539c5062010-08-18 23:57:32 +00002031 case STATISTICS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002032 TotalNumStatements += Record[0];
2033 TotalNumMacros += Record[1];
2034 TotalLexicalDeclContexts += Record[2];
2035 TotalVisibleDeclContexts += Record[3];
Douglas Gregor08f01292009-04-17 22:13:46 +00002036 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00002037
Sebastian Redl539c5062010-08-18 23:57:32 +00002038 case UNUSED_FILESCOPED_DECLS:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002039 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2040 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
Tanya Lattner90073802010-02-12 00:07:30 +00002041 break;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00002042
Alexis Hunt27a761d2011-05-04 23:29:54 +00002043 case DELEGATING_CTORS:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002044 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2045 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
Alexis Hunt27a761d2011-05-04 23:29:54 +00002046 break;
2047
Sebastian Redl539c5062010-08-18 23:57:32 +00002048 case WEAK_UNDECLARED_IDENTIFIERS:
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00002049 if (Record.size() % 4 != 0) {
2050 Error("invalid weak identifiers record");
2051 return Failure;
2052 }
2053
2054 // FIXME: Ignore weak undeclared identifiers from non-original PCH
2055 // files. This isn't the way to do it :)
2056 WeakUndeclaredIdentifiers.clear();
2057
2058 // Translate the weak, undeclared identifiers into global IDs.
2059 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2060 WeakUndeclaredIdentifiers.push_back(
2061 getGlobalIdentifierID(F, Record[I++]));
2062 WeakUndeclaredIdentifiers.push_back(
2063 getGlobalIdentifierID(F, Record[I++]));
2064 WeakUndeclaredIdentifiers.push_back(
2065 ReadSourceLocation(F, Record, I).getRawEncoding());
2066 WeakUndeclaredIdentifiers.push_back(Record[I++]);
2067 }
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002068 break;
2069
Sebastian Redl539c5062010-08-18 23:57:32 +00002070 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002071 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2072 LocallyScopedExternalDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002073 break;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002074
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002075 case SELECTOR_OFFSETS: {
Sebastian Redla19a67f2010-08-03 21:58:15 +00002076 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redlada023c2010-08-04 20:40:17 +00002077 F.LocalNumSelectors = Record[0];
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002078 unsigned LocalBaseSelectorID = Record[1];
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00002079 F.BaseSelectorID = getTotalNumSelectors();
Douglas Gregor2262d282011-07-20 01:10:58 +00002080
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002081 if (F.LocalNumSelectors > 0) {
2082 // Introduce the global -> local mapping for selectors within this
2083 // module.
2084 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2085
2086 // Introduce the local -> global mapping for selectors within this
2087 // module.
2088 F.SelectorRemap.insert(std::make_pair(LocalBaseSelectorID,
2089 F.BaseSelectorID - LocalBaseSelectorID));
Douglas Gregor95c13f52009-04-25 17:48:32 +00002090
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002091 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
2092 }
2093 break;
2094 }
2095
Sebastian Redl539c5062010-08-18 23:57:32 +00002096 case METHOD_POOL:
Sebastian Redlada023c2010-08-04 20:40:17 +00002097 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002098 if (Record[0])
Sebastian Redlada023c2010-08-04 20:40:17 +00002099 F.SelectorLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002100 = ASTSelectorLookupTable::Create(
Sebastian Redlada023c2010-08-04 20:40:17 +00002101 F.SelectorLookupTableData + Record[0],
2102 F.SelectorLookupTableData,
Douglas Gregor7fb09192011-07-21 22:35:25 +00002103 ASTSelectorLookupTrait(*this, F));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002104 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorc78d3462009-04-24 21:10:55 +00002105 break;
Douglas Gregoreda6a892009-04-26 00:07:37 +00002106
Sebastian Redl96371b42010-09-22 00:42:30 +00002107 case REFERENCED_SELECTOR_POOL:
Douglas Gregor3f8f04f2011-07-28 14:41:43 +00002108 if (!Record.empty()) {
2109 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2110 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2111 Record[Idx++]));
2112 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2113 getRawEncoding());
2114 }
2115 }
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002116 break;
2117
Sebastian Redl539c5062010-08-18 23:57:32 +00002118 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002119 if (!Record.empty() && Listener)
2120 Listener->ReadCounter(Record[0]);
Douglas Gregoreda6a892009-04-26 00:07:37 +00002121 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00002122
Douglas Gregor925296b2011-07-19 16:10:42 +00002123 case SOURCE_LOCATION_OFFSETS: {
2124 F.SLocEntryOffsets = (const uint32_t *)BlobStart;
Sebastian Redlb293a452010-07-20 21:20:32 +00002125 F.LocalNumSLocEntries = Record[0];
Douglas Gregor925296b2011-07-19 16:10:42 +00002126 llvm::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
2127 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries, Record[1]);
2128 // Make our entry in the range map. BaseID is negative and growing, so
2129 // we invert it. Because we invert it, though, we need the other end of
2130 // the range.
2131 unsigned RangeStart =
2132 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2133 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2134 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2135
2136 // Initialize the remapping table.
2137 // Invalid stays invalid.
2138 F.SLocRemap.insert(std::make_pair(0U, 0));
2139 // This module. Base was 2 when being compiled.
2140 F.SLocRemap.insert(std::make_pair(2U,
2141 static_cast<int>(F.SLocEntryBaseOffset - 2)));
Douglas Gregor49bf76b2011-07-21 18:46:38 +00002142
2143 TotalNumSLocEntries += F.LocalNumSLocEntries;
Douglas Gregor925296b2011-07-19 16:10:42 +00002144 break;
2145 }
2146
Douglas Gregor5a1797c2011-08-01 16:01:55 +00002147 case MODULE_OFFSET_MAP: {
Douglas Gregor925296b2011-07-19 16:10:42 +00002148 // Additional remapping information.
2149 const unsigned char *Data = (const unsigned char*)BlobStart;
2150 const unsigned char *DataEnd = Data + BlobLen;
Douglas Gregor00659902011-08-02 10:56:51 +00002151
2152 // Continuous range maps we may be updating in our module.
2153 ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap);
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002154 ContinuousRangeMap<uint32_t, int, 2>::Builder
2155 IdentifierRemap(F.IdentifierRemap);
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002156 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002157 PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2158 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregora863b4b2011-08-04 16:36:56 +00002159 MacroDefinitionRemap(F.MacroDefinitionRemap);
2160 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002161 SelectorRemap(F.SelectorRemap);
Douglas Gregorf7180622011-08-03 15:48:04 +00002162 ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap);
Douglas Gregor5204bde2011-08-02 16:26:37 +00002163 ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap);
2164
Douglas Gregor925296b2011-07-19 16:10:42 +00002165 while(Data < DataEnd) {
Douglas Gregor925296b2011-07-19 16:10:42 +00002166 uint16_t Len = io::ReadUnalignedLE16(Data);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002167 StringRef Name = StringRef((const char*)Data, Len);
Douglas Gregor00659902011-08-02 10:56:51 +00002168 Data += Len;
Jonathan D. Turnerb2b08232011-07-26 18:21:30 +00002169 Module *OM = ModuleMgr.lookup(Name);
Douglas Gregor925296b2011-07-19 16:10:42 +00002170 if (!OM) {
2171 Error("SourceLocation remap refers to unknown module");
2172 return Failure;
2173 }
Douglas Gregor00659902011-08-02 10:56:51 +00002174
2175 uint32_t SLocOffset = io::ReadUnalignedLE32(Data);
2176 uint32_t IdentifierIDOffset = io::ReadUnalignedLE32(Data);
2177 uint32_t PreprocessedEntityIDOffset = io::ReadUnalignedLE32(Data);
2178 uint32_t MacroDefinitionIDOffset = io::ReadUnalignedLE32(Data);
2179 uint32_t SelectorIDOffset = io::ReadUnalignedLE32(Data);
2180 uint32_t DeclIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregor5204bde2011-08-02 16:26:37 +00002181 uint32_t TypeIndexOffset = io::ReadUnalignedLE32(Data);
Douglas Gregor00659902011-08-02 10:56:51 +00002182
2183 // Source location offset is mapped to OM->SLocEntryBaseOffset.
2184 SLocRemap.insert(std::make_pair(SLocOffset,
2185 static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset)));
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002186 IdentifierRemap.insert(
2187 std::make_pair(IdentifierIDOffset,
2188 OM->BaseIdentifierID - IdentifierIDOffset));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002189 PreprocessedEntityRemap.insert(
2190 std::make_pair(PreprocessedEntityIDOffset,
2191 OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset));
Douglas Gregora863b4b2011-08-04 16:36:56 +00002192 MacroDefinitionRemap.insert(
2193 std::make_pair(MacroDefinitionIDOffset,
2194 OM->BaseMacroDefinitionID - MacroDefinitionIDOffset));
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002195 SelectorRemap.insert(std::make_pair(SelectorIDOffset,
2196 OM->BaseSelectorID - SelectorIDOffset));
Douglas Gregorf7180622011-08-03 15:48:04 +00002197 DeclRemap.insert(std::make_pair(DeclIDOffset,
2198 OM->BaseDeclID - DeclIDOffset));
2199
Douglas Gregor5204bde2011-08-02 16:26:37 +00002200 TypeRemap.insert(std::make_pair(TypeIndexOffset,
Douglas Gregor3b65ed02011-08-02 18:32:54 +00002201 OM->BaseTypeIndex - TypeIndexOffset));
Douglas Gregor925296b2011-07-19 16:10:42 +00002202 }
2203 break;
2204 }
2205
Douglas Gregor925296b2011-07-19 16:10:42 +00002206 case SOURCE_MANAGER_LINE_TABLE:
2207 if (ParseLineTable(F, Record))
2208 return Failure;
Douglas Gregor258ae542009-04-27 06:38:32 +00002209 break;
2210
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00002211 case FILE_SOURCE_LOCATION_OFFSETS:
2212 F.SLocFileOffsets = (const uint32_t *)BlobStart;
2213 F.LocalNumSLocFileEntries = Record[0];
2214 break;
2215
Douglas Gregor925296b2011-07-19 16:10:42 +00002216 case SOURCE_LOCATION_PRELOADS: {
2217 // Need to transform from the local view (1-based IDs) to the global view,
2218 // which is based off F.SLocEntryBaseID.
Douglas Gregora918bab2011-08-25 21:09:44 +00002219 if (!F.PreloadSLocEntries.empty()) {
2220 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
2221 return Failure;
2222 }
2223
2224 F.PreloadSLocEntries.swap(Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002225 break;
Douglas Gregor925296b2011-07-19 16:10:42 +00002226 }
Douglas Gregorc5046832009-04-27 18:38:38 +00002227
Sebastian Redl539c5062010-08-18 23:57:32 +00002228 case STAT_CACHE: {
Douglas Gregor606c4ac2011-02-05 19:42:43 +00002229 if (!DisableStatCache) {
2230 ASTStatCache *MyStatCache =
2231 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
2232 (const unsigned char *)BlobStart,
2233 NumStatHits, NumStatMisses);
2234 FileMgr.addStatCache(MyStatCache);
2235 F.StatCache = MyStatCache;
2236 }
Douglas Gregorc5046832009-04-27 18:38:38 +00002237 break;
Douglas Gregord2eb58a2009-10-16 18:18:30 +00002238 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00002239
Sebastian Redl539c5062010-08-18 23:57:32 +00002240 case EXT_VECTOR_DECLS:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002241 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2242 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002243 break;
2244
Sebastian Redl539c5062010-08-18 23:57:32 +00002245 case VTABLE_USES:
Douglas Gregor4daf6a32011-07-28 19:11:31 +00002246 if (Record.size() % 3 != 0) {
2247 Error("Invalid VTABLE_USES record");
2248 return Failure;
2249 }
2250
Sebastian Redl08aca90252010-08-05 18:21:25 +00002251 // Later tables overwrite earlier ones.
Douglas Gregor4daf6a32011-07-28 19:11:31 +00002252 // FIXME: Modules will have some trouble with this. This is clearly not
2253 // the right way to do this.
Douglas Gregor7fb09192011-07-21 22:35:25 +00002254 VTableUses.clear();
Douglas Gregor4daf6a32011-07-28 19:11:31 +00002255
2256 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
2257 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
2258 VTableUses.push_back(
2259 ReadSourceLocation(F, Record, Idx).getRawEncoding());
2260 VTableUses.push_back(Record[Idx++]);
2261 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002262 break;
2263
Sebastian Redl539c5062010-08-18 23:57:32 +00002264 case DYNAMIC_CLASSES:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002265 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2266 DynamicClasses.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002267 break;
2268
Sebastian Redl539c5062010-08-18 23:57:32 +00002269 case PENDING_IMPLICIT_INSTANTIATIONS:
Douglas Gregorbbbc3672011-07-28 19:26:52 +00002270 if (PendingInstantiations.size() % 2 != 0) {
2271 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
2272 return Failure;
2273 }
2274
2275 // Later lists of pending instantiations overwrite earlier ones.
2276 // FIXME: This is most certainly wrong for modules.
2277 PendingInstantiations.clear();
2278 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2279 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
2280 PendingInstantiations.push_back(
2281 ReadSourceLocation(F, Record, I).getRawEncoding());
2282 }
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002283 break;
2284
Sebastian Redl539c5062010-08-18 23:57:32 +00002285 case SEMA_DECL_REFS:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002286 // Later tables overwrite earlier ones.
Douglas Gregor7fb09192011-07-21 22:35:25 +00002287 // FIXME: Modules will have some trouble with this.
2288 SemaDeclRefs.clear();
2289 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2290 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002291 break;
2292
Sebastian Redl539c5062010-08-18 23:57:32 +00002293 case ORIGINAL_FILE_NAME:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002294 // The primary AST will be the last to get here, so it will be the one
Sebastian Redlb293a452010-07-20 21:20:32 +00002295 // that's used.
Daniel Dunbar000c4ff2009-11-11 05:29:04 +00002296 ActualOriginalFileName.assign(BlobStart, BlobLen);
2297 OriginalFileName = ActualOriginalFileName;
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002298 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregor45fe0362009-05-12 01:31:05 +00002299 break;
Mike Stump11289f42009-09-09 15:08:12 +00002300
Douglas Gregora3b20262011-05-06 21:43:30 +00002301 case ORIGINAL_FILE_ID:
2302 OriginalFileID = FileID::get(Record[0]);
2303 break;
2304
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002305 case ORIGINAL_PCH_DIR:
2306 // The primary AST will be the last to get here, so it will be the one
2307 // that's used.
2308 OriginalDir.assign(BlobStart, BlobLen);
2309 break;
2310
Sebastian Redl539c5062010-08-18 23:57:32 +00002311 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek8bd09292010-02-12 23:31:14 +00002312 const std::string &CurBranch = getClangFullRepositoryVersion();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002313 StringRef ASTBranch(BlobStart, BlobLen);
2314 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002315 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregord54f3a12009-10-05 21:07:28 +00002316 return IgnorePCH;
2317 }
2318 break;
2319 }
Sebastian Redlfa061442010-07-21 20:07:32 +00002320
Douglas Gregor4a9c39a2011-07-21 00:47:40 +00002321 case MACRO_DEFINITION_OFFSETS: {
Sebastian Redlfa061442010-07-21 20:07:32 +00002322 F.MacroDefinitionOffsets = (const uint32_t *)BlobStart;
2323 F.NumPreallocatedPreprocessingEntities = Record[0];
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002324 unsigned LocalBasePreprocessedEntityID = Record[1];
2325 F.LocalNumMacroDefinitions = Record[2];
2326 unsigned LocalBaseMacroID = Record[3];
Douglas Gregora863b4b2011-08-04 16:36:56 +00002327
Douglas Gregor4a9c39a2011-07-21 00:47:40 +00002328 unsigned StartingID;
2329 if (PP) {
2330 if (!PP->getPreprocessingRecord())
2331 PP->createPreprocessingRecord(true);
2332 if (!PP->getPreprocessingRecord()->getExternalSource())
2333 PP->getPreprocessingRecord()->SetExternalSource(*this);
2334 StartingID
2335 = PP->getPreprocessingRecord()
2336 ->allocateLoadedEntities(F.NumPreallocatedPreprocessingEntities);
2337 } else {
2338 // FIXME: We'll eventually want to kill this path, since it assumes
2339 // a particular allocation strategy in the preprocessing record.
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002340 StartingID = getTotalNumPreprocessedEntities()
2341 - F.NumPreallocatedPreprocessingEntities;
Douglas Gregor4a9c39a2011-07-21 00:47:40 +00002342 }
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00002343 F.BaseMacroDefinitionID = getTotalNumMacroDefinitions();
2344 F.BasePreprocessedEntityID = StartingID;
Douglas Gregor4a9c39a2011-07-21 00:47:40 +00002345
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002346 if (F.NumPreallocatedPreprocessingEntities > 0) {
2347 // Introduce the global -> local mapping for preprocessed entities in
2348 // this module.
2349 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
2350
2351 // Introduce the local -> global mapping for preprocessed entities in
2352 // this module.
2353 F.PreprocessedEntityRemap.insert(
2354 std::make_pair(LocalBasePreprocessedEntityID,
2355 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
2356 }
2357
2358
Douglas Gregora863b4b2011-08-04 16:36:56 +00002359 if (F.LocalNumMacroDefinitions > 0) {
2360 // Introduce the global -> local mapping for macro definitions within
2361 // this module.
2362 GlobalMacroDefinitionMap.insert(
2363 std::make_pair(getTotalNumMacroDefinitions() + 1, &F));
2364
2365 // Introduce the local -> global mapping for macro definitions within
2366 // this module.
2367 F.MacroDefinitionRemap.insert(
2368 std::make_pair(LocalBaseMacroID,
2369 F.BaseMacroDefinitionID - LocalBaseMacroID));
2370
2371 MacroDefinitionsLoaded.resize(
Douglas Gregor270e0142011-07-20 01:29:15 +00002372 MacroDefinitionsLoaded.size() + F.LocalNumMacroDefinitions);
Douglas Gregora863b4b2011-08-04 16:36:56 +00002373 }
2374
Douglas Gregoraae92242010-03-19 21:51:54 +00002375 break;
Douglas Gregor4a9c39a2011-07-21 00:47:40 +00002376 }
2377
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002378 case DECL_UPDATE_OFFSETS: {
2379 if (Record.size() % 2 != 0) {
2380 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2381 return Failure;
2382 }
2383 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Douglas Gregorf7180622011-08-03 15:48:04 +00002384 DeclUpdateOffsets[getGlobalDeclID(F, Record[I])]
2385 .push_back(std::make_pair(&F, Record[I+1]));
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002386 break;
2387 }
2388
Sebastian Redl539c5062010-08-18 23:57:32 +00002389 case DECL_REPLACEMENTS: {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002390 if (Record.size() % 2 != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002391 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002392 return Failure;
2393 }
2394 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Douglas Gregorf7180622011-08-03 15:48:04 +00002395 ReplacedDecls[getGlobalDeclID(F, Record[I])]
2396 = std::make_pair(&F, Record[I+1]);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002397 break;
2398 }
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002399
2400 case CXX_BASE_SPECIFIER_OFFSETS: {
2401 if (F.LocalNumCXXBaseSpecifiers != 0) {
2402 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2403 return Failure;
2404 }
2405
2406 F.LocalNumCXXBaseSpecifiers = Record[0];
2407 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
Jonathan D. Turner3766fdb2011-07-21 21:15:19 +00002408 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002409 break;
2410 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002411
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002412 case DIAG_PRAGMA_MAPPINGS:
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002413 if (Record.size() % 2 != 0) {
2414 Error("invalid DIAG_USER_MAPPINGS block in AST file");
2415 return Failure;
2416 }
Douglas Gregor925296b2011-07-19 16:10:42 +00002417
2418 if (F.PragmaDiagMappings.empty())
2419 F.PragmaDiagMappings.swap(Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002420 else
Douglas Gregor925296b2011-07-19 16:10:42 +00002421 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
2422 Record.begin(), Record.end());
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002423 break;
Douglas Gregor09b69892011-02-10 17:09:37 +00002424
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002425 case CUDA_SPECIAL_DECL_REFS:
2426 // Later tables overwrite earlier ones.
Douglas Gregor7fb09192011-07-21 22:35:25 +00002427 // FIXME: Modules will have trouble with this.
2428 CUDASpecialDeclRefs.clear();
2429 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2430 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002431 break;
Douglas Gregor09b69892011-02-10 17:09:37 +00002432
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002433 case HEADER_SEARCH_TABLE: {
Douglas Gregor09b69892011-02-10 17:09:37 +00002434 F.HeaderFileInfoTableData = BlobStart;
2435 F.LocalNumHeaderFileInfos = Record[1];
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002436 F.HeaderFileFrameworkStrings = BlobStart + Record[2];
Douglas Gregor09b69892011-02-10 17:09:37 +00002437 if (Record[0]) {
2438 F.HeaderFileInfoTable
2439 = HeaderFileInfoLookupTable::Create(
2440 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002441 (const unsigned char *)F.HeaderFileInfoTableData,
Douglas Gregora3e41532011-07-28 20:55:49 +00002442 HeaderFileInfoTrait(*this, F,
2443 PP? &PP->getHeaderSearchInfo() : 0,
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002444 BlobStart + Record[2]));
Douglas Gregor09b69892011-02-10 17:09:37 +00002445 if (PP)
2446 PP->getHeaderSearchInfo().SetExternalSource(this);
2447 }
2448 break;
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002449 }
2450
Peter Collingbourne5df20e02011-02-15 19:46:30 +00002451 case FP_PRAGMA_OPTIONS:
2452 // Later tables overwrite earlier ones.
2453 FPPragmaOptions.swap(Record);
2454 break;
2455
2456 case OPENCL_EXTENSIONS:
2457 // Later tables overwrite earlier ones.
2458 OpenCLExtensions.swap(Record);
2459 break;
Alexis Hunt27a761d2011-05-04 23:29:54 +00002460
2461 case TENTATIVE_DEFINITIONS:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002462 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2463 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Alexis Hunt27a761d2011-05-04 23:29:54 +00002464 break;
Douglas Gregorc2fa1692011-06-28 16:20:02 +00002465
2466 case KNOWN_NAMESPACES:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002467 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2468 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregorc2fa1692011-06-28 16:20:02 +00002469 break;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002470 }
Sebastian Redl393f8b72010-07-19 20:52:06 +00002471 First = false;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002472 }
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002473 Error("premature end of bitstream in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00002474 return Failure;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002475}
2476
Douglas Gregor2fdb6b52011-08-25 20:58:51 +00002477ASTReader::ASTReadResult ASTReader::validateFileEntries(Module &M) {
2478 llvm::BitstreamCursor &SLocEntryCursor = M.SLocEntryCursor;
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00002479
Douglas Gregor2fdb6b52011-08-25 20:58:51 +00002480 for (unsigned i = 0, e = M.LocalNumSLocFileEntries; i != e; ++i) {
2481 SLocEntryCursor.JumpToBit(M.SLocFileOffsets[i]);
2482 unsigned Code = SLocEntryCursor.ReadCode();
2483 if (Code == llvm::bitc::END_BLOCK ||
2484 Code == llvm::bitc::ENTER_SUBBLOCK ||
2485 Code == llvm::bitc::DEFINE_ABBREV) {
2486 Error("incorrectly-formatted source location entry in AST file");
2487 return Failure;
2488 }
2489
2490 RecordData Record;
2491 const char *BlobStart;
2492 unsigned BlobLen;
2493 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
2494 default:
2495 Error("incorrectly-formatted source location entry in AST file");
2496 return Failure;
2497
2498 case SM_SLOC_FILE_ENTRY: {
2499 StringRef Filename(BlobStart, BlobLen);
2500 const FileEntry *File = getFileEntry(Filename);
2501
2502 if (File == 0) {
2503 std::string ErrorStr = "could not find file '";
2504 ErrorStr += Filename;
2505 ErrorStr += "' referenced by AST file";
2506 Error(ErrorStr.c_str());
2507 return IgnorePCH;
2508 }
2509
2510 if (Record.size() < 6) {
2511 Error("source location entry is incorrect");
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00002512 return Failure;
2513 }
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00002514
Douglas Gregor2fdb6b52011-08-25 20:58:51 +00002515 // The stat info from the FileEntry came from the cached stat
2516 // info of the PCH, so we cannot trust it.
2517 struct stat StatBuf;
2518 if (::stat(File->getName(), &StatBuf) != 0) {
2519 StatBuf.st_size = File->getSize();
2520 StatBuf.st_mtime = File->getModificationTime();
2521 }
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00002522
Douglas Gregor2fdb6b52011-08-25 20:58:51 +00002523 if (((off_t)Record[4] != StatBuf.st_size
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00002524#if !defined(LLVM_ON_WIN32)
Douglas Gregor2fdb6b52011-08-25 20:58:51 +00002525 // In our regression testing, the Windows file system seems to
2526 // have inconsistent modification times that sometimes
2527 // erroneously trigger this error-handling path.
2528 || (time_t)Record[5] != StatBuf.st_mtime
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00002529#endif
Douglas Gregor2fdb6b52011-08-25 20:58:51 +00002530 )) {
2531 Error(diag::err_fe_pch_file_modified, Filename);
2532 return IgnorePCH;
2533 }
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00002534
Douglas Gregor2fdb6b52011-08-25 20:58:51 +00002535 break;
2536 }
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00002537 }
2538 }
2539
2540 return Success;
2541}
2542
Sebastian Redl009e7f22010-10-05 16:15:19 +00002543ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
Douglas Gregora6895d82011-07-22 16:00:58 +00002544 ModuleKind Type) {
Douglas Gregordf0c1512011-08-18 04:12:04 +00002545 switch(ReadASTCore(FileName, Type, /*ImportedBy=*/0)) {
Sebastian Redl2abc0382010-07-16 20:41:52 +00002546 case Failure: return Failure;
2547 case IgnorePCH: return IgnorePCH;
2548 case Success: break;
2549 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002550
2551 // Here comes stuff that we only do once the entire chain is loaded.
Douglas Gregor49bf76b2011-07-21 18:46:38 +00002552
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002553 // Check the predefines buffers.
Douglas Gregor9125bd62011-07-27 16:30:06 +00002554 if (!DisableValidation && Type != MK_Module && CheckPredefinesBuffers())
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002555 return IgnorePCH;
2556
2557 if (PP) {
2558 // Initialization of keywords and pragmas occurs before the
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002559 // AST file is read, so there may be some identifiers that were
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002560 // loaded into the IdentifierTable before we intercepted the
2561 // creation of identifiers. Iterate through the list of known
2562 // identifiers and determine whether we have to establish
2563 // preprocessor definitions or top-level identifier declaration
2564 // chains for those identifiers.
2565 //
2566 // We copy the IdentifierInfo pointers to a small vector first,
2567 // since de-serializing declarations or macro definitions can add
2568 // new entries into the identifier table, invalidating the
2569 // iterators.
Douglas Gregor9125bd62011-07-27 16:30:06 +00002570 //
2571 // FIXME: We need a lazier way to load this information, e.g., by marking
2572 // the identifier data as 'dirty', so that it will be looked up in the
2573 // AST file(s) if it is uttered in the source. This could save us some
2574 // module load time.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002575 SmallVector<IdentifierInfo *, 128> Identifiers;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002576 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
2577 IdEnd = PP->getIdentifierTable().end();
2578 Id != IdEnd; ++Id)
2579 Identifiers.push_back(Id->second);
Sebastian Redlfa061442010-07-21 20:07:32 +00002580 // We need to search the tables in all files.
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00002581 for (ModuleIterator J = ModuleMgr.begin(),
2582 M = ModuleMgr.end(); J != M; ++J) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002583 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00002584 = (ASTIdentifierLookupTable *)(*J)->IdentifierLookupTable;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002585 // Not all AST files necessarily have identifier tables, only the useful
Sebastian Redl5c415f32010-07-22 17:01:13 +00002586 // ones.
2587 if (!IdTable)
2588 continue;
Sebastian Redlfa061442010-07-21 20:07:32 +00002589 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2590 IdentifierInfo *II = Identifiers[I];
2591 // Look in the on-disk hash tables for an entry for this identifier
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00002592 ASTIdentifierLookupTrait Info(*this, *(*J), II);
Sebastian Redlfa061442010-07-21 20:07:32 +00002593 std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength());
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002594 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
Sebastian Redlb293a452010-07-20 21:20:32 +00002595 if (Pos == IdTable->end())
2596 continue;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002597
Sebastian Redlb293a452010-07-20 21:20:32 +00002598 // Dereferencing the iterator has the effect of populating the
2599 // IdentifierInfo node with the various declarations it needs.
2600 (void)*Pos;
2601 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002602 }
2603 }
2604
2605 if (Context)
2606 InitializeContext(*Context);
2607
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002608 if (DeserializationListener)
2609 DeserializationListener->ReaderInitialized(this);
2610
Douglas Gregor936a5b42010-11-30 05:23:00 +00002611 // If this AST file is a precompiled preamble, then set the main file ID of
2612 // the source manager to the file source file from which the preamble was
2613 // built. This is the only valid way to use a precompiled preamble.
Douglas Gregora6895d82011-07-22 16:00:58 +00002614 if (Type == MK_Preamble) {
Douglas Gregora3b20262011-05-06 21:43:30 +00002615 if (OriginalFileID.isInvalid()) {
2616 SourceLocation Loc
2617 = SourceMgr.getLocation(FileMgr.getFile(getOriginalSourceFile()), 1, 1);
2618 if (Loc.isValid())
2619 OriginalFileID = SourceMgr.getDecomposedLoc(Loc).first;
Douglas Gregor936a5b42010-11-30 05:23:00 +00002620 }
Douglas Gregor925296b2011-07-19 16:10:42 +00002621 else {
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00002622 OriginalFileID = FileID::get(ModuleMgr.getPrimaryModule().SLocEntryBaseID
Douglas Gregor925296b2011-07-19 16:10:42 +00002623 + OriginalFileID.getOpaqueValue() - 1);
2624 }
2625
Douglas Gregora3b20262011-05-06 21:43:30 +00002626 if (!OriginalFileID.isInvalid())
2627 SourceMgr.SetPreambleFileID(OriginalFileID);
Douglas Gregor936a5b42010-11-30 05:23:00 +00002628 }
2629
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002630 return Success;
2631}
2632
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002633ASTReader::ASTReadResult ASTReader::ReadASTCore(StringRef FileName,
Douglas Gregordf0c1512011-08-18 04:12:04 +00002634 ModuleKind Type,
2635 Module *ImportedBy) {
Douglas Gregor4dd3e942011-08-19 02:29:29 +00002636 Module *M;
2637 bool NewModule;
2638 std::string ErrorStr;
2639 llvm::tie(M, NewModule) = ModuleMgr.addModule(FileName, Type, ImportedBy,
2640 ErrorStr);
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002641
Douglas Gregor4dd3e942011-08-19 02:29:29 +00002642 if (!M) {
2643 // We couldn't load the module.
2644 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
2645 + ErrorStr;
2646 Error(Msg);
2647 return Failure;
2648 }
2649
2650 if (!NewModule) {
2651 // We've already loaded this module.
2652 return Success;
2653 }
2654
2655 // FIXME: This seems rather a hack. Should CurrentDir be part of the
2656 // module?
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002657 if (FileName != "-") {
2658 CurrentDir = llvm::sys::path::parent_path(FileName);
2659 if (CurrentDir.empty()) CurrentDir = ".";
2660 }
2661
Douglas Gregor4dd3e942011-08-19 02:29:29 +00002662 Module &F = *M;
Sebastian Redl34522812010-07-16 17:50:48 +00002663 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002664 Stream.init(F.StreamFile);
Sebastian Redlfa061442010-07-21 20:07:32 +00002665 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Douglas Gregord32f0352011-07-22 06:10:01 +00002666
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002667 // Sniff for the signature.
2668 if (Stream.Read(8) != 'C' ||
2669 Stream.Read(8) != 'P' ||
2670 Stream.Read(8) != 'C' ||
2671 Stream.Read(8) != 'H') {
2672 Diag(diag::err_not_a_pch_file) << FileName;
2673 return Failure;
2674 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002675
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002676 while (!Stream.AtEndOfStream()) {
2677 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00002678
Douglas Gregor92863e42009-04-10 23:10:45 +00002679 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002680 Error("invalid record at top-level of AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002681 return Failure;
2682 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002683
2684 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregora868bbd2009-04-21 22:25:48 +00002685
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002686 // We only know the AST subblock ID.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002687 switch (BlockID) {
2688 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregor92863e42009-04-10 23:10:45 +00002689 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002690 Error("malformed BlockInfoBlock in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002691 return Failure;
2692 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002693 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00002694 case AST_BLOCK_ID:
Sebastian Redl3e31c722010-08-18 23:56:56 +00002695 switch (ReadASTBlock(F)) {
Douglas Gregor55abb232009-04-10 20:39:37 +00002696 case Success:
2697 break;
2698
2699 case Failure:
Douglas Gregor92863e42009-04-10 23:10:45 +00002700 return Failure;
Douglas Gregor55abb232009-04-10 20:39:37 +00002701
2702 case IgnorePCH:
Douglas Gregorbfbde532009-04-10 21:16:55 +00002703 // FIXME: We could consider reading through to the end of this
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002704 // AST block, skipping subblocks, to see if there are other
2705 // AST blocks elsewhere.
Douglas Gregor0bc12932009-04-27 21:28:04 +00002706
Douglas Gregor925296b2011-07-19 16:10:42 +00002707 // FIXME: We can't clear loaded slocentries anymore.
2708 //SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor0bc12932009-04-27 21:28:04 +00002709
2710 // Remove the stat cache.
Sebastian Redl34522812010-07-16 17:50:48 +00002711 if (F.StatCache)
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002712 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor0bc12932009-04-27 21:28:04 +00002713
Douglas Gregor92863e42009-04-10 23:10:45 +00002714 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00002715 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002716 break;
2717 default:
Douglas Gregor92863e42009-04-10 23:10:45 +00002718 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002719 Error("malformed block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002720 return Failure;
2721 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002722 break;
2723 }
Mike Stump11289f42009-09-09 15:08:12 +00002724 }
Douglas Gregord32f0352011-07-22 06:10:01 +00002725
Douglas Gregora6895d82011-07-22 16:00:58 +00002726 // Once read, set the Module bit base offset and update the size in
Douglas Gregord32f0352011-07-22 06:10:01 +00002727 // bits of all files we've seen.
2728 F.GlobalBitOffset = TotalModulesSizeInBits;
2729 TotalModulesSizeInBits += F.SizeInBits;
2730 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
Douglas Gregor2fdb6b52011-08-25 20:58:51 +00002731
2732 // Make sure that the files this module was built against are still available.
2733 if (!DisableValidation) {
2734 switch(validateFileEntries(*M)) {
2735 case Failure: return Failure;
2736 case IgnorePCH: return IgnorePCH;
2737 case Success: break;
2738 }
2739 }
Douglas Gregora918bab2011-08-25 21:09:44 +00002740
2741 // Preload SLocEntries.
2742 for (unsigned I = 0, N = M->PreloadSLocEntries.size(); I != N; ++I) {
2743 int Index = int(M->PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
2744 ASTReadResult Result = ReadSLocEntryRecord(Index);
2745 if (Result != Success)
2746 return Failure;
2747 }
2748
Douglas Gregor2fdb6b52011-08-25 20:58:51 +00002749
Sebastian Redl2abc0382010-07-16 20:41:52 +00002750 return Success;
2751}
2752
Sebastian Redl2c499f62010-08-18 23:56:43 +00002753void ASTReader::setPreprocessor(Preprocessor &pp) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002754 PP = &pp;
Douglas Gregor4a9c39a2011-07-21 00:47:40 +00002755
2756 if (unsigned N = getTotalNumPreprocessedEntities()) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002757 if (!PP->getPreprocessingRecord())
Douglas Gregor998caea2011-05-06 16:33:08 +00002758 PP->createPreprocessingRecord(true);
Douglas Gregor4a9c39a2011-07-21 00:47:40 +00002759 PP->getPreprocessingRecord()->SetExternalSource(*this);
2760 PP->getPreprocessingRecord()->allocateLoadedEntities(N);
Douglas Gregoraae92242010-03-19 21:51:54 +00002761 }
Douglas Gregor4a9c39a2011-07-21 00:47:40 +00002762
2763 PP->getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor49bf76b2011-07-21 18:46:38 +00002764 PP->getHeaderSearchInfo().SetExternalSource(this);
Douglas Gregoraae92242010-03-19 21:51:54 +00002765}
2766
Sebastian Redl2c499f62010-08-18 23:56:43 +00002767void ASTReader::InitializeContext(ASTContext &Ctx) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002768 Context = &Ctx;
2769 assert(Context && "Passed null context!");
2770
2771 assert(PP && "Forgot to set Preprocessor ?");
2772 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor9882a5a2010-01-04 19:18:44 +00002773 PP->setExternalSource(this);
Douglas Gregor09b69892011-02-10 17:09:37 +00002774
Douglas Gregor94619c82011-08-24 19:03:07 +00002775 // If we have any update blocks for the TU waiting, we have to add
2776 // them before we deserialize anything.
Douglas Gregordab42432011-08-12 00:15:20 +00002777 TranslationUnitDecl *TU = Ctx.getTranslationUnitDecl();
Douglas Gregor94619c82011-08-24 19:03:07 +00002778 for (ModuleIterator M = ModuleMgr.begin(), MEnd = ModuleMgr.end();
2779 M != MEnd; ++M) {
2780 Module::DeclContextInfosMap::iterator DCU
2781 = (*M)->DeclContextInfos.find(0);
2782 if (DCU != (*M)->DeclContextInfos.end()) {
2783 // Insertion could invalidate map, so grab value first.
2784 DeclContextInfo Info = DCU->second;
2785 (*M)->DeclContextInfos.erase(DCU);
2786 (*M)->DeclContextInfos[TU] = Info;
2787 }
Douglas Gregoraa433012010-10-01 01:18:02 +00002788 }
Douglas Gregordab42432011-08-12 00:15:20 +00002789
2790 // If there's a listener, notify them that we "read" the translation unit.
2791 if (DeserializationListener)
2792 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID, TU);
Douglas Gregoraa433012010-10-01 01:18:02 +00002793
Douglas Gregordab42432011-08-12 00:15:20 +00002794 // Make sure we load the declaration update records for the translation unit,
2795 // if there are any.
2796 loadDeclUpdateRecords(PREDEF_DECL_TRANSLATION_UNIT_ID, TU);
2797
2798 // Note that the translation unit has external lexical and visible storage.
2799 TU->setHasExternalLexicalStorage(true);
2800 TU->setHasExternalVisibleStorage(true);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002801
Douglas Gregoraa8a8272011-08-11 22:18:49 +00002802 // FIXME: Find a better way to deal with collisions between these
2803 // built-in types. Right now, we just ignore the problem.
2804
2805 // Load the special types.
Jonathan D. Turnerf07f1312011-08-05 23:07:10 +00002806 if (Context->getBuiltinVaListType().isNull()) {
Jonathan D. Turnerf07f1312011-08-05 23:07:10 +00002807 Context->setBuiltinVaListType(
2808 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
Douglas Gregoraa8a8272011-08-11 22:18:49 +00002809 }
2810
Douglas Gregoraa8a8272011-08-11 22:18:49 +00002811 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL]) {
2812 if (Context->ObjCProtoType.isNull())
Douglas Gregor09c4aa82011-08-11 22:04:35 +00002813 Context->ObjCProtoType = GetType(Proto);
Douglas Gregoraa8a8272011-08-11 22:18:49 +00002814 }
2815
Douglas Gregoraa8a8272011-08-11 22:18:49 +00002816 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
2817 if (!Context->CFConstantStringTypeDecl)
Jonathan D. Turnerf07f1312011-08-05 23:07:10 +00002818 Context->setCFConstantStringType(GetType(String));
Douglas Gregoraa8a8272011-08-11 22:18:49 +00002819 }
2820
2821 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
2822 QualType FileType = GetType(File);
2823 if (FileType.isNull()) {
2824 Error("FILE type is NULL");
2825 return;
2826 }
2827
2828 if (!Context->FILEDecl) {
Jonathan D. Turnerf07f1312011-08-05 23:07:10 +00002829 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
2830 Context->setFILEDecl(Typedef->getDecl());
2831 else {
2832 const TagType *Tag = FileType->getAs<TagType>();
2833 if (!Tag) {
2834 Error("Invalid FILE type in AST file");
2835 return;
2836 }
2837 Context->setFILEDecl(Tag->getDecl());
2838 }
Douglas Gregor27821ce2009-07-07 16:35:42 +00002839 }
Douglas Gregoraa8a8272011-08-11 22:18:49 +00002840 }
2841
2842 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
2843 QualType Jmp_bufType = GetType(Jmp_buf);
2844 if (Jmp_bufType.isNull()) {
2845 Error("jmp_buf type is NULL");
2846 return;
2847 }
2848
2849 if (!Context->jmp_bufDecl) {
Jonathan D. Turnerf07f1312011-08-05 23:07:10 +00002850 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
2851 Context->setjmp_bufDecl(Typedef->getDecl());
2852 else {
2853 const TagType *Tag = Jmp_bufType->getAs<TagType>();
2854 if (!Tag) {
2855 Error("Invalid jmp_buf type in AST file");
2856 return;
2857 }
2858 Context->setjmp_bufDecl(Tag->getDecl());
2859 }
Mike Stumpa4de80b2009-07-28 02:25:19 +00002860 }
Douglas Gregoraa8a8272011-08-11 22:18:49 +00002861 }
2862
2863 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
2864 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
2865 if (Sigjmp_bufType.isNull()) {
2866 Error("sigjmp_buf type is NULL");
2867 return;
2868 }
2869
2870 if (!Context->sigjmp_bufDecl) {
Jonathan D. Turnerf07f1312011-08-05 23:07:10 +00002871 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
2872 Context->setsigjmp_bufDecl(Typedef->getDecl());
2873 else {
2874 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
2875 assert(Tag && "Invalid sigjmp_buf type in AST file");
2876 Context->setsigjmp_bufDecl(Tag->getDecl());
2877 }
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002878 }
Jonathan D. Turnerf07f1312011-08-05 23:07:10 +00002879 }
Richard Smith02e85f32011-04-14 22:09:26 +00002880
Douglas Gregoraa8a8272011-08-11 22:18:49 +00002881 if (unsigned ObjCIdRedef
2882 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
2883 if (Context->ObjCIdRedefinitionType.isNull())
2884 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
2885 }
2886
2887 if (unsigned ObjCClassRedef
2888 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
2889 if (Context->ObjCClassRedefinitionType.isNull())
2890 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
2891 }
2892
2893 if (unsigned ObjCSelRedef
2894 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
2895 if (Context->ObjCSelRedefinitionType.isNull())
2896 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
2897 }
2898
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002899 ReadPragmaDiagnosticMappings(Context->getDiagnostics());
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002900
2901 // If there were any CUDA special declarations, deserialize them.
2902 if (!CUDASpecialDeclRefs.empty()) {
2903 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
2904 Context->setcudaConfigureCallDecl(
2905 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
2906 }
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002907}
2908
Douglas Gregor45fe0362009-05-12 01:31:05 +00002909/// \brief Retrieve the name of the original source file name
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002910/// directly from the AST file, without actually loading the AST
Douglas Gregor45fe0362009-05-12 01:31:05 +00002911/// file.
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002912std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +00002913 FileManager &FileMgr,
Daniel Dunbar3b951482009-12-03 09:13:06 +00002914 Diagnostic &Diags) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002915 // Open the AST file.
Douglas Gregor45fe0362009-05-12 01:31:05 +00002916 std::string ErrStr;
2917 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner5159f612010-11-23 08:35:12 +00002918 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregor45fe0362009-05-12 01:31:05 +00002919 if (!Buffer) {
Daniel Dunbar3b951482009-12-03 09:13:06 +00002920 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002921 return std::string();
2922 }
2923
2924 // Initialize the stream
2925 llvm::BitstreamReader StreamFile;
2926 llvm::BitstreamCursor Stream;
Mike Stump11289f42009-09-09 15:08:12 +00002927 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregor45fe0362009-05-12 01:31:05 +00002928 (const unsigned char *)Buffer->getBufferEnd());
2929 Stream.init(StreamFile);
2930
2931 // Sniff for the signature.
2932 if (Stream.Read(8) != 'C' ||
2933 Stream.Read(8) != 'P' ||
2934 Stream.Read(8) != 'C' ||
2935 Stream.Read(8) != 'H') {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002936 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002937 return std::string();
2938 }
2939
2940 RecordData Record;
2941 while (!Stream.AtEndOfStream()) {
2942 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00002943
Douglas Gregor45fe0362009-05-12 01:31:05 +00002944 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2945 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump11289f42009-09-09 15:08:12 +00002946
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002947 // We only know the AST subblock ID.
Douglas Gregor45fe0362009-05-12 01:31:05 +00002948 switch (BlockID) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002949 case AST_BLOCK_ID:
2950 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002951 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002952 return std::string();
2953 }
2954 break;
Mike Stump11289f42009-09-09 15:08:12 +00002955
Douglas Gregor45fe0362009-05-12 01:31:05 +00002956 default:
2957 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002958 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002959 return std::string();
2960 }
2961 break;
2962 }
2963 continue;
2964 }
2965
2966 if (Code == llvm::bitc::END_BLOCK) {
2967 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002968 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002969 return std::string();
2970 }
2971 continue;
2972 }
2973
2974 if (Code == llvm::bitc::DEFINE_ABBREV) {
2975 Stream.ReadAbbrevRecord();
2976 continue;
2977 }
2978
2979 Record.clear();
2980 const char *BlobStart = 0;
2981 unsigned BlobLen = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002982 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl539c5062010-08-18 23:57:32 +00002983 == ORIGINAL_FILE_NAME)
Douglas Gregor45fe0362009-05-12 01:31:05 +00002984 return std::string(BlobStart, BlobLen);
Mike Stump11289f42009-09-09 15:08:12 +00002985 }
Douglas Gregor45fe0362009-05-12 01:31:05 +00002986
2987 return std::string();
2988}
2989
Douglas Gregor55abb232009-04-10 20:39:37 +00002990/// \brief Parse the record that corresponds to a LangOptions data
2991/// structure.
2992///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002993/// This routine parses the language options from the AST file and then gives
2994/// them to the AST listener if one is set.
Douglas Gregor55abb232009-04-10 20:39:37 +00002995///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002996/// \returns true if the listener deems the file unacceptable, false otherwise.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002997bool ASTReader::ParseLanguageOptions(
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002998 const SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002999 if (Listener) {
3000 LangOptions LangOpts;
Mike Stump11289f42009-09-09 15:08:12 +00003001
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003002 #define PARSE_LANGOPT(Option) \
3003 LangOpts.Option = Record[Idx]; \
3004 ++Idx
Mike Stump11289f42009-09-09 15:08:12 +00003005
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003006 unsigned Idx = 0;
3007 PARSE_LANGOPT(Trigraphs);
3008 PARSE_LANGOPT(BCPLComment);
3009 PARSE_LANGOPT(DollarIdents);
3010 PARSE_LANGOPT(AsmPreprocessor);
3011 PARSE_LANGOPT(GNUMode);
Chandler Carruthe03aa552010-04-17 20:17:31 +00003012 PARSE_LANGOPT(GNUKeywords);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003013 PARSE_LANGOPT(ImplicitInt);
3014 PARSE_LANGOPT(Digraphs);
3015 PARSE_LANGOPT(HexFloats);
3016 PARSE_LANGOPT(C99);
Peter Collingbournea686b5f2011-04-15 00:35:23 +00003017 PARSE_LANGOPT(C1X);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003018 PARSE_LANGOPT(Microsoft);
3019 PARSE_LANGOPT(CPlusPlus);
3020 PARSE_LANGOPT(CPlusPlus0x);
3021 PARSE_LANGOPT(CXXOperatorNames);
3022 PARSE_LANGOPT(ObjC1);
3023 PARSE_LANGOPT(ObjC2);
3024 PARSE_LANGOPT(ObjCNonFragileABI);
Fariborz Jahanian45878032010-02-09 19:31:38 +00003025 PARSE_LANGOPT(ObjCNonFragileABI2);
Fariborz Jahanian13f3b2f2011-01-07 18:59:25 +00003026 PARSE_LANGOPT(AppleKext);
Ted Kremenek1d56c9e2010-12-23 21:35:43 +00003027 PARSE_LANGOPT(ObjCDefaultSynthProperties);
Douglas Gregora860e6a2011-06-14 23:20:43 +00003028 PARSE_LANGOPT(ObjCInferRelatedResultType);
Fariborz Jahanian62c56022010-04-22 21:01:59 +00003029 PARSE_LANGOPT(NoConstantCFStrings);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003030 PARSE_LANGOPT(PascalStrings);
3031 PARSE_LANGOPT(WritableStrings);
3032 PARSE_LANGOPT(LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +00003033 PARSE_LANGOPT(AltiVec);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003034 PARSE_LANGOPT(Exceptions);
Anders Carlssonce8dd3a2011-02-19 23:53:54 +00003035 PARSE_LANGOPT(ObjCExceptions);
Anders Carlsson6bbd2682011-02-23 03:04:54 +00003036 PARSE_LANGOPT(CXXExceptions);
3037 PARSE_LANGOPT(SjLjExceptions);
Douglas Gregordbe39272011-02-01 15:15:22 +00003038 PARSE_LANGOPT(MSBitfields);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003039 PARSE_LANGOPT(NeXTRuntime);
3040 PARSE_LANGOPT(Freestanding);
3041 PARSE_LANGOPT(NoBuiltin);
3042 PARSE_LANGOPT(ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +00003043 PARSE_LANGOPT(POSIXThreads);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003044 PARSE_LANGOPT(Blocks);
3045 PARSE_LANGOPT(EmitAllDecls);
3046 PARSE_LANGOPT(MathErrno);
Chris Lattner51924e512010-06-26 21:25:03 +00003047 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
3048 Record[Idx++]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003049 PARSE_LANGOPT(HeinousExtensions);
3050 PARSE_LANGOPT(Optimize);
3051 PARSE_LANGOPT(OptimizeSize);
3052 PARSE_LANGOPT(Static);
3053 PARSE_LANGOPT(PICLevel);
3054 PARSE_LANGOPT(GNUInline);
3055 PARSE_LANGOPT(NoInline);
Chandler Carruth7ffce732011-04-23 20:05:38 +00003056 PARSE_LANGOPT(Deprecated);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003057 PARSE_LANGOPT(AccessControl);
3058 PARSE_LANGOPT(CharIsSigned);
John Thompsoned4e2952009-11-05 20:14:16 +00003059 PARSE_LANGOPT(ShortWChar);
Argyrios Kyrtzidisa88942a2011-01-15 02:56:16 +00003060 PARSE_LANGOPT(ShortEnums);
Chris Lattner51924e512010-06-26 21:25:03 +00003061 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
John McCall457a04e2010-10-22 21:05:15 +00003062 LangOpts.setVisibilityMode((Visibility)Record[Idx++]);
Daniel Dunbar143021e2009-09-21 04:16:19 +00003063 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
Chris Lattner51924e512010-06-26 21:25:03 +00003064 Record[Idx++]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003065 PARSE_LANGOPT(InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +00003066 PARSE_LANGOPT(OpenCL);
Peter Collingbourne546d0792010-12-01 19:14:57 +00003067 PARSE_LANGOPT(CUDA);
Mike Stumpd9546382009-12-12 01:27:46 +00003068 PARSE_LANGOPT(CatchUndefined);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003069 PARSE_LANGOPT(DefaultFPContract);
Roman Divackydc1f68d2011-03-01 17:36:40 +00003070 PARSE_LANGOPT(ElideConstructors);
3071 PARSE_LANGOPT(SpellChecking);
Roman Divacky65b88cd2011-03-01 17:40:53 +00003072 PARSE_LANGOPT(MRTD);
John McCall31168b02011-06-15 23:02:42 +00003073 PARSE_LANGOPT(ObjCAutoRefCount);
Douglas Gregor49b236a2011-08-04 15:46:00 +00003074 PARSE_LANGOPT(ObjCInferRelatedReturnType);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003075 #undef PARSE_LANGOPT
Douglas Gregor55abb232009-04-10 20:39:37 +00003076
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003077 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor55abb232009-04-10 20:39:37 +00003078 }
Douglas Gregor55abb232009-04-10 20:39:37 +00003079
3080 return false;
3081}
3082
Douglas Gregor14f77162011-08-25 18:03:05 +00003083namespace {
3084 /// \brief Visitor used by ASTReader::ReadPreprocessedEntities() to load
3085 /// all of the preprocessed entities within a module.
3086 class ReadPreprocessedEntitiesVisitor {
3087 ASTReader &Reader;
3088
3089 public:
3090 explicit ReadPreprocessedEntitiesVisitor(ASTReader &Reader)
3091 : Reader(Reader) { }
3092
3093 static bool visit(Module &M, bool Preorder, void *UserData) {
3094 if (Preorder)
3095 return false;
3096
3097 ReadPreprocessedEntitiesVisitor *This
3098 = static_cast<ReadPreprocessedEntitiesVisitor *>(UserData);
3099
3100 if (!M.PreprocessorDetailCursor.getBitStreamReader())
3101 return false;
3102
3103 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
3104 M.PreprocessorDetailCursor.JumpToBit(M.PreprocessorDetailStartOffset);
3105 while (This->Reader.LoadPreprocessedEntity(M)) { }
3106 return false;
3107 }
3108 };
3109}
Douglas Gregor92a96f52011-02-08 21:58:10 +00003110
Douglas Gregor14f77162011-08-25 18:03:05 +00003111void ASTReader::ReadPreprocessedEntities() {
3112 ReadPreprocessedEntitiesVisitor Visitor(*this);
3113 ModuleMgr.visitDepthFirst(&ReadPreprocessedEntitiesVisitor::visit, &Visitor);
Douglas Gregoraae92242010-03-19 21:51:54 +00003114}
3115
Douglas Gregor46c50012011-02-11 19:46:30 +00003116PreprocessedEntity *ASTReader::ReadPreprocessedEntityAtOffset(uint64_t Offset) {
Douglas Gregord32f0352011-07-22 06:10:01 +00003117 RecordLocation Loc = getLocalBitOffset(Offset);
Douglas Gregorf88e35b2010-11-30 06:16:57 +00003118
Douglas Gregor92a96f52011-02-08 21:58:10 +00003119 // Keep track of where we are in the stream, then jump back there
3120 // after reading this entity.
Douglas Gregord32f0352011-07-22 06:10:01 +00003121 SavedStreamPosition SavedPosition(Loc.F->PreprocessorDetailCursor);
3122 Loc.F->PreprocessorDetailCursor.JumpToBit(Loc.Offset);
3123 return LoadPreprocessedEntity(*Loc.F);
Douglas Gregorf88e35b2010-11-30 06:16:57 +00003124}
3125
Douglas Gregor69e94642011-08-25 18:14:34 +00003126namespace {
3127 /// \brief Visitor used to search for information about a header file.
3128 class HeaderFileInfoVisitor {
3129 ASTReader &Reader;
3130 const FileEntry *FE;
3131
3132 llvm::Optional<HeaderFileInfo> HFI;
3133
3134 public:
3135 HeaderFileInfoVisitor(ASTReader &Reader, const FileEntry *FE)
3136 : Reader(Reader), FE(FE) { }
3137
3138 static bool visit(Module &M, void *UserData) {
3139 HeaderFileInfoVisitor *This
3140 = static_cast<HeaderFileInfoVisitor *>(UserData);
3141
3142 HeaderFileInfoTrait Trait(This->Reader, M,
3143 &This->Reader.getPreprocessor().getHeaderSearchInfo(),
3144 M.HeaderFileFrameworkStrings,
3145 This->FE->getName());
3146
3147 HeaderFileInfoLookupTable *Table
3148 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
3149 if (!Table)
3150 return false;
3151
3152 // Look in the on-disk hash table for an entry for this file name.
3153 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE->getName(),
3154 &Trait);
3155 if (Pos == Table->end())
3156 return false;
3157
3158 This->HFI = *Pos;
3159 return true;
3160 }
3161
3162 llvm::Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
3163 };
3164}
3165
Douglas Gregor09b69892011-02-10 17:09:37 +00003166HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Douglas Gregor69e94642011-08-25 18:14:34 +00003167 HeaderFileInfoVisitor Visitor(*this, FE);
3168 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
3169 if (llvm::Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) {
Douglas Gregor09b69892011-02-10 17:09:37 +00003170 if (Listener)
Douglas Gregor69e94642011-08-25 18:14:34 +00003171 Listener->ReadHeaderFileInfo(*HFI, FE->getUID());
3172 return *HFI;
Douglas Gregor09b69892011-02-10 17:09:37 +00003173 }
3174
3175 return HeaderFileInfo();
3176}
3177
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00003178void ASTReader::ReadPragmaDiagnosticMappings(Diagnostic &Diag) {
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00003179 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
3180 Module &F = *(*I);
Douglas Gregor925296b2011-07-19 16:10:42 +00003181 unsigned Idx = 0;
3182 while (Idx < F.PragmaDiagMappings.size()) {
3183 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
3184 while (1) {
3185 assert(Idx < F.PragmaDiagMappings.size() &&
3186 "Invalid data, didn't find '-1' marking end of diag/map pairs");
3187 if (Idx >= F.PragmaDiagMappings.size()) {
3188 break; // Something is messed up but at least avoid infinite loop in
3189 // release build.
3190 }
3191 unsigned DiagID = F.PragmaDiagMappings[Idx++];
3192 if (DiagID == (unsigned)-1) {
3193 break; // no more diag/map pairs for this location.
3194 }
3195 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++];
3196 Diag.setDiagnosticMapping(DiagID, Map, Loc);
3197 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00003198 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00003199 }
3200}
3201
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003202/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redl2c499f62010-08-18 23:56:43 +00003203ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Douglas Gregor5204bde2011-08-02 16:26:37 +00003204 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
Jonathan D. Turner35005682011-07-20 21:31:32 +00003205 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
Douglas Gregor8ab4ea82011-07-29 00:21:44 +00003206 Module *M = I->second;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003207 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003208}
3209
3210/// \brief Read and return the type with the given index..
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003211///
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003212/// The index is the type ID, shifted and minus the number of predefs. This
3213/// routine actually reads the record corresponding to the type at the given
3214/// location. It is a helper routine for GetType, which deals with reading type
3215/// IDs.
Douglas Gregor903b7e92011-07-22 00:38:23 +00003216QualType ASTReader::readTypeRecord(unsigned Index) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003217 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003218 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00003219
Douglas Gregorfeb84b02009-04-14 21:18:50 +00003220 // Keep track of where we are in the stream, then jump back there
3221 // after reading this type.
Douglas Gregor12bfa382009-10-17 00:13:19 +00003222 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00003223
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003224 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redleaa4ade2010-08-11 18:52:41 +00003225
Douglas Gregor1342e842009-07-06 18:54:52 +00003226 // Note that we are loading a type record.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00003227 Deserializing AType(this);
Mike Stump11289f42009-09-09 15:08:12 +00003228
Douglas Gregor903b7e92011-07-22 00:38:23 +00003229 unsigned Idx = 0;
Sebastian Redl2c373b92010-10-05 15:59:54 +00003230 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003231 RecordData Record;
Douglas Gregor12bfa382009-10-17 00:13:19 +00003232 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl539c5062010-08-18 23:57:32 +00003233 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
3234 case TYPE_EXT_QUAL: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003235 if (Record.size() != 2) {
3236 Error("Incorrect encoding of extended qualifier type");
3237 return QualType();
3238 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003239 QualType Base = readType(*Loc.F, Record, Idx);
3240 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
John McCall8ccfcb52009-09-24 19:53:00 +00003241 return Context->getQualifiedType(Base, Quals);
Douglas Gregor455b8f42009-04-15 22:00:08 +00003242 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003243
Sebastian Redl539c5062010-08-18 23:57:32 +00003244 case TYPE_COMPLEX: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003245 if (Record.size() != 1) {
3246 Error("Incorrect encoding of complex type");
3247 return QualType();
3248 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003249 QualType ElemType = readType(*Loc.F, Record, Idx);
Chris Lattner8575daa2009-04-27 21:45:14 +00003250 return Context->getComplexType(ElemType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003251 }
3252
Sebastian Redl539c5062010-08-18 23:57:32 +00003253 case TYPE_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003254 if (Record.size() != 1) {
3255 Error("Incorrect encoding of pointer type");
3256 return QualType();
3257 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003258 QualType PointeeType = readType(*Loc.F, Record, Idx);
Chris Lattner8575daa2009-04-27 21:45:14 +00003259 return Context->getPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003260 }
3261
Sebastian Redl539c5062010-08-18 23:57:32 +00003262 case TYPE_BLOCK_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003263 if (Record.size() != 1) {
3264 Error("Incorrect encoding of block pointer type");
3265 return QualType();
3266 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003267 QualType PointeeType = readType(*Loc.F, Record, Idx);
Chris Lattner8575daa2009-04-27 21:45:14 +00003268 return Context->getBlockPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003269 }
3270
Sebastian Redl539c5062010-08-18 23:57:32 +00003271 case TYPE_LVALUE_REFERENCE: {
Richard Smith0f538462011-04-12 10:38:03 +00003272 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003273 Error("Incorrect encoding of lvalue reference type");
3274 return QualType();
3275 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003276 QualType PointeeType = readType(*Loc.F, Record, Idx);
Richard Smith0f538462011-04-12 10:38:03 +00003277 return Context->getLValueReferenceType(PointeeType, Record[1]);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003278 }
3279
Sebastian Redl539c5062010-08-18 23:57:32 +00003280 case TYPE_RVALUE_REFERENCE: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003281 if (Record.size() != 1) {
3282 Error("Incorrect encoding of rvalue reference type");
3283 return QualType();
3284 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003285 QualType PointeeType = readType(*Loc.F, Record, Idx);
Chris Lattner8575daa2009-04-27 21:45:14 +00003286 return Context->getRValueReferenceType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003287 }
3288
Sebastian Redl539c5062010-08-18 23:57:32 +00003289 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidisee776bc2010-07-02 11:55:15 +00003290 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003291 Error("Incorrect encoding of member pointer type");
3292 return QualType();
3293 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003294 QualType PointeeType = readType(*Loc.F, Record, Idx);
3295 QualType ClassType = readType(*Loc.F, Record, Idx);
Douglas Gregor0cdc8322010-12-10 17:03:06 +00003296 if (PointeeType.isNull() || ClassType.isNull())
3297 return QualType();
3298
Chris Lattner8575daa2009-04-27 21:45:14 +00003299 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003300 }
3301
Sebastian Redl539c5062010-08-18 23:57:32 +00003302 case TYPE_CONSTANT_ARRAY: {
Douglas Gregor903b7e92011-07-22 00:38:23 +00003303 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003304 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3305 unsigned IndexTypeQuals = Record[2];
3306 unsigned Idx = 3;
3307 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor04318252009-07-06 15:59:29 +00003308 return Context->getConstantArrayType(ElementType, Size,
3309 ASM, IndexTypeQuals);
3310 }
3311
Sebastian Redl539c5062010-08-18 23:57:32 +00003312 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregor903b7e92011-07-22 00:38:23 +00003313 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003314 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3315 unsigned IndexTypeQuals = Record[2];
Chris Lattner8575daa2009-04-27 21:45:14 +00003316 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003317 }
3318
Sebastian Redl539c5062010-08-18 23:57:32 +00003319 case TYPE_VARIABLE_ARRAY: {
Douglas Gregor903b7e92011-07-22 00:38:23 +00003320 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00003321 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3322 unsigned IndexTypeQuals = Record[2];
Sebastian Redl2c373b92010-10-05 15:59:54 +00003323 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
3324 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
3325 return Context->getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor04318252009-07-06 15:59:29 +00003326 ASM, IndexTypeQuals,
3327 SourceRange(LBLoc, RBLoc));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003328 }
3329
Sebastian Redl539c5062010-08-18 23:57:32 +00003330 case TYPE_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00003331 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003332 Error("incorrect encoding of vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003333 return QualType();
3334 }
3335
Douglas Gregor903b7e92011-07-22 00:38:23 +00003336 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003337 unsigned NumElements = Record[1];
Bob Wilsonaeb56442010-11-10 21:56:12 +00003338 unsigned VecKind = Record[2];
Chris Lattner37141f42010-06-23 06:00:24 +00003339 return Context->getVectorType(ElementType, NumElements,
Bob Wilsonaeb56442010-11-10 21:56:12 +00003340 (VectorType::VectorKind)VecKind);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003341 }
3342
Sebastian Redl539c5062010-08-18 23:57:32 +00003343 case TYPE_EXT_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00003344 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003345 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003346 return QualType();
3347 }
3348
Douglas Gregor903b7e92011-07-22 00:38:23 +00003349 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003350 unsigned NumElements = Record[1];
Chris Lattner8575daa2009-04-27 21:45:14 +00003351 return Context->getExtVectorType(ElementType, NumElements);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003352 }
3353
Sebastian Redl539c5062010-08-18 23:57:32 +00003354 case TYPE_FUNCTION_NO_PROTO: {
John McCall31168b02011-06-15 23:02:42 +00003355 if (Record.size() != 6) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00003356 Error("incorrect encoding of no-proto function type");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003357 return QualType();
3358 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003359 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCall31168b02011-06-15 23:02:42 +00003360 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
3361 (CallingConv)Record[4], Record[5]);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00003362 return Context->getFunctionNoProtoType(ResultType, Info);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003363 }
3364
Sebastian Redl539c5062010-08-18 23:57:32 +00003365 case TYPE_FUNCTION_PROTO: {
Douglas Gregor903b7e92011-07-22 00:38:23 +00003366 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCalldb40c7f2010-12-14 08:05:40 +00003367
3368 FunctionProtoType::ExtProtoInfo EPI;
3369 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
Eli Friedmanc5b20b52011-04-09 08:18:08 +00003370 /*hasregparm*/ Record[2],
3371 /*regparm*/ Record[3],
John McCall31168b02011-06-15 23:02:42 +00003372 static_cast<CallingConv>(Record[4]),
3373 /*produces*/ Record[5]);
John McCalldb40c7f2010-12-14 08:05:40 +00003374
John McCall31168b02011-06-15 23:02:42 +00003375 unsigned Idx = 6;
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003376 unsigned NumParams = Record[Idx++];
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003377 SmallVector<QualType, 16> ParamTypes;
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003378 for (unsigned I = 0; I != NumParams; ++I)
Douglas Gregor903b7e92011-07-22 00:38:23 +00003379 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
John McCalldb40c7f2010-12-14 08:05:40 +00003380
3381 EPI.Variadic = Record[Idx++];
3382 EPI.TypeQuals = Record[Idx++];
Douglas Gregordb9d6642011-01-26 05:01:58 +00003383 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003384 ExceptionSpecificationType EST =
3385 static_cast<ExceptionSpecificationType>(Record[Idx++]);
3386 EPI.ExceptionSpecType = EST;
3387 if (EST == EST_Dynamic) {
3388 EPI.NumExceptions = Record[Idx++];
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003389 SmallVector<QualType, 2> Exceptions;
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003390 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
Douglas Gregor903b7e92011-07-22 00:38:23 +00003391 Exceptions.push_back(readType(*Loc.F, Record, Idx));
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003392 EPI.Exceptions = Exceptions.data();
3393 } else if (EST == EST_ComputedNoexcept) {
3394 EPI.NoexceptExpr = ReadExpr(*Loc.F);
3395 }
Jay Foad7d0479f2009-05-21 09:52:38 +00003396 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
John McCalldb40c7f2010-12-14 08:05:40 +00003397 EPI);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003398 }
3399
Douglas Gregor7fb09192011-07-21 22:35:25 +00003400 case TYPE_UNRESOLVED_USING: {
3401 unsigned Idx = 0;
John McCallb96ec562009-12-04 22:46:56 +00003402 return Context->getTypeDeclType(
Douglas Gregor7fb09192011-07-21 22:35:25 +00003403 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
3404 }
3405
Sebastian Redl539c5062010-08-18 23:57:32 +00003406 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003407 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003408 Error("incorrect encoding of typedef type");
3409 return QualType();
3410 }
Douglas Gregor7fb09192011-07-21 22:35:25 +00003411 unsigned Idx = 0;
3412 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
Douglas Gregor903b7e92011-07-22 00:38:23 +00003413 QualType Canonical = readType(*Loc.F, Record, Idx);
Douglas Gregorf86c9392010-10-26 00:51:02 +00003414 if (!Canonical.isNull())
3415 Canonical = Context->getCanonicalType(Canonical);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003416 return Context->getTypedefType(Decl, Canonical);
3417 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003418
Sebastian Redl539c5062010-08-18 23:57:32 +00003419 case TYPE_TYPEOF_EXPR:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003420 return Context->getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003421
Sebastian Redl539c5062010-08-18 23:57:32 +00003422 case TYPE_TYPEOF: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003423 if (Record.size() != 1) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003424 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003425 return QualType();
3426 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003427 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Chris Lattner8575daa2009-04-27 21:45:14 +00003428 return Context->getTypeOfType(UnderlyingType);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003429 }
Mike Stump11289f42009-09-09 15:08:12 +00003430
Sebastian Redl539c5062010-08-18 23:57:32 +00003431 case TYPE_DECLTYPE:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003432 return Context->getDecltypeType(ReadExpr(*Loc.F));
Anders Carlsson81df7b82009-06-24 19:06:50 +00003433
Alexis Hunte852b102011-05-24 22:41:36 +00003434 case TYPE_UNARY_TRANSFORM: {
Douglas Gregor903b7e92011-07-22 00:38:23 +00003435 QualType BaseType = readType(*Loc.F, Record, Idx);
3436 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Alexis Hunte852b102011-05-24 22:41:36 +00003437 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
3438 return Context->getUnaryTransformType(BaseType, UnderlyingType, UKind);
3439 }
3440
Richard Smith30482bc2011-02-20 03:19:35 +00003441 case TYPE_AUTO:
Douglas Gregor903b7e92011-07-22 00:38:23 +00003442 return Context->getAutoType(readType(*Loc.F, Record, Idx));
Richard Smith30482bc2011-02-20 03:19:35 +00003443
Sebastian Redl539c5062010-08-18 23:57:32 +00003444 case TYPE_RECORD: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003445 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003446 Error("incorrect encoding of record type");
3447 return QualType();
3448 }
Douglas Gregor7fb09192011-07-21 22:35:25 +00003449 unsigned Idx = 0;
3450 bool IsDependent = Record[Idx++];
3451 QualType T
3452 = Context->getRecordType(ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx));
John McCall424cec92011-01-19 06:33:43 +00003453 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003454 return T;
3455 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003456
Sebastian Redl539c5062010-08-18 23:57:32 +00003457 case TYPE_ENUM: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003458 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003459 Error("incorrect encoding of enum type");
3460 return QualType();
3461 }
Douglas Gregor7fb09192011-07-21 22:35:25 +00003462 unsigned Idx = 0;
3463 bool IsDependent = Record[Idx++];
3464 QualType T
3465 = Context->getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
John McCall424cec92011-01-19 06:33:43 +00003466 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003467 return T;
3468 }
Douglas Gregor1daeb692009-04-13 18:14:40 +00003469
John McCall81904512011-01-06 01:58:22 +00003470 case TYPE_ATTRIBUTED: {
3471 if (Record.size() != 3) {
3472 Error("incorrect encoding of attributed type");
3473 return QualType();
3474 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003475 QualType modifiedType = readType(*Loc.F, Record, Idx);
3476 QualType equivalentType = readType(*Loc.F, Record, Idx);
John McCall81904512011-01-06 01:58:22 +00003477 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
3478 return Context->getAttributedType(kind, modifiedType, equivalentType);
3479 }
3480
Abramo Bagnara924a8f32010-12-10 16:29:40 +00003481 case TYPE_PAREN: {
3482 if (Record.size() != 1) {
3483 Error("incorrect encoding of paren type");
3484 return QualType();
3485 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003486 QualType InnerType = readType(*Loc.F, Record, Idx);
Abramo Bagnara924a8f32010-12-10 16:29:40 +00003487 return Context->getParenType(InnerType);
3488 }
3489
Douglas Gregord2fa7662010-12-20 02:24:11 +00003490 case TYPE_PACK_EXPANSION: {
Douglas Gregor17328502011-02-01 15:24:58 +00003491 if (Record.size() != 2) {
Douglas Gregord2fa7662010-12-20 02:24:11 +00003492 Error("incorrect encoding of pack expansion type");
3493 return QualType();
3494 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003495 QualType Pattern = readType(*Loc.F, Record, Idx);
Douglas Gregord2fa7662010-12-20 02:24:11 +00003496 if (Pattern.isNull())
3497 return QualType();
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00003498 llvm::Optional<unsigned> NumExpansions;
3499 if (Record[1])
3500 NumExpansions = Record[1] - 1;
3501 return Context->getPackExpansionType(Pattern, NumExpansions);
Douglas Gregord2fa7662010-12-20 02:24:11 +00003502 }
3503
Sebastian Redl539c5062010-08-18 23:57:32 +00003504 case TYPE_ELABORATED: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003505 unsigned Idx = 0;
3506 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor7fb09192011-07-21 22:35:25 +00003507 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor903b7e92011-07-22 00:38:23 +00003508 QualType NamedType = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003509 return Context->getElaboratedType(Keyword, NNS, NamedType);
John McCallfcc33b02009-09-05 00:15:47 +00003510 }
3511
Sebastian Redl539c5062010-08-18 23:57:32 +00003512 case TYPE_OBJC_INTERFACE: {
Chris Lattner587cbe12009-04-22 06:45:28 +00003513 unsigned Idx = 0;
Douglas Gregor7fb09192011-07-21 22:35:25 +00003514 ObjCInterfaceDecl *ItfD
3515 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
John McCall8b07ec22010-05-15 11:32:37 +00003516 return Context->getObjCInterfaceType(ItfD);
3517 }
3518
Sebastian Redl539c5062010-08-18 23:57:32 +00003519 case TYPE_OBJC_OBJECT: {
John McCall8b07ec22010-05-15 11:32:37 +00003520 unsigned Idx = 0;
Douglas Gregor903b7e92011-07-22 00:38:23 +00003521 QualType Base = readType(*Loc.F, Record, Idx);
Chris Lattner587cbe12009-04-22 06:45:28 +00003522 unsigned NumProtos = Record[Idx++];
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003523 SmallVector<ObjCProtocolDecl*, 4> Protos;
Chris Lattner587cbe12009-04-22 06:45:28 +00003524 for (unsigned I = 0; I != NumProtos; ++I)
Douglas Gregor7fb09192011-07-21 22:35:25 +00003525 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003526 return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattner587cbe12009-04-22 06:45:28 +00003527 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003528
Sebastian Redl539c5062010-08-18 23:57:32 +00003529 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattner6e054af2009-04-22 06:40:03 +00003530 unsigned Idx = 0;
Douglas Gregor903b7e92011-07-22 00:38:23 +00003531 QualType Pointee = readType(*Loc.F, Record, Idx);
John McCall8b07ec22010-05-15 11:32:37 +00003532 return Context->getObjCObjectPointerType(Pointee);
Chris Lattner6e054af2009-04-22 06:40:03 +00003533 }
Argyrios Kyrtzidisa7a36df2009-09-29 19:42:55 +00003534
Sebastian Redl539c5062010-08-18 23:57:32 +00003535 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCallcebee162009-10-18 09:09:24 +00003536 unsigned Idx = 0;
Douglas Gregor903b7e92011-07-22 00:38:23 +00003537 QualType Parm = readType(*Loc.F, Record, Idx);
3538 QualType Replacement = readType(*Loc.F, Record, Idx);
John McCallcebee162009-10-18 09:09:24 +00003539 return
3540 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
3541 Replacement);
3542 }
John McCalle78aac42010-03-10 03:28:59 +00003543
Douglas Gregorada4b792011-01-14 02:55:32 +00003544 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
3545 unsigned Idx = 0;
Douglas Gregor903b7e92011-07-22 00:38:23 +00003546 QualType Parm = readType(*Loc.F, Record, Idx);
Douglas Gregorada4b792011-01-14 02:55:32 +00003547 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
3548 return Context->getSubstTemplateTypeParmPackType(
3549 cast<TemplateTypeParmType>(Parm),
3550 ArgPack);
3551 }
3552
Sebastian Redl539c5062010-08-18 23:57:32 +00003553 case TYPE_INJECTED_CLASS_NAME: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00003554 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
Douglas Gregor903b7e92011-07-22 00:38:23 +00003555 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00003556 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003557 // for AST reading, too much interdependencies.
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00003558 return
3559 QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCalle78aac42010-03-10 03:28:59 +00003560 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003561
Sebastian Redl539c5062010-08-18 23:57:32 +00003562 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003563 unsigned Idx = 0;
3564 unsigned Depth = Record[Idx++];
3565 unsigned Index = Record[Idx++];
3566 bool Pack = Record[Idx++];
Douglas Gregor7fb09192011-07-21 22:35:25 +00003567 TemplateTypeParmDecl *D
3568 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
Chandler Carruth08836322011-05-01 00:51:33 +00003569 return Context->getTemplateTypeParmType(Depth, Index, Pack, D);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003570 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003571
Sebastian Redl539c5062010-08-18 23:57:32 +00003572 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00003573 unsigned Idx = 0;
3574 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor7fb09192011-07-21 22:35:25 +00003575 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregora3e41532011-07-28 20:55:49 +00003576 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Douglas Gregor903b7e92011-07-22 00:38:23 +00003577 QualType Canon = readType(*Loc.F, Record, Idx);
Douglas Gregorf86c9392010-10-26 00:51:02 +00003578 if (!Canon.isNull())
3579 Canon = Context->getCanonicalType(Canon);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +00003580 return Context->getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00003581 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003582
Sebastian Redl539c5062010-08-18 23:57:32 +00003583 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003584 unsigned Idx = 0;
3585 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor7fb09192011-07-21 22:35:25 +00003586 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregora3e41532011-07-28 20:55:49 +00003587 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003588 unsigned NumArgs = Record[Idx++];
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003589 SmallVector<TemplateArgument, 8> Args;
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003590 Args.reserve(NumArgs);
3591 while (NumArgs--)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003592 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003593 return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
3594 Args.size(), Args.data());
3595 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003596
Sebastian Redl539c5062010-08-18 23:57:32 +00003597 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00003598 unsigned Idx = 0;
3599
3600 // ArrayType
Douglas Gregor903b7e92011-07-22 00:38:23 +00003601 QualType ElementType = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00003602 ArrayType::ArraySizeModifier ASM
3603 = (ArrayType::ArraySizeModifier)Record[Idx++];
3604 unsigned IndexTypeQuals = Record[Idx++];
3605
3606 // DependentSizedArrayType
Sebastian Redl2c373b92010-10-05 15:59:54 +00003607 Expr *NumElts = ReadExpr(*Loc.F);
3608 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00003609
3610 return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
3611 IndexTypeQuals, Brackets);
3612 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003613
Sebastian Redl539c5062010-08-18 23:57:32 +00003614 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003615 unsigned Idx = 0;
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003616 bool IsDependent = Record[Idx++];
Douglas Gregor5590be02011-01-15 06:45:20 +00003617 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003618 SmallVector<TemplateArgument, 8> Args;
Sebastian Redl2c373b92010-10-05 15:59:54 +00003619 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Douglas Gregor903b7e92011-07-22 00:38:23 +00003620 QualType Underlying = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003621 QualType T;
Richard Smith3f1b5d02011-05-05 21:57:07 +00003622 if (Underlying.isNull())
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003623 T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(),
3624 Args.size());
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003625 else
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003626 T = Context->getTemplateSpecializationType(Name, Args.data(),
Richard Smith3f1b5d02011-05-05 21:57:07 +00003627 Args.size(), Underlying);
John McCall424cec92011-01-19 06:33:43 +00003628 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003629 return T;
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003630 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003631 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003632 // Suppress a GCC warning
3633 return QualType();
3634}
3635
Sebastian Redl2c373b92010-10-05 15:59:54 +00003636class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redl2c499f62010-08-18 23:56:43 +00003637 ASTReader &Reader;
Douglas Gregora6895d82011-07-22 16:00:58 +00003638 Module &F;
Sebastian Redlc67764e2010-07-22 22:43:28 +00003639 llvm::BitstreamCursor &DeclsCursor;
Sebastian Redl2c499f62010-08-18 23:56:43 +00003640 const ASTReader::RecordData &Record;
John McCall8f115c62009-10-16 21:56:05 +00003641 unsigned &Idx;
3642
Sebastian Redl2c373b92010-10-05 15:59:54 +00003643 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
3644 unsigned &I) {
3645 return Reader.ReadSourceLocation(F, R, I);
3646 }
3647
Douglas Gregor7fb09192011-07-21 22:35:25 +00003648 template<typename T>
3649 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
3650 return Reader.ReadDeclAs<T>(F, Record, Idx);
3651 }
3652
John McCall8f115c62009-10-16 21:56:05 +00003653public:
Douglas Gregora6895d82011-07-22 16:00:58 +00003654 TypeLocReader(ASTReader &Reader, Module &F,
Sebastian Redl2c499f62010-08-18 23:56:43 +00003655 const ASTReader::RecordData &Record, unsigned &Idx)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003656 : Reader(Reader), F(F), DeclsCursor(F.DeclsCursor), Record(Record), Idx(Idx)
3657 { }
John McCall8f115c62009-10-16 21:56:05 +00003658
John McCall17001972009-10-18 01:05:36 +00003659 // We want compile-time assurance that we've enumerated all of
3660 // these, so unfortunately we have to declare them first, then
3661 // define them out-of-line.
3662#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +00003663#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +00003664 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +00003665#include "clang/AST/TypeLocNodes.def"
3666
John McCall17001972009-10-18 01:05:36 +00003667 void VisitFunctionTypeLoc(FunctionTypeLoc);
3668 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCall8f115c62009-10-16 21:56:05 +00003669};
3670
John McCall17001972009-10-18 01:05:36 +00003671void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCall8f115c62009-10-16 21:56:05 +00003672 // nothing to do
3673}
John McCall17001972009-10-18 01:05:36 +00003674void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003675 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorc9b7a592010-01-18 18:04:31 +00003676 if (TL.needsExtraLocalData()) {
3677 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
3678 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
3679 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
3680 TL.setModeAttr(Record[Idx++]);
3681 }
John McCall8f115c62009-10-16 21:56:05 +00003682}
John McCall17001972009-10-18 01:05:36 +00003683void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003684 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003685}
John McCall17001972009-10-18 01:05:36 +00003686void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003687 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003688}
John McCall17001972009-10-18 01:05:36 +00003689void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003690 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003691}
John McCall17001972009-10-18 01:05:36 +00003692void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003693 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003694}
John McCall17001972009-10-18 01:05:36 +00003695void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003696 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003697}
John McCall17001972009-10-18 01:05:36 +00003698void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003699 TL.setStarLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnara509357842011-03-05 14:42:21 +00003700 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003701}
John McCall17001972009-10-18 01:05:36 +00003702void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003703 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
3704 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003705 if (Record[Idx++])
Sebastian Redl2c373b92010-10-05 15:59:54 +00003706 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor12bfa382009-10-17 00:13:19 +00003707 else
John McCall17001972009-10-18 01:05:36 +00003708 TL.setSizeExpr(0);
3709}
3710void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
3711 VisitArrayTypeLoc(TL);
3712}
3713void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
3714 VisitArrayTypeLoc(TL);
3715}
3716void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
3717 VisitArrayTypeLoc(TL);
3718}
3719void TypeLocReader::VisitDependentSizedArrayTypeLoc(
3720 DependentSizedArrayTypeLoc TL) {
3721 VisitArrayTypeLoc(TL);
3722}
3723void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
3724 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003725 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003726}
3727void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003728 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003729}
3730void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003731 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003732}
3733void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003734 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
3735 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
Douglas Gregor7fb25412010-10-01 18:44:50 +00003736 TL.setTrailingReturn(Record[Idx++]);
John McCall17001972009-10-18 01:05:36 +00003737 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
Douglas Gregor7fb09192011-07-21 22:35:25 +00003738 TL.setArg(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003739 }
3740}
3741void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
3742 VisitFunctionTypeLoc(TL);
3743}
3744void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
3745 VisitFunctionTypeLoc(TL);
3746}
John McCallb96ec562009-12-04 22:46:56 +00003747void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003748 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallb96ec562009-12-04 22:46:56 +00003749}
John McCall17001972009-10-18 01:05:36 +00003750void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003751 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003752}
3753void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003754 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3755 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3756 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003757}
3758void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003759 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3760 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3761 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3762 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003763}
3764void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003765 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003766}
Alexis Hunte852b102011-05-24 22:41:36 +00003767void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
3768 TL.setKWLoc(ReadSourceLocation(Record, Idx));
3769 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3770 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3771 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
3772}
Richard Smith30482bc2011-02-20 03:19:35 +00003773void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
3774 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3775}
John McCall17001972009-10-18 01:05:36 +00003776void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003777 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003778}
3779void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003780 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003781}
John McCall81904512011-01-06 01:58:22 +00003782void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
3783 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
3784 if (TL.hasAttrOperand()) {
3785 SourceRange range;
3786 range.setBegin(ReadSourceLocation(Record, Idx));
3787 range.setEnd(ReadSourceLocation(Record, Idx));
3788 TL.setAttrOperandParensRange(range);
3789 }
3790 if (TL.hasAttrExprOperand()) {
3791 if (Record[Idx++])
3792 TL.setAttrExprOperand(Reader.ReadExpr(F));
3793 else
3794 TL.setAttrExprOperand(0);
3795 } else if (TL.hasAttrEnumOperand())
3796 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
3797}
John McCall17001972009-10-18 01:05:36 +00003798void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003799 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003800}
John McCallcebee162009-10-18 09:09:24 +00003801void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
3802 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003803 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallcebee162009-10-18 09:09:24 +00003804}
Douglas Gregorada4b792011-01-14 02:55:32 +00003805void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
3806 SubstTemplateTypeParmPackTypeLoc TL) {
3807 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3808}
John McCall17001972009-10-18 01:05:36 +00003809void TypeLocReader::VisitTemplateSpecializationTypeLoc(
3810 TemplateSpecializationTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003811 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
3812 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3813 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall0ad16662009-10-29 08:12:44 +00003814 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
3815 TL.setArgLocInfo(i,
Sebastian Redl2c373b92010-10-05 15:59:54 +00003816 Reader.GetTemplateArgumentLocInfo(F,
3817 TL.getTypePtr()->getArg(i).getKind(),
3818 Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003819}
Abramo Bagnara924a8f32010-12-10 16:29:40 +00003820void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
3821 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3822 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3823}
Abramo Bagnara6150c882010-05-11 21:36:43 +00003824void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003825 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor844cb502011-03-01 18:12:44 +00003826 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003827}
John McCalle78aac42010-03-10 03:28:59 +00003828void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003829 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalle78aac42010-03-10 03:28:59 +00003830}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00003831void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003832 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor3d0da5f2011-03-01 01:34:45 +00003833 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redl2c373b92010-10-05 15:59:54 +00003834 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003835}
John McCallc392f372010-06-11 00:33:02 +00003836void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
3837 DependentTemplateSpecializationTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003838 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregora7a795b2011-03-01 20:11:18 +00003839 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redl2c373b92010-10-05 15:59:54 +00003840 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3841 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3842 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCallc392f372010-06-11 00:33:02 +00003843 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
3844 TL.setArgLocInfo(I,
Sebastian Redl2c373b92010-10-05 15:59:54 +00003845 Reader.GetTemplateArgumentLocInfo(F,
3846 TL.getTypePtr()->getArg(I).getKind(),
3847 Record, Idx));
John McCallc392f372010-06-11 00:33:02 +00003848}
Douglas Gregord2fa7662010-12-20 02:24:11 +00003849void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
3850 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
3851}
John McCall17001972009-10-18 01:05:36 +00003852void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003853 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall8b07ec22010-05-15 11:32:37 +00003854}
3855void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3856 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003857 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3858 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003859 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003860 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003861}
John McCallfc93cf92009-10-22 22:37:11 +00003862void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003863 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCallfc93cf92009-10-22 22:37:11 +00003864}
John McCall8f115c62009-10-16 21:56:05 +00003865
Douglas Gregora6895d82011-07-22 16:00:58 +00003866TypeSourceInfo *ASTReader::GetTypeSourceInfo(Module &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003867 const RecordData &Record,
John McCall8f115c62009-10-16 21:56:05 +00003868 unsigned &Idx) {
Douglas Gregor903b7e92011-07-22 00:38:23 +00003869 QualType InfoTy = readType(F, Record, Idx);
John McCall8f115c62009-10-16 21:56:05 +00003870 if (InfoTy.isNull())
3871 return 0;
3872
John McCallbcd03502009-12-07 02:54:59 +00003873 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003874 TypeLocReader TLR(*this, F, Record, Idx);
John McCallbcd03502009-12-07 02:54:59 +00003875 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCall8f115c62009-10-16 21:56:05 +00003876 TLR.Visit(TL);
John McCallbcd03502009-12-07 02:54:59 +00003877 return TInfo;
John McCall8f115c62009-10-16 21:56:05 +00003878}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003879
Sebastian Redl539c5062010-08-18 23:57:32 +00003880QualType ASTReader::GetType(TypeID ID) {
John McCall8ccfcb52009-09-24 19:53:00 +00003881 unsigned FastQuals = ID & Qualifiers::FastMask;
3882 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003883
Sebastian Redl539c5062010-08-18 23:57:32 +00003884 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003885 QualType T;
Sebastian Redl539c5062010-08-18 23:57:32 +00003886 switch ((PredefinedTypeIDs)Index) {
3887 case PREDEF_TYPE_NULL_ID: return QualType();
3888 case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
3889 case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003890
Sebastian Redl539c5062010-08-18 23:57:32 +00003891 case PREDEF_TYPE_CHAR_U_ID:
3892 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003893 // FIXME: Check that the signedness of CharTy is correct!
Chris Lattner8575daa2009-04-27 21:45:14 +00003894 T = Context->CharTy;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003895 break;
3896
Sebastian Redl539c5062010-08-18 23:57:32 +00003897 case PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break;
3898 case PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break;
3899 case PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break;
3900 case PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break;
3901 case PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break;
3902 case PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break;
3903 case PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break;
3904 case PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break;
3905 case PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break;
3906 case PREDEF_TYPE_INT_ID: T = Context->IntTy; break;
3907 case PREDEF_TYPE_LONG_ID: T = Context->LongTy; break;
3908 case PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break;
3909 case PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break;
3910 case PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break;
3911 case PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break;
3912 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break;
3913 case PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break;
John McCall0009fcc2011-04-26 20:42:42 +00003914 case PREDEF_TYPE_BOUND_MEMBER: T = Context->BoundMemberTy; break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003915 case PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break;
John McCall31996342011-04-07 08:22:57 +00003916 case PREDEF_TYPE_UNKNOWN_ANY: T = Context->UnknownAnyTy; break;
Sebastian Redl539c5062010-08-18 23:57:32 +00003917 case PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break;
3918 case PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break;
3919 case PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break;
3920 case PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break;
3921 case PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break;
3922 case PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break;
Douglas Gregoreda8e122011-08-09 15:13:55 +00003923 case PREDEF_TYPE_AUTO_DEDUCT: T = Context->getAutoDeductType(); break;
3924
3925 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
3926 T = Context->getAutoRRefDeductType();
3927 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003928 }
3929
3930 assert(!T.isNull() && "Unknown predefined type");
John McCall8ccfcb52009-09-24 19:53:00 +00003931 return T.withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003932 }
3933
Sebastian Redl539c5062010-08-18 23:57:32 +00003934 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003935 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl409183f2010-07-14 20:26:45 +00003936 if (TypesLoaded[Index].isNull()) {
Douglas Gregor903b7e92011-07-22 00:38:23 +00003937 TypesLoaded[Index] = readTypeRecord(Index);
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003938 if (TypesLoaded[Index].isNull())
3939 return QualType();
3940
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003941 TypesLoaded[Index]->setFromAST();
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003942 if (DeserializationListener)
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003943 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003944 TypesLoaded[Index]);
Sebastian Redl409183f2010-07-14 20:26:45 +00003945 }
Mike Stump11289f42009-09-09 15:08:12 +00003946
John McCall8ccfcb52009-09-24 19:53:00 +00003947 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003948}
3949
Douglas Gregora6895d82011-07-22 16:00:58 +00003950QualType ASTReader::getLocalType(Module &F, unsigned LocalID) {
Douglas Gregor903b7e92011-07-22 00:38:23 +00003951 return GetType(getGlobalTypeID(F, LocalID));
3952}
3953
3954serialization::TypeID
Douglas Gregora6895d82011-07-22 16:00:58 +00003955ASTReader::getGlobalTypeID(Module &F, unsigned LocalID) const {
Douglas Gregor5204bde2011-08-02 16:26:37 +00003956 unsigned FastQuals = LocalID & Qualifiers::FastMask;
3957 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
3958
3959 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
3960 return LocalID;
3961
3962 ContinuousRangeMap<uint32_t, int, 2>::iterator I
3963 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
3964 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
3965
3966 unsigned GlobalIndex = LocalIndex + I->second;
3967 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
3968}
3969
John McCall0ad16662009-10-29 08:12:44 +00003970TemplateArgumentLocInfo
Douglas Gregora6895d82011-07-22 16:00:58 +00003971ASTReader::GetTemplateArgumentLocInfo(Module &F,
Sebastian Redl2c373b92010-10-05 15:59:54 +00003972 TemplateArgument::ArgKind Kind,
John McCall0ad16662009-10-29 08:12:44 +00003973 const RecordData &Record,
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003974 unsigned &Index) {
John McCall0ad16662009-10-29 08:12:44 +00003975 switch (Kind) {
3976 case TemplateArgument::Expression:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003977 return ReadExpr(F);
John McCall0ad16662009-10-29 08:12:44 +00003978 case TemplateArgument::Type:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003979 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003980 case TemplateArgument::Template: {
Douglas Gregor9d802122011-03-02 17:09:35 +00003981 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
3982 Index);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003983 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregor9d802122011-03-02 17:09:35 +00003984 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003985 SourceLocation());
3986 }
3987 case TemplateArgument::TemplateExpansion: {
Douglas Gregor9d802122011-03-02 17:09:35 +00003988 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
3989 Index);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003990 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregoreb29d182011-01-05 17:40:24 +00003991 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregor9d802122011-03-02 17:09:35 +00003992 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregoreb29d182011-01-05 17:40:24 +00003993 EllipsisLoc);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003994 }
John McCall0ad16662009-10-29 08:12:44 +00003995 case TemplateArgument::Null:
3996 case TemplateArgument::Integral:
3997 case TemplateArgument::Declaration:
3998 case TemplateArgument::Pack:
3999 return TemplateArgumentLocInfo();
4000 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +00004001 llvm_unreachable("unexpected template argument loc");
John McCall0ad16662009-10-29 08:12:44 +00004002 return TemplateArgumentLocInfo();
4003}
4004
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004005TemplateArgumentLoc
Douglas Gregora6895d82011-07-22 16:00:58 +00004006ASTReader::ReadTemplateArgumentLoc(Module &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00004007 const RecordData &Record, unsigned &Index) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004008 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004009
4010 if (Arg.getKind() == TemplateArgument::Expression) {
4011 if (Record[Index++]) // bool InfoHasSameExpr.
4012 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
4013 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00004014 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00004015 Record, Index));
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004016}
4017
Sebastian Redl2c499f62010-08-18 23:56:43 +00004018Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall75b960e2010-06-01 09:23:16 +00004019 return GetDecl(ID);
4020}
4021
Douglas Gregorc27b2872011-08-04 00:01:48 +00004022uint64_t ASTReader::readCXXBaseSpecifiers(Module &M, const RecordData &Record,
4023 unsigned &Idx){
4024 if (Idx >= Record.size())
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004025 return 0;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004026
Douglas Gregorc27b2872011-08-04 00:01:48 +00004027 unsigned LocalID = Record[Idx++];
4028 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004029}
4030
4031CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
Douglas Gregord32f0352011-07-22 06:10:01 +00004032 RecordLocation Loc = getLocalBitOffset(Offset);
4033 llvm::BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004034 SavedStreamPosition SavedPosition(Cursor);
Douglas Gregord32f0352011-07-22 06:10:01 +00004035 Cursor.JumpToBit(Loc.Offset);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004036 ReadingKindTracker ReadingKind(Read_Decl, *this);
4037 RecordData Record;
4038 unsigned Code = Cursor.ReadCode();
4039 unsigned RecCode = Cursor.ReadRecord(Code, Record);
4040 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
4041 Error("Malformed AST file: missing C++ base specifiers");
4042 return 0;
4043 }
4044
4045 unsigned Idx = 0;
4046 unsigned NumBases = Record[Idx++];
4047 void *Mem = Context->Allocate(sizeof(CXXBaseSpecifier) * NumBases);
4048 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
4049 for (unsigned I = 0; I != NumBases; ++I)
Douglas Gregord32f0352011-07-22 06:10:01 +00004050 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004051 return Bases;
4052}
4053
Douglas Gregor7fb09192011-07-21 22:35:25 +00004054serialization::DeclID
Douglas Gregora6895d82011-07-22 16:00:58 +00004055ASTReader::getGlobalDeclID(Module &F, unsigned LocalID) const {
Douglas Gregor6f8912e2011-08-03 16:05:40 +00004056 if (LocalID < NUM_PREDEF_DECL_IDS)
Douglas Gregorf7180622011-08-03 15:48:04 +00004057 return LocalID;
4058
4059 ContinuousRangeMap<uint32_t, int, 2>::iterator I
Douglas Gregor6f8912e2011-08-03 16:05:40 +00004060 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
Douglas Gregorf7180622011-08-03 15:48:04 +00004061 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
4062
4063 return LocalID + I->second;
Douglas Gregor7fb09192011-07-21 22:35:25 +00004064}
4065
Sebastian Redl539c5062010-08-18 23:57:32 +00004066Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregor6f8912e2011-08-03 16:05:40 +00004067 if (ID < NUM_PREDEF_DECL_IDS) {
4068 switch ((PredefinedDeclIDs)ID) {
Douglas Gregordab42432011-08-12 00:15:20 +00004069 case PREDEF_DECL_NULL_ID:
Douglas Gregor6f8912e2011-08-03 16:05:40 +00004070 return 0;
Douglas Gregordab42432011-08-12 00:15:20 +00004071
4072 case PREDEF_DECL_TRANSLATION_UNIT_ID:
4073 assert(Context && "No context available?");
4074 return Context->getTranslationUnitDecl();
Douglas Gregor3ea72692011-08-12 05:46:01 +00004075
4076 case PREDEF_DECL_OBJC_ID_ID:
4077 assert(Context && "No context available?");
4078 return Context->getObjCIdDecl();
Douglas Gregor0a586182011-08-12 05:59:41 +00004079
Douglas Gregor52e02802011-08-12 06:17:30 +00004080 case PREDEF_DECL_OBJC_SEL_ID:
4081 assert(Context && "No context available?");
4082 return Context->getObjCSelDecl();
4083
Douglas Gregor0a586182011-08-12 05:59:41 +00004084 case PREDEF_DECL_OBJC_CLASS_ID:
4085 assert(Context && "No context available?");
4086 return Context->getObjCClassDecl();
Douglas Gregor801c99d2011-08-12 06:49:56 +00004087
4088 case PREDEF_DECL_INT_128_ID:
4089 assert(Context && "No context available?");
4090 return Context->getInt128Decl();
4091
4092 case PREDEF_DECL_UNSIGNED_INT_128_ID:
4093 assert(Context && "No context available?");
4094 return Context->getUInt128Decl();
Douglas Gregor6f8912e2011-08-03 16:05:40 +00004095 }
4096
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004097 return 0;
Douglas Gregor6f8912e2011-08-03 16:05:40 +00004098 }
4099
Douglas Gregordab42432011-08-12 00:15:20 +00004100 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
4101
4102 if (Index > DeclsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004103 Error("declaration ID out-of-range for AST file");
Douglas Gregor745ed142009-04-25 18:35:21 +00004104 return 0;
4105 }
Douglas Gregordab42432011-08-12 00:15:20 +00004106
4107if (!DeclsLoaded[Index]) {
Douglas Gregorf7180622011-08-03 15:48:04 +00004108 ReadDeclRecord(ID);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00004109 if (DeserializationListener)
4110 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
4111 }
Douglas Gregor745ed142009-04-25 18:35:21 +00004112
4113 return DeclsLoaded[Index];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004114}
4115
Douglas Gregora6895d82011-07-22 16:00:58 +00004116serialization::DeclID ASTReader::ReadDeclID(Module &F,
Douglas Gregor7fb09192011-07-21 22:35:25 +00004117 const RecordData &Record,
4118 unsigned &Idx) {
4119 if (Idx >= Record.size()) {
4120 Error("Corrupted AST file");
4121 return 0;
4122 }
4123
4124 return getGlobalDeclID(F, Record[Idx++]);
4125}
4126
Chris Lattner9c28af02009-04-27 05:46:25 +00004127/// \brief Resolve the offset of a statement into a statement.
4128///
4129/// This operation will read a new statement from the external
4130/// source each time it is called, and is meant to be used via a
4131/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redl2c499f62010-08-18 23:56:43 +00004132Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidisd9f526f2010-10-28 09:29:32 +00004133 // Switch case IDs are per Decl.
4134 ClearSwitchCaseIDs();
4135
Sebastian Redl5c415f32010-07-22 17:01:13 +00004136 // Offset here is a global offset across the entire chain.
Douglas Gregord32f0352011-07-22 06:10:01 +00004137 RecordLocation Loc = getLocalBitOffset(Offset);
4138 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
4139 return ReadStmtFromStream(*Loc.F);
Douglas Gregor3c3aa612009-04-18 00:07:54 +00004140}
4141
Douglas Gregor1257f972011-08-24 21:27:34 +00004142namespace {
4143 class FindExternalLexicalDeclsVisitor {
4144 ASTReader &Reader;
4145 const DeclContext *DC;
4146 bool (*isKindWeWant)(Decl::Kind);
4147 SmallVectorImpl<Decl*> &Decls;
4148 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
4149
4150 public:
4151 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
4152 bool (*isKindWeWant)(Decl::Kind),
4153 SmallVectorImpl<Decl*> &Decls)
4154 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
4155 {
4156 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
4157 PredefsVisited[I] = false;
4158 }
4159
4160 static bool visit(Module &M, bool Preorder, void *UserData) {
4161 if (Preorder)
4162 return false;
4163
4164 FindExternalLexicalDeclsVisitor *This
4165 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
4166
4167 Module::DeclContextInfosMap::iterator Info
4168 = M.DeclContextInfos.find(This->DC);
4169 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
4170 return false;
4171
4172 // Load all of the declaration IDs
4173 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
4174 *IDE = ID + Info->second.NumLexicalDecls;
4175 ID != IDE; ++ID) {
4176 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
4177 continue;
4178
4179 // Don't add predefined declarations to the lexical context more
4180 // than once.
4181 if (ID->second < NUM_PREDEF_DECL_IDS) {
4182 if (This->PredefsVisited[ID->second])
4183 continue;
4184
4185 This->PredefsVisited[ID->second] = true;
4186 }
4187
4188 Decl *D = This->Reader.GetLocalDecl(M, ID->second);
4189 assert(D && "Null decl in lexical decls");
4190 This->Decls.push_back(D);
4191 }
4192
4193 return false;
4194 }
4195 };
4196}
4197
Douglas Gregor3d0adb32011-07-15 21:46:17 +00004198ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00004199 bool (*isKindWeWant)(Decl::Kind),
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004200 SmallVectorImpl<Decl*> &Decls) {
Douglas Gregor94619c82011-08-24 19:03:07 +00004201 // There might be lexical decls in multiple modules, for the TU at
Douglas Gregor1257f972011-08-24 21:27:34 +00004202 // least. Walk all of the modules in the order they were loaded.
4203 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
4204 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00004205 ++NumLexicalDeclContextsRead;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00004206 return ELR_Success;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004207}
4208
Douglas Gregor94619c82011-08-24 19:03:07 +00004209namespace {
4210 /// \brief Module visitor used to perform name lookup into a
4211 /// declaration context.
4212 class DeclContextNameLookupVisitor {
4213 ASTReader &Reader;
4214 const DeclContext *DC;
4215 DeclarationName Name;
4216 SmallVectorImpl<NamedDecl *> &Decls;
4217
4218 public:
4219 DeclContextNameLookupVisitor(ASTReader &Reader,
4220 const DeclContext *DC, DeclarationName Name,
4221 SmallVectorImpl<NamedDecl *> &Decls)
4222 : Reader(Reader), DC(DC), Name(Name), Decls(Decls) { }
4223
4224 static bool visit(Module &M, void *UserData) {
4225 DeclContextNameLookupVisitor *This
4226 = static_cast<DeclContextNameLookupVisitor *>(UserData);
4227
4228 // Check whether we have any visible declaration information for
4229 // this context in this module.
4230 Module::DeclContextInfosMap::iterator Info
4231 = M.DeclContextInfos.find(This->DC);
4232 if (Info == M.DeclContextInfos.end() || !Info->second.NameLookupTableData)
4233 return false;
4234
4235 // Look for this name within this module.
4236 ASTDeclContextNameLookupTable *LookupTable =
4237 (ASTDeclContextNameLookupTable*)Info->second.NameLookupTableData;
4238 ASTDeclContextNameLookupTable::iterator Pos
4239 = LookupTable->find(This->Name);
4240 if (Pos == LookupTable->end())
4241 return false;
4242
4243 bool FoundAnything = false;
4244 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
4245 for (; Data.first != Data.second; ++Data.first) {
4246 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
4247 if (!ND)
4248 continue;
4249
4250 if (ND->getDeclName() != This->Name) {
4251 assert(!This->Name.getCXXNameType().isNull() &&
4252 "Name mismatch without a type");
4253 continue;
4254 }
4255
4256 // Record this declaration.
4257 FoundAnything = true;
4258 This->Decls.push_back(ND);
4259 }
4260
4261 return FoundAnything;
4262 }
4263 };
4264}
4265
John McCall75b960e2010-06-01 09:23:16 +00004266DeclContext::lookup_result
Sebastian Redl2c499f62010-08-18 23:56:43 +00004267ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall75b960e2010-06-01 09:23:16 +00004268 DeclarationName Name) {
Mike Stump11289f42009-09-09 15:08:12 +00004269 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004270 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004271 if (!Name)
4272 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
4273 DeclContext::lookup_iterator(0));
Ted Kremenek1ff615c2010-03-18 00:56:54 +00004274
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004275 SmallVector<NamedDecl *, 64> Decls;
Douglas Gregor94619c82011-08-24 19:03:07 +00004276 DeclContextNameLookupVisitor Visitor(*this, DC, Name, Decls);
4277 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00004278 ++NumVisibleDeclContextsRead;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004279 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall75b960e2010-06-01 09:23:16 +00004280 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004281}
4282
Sebastian Redl2c499f62010-08-18 23:56:43 +00004283void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004284 assert(Consumer);
4285 while (!InterestingDecls.empty()) {
4286 DeclGroupRef DG(InterestingDecls.front());
4287 InterestingDecls.pop_front();
Sebastian Redleaa4ade2010-08-11 18:52:41 +00004288 Consumer->HandleInterestingDecl(DG);
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004289 }
4290}
4291
Sebastian Redl2c499f62010-08-18 23:56:43 +00004292void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregorb985eeb2009-04-22 19:09:20 +00004293 this->Consumer = Consumer;
4294
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00004295 if (!Consumer)
4296 return;
4297
4298 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004299 // Force deserialization of this decl, which will cause it to be queued for
4300 // passing to the consumer.
Daniel Dunbar865c2a72009-09-17 03:06:44 +00004301 GetDecl(ExternalDefinitions[I]);
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00004302 }
Douglas Gregorf005eac2009-04-25 00:41:30 +00004303
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004304 PassInterestingDeclsToConsumer();
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00004305}
4306
Sebastian Redl2c499f62010-08-18 23:56:43 +00004307void ASTReader::PrintStats() {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004308 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004309
Mike Stump11289f42009-09-09 15:08:12 +00004310 unsigned NumTypesLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00004311 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall8ccfcb52009-09-24 19:53:00 +00004312 QualType());
Douglas Gregor0e149972009-04-25 19:10:14 +00004313 unsigned NumDeclsLoaded
4314 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
4315 (Decl *)0);
4316 unsigned NumIdentifiersLoaded
4317 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
4318 IdentifiersLoaded.end(),
4319 (IdentifierInfo *)0);
Mike Stump11289f42009-09-09 15:08:12 +00004320 unsigned NumSelectorsLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00004321 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
4322 SelectorsLoaded.end(),
4323 Selector());
Douglas Gregorc3b1dd12009-04-13 20:50:16 +00004324
Douglas Gregorc5046832009-04-27 18:38:38 +00004325 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
4326 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor49bf76b2011-07-21 18:46:38 +00004327 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
Douglas Gregor258ae542009-04-27 06:38:32 +00004328 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
4329 NumSLocEntriesRead, TotalNumSLocEntries,
4330 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor745ed142009-04-25 18:35:21 +00004331 if (!TypesLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00004332 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00004333 NumTypesLoaded, (unsigned)TypesLoaded.size(),
4334 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
4335 if (!DeclsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00004336 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00004337 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
4338 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor0e149972009-04-25 19:10:14 +00004339 if (!IdentifiersLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00004340 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor0e149972009-04-25 19:10:14 +00004341 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
4342 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redlada023c2010-08-04 20:40:17 +00004343 if (!SelectorsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00004344 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redlada023c2010-08-04 20:40:17 +00004345 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
4346 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor95c13f52009-04-25 17:48:32 +00004347 if (TotalNumStatements)
4348 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
4349 NumStatementsRead, TotalNumStatements,
4350 ((float)NumStatementsRead/TotalNumStatements * 100));
4351 if (TotalNumMacros)
4352 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
4353 NumMacrosRead, TotalNumMacros,
4354 ((float)NumMacrosRead/TotalNumMacros * 100));
4355 if (TotalLexicalDeclContexts)
4356 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
4357 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
4358 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
4359 * 100));
4360 if (TotalVisibleDeclContexts)
4361 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
4362 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
4363 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
4364 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004365 if (TotalNumMethodPoolEntries) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004366 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004367 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
4368 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor95c13f52009-04-25 17:48:32 +00004369 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004370 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor95c13f52009-04-25 17:48:32 +00004371 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004372 std::fprintf(stderr, "\n");
Douglas Gregor204b8712011-07-21 19:50:14 +00004373 dump();
4374 std::fprintf(stderr, "\n");
4375}
4376
Douglas Gregora6895d82011-07-22 16:00:58 +00004377template<typename Key, typename Module, unsigned InitialCapacity>
Douglas Gregor204b8712011-07-21 19:50:14 +00004378static void
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004379dumpModuleIDMap(StringRef Name,
Douglas Gregora6895d82011-07-22 16:00:58 +00004380 const ContinuousRangeMap<Key, Module *,
Douglas Gregor204b8712011-07-21 19:50:14 +00004381 InitialCapacity> &Map) {
4382 if (Map.begin() == Map.end())
4383 return;
4384
Douglas Gregora6895d82011-07-22 16:00:58 +00004385 typedef ContinuousRangeMap<Key, Module *, InitialCapacity> MapType;
Douglas Gregor204b8712011-07-21 19:50:14 +00004386 llvm::errs() << Name << ":\n";
4387 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
4388 I != IEnd; ++I) {
4389 llvm::errs() << " " << I->first << " -> " << I->second->FileName
4390 << "\n";
4391 }
4392}
4393
Douglas Gregor204b8712011-07-21 19:50:14 +00004394void ASTReader::dump() {
Douglas Gregor1cc9c062011-08-02 11:12:41 +00004395 llvm::errs() << "*** PCH/Module Remappings:\n";
Douglas Gregord32f0352011-07-22 06:10:01 +00004396 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
Douglas Gregor204b8712011-07-21 19:50:14 +00004397 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
Douglas Gregor8ab4ea82011-07-29 00:21:44 +00004398 dumpModuleIDMap("Global type map", GlobalTypeMap);
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00004399 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00004400 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
4401 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
4402 dumpModuleIDMap("Global macro definition map", GlobalMacroDefinitionMap);
4403 dumpModuleIDMap("Global preprocessed entity map",
4404 GlobalPreprocessedEntityMap);
Douglas Gregor1cc9c062011-08-02 11:12:41 +00004405
4406 llvm::errs() << "\n*** PCH/Modules Loaded:";
4407 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
4408 MEnd = ModuleMgr.end();
4409 M != MEnd; ++M)
4410 (*M)->dump();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004411}
4412
Ted Kremenek5e1ed7b2011-04-28 23:46:20 +00004413/// Return the amount of memory used by memory buffers, breaking down
4414/// by heap-backed versus mmap'ed memory.
4415void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00004416 for (ModuleConstIterator I = ModuleMgr.begin(),
4417 E = ModuleMgr.end(); I != E; ++I) {
4418 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
Ted Kremenek5e1ed7b2011-04-28 23:46:20 +00004419 size_t bytes = buf->getBufferSize();
4420 switch (buf->getBufferKind()) {
4421 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
4422 sizes.malloc_bytes += bytes;
4423 break;
4424 case llvm::MemoryBuffer::MemoryBuffer_MMap:
4425 sizes.mmap_bytes += bytes;
4426 break;
4427 }
4428 }
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00004429 }
Ted Kremenek5e1ed7b2011-04-28 23:46:20 +00004430}
4431
Sebastian Redl2c499f62010-08-18 23:56:43 +00004432void ASTReader::InitializeSema(Sema &S) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00004433 SemaObj = &S;
Douglas Gregorc78d3462009-04-24 21:10:55 +00004434 S.ExternalSource = this;
4435
Douglas Gregor7cd60f72009-04-22 21:15:06 +00004436 // Makes sure any declarations that were deserialized "too early"
4437 // still get added to the identifier's declaration chains.
Douglas Gregor2fb99df2010-09-24 23:29:12 +00004438 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
4439 if (SemaObj->TUScope)
John McCall48871652010-08-21 09:40:31 +00004440 SemaObj->TUScope->AddDecl(PreloadedDecls[I]);
Douglas Gregor2fb99df2010-09-24 23:29:12 +00004441
4442 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregora868bbd2009-04-21 22:25:48 +00004443 }
Douglas Gregor7cd60f72009-04-22 21:15:06 +00004444 PreloadedDecls.clear();
Douglas Gregord4df8652009-04-22 22:02:47 +00004445
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004446 // Load the offsets of the declarations that Sema references.
4447 // They will be lazily deserialized when needed.
4448 if (!SemaDeclRefs.empty()) {
4449 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
Douglas Gregorb0f3ae62011-07-28 00:57:24 +00004450 if (!SemaObj->StdNamespace)
4451 SemaObj->StdNamespace = SemaDeclRefs[0];
4452 if (!SemaObj->StdBadAlloc)
4453 SemaObj->StdBadAlloc = SemaDeclRefs[1];
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004454 }
4455
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004456 if (!FPPragmaOptions.empty()) {
4457 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
4458 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
4459 }
4460
4461 if (!OpenCLExtensions.empty()) {
4462 unsigned I = 0;
4463#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
4464#include "clang/Basic/OpenCLExtensions.def"
4465
4466 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
4467 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00004468}
4469
Douglas Gregorab443b92011-08-20 04:39:52 +00004470namespace {
4471 /// \brief Visitor class used to look up identifirs in
4472 class IdentifierLookupVisitor {
4473 StringRef Name;
4474 IdentifierInfo *Found;
4475 public:
4476 explicit IdentifierLookupVisitor(StringRef Name) : Name(Name), Found() { }
Douglas Gregora868bbd2009-04-21 22:25:48 +00004477
Douglas Gregorab443b92011-08-20 04:39:52 +00004478 static bool visit(Module &M, void *UserData) {
4479 IdentifierLookupVisitor *This
4480 = static_cast<IdentifierLookupVisitor *>(UserData);
4481
4482 ASTIdentifierLookupTable *IdTable
4483 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
4484 if (!IdTable)
4485 return false;
4486
4487 std::pair<const char*, unsigned> Key(This->Name.begin(),
4488 This->Name.size());
4489 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
4490 if (Pos == IdTable->end())
4491 return false;
4492
4493 // Dereferencing the iterator has the effect of building the
4494 // IdentifierInfo node and populating it with the various
4495 // declarations it needs.
4496 This->Found = *Pos;
4497 return true;
4498 }
4499
4500 // \brief Retrieve the identifier info found within the module
4501 // files.
4502 IdentifierInfo *getIdentifierInfo() const { return Found; }
4503 };
4504}
4505
4506IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Douglas Gregorab443b92011-08-20 04:39:52 +00004507 IdentifierLookupVisitor Visitor(StringRef(NameStart, NameEnd - NameStart));
4508 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
4509 return Visitor.getIdentifierInfo();
Douglas Gregora868bbd2009-04-21 22:25:48 +00004510}
4511
Douglas Gregor57756ea2010-10-14 22:11:03 +00004512namespace clang {
4513 /// \brief An identifier-lookup iterator that enumerates all of the
4514 /// identifiers stored within a set of AST files.
4515 class ASTIdentifierIterator : public IdentifierIterator {
4516 /// \brief The AST reader whose identifiers are being enumerated.
4517 const ASTReader &Reader;
4518
4519 /// \brief The current index into the chain of AST files stored in
4520 /// the AST reader.
4521 unsigned Index;
4522
4523 /// \brief The current position within the identifier lookup table
4524 /// of the current AST file.
4525 ASTIdentifierLookupTable::key_iterator Current;
4526
4527 /// \brief The end position within the identifier lookup table of
4528 /// the current AST file.
4529 ASTIdentifierLookupTable::key_iterator End;
4530
4531 public:
4532 explicit ASTIdentifierIterator(const ASTReader &Reader);
4533
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004534 virtual StringRef Next();
Douglas Gregor57756ea2010-10-14 22:11:03 +00004535 };
4536}
4537
4538ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00004539 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
Douglas Gregor57756ea2010-10-14 22:11:03 +00004540 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00004541 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
Douglas Gregor57756ea2010-10-14 22:11:03 +00004542 Current = IdTable->key_begin();
4543 End = IdTable->key_end();
4544}
4545
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004546StringRef ASTIdentifierIterator::Next() {
Douglas Gregor57756ea2010-10-14 22:11:03 +00004547 while (Current == End) {
4548 // If we have exhausted all of our AST files, we're done.
4549 if (Index == 0)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004550 return StringRef();
Douglas Gregor57756ea2010-10-14 22:11:03 +00004551
4552 --Index;
4553 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00004554 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
4555 IdentifierLookupTable;
Douglas Gregor57756ea2010-10-14 22:11:03 +00004556 Current = IdTable->key_begin();
4557 End = IdTable->key_end();
4558 }
4559
4560 // We have any identifiers remaining in the current AST file; return
4561 // the next one.
4562 std::pair<const char*, unsigned> Key = *Current;
4563 ++Current;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004564 return StringRef(Key.first, Key.second);
Douglas Gregor57756ea2010-10-14 22:11:03 +00004565}
4566
4567IdentifierIterator *ASTReader::getIdentifiers() const {
4568 return new ASTIdentifierIterator(*this);
4569}
4570
Douglas Gregorc10edd62011-08-25 14:51:20 +00004571namespace clang { namespace serialization {
4572 class ReadMethodPoolVisitor {
4573 ASTReader &Reader;
4574 Selector Sel;
4575 llvm::SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
4576 llvm::SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Douglas Gregorc78d3462009-04-24 21:10:55 +00004577
Douglas Gregorc10edd62011-08-25 14:51:20 +00004578 /// \brief Build an ObjCMethodList from a vector of Objective-C method
4579 /// declarations.
4580 ObjCMethodList
4581 buildObjCMethodList(const SmallVectorImpl<ObjCMethodDecl *> &Vec) const
4582 {
4583 ObjCMethodList List;
4584 ObjCMethodList *Prev = 0;
4585 for (unsigned I = 0, N = Vec.size(); I != N; ++I) {
4586 if (!List.Method) {
4587 // This is the first method, which is the easy case.
4588 List.Method = Vec[I];
4589 Prev = &List;
4590 continue;
4591 }
4592
4593 ObjCMethodList *Mem =
4594 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
4595 Prev->Next = new (Mem) ObjCMethodList(Vec[I], 0);
4596 Prev = Prev->Next;
4597 }
4598
4599 return List;
4600 }
4601
4602 public:
4603 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel)
4604 : Reader(Reader), Sel(Sel) { }
4605
4606 static bool visit(Module &M, void *UserData) {
4607 ReadMethodPoolVisitor *This
4608 = static_cast<ReadMethodPoolVisitor *>(UserData);
4609
4610 if (!M.SelectorLookupTable)
4611 return false;
4612
4613 ASTSelectorLookupTable *PoolTable
4614 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
4615 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
4616 if (Pos == PoolTable->end())
4617 return false;
4618
4619 ++This->Reader.NumSelectorsRead;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004620 // FIXME: Not quite happy with the statistics here. We probably should
4621 // disable this tracking when called via LoadSelector.
4622 // Also, should entries without methods count as misses?
Douglas Gregorc10edd62011-08-25 14:51:20 +00004623 ++This->Reader.NumMethodPoolEntriesRead;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004624 ASTSelectorLookupTrait::data_type Data = *Pos;
Douglas Gregorc10edd62011-08-25 14:51:20 +00004625 if (This->Reader.DeserializationListener)
4626 This->Reader.DeserializationListener->SelectorRead(Data.ID,
4627 This->Sel);
4628
4629 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
4630 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
4631 return true;
Sebastian Redlada023c2010-08-04 20:40:17 +00004632 }
Douglas Gregorc10edd62011-08-25 14:51:20 +00004633
4634 /// \brief Retrieve the instance methods found by this visitor.
4635 ObjCMethodList getInstanceMethods() const {
4636 return buildObjCMethodList(InstanceMethods);
4637 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00004638
Douglas Gregorc10edd62011-08-25 14:51:20 +00004639 /// \brief Retrieve the instance methods found by this visitor.
4640 ObjCMethodList getFactoryMethods() const {
4641 return buildObjCMethodList(FactoryMethods);
4642 }
4643 };
4644} } // end namespace clang::serialization
4645
4646std::pair<ObjCMethodList, ObjCMethodList>
4647ASTReader::ReadMethodPool(Selector Sel) {
4648 ReadMethodPoolVisitor Visitor(*this, Sel);
4649 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
4650 std::pair<ObjCMethodList, ObjCMethodList> Result;
4651 Result.first = Visitor.getInstanceMethods();
4652 Result.second = Visitor.getFactoryMethods();
4653
4654 if (!Result.first.Method && !Result.second.Method)
4655 ++NumMethodPoolMisses;
4656 return Result;
Douglas Gregorc78d3462009-04-24 21:10:55 +00004657}
4658
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004659void ASTReader::ReadKnownNamespaces(
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004660 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004661 Namespaces.clear();
4662
4663 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
4664 if (NamespaceDecl *Namespace
4665 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
4666 Namespaces.push_back(Namespace);
4667 }
4668}
4669
Douglas Gregoreb08bd42011-07-27 20:58:46 +00004670void ASTReader::ReadTentativeDefinitions(
4671 SmallVectorImpl<VarDecl *> &TentativeDefs) {
4672 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
4673 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
4674 if (Var)
4675 TentativeDefs.push_back(Var);
4676 }
4677 TentativeDefinitions.clear();
4678}
4679
Douglas Gregora94a1542011-07-27 21:45:57 +00004680void ASTReader::ReadUnusedFileScopedDecls(
4681 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
4682 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
4683 DeclaratorDecl *D
4684 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
4685 if (D)
4686 Decls.push_back(D);
4687 }
4688 UnusedFileScopedDecls.clear();
4689}
4690
Douglas Gregorbae31202011-07-27 21:57:17 +00004691void ASTReader::ReadDelegatingConstructors(
4692 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
4693 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
4694 CXXConstructorDecl *D
4695 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
4696 if (D)
4697 Decls.push_back(D);
4698 }
4699 DelegatingCtorDecls.clear();
4700}
4701
Douglas Gregorb7098a32011-07-28 00:39:29 +00004702void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
4703 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
4704 TypedefNameDecl *D
4705 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
4706 if (D)
4707 Decls.push_back(D);
4708 }
4709 ExtVectorDecls.clear();
4710}
4711
Douglas Gregor32002192011-07-28 00:53:40 +00004712void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
4713 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
4714 CXXRecordDecl *D
4715 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
4716 if (D)
4717 Decls.push_back(D);
4718 }
4719 DynamicClasses.clear();
4720}
4721
Douglas Gregordc5c9582011-07-28 14:20:37 +00004722void
4723ASTReader::ReadLocallyScopedExternalDecls(SmallVectorImpl<NamedDecl *> &Decls) {
4724 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
4725 NamedDecl *D
4726 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
4727 if (D)
4728 Decls.push_back(D);
4729 }
4730 LocallyScopedExternalDecls.clear();
4731}
4732
Douglas Gregor72e357f2011-07-28 14:54:22 +00004733void ASTReader::ReadReferencedSelectors(
4734 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
4735 if (ReferencedSelectorsData.empty())
4736 return;
4737
4738 // If there are @selector references added them to its pool. This is for
4739 // implementation of -Wselector.
4740 unsigned int DataSize = ReferencedSelectorsData.size()-1;
4741 unsigned I = 0;
4742 while (I < DataSize) {
4743 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
4744 SourceLocation SelLoc
4745 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
4746 Sels.push_back(std::make_pair(Sel, SelLoc));
4747 }
4748 ReferencedSelectorsData.clear();
4749}
4750
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00004751void ASTReader::ReadWeakUndeclaredIdentifiers(
4752 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
4753 if (WeakUndeclaredIdentifiers.empty())
4754 return;
4755
4756 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
4757 IdentifierInfo *WeakId
4758 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
4759 IdentifierInfo *AliasId
4760 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
4761 SourceLocation Loc
4762 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
4763 bool Used = WeakUndeclaredIdentifiers[I++];
4764 WeakInfo WI(AliasId, Loc);
4765 WI.setUsed(Used);
4766 WeakIDs.push_back(std::make_pair(WeakId, WI));
4767 }
4768 WeakUndeclaredIdentifiers.clear();
4769}
4770
Douglas Gregor4daf6a32011-07-28 19:11:31 +00004771void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
4772 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
4773 ExternalVTableUse VT;
4774 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
4775 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
4776 VT.DefinitionRequired = VTableUses[Idx++];
4777 VTables.push_back(VT);
4778 }
4779
4780 VTableUses.clear();
4781}
4782
Douglas Gregore39f97c2011-07-28 19:49:54 +00004783void ASTReader::ReadPendingInstantiations(
4784 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
4785 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
4786 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
4787 SourceLocation Loc
4788 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
4789 Pending.push_back(std::make_pair(D, Loc));
4790 }
4791 PendingInstantiations.clear();
4792}
4793
Sebastian Redl2c499f62010-08-18 23:56:43 +00004794void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00004795 // It would be complicated to avoid reading the methods anyway. So don't.
4796 ReadMethodPool(Sel);
4797}
4798
Douglas Gregora3e41532011-07-28 20:55:49 +00004799void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00004800 assert(ID && "Non-zero identifier ID required");
Douglas Gregor6f00bf82009-04-28 21:53:25 +00004801 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor0e149972009-04-25 19:10:14 +00004802 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlff4a2952010-07-23 23:49:55 +00004803 if (DeserializationListener)
4804 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregora868bbd2009-04-21 22:25:48 +00004805}
4806
Douglas Gregor1342e842009-07-06 18:54:52 +00004807/// \brief Set the globally-visible declarations associated with the given
4808/// identifier.
4809///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004810/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump11289f42009-09-09 15:08:12 +00004811/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregor1342e842009-07-06 18:54:52 +00004812/// them.
4813///
4814/// \param II an IdentifierInfo that refers to one or more globally-visible
4815/// declarations.
4816///
4817/// \param DeclIDs the set of declaration IDs with the name @p II that are
4818/// visible at global scope.
4819///
4820/// \param Nonrecursive should be true to indicate that the caller knows that
4821/// this call is non-recursive, and therefore the globally-visible declarations
4822/// will not be placed onto the pending queue.
Mike Stump11289f42009-09-09 15:08:12 +00004823void
Sebastian Redl2c499f62010-08-18 23:56:43 +00004824ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004825 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregor1342e842009-07-06 18:54:52 +00004826 bool Nonrecursive) {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004827 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregor1342e842009-07-06 18:54:52 +00004828 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
4829 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
4830 PII.II = II;
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00004831 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregor1342e842009-07-06 18:54:52 +00004832 return;
4833 }
Mike Stump11289f42009-09-09 15:08:12 +00004834
Douglas Gregor1342e842009-07-06 18:54:52 +00004835 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
4836 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
4837 if (SemaObj) {
Douglas Gregor6fd55e02010-08-13 03:15:25 +00004838 if (SemaObj->TUScope) {
4839 // Introduce this declaration into the translation-unit scope
4840 // and add it to the declaration chain for this identifier, so
4841 // that (unqualified) name lookup will find it.
John McCall48871652010-08-21 09:40:31 +00004842 SemaObj->TUScope->AddDecl(D);
Douglas Gregor6fd55e02010-08-13 03:15:25 +00004843 }
Douglas Gregor2fb99df2010-09-24 23:29:12 +00004844 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
Douglas Gregor1342e842009-07-06 18:54:52 +00004845 } else {
4846 // Queue this declaration so that it will be added to the
4847 // translation unit scope and identifier's declaration chain
4848 // once a Sema object is known.
4849 PreloadedDecls.push_back(D);
4850 }
4851 }
4852}
4853
Douglas Gregora3e41532011-07-28 20:55:49 +00004854IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004855 if (ID == 0)
4856 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00004857
Sebastian Redlc713b962010-07-21 00:46:22 +00004858 if (IdentifiersLoaded.empty()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004859 Error("no identifier table in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004860 return 0;
4861 }
Mike Stump11289f42009-09-09 15:08:12 +00004862
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00004863 assert(PP && "Forgot to set Preprocessor ?");
Sebastian Redlc713b962010-07-21 00:46:22 +00004864 ID -= 1;
4865 if (!IdentifiersLoaded[ID]) {
Douglas Gregor19d26352011-07-20 00:59:32 +00004866 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
4867 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00004868 Module *M = I->second;
4869 unsigned Index = ID - M->BaseIdentifierID;
4870 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
Douglas Gregor5287b4e2009-04-25 21:04:17 +00004871
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004872 // All of the strings in the AST file are preceded by a 16-bit length.
4873 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenekca42a512009-10-23 04:45:31 +00004874 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
4875 // unsigned integers. This is important to avoid integer overflow when
4876 // we cast them to 'unsigned'.
Ted Kremenek49c52322009-10-23 03:57:22 +00004877 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregorab4df582009-04-28 20:01:51 +00004878 unsigned StrLen = (((unsigned) StrLenPtr[0])
4879 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redlc713b962010-07-21 00:46:22 +00004880 IdentifiersLoaded[ID]
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004881 = &PP->getIdentifierTable().get(StringRef(Str, StrLen));
Sebastian Redlff4a2952010-07-23 23:49:55 +00004882 if (DeserializationListener)
4883 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004884 }
Mike Stump11289f42009-09-09 15:08:12 +00004885
Sebastian Redlc713b962010-07-21 00:46:22 +00004886 return IdentifiersLoaded[ID];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004887}
4888
Douglas Gregora3e41532011-07-28 20:55:49 +00004889IdentifierInfo *ASTReader::getLocalIdentifier(Module &M, unsigned LocalID) {
4890 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
4891}
4892
4893IdentifierID ASTReader::getGlobalIdentifierID(Module &M, unsigned LocalID) {
Douglas Gregor1ab036c2011-08-03 21:49:18 +00004894 if (LocalID < NUM_PREDEF_IDENT_IDS)
4895 return LocalID;
4896
4897 ContinuousRangeMap<uint32_t, int, 2>::iterator I
4898 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
4899 assert(I != M.IdentifierRemap.end()
4900 && "Invalid index into identifier index remap");
4901
4902 return LocalID + I->second;
Douglas Gregora3e41532011-07-28 20:55:49 +00004903}
4904
Douglas Gregor925296b2011-07-19 16:10:42 +00004905bool ASTReader::ReadSLocEntry(int ID) {
Douglas Gregor49f754f2011-04-20 00:21:03 +00004906 return ReadSLocEntryRecord(ID) != Success;
Douglas Gregor258ae542009-04-27 06:38:32 +00004907}
4908
Douglas Gregor074fdc52011-07-28 21:16:51 +00004909Selector ASTReader::getLocalSelector(Module &M, unsigned LocalID) {
4910 return DecodeSelector(getGlobalSelectorID(M, LocalID));
4911}
4912
4913Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
Steve Naroff2ddea052009-04-23 10:39:46 +00004914 if (ID == 0)
4915 return Selector();
Mike Stump11289f42009-09-09 15:08:12 +00004916
Sebastian Redlada023c2010-08-04 20:40:17 +00004917 if (ID > SelectorsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004918 Error("selector ID out of range in AST file");
Steve Naroff2ddea052009-04-23 10:39:46 +00004919 return Selector();
4920 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00004921
Sebastian Redlada023c2010-08-04 20:40:17 +00004922 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004923 // Load this selector from the selector table.
Douglas Gregor2262d282011-07-20 01:10:58 +00004924 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
4925 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00004926 Module &M = *I->second;
4927 ASTSelectorLookupTrait Trait(*this, M);
Douglas Gregor8f364fb2011-08-03 23:28:44 +00004928 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
Douglas Gregor2262d282011-07-20 01:10:58 +00004929 SelectorsLoaded[ID - 1] =
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00004930 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
Douglas Gregor2262d282011-07-20 01:10:58 +00004931 if (DeserializationListener)
4932 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
Douglas Gregor95c13f52009-04-25 17:48:32 +00004933 }
4934
Sebastian Redlada023c2010-08-04 20:40:17 +00004935 return SelectorsLoaded[ID - 1];
Steve Naroff2ddea052009-04-23 10:39:46 +00004936}
4937
Douglas Gregor3f8f04f2011-07-28 14:41:43 +00004938Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
Douglas Gregord720daf2010-04-06 17:30:22 +00004939 return DecodeSelector(ID);
4940}
4941
Sebastian Redl2c499f62010-08-18 23:56:43 +00004942uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redlada023c2010-08-04 20:40:17 +00004943 // ID 0 (the null selector) is considered an external selector.
4944 return getTotalNumSelectors() + 1;
Douglas Gregord720daf2010-04-06 17:30:22 +00004945}
4946
Douglas Gregor8f364fb2011-08-03 23:28:44 +00004947serialization::SelectorID
4948ASTReader::getGlobalSelectorID(Module &M, unsigned LocalID) const {
4949 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
4950 return LocalID;
4951
4952 ContinuousRangeMap<uint32_t, int, 2>::iterator I
4953 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
4954 assert(I != M.SelectorRemap.end()
4955 && "Invalid index into identifier index remap");
4956
4957 return LocalID + I->second;
Douglas Gregor3f8f04f2011-07-28 14:41:43 +00004958}
4959
Mike Stump11289f42009-09-09 15:08:12 +00004960DeclarationName
Douglas Gregora6895d82011-07-22 16:00:58 +00004961ASTReader::ReadDeclarationName(Module &F,
Douglas Gregor903b7e92011-07-22 00:38:23 +00004962 const RecordData &Record, unsigned &Idx) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004963 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
4964 switch (Kind) {
4965 case DeclarationName::Identifier:
Douglas Gregora3e41532011-07-28 20:55:49 +00004966 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004967
4968 case DeclarationName::ObjCZeroArgSelector:
4969 case DeclarationName::ObjCOneArgSelector:
4970 case DeclarationName::ObjCMultiArgSelector:
Douglas Gregor074fdc52011-07-28 21:16:51 +00004971 return DeclarationName(ReadSelector(F, Record, Idx));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004972
4973 case DeclarationName::CXXConstructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004974 return Context->DeclarationNames.getCXXConstructorName(
Douglas Gregor903b7e92011-07-22 00:38:23 +00004975 Context->getCanonicalType(readType(F, Record, Idx)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004976
4977 case DeclarationName::CXXDestructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004978 return Context->DeclarationNames.getCXXDestructorName(
Douglas Gregor903b7e92011-07-22 00:38:23 +00004979 Context->getCanonicalType(readType(F, Record, Idx)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004980
4981 case DeclarationName::CXXConversionFunctionName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004982 return Context->DeclarationNames.getCXXConversionFunctionName(
Douglas Gregor903b7e92011-07-22 00:38:23 +00004983 Context->getCanonicalType(readType(F, Record, Idx)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004984
4985 case DeclarationName::CXXOperatorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004986 return Context->DeclarationNames.getCXXOperatorName(
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004987 (OverloadedOperatorKind)Record[Idx++]);
4988
Alexis Hunt3d221f22009-11-29 07:34:05 +00004989 case DeclarationName::CXXLiteralOperatorName:
4990 return Context->DeclarationNames.getCXXLiteralOperatorName(
Douglas Gregora3e41532011-07-28 20:55:49 +00004991 GetIdentifierInfo(F, Record, Idx));
Alexis Hunt3d221f22009-11-29 07:34:05 +00004992
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004993 case DeclarationName::CXXUsingDirective:
4994 return DeclarationName::getUsingDirectiveName();
4995 }
4996
4997 // Required to silence GCC warning
4998 return DeclarationName();
4999}
Douglas Gregor55abb232009-04-10 20:39:37 +00005000
Douglas Gregora6895d82011-07-22 16:00:58 +00005001void ASTReader::ReadDeclarationNameLoc(Module &F,
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005002 DeclarationNameLoc &DNLoc,
5003 DeclarationName Name,
5004 const RecordData &Record, unsigned &Idx) {
5005 switch (Name.getNameKind()) {
5006 case DeclarationName::CXXConstructorName:
5007 case DeclarationName::CXXDestructorName:
5008 case DeclarationName::CXXConversionFunctionName:
5009 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
5010 break;
5011
5012 case DeclarationName::CXXOperatorName:
5013 DNLoc.CXXOperatorName.BeginOpNameLoc
5014 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5015 DNLoc.CXXOperatorName.EndOpNameLoc
5016 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5017 break;
5018
5019 case DeclarationName::CXXLiteralOperatorName:
5020 DNLoc.CXXLiteralOperatorName.OpNameLoc
5021 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5022 break;
5023
5024 case DeclarationName::Identifier:
5025 case DeclarationName::ObjCZeroArgSelector:
5026 case DeclarationName::ObjCOneArgSelector:
5027 case DeclarationName::ObjCMultiArgSelector:
5028 case DeclarationName::CXXUsingDirective:
5029 break;
5030 }
5031}
5032
Douglas Gregora6895d82011-07-22 16:00:58 +00005033void ASTReader::ReadDeclarationNameInfo(Module &F,
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005034 DeclarationNameInfo &NameInfo,
5035 const RecordData &Record, unsigned &Idx) {
Douglas Gregor903b7e92011-07-22 00:38:23 +00005036 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005037 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
5038 DeclarationNameLoc DNLoc;
5039 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
5040 NameInfo.setInfo(DNLoc);
5041}
5042
Douglas Gregora6895d82011-07-22 16:00:58 +00005043void ASTReader::ReadQualifierInfo(Module &F, QualifierInfo &Info,
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005044 const RecordData &Record, unsigned &Idx) {
Douglas Gregor14454802011-02-25 02:25:35 +00005045 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005046 unsigned NumTPLists = Record[Idx++];
5047 Info.NumTemplParamLists = NumTPLists;
5048 if (NumTPLists) {
5049 Info.TemplParamLists = new (*Context) TemplateParameterList*[NumTPLists];
5050 for (unsigned i=0; i != NumTPLists; ++i)
5051 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
5052 }
5053}
5054
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005055TemplateName
Douglas Gregora6895d82011-07-22 16:00:58 +00005056ASTReader::ReadTemplateName(Module &F, const RecordData &Record,
Douglas Gregor5590be02011-01-15 06:45:20 +00005057 unsigned &Idx) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005058 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005059 switch (Kind) {
5060 case TemplateName::Template:
Douglas Gregor7fb09192011-07-21 22:35:25 +00005061 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005062
5063 case TemplateName::OverloadedTemplate: {
5064 unsigned size = Record[Idx++];
5065 UnresolvedSet<8> Decls;
5066 while (size--)
Douglas Gregor7fb09192011-07-21 22:35:25 +00005067 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005068
5069 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
5070 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005071
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005072 case TemplateName::QualifiedTemplate: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00005073 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005074 bool hasTemplKeyword = Record[Idx++];
Douglas Gregor7fb09192011-07-21 22:35:25 +00005075 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005076 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
5077 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005078
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005079 case TemplateName::DependentTemplate: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00005080 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005081 if (Record[Idx++]) // isIdentifier
5082 return Context->getDependentTemplateName(NNS,
Douglas Gregora3e41532011-07-28 20:55:49 +00005083 GetIdentifierInfo(F, Record,
5084 Idx));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005085 return Context->getDependentTemplateName(NNS,
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00005086 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005087 }
John McCalld9dfe3a2011-06-30 08:33:18 +00005088
5089 case TemplateName::SubstTemplateTemplateParm: {
5090 TemplateTemplateParmDecl *param
Douglas Gregor7fb09192011-07-21 22:35:25 +00005091 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
John McCalld9dfe3a2011-06-30 08:33:18 +00005092 if (!param) return TemplateName();
5093 TemplateName replacement = ReadTemplateName(F, Record, Idx);
5094 return Context->getSubstTemplateTemplateParm(param, replacement);
5095 }
Douglas Gregor5590be02011-01-15 06:45:20 +00005096
5097 case TemplateName::SubstTemplateTemplateParmPack: {
5098 TemplateTemplateParmDecl *Param
Douglas Gregor7fb09192011-07-21 22:35:25 +00005099 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
Douglas Gregor5590be02011-01-15 06:45:20 +00005100 if (!Param)
5101 return TemplateName();
5102
5103 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
5104 if (ArgPack.getKind() != TemplateArgument::Pack)
5105 return TemplateName();
5106
5107 return Context->getSubstTemplateTemplateParmPack(Param, ArgPack);
5108 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005109 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005110
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005111 assert(0 && "Unhandled template name kind!");
5112 return TemplateName();
5113}
5114
5115TemplateArgument
Douglas Gregora6895d82011-07-22 16:00:58 +00005116ASTReader::ReadTemplateArgument(Module &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00005117 const RecordData &Record, unsigned &Idx) {
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005118 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
5119 switch (Kind) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005120 case TemplateArgument::Null:
5121 return TemplateArgument();
5122 case TemplateArgument::Type:
Douglas Gregor903b7e92011-07-22 00:38:23 +00005123 return TemplateArgument(readType(F, Record, Idx));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005124 case TemplateArgument::Declaration:
Douglas Gregor7fb09192011-07-21 22:35:25 +00005125 return TemplateArgument(ReadDecl(F, Record, Idx));
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +00005126 case TemplateArgument::Integral: {
5127 llvm::APSInt Value = ReadAPSInt(Record, Idx);
Douglas Gregor903b7e92011-07-22 00:38:23 +00005128 QualType T = readType(F, Record, Idx);
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +00005129 return TemplateArgument(Value, T);
5130 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005131 case TemplateArgument::Template:
Douglas Gregor5590be02011-01-15 06:45:20 +00005132 return TemplateArgument(ReadTemplateName(F, Record, Idx));
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005133 case TemplateArgument::TemplateExpansion: {
Douglas Gregor5590be02011-01-15 06:45:20 +00005134 TemplateName Name = ReadTemplateName(F, Record, Idx);
Douglas Gregore1d60df2011-01-14 23:41:42 +00005135 llvm::Optional<unsigned> NumTemplateExpansions;
5136 if (unsigned NumExpansions = Record[Idx++])
5137 NumTemplateExpansions = NumExpansions - 1;
5138 return TemplateArgument(Name, NumTemplateExpansions);
Douglas Gregoreb29d182011-01-05 17:40:24 +00005139 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005140 case TemplateArgument::Expression:
Sebastian Redl2c373b92010-10-05 15:59:54 +00005141 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005142 case TemplateArgument::Pack: {
5143 unsigned NumArgs = Record[Idx++];
Douglas Gregor1ccc8412010-11-07 23:05:16 +00005144 TemplateArgument *Args = new (*Context) TemplateArgument[NumArgs];
5145 for (unsigned I = 0; I != NumArgs; ++I)
5146 Args[I] = ReadTemplateArgument(F, Record, Idx);
5147 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005148 }
5149 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005150
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005151 assert(0 && "Unhandled template argument kind!");
5152 return TemplateArgument();
5153}
5154
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005155TemplateParameterList *
Douglas Gregora6895d82011-07-22 16:00:58 +00005156ASTReader::ReadTemplateParameterList(Module &F,
Sebastian Redl2c373b92010-10-05 15:59:54 +00005157 const RecordData &Record, unsigned &Idx) {
5158 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
5159 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
5160 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005161
5162 unsigned NumParams = Record[Idx++];
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005163 SmallVector<NamedDecl *, 16> Params;
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005164 Params.reserve(NumParams);
5165 while (NumParams--)
Douglas Gregor7fb09192011-07-21 22:35:25 +00005166 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005167
5168 TemplateParameterList* TemplateParams =
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005169 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
5170 Params.data(), Params.size(), RAngleLoc);
5171 return TemplateParams;
5172}
5173
5174void
Sebastian Redl2c499f62010-08-18 23:56:43 +00005175ASTReader::
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005176ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs,
Douglas Gregora6895d82011-07-22 16:00:58 +00005177 Module &F, const RecordData &Record,
Sebastian Redl2c373b92010-10-05 15:59:54 +00005178 unsigned &Idx) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005179 unsigned NumTemplateArgs = Record[Idx++];
5180 TemplArgs.reserve(NumTemplateArgs);
5181 while (NumTemplateArgs--)
Sebastian Redl2c373b92010-10-05 15:59:54 +00005182 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005183}
5184
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005185/// \brief Read a UnresolvedSet structure.
Douglas Gregora6895d82011-07-22 16:00:58 +00005186void ASTReader::ReadUnresolvedSet(Module &F, UnresolvedSetImpl &Set,
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005187 const RecordData &Record, unsigned &Idx) {
5188 unsigned NumDecls = Record[Idx++];
5189 while (NumDecls--) {
Douglas Gregor7fb09192011-07-21 22:35:25 +00005190 NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx);
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005191 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
5192 Set.addDecl(D, AS);
5193 }
5194}
5195
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005196CXXBaseSpecifier
Douglas Gregora6895d82011-07-22 16:00:58 +00005197ASTReader::ReadCXXBaseSpecifier(Module &F,
Nick Lewycky19b9f952010-07-26 16:56:01 +00005198 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005199 bool isVirtual = static_cast<bool>(Record[Idx++]);
5200 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
5201 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redl08905022011-02-05 19:23:19 +00005202 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00005203 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
5204 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor752a5952011-01-03 22:36:02 +00005205 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redl08905022011-02-05 19:23:19 +00005206 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
Douglas Gregor752a5952011-01-03 22:36:02 +00005207 EllipsisLoc);
Sebastian Redl08905022011-02-05 19:23:19 +00005208 Result.setInheritConstructors(inheritConstructors);
5209 return Result;
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005210}
5211
Alexis Hunt1d792652011-01-08 20:30:50 +00005212std::pair<CXXCtorInitializer **, unsigned>
Douglas Gregora6895d82011-07-22 16:00:58 +00005213ASTReader::ReadCXXCtorInitializers(Module &F, const RecordData &Record,
Alexis Hunt1d792652011-01-08 20:30:50 +00005214 unsigned &Idx) {
5215 CXXCtorInitializer **CtorInitializers = 0;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005216 unsigned NumInitializers = Record[Idx++];
5217 if (NumInitializers) {
5218 ASTContext &C = *getContext();
5219
Alexis Hunt1d792652011-01-08 20:30:50 +00005220 CtorInitializers
5221 = new (C) CXXCtorInitializer*[NumInitializers];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005222 for (unsigned i=0; i != NumInitializers; ++i) {
5223 TypeSourceInfo *BaseClassInfo = 0;
5224 bool IsBaseVirtual = false;
5225 FieldDecl *Member = 0;
Francois Pichetd583da02010-12-04 09:14:42 +00005226 IndirectFieldDecl *IndirectMember = 0;
Alexis Hunt37a477f2011-05-04 01:19:08 +00005227 CXXConstructorDecl *Target = 0;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005228
Alexis Hunt37a477f2011-05-04 01:19:08 +00005229 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
5230 switch (Type) {
5231 case CTOR_INITIALIZER_BASE:
Sebastian Redl2c373b92010-10-05 15:59:54 +00005232 BaseClassInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005233 IsBaseVirtual = Record[Idx++];
Alexis Hunt37a477f2011-05-04 01:19:08 +00005234 break;
5235
5236 case CTOR_INITIALIZER_DELEGATING:
Douglas Gregor7fb09192011-07-21 22:35:25 +00005237 Target = ReadDeclAs<CXXConstructorDecl>(F, Record, Idx);
Alexis Hunt37a477f2011-05-04 01:19:08 +00005238 break;
5239
5240 case CTOR_INITIALIZER_MEMBER:
Douglas Gregor7fb09192011-07-21 22:35:25 +00005241 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
Alexis Hunt37a477f2011-05-04 01:19:08 +00005242 break;
5243
5244 case CTOR_INITIALIZER_INDIRECT_MEMBER:
Douglas Gregor7fb09192011-07-21 22:35:25 +00005245 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
Alexis Hunt37a477f2011-05-04 01:19:08 +00005246 break;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005247 }
Alexis Hunt37a477f2011-05-04 01:19:08 +00005248
Douglas Gregor44e7df62011-01-04 00:32:56 +00005249 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redl2c373b92010-10-05 15:59:54 +00005250 Expr *Init = ReadExpr(F);
Sebastian Redl2c373b92010-10-05 15:59:54 +00005251 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
5252 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005253 bool IsWritten = Record[Idx++];
5254 unsigned SourceOrderOrNumArrayIndices;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005255 SmallVector<VarDecl *, 8> Indices;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005256 if (IsWritten) {
5257 SourceOrderOrNumArrayIndices = Record[Idx++];
5258 } else {
5259 SourceOrderOrNumArrayIndices = Record[Idx++];
5260 Indices.reserve(SourceOrderOrNumArrayIndices);
5261 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
Douglas Gregor7fb09192011-07-21 22:35:25 +00005262 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005263 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005264
Alexis Hunt1d792652011-01-08 20:30:50 +00005265 CXXCtorInitializer *BOMInit;
Alexis Hunt37a477f2011-05-04 01:19:08 +00005266 if (Type == CTOR_INITIALIZER_BASE) {
Alexis Hunt1d792652011-01-08 20:30:50 +00005267 BOMInit = new (C) CXXCtorInitializer(C, BaseClassInfo, IsBaseVirtual,
5268 LParenLoc, Init, RParenLoc,
5269 MemberOrEllipsisLoc);
Alexis Hunt37a477f2011-05-04 01:19:08 +00005270 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
5271 BOMInit = new (C) CXXCtorInitializer(C, MemberOrEllipsisLoc, LParenLoc,
5272 Target, Init, RParenLoc);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005273 } else if (IsWritten) {
Francois Pichetd583da02010-12-04 09:14:42 +00005274 if (Member)
Alexis Hunt1d792652011-01-08 20:30:50 +00005275 BOMInit = new (C) CXXCtorInitializer(C, Member, MemberOrEllipsisLoc,
5276 LParenLoc, Init, RParenLoc);
Francois Pichetd583da02010-12-04 09:14:42 +00005277 else
Alexis Hunt1d792652011-01-08 20:30:50 +00005278 BOMInit = new (C) CXXCtorInitializer(C, IndirectMember,
5279 MemberOrEllipsisLoc, LParenLoc,
5280 Init, RParenLoc);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005281 } else {
Alexis Hunt1d792652011-01-08 20:30:50 +00005282 BOMInit = CXXCtorInitializer::Create(C, Member, MemberOrEllipsisLoc,
5283 LParenLoc, Init, RParenLoc,
5284 Indices.data(), Indices.size());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005285 }
5286
Argyrios Kyrtzidisd05f3e32010-09-06 19:04:27 +00005287 if (IsWritten)
5288 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Alexis Hunt1d792652011-01-08 20:30:50 +00005289 CtorInitializers[i] = BOMInit;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005290 }
5291 }
5292
Alexis Hunt1d792652011-01-08 20:30:50 +00005293 return std::make_pair(CtorInitializers, NumInitializers);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005294}
5295
Chris Lattnerca025db2010-05-07 21:43:38 +00005296NestedNameSpecifier *
Douglas Gregora6895d82011-07-22 16:00:58 +00005297ASTReader::ReadNestedNameSpecifier(Module &F,
Douglas Gregor7fb09192011-07-21 22:35:25 +00005298 const RecordData &Record, unsigned &Idx) {
Chris Lattnerca025db2010-05-07 21:43:38 +00005299 unsigned N = Record[Idx++];
5300 NestedNameSpecifier *NNS = 0, *Prev = 0;
5301 for (unsigned I = 0; I != N; ++I) {
5302 NestedNameSpecifier::SpecifierKind Kind
5303 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
5304 switch (Kind) {
5305 case NestedNameSpecifier::Identifier: {
Douglas Gregora3e41532011-07-28 20:55:49 +00005306 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Chris Lattnerca025db2010-05-07 21:43:38 +00005307 NNS = NestedNameSpecifier::Create(*Context, Prev, II);
5308 break;
5309 }
5310
5311 case NestedNameSpecifier::Namespace: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00005312 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Chris Lattnerca025db2010-05-07 21:43:38 +00005313 NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
5314 break;
5315 }
5316
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005317 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00005318 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005319 NNS = NestedNameSpecifier::Create(*Context, Prev, Alias);
5320 break;
5321 }
5322
Chris Lattnerca025db2010-05-07 21:43:38 +00005323 case NestedNameSpecifier::TypeSpec:
5324 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregor903b7e92011-07-22 00:38:23 +00005325 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
Douglas Gregor0cdc8322010-12-10 17:03:06 +00005326 if (!T)
5327 return 0;
5328
Chris Lattnerca025db2010-05-07 21:43:38 +00005329 bool Template = Record[Idx++];
5330 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
5331 break;
5332 }
5333
5334 case NestedNameSpecifier::Global: {
5335 NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
5336 // No associated value, and there can't be a prefix.
5337 break;
5338 }
Chris Lattnerca025db2010-05-07 21:43:38 +00005339 }
Argyrios Kyrtzidisad65c692010-07-07 15:46:30 +00005340 Prev = NNS;
Chris Lattnerca025db2010-05-07 21:43:38 +00005341 }
5342 return NNS;
5343}
5344
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005345NestedNameSpecifierLoc
Douglas Gregora6895d82011-07-22 16:00:58 +00005346ASTReader::ReadNestedNameSpecifierLoc(Module &F, const RecordData &Record,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005347 unsigned &Idx) {
5348 unsigned N = Record[Idx++];
Douglas Gregor9b272512011-02-28 23:58:31 +00005349 NestedNameSpecifierLocBuilder Builder;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005350 for (unsigned I = 0; I != N; ++I) {
5351 NestedNameSpecifier::SpecifierKind Kind
5352 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
5353 switch (Kind) {
5354 case NestedNameSpecifier::Identifier: {
Douglas Gregora3e41532011-07-28 20:55:49 +00005355 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005356 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00005357 Builder.Extend(*Context, II, Range.getBegin(), Range.getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005358 break;
5359 }
5360
5361 case NestedNameSpecifier::Namespace: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00005362 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005363 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00005364 Builder.Extend(*Context, NS, Range.getBegin(), Range.getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005365 break;
5366 }
5367
5368 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00005369 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005370 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00005371 Builder.Extend(*Context, Alias, Range.getBegin(), Range.getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005372 break;
5373 }
5374
5375 case NestedNameSpecifier::TypeSpec:
5376 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005377 bool Template = Record[Idx++];
5378 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
5379 if (!T)
5380 return NestedNameSpecifierLoc();
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005381 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00005382
5383 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
5384 Builder.Extend(*Context,
5385 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
5386 T->getTypeLoc(), ColonColonLoc);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005387 break;
5388 }
5389
5390 case NestedNameSpecifier::Global: {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005391 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00005392 Builder.MakeGlobal(*Context, ColonColonLoc);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005393 break;
5394 }
5395 }
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005396 }
5397
Douglas Gregor9b272512011-02-28 23:58:31 +00005398 return Builder.getWithLocInContext(*Context);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005399}
5400
Chris Lattnerca025db2010-05-07 21:43:38 +00005401SourceRange
Douglas Gregora6895d82011-07-22 16:00:58 +00005402ASTReader::ReadSourceRange(Module &F, const RecordData &Record,
Sebastian Redl2c373b92010-10-05 15:59:54 +00005403 unsigned &Idx) {
5404 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
5405 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar6d3bc082010-06-02 15:47:10 +00005406 return SourceRange(beg, end);
Chris Lattnerca025db2010-05-07 21:43:38 +00005407}
5408
Douglas Gregor1daeb692009-04-13 18:14:40 +00005409/// \brief Read an integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00005410llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00005411 unsigned BitWidth = Record[Idx++];
5412 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
5413 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
5414 Idx += NumWords;
5415 return Result;
5416}
5417
5418/// \brief Read a signed integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00005419llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00005420 bool isUnsigned = Record[Idx++];
5421 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
5422}
5423
Douglas Gregore0a3a512009-04-14 21:55:33 +00005424/// \brief Read a floating-point value
Sebastian Redl2c499f62010-08-18 23:56:43 +00005425llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00005426 return llvm::APFloat(ReadAPInt(Record, Idx));
5427}
5428
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00005429// \brief Read a string
Sebastian Redl2c499f62010-08-18 23:56:43 +00005430std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00005431 unsigned Len = Record[Idx++];
Jay Foad7d0479f2009-05-21 09:52:38 +00005432 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00005433 Idx += Len;
5434 return Result;
5435}
5436
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005437VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
5438 unsigned &Idx) {
5439 unsigned Major = Record[Idx++];
5440 unsigned Minor = Record[Idx++];
5441 unsigned Subminor = Record[Idx++];
5442 if (Minor == 0)
5443 return VersionTuple(Major);
5444 if (Subminor == 0)
5445 return VersionTuple(Major, Minor - 1);
5446 return VersionTuple(Major, Minor - 1, Subminor - 1);
5447}
5448
Douglas Gregora6895d82011-07-22 16:00:58 +00005449CXXTemporary *ASTReader::ReadCXXTemporary(Module &F,
Douglas Gregor7fb09192011-07-21 22:35:25 +00005450 const RecordData &Record,
Chris Lattnercba86142010-05-10 00:25:06 +00005451 unsigned &Idx) {
Douglas Gregor7fb09192011-07-21 22:35:25 +00005452 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
Chris Lattnercba86142010-05-10 00:25:06 +00005453 return CXXTemporary::Create(*Context, Decl);
5454}
5455
Sebastian Redl2c499f62010-08-18 23:56:43 +00005456DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregor92863e42009-04-10 23:10:45 +00005457 return Diag(SourceLocation(), DiagID);
5458}
5459
Sebastian Redl2c499f62010-08-18 23:56:43 +00005460DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00005461 return Diags.Report(Loc, DiagID);
Douglas Gregor55abb232009-04-10 20:39:37 +00005462}
Douglas Gregora9af1d12009-04-17 00:04:06 +00005463
Douglas Gregora868bbd2009-04-21 22:25:48 +00005464/// \brief Retrieve the identifier table associated with the
5465/// preprocessor.
Sebastian Redl2c499f62010-08-18 23:56:43 +00005466IdentifierTable &ASTReader::getIdentifierTable() {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00005467 assert(PP && "Forgot to set Preprocessor ?");
5468 return PP->getIdentifierTable();
Douglas Gregora868bbd2009-04-21 22:25:48 +00005469}
5470
Douglas Gregora9af1d12009-04-17 00:04:06 +00005471/// \brief Record that the given ID maps to the given switch-case
5472/// statement.
Sebastian Redl2c499f62010-08-18 23:56:43 +00005473void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Douglas Gregora9af1d12009-04-17 00:04:06 +00005474 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
5475 SwitchCaseStmts[ID] = SC;
5476}
5477
5478/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00005479SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Douglas Gregora9af1d12009-04-17 00:04:06 +00005480 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
5481 return SwitchCaseStmts[ID];
5482}
Douglas Gregor6cc68a42009-04-17 18:18:49 +00005483
Argyrios Kyrtzidisd9f526f2010-10-28 09:29:32 +00005484void ASTReader::ClearSwitchCaseIDs() {
5485 SwitchCaseStmts.clear();
5486}
5487
Sebastian Redl2c499f62010-08-18 23:56:43 +00005488void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00005489 assert(NumCurrentElementsDeserializing &&
5490 "FinishedDeserializing not paired with StartedDeserializing");
5491 if (NumCurrentElementsDeserializing == 1) {
Douglas Gregor1342e842009-07-06 18:54:52 +00005492 // If any identifiers with corresponding top-level declarations have
5493 // been loaded, load those declarations now.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00005494 while (!PendingIdentifierInfos.empty()) {
5495 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
5496 PendingIdentifierInfos.front().DeclIDs, true);
5497 PendingIdentifierInfos.pop_front();
Douglas Gregor1342e842009-07-06 18:54:52 +00005498 }
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00005499
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00005500 // Ready to load previous declarations of Decls that were delayed.
5501 while (!PendingPreviousDecls.empty()) {
5502 loadAndAttachPreviousDecl(PendingPreviousDecls.front().first,
5503 PendingPreviousDecls.front().second);
5504 PendingPreviousDecls.pop_front();
5505 }
5506
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00005507 // We are not in recursive loading, so it's safe to pass the "interesting"
5508 // decls to the consumer.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00005509 if (Consumer)
5510 PassInterestingDeclsToConsumer();
Argyrios Kyrtzidisad5f95c2010-10-24 17:26:31 +00005511
5512 assert(PendingForwardRefs.size() == 0 &&
5513 "Some forward refs did not get linked to the definition!");
Douglas Gregor1342e842009-07-06 18:54:52 +00005514 }
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00005515 --NumCurrentElementsDeserializing;
Douglas Gregor1342e842009-07-06 18:54:52 +00005516}
Douglas Gregorb473b072010-08-19 00:28:17 +00005517
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005518ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
Douglas Gregorc567ba22011-07-22 16:35:34 +00005519 StringRef isysroot, bool DisableValidation,
Douglas Gregor606c4ac2011-02-05 19:42:43 +00005520 bool DisableStatCache)
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005521 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
5522 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
5523 Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
Jonathan D. Turnerecc27402011-07-28 17:20:23 +00005524 Consumer(0), ModuleMgr(FileMgr.getFileSystemOptions()),
5525 RelocatablePCH(false), isysroot(isysroot),
Douglas Gregor925296b2011-07-19 16:10:42 +00005526 DisableValidation(DisableValidation),
Douglas Gregor606c4ac2011-02-05 19:42:43 +00005527 DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0),
Douglas Gregor925296b2011-07-19 16:10:42 +00005528 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Douglas Gregor606c4ac2011-02-05 19:42:43 +00005529 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
5530 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
5531 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
5532 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
Jonathan D. Turner3766fdb2011-07-21 21:15:19 +00005533 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
5534 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
5535 NumCXXBaseSpecifiersLoaded(0)
Douglas Gregor606c4ac2011-02-05 19:42:43 +00005536{
Douglas Gregor925296b2011-07-19 16:10:42 +00005537 SourceMgr.setExternalSLocEntrySource(this);
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005538}
5539
5540ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
Douglas Gregorc567ba22011-07-22 16:35:34 +00005541 Diagnostic &Diags, StringRef isysroot,
Douglas Gregor606c4ac2011-02-05 19:42:43 +00005542 bool DisableValidation, bool DisableStatCache)
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005543 : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
Jonathan D. Turnerecc27402011-07-28 17:20:23 +00005544 Diags(Diags), SemaObj(0), PP(0), Context(0),
5545 Consumer(0), ModuleMgr(FileMgr.getFileSystemOptions()),
Douglas Gregor925296b2011-07-19 16:10:42 +00005546 RelocatablePCH(false), isysroot(isysroot),
5547 DisableValidation(DisableValidation), DisableStatCache(DisableStatCache),
5548 NumStatHits(0), NumStatMisses(0), NumSLocEntriesRead(0),
5549 TotalNumSLocEntries(0), NumStatementsRead(0),
5550 TotalNumStatements(0), NumMacrosRead(0), TotalNumMacros(0),
5551 NumSelectorsRead(0), NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
Sebastian Redlc1d035f2010-09-22 20:19:08 +00005552 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
5553 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
Jonathan D. Turner3766fdb2011-07-21 21:15:19 +00005554 TotalVisibleDeclContexts(0), TotalModulesSizeInBits(0),
5555 NumCurrentElementsDeserializing(0), NumCXXBaseSpecifiersLoaded(0)
Douglas Gregor925296b2011-07-19 16:10:42 +00005556{
5557 SourceMgr.setExternalSLocEntrySource(this);
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005558}
5559
5560ASTReader::~ASTReader() {
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005561 for (DeclContextVisibleUpdatesPending::iterator
5562 I = PendingVisibleUpdates.begin(),
5563 E = PendingVisibleUpdates.end();
5564 I != E; ++I) {
5565 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
5566 F = I->second.end();
5567 J != F; ++J)
Douglas Gregorf7180622011-08-03 15:48:04 +00005568 delete static_cast<ASTDeclContextNameLookupTable*>(J->first);
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005569 }
5570}