blob: 7a6a6039f3d94c52ccc0234cc67c483b0264fae6 [file] [log] [blame]
Sebastian Redl3b3c8742010-08-18 23:57:11 +00001//===--- ASTReader.cpp - AST File Reader ------------------------*- C++ -*-===//
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Sebastian Redl2c499f62010-08-18 23:56:43 +000010// This file defines the ASTReader class, which reads AST files.
Douglas Gregoref84c4b2009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
Chris Lattner92ba5ff2009-04-27 05:14:47 +000013
Sebastian Redlf5b13462010-08-18 23:57:17 +000014#include "clang/Serialization/ASTReader.h"
15#include "clang/Serialization/ASTDeserializationListener.h"
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +000016#include "ASTCommon.h"
Douglas Gregor55abb232009-04-10 20:39:37 +000017#include "clang/Frontend/FrontendDiagnostic.h"
Daniel Dunbar732ef8a2009-11-11 23:58:53 +000018#include "clang/Frontend/Utils.h"
Douglas Gregorc3a6ade2010-08-12 20:07:10 +000019#include "clang/Sema/Sema.h"
John McCallcc14d1f2010-08-24 08:50:51 +000020#include "clang/Sema/Scope.h"
Douglas Gregor1a0d0b92009-04-14 00:24:19 +000021#include "clang/AST/ASTConsumer.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000022#include "clang/AST/ASTContext.h"
John McCall19c1bfd2010-08-25 05:32:35 +000023#include "clang/AST/DeclTemplate.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000024#include "clang/AST/Expr.h"
John McCallbfd822c2010-08-24 07:32:53 +000025#include "clang/AST/ExprCXX.h"
Douglas Gregor9b272512011-02-28 23:58:31 +000026#include "clang/AST/NestedNameSpecifier.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000027#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000028#include "clang/AST/TypeLocVisitor.h"
Chris Lattner34321bc2009-04-10 21:41:48 +000029#include "clang/Lex/MacroInfo.h"
Douglas Gregoraae92242010-03-19 21:51:54 +000030#include "clang/Lex/PreprocessingRecord.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000031#include "clang/Lex/Preprocessor.h"
Steve Naroff3fa455a2009-04-24 20:03:17 +000032#include "clang/Lex/HeaderSearch.h"
Douglas Gregora868bbd2009-04-21 22:25:48 +000033#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000034#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000035#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000036#include "clang/Basic/FileManager.h"
Chris Lattner226efd32010-11-23 19:19:34 +000037#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000038#include "clang/Basic/TargetInfo.h"
Douglas Gregord54f3a12009-10-05 21:07:28 +000039#include "clang/Basic/Version.h"
Douglas Gregor20b2ebd2011-03-23 00:50:03 +000040#include "clang/Basic/VersionTuple.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000041#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000042#include "llvm/Bitcode/BitstreamReader.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000043#include "llvm/Support/MemoryBuffer.h"
John McCall0ad16662009-10-29 08:12:44 +000044#include "llvm/Support/ErrorHandling.h"
Douglas Gregor09b69892011-02-10 17:09:37 +000045#include "llvm/Support/FileSystem.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000046#include "llvm/Support/Path.h"
Michael J. Spencerf25faaa2010-12-09 17:36:38 +000047#include "llvm/Support/system_error.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000048#include <algorithm>
Douglas Gregorc379c072009-04-28 18:58:38 +000049#include <iterator>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000050#include <cstdio>
Douglas Gregorc5046832009-04-27 18:38:38 +000051#include <sys/stat.h>
Douglas Gregor09b69892011-02-10 17:09:37 +000052
Douglas Gregoref84c4b2009-04-09 22:27:44 +000053using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000054using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000055
56//===----------------------------------------------------------------------===//
Sebastian Redld44cd6a2010-08-18 23:57:06 +000057// PCH validator implementation
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000058//===----------------------------------------------------------------------===//
59
Sebastian Redl3e31c722010-08-18 23:56:56 +000060ASTReaderListener::~ASTReaderListener() {}
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000061
62bool
63PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts) {
64 const LangOptions &PPLangOpts = PP.getLangOptions();
65#define PARSE_LANGOPT_BENIGN(Option)
66#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
67 if (PPLangOpts.Option != LangOpts.Option) { \
68 Reader.Diag(DiagID) << LangOpts.Option << PPLangOpts.Option; \
69 return true; \
70 }
71
72 PARSE_LANGOPT_BENIGN(Trigraphs);
73 PARSE_LANGOPT_BENIGN(BCPLComment);
74 PARSE_LANGOPT_BENIGN(DollarIdents);
75 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
76 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
Chandler Carruthe03aa552010-04-17 20:17:31 +000077 PARSE_LANGOPT_IMPORTANT(GNUKeywords, diag::warn_pch_gnu_keywords);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000078 PARSE_LANGOPT_BENIGN(ImplicitInt);
79 PARSE_LANGOPT_BENIGN(Digraphs);
80 PARSE_LANGOPT_BENIGN(HexFloats);
81 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
Peter Collingbournea686b5f2011-04-15 00:35:23 +000082 PARSE_LANGOPT_IMPORTANT(C1X, diag::warn_pch_c1x);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000083 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
Michael J. Spencer4992ca4b2010-10-21 05:21:48 +000084 PARSE_LANGOPT_BENIGN(MSCVersion);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000085 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
86 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
87 PARSE_LANGOPT_BENIGN(CXXOperatorName);
88 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
89 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
90 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
Fariborz Jahanian45878032010-02-09 19:31:38 +000091 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI2, diag::warn_pch_nonfragile_abi2);
Fariborz Jahanian13f3b2f2011-01-07 18:59:25 +000092 PARSE_LANGOPT_IMPORTANT(AppleKext, diag::warn_pch_apple_kext);
Ted Kremenek1d56c9e2010-12-23 21:35:43 +000093 PARSE_LANGOPT_IMPORTANT(ObjCDefaultSynthProperties,
94 diag::warn_pch_objc_auto_properties);
Douglas Gregora860e6a2011-06-14 23:20:43 +000095 PARSE_LANGOPT_BENIGN(ObjCInferRelatedResultType)
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +000096 PARSE_LANGOPT_IMPORTANT(NoConstantCFStrings,
Fariborz Jahanian62c56022010-04-22 21:01:59 +000097 diag::warn_pch_no_constant_cfstrings);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000098 PARSE_LANGOPT_BENIGN(PascalStrings);
99 PARSE_LANGOPT_BENIGN(WritableStrings);
Mike Stump11289f42009-09-09 15:08:12 +0000100 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000101 diag::warn_pch_lax_vector_conversions);
Nate Begeman9d905792009-06-25 22:57:40 +0000102 PARSE_LANGOPT_IMPORTANT(AltiVec, diag::warn_pch_altivec);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000103 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
Anders Carlssonce8dd3a2011-02-19 23:53:54 +0000104 PARSE_LANGOPT_IMPORTANT(ObjCExceptions, diag::warn_pch_objc_exceptions);
Anders Carlsson6bbd2682011-02-23 03:04:54 +0000105 PARSE_LANGOPT_IMPORTANT(CXXExceptions, diag::warn_pch_cxx_exceptions);
106 PARSE_LANGOPT_IMPORTANT(SjLjExceptions, diag::warn_pch_sjlj_exceptions);
Douglas Gregordbe39272011-02-01 15:15:22 +0000107 PARSE_LANGOPT_IMPORTANT(MSBitfields, diag::warn_pch_ms_bitfields);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000108 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
109 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
110 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
Mike Stump11289f42009-09-09 15:08:12 +0000111 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000112 diag::warn_pch_thread_safe_statics);
Daniel Dunbara77eaeb2009-09-03 04:54:28 +0000113 PARSE_LANGOPT_IMPORTANT(POSIXThreads, diag::warn_pch_posix_threads);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000114 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
115 PARSE_LANGOPT_BENIGN(EmitAllDecls);
116 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
Chris Lattner51924e512010-06-26 21:25:03 +0000117 PARSE_LANGOPT_BENIGN(getSignedOverflowBehavior());
Mike Stump11289f42009-09-09 15:08:12 +0000118 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000119 diag::warn_pch_heinous_extensions);
120 // FIXME: Most of the options below are benign if the macro wasn't
121 // used. Unfortunately, this means that a PCH compiled without
122 // optimization can't be used with optimization turned on, even
123 // though the only thing that changes is whether __OPTIMIZE__ was
124 // defined... but if __OPTIMIZE__ never showed up in the header, it
125 // doesn't matter. We could consider making this some special kind
126 // of check.
127 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
128 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
129 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
130 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
131 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
132 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
Chandler Carruth7ffce732011-04-23 20:05:38 +0000133 PARSE_LANGOPT_IMPORTANT(Deprecated, diag::warn_pch_deprecated);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000134 PARSE_LANGOPT_IMPORTANT(AccessControl, diag::warn_pch_access_control);
135 PARSE_LANGOPT_IMPORTANT(CharIsSigned, diag::warn_pch_char_signed);
John Thompsoned4e2952009-11-05 20:14:16 +0000136 PARSE_LANGOPT_IMPORTANT(ShortWChar, diag::warn_pch_short_wchar);
Argyrios Kyrtzidis74825bc2010-10-08 00:25:19 +0000137 PARSE_LANGOPT_IMPORTANT(ShortEnums, diag::warn_pch_short_enums);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000138 if ((PPLangOpts.getGCMode() != 0) != (LangOpts.getGCMode() != 0)) {
Mike Stump11289f42009-09-09 15:08:12 +0000139 Reader.Diag(diag::warn_pch_gc_mode)
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000140 << LangOpts.getGCMode() << PPLangOpts.getGCMode();
141 return true;
142 }
143 PARSE_LANGOPT_BENIGN(getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +0000144 PARSE_LANGOPT_IMPORTANT(getStackProtectorMode(),
145 diag::warn_pch_stack_protector);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000146 PARSE_LANGOPT_BENIGN(InstantiationDepth);
Nate Begeman9d905792009-06-25 22:57:40 +0000147 PARSE_LANGOPT_IMPORTANT(OpenCL, diag::warn_pch_opencl);
Peter Collingbourne546d0792010-12-01 19:14:57 +0000148 PARSE_LANGOPT_IMPORTANT(CUDA, diag::warn_pch_cuda);
Mike Stumpd9546382009-12-12 01:27:46 +0000149 PARSE_LANGOPT_BENIGN(CatchUndefined);
John McCall31168b02011-06-15 23:02:42 +0000150 PARSE_LANGOPT_BENIGN(DefaultFPContract);
Daniel Dunbar143021e2009-09-21 04:16:19 +0000151 PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors);
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +0000152 PARSE_LANGOPT_BENIGN(SpellChecking);
John McCall31168b02011-06-15 23:02:42 +0000153 PARSE_LANGOPT_IMPORTANT(ObjCAutoRefCount, diag::warn_pch_auto_ref_count);
154 PARSE_LANGOPT_BENIGN(ObjCInferRelatedReturnType);
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +0000155#undef PARSE_LANGOPT_IMPORTANT
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000156#undef PARSE_LANGOPT_BENIGN
157
158 return false;
159}
160
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000161bool PCHValidator::ReadTargetTriple(StringRef Triple) {
Daniel Dunbar20a682d2009-11-11 00:52:11 +0000162 if (Triple == PP.getTargetInfo().getTriple().str())
163 return false;
164
165 Reader.Diag(diag::warn_pch_target_triple)
166 << Triple << PP.getTargetInfo().getTriple().str();
167 return true;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000168}
169
Benjamin Kramer90b5b682010-11-25 18:29:30 +0000170namespace {
171 struct EmptyStringRef {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000172 bool operator ()(StringRef r) const { return r.empty(); }
Benjamin Kramer90b5b682010-11-25 18:29:30 +0000173 };
174 struct EmptyBlock {
175 bool operator ()(const PCHPredefinesBlock &r) const {return r.Data.empty();}
176 };
177}
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000178
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000179static bool EqualConcatenations(SmallVector<StringRef, 2> L,
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000180 PCHPredefinesBlocks R) {
181 // First, sum up the lengths.
182 unsigned LL = 0, RL = 0;
183 for (unsigned I = 0, N = L.size(); I != N; ++I) {
184 LL += L[I].size();
185 }
186 for (unsigned I = 0, N = R.size(); I != N; ++I) {
187 RL += R[I].Data.size();
188 }
189 if (LL != RL)
190 return false;
191 if (LL == 0 && RL == 0)
192 return true;
193
194 // Kick out empty parts, they confuse the algorithm below.
195 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
196 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
197
198 // Do it the hard way. At this point, both vectors must be non-empty.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000199 StringRef LR = L[0], RR = R[0].Data;
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000200 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
Daniel Dunbar01ad0a72010-07-16 00:00:11 +0000201 (void) RN;
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000202 for (;;) {
203 // Compare the current pieces.
204 if (LR.size() == RR.size()) {
205 // If they're the same length, it's pretty easy.
206 if (LR != RR)
207 return false;
208 // Both pieces are done, advance.
209 ++LI;
210 ++RI;
211 // If either string is done, they're both done, since they're the same
212 // length.
213 if (LI == LN) {
214 assert(RI == RN && "Strings not the same length after all?");
215 return true;
216 }
217 LR = L[LI];
218 RR = R[RI].Data;
219 } else if (LR.size() < RR.size()) {
220 // Right piece is longer.
221 if (!RR.startswith(LR))
222 return false;
223 ++LI;
224 assert(LI != LN && "Strings not the same length after all?");
225 RR = RR.substr(LR.size());
226 LR = L[LI];
227 } else {
228 // Left piece is longer.
229 if (!LR.startswith(RR))
230 return false;
231 ++RI;
232 assert(RI != RN && "Strings not the same length after all?");
233 LR = LR.substr(RR.size());
234 RR = R[RI].Data;
235 }
236 }
237}
238
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000239static std::pair<FileID, StringRef::size_type>
240FindMacro(const PCHPredefinesBlocks &Buffers, StringRef MacroDef) {
241 std::pair<FileID, StringRef::size_type> Res;
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000242 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
243 Res.second = Buffers[I].Data.find(MacroDef);
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000244 if (Res.second != StringRef::npos) {
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000245 Res.first = Buffers[I].BufferID;
246 break;
247 }
248 }
249 return Res;
250}
251
252bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000253 StringRef OriginalFileName,
Nick Lewycky36079892011-02-23 21:16:44 +0000254 std::string &SuggestedPredefines,
255 FileManager &FileMgr) {
Daniel Dunbar732ef8a2009-11-11 23:58:53 +0000256 // We are in the context of an implicit include, so the predefines buffer will
257 // have a #include entry for the PCH file itself (as normalized by the
258 // preprocessor initialization). Find it and skip over it in the checking
259 // below.
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000260 llvm::SmallString<256> PCHInclude;
261 PCHInclude += "#include \"";
Nick Lewycky36079892011-02-23 21:16:44 +0000262 PCHInclude += NormalizeDashIncludePath(OriginalFileName, FileMgr);
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000263 PCHInclude += "\"\n";
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000264 std::pair<StringRef,StringRef> Split =
265 StringRef(PP.getPredefines()).split(PCHInclude.str());
266 StringRef Left = Split.first, Right = Split.second;
Ted Kremenek1ff615c2010-03-18 00:56:54 +0000267 if (Left == PP.getPredefines()) {
268 Error("Missing PCH include entry!");
269 return true;
270 }
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000271
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000272 // If the concatenation of all the PCH buffers is equal to the adjusted
273 // command line, we're done.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000274 SmallVector<StringRef, 2> CommandLine;
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000275 CommandLine.push_back(Left);
276 CommandLine.push_back(Right);
277 if (EqualConcatenations(CommandLine, Buffers))
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000278 return false;
279
280 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump11289f42009-09-09 15:08:12 +0000281
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000282 // The predefines buffers are different. Determine what the differences are,
283 // and whether they require us to reject the PCH file.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000284 SmallVector<StringRef, 8> PCHLines;
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000285 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
286 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000287
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000288 SmallVector<StringRef, 8> CmdLineLines;
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000289 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis58c65412010-09-30 16:53:50 +0000290
291 // Pick out implicit #includes after the PCH and don't consider them for
292 // validation; we will insert them into SuggestedPredefines so that the
293 // preprocessor includes them.
294 std::string IncludesAfterPCH;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000295 SmallVector<StringRef, 8> AfterPCHLines;
Argyrios Kyrtzidis58c65412010-09-30 16:53:50 +0000296 Right.split(AfterPCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
297 for (unsigned i = 0, e = AfterPCHLines.size(); i != e; ++i) {
298 if (AfterPCHLines[i].startswith("#include ")) {
299 IncludesAfterPCH += AfterPCHLines[i];
300 IncludesAfterPCH += '\n';
301 } else {
302 CmdLineLines.push_back(AfterPCHLines[i]);
303 }
304 }
305
306 // Make sure we add the includes last into SuggestedPredefines before we
307 // exit this function.
308 struct AddIncludesRAII {
309 std::string &SuggestedPredefines;
310 std::string &IncludesAfterPCH;
311
312 AddIncludesRAII(std::string &SuggestedPredefines,
313 std::string &IncludesAfterPCH)
314 : SuggestedPredefines(SuggestedPredefines),
315 IncludesAfterPCH(IncludesAfterPCH) { }
316 ~AddIncludesRAII() {
317 SuggestedPredefines += IncludesAfterPCH;
318 }
319 } AddIncludes(SuggestedPredefines, IncludesAfterPCH);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000320
Daniel Dunbar499baed2009-11-11 05:26:28 +0000321 // Sort both sets of predefined buffer lines, since we allow some extra
322 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000323 std::sort(CmdLineLines.begin(), CmdLineLines.end());
324 std::sort(PCHLines.begin(), PCHLines.end());
325
Daniel Dunbar499baed2009-11-11 05:26:28 +0000326 // Determine which predefines that were used to build the PCH file are missing
327 // from the command line.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000328 std::vector<StringRef> MissingPredefines;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000329 std::set_difference(PCHLines.begin(), PCHLines.end(),
330 CmdLineLines.begin(), CmdLineLines.end(),
331 std::back_inserter(MissingPredefines));
332
333 bool MissingDefines = false;
334 bool ConflictingDefines = false;
335 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000336 StringRef Missing = MissingPredefines[I];
Argyrios Kyrtzidis58c65412010-09-30 16:53:50 +0000337 if (Missing.startswith("#include ")) {
338 // An -include was specified when generating the PCH; it is included in
339 // the PCH, just ignore it.
340 continue;
341 }
Daniel Dunbar499baed2009-11-11 05:26:28 +0000342 if (!Missing.startswith("#define ")) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000343 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
344 return true;
345 }
Mike Stump11289f42009-09-09 15:08:12 +0000346
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000347 // This is a macro definition. Determine the name of the macro we're
348 // defining.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000349 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump11289f42009-09-09 15:08:12 +0000350 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000351 = Missing.find_first_of("( \n\r", StartOfMacroName);
352 assert(EndOfMacroName != std::string::npos &&
353 "Couldn't find the end of the macro name");
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000354 StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000355
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000356 // Determine whether this macro was given a different definition on the
357 // command line.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000358 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000359 std::string::size_type MacroDefLen = MacroDefStart.size();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000360 SmallVector<StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000361 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
362 MacroDefStart);
363 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000364 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000365 // Different macro; we're done.
366 ConflictPos = CmdLineLines.end();
Mike Stump11289f42009-09-09 15:08:12 +0000367 break;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000368 }
Mike Stump11289f42009-09-09 15:08:12 +0000369
370 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000371 "Invalid #define in predefines buffer?");
Mike Stump11289f42009-09-09 15:08:12 +0000372 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000373 (*ConflictPos)[MacroDefLen] != '(')
374 continue; // Longer macro name; keep trying.
Mike Stump11289f42009-09-09 15:08:12 +0000375
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000376 // We found a conflicting macro definition.
377 break;
378 }
Mike Stump11289f42009-09-09 15:08:12 +0000379
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000380 if (ConflictPos != CmdLineLines.end()) {
381 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
382 << MacroName;
383
384 // Show the definition of this macro within the PCH file.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000385 std::pair<FileID, StringRef::size_type> MacroLoc =
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000386 FindMacro(Buffers, Missing);
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000387 assert(MacroLoc.second!=StringRef::npos && "Unable to find macro!");
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000388 SourceLocation PCHMissingLoc =
389 SourceMgr.getLocForStartOfFile(MacroLoc.first)
390 .getFileLocWithOffset(MacroLoc.second);
Daniel Dunbar499baed2009-11-11 05:26:28 +0000391 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000392
393 ConflictingDefines = true;
394 continue;
395 }
Mike Stump11289f42009-09-09 15:08:12 +0000396
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000397 // If the macro doesn't conflict, then we'll just pick up the macro
398 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000399 if (ConflictingDefines)
400 continue; // Don't complain if there are already conflicting defs
Mike Stump11289f42009-09-09 15:08:12 +0000401
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000402 if (!MissingDefines) {
403 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
404 MissingDefines = true;
405 }
406
407 // Show the definition of this macro within the PCH file.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000408 std::pair<FileID, StringRef::size_type> MacroLoc =
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000409 FindMacro(Buffers, Missing);
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000410 assert(MacroLoc.second!=StringRef::npos && "Unable to find macro!");
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000411 SourceLocation PCHMissingLoc =
412 SourceMgr.getLocForStartOfFile(MacroLoc.first)
413 .getFileLocWithOffset(MacroLoc.second);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000414 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
415 }
Mike Stump11289f42009-09-09 15:08:12 +0000416
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000417 if (ConflictingDefines)
418 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000419
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000420 // Determine what predefines were introduced based on command-line
421 // parameters that were not present when building the PCH
422 // file. Extra #defines are okay, so long as the identifiers being
423 // defined were not used within the precompiled header.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000424 std::vector<StringRef> ExtraPredefines;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000425 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
426 PCHLines.begin(), PCHLines.end(),
Mike Stump11289f42009-09-09 15:08:12 +0000427 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000428 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000429 StringRef &Extra = ExtraPredefines[I];
Daniel Dunbar499baed2009-11-11 05:26:28 +0000430 if (!Extra.startswith("#define ")) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000431 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
432 return true;
433 }
434
435 // This is an extra macro definition. Determine the name of the
436 // macro we're defining.
437 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump11289f42009-09-09 15:08:12 +0000438 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000439 = Extra.find_first_of("( \n\r", StartOfMacroName);
440 assert(EndOfMacroName != std::string::npos &&
441 "Couldn't find the end of the macro name");
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000442 StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000443
444 // Check whether this name was used somewhere in the PCH file. If
445 // so, defining it as a macro could change behavior, so we reject
446 // the PCH file.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000447 if (IdentifierInfo *II = Reader.get(MacroName)) {
Daniel Dunbar045c92f2009-11-11 00:52:00 +0000448 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000449 return true;
450 }
451
452 // Add this definition to the suggested predefines buffer.
453 SuggestedPredefines += Extra;
454 SuggestedPredefines += '\n';
455 }
456
457 // If we get here, it's because the predefines buffer had compatible
458 // contents. Accept the PCH file.
459 return false;
460}
461
Douglas Gregor5712ebc2010-03-16 16:35:32 +0000462void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
463 unsigned ID) {
464 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
465 ++NumHeaderInfos;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000466}
467
468void PCHValidator::ReadCounter(unsigned Value) {
469 PP.setCounterValue(Value);
470}
471
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000472//===----------------------------------------------------------------------===//
Sebastian Redl2c499f62010-08-18 23:56:43 +0000473// AST reader implementation
Douglas Gregora868bbd2009-04-21 22:25:48 +0000474//===----------------------------------------------------------------------===//
475
Sebastian Redl07a89a82010-07-30 00:29:29 +0000476void
Sebastian Redl3e31c722010-08-18 23:56:56 +0000477ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redl07a89a82010-07-30 00:29:29 +0000478 DeserializationListener = Listener;
Sebastian Redl07a89a82010-07-30 00:29:29 +0000479}
480
Chris Lattner92ba5ff2009-04-27 05:14:47 +0000481
Douglas Gregora868bbd2009-04-21 22:25:48 +0000482namespace {
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000483class ASTSelectorLookupTrait {
Sebastian Redl2c499f62010-08-18 23:56:43 +0000484 ASTReader &Reader;
Douglas Gregora6895d82011-07-22 16:00:58 +0000485 Module &F;
Douglas Gregor7fb09192011-07-21 22:35:25 +0000486
Douglas Gregorc78d3462009-04-24 21:10:55 +0000487public:
Sebastian Redl834bb972010-08-04 17:20:04 +0000488 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +0000489 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +0000490 ObjCMethodList Instance, Factory;
491 };
Douglas Gregorc78d3462009-04-24 21:10:55 +0000492
493 typedef Selector external_key_type;
494 typedef external_key_type internal_key_type;
495
Douglas Gregora6895d82011-07-22 16:00:58 +0000496 ASTSelectorLookupTrait(ASTReader &Reader, Module &F)
Douglas Gregor7fb09192011-07-21 22:35:25 +0000497 : Reader(Reader), F(F) { }
Mike Stump11289f42009-09-09 15:08:12 +0000498
Douglas Gregorc78d3462009-04-24 21:10:55 +0000499 static bool EqualKey(const internal_key_type& a,
500 const internal_key_type& b) {
501 return a == b;
502 }
Mike Stump11289f42009-09-09 15:08:12 +0000503
Douglas Gregorc78d3462009-04-24 21:10:55 +0000504 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +0000505 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000506 }
Mike Stump11289f42009-09-09 15:08:12 +0000507
Douglas Gregorc78d3462009-04-24 21:10:55 +0000508 // This hopefully will just get inlined and removed by the optimizer.
509 static const internal_key_type&
510 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump11289f42009-09-09 15:08:12 +0000511
Douglas Gregorc78d3462009-04-24 21:10:55 +0000512 static std::pair<unsigned, unsigned>
513 ReadKeyDataLength(const unsigned char*& d) {
514 using namespace clang::io;
515 unsigned KeyLen = ReadUnalignedLE16(d);
516 unsigned DataLen = ReadUnalignedLE16(d);
517 return std::make_pair(KeyLen, DataLen);
518 }
Mike Stump11289f42009-09-09 15:08:12 +0000519
Douglas Gregor95c13f52009-04-25 17:48:32 +0000520 internal_key_type ReadKey(const unsigned char* d, unsigned) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000521 using namespace clang::io;
Chris Lattner8575daa2009-04-27 21:45:14 +0000522 SelectorTable &SelTable = Reader.getContext()->Selectors;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000523 unsigned N = ReadUnalignedLE16(d);
Mike Stump11289f42009-09-09 15:08:12 +0000524 IdentifierInfo *FirstII
Douglas Gregora3e41532011-07-28 20:55:49 +0000525 = Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
Douglas Gregorc78d3462009-04-24 21:10:55 +0000526 if (N == 0)
527 return SelTable.getNullarySelector(FirstII);
528 else if (N == 1)
529 return SelTable.getUnarySelector(FirstII);
530
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000531 SmallVector<IdentifierInfo *, 16> Args;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000532 Args.push_back(FirstII);
533 for (unsigned I = 1; I != N; ++I)
Douglas Gregora3e41532011-07-28 20:55:49 +0000534 Args.push_back(Reader.getLocalIdentifier(F, ReadUnalignedLE32(d)));
Douglas Gregorc78d3462009-04-24 21:10:55 +0000535
Douglas Gregor038c3382009-05-22 22:45:36 +0000536 return SelTable.getSelector(N, Args.data());
Douglas Gregorc78d3462009-04-24 21:10:55 +0000537 }
Mike Stump11289f42009-09-09 15:08:12 +0000538
Douglas Gregorc78d3462009-04-24 21:10:55 +0000539 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
540 using namespace clang::io;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000541
542 data_type Result;
543
Douglas Gregor074fdc52011-07-28 21:16:51 +0000544 Result.ID = Reader.getGlobalSelectorID(F, ReadUnalignedLE32(d));
Sebastian Redl834bb972010-08-04 17:20:04 +0000545 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
546 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
547
Douglas Gregorc78d3462009-04-24 21:10:55 +0000548 // Load instance methods
549 ObjCMethodList *Prev = 0;
550 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000551 ObjCMethodDecl *Method
Douglas Gregor7fb09192011-07-21 22:35:25 +0000552 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d));
Sebastian Redl834bb972010-08-04 17:20:04 +0000553 if (!Result.Instance.Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000554 // This is the first method, which is the easy case.
Sebastian Redl834bb972010-08-04 17:20:04 +0000555 Result.Instance.Method = Method;
556 Prev = &Result.Instance;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000557 continue;
558 }
559
Ted Kremenekda4abf12010-02-11 00:53:01 +0000560 ObjCMethodList *Mem =
561 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
562 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000563 Prev = Prev->Next;
564 }
565
566 // Load factory methods
567 Prev = 0;
568 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000569 ObjCMethodDecl *Method
Douglas Gregor7fb09192011-07-21 22:35:25 +0000570 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d));
Sebastian Redl834bb972010-08-04 17:20:04 +0000571 if (!Result.Factory.Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000572 // This is the first method, which is the easy case.
Sebastian Redl834bb972010-08-04 17:20:04 +0000573 Result.Factory.Method = Method;
574 Prev = &Result.Factory;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000575 continue;
576 }
577
Ted Kremenekda4abf12010-02-11 00:53:01 +0000578 ObjCMethodList *Mem =
579 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
580 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000581 Prev = Prev->Next;
582 }
583
584 return Result;
585 }
586};
Mike Stump11289f42009-09-09 15:08:12 +0000587
588} // end anonymous namespace
Douglas Gregorc78d3462009-04-24 21:10:55 +0000589
590/// \brief The on-disk hash table used for the global method pool.
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000591typedef OnDiskChainedHashTable<ASTSelectorLookupTrait>
592 ASTSelectorLookupTable;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000593
Sebastian Redl2c373b92010-10-05 15:59:54 +0000594namespace clang {
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000595class ASTIdentifierLookupTrait {
Sebastian Redl2c499f62010-08-18 23:56:43 +0000596 ASTReader &Reader;
Douglas Gregora6895d82011-07-22 16:00:58 +0000597 Module &F;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000598
599 // If we know the IdentifierInfo in advance, it is here and we will
600 // not build a new one. Used when deserializing information about an
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000601 // identifier that was constructed before the AST file was read.
Douglas Gregora868bbd2009-04-21 22:25:48 +0000602 IdentifierInfo *KnownII;
603
604public:
605 typedef IdentifierInfo * data_type;
606
607 typedef const std::pair<const char*, unsigned> external_key_type;
608
609 typedef external_key_type internal_key_type;
610
Douglas Gregora6895d82011-07-22 16:00:58 +0000611 ASTIdentifierLookupTrait(ASTReader &Reader, Module &F,
Sebastian Redl4e6c5672010-07-21 22:31:37 +0000612 IdentifierInfo *II = 0)
Sebastian Redl2c373b92010-10-05 15:59:54 +0000613 : Reader(Reader), F(F), KnownII(II) { }
Mike Stump11289f42009-09-09 15:08:12 +0000614
Douglas Gregora868bbd2009-04-21 22:25:48 +0000615 static bool EqualKey(const internal_key_type& a,
616 const internal_key_type& b) {
617 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
618 : false;
619 }
Mike Stump11289f42009-09-09 15:08:12 +0000620
Douglas Gregora868bbd2009-04-21 22:25:48 +0000621 static unsigned ComputeHash(const internal_key_type& a) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000622 return llvm::HashString(StringRef(a.first, a.second));
Douglas Gregora868bbd2009-04-21 22:25:48 +0000623 }
Mike Stump11289f42009-09-09 15:08:12 +0000624
Douglas Gregora868bbd2009-04-21 22:25:48 +0000625 // This hopefully will just get inlined and removed by the optimizer.
626 static const internal_key_type&
627 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump11289f42009-09-09 15:08:12 +0000628
Douglas Gregor57756ea2010-10-14 22:11:03 +0000629 // This hopefully will just get inlined and removed by the optimizer.
630 static const external_key_type&
631 GetExternalKey(const internal_key_type& x) { return x; }
632
Douglas Gregora868bbd2009-04-21 22:25:48 +0000633 static std::pair<unsigned, unsigned>
634 ReadKeyDataLength(const unsigned char*& d) {
635 using namespace clang::io;
Douglas Gregor6b7bf5a2009-04-25 20:26:24 +0000636 unsigned DataLen = ReadUnalignedLE16(d);
Douglas Gregor5287b4e2009-04-25 21:04:17 +0000637 unsigned KeyLen = ReadUnalignedLE16(d);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000638 return std::make_pair(KeyLen, DataLen);
639 }
Mike Stump11289f42009-09-09 15:08:12 +0000640
Douglas Gregora868bbd2009-04-21 22:25:48 +0000641 static std::pair<const char*, unsigned>
642 ReadKey(const unsigned char* d, unsigned n) {
643 assert(n >= 2 && d[n-1] == '\0');
644 return std::make_pair((const char*) d, n-1);
645 }
Mike Stump11289f42009-09-09 15:08:12 +0000646
647 IdentifierInfo *ReadData(const internal_key_type& k,
Douglas Gregora868bbd2009-04-21 22:25:48 +0000648 const unsigned char* d,
649 unsigned DataLen) {
650 using namespace clang::io;
Douglas Gregor1ab036c2011-08-03 21:49:18 +0000651 unsigned RawID = ReadUnalignedLE32(d);
652 bool IsInteresting = RawID & 0x01;
Douglas Gregor1d583f22009-04-28 21:18:29 +0000653
654 // Wipe out the "is interesting" bit.
Douglas Gregor1ab036c2011-08-03 21:49:18 +0000655 RawID = RawID >> 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +0000656
Douglas Gregor1ab036c2011-08-03 21:49:18 +0000657 IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
Douglas Gregor1d583f22009-04-28 21:18:29 +0000658 if (!IsInteresting) {
Sebastian Redl98912122010-07-27 23:01:28 +0000659 // For uninteresting identifiers, just build the IdentifierInfo
Douglas Gregor1d583f22009-04-28 21:18:29 +0000660 // and associate it with the persistent ID.
661 IdentifierInfo *II = KnownII;
662 if (!II)
Douglas Gregor1ab036c2011-08-03 21:49:18 +0000663 II = &Reader.getIdentifierTable().getOwn(StringRef(k.first, k.second));
Douglas Gregor1d583f22009-04-28 21:18:29 +0000664 Reader.SetIdentifierInfo(ID, II);
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000665 II->setIsFromAST();
Douglas Gregor1d583f22009-04-28 21:18:29 +0000666 return II;
667 }
668
Douglas Gregorb9256522009-04-28 21:32:13 +0000669 unsigned Bits = ReadUnalignedLE16(d);
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000670 bool CPlusPlusOperatorKeyword = Bits & 0x01;
671 Bits >>= 1;
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +0000672 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
673 Bits >>= 1;
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000674 bool Poisoned = Bits & 0x01;
675 Bits >>= 1;
676 bool ExtensionToken = Bits & 0x01;
677 Bits >>= 1;
678 bool hasMacroDefinition = Bits & 0x01;
679 Bits >>= 1;
680 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
681 Bits >>= 10;
Mike Stump11289f42009-09-09 15:08:12 +0000682
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000683 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregorb9256522009-04-28 21:32:13 +0000684 DataLen -= 6;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000685
686 // Build the IdentifierInfo itself and link the identifier ID with
687 // the new IdentifierInfo.
688 IdentifierInfo *II = KnownII;
689 if (!II)
Douglas Gregor1ab036c2011-08-03 21:49:18 +0000690 II = &Reader.getIdentifierTable().getOwn(StringRef(k.first, k.second));
Douglas Gregora868bbd2009-04-21 22:25:48 +0000691 Reader.SetIdentifierInfo(ID, II);
692
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000693 // Set or check the various bits in the IdentifierInfo structure.
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +0000694 // Token IDs are read-only.
695 if (HasRevertedTokenIDToIdentifier)
696 II->RevertTokenIDToIdentifier();
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000697 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
Mike Stump11289f42009-09-09 15:08:12 +0000698 assert(II->isExtensionToken() == ExtensionToken &&
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000699 "Incorrect extension token flag");
700 (void)ExtensionToken;
Douglas Gregorb36fc532011-08-20 05:09:43 +0000701 if (Poisoned)
702 II->setIsPoisoned(true);
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000703 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
704 "Incorrect C++ operator keyword flag");
705 (void)CPlusPlusOperatorKeyword;
706
Douglas Gregorc3366a52009-04-21 23:56:24 +0000707 // If this identifier is a macro, deserialize the macro
708 // definition.
709 if (hasMacroDefinition) {
Douglas Gregorb36fc532011-08-20 05:09:43 +0000710 // FIXME: Check for conflicts?
Douglas Gregorb9256522009-04-28 21:32:13 +0000711 uint32_t Offset = ReadUnalignedLE32(d);
Douglas Gregor5ef9e332010-10-30 00:23:06 +0000712 Reader.SetIdentifierIsMacro(II, F, Offset);
Douglas Gregorb9256522009-04-28 21:32:13 +0000713 DataLen -= 4;
Douglas Gregorc3366a52009-04-21 23:56:24 +0000714 }
Douglas Gregora868bbd2009-04-21 22:25:48 +0000715
716 // Read all of the declarations visible at global scope with this
717 // name.
Chris Lattner1d728882009-04-27 22:17:41 +0000718 if (Reader.getContext() == 0) return II;
Douglas Gregor1342e842009-07-06 18:54:52 +0000719 if (DataLen > 0) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000720 SmallVector<uint32_t, 4> DeclIDs;
Douglas Gregor1342e842009-07-06 18:54:52 +0000721 for (; DataLen > 0; DataLen -= 4)
Douglas Gregorf224ae02011-07-21 23:29:11 +0000722 DeclIDs.push_back(Reader.getGlobalDeclID(F, ReadUnalignedLE32(d)));
Douglas Gregor1342e842009-07-06 18:54:52 +0000723 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000724 }
Mike Stump11289f42009-09-09 15:08:12 +0000725
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000726 II->setIsFromAST();
Douglas Gregora868bbd2009-04-21 22:25:48 +0000727 return II;
728 }
729};
Mike Stump11289f42009-09-09 15:08:12 +0000730
731} // end anonymous namespace
Douglas Gregora868bbd2009-04-21 22:25:48 +0000732
733/// \brief The on-disk hash table used to contain information about
734/// all of the identifiers in the program.
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000735typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait>
736 ASTIdentifierLookupTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000737
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000738namespace {
739class ASTDeclContextNameLookupTrait {
740 ASTReader &Reader;
Douglas Gregora6895d82011-07-22 16:00:58 +0000741 Module &F;
Douglas Gregor903b7e92011-07-22 00:38:23 +0000742
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000743public:
744 /// \brief Pair of begin/end iterators for DeclIDs.
Douglas Gregor035611e2011-07-28 22:16:57 +0000745 ///
746 /// Note that these declaration IDs are local to the module that contains this
747 /// particular lookup t
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000748 typedef std::pair<DeclID *, DeclID *> data_type;
749
750 /// \brief Special internal key for declaration names.
751 /// The hash table creates keys for comparison; we do not create
752 /// a DeclarationName for the internal key to avoid deserializing types.
753 struct DeclNameKey {
754 DeclarationName::NameKind Kind;
755 uint64_t Data;
756 DeclNameKey() : Kind((DeclarationName::NameKind)0), Data(0) { }
757 };
758
759 typedef DeclarationName external_key_type;
760 typedef DeclNameKey internal_key_type;
761
Douglas Gregor903b7e92011-07-22 00:38:23 +0000762 explicit ASTDeclContextNameLookupTrait(ASTReader &Reader,
Douglas Gregora6895d82011-07-22 16:00:58 +0000763 Module &F)
Douglas Gregor903b7e92011-07-22 00:38:23 +0000764 : Reader(Reader), F(F) { }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000765
766 static bool EqualKey(const internal_key_type& a,
767 const internal_key_type& b) {
768 return a.Kind == b.Kind && a.Data == b.Data;
769 }
770
771 unsigned ComputeHash(const DeclNameKey &Key) const {
772 llvm::FoldingSetNodeID ID;
773 ID.AddInteger(Key.Kind);
774
775 switch (Key.Kind) {
776 case DeclarationName::Identifier:
777 case DeclarationName::CXXLiteralOperatorName:
778 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
779 break;
780 case DeclarationName::ObjCZeroArgSelector:
781 case DeclarationName::ObjCOneArgSelector:
782 case DeclarationName::ObjCMultiArgSelector:
783 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
784 break;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000785 case DeclarationName::CXXOperatorName:
786 ID.AddInteger((OverloadedOperatorKind)Key.Data);
787 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +0000788 case DeclarationName::CXXConstructorName:
789 case DeclarationName::CXXDestructorName:
790 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000791 case DeclarationName::CXXUsingDirective:
792 break;
793 }
794
795 return ID.ComputeHash();
796 }
797
798 internal_key_type GetInternalKey(const external_key_type& Name) const {
799 DeclNameKey Key;
800 Key.Kind = Name.getNameKind();
801 switch (Name.getNameKind()) {
802 case DeclarationName::Identifier:
803 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
804 break;
805 case DeclarationName::ObjCZeroArgSelector:
806 case DeclarationName::ObjCOneArgSelector:
807 case DeclarationName::ObjCMultiArgSelector:
808 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
809 break;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000810 case DeclarationName::CXXOperatorName:
811 Key.Data = Name.getCXXOverloadedOperator();
812 break;
813 case DeclarationName::CXXLiteralOperatorName:
814 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
815 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +0000816 case DeclarationName::CXXConstructorName:
817 case DeclarationName::CXXDestructorName:
818 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000819 case DeclarationName::CXXUsingDirective:
Douglas Gregor3b65ed02011-08-02 18:32:54 +0000820 Key.Data = 0;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000821 break;
822 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000823
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000824 return Key;
825 }
826
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +0000827 external_key_type GetExternalKey(const internal_key_type& Key) const {
828 ASTContext *Context = Reader.getContext();
829 switch (Key.Kind) {
830 case DeclarationName::Identifier:
831 return DeclarationName((IdentifierInfo*)Key.Data);
832
833 case DeclarationName::ObjCZeroArgSelector:
834 case DeclarationName::ObjCOneArgSelector:
835 case DeclarationName::ObjCMultiArgSelector:
836 return DeclarationName(Selector(Key.Data));
837
838 case DeclarationName::CXXConstructorName:
839 return Context->DeclarationNames.getCXXConstructorName(
Douglas Gregor903b7e92011-07-22 00:38:23 +0000840 Context->getCanonicalType(Reader.getLocalType(F, Key.Data)));
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +0000841
842 case DeclarationName::CXXDestructorName:
843 return Context->DeclarationNames.getCXXDestructorName(
Douglas Gregor903b7e92011-07-22 00:38:23 +0000844 Context->getCanonicalType(Reader.getLocalType(F, Key.Data)));
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +0000845
846 case DeclarationName::CXXConversionFunctionName:
847 return Context->DeclarationNames.getCXXConversionFunctionName(
Douglas Gregor903b7e92011-07-22 00:38:23 +0000848 Context->getCanonicalType(Reader.getLocalType(F, Key.Data)));
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +0000849
850 case DeclarationName::CXXOperatorName:
851 return Context->DeclarationNames.getCXXOperatorName(
852 (OverloadedOperatorKind)Key.Data);
853
854 case DeclarationName::CXXLiteralOperatorName:
855 return Context->DeclarationNames.getCXXLiteralOperatorName(
856 (IdentifierInfo*)Key.Data);
857
858 case DeclarationName::CXXUsingDirective:
859 return DeclarationName::getUsingDirectiveName();
860 }
861
862 llvm_unreachable("Invalid Name Kind ?");
863 }
864
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000865 static std::pair<unsigned, unsigned>
866 ReadKeyDataLength(const unsigned char*& d) {
867 using namespace clang::io;
868 unsigned KeyLen = ReadUnalignedLE16(d);
869 unsigned DataLen = ReadUnalignedLE16(d);
870 return std::make_pair(KeyLen, DataLen);
871 }
872
873 internal_key_type ReadKey(const unsigned char* d, unsigned) {
874 using namespace clang::io;
875
876 DeclNameKey Key;
877 Key.Kind = (DeclarationName::NameKind)*d++;
878 switch (Key.Kind) {
879 case DeclarationName::Identifier:
Douglas Gregora3e41532011-07-28 20:55:49 +0000880 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000881 break;
882 case DeclarationName::ObjCZeroArgSelector:
883 case DeclarationName::ObjCOneArgSelector:
884 case DeclarationName::ObjCMultiArgSelector:
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000885 Key.Data =
Douglas Gregor074fdc52011-07-28 21:16:51 +0000886 (uint64_t)Reader.getLocalSelector(F, ReadUnalignedLE32(d))
887 .getAsOpaquePtr();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000888 break;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000889 case DeclarationName::CXXOperatorName:
890 Key.Data = *d++; // OverloadedOperatorKind
891 break;
892 case DeclarationName::CXXLiteralOperatorName:
Douglas Gregora3e41532011-07-28 20:55:49 +0000893 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000894 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +0000895 case DeclarationName::CXXConstructorName:
896 case DeclarationName::CXXDestructorName:
897 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000898 case DeclarationName::CXXUsingDirective:
Douglas Gregor3b65ed02011-08-02 18:32:54 +0000899 Key.Data = 0;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000900 break;
901 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000902
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000903 return Key;
904 }
905
906 data_type ReadData(internal_key_type, const unsigned char* d,
907 unsigned DataLen) {
908 using namespace clang::io;
909 unsigned NumDecls = ReadUnalignedLE16(d);
910 DeclID *Start = (DeclID *)d;
911 return std::make_pair(Start, Start + NumDecls);
912 }
913};
914
915} // end anonymous namespace
916
917/// \brief The on-disk hash table used for the DeclContext's Name lookup table.
918typedef OnDiskChainedHashTable<ASTDeclContextNameLookupTrait>
919 ASTDeclContextNameLookupTable;
920
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000921bool ASTReader::ReadDeclContextStorage(llvm::BitstreamCursor &Cursor,
922 const std::pair<uint64_t, uint64_t> &Offsets,
923 DeclContextInfo &Info) {
924 SavedStreamPosition SavedPosition(Cursor);
925 // First the lexical decls.
926 if (Offsets.first != 0) {
927 Cursor.JumpToBit(Offsets.first);
928
929 RecordData Record;
930 const char *Blob;
931 unsigned BlobLen;
932 unsigned Code = Cursor.ReadCode();
933 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
934 if (RecCode != DECL_CONTEXT_LEXICAL) {
935 Error("Expected lexical block");
936 return true;
937 }
938
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000939 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
940 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000941 } else {
942 Info.LexicalDecls = 0;
943 Info.NumLexicalDecls = 0;
944 }
945
946 // Now the lookup table.
947 if (Offsets.second != 0) {
948 Cursor.JumpToBit(Offsets.second);
949
950 RecordData Record;
951 const char *Blob;
952 unsigned BlobLen;
953 unsigned Code = Cursor.ReadCode();
954 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
955 if (RecCode != DECL_CONTEXT_VISIBLE) {
956 Error("Expected visible lookup table block");
957 return true;
958 }
959 Info.NameLookupTableData
960 = ASTDeclContextNameLookupTable::Create(
961 (const unsigned char *)Blob + Record[0],
962 (const unsigned char *)Blob,
Douglas Gregor903b7e92011-07-22 00:38:23 +0000963 ASTDeclContextNameLookupTrait(*this, *Info.F));
Sebastian Redl9d8f58b2010-08-24 00:50:00 +0000964 } else {
965 Info.NameLookupTableData = 0;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000966 }
967
968 return false;
969}
970
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000971void ASTReader::Error(StringRef Msg) {
Argyrios Kyrtzidisdaa41f52011-04-25 22:23:56 +0000972 Error(diag::err_fe_pch_malformed, Msg);
973}
974
975void ASTReader::Error(unsigned DiagID,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000976 StringRef Arg1, StringRef Arg2) {
Argyrios Kyrtzidisdaa41f52011-04-25 22:23:56 +0000977 if (Diags.isDiagnosticInFlight())
978 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
979 else
980 Diag(DiagID) << Arg1 << Arg2;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000981}
982
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000983/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redl2c499f62010-08-18 23:56:43 +0000984bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000985 if (Listener)
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000986 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000987 ActualOriginalFileName,
Nick Lewycky36079892011-02-23 21:16:44 +0000988 SuggestedPredefines,
989 FileMgr);
Douglas Gregorc379c072009-04-28 18:58:38 +0000990 return false;
Douglas Gregor92863e42009-04-10 23:10:45 +0000991}
992
Douglas Gregorc5046832009-04-27 18:38:38 +0000993//===----------------------------------------------------------------------===//
994// Source Manager Deserialization
995//===----------------------------------------------------------------------===//
996
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000997/// \brief Read the line table in the source manager block.
Sebastian Redl2c373b92010-10-05 15:59:54 +0000998/// \returns true if there was an error.
Douglas Gregora6895d82011-07-22 16:00:58 +0000999bool ASTReader::ParseLineTable(Module &F,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001000 SmallVectorImpl<uint64_t> &Record) {
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001001 unsigned Idx = 0;
1002 LineTableInfo &LineTable = SourceMgr.getLineTable();
1003
1004 // Parse the file names
Douglas Gregora8854652009-04-13 17:12:42 +00001005 std::map<int, int> FileIDs;
1006 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001007 // Extract the file name
1008 unsigned FilenameLen = Record[Idx++];
1009 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
1010 Idx += FilenameLen;
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001011 MaybeAddSystemRootToFilename(Filename);
Jay Foad9a6b0982011-06-21 15:13:30 +00001012 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001013 }
1014
1015 // Parse the line entries
1016 std::vector<LineEntry> Entries;
1017 while (Idx < Record.size()) {
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +00001018 int FID = Record[Idx++];
Douglas Gregor925296b2011-07-19 16:10:42 +00001019 assert(FID >= 0 && "Serialized line entries for non-local file.");
1020 // Remap FileID from 1-based old view.
1021 FID += F.SLocEntryBaseID - 1;
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001022
1023 // Extract the line entries
1024 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +00001025 assert(NumEntries && "Numentries is 00000");
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001026 Entries.clear();
1027 Entries.reserve(NumEntries);
1028 for (unsigned I = 0; I != NumEntries; ++I) {
1029 unsigned FileOffset = Record[Idx++];
1030 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +00001031 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump11289f42009-09-09 15:08:12 +00001032 SrcMgr::CharacteristicKind FileKind
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001033 = (SrcMgr::CharacteristicKind)Record[Idx++];
1034 unsigned IncludeOffset = Record[Idx++];
1035 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1036 FileKind, IncludeOffset));
1037 }
1038 LineTable.AddEntry(FID, Entries);
1039 }
1040
1041 return false;
1042}
1043
Douglas Gregorc5046832009-04-27 18:38:38 +00001044namespace {
1045
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001046class ASTStatData {
Douglas Gregorc5046832009-04-27 18:38:38 +00001047public:
Douglas Gregorc5046832009-04-27 18:38:38 +00001048 const ino_t ino;
1049 const dev_t dev;
1050 const mode_t mode;
1051 const time_t mtime;
1052 const off_t size;
Mike Stump11289f42009-09-09 15:08:12 +00001053
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001054 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Chris Lattner2a6fa472010-11-23 19:28:12 +00001055 : ino(i), dev(d), mode(mo), mtime(m), size(s) {}
Douglas Gregorc5046832009-04-27 18:38:38 +00001056};
1057
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001058class ASTStatLookupTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +00001059 public:
1060 typedef const char *external_key_type;
1061 typedef const char *internal_key_type;
1062
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001063 typedef ASTStatData data_type;
Douglas Gregorc5046832009-04-27 18:38:38 +00001064
1065 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001066 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001067 }
1068
1069 static internal_key_type GetInternalKey(const char *path) { return path; }
1070
1071 static bool EqualKey(internal_key_type a, internal_key_type b) {
1072 return strcmp(a, b) == 0;
1073 }
1074
1075 static std::pair<unsigned, unsigned>
1076 ReadKeyDataLength(const unsigned char*& d) {
1077 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1078 unsigned DataLen = (unsigned) *d++;
1079 return std::make_pair(KeyLen + 1, DataLen);
1080 }
1081
1082 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1083 return (const char *)d;
1084 }
1085
1086 static data_type ReadData(const internal_key_type, const unsigned char *d,
1087 unsigned /*DataLen*/) {
1088 using namespace clang::io;
1089
Douglas Gregorc5046832009-04-27 18:38:38 +00001090 ino_t ino = (ino_t) ReadUnalignedLE32(d);
1091 dev_t dev = (dev_t) ReadUnalignedLE32(d);
1092 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump11289f42009-09-09 15:08:12 +00001093 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregorc5046832009-04-27 18:38:38 +00001094 off_t size = (off_t) ReadUnalignedLE64(d);
1095 return data_type(ino, dev, mode, mtime, size);
1096 }
1097};
1098
1099/// \brief stat() cache for precompiled headers.
1100///
1101/// This cache is very similar to the stat cache used by pretokenized
1102/// headers.
Chris Lattner226efd32010-11-23 19:19:34 +00001103class ASTStatCache : public FileSystemStatCache {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001104 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregorc5046832009-04-27 18:38:38 +00001105 CacheTy *Cache;
1106
1107 unsigned &NumStatHits, &NumStatMisses;
Mike Stump11289f42009-09-09 15:08:12 +00001108public:
Chris Lattner2a6fa472010-11-23 19:28:12 +00001109 ASTStatCache(const unsigned char *Buckets, const unsigned char *Base,
1110 unsigned &NumStatHits, unsigned &NumStatMisses)
Douglas Gregorc5046832009-04-27 18:38:38 +00001111 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
1112 Cache = CacheTy::Create(Buckets, Base);
1113 }
1114
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001115 ~ASTStatCache() { delete Cache; }
Mike Stump11289f42009-09-09 15:08:12 +00001116
Chris Lattnerdd278432010-11-23 21:17:56 +00001117 LookupResult getStat(const char *Path, struct stat &StatBuf,
1118 int *FileDescriptor) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001119 // Do the lookup for the file's data in the AST file.
Chris Lattner226efd32010-11-23 19:19:34 +00001120 CacheTy::iterator I = Cache->find(Path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001121
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001122 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregorc5046832009-04-27 18:38:38 +00001123 if (I == Cache->end()) {
1124 ++NumStatMisses;
Chris Lattnerdd278432010-11-23 21:17:56 +00001125 return statChained(Path, StatBuf, FileDescriptor);
Douglas Gregorc5046832009-04-27 18:38:38 +00001126 }
Mike Stump11289f42009-09-09 15:08:12 +00001127
Douglas Gregorc5046832009-04-27 18:38:38 +00001128 ++NumStatHits;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001129 ASTStatData Data = *I;
Mike Stump11289f42009-09-09 15:08:12 +00001130
Chris Lattner226efd32010-11-23 19:19:34 +00001131 StatBuf.st_ino = Data.ino;
1132 StatBuf.st_dev = Data.dev;
1133 StatBuf.st_mtime = Data.mtime;
1134 StatBuf.st_mode = Data.mode;
1135 StatBuf.st_size = Data.size;
Chris Lattner8f0583d2010-11-23 20:05:15 +00001136 return CacheExists;
Douglas Gregorc5046832009-04-27 18:38:38 +00001137 }
1138};
1139} // end anonymous namespace
1140
1141
Sebastian Redl393f8b72010-07-19 20:52:06 +00001142/// \brief Read a source manager block
Douglas Gregora6895d82011-07-22 16:00:58 +00001143ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(Module &F) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001144 using namespace SrcMgr;
Douglas Gregor258ae542009-04-27 06:38:32 +00001145
Sebastian Redl393f8b72010-07-19 20:52:06 +00001146 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001147
Douglas Gregor258ae542009-04-27 06:38:32 +00001148 // Set the source-location entry cursor to the current position in
1149 // the stream. This cursor will be used to read the contents of the
1150 // source manager block initially, and then lazily read
1151 // source-location entries as needed.
Sebastian Redl393f8b72010-07-19 20:52:06 +00001152 SLocEntryCursor = F.Stream;
Douglas Gregor258ae542009-04-27 06:38:32 +00001153
1154 // The stream itself is going to skip over the source manager block.
Sebastian Redl393f8b72010-07-19 20:52:06 +00001155 if (F.Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001156 Error("malformed block record in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001157 return Failure;
1158 }
1159
1160 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001161 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001162 Error("malformed source manager block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001163 return Failure;
1164 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001165
Douglas Gregora7f71a92009-04-10 03:52:48 +00001166 RecordData Record;
1167 while (true) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001168 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregora7f71a92009-04-10 03:52:48 +00001169 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001170 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001171 Error("error at end of Source Manager block in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001172 return Failure;
1173 }
Douglas Gregor92863e42009-04-10 23:10:45 +00001174 return Success;
Douglas Gregora7f71a92009-04-10 03:52:48 +00001175 }
Mike Stump11289f42009-09-09 15:08:12 +00001176
Douglas Gregora7f71a92009-04-10 03:52:48 +00001177 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1178 // No known subblocks, always skip them.
Douglas Gregor258ae542009-04-27 06:38:32 +00001179 SLocEntryCursor.ReadSubBlockID();
1180 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001181 Error("malformed block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001182 return Failure;
1183 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001184 continue;
1185 }
Mike Stump11289f42009-09-09 15:08:12 +00001186
Douglas Gregora7f71a92009-04-10 03:52:48 +00001187 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001188 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregora7f71a92009-04-10 03:52:48 +00001189 continue;
1190 }
Mike Stump11289f42009-09-09 15:08:12 +00001191
Douglas Gregora7f71a92009-04-10 03:52:48 +00001192 // Read a record.
1193 const char *BlobStart;
1194 unsigned BlobLen;
1195 Record.clear();
Douglas Gregor258ae542009-04-27 06:38:32 +00001196 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001197 default: // Default behavior: ignore.
1198 break;
1199
Sebastian Redl539c5062010-08-18 23:57:32 +00001200 case SM_SLOC_FILE_ENTRY:
1201 case SM_SLOC_BUFFER_ENTRY:
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001202 case SM_SLOC_EXPANSION_ENTRY:
Douglas Gregor258ae542009-04-27 06:38:32 +00001203 // Once we hit one of the source location entries, we're done.
1204 return Success;
Douglas Gregora7f71a92009-04-10 03:52:48 +00001205 }
1206 }
1207}
1208
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001209/// \brief If a header file is not found at the path that we expect it to be
1210/// and the PCH file was moved from its original location, try to resolve the
1211/// file by assuming that header+PCH were moved together and the header is in
1212/// the same place relative to the PCH.
1213static std::string
1214resolveFileRelativeToOriginalDir(const std::string &Filename,
1215 const std::string &OriginalDir,
1216 const std::string &CurrDir) {
1217 assert(OriginalDir != CurrDir &&
1218 "No point trying to resolve the file if the PCH dir didn't change");
1219 using namespace llvm::sys;
1220 llvm::SmallString<128> filePath(Filename);
1221 fs::make_absolute(filePath);
1222 assert(path::is_absolute(OriginalDir));
1223 llvm::SmallString<128> currPCHPath(CurrDir);
1224
1225 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1226 fileDirE = path::end(path::parent_path(filePath));
1227 path::const_iterator origDirI = path::begin(OriginalDir),
1228 origDirE = path::end(OriginalDir);
1229 // Skip the common path components from filePath and OriginalDir.
1230 while (fileDirI != fileDirE && origDirI != origDirE &&
1231 *fileDirI == *origDirI) {
1232 ++fileDirI;
1233 ++origDirI;
1234 }
1235 for (; origDirI != origDirE; ++origDirI)
1236 path::append(currPCHPath, "..");
1237 path::append(currPCHPath, fileDirI, fileDirE);
1238 path::append(currPCHPath, path::filename(Filename));
1239 return currPCHPath.str();
1240}
1241
Douglas Gregor258ae542009-04-27 06:38:32 +00001242/// \brief Read in the source location entry with the given ID.
Douglas Gregor925296b2011-07-19 16:10:42 +00001243ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(int ID) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001244 if (ID == 0)
1245 return Success;
1246
Douglas Gregor49bf76b2011-07-21 18:46:38 +00001247 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001248 Error("source location entry ID out-of-range for AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001249 return Failure;
1250 }
1251
Douglas Gregora6895d82011-07-22 16:00:58 +00001252 Module *F = GlobalSLocEntryMap.find(-ID)->second;
Douglas Gregor925296b2011-07-19 16:10:42 +00001253 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00001254 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Douglas Gregor925296b2011-07-19 16:10:42 +00001255 unsigned BaseOffset = F->SLocEntryBaseOffset;
Sebastian Redl34522812010-07-16 17:50:48 +00001256
Douglas Gregor258ae542009-04-27 06:38:32 +00001257 ++NumSLocEntriesRead;
Douglas Gregor258ae542009-04-27 06:38:32 +00001258 unsigned Code = SLocEntryCursor.ReadCode();
1259 if (Code == llvm::bitc::END_BLOCK ||
1260 Code == llvm::bitc::ENTER_SUBBLOCK ||
1261 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001262 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001263 return Failure;
1264 }
1265
Douglas Gregor258ae542009-04-27 06:38:32 +00001266 RecordData Record;
1267 const char *BlobStart;
1268 unsigned BlobLen;
1269 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1270 default:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001271 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001272 return Failure;
1273
Sebastian Redl539c5062010-08-18 23:57:32 +00001274 case SM_SLOC_FILE_ENTRY: {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001275 std::string Filename(BlobStart, BlobStart + BlobLen);
1276 MaybeAddSystemRootToFilename(Filename);
Chris Lattner5159f612010-11-23 08:35:12 +00001277 const FileEntry *File = FileMgr.getFile(Filename);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001278 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1279 OriginalDir != CurrentDir) {
1280 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1281 OriginalDir,
1282 CurrentDir);
1283 if (!resolved.empty())
1284 File = FileMgr.getFile(resolved);
1285 }
Axel Naumann63fbaed2011-01-27 10:55:51 +00001286 if (File == 0)
1287 File = FileMgr.getVirtualFile(Filename, (off_t)Record[4],
1288 (time_t)Record[5]);
Chris Lattnerd20dc872009-06-15 04:35:16 +00001289 if (File == 0) {
1290 std::string ErrorStr = "could not find file '";
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001291 ErrorStr += Filename;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001292 ErrorStr += "' referenced by AST file";
Chris Lattnerd20dc872009-06-15 04:35:16 +00001293 Error(ErrorStr.c_str());
1294 return Failure;
1295 }
Mike Stump11289f42009-09-09 15:08:12 +00001296
Douglas Gregor09b69892011-02-10 17:09:37 +00001297 if (Record.size() < 6) {
Ted Kremenekabb1ddd2010-03-18 21:23:05 +00001298 Error("source location entry is incorrect");
1299 return Failure;
1300 }
1301
Douglas Gregorce3a8292010-07-27 00:27:13 +00001302 if (!DisableValidation &&
1303 ((off_t)Record[4] != File->getSize()
Douglas Gregor08288f22010-04-09 15:54:22 +00001304#if !defined(LLVM_ON_WIN32)
1305 // In our regression testing, the Windows file system seems to
1306 // have inconsistent modification times that sometimes
1307 // erroneously trigger this error-handling path.
Douglas Gregorce3a8292010-07-27 00:27:13 +00001308 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor08288f22010-04-09 15:54:22 +00001309#endif
Douglas Gregorce3a8292010-07-27 00:27:13 +00001310 )) {
Argyrios Kyrtzidisdaa41f52011-04-25 22:23:56 +00001311 Error(diag::err_fe_pch_file_modified, Filename);
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001312 return Failure;
1313 }
1314
Douglas Gregor925296b2011-07-19 16:10:42 +00001315 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregora6895d82011-07-22 16:00:58 +00001316 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
Douglas Gregor925296b2011-07-19 16:10:42 +00001317 // This is the module's main file.
1318 IncludeLoc = getImportLocation(F);
1319 }
1320 FileID FID = SourceMgr.createFileID(File, IncludeLoc,
Chris Lattner26b5c192010-11-23 09:19:42 +00001321 (SrcMgr::CharacteristicKind)Record[2],
Douglas Gregor925296b2011-07-19 16:10:42 +00001322 ID, BaseOffset + Record[0]);
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001323 SrcMgr::FileInfo &FileInfo =
1324 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1325 FileInfo.NumCreatedFIDs = Record[6];
Douglas Gregor258ae542009-04-27 06:38:32 +00001326 if (Record[3])
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001327 FileInfo.setHasLineDirectives();
Douglas Gregor09b69892011-02-10 17:09:37 +00001328
Douglas Gregor258ae542009-04-27 06:38:32 +00001329 break;
1330 }
1331
Sebastian Redl539c5062010-08-18 23:57:32 +00001332 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor258ae542009-04-27 06:38:32 +00001333 const char *Name = BlobStart;
1334 unsigned Offset = Record[0];
1335 unsigned Code = SLocEntryCursor.ReadCode();
1336 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00001337 unsigned RecCode
Douglas Gregor258ae542009-04-27 06:38:32 +00001338 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001339
Sebastian Redl539c5062010-08-18 23:57:32 +00001340 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001341 Error("AST record has invalid code");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001342 return Failure;
1343 }
1344
Douglas Gregor258ae542009-04-27 06:38:32 +00001345 llvm::MemoryBuffer *Buffer
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001346 = llvm::MemoryBuffer::getMemBuffer(StringRef(BlobStart, BlobLen - 1),
Chris Lattner58c79342010-04-05 22:42:27 +00001347 Name);
Douglas Gregor925296b2011-07-19 16:10:42 +00001348 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID,
1349 BaseOffset + Offset);
Mike Stump11289f42009-09-09 15:08:12 +00001350
Douglas Gregore6648fb2009-04-28 20:33:11 +00001351 if (strcmp(Name, "<built-in>") == 0) {
Sebastian Redl75fbb3b2010-07-14 17:49:11 +00001352 PCHPredefinesBlock Block = {
1353 BufferID,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001354 StringRef(BlobStart, BlobLen - 1)
Sebastian Redl75fbb3b2010-07-14 17:49:11 +00001355 };
1356 PCHPredefinesBuffers.push_back(Block);
Douglas Gregore6648fb2009-04-28 20:33:11 +00001357 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001358
1359 break;
1360 }
1361
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001362 case SM_SLOC_EXPANSION_ENTRY: {
Sebastian Redl2c373b92010-10-05 15:59:54 +00001363 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Chandler Carruth115b0772011-07-26 03:03:05 +00001364 SourceMgr.createExpansionLoc(SpellingLoc,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001365 ReadSourceLocation(*F, Record[2]),
1366 ReadSourceLocation(*F, Record[3]),
Douglas Gregor258ae542009-04-27 06:38:32 +00001367 Record[4],
1368 ID,
Douglas Gregor925296b2011-07-19 16:10:42 +00001369 BaseOffset + Record[0]);
Douglas Gregor258ae542009-04-27 06:38:32 +00001370 break;
Mike Stump11289f42009-09-09 15:08:12 +00001371 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001372 }
1373
1374 return Success;
1375}
1376
Douglas Gregor925296b2011-07-19 16:10:42 +00001377/// \brief Find the location where the module F is imported.
Douglas Gregora6895d82011-07-22 16:00:58 +00001378SourceLocation ASTReader::getImportLocation(Module *F) {
Douglas Gregor925296b2011-07-19 16:10:42 +00001379 if (F->ImportLoc.isValid())
1380 return F->ImportLoc;
Jonathan D. Turner10d52012011-07-29 18:09:09 +00001381
Douglas Gregor925296b2011-07-19 16:10:42 +00001382 // Otherwise we have a PCH. It's considered to be "imported" at the first
1383 // location of its includer.
Jonathan D. Turner10d52012011-07-29 18:09:09 +00001384 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
Douglas Gregor925296b2011-07-19 16:10:42 +00001385 // Main file is the importer. We assume that it is the first entry in the
1386 // entry table. We can't ask the manager, because at the time of PCH loading
1387 // the main file entry doesn't exist yet.
1388 // The very first entry is the invalid instantiation loc, which takes up
1389 // offsets 0 and 1.
1390 return SourceLocation::getFromRawEncoding(2U);
1391 }
Jonathan D. Turner10d52012011-07-29 18:09:09 +00001392 //return F->Loaders[0]->FirstLoc;
1393 return F->ImportedBy[0]->FirstLoc;
Douglas Gregor925296b2011-07-19 16:10:42 +00001394}
1395
Chris Lattnere78a6be2009-04-27 01:05:14 +00001396/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1397/// specified cursor. Read the abbreviations that are at the top of the block
1398/// and then leave the cursor pointing into the block.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001399bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattnere78a6be2009-04-27 01:05:14 +00001400 unsigned BlockID) {
1401 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001402 Error("malformed block record in AST file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001403 return Failure;
1404 }
Mike Stump11289f42009-09-09 15:08:12 +00001405
Chris Lattnere78a6be2009-04-27 01:05:14 +00001406 while (true) {
Douglas Gregor796d76a2010-10-20 22:00:55 +00001407 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattnere78a6be2009-04-27 01:05:14 +00001408 unsigned Code = Cursor.ReadCode();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001409
Chris Lattnere78a6be2009-04-27 01:05:14 +00001410 // We expect all abbrevs to be at the start of the block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001411 if (Code != llvm::bitc::DEFINE_ABBREV) {
1412 Cursor.JumpToBit(Offset);
Chris Lattnere78a6be2009-04-27 01:05:14 +00001413 return false;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001414 }
Chris Lattnere78a6be2009-04-27 01:05:14 +00001415 Cursor.ReadAbbrevRecord();
1416 }
1417}
1418
Douglas Gregor7cb0d012011-08-04 18:09:14 +00001419void ASTReader::ReadMacroRecord(Module &F, uint64_t Offset) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001420 assert(PP && "Forgot to set Preprocessor ?");
Douglas Gregor796d76a2010-10-20 22:00:55 +00001421 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump11289f42009-09-09 15:08:12 +00001422
Douglas Gregorc3366a52009-04-21 23:56:24 +00001423 // Keep track of where we are in the stream, then jump back there
1424 // after reading this macro.
1425 SavedStreamPosition SavedPosition(Stream);
1426
1427 Stream.JumpToBit(Offset);
1428 RecordData Record;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001429 SmallVector<IdentifierInfo*, 16> MacroArgs;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001430 MacroInfo *Macro = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001431
Douglas Gregorc3366a52009-04-21 23:56:24 +00001432 while (true) {
1433 unsigned Code = Stream.ReadCode();
1434 switch (Code) {
1435 case llvm::bitc::END_BLOCK:
Douglas Gregor7cb0d012011-08-04 18:09:14 +00001436 return;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001437
1438 case llvm::bitc::ENTER_SUBBLOCK:
1439 // No known subblocks, always skip them.
1440 Stream.ReadSubBlockID();
1441 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001442 Error("malformed block record in AST file");
Douglas Gregor7cb0d012011-08-04 18:09:14 +00001443 return;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001444 }
1445 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001446
Douglas Gregorc3366a52009-04-21 23:56:24 +00001447 case llvm::bitc::DEFINE_ABBREV:
1448 Stream.ReadAbbrevRecord();
1449 continue;
1450 default: break;
1451 }
1452
1453 // Read a record.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001454 const char *BlobStart = 0;
1455 unsigned BlobLen = 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001456 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001457 PreprocessorRecordTypes RecType =
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001458 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregor796d76a2010-10-20 22:00:55 +00001459 BlobLen);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001460 switch (RecType) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001461 case PP_MACRO_OBJECT_LIKE:
1462 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001463 // If we already have a macro, that means that we've hit the end
1464 // of the definition of the macro we were looking for. We're
1465 // done.
1466 if (Macro)
Douglas Gregor7cb0d012011-08-04 18:09:14 +00001467 return;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001468
Douglas Gregora3e41532011-07-28 20:55:49 +00001469 IdentifierInfo *II = getLocalIdentifier(F, Record[0]);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001470 if (II == 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001471 Error("macro must have a name in AST file");
Douglas Gregor7cb0d012011-08-04 18:09:14 +00001472 return;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001473 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00001474 SourceLocation Loc = ReadSourceLocation(F, Record[1]);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001475 bool isUsed = Record[2];
Mike Stump11289f42009-09-09 15:08:12 +00001476
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001477 MacroInfo *MI = PP->AllocateMacroInfo(Loc);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001478 MI->setIsUsed(isUsed);
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001479 MI->setIsFromAST();
Mike Stump11289f42009-09-09 15:08:12 +00001480
Douglas Gregoraae92242010-03-19 21:51:54 +00001481 unsigned NextIndex = 3;
Sebastian Redl539c5062010-08-18 23:57:32 +00001482 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001483 // Decode function-like macro info.
1484 bool isC99VarArgs = Record[3];
1485 bool isGNUVarArgs = Record[4];
1486 MacroArgs.clear();
1487 unsigned NumArgs = Record[5];
Douglas Gregoraae92242010-03-19 21:51:54 +00001488 NextIndex = 6 + NumArgs;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001489 for (unsigned i = 0; i != NumArgs; ++i)
Douglas Gregora3e41532011-07-28 20:55:49 +00001490 MacroArgs.push_back(getLocalIdentifier(F, Record[6+i]));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001491
1492 // Install function-like macro info.
1493 MI->setIsFunctionLike();
1494 if (isC99VarArgs) MI->setIsC99Varargs();
1495 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor038c3382009-05-22 22:45:36 +00001496 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001497 PP->getPreprocessorAllocator());
Douglas Gregorc3366a52009-04-21 23:56:24 +00001498 }
1499
1500 // Finally, install the macro.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001501 PP->setMacroInfo(II, MI);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001502
1503 // Remember that we saw this macro last so that we add the tokens that
1504 // form its body to it.
1505 Macro = MI;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001506
Douglas Gregoraae92242010-03-19 21:51:54 +00001507 if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) {
1508 // We have a macro definition. Load it now.
1509 PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro,
Douglas Gregor035611e2011-07-28 22:16:57 +00001510 getLocalMacroDefinition(F, Record[NextIndex]));
Douglas Gregoraae92242010-03-19 21:51:54 +00001511 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001512
Douglas Gregorc3366a52009-04-21 23:56:24 +00001513 ++NumMacrosRead;
1514 break;
1515 }
Mike Stump11289f42009-09-09 15:08:12 +00001516
Sebastian Redl539c5062010-08-18 23:57:32 +00001517 case PP_TOKEN: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001518 // If we see a TOKEN before a PP_MACRO_*, then the file is
1519 // erroneous, just pretend we didn't see this.
1520 if (Macro == 0) break;
Mike Stump11289f42009-09-09 15:08:12 +00001521
Douglas Gregorc3366a52009-04-21 23:56:24 +00001522 Token Tok;
1523 Tok.startToken();
Sebastian Redl2c373b92010-10-05 15:59:54 +00001524 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001525 Tok.setLength(Record[1]);
Douglas Gregora3e41532011-07-28 20:55:49 +00001526 if (IdentifierInfo *II = getLocalIdentifier(F, Record[2]))
Douglas Gregorc3366a52009-04-21 23:56:24 +00001527 Tok.setIdentifierInfo(II);
1528 Tok.setKind((tok::TokenKind)Record[3]);
1529 Tok.setFlag((Token::TokenFlags)Record[4]);
1530 Macro->AddTokenToBody(Tok);
1531 break;
1532 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00001533 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001534 }
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001535
Douglas Gregor7cb0d012011-08-04 18:09:14 +00001536 return;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001537}
1538
Douglas Gregora6895d82011-07-22 16:00:58 +00001539PreprocessedEntity *ASTReader::LoadPreprocessedEntity(Module &F) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00001540 assert(PP && "Forgot to set Preprocessor ?");
1541 unsigned Code = F.PreprocessorDetailCursor.ReadCode();
1542 switch (Code) {
1543 case llvm::bitc::END_BLOCK:
1544 return 0;
1545
1546 case llvm::bitc::ENTER_SUBBLOCK:
1547 Error("unexpected subblock record in preprocessor detail block");
1548 return 0;
1549
1550 case llvm::bitc::DEFINE_ABBREV:
1551 Error("unexpected abbrevation record in preprocessor detail block");
1552 return 0;
1553
1554 default:
1555 break;
1556 }
1557
1558 if (!PP->getPreprocessingRecord()) {
1559 Error("no preprocessing record");
1560 return 0;
1561 }
1562
1563 // Read the record.
1564 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1565 const char *BlobStart = 0;
1566 unsigned BlobLen = 0;
1567 RecordData Record;
1568 PreprocessorDetailRecordTypes RecType =
1569 (PreprocessorDetailRecordTypes)F.PreprocessorDetailCursor.ReadRecord(
1570 Code, Record, BlobStart, BlobLen);
1571 switch (RecType) {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001572 case PPD_MACRO_EXPANSION: {
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001573 PreprocessedEntityID GlobalID = getGlobalPreprocessedEntityID(F, Record[0]);
Douglas Gregor0d4b4312011-08-04 17:06:18 +00001574 if (PreprocessedEntity *PE = PPRec.getLoadedPreprocessedEntity(GlobalID-1))
Douglas Gregor92a96f52011-02-08 21:58:10 +00001575 return PE;
1576
Chandler Carrutha88a22182011-07-14 08:20:46 +00001577 MacroExpansion *ME =
Douglas Gregora3e41532011-07-28 20:55:49 +00001578 new (PPRec) MacroExpansion(getLocalIdentifier(F, Record[3]),
Douglas Gregor92a96f52011-02-08 21:58:10 +00001579 SourceRange(ReadSourceLocation(F, Record[1]),
1580 ReadSourceLocation(F, Record[2])),
Douglas Gregor035611e2011-07-28 22:16:57 +00001581 getLocalMacroDefinition(F, Record[4]));
Douglas Gregor0d4b4312011-08-04 17:06:18 +00001582 PPRec.setLoadedPreallocatedEntity(GlobalID - 1, ME);
Chandler Carrutha88a22182011-07-14 08:20:46 +00001583 return ME;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001584 }
1585
1586 case PPD_MACRO_DEFINITION: {
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001587 PreprocessedEntityID GlobalID = getGlobalPreprocessedEntityID(F, Record[0]);
Douglas Gregor0d4b4312011-08-04 17:06:18 +00001588 if (PreprocessedEntity *PE = PPRec.getLoadedPreprocessedEntity(GlobalID-1))
Douglas Gregor92a96f52011-02-08 21:58:10 +00001589 return PE;
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001590
1591 unsigned MacroDefID = getGlobalMacroDefinitionID(F, Record[1]);
1592 if (MacroDefID > MacroDefinitionsLoaded.size()) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00001593 Error("out-of-bounds macro definition record");
1594 return 0;
1595 }
1596
1597 // Decode the identifier info and then check again; if the macro is
1598 // still defined and associated with the identifier,
Douglas Gregora3e41532011-07-28 20:55:49 +00001599 IdentifierInfo *II = getLocalIdentifier(F, Record[4]);
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001600 if (!MacroDefinitionsLoaded[MacroDefID - 1]) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00001601 MacroDefinition *MD
1602 = new (PPRec) MacroDefinition(II,
1603 ReadSourceLocation(F, Record[5]),
1604 SourceRange(
1605 ReadSourceLocation(F, Record[2]),
1606 ReadSourceLocation(F, Record[3])));
1607
Douglas Gregor0d4b4312011-08-04 17:06:18 +00001608 PPRec.setLoadedPreallocatedEntity(GlobalID - 1, MD);
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001609 MacroDefinitionsLoaded[MacroDefID - 1] = MD;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001610
1611 if (DeserializationListener)
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001612 DeserializationListener->MacroDefinitionRead(MacroDefID, MD);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001613 }
1614
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001615 return MacroDefinitionsLoaded[MacroDefID - 1];
Douglas Gregor92a96f52011-02-08 21:58:10 +00001616 }
1617
1618 case PPD_INCLUSION_DIRECTIVE: {
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001619 PreprocessedEntityID GlobalID = getGlobalPreprocessedEntityID(F, Record[0]);
Douglas Gregor0d4b4312011-08-04 17:06:18 +00001620 if (PreprocessedEntity *PE = PPRec.getLoadedPreprocessedEntity(GlobalID-1))
Douglas Gregor92a96f52011-02-08 21:58:10 +00001621 return PE;
1622
1623 const char *FullFileNameStart = BlobStart + Record[3];
1624 const FileEntry *File
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001625 = PP->getFileManager().getFile(StringRef(FullFileNameStart,
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001626 BlobLen - Record[3]));
Douglas Gregor92a96f52011-02-08 21:58:10 +00001627
1628 // FIXME: Stable encoding
1629 InclusionDirective::InclusionKind Kind
1630 = static_cast<InclusionDirective::InclusionKind>(Record[5]);
1631 InclusionDirective *ID
1632 = new (PPRec) InclusionDirective(PPRec, Kind,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001633 StringRef(BlobStart, Record[3]),
Douglas Gregor92a96f52011-02-08 21:58:10 +00001634 Record[4],
1635 File,
1636 SourceRange(ReadSourceLocation(F, Record[1]),
1637 ReadSourceLocation(F, Record[2])));
Douglas Gregor0d4b4312011-08-04 17:06:18 +00001638 PPRec.setLoadedPreallocatedEntity(GlobalID - 1, ID);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001639 return ID;
1640 }
1641 }
1642
1643 Error("invalid offset in preprocessor detail block");
1644 return 0;
1645}
1646
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001647PreprocessedEntityID
1648ASTReader::getGlobalPreprocessedEntityID(Module &M, unsigned LocalID) {
Douglas Gregor2f555fc2011-08-04 18:56:47 +00001649 ContinuousRangeMap<uint32_t, int, 2>::iterator
1650 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1651 assert(I != M.PreprocessedEntityRemap.end()
1652 && "Invalid index into preprocessed entity index remap");
1653
1654 return LocalID + I->second;
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001655}
1656
Douglas Gregor09b69892011-02-10 17:09:37 +00001657namespace {
1658 /// \brief Trait class used to search the on-disk hash table containing all of
1659 /// the header search information.
1660 ///
1661 /// The on-disk hash table contains a mapping from each header path to
1662 /// information about that header (how many times it has been included, its
1663 /// controlling macro, etc.). Note that we actually hash based on the
1664 /// filename, and support "deep" comparisons of file names based on current
1665 /// inode numbers, so that the search can cope with non-normalized path names
1666 /// and symlinks.
1667 class HeaderFileInfoTrait {
Douglas Gregora3e41532011-07-28 20:55:49 +00001668 ASTReader &Reader;
1669 Module &M;
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001670 HeaderSearch *HS;
1671 const char *FrameworkStrings;
Douglas Gregor09b69892011-02-10 17:09:37 +00001672 const char *SearchPath;
1673 struct stat SearchPathStatBuf;
1674 llvm::Optional<int> SearchPathStatResult;
1675
1676 int StatSimpleCache(const char *Path, struct stat *StatBuf) {
1677 if (Path == SearchPath) {
1678 if (!SearchPathStatResult)
1679 SearchPathStatResult = stat(Path, &SearchPathStatBuf);
1680
1681 *StatBuf = SearchPathStatBuf;
1682 return *SearchPathStatResult;
1683 }
1684
1685 return stat(Path, StatBuf);
1686 }
1687
1688 public:
1689 typedef const char *external_key_type;
1690 typedef const char *internal_key_type;
1691
1692 typedef HeaderFileInfo data_type;
1693
Douglas Gregora3e41532011-07-28 20:55:49 +00001694 HeaderFileInfoTrait(ASTReader &Reader, Module &M, HeaderSearch *HS,
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001695 const char *FrameworkStrings,
1696 const char *SearchPath = 0)
Douglas Gregora3e41532011-07-28 20:55:49 +00001697 : Reader(Reader), M(M), HS(HS), FrameworkStrings(FrameworkStrings),
1698 SearchPath(SearchPath) { }
Douglas Gregor09b69892011-02-10 17:09:37 +00001699
1700 static unsigned ComputeHash(const char *path) {
1701 return llvm::HashString(llvm::sys::path::filename(path));
1702 }
1703
1704 static internal_key_type GetInternalKey(const char *path) { return path; }
1705
1706 bool EqualKey(internal_key_type a, internal_key_type b) {
1707 if (strcmp(a, b) == 0)
1708 return true;
1709
1710 if (llvm::sys::path::filename(a) != llvm::sys::path::filename(b))
1711 return false;
1712
1713 // The file names match, but the path names don't. stat() the files to
1714 // see if they are the same.
1715 struct stat StatBufA, StatBufB;
1716 if (StatSimpleCache(a, &StatBufA) || StatSimpleCache(b, &StatBufB))
1717 return false;
1718
1719 return StatBufA.st_ino == StatBufB.st_ino;
1720 }
1721
1722 static std::pair<unsigned, unsigned>
1723 ReadKeyDataLength(const unsigned char*& d) {
1724 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1725 unsigned DataLen = (unsigned) *d++;
1726 return std::make_pair(KeyLen + 1, DataLen);
1727 }
1728
1729 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1730 return (const char *)d;
1731 }
1732
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001733 data_type ReadData(const internal_key_type, const unsigned char *d,
1734 unsigned DataLen) {
Douglas Gregor09b69892011-02-10 17:09:37 +00001735 const unsigned char *End = d + DataLen;
1736 using namespace clang::io;
1737 HeaderFileInfo HFI;
1738 unsigned Flags = *d++;
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001739 HFI.isImport = (Flags >> 5) & 0x01;
1740 HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1741 HFI.DirInfo = (Flags >> 2) & 0x03;
1742 HFI.Resolved = (Flags >> 1) & 0x01;
1743 HFI.IndexHeaderMapHeader = Flags & 0x01;
Douglas Gregor09b69892011-02-10 17:09:37 +00001744 HFI.NumIncludes = ReadUnalignedLE16(d);
Douglas Gregora3e41532011-07-28 20:55:49 +00001745 HFI.ControllingMacroID = Reader.getGlobalDeclID(M, ReadUnalignedLE32(d));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001746 if (unsigned FrameworkOffset = ReadUnalignedLE32(d)) {
1747 // The framework offset is 1 greater than the actual offset,
1748 // since 0 is used as an indicator for "no framework name".
1749 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1750 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1751 }
1752
Douglas Gregor09b69892011-02-10 17:09:37 +00001753 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1754 (void)End;
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001755
Douglas Gregor09b69892011-02-10 17:09:37 +00001756 // This HeaderFileInfo was externally loaded.
1757 HFI.External = true;
1758 return HFI;
1759 }
1760 };
1761}
1762
1763/// \brief The on-disk hash table used for the global method pool.
1764typedef OnDiskChainedHashTable<HeaderFileInfoTrait>
1765 HeaderFileInfoLookupTable;
1766
Douglas Gregora6895d82011-07-22 16:00:58 +00001767void ASTReader::SetIdentifierIsMacro(IdentifierInfo *II, Module &F,
Douglas Gregor074fdc52011-07-28 21:16:51 +00001768 uint64_t LocalOffset) {
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001769 // Note that this identifier has a macro definition.
1770 II->setHasMacroDefinition(true);
1771
Douglas Gregord32f0352011-07-22 06:10:01 +00001772 // Adjust the offset to a global offset.
Douglas Gregor074fdc52011-07-28 21:16:51 +00001773 UnreadMacroRecordOffsets[II] = F.GlobalBitOffset + LocalOffset;
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001774}
1775
Sebastian Redl2c499f62010-08-18 23:56:43 +00001776void ASTReader::ReadDefinedMacros() {
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00001777 for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1778 E = ModuleMgr.rend(); I != E; ++I) {
1779 llvm::BitstreamCursor &MacroCursor = (*I)->MacroCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001780
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001781 // If there was no preprocessor block, skip this file.
1782 if (!MacroCursor.getBitStreamReader())
1783 continue;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001784
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001785 llvm::BitstreamCursor Cursor = MacroCursor;
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00001786 Cursor.JumpToBit((*I)->MacroStartOffset);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001787
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001788 RecordData Record;
1789 while (true) {
1790 unsigned Code = Cursor.ReadCode();
Douglas Gregor796d76a2010-10-20 22:00:55 +00001791 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001792 break;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001793
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001794 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1795 // No known subblocks, always skip them.
1796 Cursor.ReadSubBlockID();
1797 if (Cursor.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001798 Error("malformed block record in AST file");
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001799 return;
1800 }
1801 continue;
1802 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001803
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001804 if (Code == llvm::bitc::DEFINE_ABBREV) {
1805 Cursor.ReadAbbrevRecord();
1806 continue;
1807 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001808
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001809 // Read a record.
1810 const char *BlobStart;
1811 unsigned BlobLen;
1812 Record.clear();
1813 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1814 default: // Default behavior: ignore.
1815 break;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001816
Sebastian Redl539c5062010-08-18 23:57:32 +00001817 case PP_MACRO_OBJECT_LIKE:
1818 case PP_MACRO_FUNCTION_LIKE:
Douglas Gregora3e41532011-07-28 20:55:49 +00001819 getLocalIdentifier(**I, Record[0]);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001820 break;
1821
Sebastian Redl539c5062010-08-18 23:57:32 +00001822 case PP_TOKEN:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001823 // Ignore tokens.
1824 break;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001825 }
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001826 }
1827 }
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001828
1829 // Drain the unread macro-record offsets map.
1830 while (!UnreadMacroRecordOffsets.empty())
1831 LoadMacroDefinition(UnreadMacroRecordOffsets.begin());
1832}
1833
1834void ASTReader::LoadMacroDefinition(
1835 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos) {
1836 assert(Pos != UnreadMacroRecordOffsets.end() && "Unknown macro definition");
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001837 uint64_t Offset = Pos->second;
1838 UnreadMacroRecordOffsets.erase(Pos);
1839
Douglas Gregord32f0352011-07-22 06:10:01 +00001840 RecordLocation Loc = getLocalBitOffset(Offset);
1841 ReadMacroRecord(*Loc.F, Loc.Offset);
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001842}
1843
1844void ASTReader::LoadMacroDefinition(IdentifierInfo *II) {
1845 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos
1846 = UnreadMacroRecordOffsets.find(II);
1847 LoadMacroDefinition(Pos);
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001848}
1849
Sebastian Redl50e26582010-09-15 19:54:06 +00001850MacroDefinition *ASTReader::getMacroDefinition(MacroID ID) {
Douglas Gregor91096292010-10-02 19:29:26 +00001851 if (ID == 0 || ID > MacroDefinitionsLoaded.size())
Douglas Gregoraae92242010-03-19 21:51:54 +00001852 return 0;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001853
Douglas Gregor91096292010-10-02 19:29:26 +00001854 if (!MacroDefinitionsLoaded[ID - 1]) {
Douglas Gregor270e0142011-07-20 01:29:15 +00001855 GlobalMacroDefinitionMapType::iterator I =GlobalMacroDefinitionMap.find(ID);
1856 assert(I != GlobalMacroDefinitionMap.end() &&
1857 "Corrupted global macro definition map");
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00001858 Module &M = *I->second;
1859 unsigned Index = ID - 1 - M.BaseMacroDefinitionID;
1860 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
1861 M.PreprocessorDetailCursor.JumpToBit(M.MacroDefinitionOffsets[Index]);
1862 LoadPreprocessedEntity(M);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001863 }
1864
Douglas Gregor91096292010-10-02 19:29:26 +00001865 return MacroDefinitionsLoaded[ID - 1];
Douglas Gregoraae92242010-03-19 21:51:54 +00001866}
1867
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001868const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) {
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00001869 std::string Filename = filenameStrRef;
1870 MaybeAddSystemRootToFilename(Filename);
1871 const FileEntry *File = FileMgr.getFile(Filename);
1872 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1873 OriginalDir != CurrentDir) {
1874 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1875 OriginalDir,
1876 CurrentDir);
1877 if (!resolved.empty())
1878 File = FileMgr.getFile(resolved);
1879 }
1880
1881 return File;
1882}
1883
Douglas Gregor035611e2011-07-28 22:16:57 +00001884MacroID ASTReader::getGlobalMacroDefinitionID(Module &M, unsigned LocalID) {
Douglas Gregora863b4b2011-08-04 16:36:56 +00001885 if (LocalID < NUM_PREDEF_MACRO_IDS)
1886 return LocalID;
1887
1888 ContinuousRangeMap<uint32_t, int, 2>::iterator I
1889 = M.MacroDefinitionRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
1890 assert(I != M.MacroDefinitionRemap.end() &&
1891 "Invalid index into macro definition ID remap");
1892
1893 return LocalID + I->second;
Douglas Gregor035611e2011-07-28 22:16:57 +00001894}
1895
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001896/// \brief If we are loading a relocatable PCH file, and the filename is
1897/// not an absolute path, add the system root to the beginning of the file
1898/// name.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001899void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001900 // If this is not a relocatable PCH file, there's nothing to do.
1901 if (!RelocatablePCH)
1902 return;
Mike Stump11289f42009-09-09 15:08:12 +00001903
Michael J. Spencerf28df4c2010-12-17 21:22:22 +00001904 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001905 return;
1906
Douglas Gregorc567ba22011-07-22 16:35:34 +00001907 if (isysroot.empty()) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001908 // If no system root was given, default to '/'
1909 Filename.insert(Filename.begin(), '/');
1910 return;
1911 }
Mike Stump11289f42009-09-09 15:08:12 +00001912
Douglas Gregorc567ba22011-07-22 16:35:34 +00001913 unsigned Length = isysroot.size();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001914 if (isysroot[Length - 1] != '/')
1915 Filename.insert(Filename.begin(), '/');
Mike Stump11289f42009-09-09 15:08:12 +00001916
Douglas Gregorc567ba22011-07-22 16:35:34 +00001917 Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001918}
1919
Sebastian Redl2c499f62010-08-18 23:56:43 +00001920ASTReader::ASTReadResult
Douglas Gregora6895d82011-07-22 16:00:58 +00001921ASTReader::ReadASTBlock(Module &F) {
Sebastian Redl34522812010-07-16 17:50:48 +00001922 llvm::BitstreamCursor &Stream = F.Stream;
1923
Sebastian Redl539c5062010-08-18 23:57:32 +00001924 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001925 Error("malformed block record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001926 return Failure;
1927 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001928
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001929 // Read all of the records and blocks for the ASt file.
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001930 RecordData Record;
Sebastian Redl393f8b72010-07-19 20:52:06 +00001931 bool First = true;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001932 while (!Stream.AtEndOfStream()) {
1933 unsigned Code = Stream.ReadCode();
1934 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor55abb232009-04-10 20:39:37 +00001935 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001936 Error("error at end of module block in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001937 return Failure;
1938 }
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001939
Douglas Gregor55abb232009-04-10 20:39:37 +00001940 return Success;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001941 }
1942
1943 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1944 switch (Stream.ReadSubBlockID()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001945 case DECLTYPES_BLOCK_ID:
Chris Lattnere78a6be2009-04-27 01:05:14 +00001946 // We lazily load the decls block, but we want to set up the
1947 // DeclsCursor cursor to point into it. Clone our current bitcode
1948 // cursor to it, enter the block and read the abbrevs in that block.
1949 // With the main cursor, we just skip over it.
Sebastian Redl34522812010-07-16 17:50:48 +00001950 F.DeclsCursor = Stream;
Chris Lattnere78a6be2009-04-27 01:05:14 +00001951 if (Stream.SkipBlock() || // Skip with the main cursor.
1952 // Read the abbrevs.
Sebastian Redl539c5062010-08-18 23:57:32 +00001953 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001954 Error("malformed block record in AST file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001955 return Failure;
1956 }
1957 break;
Mike Stump11289f42009-09-09 15:08:12 +00001958
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00001959 case DECL_UPDATES_BLOCK_ID:
1960 if (Stream.SkipBlock()) {
1961 Error("malformed block record in AST file");
1962 return Failure;
1963 }
1964 break;
1965
Sebastian Redl539c5062010-08-18 23:57:32 +00001966 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl34522812010-07-16 17:50:48 +00001967 F.MacroCursor = Stream;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001968 if (PP)
1969 PP->setExternalSource(this);
1970
Douglas Gregor796d76a2010-10-20 22:00:55 +00001971 if (Stream.SkipBlock() ||
1972 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001973 Error("malformed block record in AST file");
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001974 return Failure;
1975 }
Douglas Gregor796d76a2010-10-20 22:00:55 +00001976 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001977 break;
Steve Naroff2ddea052009-04-23 10:39:46 +00001978
Douglas Gregor92a96f52011-02-08 21:58:10 +00001979 case PREPROCESSOR_DETAIL_BLOCK_ID:
1980 F.PreprocessorDetailCursor = Stream;
1981 if (Stream.SkipBlock() ||
1982 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
1983 PREPROCESSOR_DETAIL_BLOCK_ID)) {
1984 Error("malformed preprocessor detail record in AST file");
1985 return Failure;
1986 }
1987 F.PreprocessorDetailStartOffset
1988 = F.PreprocessorDetailCursor.GetCurrentBitNo();
1989 break;
1990
Sebastian Redl539c5062010-08-18 23:57:32 +00001991 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl393f8b72010-07-19 20:52:06 +00001992 switch (ReadSourceManagerBlock(F)) {
Douglas Gregor92863e42009-04-10 23:10:45 +00001993 case Success:
1994 break;
1995
1996 case Failure:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001997 Error("malformed source manager block in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001998 return Failure;
Douglas Gregor92863e42009-04-10 23:10:45 +00001999
2000 case IgnorePCH:
2001 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00002002 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00002003 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002004 }
Sebastian Redl393f8b72010-07-19 20:52:06 +00002005 First = false;
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00002006 continue;
2007 }
2008
2009 if (Code == llvm::bitc::DEFINE_ABBREV) {
2010 Stream.ReadAbbrevRecord();
2011 continue;
2012 }
2013
2014 // Read and process a record.
2015 Record.clear();
Douglas Gregorbfbde532009-04-10 21:16:55 +00002016 const char *BlobStart = 0;
2017 unsigned BlobLen = 0;
Sebastian Redl539c5062010-08-18 23:57:32 +00002018 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redl2c373b92010-10-05 15:59:54 +00002019 &BlobStart, &BlobLen)) {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00002020 default: // Default behavior: ignore.
2021 break;
2022
Sebastian Redl539c5062010-08-18 23:57:32 +00002023 case METADATA: {
2024 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2025 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002026 : diag::warn_pch_version_too_new);
2027 return IgnorePCH;
2028 }
2029
2030 RelocatablePCH = Record[4];
2031 if (Listener) {
2032 std::string TargetTriple(BlobStart, BlobLen);
2033 if (Listener->ReadTargetTriple(TargetTriple))
2034 return IgnorePCH;
2035 }
2036 break;
2037 }
2038
Douglas Gregor29cc6422011-08-17 21:07:30 +00002039 case IMPORTS: {
2040 // Load each of the imported PCH files.
2041 unsigned Idx = 0, N = Record.size();
2042 while (Idx < N) {
2043 // Read information about the AST file.
2044 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2045 unsigned Length = Record[Idx++];
2046 llvm::SmallString<128> ImportedFile(Record.begin() + Idx,
2047 Record.begin() + Idx + Length);
2048 Idx += Length;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002049
Douglas Gregor29cc6422011-08-17 21:07:30 +00002050 // Load the AST file.
Douglas Gregordf0c1512011-08-18 04:12:04 +00002051 switch(ReadASTCore(ImportedFile, ImportedKind, &F)) {
Douglas Gregor29cc6422011-08-17 21:07:30 +00002052 case Failure: return Failure;
2053 // If we have to ignore the dependency, we'll have to ignore this too.
2054 case IgnorePCH: return IgnorePCH;
2055 case Success: break;
2056 }
2057 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002058 break;
2059 }
2060
Douglas Gregor5204bde2011-08-02 16:26:37 +00002061 case TYPE_OFFSET: {
Sebastian Redl9e687992010-07-19 22:06:55 +00002062 if (F.LocalNumTypes != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002063 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00002064 return Failure;
2065 }
Sebastian Redl9e687992010-07-19 22:06:55 +00002066 F.TypeOffsets = (const uint32_t *)BlobStart;
2067 F.LocalNumTypes = Record[0];
Douglas Gregor3b65ed02011-08-02 18:32:54 +00002068 unsigned LocalBaseTypeIndex = Record[1];
2069 F.BaseTypeIndex = getTotalNumTypes();
Douglas Gregor8ab4ea82011-07-29 00:21:44 +00002070
Douglas Gregor5204bde2011-08-02 16:26:37 +00002071 if (F.LocalNumTypes > 0) {
2072 // Introduce the global -> local mapping for types within this module.
2073 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
2074
2075 // Introduce the local -> global mapping for types within this module.
Douglas Gregor3b65ed02011-08-02 18:32:54 +00002076 F.TypeRemap.insert(std::make_pair(LocalBaseTypeIndex,
2077 F.BaseTypeIndex - LocalBaseTypeIndex));
Douglas Gregor5204bde2011-08-02 16:26:37 +00002078
2079 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
2080 }
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00002081 break;
Douglas Gregor5204bde2011-08-02 16:26:37 +00002082 }
2083
Douglas Gregorf7180622011-08-03 15:48:04 +00002084 case DECL_OFFSET: {
Sebastian Redl9e687992010-07-19 22:06:55 +00002085 if (F.LocalNumDecls != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002086 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00002087 return Failure;
2088 }
Sebastian Redl9e687992010-07-19 22:06:55 +00002089 F.DeclOffsets = (const uint32_t *)BlobStart;
2090 F.LocalNumDecls = Record[0];
Douglas Gregorf7180622011-08-03 15:48:04 +00002091 unsigned LocalBaseDeclID = Record[1];
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00002092 F.BaseDeclID = getTotalNumDecls();
Douglas Gregor047d2ef2011-07-20 00:27:43 +00002093
Douglas Gregorf7180622011-08-03 15:48:04 +00002094 if (F.LocalNumDecls > 0) {
2095 // Introduce the global -> local mapping for declarations within this
2096 // module.
Douglas Gregordab42432011-08-12 00:15:20 +00002097 GlobalDeclMap.insert(
2098 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
Douglas Gregorf7180622011-08-03 15:48:04 +00002099
2100 // Introduce the local -> global mapping for declarations within this
2101 // module.
2102 F.DeclRemap.insert(std::make_pair(LocalBaseDeclID,
2103 F.BaseDeclID - LocalBaseDeclID));
2104
2105 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
2106 }
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00002107 break;
Douglas Gregorf7180622011-08-03 15:48:04 +00002108 }
2109
Sebastian Redl539c5062010-08-18 23:57:32 +00002110 case TU_UPDATE_LEXICAL: {
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002111 DeclContextInfo Info = {
Douglas Gregorf224ae02011-07-21 23:29:11 +00002112 &F,
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002113 /* No visible information */ 0,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002114 reinterpret_cast<const KindDeclIDPair *>(BlobStart),
Douglas Gregordc5c9582011-07-28 14:20:37 +00002115 static_cast<unsigned int>(BlobLen / sizeof(KindDeclIDPair))
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002116 };
Douglas Gregordab42432011-08-12 00:15:20 +00002117
2118 DeclContext *TU = Context ? Context->getTranslationUnitDecl() : 0;
2119 DeclContextOffsets[TU].push_back(Info);
2120 if (TU)
2121 TU->setHasExternalLexicalStorage(true);
2122
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002123 break;
2124 }
2125
Sebastian Redld7dce0a2010-08-24 00:50:04 +00002126 case UPDATE_VISIBLE: {
Douglas Gregorf7180622011-08-03 15:48:04 +00002127 unsigned Idx = 0;
2128 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
Sebastian Redld7dce0a2010-08-24 00:50:04 +00002129 void *Table = ASTDeclContextNameLookupTable::Create(
Douglas Gregorf7180622011-08-03 15:48:04 +00002130 (const unsigned char *)BlobStart + Record[Idx++],
Sebastian Redld7dce0a2010-08-24 00:50:04 +00002131 (const unsigned char *)BlobStart,
Douglas Gregor903b7e92011-07-22 00:38:23 +00002132 ASTDeclContextNameLookupTrait(*this, F));
Douglas Gregorf7180622011-08-03 15:48:04 +00002133 // FIXME: Complete hack to check for the TU
Douglas Gregordab42432011-08-12 00:15:20 +00002134 if (ID == PREDEF_DECL_TRANSLATION_UNIT_ID && Context) { // Is it the TU?
Sebastian Redld7dce0a2010-08-24 00:50:04 +00002135 DeclContextInfo Info = {
Douglas Gregorf7180622011-08-03 15:48:04 +00002136 &F, Table, /* No lexical information */ 0, 0
Sebastian Redld7dce0a2010-08-24 00:50:04 +00002137 };
Douglas Gregordab42432011-08-12 00:15:20 +00002138
2139 DeclContext *TU = Context->getTranslationUnitDecl();
2140 DeclContextOffsets[TU].push_back(Info);
2141 TU->setHasExternalVisibleStorage(true);
Sebastian Redld7dce0a2010-08-24 00:50:04 +00002142 } else
Douglas Gregorf7180622011-08-03 15:48:04 +00002143 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
Sebastian Redld7dce0a2010-08-24 00:50:04 +00002144 break;
2145 }
2146
Sebastian Redl539c5062010-08-18 23:57:32 +00002147 case REDECLS_UPDATE_LATEST: {
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002148 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
Douglas Gregorf7180622011-08-03 15:48:04 +00002149 for (unsigned i = 0, e = Record.size(); i < e; /* in loop */) {
2150 DeclID First = ReadDeclID(F, Record, i);
2151 DeclID Latest = ReadDeclID(F, Record, i);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002152 FirstLatestDeclIDs[First] = Latest;
2153 }
2154 break;
2155 }
2156
Sebastian Redl539c5062010-08-18 23:57:32 +00002157 case LANGUAGE_OPTIONS:
Douglas Gregorce3a8292010-07-27 00:27:13 +00002158 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor55abb232009-04-10 20:39:37 +00002159 return IgnorePCH;
2160 break;
Douglas Gregorbfbde532009-04-10 21:16:55 +00002161
Sebastian Redl539c5062010-08-18 23:57:32 +00002162 case IDENTIFIER_TABLE:
Sebastian Redl393f8b72010-07-19 20:52:06 +00002163 F.IdentifierTableData = BlobStart;
Douglas Gregor0e149972009-04-25 19:10:14 +00002164 if (Record[0]) {
Sebastian Redl393f8b72010-07-19 20:52:06 +00002165 F.IdentifierLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002166 = ASTIdentifierLookupTable::Create(
Sebastian Redl393f8b72010-07-19 20:52:06 +00002167 (const unsigned char *)F.IdentifierTableData + Record[0],
2168 (const unsigned char *)F.IdentifierTableData,
Sebastian Redl2c373b92010-10-05 15:59:54 +00002169 ASTIdentifierLookupTrait(*this, F));
Douglas Gregor49bf76b2011-07-21 18:46:38 +00002170 if (PP) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002171 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor49bf76b2011-07-21 18:46:38 +00002172 PP->getHeaderSearchInfo().SetExternalLookup(this);
2173 }
Douglas Gregor0e149972009-04-25 19:10:14 +00002174 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002175 break;
2176
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002177 case IDENTIFIER_OFFSET: {
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002178 if (F.LocalNumIdentifiers != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002179 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002180 return Failure;
2181 }
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002182 F.IdentifierOffsets = (const uint32_t *)BlobStart;
2183 F.LocalNumIdentifiers = Record[0];
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002184 unsigned LocalBaseIdentifierID = Record[1];
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00002185 F.BaseIdentifierID = getTotalNumIdentifiers();
Douglas Gregor19d26352011-07-20 00:59:32 +00002186
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002187 if (F.LocalNumIdentifiers > 0) {
2188 // Introduce the global -> local mapping for identifiers within this
2189 // module.
2190 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
2191 &F));
2192
2193 // Introduce the local -> global mapping for identifiers within this
2194 // module.
2195 F.IdentifierRemap.insert(
2196 std::make_pair(LocalBaseIdentifierID,
2197 F.BaseIdentifierID - LocalBaseIdentifierID));
2198
2199 IdentifiersLoaded.resize(IdentifiersLoaded.size()
2200 + F.LocalNumIdentifiers);
2201 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002202 break;
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002203 }
2204
Sebastian Redl539c5062010-08-18 23:57:32 +00002205 case EXTERNAL_DEFINITIONS:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002206 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2207 ExternalDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002208 break;
Douglas Gregor08f01292009-04-17 22:13:46 +00002209
Sebastian Redl539c5062010-08-18 23:57:32 +00002210 case SPECIAL_TYPES:
Douglas Gregor903b7e92011-07-22 00:38:23 +00002211 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2212 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
Douglas Gregor652d82a2009-04-18 05:55:16 +00002213 break;
2214
Sebastian Redl539c5062010-08-18 23:57:32 +00002215 case STATISTICS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002216 TotalNumStatements += Record[0];
2217 TotalNumMacros += Record[1];
2218 TotalLexicalDeclContexts += Record[2];
2219 TotalVisibleDeclContexts += Record[3];
Douglas Gregor08f01292009-04-17 22:13:46 +00002220 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00002221
Sebastian Redl539c5062010-08-18 23:57:32 +00002222 case UNUSED_FILESCOPED_DECLS:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002223 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2224 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
Tanya Lattner90073802010-02-12 00:07:30 +00002225 break;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00002226
Alexis Hunt27a761d2011-05-04 23:29:54 +00002227 case DELEGATING_CTORS:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002228 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2229 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
Alexis Hunt27a761d2011-05-04 23:29:54 +00002230 break;
2231
Sebastian Redl539c5062010-08-18 23:57:32 +00002232 case WEAK_UNDECLARED_IDENTIFIERS:
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00002233 if (Record.size() % 4 != 0) {
2234 Error("invalid weak identifiers record");
2235 return Failure;
2236 }
2237
2238 // FIXME: Ignore weak undeclared identifiers from non-original PCH
2239 // files. This isn't the way to do it :)
2240 WeakUndeclaredIdentifiers.clear();
2241
2242 // Translate the weak, undeclared identifiers into global IDs.
2243 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2244 WeakUndeclaredIdentifiers.push_back(
2245 getGlobalIdentifierID(F, Record[I++]));
2246 WeakUndeclaredIdentifiers.push_back(
2247 getGlobalIdentifierID(F, Record[I++]));
2248 WeakUndeclaredIdentifiers.push_back(
2249 ReadSourceLocation(F, Record, I).getRawEncoding());
2250 WeakUndeclaredIdentifiers.push_back(Record[I++]);
2251 }
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002252 break;
2253
Sebastian Redl539c5062010-08-18 23:57:32 +00002254 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002255 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2256 LocallyScopedExternalDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002257 break;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002258
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002259 case SELECTOR_OFFSETS: {
Sebastian Redla19a67f2010-08-03 21:58:15 +00002260 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redlada023c2010-08-04 20:40:17 +00002261 F.LocalNumSelectors = Record[0];
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002262 unsigned LocalBaseSelectorID = Record[1];
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00002263 F.BaseSelectorID = getTotalNumSelectors();
Douglas Gregor2262d282011-07-20 01:10:58 +00002264
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002265 if (F.LocalNumSelectors > 0) {
2266 // Introduce the global -> local mapping for selectors within this
2267 // module.
2268 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2269
2270 // Introduce the local -> global mapping for selectors within this
2271 // module.
2272 F.SelectorRemap.insert(std::make_pair(LocalBaseSelectorID,
2273 F.BaseSelectorID - LocalBaseSelectorID));
Douglas Gregor95c13f52009-04-25 17:48:32 +00002274
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002275 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
2276 }
2277 break;
2278 }
2279
Sebastian Redl539c5062010-08-18 23:57:32 +00002280 case METHOD_POOL:
Sebastian Redlada023c2010-08-04 20:40:17 +00002281 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002282 if (Record[0])
Sebastian Redlada023c2010-08-04 20:40:17 +00002283 F.SelectorLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002284 = ASTSelectorLookupTable::Create(
Sebastian Redlada023c2010-08-04 20:40:17 +00002285 F.SelectorLookupTableData + Record[0],
2286 F.SelectorLookupTableData,
Douglas Gregor7fb09192011-07-21 22:35:25 +00002287 ASTSelectorLookupTrait(*this, F));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002288 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorc78d3462009-04-24 21:10:55 +00002289 break;
Douglas Gregoreda6a892009-04-26 00:07:37 +00002290
Sebastian Redl96371b42010-09-22 00:42:30 +00002291 case REFERENCED_SELECTOR_POOL:
Douglas Gregor3f8f04f2011-07-28 14:41:43 +00002292 if (!Record.empty()) {
2293 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2294 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2295 Record[Idx++]));
2296 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2297 getRawEncoding());
2298 }
2299 }
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002300 break;
2301
Sebastian Redl539c5062010-08-18 23:57:32 +00002302 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002303 if (!Record.empty() && Listener)
2304 Listener->ReadCounter(Record[0]);
Douglas Gregoreda6a892009-04-26 00:07:37 +00002305 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00002306
Douglas Gregor925296b2011-07-19 16:10:42 +00002307 case SOURCE_LOCATION_OFFSETS: {
2308 F.SLocEntryOffsets = (const uint32_t *)BlobStart;
Sebastian Redlb293a452010-07-20 21:20:32 +00002309 F.LocalNumSLocEntries = Record[0];
Douglas Gregor925296b2011-07-19 16:10:42 +00002310 llvm::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
2311 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries, Record[1]);
2312 // Make our entry in the range map. BaseID is negative and growing, so
2313 // we invert it. Because we invert it, though, we need the other end of
2314 // the range.
2315 unsigned RangeStart =
2316 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2317 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2318 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2319
2320 // Initialize the remapping table.
2321 // Invalid stays invalid.
2322 F.SLocRemap.insert(std::make_pair(0U, 0));
2323 // This module. Base was 2 when being compiled.
2324 F.SLocRemap.insert(std::make_pair(2U,
2325 static_cast<int>(F.SLocEntryBaseOffset - 2)));
Douglas Gregor49bf76b2011-07-21 18:46:38 +00002326
2327 TotalNumSLocEntries += F.LocalNumSLocEntries;
Douglas Gregor925296b2011-07-19 16:10:42 +00002328 break;
2329 }
2330
Douglas Gregor5a1797c2011-08-01 16:01:55 +00002331 case MODULE_OFFSET_MAP: {
Douglas Gregor925296b2011-07-19 16:10:42 +00002332 // Additional remapping information.
2333 const unsigned char *Data = (const unsigned char*)BlobStart;
2334 const unsigned char *DataEnd = Data + BlobLen;
Douglas Gregor00659902011-08-02 10:56:51 +00002335
2336 // Continuous range maps we may be updating in our module.
2337 ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap);
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002338 ContinuousRangeMap<uint32_t, int, 2>::Builder
2339 IdentifierRemap(F.IdentifierRemap);
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002340 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002341 PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2342 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregora863b4b2011-08-04 16:36:56 +00002343 MacroDefinitionRemap(F.MacroDefinitionRemap);
2344 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002345 SelectorRemap(F.SelectorRemap);
Douglas Gregorf7180622011-08-03 15:48:04 +00002346 ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap);
Douglas Gregor5204bde2011-08-02 16:26:37 +00002347 ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap);
2348
Douglas Gregor925296b2011-07-19 16:10:42 +00002349 while(Data < DataEnd) {
Douglas Gregor925296b2011-07-19 16:10:42 +00002350 uint16_t Len = io::ReadUnalignedLE16(Data);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002351 StringRef Name = StringRef((const char*)Data, Len);
Douglas Gregor00659902011-08-02 10:56:51 +00002352 Data += Len;
Jonathan D. Turnerb2b08232011-07-26 18:21:30 +00002353 Module *OM = ModuleMgr.lookup(Name);
Douglas Gregor925296b2011-07-19 16:10:42 +00002354 if (!OM) {
2355 Error("SourceLocation remap refers to unknown module");
2356 return Failure;
2357 }
Douglas Gregor00659902011-08-02 10:56:51 +00002358
2359 uint32_t SLocOffset = io::ReadUnalignedLE32(Data);
2360 uint32_t IdentifierIDOffset = io::ReadUnalignedLE32(Data);
2361 uint32_t PreprocessedEntityIDOffset = io::ReadUnalignedLE32(Data);
2362 uint32_t MacroDefinitionIDOffset = io::ReadUnalignedLE32(Data);
2363 uint32_t SelectorIDOffset = io::ReadUnalignedLE32(Data);
2364 uint32_t DeclIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregor5204bde2011-08-02 16:26:37 +00002365 uint32_t TypeIndexOffset = io::ReadUnalignedLE32(Data);
Douglas Gregor00659902011-08-02 10:56:51 +00002366
2367 // Source location offset is mapped to OM->SLocEntryBaseOffset.
2368 SLocRemap.insert(std::make_pair(SLocOffset,
2369 static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset)));
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002370 IdentifierRemap.insert(
2371 std::make_pair(IdentifierIDOffset,
2372 OM->BaseIdentifierID - IdentifierIDOffset));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002373 PreprocessedEntityRemap.insert(
2374 std::make_pair(PreprocessedEntityIDOffset,
2375 OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset));
Douglas Gregora863b4b2011-08-04 16:36:56 +00002376 MacroDefinitionRemap.insert(
2377 std::make_pair(MacroDefinitionIDOffset,
2378 OM->BaseMacroDefinitionID - MacroDefinitionIDOffset));
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002379 SelectorRemap.insert(std::make_pair(SelectorIDOffset,
2380 OM->BaseSelectorID - SelectorIDOffset));
Douglas Gregorf7180622011-08-03 15:48:04 +00002381 DeclRemap.insert(std::make_pair(DeclIDOffset,
2382 OM->BaseDeclID - DeclIDOffset));
2383
Douglas Gregor5204bde2011-08-02 16:26:37 +00002384 TypeRemap.insert(std::make_pair(TypeIndexOffset,
Douglas Gregor3b65ed02011-08-02 18:32:54 +00002385 OM->BaseTypeIndex - TypeIndexOffset));
Douglas Gregor925296b2011-07-19 16:10:42 +00002386 }
2387 break;
2388 }
2389
Douglas Gregor925296b2011-07-19 16:10:42 +00002390 case SOURCE_MANAGER_LINE_TABLE:
2391 if (ParseLineTable(F, Record))
2392 return Failure;
Douglas Gregor258ae542009-04-27 06:38:32 +00002393 break;
2394
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00002395 case FILE_SOURCE_LOCATION_OFFSETS:
2396 F.SLocFileOffsets = (const uint32_t *)BlobStart;
2397 F.LocalNumSLocFileEntries = Record[0];
2398 break;
2399
Douglas Gregor925296b2011-07-19 16:10:42 +00002400 case SOURCE_LOCATION_PRELOADS: {
2401 // Need to transform from the local view (1-based IDs) to the global view,
2402 // which is based off F.SLocEntryBaseID.
2403 PreloadSLocEntries.reserve(PreloadSLocEntries.size() + Record.size());
2404 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2405 PreloadSLocEntries.push_back(int(Record[I] - 1) + F.SLocEntryBaseID);
Douglas Gregor258ae542009-04-27 06:38:32 +00002406 break;
Douglas Gregor925296b2011-07-19 16:10:42 +00002407 }
Douglas Gregorc5046832009-04-27 18:38:38 +00002408
Sebastian Redl539c5062010-08-18 23:57:32 +00002409 case STAT_CACHE: {
Douglas Gregor606c4ac2011-02-05 19:42:43 +00002410 if (!DisableStatCache) {
2411 ASTStatCache *MyStatCache =
2412 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
2413 (const unsigned char *)BlobStart,
2414 NumStatHits, NumStatMisses);
2415 FileMgr.addStatCache(MyStatCache);
2416 F.StatCache = MyStatCache;
2417 }
Douglas Gregorc5046832009-04-27 18:38:38 +00002418 break;
Douglas Gregord2eb58a2009-10-16 18:18:30 +00002419 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00002420
Sebastian Redl539c5062010-08-18 23:57:32 +00002421 case EXT_VECTOR_DECLS:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002422 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2423 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002424 break;
2425
Sebastian Redl539c5062010-08-18 23:57:32 +00002426 case VTABLE_USES:
Douglas Gregor4daf6a32011-07-28 19:11:31 +00002427 if (Record.size() % 3 != 0) {
2428 Error("Invalid VTABLE_USES record");
2429 return Failure;
2430 }
2431
Sebastian Redl08aca90252010-08-05 18:21:25 +00002432 // Later tables overwrite earlier ones.
Douglas Gregor4daf6a32011-07-28 19:11:31 +00002433 // FIXME: Modules will have some trouble with this. This is clearly not
2434 // the right way to do this.
Douglas Gregor7fb09192011-07-21 22:35:25 +00002435 VTableUses.clear();
Douglas Gregor4daf6a32011-07-28 19:11:31 +00002436
2437 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
2438 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
2439 VTableUses.push_back(
2440 ReadSourceLocation(F, Record, Idx).getRawEncoding());
2441 VTableUses.push_back(Record[Idx++]);
2442 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002443 break;
2444
Sebastian Redl539c5062010-08-18 23:57:32 +00002445 case DYNAMIC_CLASSES:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002446 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2447 DynamicClasses.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002448 break;
2449
Sebastian Redl539c5062010-08-18 23:57:32 +00002450 case PENDING_IMPLICIT_INSTANTIATIONS:
Douglas Gregorbbbc3672011-07-28 19:26:52 +00002451 if (PendingInstantiations.size() % 2 != 0) {
2452 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
2453 return Failure;
2454 }
2455
2456 // Later lists of pending instantiations overwrite earlier ones.
2457 // FIXME: This is most certainly wrong for modules.
2458 PendingInstantiations.clear();
2459 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2460 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
2461 PendingInstantiations.push_back(
2462 ReadSourceLocation(F, Record, I).getRawEncoding());
2463 }
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002464 break;
2465
Sebastian Redl539c5062010-08-18 23:57:32 +00002466 case SEMA_DECL_REFS:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002467 // Later tables overwrite earlier ones.
Douglas Gregor7fb09192011-07-21 22:35:25 +00002468 // FIXME: Modules will have some trouble with this.
2469 SemaDeclRefs.clear();
2470 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2471 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002472 break;
2473
Sebastian Redl539c5062010-08-18 23:57:32 +00002474 case ORIGINAL_FILE_NAME:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002475 // The primary AST will be the last to get here, so it will be the one
Sebastian Redlb293a452010-07-20 21:20:32 +00002476 // that's used.
Daniel Dunbar000c4ff2009-11-11 05:29:04 +00002477 ActualOriginalFileName.assign(BlobStart, BlobLen);
2478 OriginalFileName = ActualOriginalFileName;
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002479 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregor45fe0362009-05-12 01:31:05 +00002480 break;
Mike Stump11289f42009-09-09 15:08:12 +00002481
Douglas Gregora3b20262011-05-06 21:43:30 +00002482 case ORIGINAL_FILE_ID:
2483 OriginalFileID = FileID::get(Record[0]);
2484 break;
2485
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002486 case ORIGINAL_PCH_DIR:
2487 // The primary AST will be the last to get here, so it will be the one
2488 // that's used.
2489 OriginalDir.assign(BlobStart, BlobLen);
2490 break;
2491
Sebastian Redl539c5062010-08-18 23:57:32 +00002492 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek8bd09292010-02-12 23:31:14 +00002493 const std::string &CurBranch = getClangFullRepositoryVersion();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002494 StringRef ASTBranch(BlobStart, BlobLen);
2495 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002496 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregord54f3a12009-10-05 21:07:28 +00002497 return IgnorePCH;
2498 }
2499 break;
2500 }
Sebastian Redlfa061442010-07-21 20:07:32 +00002501
Douglas Gregor4a9c39a2011-07-21 00:47:40 +00002502 case MACRO_DEFINITION_OFFSETS: {
Sebastian Redlfa061442010-07-21 20:07:32 +00002503 F.MacroDefinitionOffsets = (const uint32_t *)BlobStart;
2504 F.NumPreallocatedPreprocessingEntities = Record[0];
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002505 unsigned LocalBasePreprocessedEntityID = Record[1];
2506 F.LocalNumMacroDefinitions = Record[2];
2507 unsigned LocalBaseMacroID = Record[3];
Douglas Gregora863b4b2011-08-04 16:36:56 +00002508
Douglas Gregor4a9c39a2011-07-21 00:47:40 +00002509 unsigned StartingID;
2510 if (PP) {
2511 if (!PP->getPreprocessingRecord())
2512 PP->createPreprocessingRecord(true);
2513 if (!PP->getPreprocessingRecord()->getExternalSource())
2514 PP->getPreprocessingRecord()->SetExternalSource(*this);
2515 StartingID
2516 = PP->getPreprocessingRecord()
2517 ->allocateLoadedEntities(F.NumPreallocatedPreprocessingEntities);
2518 } else {
2519 // FIXME: We'll eventually want to kill this path, since it assumes
2520 // a particular allocation strategy in the preprocessing record.
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002521 StartingID = getTotalNumPreprocessedEntities()
2522 - F.NumPreallocatedPreprocessingEntities;
Douglas Gregor4a9c39a2011-07-21 00:47:40 +00002523 }
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00002524 F.BaseMacroDefinitionID = getTotalNumMacroDefinitions();
2525 F.BasePreprocessedEntityID = StartingID;
Douglas Gregor4a9c39a2011-07-21 00:47:40 +00002526
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002527 if (F.NumPreallocatedPreprocessingEntities > 0) {
2528 // Introduce the global -> local mapping for preprocessed entities in
2529 // this module.
2530 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
2531
2532 // Introduce the local -> global mapping for preprocessed entities in
2533 // this module.
2534 F.PreprocessedEntityRemap.insert(
2535 std::make_pair(LocalBasePreprocessedEntityID,
2536 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
2537 }
2538
2539
Douglas Gregora863b4b2011-08-04 16:36:56 +00002540 if (F.LocalNumMacroDefinitions > 0) {
2541 // Introduce the global -> local mapping for macro definitions within
2542 // this module.
2543 GlobalMacroDefinitionMap.insert(
2544 std::make_pair(getTotalNumMacroDefinitions() + 1, &F));
2545
2546 // Introduce the local -> global mapping for macro definitions within
2547 // this module.
2548 F.MacroDefinitionRemap.insert(
2549 std::make_pair(LocalBaseMacroID,
2550 F.BaseMacroDefinitionID - LocalBaseMacroID));
2551
2552 MacroDefinitionsLoaded.resize(
Douglas Gregor270e0142011-07-20 01:29:15 +00002553 MacroDefinitionsLoaded.size() + F.LocalNumMacroDefinitions);
Douglas Gregora863b4b2011-08-04 16:36:56 +00002554 }
2555
Douglas Gregoraae92242010-03-19 21:51:54 +00002556 break;
Douglas Gregor4a9c39a2011-07-21 00:47:40 +00002557 }
2558
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002559 case DECL_UPDATE_OFFSETS: {
2560 if (Record.size() % 2 != 0) {
2561 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2562 return Failure;
2563 }
2564 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Douglas Gregorf7180622011-08-03 15:48:04 +00002565 DeclUpdateOffsets[getGlobalDeclID(F, Record[I])]
2566 .push_back(std::make_pair(&F, Record[I+1]));
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002567 break;
2568 }
2569
Sebastian Redl539c5062010-08-18 23:57:32 +00002570 case DECL_REPLACEMENTS: {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002571 if (Record.size() % 2 != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002572 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002573 return Failure;
2574 }
2575 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Douglas Gregorf7180622011-08-03 15:48:04 +00002576 ReplacedDecls[getGlobalDeclID(F, Record[I])]
2577 = std::make_pair(&F, Record[I+1]);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002578 break;
2579 }
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002580
2581 case CXX_BASE_SPECIFIER_OFFSETS: {
2582 if (F.LocalNumCXXBaseSpecifiers != 0) {
2583 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2584 return Failure;
2585 }
2586
2587 F.LocalNumCXXBaseSpecifiers = Record[0];
2588 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
Jonathan D. Turner3766fdb2011-07-21 21:15:19 +00002589 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002590 break;
2591 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002592
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002593 case DIAG_PRAGMA_MAPPINGS:
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002594 if (Record.size() % 2 != 0) {
2595 Error("invalid DIAG_USER_MAPPINGS block in AST file");
2596 return Failure;
2597 }
Douglas Gregor925296b2011-07-19 16:10:42 +00002598
2599 if (F.PragmaDiagMappings.empty())
2600 F.PragmaDiagMappings.swap(Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002601 else
Douglas Gregor925296b2011-07-19 16:10:42 +00002602 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
2603 Record.begin(), Record.end());
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002604 break;
Douglas Gregor09b69892011-02-10 17:09:37 +00002605
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002606 case CUDA_SPECIAL_DECL_REFS:
2607 // Later tables overwrite earlier ones.
Douglas Gregor7fb09192011-07-21 22:35:25 +00002608 // FIXME: Modules will have trouble with this.
2609 CUDASpecialDeclRefs.clear();
2610 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2611 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002612 break;
Douglas Gregor09b69892011-02-10 17:09:37 +00002613
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002614 case HEADER_SEARCH_TABLE: {
Douglas Gregor09b69892011-02-10 17:09:37 +00002615 F.HeaderFileInfoTableData = BlobStart;
2616 F.LocalNumHeaderFileInfos = Record[1];
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002617 F.HeaderFileFrameworkStrings = BlobStart + Record[2];
Douglas Gregor09b69892011-02-10 17:09:37 +00002618 if (Record[0]) {
2619 F.HeaderFileInfoTable
2620 = HeaderFileInfoLookupTable::Create(
2621 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002622 (const unsigned char *)F.HeaderFileInfoTableData,
Douglas Gregora3e41532011-07-28 20:55:49 +00002623 HeaderFileInfoTrait(*this, F,
2624 PP? &PP->getHeaderSearchInfo() : 0,
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002625 BlobStart + Record[2]));
Douglas Gregor09b69892011-02-10 17:09:37 +00002626 if (PP)
2627 PP->getHeaderSearchInfo().SetExternalSource(this);
2628 }
2629 break;
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002630 }
2631
Peter Collingbourne5df20e02011-02-15 19:46:30 +00002632 case FP_PRAGMA_OPTIONS:
2633 // Later tables overwrite earlier ones.
2634 FPPragmaOptions.swap(Record);
2635 break;
2636
2637 case OPENCL_EXTENSIONS:
2638 // Later tables overwrite earlier ones.
2639 OpenCLExtensions.swap(Record);
2640 break;
Alexis Hunt27a761d2011-05-04 23:29:54 +00002641
2642 case TENTATIVE_DEFINITIONS:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002643 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2644 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Alexis Hunt27a761d2011-05-04 23:29:54 +00002645 break;
Douglas Gregorc2fa1692011-06-28 16:20:02 +00002646
2647 case KNOWN_NAMESPACES:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002648 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2649 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregorc2fa1692011-06-28 16:20:02 +00002650 break;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002651 }
Sebastian Redl393f8b72010-07-19 20:52:06 +00002652 First = false;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002653 }
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002654 Error("premature end of bitstream in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00002655 return Failure;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002656}
2657
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00002658ASTReader::ASTReadResult ASTReader::validateFileEntries() {
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00002659 for (ModuleIterator I = ModuleMgr.begin(),
2660 E = ModuleMgr.end(); I != E; ++I) {
2661 Module *F = *I;
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00002662 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
2663
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00002664 for (unsigned i = 0, e = F->LocalNumSLocFileEntries; i != e; ++i) {
2665 SLocEntryCursor.JumpToBit(F->SLocFileOffsets[i]);
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00002666 unsigned Code = SLocEntryCursor.ReadCode();
2667 if (Code == llvm::bitc::END_BLOCK ||
2668 Code == llvm::bitc::ENTER_SUBBLOCK ||
2669 Code == llvm::bitc::DEFINE_ABBREV) {
2670 Error("incorrectly-formatted source location entry in AST file");
2671 return Failure;
2672 }
2673
2674 RecordData Record;
2675 const char *BlobStart;
2676 unsigned BlobLen;
2677 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
2678 default:
2679 Error("incorrectly-formatted source location entry in AST file");
2680 return Failure;
2681
2682 case SM_SLOC_FILE_ENTRY: {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002683 StringRef Filename(BlobStart, BlobLen);
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00002684 const FileEntry *File = getFileEntry(Filename);
2685
2686 if (File == 0) {
2687 std::string ErrorStr = "could not find file '";
2688 ErrorStr += Filename;
2689 ErrorStr += "' referenced by AST file";
2690 Error(ErrorStr.c_str());
2691 return IgnorePCH;
2692 }
2693
2694 if (Record.size() < 6) {
2695 Error("source location entry is incorrect");
2696 return Failure;
2697 }
2698
2699 // The stat info from the FileEntry came from the cached stat
2700 // info of the PCH, so we cannot trust it.
2701 struct stat StatBuf;
2702 if (::stat(File->getName(), &StatBuf) != 0) {
2703 StatBuf.st_size = File->getSize();
2704 StatBuf.st_mtime = File->getModificationTime();
2705 }
2706
2707 if (((off_t)Record[4] != StatBuf.st_size
2708#if !defined(LLVM_ON_WIN32)
2709 // In our regression testing, the Windows file system seems to
2710 // have inconsistent modification times that sometimes
2711 // erroneously trigger this error-handling path.
2712 || (time_t)Record[5] != StatBuf.st_mtime
2713#endif
2714 )) {
2715 Error(diag::err_fe_pch_file_modified, Filename);
2716 return IgnorePCH;
2717 }
2718
2719 break;
2720 }
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00002721 }
2722 }
2723 }
2724
2725 return Success;
2726}
2727
Sebastian Redl009e7f22010-10-05 16:15:19 +00002728ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
Douglas Gregora6895d82011-07-22 16:00:58 +00002729 ModuleKind Type) {
Douglas Gregordf0c1512011-08-18 04:12:04 +00002730 switch(ReadASTCore(FileName, Type, /*ImportedBy=*/0)) {
Sebastian Redl2abc0382010-07-16 20:41:52 +00002731 case Failure: return Failure;
2732 case IgnorePCH: return IgnorePCH;
2733 case Success: break;
2734 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002735
2736 // Here comes stuff that we only do once the entire chain is loaded.
2737
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00002738 if (!DisableValidation) {
2739 switch(validateFileEntries()) {
2740 case Failure: return Failure;
2741 case IgnorePCH: return IgnorePCH;
2742 case Success: break;
2743 }
2744 }
2745
Sebastian Redl96371b42010-09-22 00:42:30 +00002746 // Preload SLocEntries.
2747 for (unsigned I = 0, N = PreloadSLocEntries.size(); I != N; ++I) {
2748 ASTReadResult Result = ReadSLocEntryRecord(PreloadSLocEntries[I]);
2749 if (Result != Success)
Douglas Gregor925296b2011-07-19 16:10:42 +00002750 return Failure;
Sebastian Redl96371b42010-09-22 00:42:30 +00002751 }
Douglas Gregor49bf76b2011-07-21 18:46:38 +00002752 PreloadSLocEntries.clear();
2753
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002754 // Check the predefines buffers.
Douglas Gregor9125bd62011-07-27 16:30:06 +00002755 if (!DisableValidation && Type != MK_Module && CheckPredefinesBuffers())
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002756 return IgnorePCH;
2757
2758 if (PP) {
2759 // Initialization of keywords and pragmas occurs before the
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002760 // AST file is read, so there may be some identifiers that were
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002761 // loaded into the IdentifierTable before we intercepted the
2762 // creation of identifiers. Iterate through the list of known
2763 // identifiers and determine whether we have to establish
2764 // preprocessor definitions or top-level identifier declaration
2765 // chains for those identifiers.
2766 //
2767 // We copy the IdentifierInfo pointers to a small vector first,
2768 // since de-serializing declarations or macro definitions can add
2769 // new entries into the identifier table, invalidating the
2770 // iterators.
Douglas Gregor9125bd62011-07-27 16:30:06 +00002771 //
2772 // FIXME: We need a lazier way to load this information, e.g., by marking
2773 // the identifier data as 'dirty', so that it will be looked up in the
2774 // AST file(s) if it is uttered in the source. This could save us some
2775 // module load time.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002776 SmallVector<IdentifierInfo *, 128> Identifiers;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002777 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
2778 IdEnd = PP->getIdentifierTable().end();
2779 Id != IdEnd; ++Id)
2780 Identifiers.push_back(Id->second);
Sebastian Redlfa061442010-07-21 20:07:32 +00002781 // We need to search the tables in all files.
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00002782 for (ModuleIterator J = ModuleMgr.begin(),
2783 M = ModuleMgr.end(); J != M; ++J) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002784 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00002785 = (ASTIdentifierLookupTable *)(*J)->IdentifierLookupTable;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002786 // Not all AST files necessarily have identifier tables, only the useful
Sebastian Redl5c415f32010-07-22 17:01:13 +00002787 // ones.
2788 if (!IdTable)
2789 continue;
Sebastian Redlfa061442010-07-21 20:07:32 +00002790 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2791 IdentifierInfo *II = Identifiers[I];
2792 // Look in the on-disk hash tables for an entry for this identifier
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00002793 ASTIdentifierLookupTrait Info(*this, *(*J), II);
Sebastian Redlfa061442010-07-21 20:07:32 +00002794 std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength());
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002795 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
Sebastian Redlb293a452010-07-20 21:20:32 +00002796 if (Pos == IdTable->end())
2797 continue;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002798
Sebastian Redlb293a452010-07-20 21:20:32 +00002799 // Dereferencing the iterator has the effect of populating the
2800 // IdentifierInfo node with the various declarations it needs.
2801 (void)*Pos;
2802 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002803 }
2804 }
2805
2806 if (Context)
2807 InitializeContext(*Context);
2808
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002809 if (DeserializationListener)
2810 DeserializationListener->ReaderInitialized(this);
2811
Douglas Gregor936a5b42010-11-30 05:23:00 +00002812 // If this AST file is a precompiled preamble, then set the main file ID of
2813 // the source manager to the file source file from which the preamble was
2814 // built. This is the only valid way to use a precompiled preamble.
Douglas Gregora6895d82011-07-22 16:00:58 +00002815 if (Type == MK_Preamble) {
Douglas Gregora3b20262011-05-06 21:43:30 +00002816 if (OriginalFileID.isInvalid()) {
2817 SourceLocation Loc
2818 = SourceMgr.getLocation(FileMgr.getFile(getOriginalSourceFile()), 1, 1);
2819 if (Loc.isValid())
2820 OriginalFileID = SourceMgr.getDecomposedLoc(Loc).first;
Douglas Gregor936a5b42010-11-30 05:23:00 +00002821 }
Douglas Gregor925296b2011-07-19 16:10:42 +00002822 else {
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00002823 OriginalFileID = FileID::get(ModuleMgr.getPrimaryModule().SLocEntryBaseID
Douglas Gregor925296b2011-07-19 16:10:42 +00002824 + OriginalFileID.getOpaqueValue() - 1);
2825 }
2826
Douglas Gregora3b20262011-05-06 21:43:30 +00002827 if (!OriginalFileID.isInvalid())
2828 SourceMgr.SetPreambleFileID(OriginalFileID);
Douglas Gregor936a5b42010-11-30 05:23:00 +00002829 }
2830
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002831 return Success;
2832}
2833
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002834ASTReader::ASTReadResult ASTReader::ReadASTCore(StringRef FileName,
Douglas Gregordf0c1512011-08-18 04:12:04 +00002835 ModuleKind Type,
2836 Module *ImportedBy) {
Douglas Gregor4dd3e942011-08-19 02:29:29 +00002837 Module *M;
2838 bool NewModule;
2839 std::string ErrorStr;
2840 llvm::tie(M, NewModule) = ModuleMgr.addModule(FileName, Type, ImportedBy,
2841 ErrorStr);
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002842
Douglas Gregor4dd3e942011-08-19 02:29:29 +00002843 if (!M) {
2844 // We couldn't load the module.
2845 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
2846 + ErrorStr;
2847 Error(Msg);
2848 return Failure;
2849 }
2850
2851 if (!NewModule) {
2852 // We've already loaded this module.
2853 return Success;
2854 }
2855
2856 // FIXME: This seems rather a hack. Should CurrentDir be part of the
2857 // module?
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002858 if (FileName != "-") {
2859 CurrentDir = llvm::sys::path::parent_path(FileName);
2860 if (CurrentDir.empty()) CurrentDir = ".";
2861 }
2862
Douglas Gregor4dd3e942011-08-19 02:29:29 +00002863 Module &F = *M;
Sebastian Redl34522812010-07-16 17:50:48 +00002864 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002865 Stream.init(F.StreamFile);
Sebastian Redlfa061442010-07-21 20:07:32 +00002866 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Douglas Gregord32f0352011-07-22 06:10:01 +00002867
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002868 // Sniff for the signature.
2869 if (Stream.Read(8) != 'C' ||
2870 Stream.Read(8) != 'P' ||
2871 Stream.Read(8) != 'C' ||
2872 Stream.Read(8) != 'H') {
2873 Diag(diag::err_not_a_pch_file) << FileName;
2874 return Failure;
2875 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002876
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002877 while (!Stream.AtEndOfStream()) {
2878 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00002879
Douglas Gregor92863e42009-04-10 23:10:45 +00002880 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002881 Error("invalid record at top-level of AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002882 return Failure;
2883 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002884
2885 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregora868bbd2009-04-21 22:25:48 +00002886
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002887 // We only know the AST subblock ID.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002888 switch (BlockID) {
2889 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregor92863e42009-04-10 23:10:45 +00002890 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002891 Error("malformed BlockInfoBlock in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002892 return Failure;
2893 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002894 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00002895 case AST_BLOCK_ID:
Sebastian Redl3e31c722010-08-18 23:56:56 +00002896 switch (ReadASTBlock(F)) {
Douglas Gregor55abb232009-04-10 20:39:37 +00002897 case Success:
2898 break;
2899
2900 case Failure:
Douglas Gregor92863e42009-04-10 23:10:45 +00002901 return Failure;
Douglas Gregor55abb232009-04-10 20:39:37 +00002902
2903 case IgnorePCH:
Douglas Gregorbfbde532009-04-10 21:16:55 +00002904 // FIXME: We could consider reading through to the end of this
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002905 // AST block, skipping subblocks, to see if there are other
2906 // AST blocks elsewhere.
Douglas Gregor0bc12932009-04-27 21:28:04 +00002907
Douglas Gregor925296b2011-07-19 16:10:42 +00002908 // FIXME: We can't clear loaded slocentries anymore.
2909 //SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor0bc12932009-04-27 21:28:04 +00002910
2911 // Remove the stat cache.
Sebastian Redl34522812010-07-16 17:50:48 +00002912 if (F.StatCache)
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002913 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor0bc12932009-04-27 21:28:04 +00002914
Douglas Gregor92863e42009-04-10 23:10:45 +00002915 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00002916 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002917 break;
2918 default:
Douglas Gregor92863e42009-04-10 23:10:45 +00002919 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002920 Error("malformed block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002921 return Failure;
2922 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002923 break;
2924 }
Mike Stump11289f42009-09-09 15:08:12 +00002925 }
Douglas Gregord32f0352011-07-22 06:10:01 +00002926
Douglas Gregora6895d82011-07-22 16:00:58 +00002927 // Once read, set the Module bit base offset and update the size in
Douglas Gregord32f0352011-07-22 06:10:01 +00002928 // bits of all files we've seen.
2929 F.GlobalBitOffset = TotalModulesSizeInBits;
2930 TotalModulesSizeInBits += F.SizeInBits;
2931 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
Sebastian Redl2abc0382010-07-16 20:41:52 +00002932 return Success;
2933}
2934
Sebastian Redl2c499f62010-08-18 23:56:43 +00002935void ASTReader::setPreprocessor(Preprocessor &pp) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002936 PP = &pp;
Douglas Gregor4a9c39a2011-07-21 00:47:40 +00002937
2938 if (unsigned N = getTotalNumPreprocessedEntities()) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002939 if (!PP->getPreprocessingRecord())
Douglas Gregor998caea2011-05-06 16:33:08 +00002940 PP->createPreprocessingRecord(true);
Douglas Gregor4a9c39a2011-07-21 00:47:40 +00002941 PP->getPreprocessingRecord()->SetExternalSource(*this);
2942 PP->getPreprocessingRecord()->allocateLoadedEntities(N);
Douglas Gregoraae92242010-03-19 21:51:54 +00002943 }
Douglas Gregor4a9c39a2011-07-21 00:47:40 +00002944
2945 PP->getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor49bf76b2011-07-21 18:46:38 +00002946 PP->getHeaderSearchInfo().SetExternalSource(this);
Douglas Gregoraae92242010-03-19 21:51:54 +00002947}
2948
Sebastian Redl2c499f62010-08-18 23:56:43 +00002949void ASTReader::InitializeContext(ASTContext &Ctx) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002950 Context = &Ctx;
2951 assert(Context && "Passed null context!");
2952
2953 assert(PP && "Forgot to set Preprocessor ?");
2954 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor9882a5a2010-01-04 19:18:44 +00002955 PP->setExternalSource(this);
Douglas Gregor09b69892011-02-10 17:09:37 +00002956
Douglas Gregoraa433012010-10-01 01:18:02 +00002957 // If we have an update block for the TU waiting, we have to add it before
2958 // deserializing the decl.
Douglas Gregordab42432011-08-12 00:15:20 +00002959 TranslationUnitDecl *TU = Ctx.getTranslationUnitDecl();
Douglas Gregoraa433012010-10-01 01:18:02 +00002960 DeclContextOffsetsMap::iterator DCU = DeclContextOffsets.find(0);
2961 if (DCU != DeclContextOffsets.end()) {
2962 // Insertion could invalidate map, so grab vector.
2963 DeclContextInfos T;
2964 T.swap(DCU->second);
2965 DeclContextOffsets.erase(DCU);
Douglas Gregordab42432011-08-12 00:15:20 +00002966 DeclContextOffsets[TU].swap(T);
Douglas Gregoraa433012010-10-01 01:18:02 +00002967 }
Douglas Gregordab42432011-08-12 00:15:20 +00002968
2969 // If there's a listener, notify them that we "read" the translation unit.
2970 if (DeserializationListener)
2971 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID, TU);
Douglas Gregoraa433012010-10-01 01:18:02 +00002972
Douglas Gregordab42432011-08-12 00:15:20 +00002973 // Make sure we load the declaration update records for the translation unit,
2974 // if there are any.
2975 loadDeclUpdateRecords(PREDEF_DECL_TRANSLATION_UNIT_ID, TU);
2976
2977 // Note that the translation unit has external lexical and visible storage.
2978 TU->setHasExternalLexicalStorage(true);
2979 TU->setHasExternalVisibleStorage(true);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002980
Douglas Gregoraa8a8272011-08-11 22:18:49 +00002981 // FIXME: Find a better way to deal with collisions between these
2982 // built-in types. Right now, we just ignore the problem.
2983
2984 // Load the special types.
Jonathan D. Turnerf07f1312011-08-05 23:07:10 +00002985 if (Context->getBuiltinVaListType().isNull()) {
Jonathan D. Turnerf07f1312011-08-05 23:07:10 +00002986 Context->setBuiltinVaListType(
2987 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
Douglas Gregoraa8a8272011-08-11 22:18:49 +00002988 }
2989
Douglas Gregoraa8a8272011-08-11 22:18:49 +00002990 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL]) {
2991 if (Context->ObjCProtoType.isNull())
Douglas Gregor09c4aa82011-08-11 22:04:35 +00002992 Context->ObjCProtoType = GetType(Proto);
Douglas Gregoraa8a8272011-08-11 22:18:49 +00002993 }
2994
Douglas Gregoraa8a8272011-08-11 22:18:49 +00002995 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
2996 if (!Context->CFConstantStringTypeDecl)
Jonathan D. Turnerf07f1312011-08-05 23:07:10 +00002997 Context->setCFConstantStringType(GetType(String));
Douglas Gregoraa8a8272011-08-11 22:18:49 +00002998 }
2999
3000 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
3001 QualType FileType = GetType(File);
3002 if (FileType.isNull()) {
3003 Error("FILE type is NULL");
3004 return;
3005 }
3006
3007 if (!Context->FILEDecl) {
Jonathan D. Turnerf07f1312011-08-05 23:07:10 +00003008 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
3009 Context->setFILEDecl(Typedef->getDecl());
3010 else {
3011 const TagType *Tag = FileType->getAs<TagType>();
3012 if (!Tag) {
3013 Error("Invalid FILE type in AST file");
3014 return;
3015 }
3016 Context->setFILEDecl(Tag->getDecl());
3017 }
Douglas Gregor27821ce2009-07-07 16:35:42 +00003018 }
Douglas Gregoraa8a8272011-08-11 22:18:49 +00003019 }
3020
3021 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
3022 QualType Jmp_bufType = GetType(Jmp_buf);
3023 if (Jmp_bufType.isNull()) {
3024 Error("jmp_buf type is NULL");
3025 return;
3026 }
3027
3028 if (!Context->jmp_bufDecl) {
Jonathan D. Turnerf07f1312011-08-05 23:07:10 +00003029 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
3030 Context->setjmp_bufDecl(Typedef->getDecl());
3031 else {
3032 const TagType *Tag = Jmp_bufType->getAs<TagType>();
3033 if (!Tag) {
3034 Error("Invalid jmp_buf type in AST file");
3035 return;
3036 }
3037 Context->setjmp_bufDecl(Tag->getDecl());
3038 }
Mike Stumpa4de80b2009-07-28 02:25:19 +00003039 }
Douglas Gregoraa8a8272011-08-11 22:18:49 +00003040 }
3041
3042 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
3043 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
3044 if (Sigjmp_bufType.isNull()) {
3045 Error("sigjmp_buf type is NULL");
3046 return;
3047 }
3048
3049 if (!Context->sigjmp_bufDecl) {
Jonathan D. Turnerf07f1312011-08-05 23:07:10 +00003050 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
3051 Context->setsigjmp_bufDecl(Typedef->getDecl());
3052 else {
3053 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
3054 assert(Tag && "Invalid sigjmp_buf type in AST file");
3055 Context->setsigjmp_bufDecl(Tag->getDecl());
3056 }
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003057 }
Jonathan D. Turnerf07f1312011-08-05 23:07:10 +00003058 }
Richard Smith02e85f32011-04-14 22:09:26 +00003059
Douglas Gregoraa8a8272011-08-11 22:18:49 +00003060 if (unsigned ObjCIdRedef
3061 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
3062 if (Context->ObjCIdRedefinitionType.isNull())
3063 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
3064 }
3065
3066 if (unsigned ObjCClassRedef
3067 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
3068 if (Context->ObjCClassRedefinitionType.isNull())
3069 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
3070 }
3071
3072 if (unsigned ObjCSelRedef
3073 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
3074 if (Context->ObjCSelRedefinitionType.isNull())
3075 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
3076 }
3077
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00003078 ReadPragmaDiagnosticMappings(Context->getDiagnostics());
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00003079
3080 // If there were any CUDA special declarations, deserialize them.
3081 if (!CUDASpecialDeclRefs.empty()) {
3082 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
3083 Context->setcudaConfigureCallDecl(
3084 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
3085 }
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003086}
3087
Douglas Gregor45fe0362009-05-12 01:31:05 +00003088/// \brief Retrieve the name of the original source file name
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003089/// directly from the AST file, without actually loading the AST
Douglas Gregor45fe0362009-05-12 01:31:05 +00003090/// file.
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003091std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +00003092 FileManager &FileMgr,
Daniel Dunbar3b951482009-12-03 09:13:06 +00003093 Diagnostic &Diags) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003094 // Open the AST file.
Douglas Gregor45fe0362009-05-12 01:31:05 +00003095 std::string ErrStr;
3096 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner5159f612010-11-23 08:35:12 +00003097 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregor45fe0362009-05-12 01:31:05 +00003098 if (!Buffer) {
Daniel Dunbar3b951482009-12-03 09:13:06 +00003099 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregor45fe0362009-05-12 01:31:05 +00003100 return std::string();
3101 }
3102
3103 // Initialize the stream
3104 llvm::BitstreamReader StreamFile;
3105 llvm::BitstreamCursor Stream;
Mike Stump11289f42009-09-09 15:08:12 +00003106 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregor45fe0362009-05-12 01:31:05 +00003107 (const unsigned char *)Buffer->getBufferEnd());
3108 Stream.init(StreamFile);
3109
3110 // Sniff for the signature.
3111 if (Stream.Read(8) != 'C' ||
3112 Stream.Read(8) != 'P' ||
3113 Stream.Read(8) != 'C' ||
3114 Stream.Read(8) != 'H') {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003115 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00003116 return std::string();
3117 }
3118
3119 RecordData Record;
3120 while (!Stream.AtEndOfStream()) {
3121 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00003122
Douglas Gregor45fe0362009-05-12 01:31:05 +00003123 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3124 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump11289f42009-09-09 15:08:12 +00003125
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003126 // We only know the AST subblock ID.
Douglas Gregor45fe0362009-05-12 01:31:05 +00003127 switch (BlockID) {
Sebastian Redl539c5062010-08-18 23:57:32 +00003128 case AST_BLOCK_ID:
3129 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003130 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00003131 return std::string();
3132 }
3133 break;
Mike Stump11289f42009-09-09 15:08:12 +00003134
Douglas Gregor45fe0362009-05-12 01:31:05 +00003135 default:
3136 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003137 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00003138 return std::string();
3139 }
3140 break;
3141 }
3142 continue;
3143 }
3144
3145 if (Code == llvm::bitc::END_BLOCK) {
3146 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003147 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00003148 return std::string();
3149 }
3150 continue;
3151 }
3152
3153 if (Code == llvm::bitc::DEFINE_ABBREV) {
3154 Stream.ReadAbbrevRecord();
3155 continue;
3156 }
3157
3158 Record.clear();
3159 const char *BlobStart = 0;
3160 unsigned BlobLen = 0;
Mike Stump11289f42009-09-09 15:08:12 +00003161 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl539c5062010-08-18 23:57:32 +00003162 == ORIGINAL_FILE_NAME)
Douglas Gregor45fe0362009-05-12 01:31:05 +00003163 return std::string(BlobStart, BlobLen);
Mike Stump11289f42009-09-09 15:08:12 +00003164 }
Douglas Gregor45fe0362009-05-12 01:31:05 +00003165
3166 return std::string();
3167}
3168
Douglas Gregor55abb232009-04-10 20:39:37 +00003169/// \brief Parse the record that corresponds to a LangOptions data
3170/// structure.
3171///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003172/// This routine parses the language options from the AST file and then gives
3173/// them to the AST listener if one is set.
Douglas Gregor55abb232009-04-10 20:39:37 +00003174///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003175/// \returns true if the listener deems the file unacceptable, false otherwise.
Sebastian Redl2c499f62010-08-18 23:56:43 +00003176bool ASTReader::ParseLanguageOptions(
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003177 const SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003178 if (Listener) {
3179 LangOptions LangOpts;
Mike Stump11289f42009-09-09 15:08:12 +00003180
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003181 #define PARSE_LANGOPT(Option) \
3182 LangOpts.Option = Record[Idx]; \
3183 ++Idx
Mike Stump11289f42009-09-09 15:08:12 +00003184
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003185 unsigned Idx = 0;
3186 PARSE_LANGOPT(Trigraphs);
3187 PARSE_LANGOPT(BCPLComment);
3188 PARSE_LANGOPT(DollarIdents);
3189 PARSE_LANGOPT(AsmPreprocessor);
3190 PARSE_LANGOPT(GNUMode);
Chandler Carruthe03aa552010-04-17 20:17:31 +00003191 PARSE_LANGOPT(GNUKeywords);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003192 PARSE_LANGOPT(ImplicitInt);
3193 PARSE_LANGOPT(Digraphs);
3194 PARSE_LANGOPT(HexFloats);
3195 PARSE_LANGOPT(C99);
Peter Collingbournea686b5f2011-04-15 00:35:23 +00003196 PARSE_LANGOPT(C1X);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003197 PARSE_LANGOPT(Microsoft);
3198 PARSE_LANGOPT(CPlusPlus);
3199 PARSE_LANGOPT(CPlusPlus0x);
3200 PARSE_LANGOPT(CXXOperatorNames);
3201 PARSE_LANGOPT(ObjC1);
3202 PARSE_LANGOPT(ObjC2);
3203 PARSE_LANGOPT(ObjCNonFragileABI);
Fariborz Jahanian45878032010-02-09 19:31:38 +00003204 PARSE_LANGOPT(ObjCNonFragileABI2);
Fariborz Jahanian13f3b2f2011-01-07 18:59:25 +00003205 PARSE_LANGOPT(AppleKext);
Ted Kremenek1d56c9e2010-12-23 21:35:43 +00003206 PARSE_LANGOPT(ObjCDefaultSynthProperties);
Douglas Gregora860e6a2011-06-14 23:20:43 +00003207 PARSE_LANGOPT(ObjCInferRelatedResultType);
Fariborz Jahanian62c56022010-04-22 21:01:59 +00003208 PARSE_LANGOPT(NoConstantCFStrings);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003209 PARSE_LANGOPT(PascalStrings);
3210 PARSE_LANGOPT(WritableStrings);
3211 PARSE_LANGOPT(LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +00003212 PARSE_LANGOPT(AltiVec);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003213 PARSE_LANGOPT(Exceptions);
Anders Carlssonce8dd3a2011-02-19 23:53:54 +00003214 PARSE_LANGOPT(ObjCExceptions);
Anders Carlsson6bbd2682011-02-23 03:04:54 +00003215 PARSE_LANGOPT(CXXExceptions);
3216 PARSE_LANGOPT(SjLjExceptions);
Douglas Gregordbe39272011-02-01 15:15:22 +00003217 PARSE_LANGOPT(MSBitfields);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003218 PARSE_LANGOPT(NeXTRuntime);
3219 PARSE_LANGOPT(Freestanding);
3220 PARSE_LANGOPT(NoBuiltin);
3221 PARSE_LANGOPT(ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +00003222 PARSE_LANGOPT(POSIXThreads);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003223 PARSE_LANGOPT(Blocks);
3224 PARSE_LANGOPT(EmitAllDecls);
3225 PARSE_LANGOPT(MathErrno);
Chris Lattner51924e512010-06-26 21:25:03 +00003226 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
3227 Record[Idx++]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003228 PARSE_LANGOPT(HeinousExtensions);
3229 PARSE_LANGOPT(Optimize);
3230 PARSE_LANGOPT(OptimizeSize);
3231 PARSE_LANGOPT(Static);
3232 PARSE_LANGOPT(PICLevel);
3233 PARSE_LANGOPT(GNUInline);
3234 PARSE_LANGOPT(NoInline);
Chandler Carruth7ffce732011-04-23 20:05:38 +00003235 PARSE_LANGOPT(Deprecated);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003236 PARSE_LANGOPT(AccessControl);
3237 PARSE_LANGOPT(CharIsSigned);
John Thompsoned4e2952009-11-05 20:14:16 +00003238 PARSE_LANGOPT(ShortWChar);
Argyrios Kyrtzidisa88942a2011-01-15 02:56:16 +00003239 PARSE_LANGOPT(ShortEnums);
Chris Lattner51924e512010-06-26 21:25:03 +00003240 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
John McCall457a04e2010-10-22 21:05:15 +00003241 LangOpts.setVisibilityMode((Visibility)Record[Idx++]);
Daniel Dunbar143021e2009-09-21 04:16:19 +00003242 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
Chris Lattner51924e512010-06-26 21:25:03 +00003243 Record[Idx++]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003244 PARSE_LANGOPT(InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +00003245 PARSE_LANGOPT(OpenCL);
Peter Collingbourne546d0792010-12-01 19:14:57 +00003246 PARSE_LANGOPT(CUDA);
Mike Stumpd9546382009-12-12 01:27:46 +00003247 PARSE_LANGOPT(CatchUndefined);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003248 PARSE_LANGOPT(DefaultFPContract);
Roman Divackydc1f68d2011-03-01 17:36:40 +00003249 PARSE_LANGOPT(ElideConstructors);
3250 PARSE_LANGOPT(SpellChecking);
Roman Divacky65b88cd2011-03-01 17:40:53 +00003251 PARSE_LANGOPT(MRTD);
John McCall31168b02011-06-15 23:02:42 +00003252 PARSE_LANGOPT(ObjCAutoRefCount);
Douglas Gregor49b236a2011-08-04 15:46:00 +00003253 PARSE_LANGOPT(ObjCInferRelatedReturnType);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003254 #undef PARSE_LANGOPT
Douglas Gregor55abb232009-04-10 20:39:37 +00003255
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003256 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor55abb232009-04-10 20:39:37 +00003257 }
Douglas Gregor55abb232009-04-10 20:39:37 +00003258
3259 return false;
3260}
3261
Sebastian Redl2c499f62010-08-18 23:56:43 +00003262void ASTReader::ReadPreprocessedEntities() {
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00003263 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
3264 Module &F = *(*I);
Douglas Gregor92a96f52011-02-08 21:58:10 +00003265 if (!F.PreprocessorDetailCursor.getBitStreamReader())
3266 continue;
3267
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00003268 SavedStreamPosition SavedPosition(F.PreprocessorDetailCursor);
Douglas Gregor92a96f52011-02-08 21:58:10 +00003269 F.PreprocessorDetailCursor.JumpToBit(F.PreprocessorDetailStartOffset);
3270 while (LoadPreprocessedEntity(F)) { }
3271 }
Douglas Gregoraae92242010-03-19 21:51:54 +00003272}
3273
Douglas Gregor46c50012011-02-11 19:46:30 +00003274PreprocessedEntity *ASTReader::ReadPreprocessedEntityAtOffset(uint64_t Offset) {
Douglas Gregord32f0352011-07-22 06:10:01 +00003275 RecordLocation Loc = getLocalBitOffset(Offset);
Douglas Gregorf88e35b2010-11-30 06:16:57 +00003276
Douglas Gregor92a96f52011-02-08 21:58:10 +00003277 // Keep track of where we are in the stream, then jump back there
3278 // after reading this entity.
Douglas Gregord32f0352011-07-22 06:10:01 +00003279 SavedStreamPosition SavedPosition(Loc.F->PreprocessorDetailCursor);
3280 Loc.F->PreprocessorDetailCursor.JumpToBit(Loc.Offset);
3281 return LoadPreprocessedEntity(*Loc.F);
Douglas Gregorf88e35b2010-11-30 06:16:57 +00003282}
3283
Douglas Gregor09b69892011-02-10 17:09:37 +00003284HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00003285 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
3286 Module &F = *(*I);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00003287
Douglas Gregora3e41532011-07-28 20:55:49 +00003288 HeaderFileInfoTrait Trait(*this, F, &PP->getHeaderSearchInfo(),
Douglas Gregor4b123cb2011-07-28 04:50:02 +00003289 F.HeaderFileFrameworkStrings,
3290 FE->getName());
3291
Douglas Gregor09b69892011-02-10 17:09:37 +00003292 HeaderFileInfoLookupTable *Table
3293 = static_cast<HeaderFileInfoLookupTable *>(F.HeaderFileInfoTable);
3294 if (!Table)
3295 continue;
3296
3297 // Look in the on-disk hash table for an entry for this file name.
3298 HeaderFileInfoLookupTable::iterator Pos = Table->find(FE->getName(),
3299 &Trait);
3300 if (Pos == Table->end())
3301 continue;
3302
3303 HeaderFileInfo HFI = *Pos;
3304 if (Listener)
3305 Listener->ReadHeaderFileInfo(HFI, FE->getUID());
3306
3307 return HFI;
3308 }
3309
3310 return HeaderFileInfo();
3311}
3312
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00003313void ASTReader::ReadPragmaDiagnosticMappings(Diagnostic &Diag) {
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00003314 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
3315 Module &F = *(*I);
Douglas Gregor925296b2011-07-19 16:10:42 +00003316 unsigned Idx = 0;
3317 while (Idx < F.PragmaDiagMappings.size()) {
3318 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
3319 while (1) {
3320 assert(Idx < F.PragmaDiagMappings.size() &&
3321 "Invalid data, didn't find '-1' marking end of diag/map pairs");
3322 if (Idx >= F.PragmaDiagMappings.size()) {
3323 break; // Something is messed up but at least avoid infinite loop in
3324 // release build.
3325 }
3326 unsigned DiagID = F.PragmaDiagMappings[Idx++];
3327 if (DiagID == (unsigned)-1) {
3328 break; // no more diag/map pairs for this location.
3329 }
3330 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++];
3331 Diag.setDiagnosticMapping(DiagID, Map, Loc);
3332 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00003333 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00003334 }
3335}
3336
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003337/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redl2c499f62010-08-18 23:56:43 +00003338ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Douglas Gregor5204bde2011-08-02 16:26:37 +00003339 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
Jonathan D. Turner35005682011-07-20 21:31:32 +00003340 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
Douglas Gregor8ab4ea82011-07-29 00:21:44 +00003341 Module *M = I->second;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003342 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003343}
3344
3345/// \brief Read and return the type with the given index..
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003346///
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003347/// The index is the type ID, shifted and minus the number of predefs. This
3348/// routine actually reads the record corresponding to the type at the given
3349/// location. It is a helper routine for GetType, which deals with reading type
3350/// IDs.
Douglas Gregor903b7e92011-07-22 00:38:23 +00003351QualType ASTReader::readTypeRecord(unsigned Index) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003352 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003353 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00003354
Douglas Gregorfeb84b02009-04-14 21:18:50 +00003355 // Keep track of where we are in the stream, then jump back there
3356 // after reading this type.
Douglas Gregor12bfa382009-10-17 00:13:19 +00003357 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00003358
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003359 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redleaa4ade2010-08-11 18:52:41 +00003360
Douglas Gregor1342e842009-07-06 18:54:52 +00003361 // Note that we are loading a type record.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00003362 Deserializing AType(this);
Mike Stump11289f42009-09-09 15:08:12 +00003363
Douglas Gregor903b7e92011-07-22 00:38:23 +00003364 unsigned Idx = 0;
Sebastian Redl2c373b92010-10-05 15:59:54 +00003365 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003366 RecordData Record;
Douglas Gregor12bfa382009-10-17 00:13:19 +00003367 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl539c5062010-08-18 23:57:32 +00003368 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
3369 case TYPE_EXT_QUAL: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003370 if (Record.size() != 2) {
3371 Error("Incorrect encoding of extended qualifier type");
3372 return QualType();
3373 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003374 QualType Base = readType(*Loc.F, Record, Idx);
3375 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
John McCall8ccfcb52009-09-24 19:53:00 +00003376 return Context->getQualifiedType(Base, Quals);
Douglas Gregor455b8f42009-04-15 22:00:08 +00003377 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003378
Sebastian Redl539c5062010-08-18 23:57:32 +00003379 case TYPE_COMPLEX: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003380 if (Record.size() != 1) {
3381 Error("Incorrect encoding of complex type");
3382 return QualType();
3383 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003384 QualType ElemType = readType(*Loc.F, Record, Idx);
Chris Lattner8575daa2009-04-27 21:45:14 +00003385 return Context->getComplexType(ElemType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003386 }
3387
Sebastian Redl539c5062010-08-18 23:57:32 +00003388 case TYPE_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003389 if (Record.size() != 1) {
3390 Error("Incorrect encoding of pointer type");
3391 return QualType();
3392 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003393 QualType PointeeType = readType(*Loc.F, Record, Idx);
Chris Lattner8575daa2009-04-27 21:45:14 +00003394 return Context->getPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003395 }
3396
Sebastian Redl539c5062010-08-18 23:57:32 +00003397 case TYPE_BLOCK_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003398 if (Record.size() != 1) {
3399 Error("Incorrect encoding of block pointer type");
3400 return QualType();
3401 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003402 QualType PointeeType = readType(*Loc.F, Record, Idx);
Chris Lattner8575daa2009-04-27 21:45:14 +00003403 return Context->getBlockPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003404 }
3405
Sebastian Redl539c5062010-08-18 23:57:32 +00003406 case TYPE_LVALUE_REFERENCE: {
Richard Smith0f538462011-04-12 10:38:03 +00003407 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003408 Error("Incorrect encoding of lvalue reference type");
3409 return QualType();
3410 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003411 QualType PointeeType = readType(*Loc.F, Record, Idx);
Richard Smith0f538462011-04-12 10:38:03 +00003412 return Context->getLValueReferenceType(PointeeType, Record[1]);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003413 }
3414
Sebastian Redl539c5062010-08-18 23:57:32 +00003415 case TYPE_RVALUE_REFERENCE: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003416 if (Record.size() != 1) {
3417 Error("Incorrect encoding of rvalue reference type");
3418 return QualType();
3419 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003420 QualType PointeeType = readType(*Loc.F, Record, Idx);
Chris Lattner8575daa2009-04-27 21:45:14 +00003421 return Context->getRValueReferenceType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003422 }
3423
Sebastian Redl539c5062010-08-18 23:57:32 +00003424 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidisee776bc2010-07-02 11:55:15 +00003425 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003426 Error("Incorrect encoding of member pointer type");
3427 return QualType();
3428 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003429 QualType PointeeType = readType(*Loc.F, Record, Idx);
3430 QualType ClassType = readType(*Loc.F, Record, Idx);
Douglas Gregor0cdc8322010-12-10 17:03:06 +00003431 if (PointeeType.isNull() || ClassType.isNull())
3432 return QualType();
3433
Chris Lattner8575daa2009-04-27 21:45:14 +00003434 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003435 }
3436
Sebastian Redl539c5062010-08-18 23:57:32 +00003437 case TYPE_CONSTANT_ARRAY: {
Douglas Gregor903b7e92011-07-22 00:38:23 +00003438 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003439 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3440 unsigned IndexTypeQuals = Record[2];
3441 unsigned Idx = 3;
3442 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor04318252009-07-06 15:59:29 +00003443 return Context->getConstantArrayType(ElementType, Size,
3444 ASM, IndexTypeQuals);
3445 }
3446
Sebastian Redl539c5062010-08-18 23:57:32 +00003447 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregor903b7e92011-07-22 00:38:23 +00003448 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003449 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3450 unsigned IndexTypeQuals = Record[2];
Chris Lattner8575daa2009-04-27 21:45:14 +00003451 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003452 }
3453
Sebastian Redl539c5062010-08-18 23:57:32 +00003454 case TYPE_VARIABLE_ARRAY: {
Douglas Gregor903b7e92011-07-22 00:38:23 +00003455 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00003456 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3457 unsigned IndexTypeQuals = Record[2];
Sebastian Redl2c373b92010-10-05 15:59:54 +00003458 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
3459 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
3460 return Context->getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor04318252009-07-06 15:59:29 +00003461 ASM, IndexTypeQuals,
3462 SourceRange(LBLoc, RBLoc));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003463 }
3464
Sebastian Redl539c5062010-08-18 23:57:32 +00003465 case TYPE_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00003466 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003467 Error("incorrect encoding of vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003468 return QualType();
3469 }
3470
Douglas Gregor903b7e92011-07-22 00:38:23 +00003471 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003472 unsigned NumElements = Record[1];
Bob Wilsonaeb56442010-11-10 21:56:12 +00003473 unsigned VecKind = Record[2];
Chris Lattner37141f42010-06-23 06:00:24 +00003474 return Context->getVectorType(ElementType, NumElements,
Bob Wilsonaeb56442010-11-10 21:56:12 +00003475 (VectorType::VectorKind)VecKind);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003476 }
3477
Sebastian Redl539c5062010-08-18 23:57:32 +00003478 case TYPE_EXT_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00003479 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003480 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003481 return QualType();
3482 }
3483
Douglas Gregor903b7e92011-07-22 00:38:23 +00003484 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003485 unsigned NumElements = Record[1];
Chris Lattner8575daa2009-04-27 21:45:14 +00003486 return Context->getExtVectorType(ElementType, NumElements);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003487 }
3488
Sebastian Redl539c5062010-08-18 23:57:32 +00003489 case TYPE_FUNCTION_NO_PROTO: {
John McCall31168b02011-06-15 23:02:42 +00003490 if (Record.size() != 6) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00003491 Error("incorrect encoding of no-proto function type");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003492 return QualType();
3493 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003494 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCall31168b02011-06-15 23:02:42 +00003495 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
3496 (CallingConv)Record[4], Record[5]);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00003497 return Context->getFunctionNoProtoType(ResultType, Info);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003498 }
3499
Sebastian Redl539c5062010-08-18 23:57:32 +00003500 case TYPE_FUNCTION_PROTO: {
Douglas Gregor903b7e92011-07-22 00:38:23 +00003501 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCalldb40c7f2010-12-14 08:05:40 +00003502
3503 FunctionProtoType::ExtProtoInfo EPI;
3504 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
Eli Friedmanc5b20b52011-04-09 08:18:08 +00003505 /*hasregparm*/ Record[2],
3506 /*regparm*/ Record[3],
John McCall31168b02011-06-15 23:02:42 +00003507 static_cast<CallingConv>(Record[4]),
3508 /*produces*/ Record[5]);
John McCalldb40c7f2010-12-14 08:05:40 +00003509
John McCall31168b02011-06-15 23:02:42 +00003510 unsigned Idx = 6;
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003511 unsigned NumParams = Record[Idx++];
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003512 SmallVector<QualType, 16> ParamTypes;
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003513 for (unsigned I = 0; I != NumParams; ++I)
Douglas Gregor903b7e92011-07-22 00:38:23 +00003514 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
John McCalldb40c7f2010-12-14 08:05:40 +00003515
3516 EPI.Variadic = Record[Idx++];
3517 EPI.TypeQuals = Record[Idx++];
Douglas Gregordb9d6642011-01-26 05:01:58 +00003518 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003519 ExceptionSpecificationType EST =
3520 static_cast<ExceptionSpecificationType>(Record[Idx++]);
3521 EPI.ExceptionSpecType = EST;
3522 if (EST == EST_Dynamic) {
3523 EPI.NumExceptions = Record[Idx++];
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003524 SmallVector<QualType, 2> Exceptions;
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003525 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
Douglas Gregor903b7e92011-07-22 00:38:23 +00003526 Exceptions.push_back(readType(*Loc.F, Record, Idx));
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003527 EPI.Exceptions = Exceptions.data();
3528 } else if (EST == EST_ComputedNoexcept) {
3529 EPI.NoexceptExpr = ReadExpr(*Loc.F);
3530 }
Jay Foad7d0479f2009-05-21 09:52:38 +00003531 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
John McCalldb40c7f2010-12-14 08:05:40 +00003532 EPI);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003533 }
3534
Douglas Gregor7fb09192011-07-21 22:35:25 +00003535 case TYPE_UNRESOLVED_USING: {
3536 unsigned Idx = 0;
John McCallb96ec562009-12-04 22:46:56 +00003537 return Context->getTypeDeclType(
Douglas Gregor7fb09192011-07-21 22:35:25 +00003538 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
3539 }
3540
Sebastian Redl539c5062010-08-18 23:57:32 +00003541 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003542 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003543 Error("incorrect encoding of typedef type");
3544 return QualType();
3545 }
Douglas Gregor7fb09192011-07-21 22:35:25 +00003546 unsigned Idx = 0;
3547 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
Douglas Gregor903b7e92011-07-22 00:38:23 +00003548 QualType Canonical = readType(*Loc.F, Record, Idx);
Douglas Gregorf86c9392010-10-26 00:51:02 +00003549 if (!Canonical.isNull())
3550 Canonical = Context->getCanonicalType(Canonical);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003551 return Context->getTypedefType(Decl, Canonical);
3552 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003553
Sebastian Redl539c5062010-08-18 23:57:32 +00003554 case TYPE_TYPEOF_EXPR:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003555 return Context->getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003556
Sebastian Redl539c5062010-08-18 23:57:32 +00003557 case TYPE_TYPEOF: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003558 if (Record.size() != 1) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003559 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003560 return QualType();
3561 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003562 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Chris Lattner8575daa2009-04-27 21:45:14 +00003563 return Context->getTypeOfType(UnderlyingType);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003564 }
Mike Stump11289f42009-09-09 15:08:12 +00003565
Sebastian Redl539c5062010-08-18 23:57:32 +00003566 case TYPE_DECLTYPE:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003567 return Context->getDecltypeType(ReadExpr(*Loc.F));
Anders Carlsson81df7b82009-06-24 19:06:50 +00003568
Alexis Hunte852b102011-05-24 22:41:36 +00003569 case TYPE_UNARY_TRANSFORM: {
Douglas Gregor903b7e92011-07-22 00:38:23 +00003570 QualType BaseType = readType(*Loc.F, Record, Idx);
3571 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Alexis Hunte852b102011-05-24 22:41:36 +00003572 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
3573 return Context->getUnaryTransformType(BaseType, UnderlyingType, UKind);
3574 }
3575
Richard Smith30482bc2011-02-20 03:19:35 +00003576 case TYPE_AUTO:
Douglas Gregor903b7e92011-07-22 00:38:23 +00003577 return Context->getAutoType(readType(*Loc.F, Record, Idx));
Richard Smith30482bc2011-02-20 03:19:35 +00003578
Sebastian Redl539c5062010-08-18 23:57:32 +00003579 case TYPE_RECORD: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003580 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003581 Error("incorrect encoding of record type");
3582 return QualType();
3583 }
Douglas Gregor7fb09192011-07-21 22:35:25 +00003584 unsigned Idx = 0;
3585 bool IsDependent = Record[Idx++];
3586 QualType T
3587 = Context->getRecordType(ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx));
John McCall424cec92011-01-19 06:33:43 +00003588 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003589 return T;
3590 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003591
Sebastian Redl539c5062010-08-18 23:57:32 +00003592 case TYPE_ENUM: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003593 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003594 Error("incorrect encoding of enum type");
3595 return QualType();
3596 }
Douglas Gregor7fb09192011-07-21 22:35:25 +00003597 unsigned Idx = 0;
3598 bool IsDependent = Record[Idx++];
3599 QualType T
3600 = Context->getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
John McCall424cec92011-01-19 06:33:43 +00003601 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003602 return T;
3603 }
Douglas Gregor1daeb692009-04-13 18:14:40 +00003604
John McCall81904512011-01-06 01:58:22 +00003605 case TYPE_ATTRIBUTED: {
3606 if (Record.size() != 3) {
3607 Error("incorrect encoding of attributed type");
3608 return QualType();
3609 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003610 QualType modifiedType = readType(*Loc.F, Record, Idx);
3611 QualType equivalentType = readType(*Loc.F, Record, Idx);
John McCall81904512011-01-06 01:58:22 +00003612 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
3613 return Context->getAttributedType(kind, modifiedType, equivalentType);
3614 }
3615
Abramo Bagnara924a8f32010-12-10 16:29:40 +00003616 case TYPE_PAREN: {
3617 if (Record.size() != 1) {
3618 Error("incorrect encoding of paren type");
3619 return QualType();
3620 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003621 QualType InnerType = readType(*Loc.F, Record, Idx);
Abramo Bagnara924a8f32010-12-10 16:29:40 +00003622 return Context->getParenType(InnerType);
3623 }
3624
Douglas Gregord2fa7662010-12-20 02:24:11 +00003625 case TYPE_PACK_EXPANSION: {
Douglas Gregor17328502011-02-01 15:24:58 +00003626 if (Record.size() != 2) {
Douglas Gregord2fa7662010-12-20 02:24:11 +00003627 Error("incorrect encoding of pack expansion type");
3628 return QualType();
3629 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003630 QualType Pattern = readType(*Loc.F, Record, Idx);
Douglas Gregord2fa7662010-12-20 02:24:11 +00003631 if (Pattern.isNull())
3632 return QualType();
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00003633 llvm::Optional<unsigned> NumExpansions;
3634 if (Record[1])
3635 NumExpansions = Record[1] - 1;
3636 return Context->getPackExpansionType(Pattern, NumExpansions);
Douglas Gregord2fa7662010-12-20 02:24:11 +00003637 }
3638
Sebastian Redl539c5062010-08-18 23:57:32 +00003639 case TYPE_ELABORATED: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003640 unsigned Idx = 0;
3641 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor7fb09192011-07-21 22:35:25 +00003642 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor903b7e92011-07-22 00:38:23 +00003643 QualType NamedType = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003644 return Context->getElaboratedType(Keyword, NNS, NamedType);
John McCallfcc33b02009-09-05 00:15:47 +00003645 }
3646
Sebastian Redl539c5062010-08-18 23:57:32 +00003647 case TYPE_OBJC_INTERFACE: {
Chris Lattner587cbe12009-04-22 06:45:28 +00003648 unsigned Idx = 0;
Douglas Gregor7fb09192011-07-21 22:35:25 +00003649 ObjCInterfaceDecl *ItfD
3650 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
John McCall8b07ec22010-05-15 11:32:37 +00003651 return Context->getObjCInterfaceType(ItfD);
3652 }
3653
Sebastian Redl539c5062010-08-18 23:57:32 +00003654 case TYPE_OBJC_OBJECT: {
John McCall8b07ec22010-05-15 11:32:37 +00003655 unsigned Idx = 0;
Douglas Gregor903b7e92011-07-22 00:38:23 +00003656 QualType Base = readType(*Loc.F, Record, Idx);
Chris Lattner587cbe12009-04-22 06:45:28 +00003657 unsigned NumProtos = Record[Idx++];
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003658 SmallVector<ObjCProtocolDecl*, 4> Protos;
Chris Lattner587cbe12009-04-22 06:45:28 +00003659 for (unsigned I = 0; I != NumProtos; ++I)
Douglas Gregor7fb09192011-07-21 22:35:25 +00003660 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003661 return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattner587cbe12009-04-22 06:45:28 +00003662 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003663
Sebastian Redl539c5062010-08-18 23:57:32 +00003664 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattner6e054af2009-04-22 06:40:03 +00003665 unsigned Idx = 0;
Douglas Gregor903b7e92011-07-22 00:38:23 +00003666 QualType Pointee = readType(*Loc.F, Record, Idx);
John McCall8b07ec22010-05-15 11:32:37 +00003667 return Context->getObjCObjectPointerType(Pointee);
Chris Lattner6e054af2009-04-22 06:40:03 +00003668 }
Argyrios Kyrtzidisa7a36df2009-09-29 19:42:55 +00003669
Sebastian Redl539c5062010-08-18 23:57:32 +00003670 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCallcebee162009-10-18 09:09:24 +00003671 unsigned Idx = 0;
Douglas Gregor903b7e92011-07-22 00:38:23 +00003672 QualType Parm = readType(*Loc.F, Record, Idx);
3673 QualType Replacement = readType(*Loc.F, Record, Idx);
John McCallcebee162009-10-18 09:09:24 +00003674 return
3675 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
3676 Replacement);
3677 }
John McCalle78aac42010-03-10 03:28:59 +00003678
Douglas Gregorada4b792011-01-14 02:55:32 +00003679 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
3680 unsigned Idx = 0;
Douglas Gregor903b7e92011-07-22 00:38:23 +00003681 QualType Parm = readType(*Loc.F, Record, Idx);
Douglas Gregorada4b792011-01-14 02:55:32 +00003682 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
3683 return Context->getSubstTemplateTypeParmPackType(
3684 cast<TemplateTypeParmType>(Parm),
3685 ArgPack);
3686 }
3687
Sebastian Redl539c5062010-08-18 23:57:32 +00003688 case TYPE_INJECTED_CLASS_NAME: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00003689 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
Douglas Gregor903b7e92011-07-22 00:38:23 +00003690 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00003691 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003692 // for AST reading, too much interdependencies.
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00003693 return
3694 QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCalle78aac42010-03-10 03:28:59 +00003695 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003696
Sebastian Redl539c5062010-08-18 23:57:32 +00003697 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003698 unsigned Idx = 0;
3699 unsigned Depth = Record[Idx++];
3700 unsigned Index = Record[Idx++];
3701 bool Pack = Record[Idx++];
Douglas Gregor7fb09192011-07-21 22:35:25 +00003702 TemplateTypeParmDecl *D
3703 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
Chandler Carruth08836322011-05-01 00:51:33 +00003704 return Context->getTemplateTypeParmType(Depth, Index, Pack, D);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003705 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003706
Sebastian Redl539c5062010-08-18 23:57:32 +00003707 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00003708 unsigned Idx = 0;
3709 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor7fb09192011-07-21 22:35:25 +00003710 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregora3e41532011-07-28 20:55:49 +00003711 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Douglas Gregor903b7e92011-07-22 00:38:23 +00003712 QualType Canon = readType(*Loc.F, Record, Idx);
Douglas Gregorf86c9392010-10-26 00:51:02 +00003713 if (!Canon.isNull())
3714 Canon = Context->getCanonicalType(Canon);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +00003715 return Context->getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00003716 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003717
Sebastian Redl539c5062010-08-18 23:57:32 +00003718 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003719 unsigned Idx = 0;
3720 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor7fb09192011-07-21 22:35:25 +00003721 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregora3e41532011-07-28 20:55:49 +00003722 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003723 unsigned NumArgs = Record[Idx++];
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003724 SmallVector<TemplateArgument, 8> Args;
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003725 Args.reserve(NumArgs);
3726 while (NumArgs--)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003727 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003728 return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
3729 Args.size(), Args.data());
3730 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003731
Sebastian Redl539c5062010-08-18 23:57:32 +00003732 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00003733 unsigned Idx = 0;
3734
3735 // ArrayType
Douglas Gregor903b7e92011-07-22 00:38:23 +00003736 QualType ElementType = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00003737 ArrayType::ArraySizeModifier ASM
3738 = (ArrayType::ArraySizeModifier)Record[Idx++];
3739 unsigned IndexTypeQuals = Record[Idx++];
3740
3741 // DependentSizedArrayType
Sebastian Redl2c373b92010-10-05 15:59:54 +00003742 Expr *NumElts = ReadExpr(*Loc.F);
3743 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00003744
3745 return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
3746 IndexTypeQuals, Brackets);
3747 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003748
Sebastian Redl539c5062010-08-18 23:57:32 +00003749 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003750 unsigned Idx = 0;
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003751 bool IsDependent = Record[Idx++];
Douglas Gregor5590be02011-01-15 06:45:20 +00003752 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003753 SmallVector<TemplateArgument, 8> Args;
Sebastian Redl2c373b92010-10-05 15:59:54 +00003754 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Douglas Gregor903b7e92011-07-22 00:38:23 +00003755 QualType Underlying = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003756 QualType T;
Richard Smith3f1b5d02011-05-05 21:57:07 +00003757 if (Underlying.isNull())
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003758 T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(),
3759 Args.size());
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003760 else
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003761 T = Context->getTemplateSpecializationType(Name, Args.data(),
Richard Smith3f1b5d02011-05-05 21:57:07 +00003762 Args.size(), Underlying);
John McCall424cec92011-01-19 06:33:43 +00003763 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003764 return T;
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003765 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003766 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003767 // Suppress a GCC warning
3768 return QualType();
3769}
3770
Sebastian Redl2c373b92010-10-05 15:59:54 +00003771class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redl2c499f62010-08-18 23:56:43 +00003772 ASTReader &Reader;
Douglas Gregora6895d82011-07-22 16:00:58 +00003773 Module &F;
Sebastian Redlc67764e2010-07-22 22:43:28 +00003774 llvm::BitstreamCursor &DeclsCursor;
Sebastian Redl2c499f62010-08-18 23:56:43 +00003775 const ASTReader::RecordData &Record;
John McCall8f115c62009-10-16 21:56:05 +00003776 unsigned &Idx;
3777
Sebastian Redl2c373b92010-10-05 15:59:54 +00003778 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
3779 unsigned &I) {
3780 return Reader.ReadSourceLocation(F, R, I);
3781 }
3782
Douglas Gregor7fb09192011-07-21 22:35:25 +00003783 template<typename T>
3784 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
3785 return Reader.ReadDeclAs<T>(F, Record, Idx);
3786 }
3787
John McCall8f115c62009-10-16 21:56:05 +00003788public:
Douglas Gregora6895d82011-07-22 16:00:58 +00003789 TypeLocReader(ASTReader &Reader, Module &F,
Sebastian Redl2c499f62010-08-18 23:56:43 +00003790 const ASTReader::RecordData &Record, unsigned &Idx)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003791 : Reader(Reader), F(F), DeclsCursor(F.DeclsCursor), Record(Record), Idx(Idx)
3792 { }
John McCall8f115c62009-10-16 21:56:05 +00003793
John McCall17001972009-10-18 01:05:36 +00003794 // We want compile-time assurance that we've enumerated all of
3795 // these, so unfortunately we have to declare them first, then
3796 // define them out-of-line.
3797#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +00003798#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +00003799 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +00003800#include "clang/AST/TypeLocNodes.def"
3801
John McCall17001972009-10-18 01:05:36 +00003802 void VisitFunctionTypeLoc(FunctionTypeLoc);
3803 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCall8f115c62009-10-16 21:56:05 +00003804};
3805
John McCall17001972009-10-18 01:05:36 +00003806void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCall8f115c62009-10-16 21:56:05 +00003807 // nothing to do
3808}
John McCall17001972009-10-18 01:05:36 +00003809void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003810 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorc9b7a592010-01-18 18:04:31 +00003811 if (TL.needsExtraLocalData()) {
3812 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
3813 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
3814 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
3815 TL.setModeAttr(Record[Idx++]);
3816 }
John McCall8f115c62009-10-16 21:56:05 +00003817}
John McCall17001972009-10-18 01:05:36 +00003818void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003819 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003820}
John McCall17001972009-10-18 01:05:36 +00003821void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003822 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003823}
John McCall17001972009-10-18 01:05:36 +00003824void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003825 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003826}
John McCall17001972009-10-18 01:05:36 +00003827void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003828 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003829}
John McCall17001972009-10-18 01:05:36 +00003830void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003831 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003832}
John McCall17001972009-10-18 01:05:36 +00003833void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003834 TL.setStarLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnara509357842011-03-05 14:42:21 +00003835 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003836}
John McCall17001972009-10-18 01:05:36 +00003837void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003838 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
3839 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003840 if (Record[Idx++])
Sebastian Redl2c373b92010-10-05 15:59:54 +00003841 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor12bfa382009-10-17 00:13:19 +00003842 else
John McCall17001972009-10-18 01:05:36 +00003843 TL.setSizeExpr(0);
3844}
3845void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
3846 VisitArrayTypeLoc(TL);
3847}
3848void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
3849 VisitArrayTypeLoc(TL);
3850}
3851void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
3852 VisitArrayTypeLoc(TL);
3853}
3854void TypeLocReader::VisitDependentSizedArrayTypeLoc(
3855 DependentSizedArrayTypeLoc TL) {
3856 VisitArrayTypeLoc(TL);
3857}
3858void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
3859 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003860 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003861}
3862void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003863 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003864}
3865void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003866 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003867}
3868void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003869 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
3870 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
Douglas Gregor7fb25412010-10-01 18:44:50 +00003871 TL.setTrailingReturn(Record[Idx++]);
John McCall17001972009-10-18 01:05:36 +00003872 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
Douglas Gregor7fb09192011-07-21 22:35:25 +00003873 TL.setArg(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003874 }
3875}
3876void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
3877 VisitFunctionTypeLoc(TL);
3878}
3879void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
3880 VisitFunctionTypeLoc(TL);
3881}
John McCallb96ec562009-12-04 22:46:56 +00003882void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003883 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallb96ec562009-12-04 22:46:56 +00003884}
John McCall17001972009-10-18 01:05:36 +00003885void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003886 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003887}
3888void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003889 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3890 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3891 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003892}
3893void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003894 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3895 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3896 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3897 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003898}
3899void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003900 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003901}
Alexis Hunte852b102011-05-24 22:41:36 +00003902void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
3903 TL.setKWLoc(ReadSourceLocation(Record, Idx));
3904 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3905 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3906 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
3907}
Richard Smith30482bc2011-02-20 03:19:35 +00003908void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
3909 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3910}
John McCall17001972009-10-18 01:05:36 +00003911void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003912 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003913}
3914void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003915 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003916}
John McCall81904512011-01-06 01:58:22 +00003917void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
3918 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
3919 if (TL.hasAttrOperand()) {
3920 SourceRange range;
3921 range.setBegin(ReadSourceLocation(Record, Idx));
3922 range.setEnd(ReadSourceLocation(Record, Idx));
3923 TL.setAttrOperandParensRange(range);
3924 }
3925 if (TL.hasAttrExprOperand()) {
3926 if (Record[Idx++])
3927 TL.setAttrExprOperand(Reader.ReadExpr(F));
3928 else
3929 TL.setAttrExprOperand(0);
3930 } else if (TL.hasAttrEnumOperand())
3931 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
3932}
John McCall17001972009-10-18 01:05:36 +00003933void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003934 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003935}
John McCallcebee162009-10-18 09:09:24 +00003936void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
3937 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003938 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallcebee162009-10-18 09:09:24 +00003939}
Douglas Gregorada4b792011-01-14 02:55:32 +00003940void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
3941 SubstTemplateTypeParmPackTypeLoc TL) {
3942 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3943}
John McCall17001972009-10-18 01:05:36 +00003944void TypeLocReader::VisitTemplateSpecializationTypeLoc(
3945 TemplateSpecializationTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003946 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
3947 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3948 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall0ad16662009-10-29 08:12:44 +00003949 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
3950 TL.setArgLocInfo(i,
Sebastian Redl2c373b92010-10-05 15:59:54 +00003951 Reader.GetTemplateArgumentLocInfo(F,
3952 TL.getTypePtr()->getArg(i).getKind(),
3953 Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003954}
Abramo Bagnara924a8f32010-12-10 16:29:40 +00003955void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
3956 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3957 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3958}
Abramo Bagnara6150c882010-05-11 21:36:43 +00003959void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003960 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor844cb502011-03-01 18:12:44 +00003961 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003962}
John McCalle78aac42010-03-10 03:28:59 +00003963void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003964 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalle78aac42010-03-10 03:28:59 +00003965}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00003966void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003967 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor3d0da5f2011-03-01 01:34:45 +00003968 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redl2c373b92010-10-05 15:59:54 +00003969 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003970}
John McCallc392f372010-06-11 00:33:02 +00003971void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
3972 DependentTemplateSpecializationTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003973 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregora7a795b2011-03-01 20:11:18 +00003974 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redl2c373b92010-10-05 15:59:54 +00003975 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3976 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3977 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCallc392f372010-06-11 00:33:02 +00003978 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
3979 TL.setArgLocInfo(I,
Sebastian Redl2c373b92010-10-05 15:59:54 +00003980 Reader.GetTemplateArgumentLocInfo(F,
3981 TL.getTypePtr()->getArg(I).getKind(),
3982 Record, Idx));
John McCallc392f372010-06-11 00:33:02 +00003983}
Douglas Gregord2fa7662010-12-20 02:24:11 +00003984void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
3985 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
3986}
John McCall17001972009-10-18 01:05:36 +00003987void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003988 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall8b07ec22010-05-15 11:32:37 +00003989}
3990void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3991 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003992 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3993 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003994 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003995 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003996}
John McCallfc93cf92009-10-22 22:37:11 +00003997void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003998 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCallfc93cf92009-10-22 22:37:11 +00003999}
John McCall8f115c62009-10-16 21:56:05 +00004000
Douglas Gregora6895d82011-07-22 16:00:58 +00004001TypeSourceInfo *ASTReader::GetTypeSourceInfo(Module &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00004002 const RecordData &Record,
John McCall8f115c62009-10-16 21:56:05 +00004003 unsigned &Idx) {
Douglas Gregor903b7e92011-07-22 00:38:23 +00004004 QualType InfoTy = readType(F, Record, Idx);
John McCall8f115c62009-10-16 21:56:05 +00004005 if (InfoTy.isNull())
4006 return 0;
4007
John McCallbcd03502009-12-07 02:54:59 +00004008 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
Sebastian Redl2c373b92010-10-05 15:59:54 +00004009 TypeLocReader TLR(*this, F, Record, Idx);
John McCallbcd03502009-12-07 02:54:59 +00004010 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCall8f115c62009-10-16 21:56:05 +00004011 TLR.Visit(TL);
John McCallbcd03502009-12-07 02:54:59 +00004012 return TInfo;
John McCall8f115c62009-10-16 21:56:05 +00004013}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004014
Sebastian Redl539c5062010-08-18 23:57:32 +00004015QualType ASTReader::GetType(TypeID ID) {
John McCall8ccfcb52009-09-24 19:53:00 +00004016 unsigned FastQuals = ID & Qualifiers::FastMask;
4017 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004018
Sebastian Redl539c5062010-08-18 23:57:32 +00004019 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004020 QualType T;
Sebastian Redl539c5062010-08-18 23:57:32 +00004021 switch ((PredefinedTypeIDs)Index) {
4022 case PREDEF_TYPE_NULL_ID: return QualType();
4023 case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
4024 case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004025
Sebastian Redl539c5062010-08-18 23:57:32 +00004026 case PREDEF_TYPE_CHAR_U_ID:
4027 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004028 // FIXME: Check that the signedness of CharTy is correct!
Chris Lattner8575daa2009-04-27 21:45:14 +00004029 T = Context->CharTy;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004030 break;
4031
Sebastian Redl539c5062010-08-18 23:57:32 +00004032 case PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break;
4033 case PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break;
4034 case PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break;
4035 case PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break;
4036 case PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break;
4037 case PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break;
4038 case PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break;
4039 case PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break;
4040 case PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break;
4041 case PREDEF_TYPE_INT_ID: T = Context->IntTy; break;
4042 case PREDEF_TYPE_LONG_ID: T = Context->LongTy; break;
4043 case PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break;
4044 case PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break;
4045 case PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break;
4046 case PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break;
4047 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break;
4048 case PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break;
John McCall0009fcc2011-04-26 20:42:42 +00004049 case PREDEF_TYPE_BOUND_MEMBER: T = Context->BoundMemberTy; break;
Sebastian Redl539c5062010-08-18 23:57:32 +00004050 case PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break;
John McCall31996342011-04-07 08:22:57 +00004051 case PREDEF_TYPE_UNKNOWN_ANY: T = Context->UnknownAnyTy; break;
Sebastian Redl539c5062010-08-18 23:57:32 +00004052 case PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break;
4053 case PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break;
4054 case PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break;
4055 case PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break;
4056 case PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break;
4057 case PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break;
Douglas Gregoreda8e122011-08-09 15:13:55 +00004058 case PREDEF_TYPE_AUTO_DEDUCT: T = Context->getAutoDeductType(); break;
4059
4060 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
4061 T = Context->getAutoRRefDeductType();
4062 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004063 }
4064
4065 assert(!T.isNull() && "Unknown predefined type");
John McCall8ccfcb52009-09-24 19:53:00 +00004066 return T.withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004067 }
4068
Sebastian Redl539c5062010-08-18 23:57:32 +00004069 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redl837a6cb2010-07-20 22:37:49 +00004070 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl409183f2010-07-14 20:26:45 +00004071 if (TypesLoaded[Index].isNull()) {
Douglas Gregor903b7e92011-07-22 00:38:23 +00004072 TypesLoaded[Index] = readTypeRecord(Index);
Douglas Gregor9b3932c2010-10-05 18:37:06 +00004073 if (TypesLoaded[Index].isNull())
4074 return QualType();
4075
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004076 TypesLoaded[Index]->setFromAST();
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00004077 if (DeserializationListener)
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00004078 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00004079 TypesLoaded[Index]);
Sebastian Redl409183f2010-07-14 20:26:45 +00004080 }
Mike Stump11289f42009-09-09 15:08:12 +00004081
John McCall8ccfcb52009-09-24 19:53:00 +00004082 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004083}
4084
Douglas Gregora6895d82011-07-22 16:00:58 +00004085QualType ASTReader::getLocalType(Module &F, unsigned LocalID) {
Douglas Gregor903b7e92011-07-22 00:38:23 +00004086 return GetType(getGlobalTypeID(F, LocalID));
4087}
4088
4089serialization::TypeID
Douglas Gregora6895d82011-07-22 16:00:58 +00004090ASTReader::getGlobalTypeID(Module &F, unsigned LocalID) const {
Douglas Gregor5204bde2011-08-02 16:26:37 +00004091 unsigned FastQuals = LocalID & Qualifiers::FastMask;
4092 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
4093
4094 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
4095 return LocalID;
4096
4097 ContinuousRangeMap<uint32_t, int, 2>::iterator I
4098 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
4099 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
4100
4101 unsigned GlobalIndex = LocalIndex + I->second;
4102 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
4103}
4104
John McCall0ad16662009-10-29 08:12:44 +00004105TemplateArgumentLocInfo
Douglas Gregora6895d82011-07-22 16:00:58 +00004106ASTReader::GetTemplateArgumentLocInfo(Module &F,
Sebastian Redl2c373b92010-10-05 15:59:54 +00004107 TemplateArgument::ArgKind Kind,
John McCall0ad16662009-10-29 08:12:44 +00004108 const RecordData &Record,
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00004109 unsigned &Index) {
John McCall0ad16662009-10-29 08:12:44 +00004110 switch (Kind) {
4111 case TemplateArgument::Expression:
Sebastian Redl2c373b92010-10-05 15:59:54 +00004112 return ReadExpr(F);
John McCall0ad16662009-10-29 08:12:44 +00004113 case TemplateArgument::Type:
Sebastian Redl2c373b92010-10-05 15:59:54 +00004114 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004115 case TemplateArgument::Template: {
Douglas Gregor9d802122011-03-02 17:09:35 +00004116 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
4117 Index);
Sebastian Redl2c373b92010-10-05 15:59:54 +00004118 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregor9d802122011-03-02 17:09:35 +00004119 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004120 SourceLocation());
4121 }
4122 case TemplateArgument::TemplateExpansion: {
Douglas Gregor9d802122011-03-02 17:09:35 +00004123 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
4124 Index);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004125 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregoreb29d182011-01-05 17:40:24 +00004126 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregor9d802122011-03-02 17:09:35 +00004127 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregoreb29d182011-01-05 17:40:24 +00004128 EllipsisLoc);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004129 }
John McCall0ad16662009-10-29 08:12:44 +00004130 case TemplateArgument::Null:
4131 case TemplateArgument::Integral:
4132 case TemplateArgument::Declaration:
4133 case TemplateArgument::Pack:
4134 return TemplateArgumentLocInfo();
4135 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +00004136 llvm_unreachable("unexpected template argument loc");
John McCall0ad16662009-10-29 08:12:44 +00004137 return TemplateArgumentLocInfo();
4138}
4139
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004140TemplateArgumentLoc
Douglas Gregora6895d82011-07-22 16:00:58 +00004141ASTReader::ReadTemplateArgumentLoc(Module &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00004142 const RecordData &Record, unsigned &Index) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004143 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004144
4145 if (Arg.getKind() == TemplateArgument::Expression) {
4146 if (Record[Index++]) // bool InfoHasSameExpr.
4147 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
4148 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00004149 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00004150 Record, Index));
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004151}
4152
Sebastian Redl2c499f62010-08-18 23:56:43 +00004153Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall75b960e2010-06-01 09:23:16 +00004154 return GetDecl(ID);
4155}
4156
Douglas Gregorc27b2872011-08-04 00:01:48 +00004157uint64_t ASTReader::readCXXBaseSpecifiers(Module &M, const RecordData &Record,
4158 unsigned &Idx){
4159 if (Idx >= Record.size())
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004160 return 0;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004161
Douglas Gregorc27b2872011-08-04 00:01:48 +00004162 unsigned LocalID = Record[Idx++];
4163 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004164}
4165
4166CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
Douglas Gregord32f0352011-07-22 06:10:01 +00004167 RecordLocation Loc = getLocalBitOffset(Offset);
4168 llvm::BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004169 SavedStreamPosition SavedPosition(Cursor);
Douglas Gregord32f0352011-07-22 06:10:01 +00004170 Cursor.JumpToBit(Loc.Offset);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004171 ReadingKindTracker ReadingKind(Read_Decl, *this);
4172 RecordData Record;
4173 unsigned Code = Cursor.ReadCode();
4174 unsigned RecCode = Cursor.ReadRecord(Code, Record);
4175 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
4176 Error("Malformed AST file: missing C++ base specifiers");
4177 return 0;
4178 }
4179
4180 unsigned Idx = 0;
4181 unsigned NumBases = Record[Idx++];
4182 void *Mem = Context->Allocate(sizeof(CXXBaseSpecifier) * NumBases);
4183 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
4184 for (unsigned I = 0; I != NumBases; ++I)
Douglas Gregord32f0352011-07-22 06:10:01 +00004185 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004186 return Bases;
4187}
4188
Douglas Gregor7fb09192011-07-21 22:35:25 +00004189serialization::DeclID
Douglas Gregora6895d82011-07-22 16:00:58 +00004190ASTReader::getGlobalDeclID(Module &F, unsigned LocalID) const {
Douglas Gregor6f8912e2011-08-03 16:05:40 +00004191 if (LocalID < NUM_PREDEF_DECL_IDS)
Douglas Gregorf7180622011-08-03 15:48:04 +00004192 return LocalID;
4193
4194 ContinuousRangeMap<uint32_t, int, 2>::iterator I
Douglas Gregor6f8912e2011-08-03 16:05:40 +00004195 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
Douglas Gregorf7180622011-08-03 15:48:04 +00004196 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
4197
4198 return LocalID + I->second;
Douglas Gregor7fb09192011-07-21 22:35:25 +00004199}
4200
Sebastian Redl539c5062010-08-18 23:57:32 +00004201Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregor6f8912e2011-08-03 16:05:40 +00004202 if (ID < NUM_PREDEF_DECL_IDS) {
4203 switch ((PredefinedDeclIDs)ID) {
Douglas Gregordab42432011-08-12 00:15:20 +00004204 case PREDEF_DECL_NULL_ID:
Douglas Gregor6f8912e2011-08-03 16:05:40 +00004205 return 0;
Douglas Gregordab42432011-08-12 00:15:20 +00004206
4207 case PREDEF_DECL_TRANSLATION_UNIT_ID:
4208 assert(Context && "No context available?");
4209 return Context->getTranslationUnitDecl();
Douglas Gregor3ea72692011-08-12 05:46:01 +00004210
4211 case PREDEF_DECL_OBJC_ID_ID:
4212 assert(Context && "No context available?");
4213 return Context->getObjCIdDecl();
Douglas Gregor0a586182011-08-12 05:59:41 +00004214
Douglas Gregor52e02802011-08-12 06:17:30 +00004215 case PREDEF_DECL_OBJC_SEL_ID:
4216 assert(Context && "No context available?");
4217 return Context->getObjCSelDecl();
4218
Douglas Gregor0a586182011-08-12 05:59:41 +00004219 case PREDEF_DECL_OBJC_CLASS_ID:
4220 assert(Context && "No context available?");
4221 return Context->getObjCClassDecl();
Douglas Gregor801c99d2011-08-12 06:49:56 +00004222
4223 case PREDEF_DECL_INT_128_ID:
4224 assert(Context && "No context available?");
4225 return Context->getInt128Decl();
4226
4227 case PREDEF_DECL_UNSIGNED_INT_128_ID:
4228 assert(Context && "No context available?");
4229 return Context->getUInt128Decl();
Douglas Gregor6f8912e2011-08-03 16:05:40 +00004230 }
4231
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004232 return 0;
Douglas Gregor6f8912e2011-08-03 16:05:40 +00004233 }
4234
Douglas Gregordab42432011-08-12 00:15:20 +00004235 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
4236
4237 if (Index > DeclsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004238 Error("declaration ID out-of-range for AST file");
Douglas Gregor745ed142009-04-25 18:35:21 +00004239 return 0;
4240 }
Douglas Gregordab42432011-08-12 00:15:20 +00004241
4242if (!DeclsLoaded[Index]) {
Douglas Gregorf7180622011-08-03 15:48:04 +00004243 ReadDeclRecord(ID);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00004244 if (DeserializationListener)
4245 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
4246 }
Douglas Gregor745ed142009-04-25 18:35:21 +00004247
4248 return DeclsLoaded[Index];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004249}
4250
Douglas Gregora6895d82011-07-22 16:00:58 +00004251serialization::DeclID ASTReader::ReadDeclID(Module &F,
Douglas Gregor7fb09192011-07-21 22:35:25 +00004252 const RecordData &Record,
4253 unsigned &Idx) {
4254 if (Idx >= Record.size()) {
4255 Error("Corrupted AST file");
4256 return 0;
4257 }
4258
4259 return getGlobalDeclID(F, Record[Idx++]);
4260}
4261
Chris Lattner9c28af02009-04-27 05:46:25 +00004262/// \brief Resolve the offset of a statement into a statement.
4263///
4264/// This operation will read a new statement from the external
4265/// source each time it is called, and is meant to be used via a
4266/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redl2c499f62010-08-18 23:56:43 +00004267Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidisd9f526f2010-10-28 09:29:32 +00004268 // Switch case IDs are per Decl.
4269 ClearSwitchCaseIDs();
4270
Sebastian Redl5c415f32010-07-22 17:01:13 +00004271 // Offset here is a global offset across the entire chain.
Douglas Gregord32f0352011-07-22 06:10:01 +00004272 RecordLocation Loc = getLocalBitOffset(Offset);
4273 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
4274 return ReadStmtFromStream(*Loc.F);
Douglas Gregor3c3aa612009-04-18 00:07:54 +00004275}
4276
Douglas Gregor3d0adb32011-07-15 21:46:17 +00004277ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00004278 bool (*isKindWeWant)(Decl::Kind),
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004279 SmallVectorImpl<Decl*> &Decls) {
Sebastian Redl5c415f32010-07-22 17:01:13 +00004280 // There might be lexical decls in multiple parts of the chain, for the TU
4281 // at least.
Sebastian Redlda6a21c2010-09-28 02:24:44 +00004282 // DeclContextOffsets might reallocate as we load additional decls below,
4283 // so make a copy of the vector.
4284 DeclContextInfos Infos = DeclContextOffsets[DC];
Sebastian Redl5c415f32010-07-22 17:01:13 +00004285 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
4286 I != E; ++I) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004287 // IDs can be 0 if this context doesn't contain declarations.
4288 if (!I->LexicalDecls)
Sebastian Redl5c415f32010-07-22 17:01:13 +00004289 continue;
Sebastian Redl5c415f32010-07-22 17:01:13 +00004290
4291 // Load all of the declaration IDs
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00004292 for (const KindDeclIDPair *ID = I->LexicalDecls,
4293 *IDE = ID + I->NumLexicalDecls; ID != IDE; ++ID) {
4294 if (isKindWeWant && !isKindWeWant((Decl::Kind)ID->first))
4295 continue;
Douglas Gregor7fb09192011-07-21 22:35:25 +00004296
Douglas Gregorf224ae02011-07-21 23:29:11 +00004297 Decl *D = GetLocalDecl(*I->F, ID->second);
Sebastian Redlda6a21c2010-09-28 02:24:44 +00004298 assert(D && "Null decl in lexical decls");
4299 Decls.push_back(D);
4300 }
Ted Kremenek1ff615c2010-03-18 00:56:54 +00004301 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004302
Douglas Gregora57c3ab2009-04-22 22:34:57 +00004303 ++NumLexicalDeclContextsRead;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00004304 return ELR_Success;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004305}
4306
John McCall75b960e2010-06-01 09:23:16 +00004307DeclContext::lookup_result
Sebastian Redl2c499f62010-08-18 23:56:43 +00004308ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall75b960e2010-06-01 09:23:16 +00004309 DeclarationName Name) {
Mike Stump11289f42009-09-09 15:08:12 +00004310 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004311 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004312 if (!Name)
4313 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
4314 DeclContext::lookup_iterator(0));
Ted Kremenek1ff615c2010-03-18 00:56:54 +00004315
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004316 SmallVector<NamedDecl *, 64> Decls;
Sebastian Redl471ac2f2010-08-24 00:49:55 +00004317 // There might be visible decls in multiple parts of the chain, for the TU
Sebastian Redl9617e7e2010-08-24 00:50:16 +00004318 // and namespaces. For any given name, the last available results replace
4319 // all earlier ones. For this reason, we walk in reverse.
Argyrios Kyrtzidis1d233f92011-08-11 23:26:42 +00004320 // Copy the DeclContextInfos vector instead of using a reference to the
4321 // vector stored in the map, because DeclContextOffsets can change while
4322 // we load declarations with GetLocalDeclAs.
4323 DeclContextInfos Infos = DeclContextOffsets[DC];
Sebastian Redl9617e7e2010-08-24 00:50:16 +00004324 for (DeclContextInfos::reverse_iterator I = Infos.rbegin(), E = Infos.rend();
Sebastian Redl5c415f32010-07-22 17:01:13 +00004325 I != E; ++I) {
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004326 if (!I->NameLookupTableData)
Sebastian Redl5c415f32010-07-22 17:01:13 +00004327 continue;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004328
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004329 ASTDeclContextNameLookupTable *LookupTable =
4330 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
4331 ASTDeclContextNameLookupTable::iterator Pos = LookupTable->find(Name);
4332 if (Pos == LookupTable->end())
Sebastian Redl5c415f32010-07-22 17:01:13 +00004333 continue;
4334
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004335 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00004336 for (; Data.first != Data.second; ++Data.first) {
4337 NamedDecl *ND = GetLocalDeclAs<NamedDecl>(*I->F, *Data.first);
4338 if (!ND)
4339 continue;
4340
4341 if (ND->getDeclName() != Name) {
4342 assert(!Name.getCXXNameType().isNull() &&
4343 "Name mismatch without a type");
4344 continue;
4345 }
4346
4347 Decls.push_back(ND);
4348 }
4349
4350 // If we rejected all of the declarations we found, e.g., because the
4351 // name didn't actually match, continue looking through DeclContexts.
4352 if (Decls.empty())
4353 continue;
4354
Sebastian Redl9617e7e2010-08-24 00:50:16 +00004355 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004356 }
4357
Douglas Gregora57c3ab2009-04-22 22:34:57 +00004358 ++NumVisibleDeclContextsRead;
John McCall75b960e2010-06-01 09:23:16 +00004359
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004360 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall75b960e2010-06-01 09:23:16 +00004361 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004362}
4363
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00004364void ASTReader::MaterializeVisibleDecls(const DeclContext *DC) {
4365 assert(DC->hasExternalVisibleStorage() &&
4366 "DeclContext has no visible decls in storage");
4367
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004368 SmallVector<NamedDecl *, 64> Decls;
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00004369 // There might be visible decls in multiple parts of the chain, for the TU
4370 // and namespaces.
4371 DeclContextInfos &Infos = DeclContextOffsets[DC];
4372 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
4373 I != E; ++I) {
4374 if (!I->NameLookupTableData)
4375 continue;
4376
4377 ASTDeclContextNameLookupTable *LookupTable =
4378 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
4379 for (ASTDeclContextNameLookupTable::item_iterator
4380 ItemI = LookupTable->item_begin(),
4381 ItemEnd = LookupTable->item_end() ; ItemI != ItemEnd; ++ItemI) {
4382 ASTDeclContextNameLookupTable::item_iterator::value_type Val
4383 = *ItemI;
4384 ASTDeclContextNameLookupTrait::data_type Data = Val.second;
4385 Decls.clear();
4386 for (; Data.first != Data.second; ++Data.first)
Douglas Gregorf224ae02011-07-21 23:29:11 +00004387 Decls.push_back(GetLocalDeclAs<NamedDecl>(*I->F, *Data.first));
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00004388 MaterializeVisibleDeclsForName(DC, Val.first, Decls);
4389 }
4390 }
4391}
4392
Sebastian Redl2c499f62010-08-18 23:56:43 +00004393void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004394 assert(Consumer);
4395 while (!InterestingDecls.empty()) {
4396 DeclGroupRef DG(InterestingDecls.front());
4397 InterestingDecls.pop_front();
Sebastian Redleaa4ade2010-08-11 18:52:41 +00004398 Consumer->HandleInterestingDecl(DG);
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004399 }
4400}
4401
Sebastian Redl2c499f62010-08-18 23:56:43 +00004402void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregorb985eeb2009-04-22 19:09:20 +00004403 this->Consumer = Consumer;
4404
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00004405 if (!Consumer)
4406 return;
4407
4408 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004409 // Force deserialization of this decl, which will cause it to be queued for
4410 // passing to the consumer.
Daniel Dunbar865c2a72009-09-17 03:06:44 +00004411 GetDecl(ExternalDefinitions[I]);
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00004412 }
Douglas Gregorf005eac2009-04-25 00:41:30 +00004413
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004414 PassInterestingDeclsToConsumer();
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00004415}
4416
Sebastian Redl2c499f62010-08-18 23:56:43 +00004417void ASTReader::PrintStats() {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004418 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004419
Mike Stump11289f42009-09-09 15:08:12 +00004420 unsigned NumTypesLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00004421 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall8ccfcb52009-09-24 19:53:00 +00004422 QualType());
Douglas Gregor0e149972009-04-25 19:10:14 +00004423 unsigned NumDeclsLoaded
4424 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
4425 (Decl *)0);
4426 unsigned NumIdentifiersLoaded
4427 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
4428 IdentifiersLoaded.end(),
4429 (IdentifierInfo *)0);
Mike Stump11289f42009-09-09 15:08:12 +00004430 unsigned NumSelectorsLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00004431 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
4432 SelectorsLoaded.end(),
4433 Selector());
Douglas Gregorc3b1dd12009-04-13 20:50:16 +00004434
Douglas Gregorc5046832009-04-27 18:38:38 +00004435 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
4436 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor49bf76b2011-07-21 18:46:38 +00004437 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
Douglas Gregor258ae542009-04-27 06:38:32 +00004438 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
4439 NumSLocEntriesRead, TotalNumSLocEntries,
4440 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor745ed142009-04-25 18:35:21 +00004441 if (!TypesLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00004442 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00004443 NumTypesLoaded, (unsigned)TypesLoaded.size(),
4444 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
4445 if (!DeclsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00004446 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00004447 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
4448 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor0e149972009-04-25 19:10:14 +00004449 if (!IdentifiersLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00004450 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor0e149972009-04-25 19:10:14 +00004451 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
4452 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redlada023c2010-08-04 20:40:17 +00004453 if (!SelectorsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00004454 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redlada023c2010-08-04 20:40:17 +00004455 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
4456 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor95c13f52009-04-25 17:48:32 +00004457 if (TotalNumStatements)
4458 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
4459 NumStatementsRead, TotalNumStatements,
4460 ((float)NumStatementsRead/TotalNumStatements * 100));
4461 if (TotalNumMacros)
4462 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
4463 NumMacrosRead, TotalNumMacros,
4464 ((float)NumMacrosRead/TotalNumMacros * 100));
4465 if (TotalLexicalDeclContexts)
4466 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
4467 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
4468 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
4469 * 100));
4470 if (TotalVisibleDeclContexts)
4471 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
4472 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
4473 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
4474 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004475 if (TotalNumMethodPoolEntries) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004476 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004477 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
4478 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor95c13f52009-04-25 17:48:32 +00004479 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004480 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor95c13f52009-04-25 17:48:32 +00004481 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004482 std::fprintf(stderr, "\n");
Douglas Gregor204b8712011-07-21 19:50:14 +00004483 dump();
4484 std::fprintf(stderr, "\n");
4485}
4486
Douglas Gregora6895d82011-07-22 16:00:58 +00004487template<typename Key, typename Module, unsigned InitialCapacity>
Douglas Gregor204b8712011-07-21 19:50:14 +00004488static void
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004489dumpModuleIDMap(StringRef Name,
Douglas Gregora6895d82011-07-22 16:00:58 +00004490 const ContinuousRangeMap<Key, Module *,
Douglas Gregor204b8712011-07-21 19:50:14 +00004491 InitialCapacity> &Map) {
4492 if (Map.begin() == Map.end())
4493 return;
4494
Douglas Gregora6895d82011-07-22 16:00:58 +00004495 typedef ContinuousRangeMap<Key, Module *, InitialCapacity> MapType;
Douglas Gregor204b8712011-07-21 19:50:14 +00004496 llvm::errs() << Name << ":\n";
4497 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
4498 I != IEnd; ++I) {
4499 llvm::errs() << " " << I->first << " -> " << I->second->FileName
4500 << "\n";
4501 }
4502}
4503
Douglas Gregor204b8712011-07-21 19:50:14 +00004504void ASTReader::dump() {
Douglas Gregor1cc9c062011-08-02 11:12:41 +00004505 llvm::errs() << "*** PCH/Module Remappings:\n";
Douglas Gregord32f0352011-07-22 06:10:01 +00004506 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
Douglas Gregor204b8712011-07-21 19:50:14 +00004507 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
Douglas Gregor8ab4ea82011-07-29 00:21:44 +00004508 dumpModuleIDMap("Global type map", GlobalTypeMap);
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00004509 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00004510 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
4511 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
4512 dumpModuleIDMap("Global macro definition map", GlobalMacroDefinitionMap);
4513 dumpModuleIDMap("Global preprocessed entity map",
4514 GlobalPreprocessedEntityMap);
Douglas Gregor1cc9c062011-08-02 11:12:41 +00004515
4516 llvm::errs() << "\n*** PCH/Modules Loaded:";
4517 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
4518 MEnd = ModuleMgr.end();
4519 M != MEnd; ++M)
4520 (*M)->dump();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004521}
4522
Ted Kremenek5e1ed7b2011-04-28 23:46:20 +00004523/// Return the amount of memory used by memory buffers, breaking down
4524/// by heap-backed versus mmap'ed memory.
4525void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00004526 for (ModuleConstIterator I = ModuleMgr.begin(),
4527 E = ModuleMgr.end(); I != E; ++I) {
4528 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
Ted Kremenek5e1ed7b2011-04-28 23:46:20 +00004529 size_t bytes = buf->getBufferSize();
4530 switch (buf->getBufferKind()) {
4531 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
4532 sizes.malloc_bytes += bytes;
4533 break;
4534 case llvm::MemoryBuffer::MemoryBuffer_MMap:
4535 sizes.mmap_bytes += bytes;
4536 break;
4537 }
4538 }
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00004539 }
Ted Kremenek5e1ed7b2011-04-28 23:46:20 +00004540}
4541
Sebastian Redl2c499f62010-08-18 23:56:43 +00004542void ASTReader::InitializeSema(Sema &S) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00004543 SemaObj = &S;
Douglas Gregorc78d3462009-04-24 21:10:55 +00004544 S.ExternalSource = this;
4545
Douglas Gregor7cd60f72009-04-22 21:15:06 +00004546 // Makes sure any declarations that were deserialized "too early"
4547 // still get added to the identifier's declaration chains.
Douglas Gregor2fb99df2010-09-24 23:29:12 +00004548 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
4549 if (SemaObj->TUScope)
John McCall48871652010-08-21 09:40:31 +00004550 SemaObj->TUScope->AddDecl(PreloadedDecls[I]);
Douglas Gregor2fb99df2010-09-24 23:29:12 +00004551
4552 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregora868bbd2009-04-21 22:25:48 +00004553 }
Douglas Gregor7cd60f72009-04-22 21:15:06 +00004554 PreloadedDecls.clear();
Douglas Gregord4df8652009-04-22 22:02:47 +00004555
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004556 // Load the offsets of the declarations that Sema references.
4557 // They will be lazily deserialized when needed.
4558 if (!SemaDeclRefs.empty()) {
4559 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
Douglas Gregorb0f3ae62011-07-28 00:57:24 +00004560 if (!SemaObj->StdNamespace)
4561 SemaObj->StdNamespace = SemaDeclRefs[0];
4562 if (!SemaObj->StdBadAlloc)
4563 SemaObj->StdBadAlloc = SemaDeclRefs[1];
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004564 }
4565
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004566 if (!FPPragmaOptions.empty()) {
4567 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
4568 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
4569 }
4570
4571 if (!OpenCLExtensions.empty()) {
4572 unsigned I = 0;
4573#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
4574#include "clang/Basic/OpenCLExtensions.def"
4575
4576 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
4577 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00004578}
4579
Douglas Gregorab443b92011-08-20 04:39:52 +00004580namespace {
4581 /// \brief Visitor class used to look up identifirs in
4582 class IdentifierLookupVisitor {
4583 StringRef Name;
4584 IdentifierInfo *Found;
4585 public:
4586 explicit IdentifierLookupVisitor(StringRef Name) : Name(Name), Found() { }
Douglas Gregora868bbd2009-04-21 22:25:48 +00004587
Douglas Gregorab443b92011-08-20 04:39:52 +00004588 static bool visit(Module &M, void *UserData) {
4589 IdentifierLookupVisitor *This
4590 = static_cast<IdentifierLookupVisitor *>(UserData);
4591
4592 ASTIdentifierLookupTable *IdTable
4593 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
4594 if (!IdTable)
4595 return false;
4596
4597 std::pair<const char*, unsigned> Key(This->Name.begin(),
4598 This->Name.size());
4599 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
4600 if (Pos == IdTable->end())
4601 return false;
4602
4603 // Dereferencing the iterator has the effect of building the
4604 // IdentifierInfo node and populating it with the various
4605 // declarations it needs.
4606 This->Found = *Pos;
4607 return true;
4608 }
4609
4610 // \brief Retrieve the identifier info found within the module
4611 // files.
4612 IdentifierInfo *getIdentifierInfo() const { return Found; }
4613 };
4614}
4615
4616IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Douglas Gregorab443b92011-08-20 04:39:52 +00004617 IdentifierLookupVisitor Visitor(StringRef(NameStart, NameEnd - NameStart));
4618 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
4619 return Visitor.getIdentifierInfo();
Douglas Gregora868bbd2009-04-21 22:25:48 +00004620}
4621
Douglas Gregor57756ea2010-10-14 22:11:03 +00004622namespace clang {
4623 /// \brief An identifier-lookup iterator that enumerates all of the
4624 /// identifiers stored within a set of AST files.
4625 class ASTIdentifierIterator : public IdentifierIterator {
4626 /// \brief The AST reader whose identifiers are being enumerated.
4627 const ASTReader &Reader;
4628
4629 /// \brief The current index into the chain of AST files stored in
4630 /// the AST reader.
4631 unsigned Index;
4632
4633 /// \brief The current position within the identifier lookup table
4634 /// of the current AST file.
4635 ASTIdentifierLookupTable::key_iterator Current;
4636
4637 /// \brief The end position within the identifier lookup table of
4638 /// the current AST file.
4639 ASTIdentifierLookupTable::key_iterator End;
4640
4641 public:
4642 explicit ASTIdentifierIterator(const ASTReader &Reader);
4643
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004644 virtual StringRef Next();
Douglas Gregor57756ea2010-10-14 22:11:03 +00004645 };
4646}
4647
4648ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00004649 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
Douglas Gregor57756ea2010-10-14 22:11:03 +00004650 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00004651 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
Douglas Gregor57756ea2010-10-14 22:11:03 +00004652 Current = IdTable->key_begin();
4653 End = IdTable->key_end();
4654}
4655
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004656StringRef ASTIdentifierIterator::Next() {
Douglas Gregor57756ea2010-10-14 22:11:03 +00004657 while (Current == End) {
4658 // If we have exhausted all of our AST files, we're done.
4659 if (Index == 0)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004660 return StringRef();
Douglas Gregor57756ea2010-10-14 22:11:03 +00004661
4662 --Index;
4663 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00004664 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
4665 IdentifierLookupTable;
Douglas Gregor57756ea2010-10-14 22:11:03 +00004666 Current = IdTable->key_begin();
4667 End = IdTable->key_end();
4668 }
4669
4670 // We have any identifiers remaining in the current AST file; return
4671 // the next one.
4672 std::pair<const char*, unsigned> Key = *Current;
4673 ++Current;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004674 return StringRef(Key.first, Key.second);
Douglas Gregor57756ea2010-10-14 22:11:03 +00004675}
4676
4677IdentifierIterator *ASTReader::getIdentifiers() const {
4678 return new ASTIdentifierIterator(*this);
4679}
4680
Mike Stump11289f42009-09-09 15:08:12 +00004681std::pair<ObjCMethodList, ObjCMethodList>
Sebastian Redl2c499f62010-08-18 23:56:43 +00004682ASTReader::ReadMethodPool(Selector Sel) {
Sebastian Redlada023c2010-08-04 20:40:17 +00004683 // Find this selector in a hash table. We want to find the most recent entry.
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00004684 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
4685 Module &F = *(*I);
Sebastian Redlada023c2010-08-04 20:40:17 +00004686 if (!F.SelectorLookupTable)
4687 continue;
Douglas Gregorc78d3462009-04-24 21:10:55 +00004688
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004689 ASTSelectorLookupTable *PoolTable
4690 = (ASTSelectorLookupTable*)F.SelectorLookupTable;
4691 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
Sebastian Redlada023c2010-08-04 20:40:17 +00004692 if (Pos != PoolTable->end()) {
4693 ++NumSelectorsRead;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004694 // FIXME: Not quite happy with the statistics here. We probably should
4695 // disable this tracking when called via LoadSelector.
4696 // Also, should entries without methods count as misses?
4697 ++NumMethodPoolEntriesRead;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004698 ASTSelectorLookupTrait::data_type Data = *Pos;
Sebastian Redlada023c2010-08-04 20:40:17 +00004699 if (DeserializationListener)
4700 DeserializationListener->SelectorRead(Data.ID, Sel);
4701 return std::make_pair(Data.Instance, Data.Factory);
4702 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00004703 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00004704
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004705 ++NumMethodPoolMisses;
Sebastian Redlada023c2010-08-04 20:40:17 +00004706 return std::pair<ObjCMethodList, ObjCMethodList>();
Douglas Gregorc78d3462009-04-24 21:10:55 +00004707}
4708
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004709void ASTReader::ReadKnownNamespaces(
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004710 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004711 Namespaces.clear();
4712
4713 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
4714 if (NamespaceDecl *Namespace
4715 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
4716 Namespaces.push_back(Namespace);
4717 }
4718}
4719
Douglas Gregoreb08bd42011-07-27 20:58:46 +00004720void ASTReader::ReadTentativeDefinitions(
4721 SmallVectorImpl<VarDecl *> &TentativeDefs) {
4722 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
4723 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
4724 if (Var)
4725 TentativeDefs.push_back(Var);
4726 }
4727 TentativeDefinitions.clear();
4728}
4729
Douglas Gregora94a1542011-07-27 21:45:57 +00004730void ASTReader::ReadUnusedFileScopedDecls(
4731 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
4732 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
4733 DeclaratorDecl *D
4734 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
4735 if (D)
4736 Decls.push_back(D);
4737 }
4738 UnusedFileScopedDecls.clear();
4739}
4740
Douglas Gregorbae31202011-07-27 21:57:17 +00004741void ASTReader::ReadDelegatingConstructors(
4742 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
4743 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
4744 CXXConstructorDecl *D
4745 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
4746 if (D)
4747 Decls.push_back(D);
4748 }
4749 DelegatingCtorDecls.clear();
4750}
4751
Douglas Gregorb7098a32011-07-28 00:39:29 +00004752void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
4753 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
4754 TypedefNameDecl *D
4755 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
4756 if (D)
4757 Decls.push_back(D);
4758 }
4759 ExtVectorDecls.clear();
4760}
4761
Douglas Gregor32002192011-07-28 00:53:40 +00004762void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
4763 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
4764 CXXRecordDecl *D
4765 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
4766 if (D)
4767 Decls.push_back(D);
4768 }
4769 DynamicClasses.clear();
4770}
4771
Douglas Gregordc5c9582011-07-28 14:20:37 +00004772void
4773ASTReader::ReadLocallyScopedExternalDecls(SmallVectorImpl<NamedDecl *> &Decls) {
4774 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
4775 NamedDecl *D
4776 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
4777 if (D)
4778 Decls.push_back(D);
4779 }
4780 LocallyScopedExternalDecls.clear();
4781}
4782
Douglas Gregor72e357f2011-07-28 14:54:22 +00004783void ASTReader::ReadReferencedSelectors(
4784 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
4785 if (ReferencedSelectorsData.empty())
4786 return;
4787
4788 // If there are @selector references added them to its pool. This is for
4789 // implementation of -Wselector.
4790 unsigned int DataSize = ReferencedSelectorsData.size()-1;
4791 unsigned I = 0;
4792 while (I < DataSize) {
4793 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
4794 SourceLocation SelLoc
4795 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
4796 Sels.push_back(std::make_pair(Sel, SelLoc));
4797 }
4798 ReferencedSelectorsData.clear();
4799}
4800
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00004801void ASTReader::ReadWeakUndeclaredIdentifiers(
4802 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
4803 if (WeakUndeclaredIdentifiers.empty())
4804 return;
4805
4806 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
4807 IdentifierInfo *WeakId
4808 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
4809 IdentifierInfo *AliasId
4810 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
4811 SourceLocation Loc
4812 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
4813 bool Used = WeakUndeclaredIdentifiers[I++];
4814 WeakInfo WI(AliasId, Loc);
4815 WI.setUsed(Used);
4816 WeakIDs.push_back(std::make_pair(WeakId, WI));
4817 }
4818 WeakUndeclaredIdentifiers.clear();
4819}
4820
Douglas Gregor4daf6a32011-07-28 19:11:31 +00004821void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
4822 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
4823 ExternalVTableUse VT;
4824 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
4825 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
4826 VT.DefinitionRequired = VTableUses[Idx++];
4827 VTables.push_back(VT);
4828 }
4829
4830 VTableUses.clear();
4831}
4832
Douglas Gregore39f97c2011-07-28 19:49:54 +00004833void ASTReader::ReadPendingInstantiations(
4834 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
4835 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
4836 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
4837 SourceLocation Loc
4838 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
4839 Pending.push_back(std::make_pair(D, Loc));
4840 }
4841 PendingInstantiations.clear();
4842}
4843
Sebastian Redl2c499f62010-08-18 23:56:43 +00004844void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00004845 // It would be complicated to avoid reading the methods anyway. So don't.
4846 ReadMethodPool(Sel);
4847}
4848
Douglas Gregora3e41532011-07-28 20:55:49 +00004849void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00004850 assert(ID && "Non-zero identifier ID required");
Douglas Gregor6f00bf82009-04-28 21:53:25 +00004851 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor0e149972009-04-25 19:10:14 +00004852 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlff4a2952010-07-23 23:49:55 +00004853 if (DeserializationListener)
4854 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregora868bbd2009-04-21 22:25:48 +00004855}
4856
Douglas Gregor1342e842009-07-06 18:54:52 +00004857/// \brief Set the globally-visible declarations associated with the given
4858/// identifier.
4859///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004860/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump11289f42009-09-09 15:08:12 +00004861/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregor1342e842009-07-06 18:54:52 +00004862/// them.
4863///
4864/// \param II an IdentifierInfo that refers to one or more globally-visible
4865/// declarations.
4866///
4867/// \param DeclIDs the set of declaration IDs with the name @p II that are
4868/// visible at global scope.
4869///
4870/// \param Nonrecursive should be true to indicate that the caller knows that
4871/// this call is non-recursive, and therefore the globally-visible declarations
4872/// will not be placed onto the pending queue.
Mike Stump11289f42009-09-09 15:08:12 +00004873void
Sebastian Redl2c499f62010-08-18 23:56:43 +00004874ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004875 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregor1342e842009-07-06 18:54:52 +00004876 bool Nonrecursive) {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004877 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregor1342e842009-07-06 18:54:52 +00004878 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
4879 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
4880 PII.II = II;
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00004881 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregor1342e842009-07-06 18:54:52 +00004882 return;
4883 }
Mike Stump11289f42009-09-09 15:08:12 +00004884
Douglas Gregor1342e842009-07-06 18:54:52 +00004885 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
4886 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
4887 if (SemaObj) {
Douglas Gregor6fd55e02010-08-13 03:15:25 +00004888 if (SemaObj->TUScope) {
4889 // Introduce this declaration into the translation-unit scope
4890 // and add it to the declaration chain for this identifier, so
4891 // that (unqualified) name lookup will find it.
John McCall48871652010-08-21 09:40:31 +00004892 SemaObj->TUScope->AddDecl(D);
Douglas Gregor6fd55e02010-08-13 03:15:25 +00004893 }
Douglas Gregor2fb99df2010-09-24 23:29:12 +00004894 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
Douglas Gregor1342e842009-07-06 18:54:52 +00004895 } else {
4896 // Queue this declaration so that it will be added to the
4897 // translation unit scope and identifier's declaration chain
4898 // once a Sema object is known.
4899 PreloadedDecls.push_back(D);
4900 }
4901 }
4902}
4903
Douglas Gregora3e41532011-07-28 20:55:49 +00004904IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004905 if (ID == 0)
4906 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00004907
Sebastian Redlc713b962010-07-21 00:46:22 +00004908 if (IdentifiersLoaded.empty()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004909 Error("no identifier table in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004910 return 0;
4911 }
Mike Stump11289f42009-09-09 15:08:12 +00004912
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00004913 assert(PP && "Forgot to set Preprocessor ?");
Sebastian Redlc713b962010-07-21 00:46:22 +00004914 ID -= 1;
4915 if (!IdentifiersLoaded[ID]) {
Douglas Gregor19d26352011-07-20 00:59:32 +00004916 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
4917 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00004918 Module *M = I->second;
4919 unsigned Index = ID - M->BaseIdentifierID;
4920 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
Douglas Gregor5287b4e2009-04-25 21:04:17 +00004921
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004922 // All of the strings in the AST file are preceded by a 16-bit length.
4923 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenekca42a512009-10-23 04:45:31 +00004924 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
4925 // unsigned integers. This is important to avoid integer overflow when
4926 // we cast them to 'unsigned'.
Ted Kremenek49c52322009-10-23 03:57:22 +00004927 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregorab4df582009-04-28 20:01:51 +00004928 unsigned StrLen = (((unsigned) StrLenPtr[0])
4929 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redlc713b962010-07-21 00:46:22 +00004930 IdentifiersLoaded[ID]
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004931 = &PP->getIdentifierTable().get(StringRef(Str, StrLen));
Sebastian Redlff4a2952010-07-23 23:49:55 +00004932 if (DeserializationListener)
4933 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004934 }
Mike Stump11289f42009-09-09 15:08:12 +00004935
Sebastian Redlc713b962010-07-21 00:46:22 +00004936 return IdentifiersLoaded[ID];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004937}
4938
Douglas Gregora3e41532011-07-28 20:55:49 +00004939IdentifierInfo *ASTReader::getLocalIdentifier(Module &M, unsigned LocalID) {
4940 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
4941}
4942
4943IdentifierID ASTReader::getGlobalIdentifierID(Module &M, unsigned LocalID) {
Douglas Gregor1ab036c2011-08-03 21:49:18 +00004944 if (LocalID < NUM_PREDEF_IDENT_IDS)
4945 return LocalID;
4946
4947 ContinuousRangeMap<uint32_t, int, 2>::iterator I
4948 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
4949 assert(I != M.IdentifierRemap.end()
4950 && "Invalid index into identifier index remap");
4951
4952 return LocalID + I->second;
Douglas Gregora3e41532011-07-28 20:55:49 +00004953}
4954
Douglas Gregor925296b2011-07-19 16:10:42 +00004955bool ASTReader::ReadSLocEntry(int ID) {
Douglas Gregor49f754f2011-04-20 00:21:03 +00004956 return ReadSLocEntryRecord(ID) != Success;
Douglas Gregor258ae542009-04-27 06:38:32 +00004957}
4958
Douglas Gregor074fdc52011-07-28 21:16:51 +00004959Selector ASTReader::getLocalSelector(Module &M, unsigned LocalID) {
4960 return DecodeSelector(getGlobalSelectorID(M, LocalID));
4961}
4962
4963Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
Steve Naroff2ddea052009-04-23 10:39:46 +00004964 if (ID == 0)
4965 return Selector();
Mike Stump11289f42009-09-09 15:08:12 +00004966
Sebastian Redlada023c2010-08-04 20:40:17 +00004967 if (ID > SelectorsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004968 Error("selector ID out of range in AST file");
Steve Naroff2ddea052009-04-23 10:39:46 +00004969 return Selector();
4970 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00004971
Sebastian Redlada023c2010-08-04 20:40:17 +00004972 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004973 // Load this selector from the selector table.
Douglas Gregor2262d282011-07-20 01:10:58 +00004974 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
4975 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00004976 Module &M = *I->second;
4977 ASTSelectorLookupTrait Trait(*this, M);
Douglas Gregor8f364fb2011-08-03 23:28:44 +00004978 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
Douglas Gregor2262d282011-07-20 01:10:58 +00004979 SelectorsLoaded[ID - 1] =
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00004980 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
Douglas Gregor2262d282011-07-20 01:10:58 +00004981 if (DeserializationListener)
4982 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
Douglas Gregor95c13f52009-04-25 17:48:32 +00004983 }
4984
Sebastian Redlada023c2010-08-04 20:40:17 +00004985 return SelectorsLoaded[ID - 1];
Steve Naroff2ddea052009-04-23 10:39:46 +00004986}
4987
Douglas Gregor3f8f04f2011-07-28 14:41:43 +00004988Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
Douglas Gregord720daf2010-04-06 17:30:22 +00004989 return DecodeSelector(ID);
4990}
4991
Sebastian Redl2c499f62010-08-18 23:56:43 +00004992uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redlada023c2010-08-04 20:40:17 +00004993 // ID 0 (the null selector) is considered an external selector.
4994 return getTotalNumSelectors() + 1;
Douglas Gregord720daf2010-04-06 17:30:22 +00004995}
4996
Douglas Gregor8f364fb2011-08-03 23:28:44 +00004997serialization::SelectorID
4998ASTReader::getGlobalSelectorID(Module &M, unsigned LocalID) const {
4999 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
5000 return LocalID;
5001
5002 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5003 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
5004 assert(I != M.SelectorRemap.end()
5005 && "Invalid index into identifier index remap");
5006
5007 return LocalID + I->second;
Douglas Gregor3f8f04f2011-07-28 14:41:43 +00005008}
5009
Mike Stump11289f42009-09-09 15:08:12 +00005010DeclarationName
Douglas Gregora6895d82011-07-22 16:00:58 +00005011ASTReader::ReadDeclarationName(Module &F,
Douglas Gregor903b7e92011-07-22 00:38:23 +00005012 const RecordData &Record, unsigned &Idx) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005013 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
5014 switch (Kind) {
5015 case DeclarationName::Identifier:
Douglas Gregora3e41532011-07-28 20:55:49 +00005016 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005017
5018 case DeclarationName::ObjCZeroArgSelector:
5019 case DeclarationName::ObjCOneArgSelector:
5020 case DeclarationName::ObjCMultiArgSelector:
Douglas Gregor074fdc52011-07-28 21:16:51 +00005021 return DeclarationName(ReadSelector(F, Record, Idx));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005022
5023 case DeclarationName::CXXConstructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00005024 return Context->DeclarationNames.getCXXConstructorName(
Douglas Gregor903b7e92011-07-22 00:38:23 +00005025 Context->getCanonicalType(readType(F, Record, Idx)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005026
5027 case DeclarationName::CXXDestructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00005028 return Context->DeclarationNames.getCXXDestructorName(
Douglas Gregor903b7e92011-07-22 00:38:23 +00005029 Context->getCanonicalType(readType(F, Record, Idx)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005030
5031 case DeclarationName::CXXConversionFunctionName:
Chris Lattner8575daa2009-04-27 21:45:14 +00005032 return Context->DeclarationNames.getCXXConversionFunctionName(
Douglas Gregor903b7e92011-07-22 00:38:23 +00005033 Context->getCanonicalType(readType(F, Record, Idx)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005034
5035 case DeclarationName::CXXOperatorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00005036 return Context->DeclarationNames.getCXXOperatorName(
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005037 (OverloadedOperatorKind)Record[Idx++]);
5038
Alexis Hunt3d221f22009-11-29 07:34:05 +00005039 case DeclarationName::CXXLiteralOperatorName:
5040 return Context->DeclarationNames.getCXXLiteralOperatorName(
Douglas Gregora3e41532011-07-28 20:55:49 +00005041 GetIdentifierInfo(F, Record, Idx));
Alexis Hunt3d221f22009-11-29 07:34:05 +00005042
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005043 case DeclarationName::CXXUsingDirective:
5044 return DeclarationName::getUsingDirectiveName();
5045 }
5046
5047 // Required to silence GCC warning
5048 return DeclarationName();
5049}
Douglas Gregor55abb232009-04-10 20:39:37 +00005050
Douglas Gregora6895d82011-07-22 16:00:58 +00005051void ASTReader::ReadDeclarationNameLoc(Module &F,
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005052 DeclarationNameLoc &DNLoc,
5053 DeclarationName Name,
5054 const RecordData &Record, unsigned &Idx) {
5055 switch (Name.getNameKind()) {
5056 case DeclarationName::CXXConstructorName:
5057 case DeclarationName::CXXDestructorName:
5058 case DeclarationName::CXXConversionFunctionName:
5059 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
5060 break;
5061
5062 case DeclarationName::CXXOperatorName:
5063 DNLoc.CXXOperatorName.BeginOpNameLoc
5064 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5065 DNLoc.CXXOperatorName.EndOpNameLoc
5066 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5067 break;
5068
5069 case DeclarationName::CXXLiteralOperatorName:
5070 DNLoc.CXXLiteralOperatorName.OpNameLoc
5071 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5072 break;
5073
5074 case DeclarationName::Identifier:
5075 case DeclarationName::ObjCZeroArgSelector:
5076 case DeclarationName::ObjCOneArgSelector:
5077 case DeclarationName::ObjCMultiArgSelector:
5078 case DeclarationName::CXXUsingDirective:
5079 break;
5080 }
5081}
5082
Douglas Gregora6895d82011-07-22 16:00:58 +00005083void ASTReader::ReadDeclarationNameInfo(Module &F,
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005084 DeclarationNameInfo &NameInfo,
5085 const RecordData &Record, unsigned &Idx) {
Douglas Gregor903b7e92011-07-22 00:38:23 +00005086 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005087 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
5088 DeclarationNameLoc DNLoc;
5089 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
5090 NameInfo.setInfo(DNLoc);
5091}
5092
Douglas Gregora6895d82011-07-22 16:00:58 +00005093void ASTReader::ReadQualifierInfo(Module &F, QualifierInfo &Info,
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005094 const RecordData &Record, unsigned &Idx) {
Douglas Gregor14454802011-02-25 02:25:35 +00005095 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005096 unsigned NumTPLists = Record[Idx++];
5097 Info.NumTemplParamLists = NumTPLists;
5098 if (NumTPLists) {
5099 Info.TemplParamLists = new (*Context) TemplateParameterList*[NumTPLists];
5100 for (unsigned i=0; i != NumTPLists; ++i)
5101 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
5102 }
5103}
5104
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005105TemplateName
Douglas Gregora6895d82011-07-22 16:00:58 +00005106ASTReader::ReadTemplateName(Module &F, const RecordData &Record,
Douglas Gregor5590be02011-01-15 06:45:20 +00005107 unsigned &Idx) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005108 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005109 switch (Kind) {
5110 case TemplateName::Template:
Douglas Gregor7fb09192011-07-21 22:35:25 +00005111 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005112
5113 case TemplateName::OverloadedTemplate: {
5114 unsigned size = Record[Idx++];
5115 UnresolvedSet<8> Decls;
5116 while (size--)
Douglas Gregor7fb09192011-07-21 22:35:25 +00005117 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005118
5119 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
5120 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005121
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005122 case TemplateName::QualifiedTemplate: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00005123 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005124 bool hasTemplKeyword = Record[Idx++];
Douglas Gregor7fb09192011-07-21 22:35:25 +00005125 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005126 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
5127 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005128
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005129 case TemplateName::DependentTemplate: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00005130 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005131 if (Record[Idx++]) // isIdentifier
5132 return Context->getDependentTemplateName(NNS,
Douglas Gregora3e41532011-07-28 20:55:49 +00005133 GetIdentifierInfo(F, Record,
5134 Idx));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005135 return Context->getDependentTemplateName(NNS,
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00005136 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005137 }
John McCalld9dfe3a2011-06-30 08:33:18 +00005138
5139 case TemplateName::SubstTemplateTemplateParm: {
5140 TemplateTemplateParmDecl *param
Douglas Gregor7fb09192011-07-21 22:35:25 +00005141 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
John McCalld9dfe3a2011-06-30 08:33:18 +00005142 if (!param) return TemplateName();
5143 TemplateName replacement = ReadTemplateName(F, Record, Idx);
5144 return Context->getSubstTemplateTemplateParm(param, replacement);
5145 }
Douglas Gregor5590be02011-01-15 06:45:20 +00005146
5147 case TemplateName::SubstTemplateTemplateParmPack: {
5148 TemplateTemplateParmDecl *Param
Douglas Gregor7fb09192011-07-21 22:35:25 +00005149 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
Douglas Gregor5590be02011-01-15 06:45:20 +00005150 if (!Param)
5151 return TemplateName();
5152
5153 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
5154 if (ArgPack.getKind() != TemplateArgument::Pack)
5155 return TemplateName();
5156
5157 return Context->getSubstTemplateTemplateParmPack(Param, ArgPack);
5158 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005159 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005160
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005161 assert(0 && "Unhandled template name kind!");
5162 return TemplateName();
5163}
5164
5165TemplateArgument
Douglas Gregora6895d82011-07-22 16:00:58 +00005166ASTReader::ReadTemplateArgument(Module &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00005167 const RecordData &Record, unsigned &Idx) {
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005168 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
5169 switch (Kind) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005170 case TemplateArgument::Null:
5171 return TemplateArgument();
5172 case TemplateArgument::Type:
Douglas Gregor903b7e92011-07-22 00:38:23 +00005173 return TemplateArgument(readType(F, Record, Idx));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005174 case TemplateArgument::Declaration:
Douglas Gregor7fb09192011-07-21 22:35:25 +00005175 return TemplateArgument(ReadDecl(F, Record, Idx));
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +00005176 case TemplateArgument::Integral: {
5177 llvm::APSInt Value = ReadAPSInt(Record, Idx);
Douglas Gregor903b7e92011-07-22 00:38:23 +00005178 QualType T = readType(F, Record, Idx);
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +00005179 return TemplateArgument(Value, T);
5180 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005181 case TemplateArgument::Template:
Douglas Gregor5590be02011-01-15 06:45:20 +00005182 return TemplateArgument(ReadTemplateName(F, Record, Idx));
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005183 case TemplateArgument::TemplateExpansion: {
Douglas Gregor5590be02011-01-15 06:45:20 +00005184 TemplateName Name = ReadTemplateName(F, Record, Idx);
Douglas Gregore1d60df2011-01-14 23:41:42 +00005185 llvm::Optional<unsigned> NumTemplateExpansions;
5186 if (unsigned NumExpansions = Record[Idx++])
5187 NumTemplateExpansions = NumExpansions - 1;
5188 return TemplateArgument(Name, NumTemplateExpansions);
Douglas Gregoreb29d182011-01-05 17:40:24 +00005189 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005190 case TemplateArgument::Expression:
Sebastian Redl2c373b92010-10-05 15:59:54 +00005191 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005192 case TemplateArgument::Pack: {
5193 unsigned NumArgs = Record[Idx++];
Douglas Gregor1ccc8412010-11-07 23:05:16 +00005194 TemplateArgument *Args = new (*Context) TemplateArgument[NumArgs];
5195 for (unsigned I = 0; I != NumArgs; ++I)
5196 Args[I] = ReadTemplateArgument(F, Record, Idx);
5197 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005198 }
5199 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005200
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005201 assert(0 && "Unhandled template argument kind!");
5202 return TemplateArgument();
5203}
5204
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005205TemplateParameterList *
Douglas Gregora6895d82011-07-22 16:00:58 +00005206ASTReader::ReadTemplateParameterList(Module &F,
Sebastian Redl2c373b92010-10-05 15:59:54 +00005207 const RecordData &Record, unsigned &Idx) {
5208 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
5209 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
5210 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005211
5212 unsigned NumParams = Record[Idx++];
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005213 SmallVector<NamedDecl *, 16> Params;
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005214 Params.reserve(NumParams);
5215 while (NumParams--)
Douglas Gregor7fb09192011-07-21 22:35:25 +00005216 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005217
5218 TemplateParameterList* TemplateParams =
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005219 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
5220 Params.data(), Params.size(), RAngleLoc);
5221 return TemplateParams;
5222}
5223
5224void
Sebastian Redl2c499f62010-08-18 23:56:43 +00005225ASTReader::
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005226ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs,
Douglas Gregora6895d82011-07-22 16:00:58 +00005227 Module &F, const RecordData &Record,
Sebastian Redl2c373b92010-10-05 15:59:54 +00005228 unsigned &Idx) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005229 unsigned NumTemplateArgs = Record[Idx++];
5230 TemplArgs.reserve(NumTemplateArgs);
5231 while (NumTemplateArgs--)
Sebastian Redl2c373b92010-10-05 15:59:54 +00005232 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005233}
5234
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005235/// \brief Read a UnresolvedSet structure.
Douglas Gregora6895d82011-07-22 16:00:58 +00005236void ASTReader::ReadUnresolvedSet(Module &F, UnresolvedSetImpl &Set,
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005237 const RecordData &Record, unsigned &Idx) {
5238 unsigned NumDecls = Record[Idx++];
5239 while (NumDecls--) {
Douglas Gregor7fb09192011-07-21 22:35:25 +00005240 NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx);
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005241 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
5242 Set.addDecl(D, AS);
5243 }
5244}
5245
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005246CXXBaseSpecifier
Douglas Gregora6895d82011-07-22 16:00:58 +00005247ASTReader::ReadCXXBaseSpecifier(Module &F,
Nick Lewycky19b9f952010-07-26 16:56:01 +00005248 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005249 bool isVirtual = static_cast<bool>(Record[Idx++]);
5250 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
5251 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redl08905022011-02-05 19:23:19 +00005252 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00005253 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
5254 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor752a5952011-01-03 22:36:02 +00005255 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redl08905022011-02-05 19:23:19 +00005256 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
Douglas Gregor752a5952011-01-03 22:36:02 +00005257 EllipsisLoc);
Sebastian Redl08905022011-02-05 19:23:19 +00005258 Result.setInheritConstructors(inheritConstructors);
5259 return Result;
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005260}
5261
Alexis Hunt1d792652011-01-08 20:30:50 +00005262std::pair<CXXCtorInitializer **, unsigned>
Douglas Gregora6895d82011-07-22 16:00:58 +00005263ASTReader::ReadCXXCtorInitializers(Module &F, const RecordData &Record,
Alexis Hunt1d792652011-01-08 20:30:50 +00005264 unsigned &Idx) {
5265 CXXCtorInitializer **CtorInitializers = 0;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005266 unsigned NumInitializers = Record[Idx++];
5267 if (NumInitializers) {
5268 ASTContext &C = *getContext();
5269
Alexis Hunt1d792652011-01-08 20:30:50 +00005270 CtorInitializers
5271 = new (C) CXXCtorInitializer*[NumInitializers];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005272 for (unsigned i=0; i != NumInitializers; ++i) {
5273 TypeSourceInfo *BaseClassInfo = 0;
5274 bool IsBaseVirtual = false;
5275 FieldDecl *Member = 0;
Francois Pichetd583da02010-12-04 09:14:42 +00005276 IndirectFieldDecl *IndirectMember = 0;
Alexis Hunt37a477f2011-05-04 01:19:08 +00005277 CXXConstructorDecl *Target = 0;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005278
Alexis Hunt37a477f2011-05-04 01:19:08 +00005279 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
5280 switch (Type) {
5281 case CTOR_INITIALIZER_BASE:
Sebastian Redl2c373b92010-10-05 15:59:54 +00005282 BaseClassInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005283 IsBaseVirtual = Record[Idx++];
Alexis Hunt37a477f2011-05-04 01:19:08 +00005284 break;
5285
5286 case CTOR_INITIALIZER_DELEGATING:
Douglas Gregor7fb09192011-07-21 22:35:25 +00005287 Target = ReadDeclAs<CXXConstructorDecl>(F, Record, Idx);
Alexis Hunt37a477f2011-05-04 01:19:08 +00005288 break;
5289
5290 case CTOR_INITIALIZER_MEMBER:
Douglas Gregor7fb09192011-07-21 22:35:25 +00005291 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
Alexis Hunt37a477f2011-05-04 01:19:08 +00005292 break;
5293
5294 case CTOR_INITIALIZER_INDIRECT_MEMBER:
Douglas Gregor7fb09192011-07-21 22:35:25 +00005295 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
Alexis Hunt37a477f2011-05-04 01:19:08 +00005296 break;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005297 }
Alexis Hunt37a477f2011-05-04 01:19:08 +00005298
Douglas Gregor44e7df62011-01-04 00:32:56 +00005299 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redl2c373b92010-10-05 15:59:54 +00005300 Expr *Init = ReadExpr(F);
Sebastian Redl2c373b92010-10-05 15:59:54 +00005301 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
5302 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005303 bool IsWritten = Record[Idx++];
5304 unsigned SourceOrderOrNumArrayIndices;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005305 SmallVector<VarDecl *, 8> Indices;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005306 if (IsWritten) {
5307 SourceOrderOrNumArrayIndices = Record[Idx++];
5308 } else {
5309 SourceOrderOrNumArrayIndices = Record[Idx++];
5310 Indices.reserve(SourceOrderOrNumArrayIndices);
5311 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
Douglas Gregor7fb09192011-07-21 22:35:25 +00005312 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005313 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005314
Alexis Hunt1d792652011-01-08 20:30:50 +00005315 CXXCtorInitializer *BOMInit;
Alexis Hunt37a477f2011-05-04 01:19:08 +00005316 if (Type == CTOR_INITIALIZER_BASE) {
Alexis Hunt1d792652011-01-08 20:30:50 +00005317 BOMInit = new (C) CXXCtorInitializer(C, BaseClassInfo, IsBaseVirtual,
5318 LParenLoc, Init, RParenLoc,
5319 MemberOrEllipsisLoc);
Alexis Hunt37a477f2011-05-04 01:19:08 +00005320 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
5321 BOMInit = new (C) CXXCtorInitializer(C, MemberOrEllipsisLoc, LParenLoc,
5322 Target, Init, RParenLoc);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005323 } else if (IsWritten) {
Francois Pichetd583da02010-12-04 09:14:42 +00005324 if (Member)
Alexis Hunt1d792652011-01-08 20:30:50 +00005325 BOMInit = new (C) CXXCtorInitializer(C, Member, MemberOrEllipsisLoc,
5326 LParenLoc, Init, RParenLoc);
Francois Pichetd583da02010-12-04 09:14:42 +00005327 else
Alexis Hunt1d792652011-01-08 20:30:50 +00005328 BOMInit = new (C) CXXCtorInitializer(C, IndirectMember,
5329 MemberOrEllipsisLoc, LParenLoc,
5330 Init, RParenLoc);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005331 } else {
Alexis Hunt1d792652011-01-08 20:30:50 +00005332 BOMInit = CXXCtorInitializer::Create(C, Member, MemberOrEllipsisLoc,
5333 LParenLoc, Init, RParenLoc,
5334 Indices.data(), Indices.size());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005335 }
5336
Argyrios Kyrtzidisd05f3e32010-09-06 19:04:27 +00005337 if (IsWritten)
5338 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Alexis Hunt1d792652011-01-08 20:30:50 +00005339 CtorInitializers[i] = BOMInit;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005340 }
5341 }
5342
Alexis Hunt1d792652011-01-08 20:30:50 +00005343 return std::make_pair(CtorInitializers, NumInitializers);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005344}
5345
Chris Lattnerca025db2010-05-07 21:43:38 +00005346NestedNameSpecifier *
Douglas Gregora6895d82011-07-22 16:00:58 +00005347ASTReader::ReadNestedNameSpecifier(Module &F,
Douglas Gregor7fb09192011-07-21 22:35:25 +00005348 const RecordData &Record, unsigned &Idx) {
Chris Lattnerca025db2010-05-07 21:43:38 +00005349 unsigned N = Record[Idx++];
5350 NestedNameSpecifier *NNS = 0, *Prev = 0;
5351 for (unsigned I = 0; I != N; ++I) {
5352 NestedNameSpecifier::SpecifierKind Kind
5353 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
5354 switch (Kind) {
5355 case NestedNameSpecifier::Identifier: {
Douglas Gregora3e41532011-07-28 20:55:49 +00005356 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Chris Lattnerca025db2010-05-07 21:43:38 +00005357 NNS = NestedNameSpecifier::Create(*Context, Prev, II);
5358 break;
5359 }
5360
5361 case NestedNameSpecifier::Namespace: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00005362 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Chris Lattnerca025db2010-05-07 21:43:38 +00005363 NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
5364 break;
5365 }
5366
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005367 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00005368 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005369 NNS = NestedNameSpecifier::Create(*Context, Prev, Alias);
5370 break;
5371 }
5372
Chris Lattnerca025db2010-05-07 21:43:38 +00005373 case NestedNameSpecifier::TypeSpec:
5374 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregor903b7e92011-07-22 00:38:23 +00005375 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
Douglas Gregor0cdc8322010-12-10 17:03:06 +00005376 if (!T)
5377 return 0;
5378
Chris Lattnerca025db2010-05-07 21:43:38 +00005379 bool Template = Record[Idx++];
5380 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
5381 break;
5382 }
5383
5384 case NestedNameSpecifier::Global: {
5385 NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
5386 // No associated value, and there can't be a prefix.
5387 break;
5388 }
Chris Lattnerca025db2010-05-07 21:43:38 +00005389 }
Argyrios Kyrtzidisad65c692010-07-07 15:46:30 +00005390 Prev = NNS;
Chris Lattnerca025db2010-05-07 21:43:38 +00005391 }
5392 return NNS;
5393}
5394
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005395NestedNameSpecifierLoc
Douglas Gregora6895d82011-07-22 16:00:58 +00005396ASTReader::ReadNestedNameSpecifierLoc(Module &F, const RecordData &Record,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005397 unsigned &Idx) {
5398 unsigned N = Record[Idx++];
Douglas Gregor9b272512011-02-28 23:58:31 +00005399 NestedNameSpecifierLocBuilder Builder;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005400 for (unsigned I = 0; I != N; ++I) {
5401 NestedNameSpecifier::SpecifierKind Kind
5402 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
5403 switch (Kind) {
5404 case NestedNameSpecifier::Identifier: {
Douglas Gregora3e41532011-07-28 20:55:49 +00005405 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005406 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00005407 Builder.Extend(*Context, II, Range.getBegin(), Range.getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005408 break;
5409 }
5410
5411 case NestedNameSpecifier::Namespace: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00005412 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005413 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00005414 Builder.Extend(*Context, NS, Range.getBegin(), Range.getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005415 break;
5416 }
5417
5418 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00005419 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005420 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00005421 Builder.Extend(*Context, Alias, Range.getBegin(), Range.getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005422 break;
5423 }
5424
5425 case NestedNameSpecifier::TypeSpec:
5426 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005427 bool Template = Record[Idx++];
5428 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
5429 if (!T)
5430 return NestedNameSpecifierLoc();
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005431 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00005432
5433 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
5434 Builder.Extend(*Context,
5435 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
5436 T->getTypeLoc(), ColonColonLoc);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005437 break;
5438 }
5439
5440 case NestedNameSpecifier::Global: {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005441 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00005442 Builder.MakeGlobal(*Context, ColonColonLoc);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005443 break;
5444 }
5445 }
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005446 }
5447
Douglas Gregor9b272512011-02-28 23:58:31 +00005448 return Builder.getWithLocInContext(*Context);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005449}
5450
Chris Lattnerca025db2010-05-07 21:43:38 +00005451SourceRange
Douglas Gregora6895d82011-07-22 16:00:58 +00005452ASTReader::ReadSourceRange(Module &F, const RecordData &Record,
Sebastian Redl2c373b92010-10-05 15:59:54 +00005453 unsigned &Idx) {
5454 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
5455 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar6d3bc082010-06-02 15:47:10 +00005456 return SourceRange(beg, end);
Chris Lattnerca025db2010-05-07 21:43:38 +00005457}
5458
Douglas Gregor1daeb692009-04-13 18:14:40 +00005459/// \brief Read an integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00005460llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00005461 unsigned BitWidth = Record[Idx++];
5462 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
5463 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
5464 Idx += NumWords;
5465 return Result;
5466}
5467
5468/// \brief Read a signed integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00005469llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00005470 bool isUnsigned = Record[Idx++];
5471 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
5472}
5473
Douglas Gregore0a3a512009-04-14 21:55:33 +00005474/// \brief Read a floating-point value
Sebastian Redl2c499f62010-08-18 23:56:43 +00005475llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00005476 return llvm::APFloat(ReadAPInt(Record, Idx));
5477}
5478
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00005479// \brief Read a string
Sebastian Redl2c499f62010-08-18 23:56:43 +00005480std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00005481 unsigned Len = Record[Idx++];
Jay Foad7d0479f2009-05-21 09:52:38 +00005482 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00005483 Idx += Len;
5484 return Result;
5485}
5486
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005487VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
5488 unsigned &Idx) {
5489 unsigned Major = Record[Idx++];
5490 unsigned Minor = Record[Idx++];
5491 unsigned Subminor = Record[Idx++];
5492 if (Minor == 0)
5493 return VersionTuple(Major);
5494 if (Subminor == 0)
5495 return VersionTuple(Major, Minor - 1);
5496 return VersionTuple(Major, Minor - 1, Subminor - 1);
5497}
5498
Douglas Gregora6895d82011-07-22 16:00:58 +00005499CXXTemporary *ASTReader::ReadCXXTemporary(Module &F,
Douglas Gregor7fb09192011-07-21 22:35:25 +00005500 const RecordData &Record,
Chris Lattnercba86142010-05-10 00:25:06 +00005501 unsigned &Idx) {
Douglas Gregor7fb09192011-07-21 22:35:25 +00005502 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
Chris Lattnercba86142010-05-10 00:25:06 +00005503 return CXXTemporary::Create(*Context, Decl);
5504}
5505
Sebastian Redl2c499f62010-08-18 23:56:43 +00005506DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregor92863e42009-04-10 23:10:45 +00005507 return Diag(SourceLocation(), DiagID);
5508}
5509
Sebastian Redl2c499f62010-08-18 23:56:43 +00005510DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00005511 return Diags.Report(Loc, DiagID);
Douglas Gregor55abb232009-04-10 20:39:37 +00005512}
Douglas Gregora9af1d12009-04-17 00:04:06 +00005513
Douglas Gregora868bbd2009-04-21 22:25:48 +00005514/// \brief Retrieve the identifier table associated with the
5515/// preprocessor.
Sebastian Redl2c499f62010-08-18 23:56:43 +00005516IdentifierTable &ASTReader::getIdentifierTable() {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00005517 assert(PP && "Forgot to set Preprocessor ?");
5518 return PP->getIdentifierTable();
Douglas Gregora868bbd2009-04-21 22:25:48 +00005519}
5520
Douglas Gregora9af1d12009-04-17 00:04:06 +00005521/// \brief Record that the given ID maps to the given switch-case
5522/// statement.
Sebastian Redl2c499f62010-08-18 23:56:43 +00005523void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Douglas Gregora9af1d12009-04-17 00:04:06 +00005524 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
5525 SwitchCaseStmts[ID] = SC;
5526}
5527
5528/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00005529SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Douglas Gregora9af1d12009-04-17 00:04:06 +00005530 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
5531 return SwitchCaseStmts[ID];
5532}
Douglas Gregor6cc68a42009-04-17 18:18:49 +00005533
Argyrios Kyrtzidisd9f526f2010-10-28 09:29:32 +00005534void ASTReader::ClearSwitchCaseIDs() {
5535 SwitchCaseStmts.clear();
5536}
5537
Sebastian Redl2c499f62010-08-18 23:56:43 +00005538void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00005539 assert(NumCurrentElementsDeserializing &&
5540 "FinishedDeserializing not paired with StartedDeserializing");
5541 if (NumCurrentElementsDeserializing == 1) {
Douglas Gregor1342e842009-07-06 18:54:52 +00005542 // If any identifiers with corresponding top-level declarations have
5543 // been loaded, load those declarations now.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00005544 while (!PendingIdentifierInfos.empty()) {
5545 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
5546 PendingIdentifierInfos.front().DeclIDs, true);
5547 PendingIdentifierInfos.pop_front();
Douglas Gregor1342e842009-07-06 18:54:52 +00005548 }
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00005549
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00005550 // Ready to load previous declarations of Decls that were delayed.
5551 while (!PendingPreviousDecls.empty()) {
5552 loadAndAttachPreviousDecl(PendingPreviousDecls.front().first,
5553 PendingPreviousDecls.front().second);
5554 PendingPreviousDecls.pop_front();
5555 }
5556
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00005557 // We are not in recursive loading, so it's safe to pass the "interesting"
5558 // decls to the consumer.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00005559 if (Consumer)
5560 PassInterestingDeclsToConsumer();
Argyrios Kyrtzidisad5f95c2010-10-24 17:26:31 +00005561
5562 assert(PendingForwardRefs.size() == 0 &&
5563 "Some forward refs did not get linked to the definition!");
Douglas Gregor1342e842009-07-06 18:54:52 +00005564 }
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00005565 --NumCurrentElementsDeserializing;
Douglas Gregor1342e842009-07-06 18:54:52 +00005566}
Douglas Gregorb473b072010-08-19 00:28:17 +00005567
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005568ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
Douglas Gregorc567ba22011-07-22 16:35:34 +00005569 StringRef isysroot, bool DisableValidation,
Douglas Gregor606c4ac2011-02-05 19:42:43 +00005570 bool DisableStatCache)
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005571 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
5572 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
5573 Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
Jonathan D. Turnerecc27402011-07-28 17:20:23 +00005574 Consumer(0), ModuleMgr(FileMgr.getFileSystemOptions()),
5575 RelocatablePCH(false), isysroot(isysroot),
Douglas Gregor925296b2011-07-19 16:10:42 +00005576 DisableValidation(DisableValidation),
Douglas Gregor606c4ac2011-02-05 19:42:43 +00005577 DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0),
Douglas Gregor925296b2011-07-19 16:10:42 +00005578 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Douglas Gregor606c4ac2011-02-05 19:42:43 +00005579 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
5580 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
5581 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
5582 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
Jonathan D. Turner3766fdb2011-07-21 21:15:19 +00005583 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
5584 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
5585 NumCXXBaseSpecifiersLoaded(0)
Douglas Gregor606c4ac2011-02-05 19:42:43 +00005586{
Douglas Gregor925296b2011-07-19 16:10:42 +00005587 SourceMgr.setExternalSLocEntrySource(this);
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005588}
5589
5590ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
Douglas Gregorc567ba22011-07-22 16:35:34 +00005591 Diagnostic &Diags, StringRef isysroot,
Douglas Gregor606c4ac2011-02-05 19:42:43 +00005592 bool DisableValidation, bool DisableStatCache)
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005593 : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
Jonathan D. Turnerecc27402011-07-28 17:20:23 +00005594 Diags(Diags), SemaObj(0), PP(0), Context(0),
5595 Consumer(0), ModuleMgr(FileMgr.getFileSystemOptions()),
Douglas Gregor925296b2011-07-19 16:10:42 +00005596 RelocatablePCH(false), isysroot(isysroot),
5597 DisableValidation(DisableValidation), DisableStatCache(DisableStatCache),
5598 NumStatHits(0), NumStatMisses(0), NumSLocEntriesRead(0),
5599 TotalNumSLocEntries(0), NumStatementsRead(0),
5600 TotalNumStatements(0), NumMacrosRead(0), TotalNumMacros(0),
5601 NumSelectorsRead(0), NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
Sebastian Redlc1d035f2010-09-22 20:19:08 +00005602 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
5603 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
Jonathan D. Turner3766fdb2011-07-21 21:15:19 +00005604 TotalVisibleDeclContexts(0), TotalModulesSizeInBits(0),
5605 NumCurrentElementsDeserializing(0), NumCXXBaseSpecifiersLoaded(0)
Douglas Gregor925296b2011-07-19 16:10:42 +00005606{
5607 SourceMgr.setExternalSLocEntrySource(this);
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005608}
5609
5610ASTReader::~ASTReader() {
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005611 // Delete all visible decl lookup tables
5612 for (DeclContextOffsetsMap::iterator I = DeclContextOffsets.begin(),
5613 E = DeclContextOffsets.end();
5614 I != E; ++I) {
5615 for (DeclContextInfos::iterator J = I->second.begin(), F = I->second.end();
5616 J != F; ++J) {
5617 if (J->NameLookupTableData)
5618 delete static_cast<ASTDeclContextNameLookupTable*>(
5619 J->NameLookupTableData);
5620 }
5621 }
5622 for (DeclContextVisibleUpdatesPending::iterator
5623 I = PendingVisibleUpdates.begin(),
5624 E = PendingVisibleUpdates.end();
5625 I != E; ++I) {
5626 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
5627 F = I->second.end();
5628 J != F; ++J)
Douglas Gregorf7180622011-08-03 15:48:04 +00005629 delete static_cast<ASTDeclContextNameLookupTable*>(J->first);
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005630 }
5631}
5632
Douglas Gregorab443b92011-08-20 04:39:52 +00005633//===----------------------------------------------------------------------===//
5634// Module implementation
5635//===----------------------------------------------------------------------===//
Douglas Gregora6895d82011-07-22 16:00:58 +00005636Module::Module(ModuleKind Kind)
Douglas Gregordf0c1512011-08-18 04:12:04 +00005637 : Kind(Kind), DirectlyImported(false), SizeInBits(0),
5638 LocalNumSLocEntries(0), SLocEntryBaseID(0),
Douglas Gregor925296b2011-07-19 16:10:42 +00005639 SLocEntryBaseOffset(0), SLocEntryOffsets(0),
5640 SLocFileOffsets(0), LocalNumIdentifiers(0),
Douglas Gregor00659902011-08-02 10:56:51 +00005641 IdentifierOffsets(0), BaseIdentifierID(0), IdentifierTableData(0),
5642 IdentifierLookupTable(0), BasePreprocessedEntityID(0),
5643 LocalNumMacroDefinitions(0), MacroDefinitionOffsets(0),
5644 BaseMacroDefinitionID(0), LocalNumHeaderFileInfos(0),
Douglas Gregor925296b2011-07-19 16:10:42 +00005645 HeaderFileInfoTableData(0), HeaderFileInfoTable(0),
Douglas Gregor4b123cb2011-07-28 04:50:02 +00005646 HeaderFileFrameworkStrings(0),
Douglas Gregor00659902011-08-02 10:56:51 +00005647 LocalNumSelectors(0), SelectorOffsets(0), BaseSelectorID(0),
Sebastian Redl949fe9e2010-09-22 00:42:27 +00005648 SelectorLookupTableData(0), SelectorLookupTable(0), LocalNumDecls(0),
Douglas Gregor00659902011-08-02 10:56:51 +00005649 DeclOffsets(0), BaseDeclID(0),
5650 LocalNumCXXBaseSpecifiers(0), CXXBaseSpecifiersOffsets(0),
Douglas Gregor3b65ed02011-08-02 18:32:54 +00005651 LocalNumTypes(0), TypeOffsets(0), BaseTypeIndex(0), StatCache(0),
Jonathan D. Turner269f2562011-07-28 23:15:22 +00005652 NumPreallocatedPreprocessingEntities(0)
Douglas Gregorb473b072010-08-19 00:28:17 +00005653{}
5654
Douglas Gregora6895d82011-07-22 16:00:58 +00005655Module::~Module() {
Douglas Gregorb473b072010-08-19 00:28:17 +00005656 delete static_cast<ASTIdentifierLookupTable *>(IdentifierLookupTable);
Douglas Gregor09b69892011-02-10 17:09:37 +00005657 delete static_cast<HeaderFileInfoLookupTable *>(HeaderFileInfoTable);
Douglas Gregorb473b072010-08-19 00:28:17 +00005658 delete static_cast<ASTSelectorLookupTable *>(SelectorLookupTable);
5659}
Jonathan D. Turnerb2b08232011-07-26 18:21:30 +00005660
Douglas Gregor1cc9c062011-08-02 11:12:41 +00005661template<typename Key, typename Offset, unsigned InitialCapacity>
5662static void
5663dumpLocalRemap(StringRef Name,
5664 const ContinuousRangeMap<Key, Offset, InitialCapacity> &Map) {
5665 if (Map.begin() == Map.end())
5666 return;
5667
5668 typedef ContinuousRangeMap<Key, Offset, InitialCapacity> MapType;
5669 llvm::errs() << " " << Name << ":\n";
5670 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
5671 I != IEnd; ++I) {
5672 llvm::errs() << " " << I->first << " -> " << I->second
5673 << "\n";
5674 }
5675}
5676
5677void Module::dump() {
5678 llvm::errs() << "\nModule: " << FileName << "\n";
5679 if (!Imports.empty()) {
5680 llvm::errs() << " Imports: ";
5681 for (unsigned I = 0, N = Imports.size(); I != N; ++I) {
5682 if (I)
5683 llvm::errs() << ", ";
5684 llvm::errs() << Imports[I]->FileName;
5685 }
5686 llvm::errs() << "\n";
5687 }
5688
5689 // Remapping tables.
5690 llvm::errs() << " Base source location offset: " << SLocEntryBaseOffset
5691 << '\n';
Douglas Gregorfddf5302011-08-04 19:00:50 +00005692 dumpLocalRemap("Source location offset local -> global map", SLocRemap);
5693
Douglas Gregor1ab036c2011-08-03 21:49:18 +00005694 llvm::errs() << " Base identifier ID: " << BaseIdentifierID << '\n'
Douglas Gregor2f555fc2011-08-04 18:56:47 +00005695 << " Number of identifiers: " << LocalNumIdentifiers << '\n';
Douglas Gregorfddf5302011-08-04 19:00:50 +00005696 dumpLocalRemap("Identifier ID local -> global map", IdentifierRemap);
5697
5698 llvm::errs() << " Base selector ID: " << BaseSelectorID << '\n'
5699 << " Number of selectors: " << LocalNumSelectors << '\n';
5700 dumpLocalRemap("Selector ID local -> global map", SelectorRemap);
5701
Douglas Gregor2f555fc2011-08-04 18:56:47 +00005702 llvm::errs() << " Base preprocessed entity ID: " << BasePreprocessedEntityID
5703 << '\n'
Douglas Gregordab42432011-08-12 00:15:20 +00005704 << " Number of preprocessed entities: "
Douglas Gregor2f555fc2011-08-04 18:56:47 +00005705 << NumPreallocatedPreprocessingEntities << '\n';
Douglas Gregorfddf5302011-08-04 19:00:50 +00005706 dumpLocalRemap("Preprocessed entity ID local -> global map",
5707 PreprocessedEntityRemap);
5708
Douglas Gregora863b4b2011-08-04 16:36:56 +00005709 llvm::errs() << " Base macro definition ID: " << BaseMacroDefinitionID
5710 << '\n'
5711 << " Number of macro definitions: " << LocalNumMacroDefinitions
5712 << '\n';
Douglas Gregorfddf5302011-08-04 19:00:50 +00005713 dumpLocalRemap("Macro definition ID local -> global map",
5714 MacroDefinitionRemap);
5715
5716 llvm::errs() << " Base type index: " << BaseTypeIndex << '\n'
5717 << " Number of types: " << LocalNumTypes << '\n';
5718 dumpLocalRemap("Type index local -> global map", TypeRemap);
5719
Douglas Gregorf7180622011-08-03 15:48:04 +00005720 llvm::errs() << " Base decl ID: " << BaseDeclID << '\n'
5721 << " Number of decls: " << LocalNumDecls << '\n';
Douglas Gregorfddf5302011-08-04 19:00:50 +00005722 dumpLocalRemap("Decl ID local -> global map", DeclRemap);
Douglas Gregor1cc9c062011-08-02 11:12:41 +00005723}
5724
Douglas Gregorab443b92011-08-20 04:39:52 +00005725//===----------------------------------------------------------------------===//
5726// Module manager implementation
5727//===----------------------------------------------------------------------===//
5728
Jonathan D. Turnerecc27402011-07-28 17:20:23 +00005729Module *ModuleManager::lookup(StringRef Name) {
5730 const FileEntry *Entry = FileMgr.getFile(Name);
5731 return Modules[Entry];
5732}
5733
Jonathan D. Turnerdb1c9e32011-08-02 17:40:32 +00005734llvm::MemoryBuffer *ModuleManager::lookupBuffer(StringRef Name) {
5735 const FileEntry *Entry = FileMgr.getFile(Name);
5736 return InMemoryBuffers[Entry];
5737}
5738
Douglas Gregor4dd3e942011-08-19 02:29:29 +00005739std::pair<Module *, bool>
5740ModuleManager::addModule(StringRef FileName, ModuleKind Type,
5741 Module *ImportedBy, std::string &ErrorStr) {
Chad Rosier222e1872011-08-18 19:06:24 +00005742 const FileEntry *Entry = FileMgr.getFile(FileName);
Douglas Gregor4dd3e942011-08-19 02:29:29 +00005743 if (!Entry && FileName != "-") {
5744 ErrorStr = "file not found";
5745 return std::make_pair(static_cast<Module*>(0), false);
5746 }
5747
5748 // Check whether we already loaded this module, before
5749 Module *&ModuleEntry = Modules[Entry];
5750 bool NewModule = false;
5751 if (!ModuleEntry) {
5752 // Allocate a new module.
5753 Module *New = new Module(Type);
5754 New->FileName = FileName.str();
5755 Chain.push_back(New);
5756 NewModule = true;
5757 ModuleEntry = New;
5758
5759 // Load the contents of the module
5760 if (llvm::MemoryBuffer *Buffer = lookupBuffer(FileName)) {
5761 // The buffer was already provided for us.
5762 assert(Buffer && "Passed null buffer");
5763 New->Buffer.reset(Buffer);
5764 } else {
5765 // Open the AST file.
5766 llvm::error_code ec;
5767 if (FileName == "-") {
5768 ec = llvm::MemoryBuffer::getSTDIN(New->Buffer);
5769 if (ec)
5770 ErrorStr = ec.message();
5771 } else
5772 New->Buffer.reset(FileMgr.getBufferForFile(FileName, &ErrorStr));
5773
5774 if (!New->Buffer)
5775 return std::make_pair(static_cast<Module*>(0), false);
5776 }
5777
5778 // Initialize the stream
5779 New->StreamFile.init((const unsigned char *)New->Buffer->getBufferStart(),
5780 (const unsigned char *)New->Buffer->getBufferEnd()); }
Jonathan D. Turnerb2b08232011-07-26 18:21:30 +00005781
Douglas Gregordf0c1512011-08-18 04:12:04 +00005782 if (ImportedBy) {
Douglas Gregor4dd3e942011-08-19 02:29:29 +00005783 ModuleEntry->ImportedBy.insert(ImportedBy);
5784 ImportedBy->Imports.insert(ModuleEntry);
Douglas Gregordf0c1512011-08-18 04:12:04 +00005785 } else {
Douglas Gregor4dd3e942011-08-19 02:29:29 +00005786 ModuleEntry->DirectlyImported = true;
Jonathan D. Turner10d52012011-07-29 18:09:09 +00005787 }
5788
Douglas Gregor4dd3e942011-08-19 02:29:29 +00005789 return std::make_pair(ModuleEntry, NewModule);
Jonathan D. Turnerb2b08232011-07-26 18:21:30 +00005790}
5791
Jonathan D. Turnerdb1c9e32011-08-02 17:40:32 +00005792void ModuleManager::addInMemoryBuffer(StringRef FileName,
5793 llvm::MemoryBuffer *Buffer) {
5794
5795 const FileEntry *Entry = FileMgr.getVirtualFile(FileName,
5796 Buffer->getBufferSize(), 0);
5797 InMemoryBuffers[Entry] = Buffer;
5798}
Jonathan D. Turnerb2b08232011-07-26 18:21:30 +00005799
Jonathan D. Turnerecc27402011-07-28 17:20:23 +00005800ModuleManager::ModuleManager(const FileSystemOptions &FSO) : FileMgr(FSO) { }
5801
Jonathan D. Turnerb2b08232011-07-26 18:21:30 +00005802ModuleManager::~ModuleManager() {
5803 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
5804 delete Chain[e - i - 1];
5805}
Douglas Gregorab443b92011-08-20 04:39:52 +00005806
5807void ModuleManager::visit(bool (*Visitor)(Module &M, void *UserData),
5808 void *UserData) {
5809 unsigned N = size();
5810
5811 // Record the number of incoming edges for each module. When we
5812 // encounter a module with no incoming edges, push it into the queue
5813 // to seed the queue.
5814 SmallVector<Module *, 4> Queue;
5815 Queue.reserve(N);
5816 llvm::DenseMap<Module *, unsigned> UnusedIncomingEdges;
5817 for (ModuleIterator M = begin(), MEnd = end(); M != MEnd; ++M) {
5818 if (unsigned Size = (*M)->ImportedBy.size())
5819 UnusedIncomingEdges[*M] = Size;
5820 else
5821 Queue.push_back(*M);
5822 }
5823
5824 llvm::SmallPtrSet<Module *, 4> Skipped;
5825 unsigned QueueStart = 0;
5826 while (QueueStart < Queue.size()) {
5827 Module *CurrentModule = Queue[QueueStart++];
5828
5829 // Check whether this module should be skipped.
5830 if (Skipped.count(CurrentModule))
5831 continue;
5832
5833 if (Visitor(*CurrentModule, UserData)) {
5834 // The visitor has requested that cut off visitation of any
5835 // module that the current module depends on. To indicate this
5836 // behavior, we mark all of the reachable modules as having N
5837 // incoming edges (which is impossible otherwise).
5838 SmallVector<Module *, 4> Stack;
5839 Stack.push_back(CurrentModule);
5840 Skipped.insert(CurrentModule);
5841 while (!Stack.empty()) {
5842 Module *NextModule = Stack.back();
5843 Stack.pop_back();
5844
5845 // For any module that this module depends on, push it on the
5846 // stack (if it hasn't already been marked as visited).
5847 for (llvm::SetVector<Module *>::iterator
5848 M = NextModule->Imports.begin(),
5849 MEnd = NextModule->Imports.end();
5850 M != MEnd; ++M) {
5851 if (Skipped.insert(*M))
5852 Stack.push_back(*M);
5853 }
5854 }
5855 continue;
5856 }
5857
5858 // For any module that this module depends on, push it on the
5859 // stack (if it hasn't already been marked as visited).
5860 for (llvm::SetVector<Module *>::iterator M = CurrentModule->Imports.begin(),
5861 MEnd = CurrentModule->Imports.end();
5862 M != MEnd; ++M) {
5863
5864 // Remove our current module as an impediment to visiting the
5865 // module we depend on. If we were the last unvisited module
5866 // that depends on this particular module, push it into the
5867 // queue to be visited.
5868 unsigned &NumUnusedEdges = UnusedIncomingEdges[*M];
5869 if (NumUnusedEdges && (--NumUnusedEdges == 0))
5870 Queue.push_back(*M);
5871 }
5872 }
5873}