blob: 8264a7421c555081818c41b1cdaac8200ad1f7a5 [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
Douglas Gregor94619c82011-08-24 19:03:07 +0000921bool ASTReader::ReadDeclContextStorage(Module &M,
922 llvm::BitstreamCursor &Cursor,
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000923 const std::pair<uint64_t, uint64_t> &Offsets,
924 DeclContextInfo &Info) {
925 SavedStreamPosition SavedPosition(Cursor);
926 // First the lexical decls.
927 if (Offsets.first != 0) {
928 Cursor.JumpToBit(Offsets.first);
929
930 RecordData Record;
931 const char *Blob;
932 unsigned BlobLen;
933 unsigned Code = Cursor.ReadCode();
934 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
935 if (RecCode != DECL_CONTEXT_LEXICAL) {
936 Error("Expected lexical block");
937 return true;
938 }
939
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000940 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
941 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000942 }
943
944 // Now the lookup table.
945 if (Offsets.second != 0) {
946 Cursor.JumpToBit(Offsets.second);
947
948 RecordData Record;
949 const char *Blob;
950 unsigned BlobLen;
951 unsigned Code = Cursor.ReadCode();
952 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
953 if (RecCode != DECL_CONTEXT_VISIBLE) {
954 Error("Expected visible lookup table block");
955 return true;
956 }
957 Info.NameLookupTableData
958 = ASTDeclContextNameLookupTable::Create(
959 (const unsigned char *)Blob + Record[0],
960 (const unsigned char *)Blob,
Douglas Gregor94619c82011-08-24 19:03:07 +0000961 ASTDeclContextNameLookupTrait(*this, M));
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000962 }
963
964 return false;
965}
966
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000967void ASTReader::Error(StringRef Msg) {
Argyrios Kyrtzidisdaa41f52011-04-25 22:23:56 +0000968 Error(diag::err_fe_pch_malformed, Msg);
969}
970
971void ASTReader::Error(unsigned DiagID,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000972 StringRef Arg1, StringRef Arg2) {
Argyrios Kyrtzidisdaa41f52011-04-25 22:23:56 +0000973 if (Diags.isDiagnosticInFlight())
974 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
975 else
976 Diag(DiagID) << Arg1 << Arg2;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000977}
978
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000979/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redl2c499f62010-08-18 23:56:43 +0000980bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000981 if (Listener)
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000982 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000983 ActualOriginalFileName,
Nick Lewycky36079892011-02-23 21:16:44 +0000984 SuggestedPredefines,
985 FileMgr);
Douglas Gregorc379c072009-04-28 18:58:38 +0000986 return false;
Douglas Gregor92863e42009-04-10 23:10:45 +0000987}
988
Douglas Gregorc5046832009-04-27 18:38:38 +0000989//===----------------------------------------------------------------------===//
990// Source Manager Deserialization
991//===----------------------------------------------------------------------===//
992
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000993/// \brief Read the line table in the source manager block.
Sebastian Redl2c373b92010-10-05 15:59:54 +0000994/// \returns true if there was an error.
Douglas Gregora6895d82011-07-22 16:00:58 +0000995bool ASTReader::ParseLineTable(Module &F,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000996 SmallVectorImpl<uint64_t> &Record) {
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000997 unsigned Idx = 0;
998 LineTableInfo &LineTable = SourceMgr.getLineTable();
999
1000 // Parse the file names
Douglas Gregora8854652009-04-13 17:12:42 +00001001 std::map<int, int> FileIDs;
1002 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001003 // Extract the file name
1004 unsigned FilenameLen = Record[Idx++];
1005 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
1006 Idx += FilenameLen;
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001007 MaybeAddSystemRootToFilename(Filename);
Jay Foad9a6b0982011-06-21 15:13:30 +00001008 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001009 }
1010
1011 // Parse the line entries
1012 std::vector<LineEntry> Entries;
1013 while (Idx < Record.size()) {
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +00001014 int FID = Record[Idx++];
Douglas Gregor925296b2011-07-19 16:10:42 +00001015 assert(FID >= 0 && "Serialized line entries for non-local file.");
1016 // Remap FileID from 1-based old view.
1017 FID += F.SLocEntryBaseID - 1;
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001018
1019 // Extract the line entries
1020 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +00001021 assert(NumEntries && "Numentries is 00000");
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001022 Entries.clear();
1023 Entries.reserve(NumEntries);
1024 for (unsigned I = 0; I != NumEntries; ++I) {
1025 unsigned FileOffset = Record[Idx++];
1026 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +00001027 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump11289f42009-09-09 15:08:12 +00001028 SrcMgr::CharacteristicKind FileKind
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001029 = (SrcMgr::CharacteristicKind)Record[Idx++];
1030 unsigned IncludeOffset = Record[Idx++];
1031 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1032 FileKind, IncludeOffset));
1033 }
1034 LineTable.AddEntry(FID, Entries);
1035 }
1036
1037 return false;
1038}
1039
Douglas Gregorc5046832009-04-27 18:38:38 +00001040namespace {
1041
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001042class ASTStatData {
Douglas Gregorc5046832009-04-27 18:38:38 +00001043public:
Douglas Gregorc5046832009-04-27 18:38:38 +00001044 const ino_t ino;
1045 const dev_t dev;
1046 const mode_t mode;
1047 const time_t mtime;
1048 const off_t size;
Mike Stump11289f42009-09-09 15:08:12 +00001049
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001050 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Chris Lattner2a6fa472010-11-23 19:28:12 +00001051 : ino(i), dev(d), mode(mo), mtime(m), size(s) {}
Douglas Gregorc5046832009-04-27 18:38:38 +00001052};
1053
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001054class ASTStatLookupTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +00001055 public:
1056 typedef const char *external_key_type;
1057 typedef const char *internal_key_type;
1058
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001059 typedef ASTStatData data_type;
Douglas Gregorc5046832009-04-27 18:38:38 +00001060
1061 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001062 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001063 }
1064
1065 static internal_key_type GetInternalKey(const char *path) { return path; }
1066
1067 static bool EqualKey(internal_key_type a, internal_key_type b) {
1068 return strcmp(a, b) == 0;
1069 }
1070
1071 static std::pair<unsigned, unsigned>
1072 ReadKeyDataLength(const unsigned char*& d) {
1073 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1074 unsigned DataLen = (unsigned) *d++;
1075 return std::make_pair(KeyLen + 1, DataLen);
1076 }
1077
1078 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1079 return (const char *)d;
1080 }
1081
1082 static data_type ReadData(const internal_key_type, const unsigned char *d,
1083 unsigned /*DataLen*/) {
1084 using namespace clang::io;
1085
Douglas Gregorc5046832009-04-27 18:38:38 +00001086 ino_t ino = (ino_t) ReadUnalignedLE32(d);
1087 dev_t dev = (dev_t) ReadUnalignedLE32(d);
1088 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump11289f42009-09-09 15:08:12 +00001089 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregorc5046832009-04-27 18:38:38 +00001090 off_t size = (off_t) ReadUnalignedLE64(d);
1091 return data_type(ino, dev, mode, mtime, size);
1092 }
1093};
1094
1095/// \brief stat() cache for precompiled headers.
1096///
1097/// This cache is very similar to the stat cache used by pretokenized
1098/// headers.
Chris Lattner226efd32010-11-23 19:19:34 +00001099class ASTStatCache : public FileSystemStatCache {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001100 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregorc5046832009-04-27 18:38:38 +00001101 CacheTy *Cache;
1102
1103 unsigned &NumStatHits, &NumStatMisses;
Mike Stump11289f42009-09-09 15:08:12 +00001104public:
Chris Lattner2a6fa472010-11-23 19:28:12 +00001105 ASTStatCache(const unsigned char *Buckets, const unsigned char *Base,
1106 unsigned &NumStatHits, unsigned &NumStatMisses)
Douglas Gregorc5046832009-04-27 18:38:38 +00001107 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
1108 Cache = CacheTy::Create(Buckets, Base);
1109 }
1110
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001111 ~ASTStatCache() { delete Cache; }
Mike Stump11289f42009-09-09 15:08:12 +00001112
Chris Lattnerdd278432010-11-23 21:17:56 +00001113 LookupResult getStat(const char *Path, struct stat &StatBuf,
1114 int *FileDescriptor) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001115 // Do the lookup for the file's data in the AST file.
Chris Lattner226efd32010-11-23 19:19:34 +00001116 CacheTy::iterator I = Cache->find(Path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001117
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001118 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregorc5046832009-04-27 18:38:38 +00001119 if (I == Cache->end()) {
1120 ++NumStatMisses;
Chris Lattnerdd278432010-11-23 21:17:56 +00001121 return statChained(Path, StatBuf, FileDescriptor);
Douglas Gregorc5046832009-04-27 18:38:38 +00001122 }
Mike Stump11289f42009-09-09 15:08:12 +00001123
Douglas Gregorc5046832009-04-27 18:38:38 +00001124 ++NumStatHits;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001125 ASTStatData Data = *I;
Mike Stump11289f42009-09-09 15:08:12 +00001126
Chris Lattner226efd32010-11-23 19:19:34 +00001127 StatBuf.st_ino = Data.ino;
1128 StatBuf.st_dev = Data.dev;
1129 StatBuf.st_mtime = Data.mtime;
1130 StatBuf.st_mode = Data.mode;
1131 StatBuf.st_size = Data.size;
Chris Lattner8f0583d2010-11-23 20:05:15 +00001132 return CacheExists;
Douglas Gregorc5046832009-04-27 18:38:38 +00001133 }
1134};
1135} // end anonymous namespace
1136
1137
Sebastian Redl393f8b72010-07-19 20:52:06 +00001138/// \brief Read a source manager block
Douglas Gregora6895d82011-07-22 16:00:58 +00001139ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(Module &F) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001140 using namespace SrcMgr;
Douglas Gregor258ae542009-04-27 06:38:32 +00001141
Sebastian Redl393f8b72010-07-19 20:52:06 +00001142 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001143
Douglas Gregor258ae542009-04-27 06:38:32 +00001144 // Set the source-location entry cursor to the current position in
1145 // the stream. This cursor will be used to read the contents of the
1146 // source manager block initially, and then lazily read
1147 // source-location entries as needed.
Sebastian Redl393f8b72010-07-19 20:52:06 +00001148 SLocEntryCursor = F.Stream;
Douglas Gregor258ae542009-04-27 06:38:32 +00001149
1150 // The stream itself is going to skip over the source manager block.
Sebastian Redl393f8b72010-07-19 20:52:06 +00001151 if (F.Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001152 Error("malformed block record in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001153 return Failure;
1154 }
1155
1156 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001157 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001158 Error("malformed source manager block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001159 return Failure;
1160 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001161
Douglas Gregora7f71a92009-04-10 03:52:48 +00001162 RecordData Record;
1163 while (true) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001164 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregora7f71a92009-04-10 03:52:48 +00001165 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001166 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001167 Error("error at end of Source Manager block in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001168 return Failure;
1169 }
Douglas Gregor92863e42009-04-10 23:10:45 +00001170 return Success;
Douglas Gregora7f71a92009-04-10 03:52:48 +00001171 }
Mike Stump11289f42009-09-09 15:08:12 +00001172
Douglas Gregora7f71a92009-04-10 03:52:48 +00001173 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1174 // No known subblocks, always skip them.
Douglas Gregor258ae542009-04-27 06:38:32 +00001175 SLocEntryCursor.ReadSubBlockID();
1176 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001177 Error("malformed block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001178 return Failure;
1179 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001180 continue;
1181 }
Mike Stump11289f42009-09-09 15:08:12 +00001182
Douglas Gregora7f71a92009-04-10 03:52:48 +00001183 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001184 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregora7f71a92009-04-10 03:52:48 +00001185 continue;
1186 }
Mike Stump11289f42009-09-09 15:08:12 +00001187
Douglas Gregora7f71a92009-04-10 03:52:48 +00001188 // Read a record.
1189 const char *BlobStart;
1190 unsigned BlobLen;
1191 Record.clear();
Douglas Gregor258ae542009-04-27 06:38:32 +00001192 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001193 default: // Default behavior: ignore.
1194 break;
1195
Sebastian Redl539c5062010-08-18 23:57:32 +00001196 case SM_SLOC_FILE_ENTRY:
1197 case SM_SLOC_BUFFER_ENTRY:
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001198 case SM_SLOC_EXPANSION_ENTRY:
Douglas Gregor258ae542009-04-27 06:38:32 +00001199 // Once we hit one of the source location entries, we're done.
1200 return Success;
Douglas Gregora7f71a92009-04-10 03:52:48 +00001201 }
1202 }
1203}
1204
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001205/// \brief If a header file is not found at the path that we expect it to be
1206/// and the PCH file was moved from its original location, try to resolve the
1207/// file by assuming that header+PCH were moved together and the header is in
1208/// the same place relative to the PCH.
1209static std::string
1210resolveFileRelativeToOriginalDir(const std::string &Filename,
1211 const std::string &OriginalDir,
1212 const std::string &CurrDir) {
1213 assert(OriginalDir != CurrDir &&
1214 "No point trying to resolve the file if the PCH dir didn't change");
1215 using namespace llvm::sys;
1216 llvm::SmallString<128> filePath(Filename);
1217 fs::make_absolute(filePath);
1218 assert(path::is_absolute(OriginalDir));
1219 llvm::SmallString<128> currPCHPath(CurrDir);
1220
1221 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1222 fileDirE = path::end(path::parent_path(filePath));
1223 path::const_iterator origDirI = path::begin(OriginalDir),
1224 origDirE = path::end(OriginalDir);
1225 // Skip the common path components from filePath and OriginalDir.
1226 while (fileDirI != fileDirE && origDirI != origDirE &&
1227 *fileDirI == *origDirI) {
1228 ++fileDirI;
1229 ++origDirI;
1230 }
1231 for (; origDirI != origDirE; ++origDirI)
1232 path::append(currPCHPath, "..");
1233 path::append(currPCHPath, fileDirI, fileDirE);
1234 path::append(currPCHPath, path::filename(Filename));
1235 return currPCHPath.str();
1236}
1237
Douglas Gregor258ae542009-04-27 06:38:32 +00001238/// \brief Read in the source location entry with the given ID.
Douglas Gregor925296b2011-07-19 16:10:42 +00001239ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(int ID) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001240 if (ID == 0)
1241 return Success;
1242
Douglas Gregor49bf76b2011-07-21 18:46:38 +00001243 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001244 Error("source location entry ID out-of-range for AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001245 return Failure;
1246 }
1247
Douglas Gregora6895d82011-07-22 16:00:58 +00001248 Module *F = GlobalSLocEntryMap.find(-ID)->second;
Douglas Gregor925296b2011-07-19 16:10:42 +00001249 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00001250 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Douglas Gregor925296b2011-07-19 16:10:42 +00001251 unsigned BaseOffset = F->SLocEntryBaseOffset;
Sebastian Redl34522812010-07-16 17:50:48 +00001252
Douglas Gregor258ae542009-04-27 06:38:32 +00001253 ++NumSLocEntriesRead;
Douglas Gregor258ae542009-04-27 06:38:32 +00001254 unsigned Code = SLocEntryCursor.ReadCode();
1255 if (Code == llvm::bitc::END_BLOCK ||
1256 Code == llvm::bitc::ENTER_SUBBLOCK ||
1257 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001258 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001259 return Failure;
1260 }
1261
Douglas Gregor258ae542009-04-27 06:38:32 +00001262 RecordData Record;
1263 const char *BlobStart;
1264 unsigned BlobLen;
1265 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1266 default:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001267 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001268 return Failure;
1269
Sebastian Redl539c5062010-08-18 23:57:32 +00001270 case SM_SLOC_FILE_ENTRY: {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001271 std::string Filename(BlobStart, BlobStart + BlobLen);
1272 MaybeAddSystemRootToFilename(Filename);
Chris Lattner5159f612010-11-23 08:35:12 +00001273 const FileEntry *File = FileMgr.getFile(Filename);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001274 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1275 OriginalDir != CurrentDir) {
1276 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1277 OriginalDir,
1278 CurrentDir);
1279 if (!resolved.empty())
1280 File = FileMgr.getFile(resolved);
1281 }
Axel Naumann63fbaed2011-01-27 10:55:51 +00001282 if (File == 0)
1283 File = FileMgr.getVirtualFile(Filename, (off_t)Record[4],
1284 (time_t)Record[5]);
Chris Lattnerd20dc872009-06-15 04:35:16 +00001285 if (File == 0) {
1286 std::string ErrorStr = "could not find file '";
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001287 ErrorStr += Filename;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001288 ErrorStr += "' referenced by AST file";
Chris Lattnerd20dc872009-06-15 04:35:16 +00001289 Error(ErrorStr.c_str());
1290 return Failure;
1291 }
Mike Stump11289f42009-09-09 15:08:12 +00001292
Douglas Gregor09b69892011-02-10 17:09:37 +00001293 if (Record.size() < 6) {
Ted Kremenekabb1ddd2010-03-18 21:23:05 +00001294 Error("source location entry is incorrect");
1295 return Failure;
1296 }
1297
Douglas Gregorce3a8292010-07-27 00:27:13 +00001298 if (!DisableValidation &&
1299 ((off_t)Record[4] != File->getSize()
Douglas Gregor08288f22010-04-09 15:54:22 +00001300#if !defined(LLVM_ON_WIN32)
1301 // In our regression testing, the Windows file system seems to
1302 // have inconsistent modification times that sometimes
1303 // erroneously trigger this error-handling path.
Douglas Gregorce3a8292010-07-27 00:27:13 +00001304 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor08288f22010-04-09 15:54:22 +00001305#endif
Douglas Gregorce3a8292010-07-27 00:27:13 +00001306 )) {
Argyrios Kyrtzidisdaa41f52011-04-25 22:23:56 +00001307 Error(diag::err_fe_pch_file_modified, Filename);
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001308 return Failure;
1309 }
1310
Douglas Gregor925296b2011-07-19 16:10:42 +00001311 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregora6895d82011-07-22 16:00:58 +00001312 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
Douglas Gregor925296b2011-07-19 16:10:42 +00001313 // This is the module's main file.
1314 IncludeLoc = getImportLocation(F);
1315 }
1316 FileID FID = SourceMgr.createFileID(File, IncludeLoc,
Chris Lattner26b5c192010-11-23 09:19:42 +00001317 (SrcMgr::CharacteristicKind)Record[2],
Douglas Gregor925296b2011-07-19 16:10:42 +00001318 ID, BaseOffset + Record[0]);
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001319 SrcMgr::FileInfo &FileInfo =
1320 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1321 FileInfo.NumCreatedFIDs = Record[6];
Douglas Gregor258ae542009-04-27 06:38:32 +00001322 if (Record[3])
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001323 FileInfo.setHasLineDirectives();
Douglas Gregor09b69892011-02-10 17:09:37 +00001324
Douglas Gregor258ae542009-04-27 06:38:32 +00001325 break;
1326 }
1327
Sebastian Redl539c5062010-08-18 23:57:32 +00001328 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor258ae542009-04-27 06:38:32 +00001329 const char *Name = BlobStart;
1330 unsigned Offset = Record[0];
1331 unsigned Code = SLocEntryCursor.ReadCode();
1332 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00001333 unsigned RecCode
Douglas Gregor258ae542009-04-27 06:38:32 +00001334 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001335
Sebastian Redl539c5062010-08-18 23:57:32 +00001336 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001337 Error("AST record has invalid code");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001338 return Failure;
1339 }
1340
Douglas Gregor258ae542009-04-27 06:38:32 +00001341 llvm::MemoryBuffer *Buffer
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001342 = llvm::MemoryBuffer::getMemBuffer(StringRef(BlobStart, BlobLen - 1),
Chris Lattner58c79342010-04-05 22:42:27 +00001343 Name);
Douglas Gregor925296b2011-07-19 16:10:42 +00001344 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID,
1345 BaseOffset + Offset);
Mike Stump11289f42009-09-09 15:08:12 +00001346
Douglas Gregore6648fb2009-04-28 20:33:11 +00001347 if (strcmp(Name, "<built-in>") == 0) {
Sebastian Redl75fbb3b2010-07-14 17:49:11 +00001348 PCHPredefinesBlock Block = {
1349 BufferID,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001350 StringRef(BlobStart, BlobLen - 1)
Sebastian Redl75fbb3b2010-07-14 17:49:11 +00001351 };
1352 PCHPredefinesBuffers.push_back(Block);
Douglas Gregore6648fb2009-04-28 20:33:11 +00001353 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001354
1355 break;
1356 }
1357
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001358 case SM_SLOC_EXPANSION_ENTRY: {
Sebastian Redl2c373b92010-10-05 15:59:54 +00001359 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Chandler Carruth115b0772011-07-26 03:03:05 +00001360 SourceMgr.createExpansionLoc(SpellingLoc,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001361 ReadSourceLocation(*F, Record[2]),
1362 ReadSourceLocation(*F, Record[3]),
Douglas Gregor258ae542009-04-27 06:38:32 +00001363 Record[4],
1364 ID,
Douglas Gregor925296b2011-07-19 16:10:42 +00001365 BaseOffset + Record[0]);
Douglas Gregor258ae542009-04-27 06:38:32 +00001366 break;
Mike Stump11289f42009-09-09 15:08:12 +00001367 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001368 }
1369
1370 return Success;
1371}
1372
Douglas Gregor925296b2011-07-19 16:10:42 +00001373/// \brief Find the location where the module F is imported.
Douglas Gregora6895d82011-07-22 16:00:58 +00001374SourceLocation ASTReader::getImportLocation(Module *F) {
Douglas Gregor925296b2011-07-19 16:10:42 +00001375 if (F->ImportLoc.isValid())
1376 return F->ImportLoc;
Jonathan D. Turner10d52012011-07-29 18:09:09 +00001377
Douglas Gregor925296b2011-07-19 16:10:42 +00001378 // Otherwise we have a PCH. It's considered to be "imported" at the first
1379 // location of its includer.
Jonathan D. Turner10d52012011-07-29 18:09:09 +00001380 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
Douglas Gregor925296b2011-07-19 16:10:42 +00001381 // Main file is the importer. We assume that it is the first entry in the
1382 // entry table. We can't ask the manager, because at the time of PCH loading
1383 // the main file entry doesn't exist yet.
1384 // The very first entry is the invalid instantiation loc, which takes up
1385 // offsets 0 and 1.
1386 return SourceLocation::getFromRawEncoding(2U);
1387 }
Jonathan D. Turner10d52012011-07-29 18:09:09 +00001388 //return F->Loaders[0]->FirstLoc;
1389 return F->ImportedBy[0]->FirstLoc;
Douglas Gregor925296b2011-07-19 16:10:42 +00001390}
1391
Chris Lattnere78a6be2009-04-27 01:05:14 +00001392/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1393/// specified cursor. Read the abbreviations that are at the top of the block
1394/// and then leave the cursor pointing into the block.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001395bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattnere78a6be2009-04-27 01:05:14 +00001396 unsigned BlockID) {
1397 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001398 Error("malformed block record in AST file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001399 return Failure;
1400 }
Mike Stump11289f42009-09-09 15:08:12 +00001401
Chris Lattnere78a6be2009-04-27 01:05:14 +00001402 while (true) {
Douglas Gregor796d76a2010-10-20 22:00:55 +00001403 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattnere78a6be2009-04-27 01:05:14 +00001404 unsigned Code = Cursor.ReadCode();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001405
Chris Lattnere78a6be2009-04-27 01:05:14 +00001406 // We expect all abbrevs to be at the start of the block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001407 if (Code != llvm::bitc::DEFINE_ABBREV) {
1408 Cursor.JumpToBit(Offset);
Chris Lattnere78a6be2009-04-27 01:05:14 +00001409 return false;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001410 }
Chris Lattnere78a6be2009-04-27 01:05:14 +00001411 Cursor.ReadAbbrevRecord();
1412 }
1413}
1414
Douglas Gregor7cb0d012011-08-04 18:09:14 +00001415void ASTReader::ReadMacroRecord(Module &F, uint64_t Offset) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001416 assert(PP && "Forgot to set Preprocessor ?");
Douglas Gregor796d76a2010-10-20 22:00:55 +00001417 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump11289f42009-09-09 15:08:12 +00001418
Douglas Gregorc3366a52009-04-21 23:56:24 +00001419 // Keep track of where we are in the stream, then jump back there
1420 // after reading this macro.
1421 SavedStreamPosition SavedPosition(Stream);
1422
1423 Stream.JumpToBit(Offset);
1424 RecordData Record;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001425 SmallVector<IdentifierInfo*, 16> MacroArgs;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001426 MacroInfo *Macro = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001427
Douglas Gregorc3366a52009-04-21 23:56:24 +00001428 while (true) {
1429 unsigned Code = Stream.ReadCode();
1430 switch (Code) {
1431 case llvm::bitc::END_BLOCK:
Douglas Gregor7cb0d012011-08-04 18:09:14 +00001432 return;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001433
1434 case llvm::bitc::ENTER_SUBBLOCK:
1435 // No known subblocks, always skip them.
1436 Stream.ReadSubBlockID();
1437 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001438 Error("malformed block record in AST file");
Douglas Gregor7cb0d012011-08-04 18:09:14 +00001439 return;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001440 }
1441 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001442
Douglas Gregorc3366a52009-04-21 23:56:24 +00001443 case llvm::bitc::DEFINE_ABBREV:
1444 Stream.ReadAbbrevRecord();
1445 continue;
1446 default: break;
1447 }
1448
1449 // Read a record.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001450 const char *BlobStart = 0;
1451 unsigned BlobLen = 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001452 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001453 PreprocessorRecordTypes RecType =
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001454 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregor796d76a2010-10-20 22:00:55 +00001455 BlobLen);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001456 switch (RecType) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001457 case PP_MACRO_OBJECT_LIKE:
1458 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001459 // If we already have a macro, that means that we've hit the end
1460 // of the definition of the macro we were looking for. We're
1461 // done.
1462 if (Macro)
Douglas Gregor7cb0d012011-08-04 18:09:14 +00001463 return;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001464
Douglas Gregora3e41532011-07-28 20:55:49 +00001465 IdentifierInfo *II = getLocalIdentifier(F, Record[0]);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001466 if (II == 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001467 Error("macro must have a name in AST file");
Douglas Gregor7cb0d012011-08-04 18:09:14 +00001468 return;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001469 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00001470 SourceLocation Loc = ReadSourceLocation(F, Record[1]);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001471 bool isUsed = Record[2];
Mike Stump11289f42009-09-09 15:08:12 +00001472
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001473 MacroInfo *MI = PP->AllocateMacroInfo(Loc);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001474 MI->setIsUsed(isUsed);
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001475 MI->setIsFromAST();
Mike Stump11289f42009-09-09 15:08:12 +00001476
Douglas Gregoraae92242010-03-19 21:51:54 +00001477 unsigned NextIndex = 3;
Sebastian Redl539c5062010-08-18 23:57:32 +00001478 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001479 // Decode function-like macro info.
1480 bool isC99VarArgs = Record[3];
1481 bool isGNUVarArgs = Record[4];
1482 MacroArgs.clear();
1483 unsigned NumArgs = Record[5];
Douglas Gregoraae92242010-03-19 21:51:54 +00001484 NextIndex = 6 + NumArgs;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001485 for (unsigned i = 0; i != NumArgs; ++i)
Douglas Gregora3e41532011-07-28 20:55:49 +00001486 MacroArgs.push_back(getLocalIdentifier(F, Record[6+i]));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001487
1488 // Install function-like macro info.
1489 MI->setIsFunctionLike();
1490 if (isC99VarArgs) MI->setIsC99Varargs();
1491 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor038c3382009-05-22 22:45:36 +00001492 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001493 PP->getPreprocessorAllocator());
Douglas Gregorc3366a52009-04-21 23:56:24 +00001494 }
1495
1496 // Finally, install the macro.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001497 PP->setMacroInfo(II, MI);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001498
1499 // Remember that we saw this macro last so that we add the tokens that
1500 // form its body to it.
1501 Macro = MI;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001502
Douglas Gregoraae92242010-03-19 21:51:54 +00001503 if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) {
1504 // We have a macro definition. Load it now.
1505 PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro,
Douglas Gregor035611e2011-07-28 22:16:57 +00001506 getLocalMacroDefinition(F, Record[NextIndex]));
Douglas Gregoraae92242010-03-19 21:51:54 +00001507 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001508
Douglas Gregorc3366a52009-04-21 23:56:24 +00001509 ++NumMacrosRead;
1510 break;
1511 }
Mike Stump11289f42009-09-09 15:08:12 +00001512
Sebastian Redl539c5062010-08-18 23:57:32 +00001513 case PP_TOKEN: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001514 // If we see a TOKEN before a PP_MACRO_*, then the file is
1515 // erroneous, just pretend we didn't see this.
1516 if (Macro == 0) break;
Mike Stump11289f42009-09-09 15:08:12 +00001517
Douglas Gregorc3366a52009-04-21 23:56:24 +00001518 Token Tok;
1519 Tok.startToken();
Sebastian Redl2c373b92010-10-05 15:59:54 +00001520 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001521 Tok.setLength(Record[1]);
Douglas Gregora3e41532011-07-28 20:55:49 +00001522 if (IdentifierInfo *II = getLocalIdentifier(F, Record[2]))
Douglas Gregorc3366a52009-04-21 23:56:24 +00001523 Tok.setIdentifierInfo(II);
1524 Tok.setKind((tok::TokenKind)Record[3]);
1525 Tok.setFlag((Token::TokenFlags)Record[4]);
1526 Macro->AddTokenToBody(Tok);
1527 break;
1528 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00001529 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001530 }
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001531
Douglas Gregor7cb0d012011-08-04 18:09:14 +00001532 return;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001533}
1534
Douglas Gregora6895d82011-07-22 16:00:58 +00001535PreprocessedEntity *ASTReader::LoadPreprocessedEntity(Module &F) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00001536 assert(PP && "Forgot to set Preprocessor ?");
1537 unsigned Code = F.PreprocessorDetailCursor.ReadCode();
1538 switch (Code) {
1539 case llvm::bitc::END_BLOCK:
1540 return 0;
1541
1542 case llvm::bitc::ENTER_SUBBLOCK:
1543 Error("unexpected subblock record in preprocessor detail block");
1544 return 0;
1545
1546 case llvm::bitc::DEFINE_ABBREV:
1547 Error("unexpected abbrevation record in preprocessor detail block");
1548 return 0;
1549
1550 default:
1551 break;
1552 }
1553
1554 if (!PP->getPreprocessingRecord()) {
1555 Error("no preprocessing record");
1556 return 0;
1557 }
1558
1559 // Read the record.
1560 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1561 const char *BlobStart = 0;
1562 unsigned BlobLen = 0;
1563 RecordData Record;
1564 PreprocessorDetailRecordTypes RecType =
1565 (PreprocessorDetailRecordTypes)F.PreprocessorDetailCursor.ReadRecord(
1566 Code, Record, BlobStart, BlobLen);
1567 switch (RecType) {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001568 case PPD_MACRO_EXPANSION: {
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001569 PreprocessedEntityID GlobalID = getGlobalPreprocessedEntityID(F, Record[0]);
Douglas Gregor0d4b4312011-08-04 17:06:18 +00001570 if (PreprocessedEntity *PE = PPRec.getLoadedPreprocessedEntity(GlobalID-1))
Douglas Gregor92a96f52011-02-08 21:58:10 +00001571 return PE;
1572
Chandler Carrutha88a22182011-07-14 08:20:46 +00001573 MacroExpansion *ME =
Douglas Gregora3e41532011-07-28 20:55:49 +00001574 new (PPRec) MacroExpansion(getLocalIdentifier(F, Record[3]),
Douglas Gregor92a96f52011-02-08 21:58:10 +00001575 SourceRange(ReadSourceLocation(F, Record[1]),
1576 ReadSourceLocation(F, Record[2])),
Douglas Gregor035611e2011-07-28 22:16:57 +00001577 getLocalMacroDefinition(F, Record[4]));
Douglas Gregor0d4b4312011-08-04 17:06:18 +00001578 PPRec.setLoadedPreallocatedEntity(GlobalID - 1, ME);
Chandler Carrutha88a22182011-07-14 08:20:46 +00001579 return ME;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001580 }
1581
1582 case PPD_MACRO_DEFINITION: {
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001583 PreprocessedEntityID GlobalID = getGlobalPreprocessedEntityID(F, Record[0]);
Douglas Gregor0d4b4312011-08-04 17:06:18 +00001584 if (PreprocessedEntity *PE = PPRec.getLoadedPreprocessedEntity(GlobalID-1))
Douglas Gregor92a96f52011-02-08 21:58:10 +00001585 return PE;
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001586
1587 unsigned MacroDefID = getGlobalMacroDefinitionID(F, Record[1]);
1588 if (MacroDefID > MacroDefinitionsLoaded.size()) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00001589 Error("out-of-bounds macro definition record");
1590 return 0;
1591 }
1592
1593 // Decode the identifier info and then check again; if the macro is
1594 // still defined and associated with the identifier,
Douglas Gregora3e41532011-07-28 20:55:49 +00001595 IdentifierInfo *II = getLocalIdentifier(F, Record[4]);
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001596 if (!MacroDefinitionsLoaded[MacroDefID - 1]) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00001597 MacroDefinition *MD
1598 = new (PPRec) MacroDefinition(II,
1599 ReadSourceLocation(F, Record[5]),
1600 SourceRange(
1601 ReadSourceLocation(F, Record[2]),
1602 ReadSourceLocation(F, Record[3])));
1603
Douglas Gregor0d4b4312011-08-04 17:06:18 +00001604 PPRec.setLoadedPreallocatedEntity(GlobalID - 1, MD);
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001605 MacroDefinitionsLoaded[MacroDefID - 1] = MD;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001606
1607 if (DeserializationListener)
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001608 DeserializationListener->MacroDefinitionRead(MacroDefID, MD);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001609 }
1610
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001611 return MacroDefinitionsLoaded[MacroDefID - 1];
Douglas Gregor92a96f52011-02-08 21:58:10 +00001612 }
1613
1614 case PPD_INCLUSION_DIRECTIVE: {
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001615 PreprocessedEntityID GlobalID = getGlobalPreprocessedEntityID(F, Record[0]);
Douglas Gregor0d4b4312011-08-04 17:06:18 +00001616 if (PreprocessedEntity *PE = PPRec.getLoadedPreprocessedEntity(GlobalID-1))
Douglas Gregor92a96f52011-02-08 21:58:10 +00001617 return PE;
1618
1619 const char *FullFileNameStart = BlobStart + Record[3];
1620 const FileEntry *File
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001621 = PP->getFileManager().getFile(StringRef(FullFileNameStart,
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001622 BlobLen - Record[3]));
Douglas Gregor92a96f52011-02-08 21:58:10 +00001623
1624 // FIXME: Stable encoding
1625 InclusionDirective::InclusionKind Kind
1626 = static_cast<InclusionDirective::InclusionKind>(Record[5]);
1627 InclusionDirective *ID
1628 = new (PPRec) InclusionDirective(PPRec, Kind,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001629 StringRef(BlobStart, Record[3]),
Douglas Gregor92a96f52011-02-08 21:58:10 +00001630 Record[4],
1631 File,
1632 SourceRange(ReadSourceLocation(F, Record[1]),
1633 ReadSourceLocation(F, Record[2])));
Douglas Gregor0d4b4312011-08-04 17:06:18 +00001634 PPRec.setLoadedPreallocatedEntity(GlobalID - 1, ID);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001635 return ID;
1636 }
1637 }
1638
1639 Error("invalid offset in preprocessor detail block");
1640 return 0;
1641}
1642
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001643PreprocessedEntityID
1644ASTReader::getGlobalPreprocessedEntityID(Module &M, unsigned LocalID) {
Douglas Gregor2f555fc2011-08-04 18:56:47 +00001645 ContinuousRangeMap<uint32_t, int, 2>::iterator
1646 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1647 assert(I != M.PreprocessedEntityRemap.end()
1648 && "Invalid index into preprocessed entity index remap");
1649
1650 return LocalID + I->second;
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001651}
1652
Douglas Gregor09b69892011-02-10 17:09:37 +00001653namespace {
1654 /// \brief Trait class used to search the on-disk hash table containing all of
1655 /// the header search information.
1656 ///
1657 /// The on-disk hash table contains a mapping from each header path to
1658 /// information about that header (how many times it has been included, its
1659 /// controlling macro, etc.). Note that we actually hash based on the
1660 /// filename, and support "deep" comparisons of file names based on current
1661 /// inode numbers, so that the search can cope with non-normalized path names
1662 /// and symlinks.
1663 class HeaderFileInfoTrait {
Douglas Gregora3e41532011-07-28 20:55:49 +00001664 ASTReader &Reader;
1665 Module &M;
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001666 HeaderSearch *HS;
1667 const char *FrameworkStrings;
Douglas Gregor09b69892011-02-10 17:09:37 +00001668 const char *SearchPath;
1669 struct stat SearchPathStatBuf;
1670 llvm::Optional<int> SearchPathStatResult;
1671
1672 int StatSimpleCache(const char *Path, struct stat *StatBuf) {
1673 if (Path == SearchPath) {
1674 if (!SearchPathStatResult)
1675 SearchPathStatResult = stat(Path, &SearchPathStatBuf);
1676
1677 *StatBuf = SearchPathStatBuf;
1678 return *SearchPathStatResult;
1679 }
1680
1681 return stat(Path, StatBuf);
1682 }
1683
1684 public:
1685 typedef const char *external_key_type;
1686 typedef const char *internal_key_type;
1687
1688 typedef HeaderFileInfo data_type;
1689
Douglas Gregora3e41532011-07-28 20:55:49 +00001690 HeaderFileInfoTrait(ASTReader &Reader, Module &M, HeaderSearch *HS,
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001691 const char *FrameworkStrings,
1692 const char *SearchPath = 0)
Douglas Gregora3e41532011-07-28 20:55:49 +00001693 : Reader(Reader), M(M), HS(HS), FrameworkStrings(FrameworkStrings),
1694 SearchPath(SearchPath) { }
Douglas Gregor09b69892011-02-10 17:09:37 +00001695
1696 static unsigned ComputeHash(const char *path) {
1697 return llvm::HashString(llvm::sys::path::filename(path));
1698 }
1699
1700 static internal_key_type GetInternalKey(const char *path) { return path; }
1701
1702 bool EqualKey(internal_key_type a, internal_key_type b) {
1703 if (strcmp(a, b) == 0)
1704 return true;
1705
1706 if (llvm::sys::path::filename(a) != llvm::sys::path::filename(b))
1707 return false;
1708
1709 // The file names match, but the path names don't. stat() the files to
1710 // see if they are the same.
1711 struct stat StatBufA, StatBufB;
1712 if (StatSimpleCache(a, &StatBufA) || StatSimpleCache(b, &StatBufB))
1713 return false;
1714
1715 return StatBufA.st_ino == StatBufB.st_ino;
1716 }
1717
1718 static std::pair<unsigned, unsigned>
1719 ReadKeyDataLength(const unsigned char*& d) {
1720 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1721 unsigned DataLen = (unsigned) *d++;
1722 return std::make_pair(KeyLen + 1, DataLen);
1723 }
1724
1725 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1726 return (const char *)d;
1727 }
1728
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001729 data_type ReadData(const internal_key_type, const unsigned char *d,
1730 unsigned DataLen) {
Douglas Gregor09b69892011-02-10 17:09:37 +00001731 const unsigned char *End = d + DataLen;
1732 using namespace clang::io;
1733 HeaderFileInfo HFI;
1734 unsigned Flags = *d++;
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001735 HFI.isImport = (Flags >> 5) & 0x01;
1736 HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1737 HFI.DirInfo = (Flags >> 2) & 0x03;
1738 HFI.Resolved = (Flags >> 1) & 0x01;
1739 HFI.IndexHeaderMapHeader = Flags & 0x01;
Douglas Gregor09b69892011-02-10 17:09:37 +00001740 HFI.NumIncludes = ReadUnalignedLE16(d);
Douglas Gregora3e41532011-07-28 20:55:49 +00001741 HFI.ControllingMacroID = Reader.getGlobalDeclID(M, ReadUnalignedLE32(d));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001742 if (unsigned FrameworkOffset = ReadUnalignedLE32(d)) {
1743 // The framework offset is 1 greater than the actual offset,
1744 // since 0 is used as an indicator for "no framework name".
1745 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1746 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1747 }
1748
Douglas Gregor09b69892011-02-10 17:09:37 +00001749 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1750 (void)End;
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001751
Douglas Gregor09b69892011-02-10 17:09:37 +00001752 // This HeaderFileInfo was externally loaded.
1753 HFI.External = true;
1754 return HFI;
1755 }
1756 };
1757}
1758
1759/// \brief The on-disk hash table used for the global method pool.
1760typedef OnDiskChainedHashTable<HeaderFileInfoTrait>
1761 HeaderFileInfoLookupTable;
1762
Douglas Gregora6895d82011-07-22 16:00:58 +00001763void ASTReader::SetIdentifierIsMacro(IdentifierInfo *II, Module &F,
Douglas Gregor074fdc52011-07-28 21:16:51 +00001764 uint64_t LocalOffset) {
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001765 // Note that this identifier has a macro definition.
1766 II->setHasMacroDefinition(true);
1767
Douglas Gregord32f0352011-07-22 06:10:01 +00001768 // Adjust the offset to a global offset.
Douglas Gregor074fdc52011-07-28 21:16:51 +00001769 UnreadMacroRecordOffsets[II] = F.GlobalBitOffset + LocalOffset;
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001770}
1771
Sebastian Redl2c499f62010-08-18 23:56:43 +00001772void ASTReader::ReadDefinedMacros() {
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00001773 for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1774 E = ModuleMgr.rend(); I != E; ++I) {
1775 llvm::BitstreamCursor &MacroCursor = (*I)->MacroCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001776
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001777 // If there was no preprocessor block, skip this file.
1778 if (!MacroCursor.getBitStreamReader())
1779 continue;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001780
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001781 llvm::BitstreamCursor Cursor = MacroCursor;
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00001782 Cursor.JumpToBit((*I)->MacroStartOffset);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001783
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001784 RecordData Record;
1785 while (true) {
1786 unsigned Code = Cursor.ReadCode();
Douglas Gregor796d76a2010-10-20 22:00:55 +00001787 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001788 break;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001789
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001790 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1791 // No known subblocks, always skip them.
1792 Cursor.ReadSubBlockID();
1793 if (Cursor.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001794 Error("malformed block record in AST file");
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001795 return;
1796 }
1797 continue;
1798 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001799
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001800 if (Code == llvm::bitc::DEFINE_ABBREV) {
1801 Cursor.ReadAbbrevRecord();
1802 continue;
1803 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001804
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001805 // Read a record.
1806 const char *BlobStart;
1807 unsigned BlobLen;
1808 Record.clear();
1809 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1810 default: // Default behavior: ignore.
1811 break;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001812
Sebastian Redl539c5062010-08-18 23:57:32 +00001813 case PP_MACRO_OBJECT_LIKE:
1814 case PP_MACRO_FUNCTION_LIKE:
Douglas Gregora3e41532011-07-28 20:55:49 +00001815 getLocalIdentifier(**I, Record[0]);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001816 break;
1817
Sebastian Redl539c5062010-08-18 23:57:32 +00001818 case PP_TOKEN:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001819 // Ignore tokens.
1820 break;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001821 }
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001822 }
1823 }
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001824
1825 // Drain the unread macro-record offsets map.
1826 while (!UnreadMacroRecordOffsets.empty())
1827 LoadMacroDefinition(UnreadMacroRecordOffsets.begin());
1828}
1829
1830void ASTReader::LoadMacroDefinition(
1831 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos) {
1832 assert(Pos != UnreadMacroRecordOffsets.end() && "Unknown macro definition");
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001833 uint64_t Offset = Pos->second;
1834 UnreadMacroRecordOffsets.erase(Pos);
1835
Douglas Gregord32f0352011-07-22 06:10:01 +00001836 RecordLocation Loc = getLocalBitOffset(Offset);
1837 ReadMacroRecord(*Loc.F, Loc.Offset);
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001838}
1839
1840void ASTReader::LoadMacroDefinition(IdentifierInfo *II) {
1841 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos
1842 = UnreadMacroRecordOffsets.find(II);
1843 LoadMacroDefinition(Pos);
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001844}
1845
Sebastian Redl50e26582010-09-15 19:54:06 +00001846MacroDefinition *ASTReader::getMacroDefinition(MacroID ID) {
Douglas Gregor91096292010-10-02 19:29:26 +00001847 if (ID == 0 || ID > MacroDefinitionsLoaded.size())
Douglas Gregoraae92242010-03-19 21:51:54 +00001848 return 0;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001849
Douglas Gregor91096292010-10-02 19:29:26 +00001850 if (!MacroDefinitionsLoaded[ID - 1]) {
Douglas Gregor270e0142011-07-20 01:29:15 +00001851 GlobalMacroDefinitionMapType::iterator I =GlobalMacroDefinitionMap.find(ID);
1852 assert(I != GlobalMacroDefinitionMap.end() &&
1853 "Corrupted global macro definition map");
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00001854 Module &M = *I->second;
1855 unsigned Index = ID - 1 - M.BaseMacroDefinitionID;
1856 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
1857 M.PreprocessorDetailCursor.JumpToBit(M.MacroDefinitionOffsets[Index]);
1858 LoadPreprocessedEntity(M);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001859 }
1860
Douglas Gregor91096292010-10-02 19:29:26 +00001861 return MacroDefinitionsLoaded[ID - 1];
Douglas Gregoraae92242010-03-19 21:51:54 +00001862}
1863
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001864const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) {
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00001865 std::string Filename = filenameStrRef;
1866 MaybeAddSystemRootToFilename(Filename);
1867 const FileEntry *File = FileMgr.getFile(Filename);
1868 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1869 OriginalDir != CurrentDir) {
1870 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1871 OriginalDir,
1872 CurrentDir);
1873 if (!resolved.empty())
1874 File = FileMgr.getFile(resolved);
1875 }
1876
1877 return File;
1878}
1879
Douglas Gregor035611e2011-07-28 22:16:57 +00001880MacroID ASTReader::getGlobalMacroDefinitionID(Module &M, unsigned LocalID) {
Douglas Gregora863b4b2011-08-04 16:36:56 +00001881 if (LocalID < NUM_PREDEF_MACRO_IDS)
1882 return LocalID;
1883
1884 ContinuousRangeMap<uint32_t, int, 2>::iterator I
1885 = M.MacroDefinitionRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
1886 assert(I != M.MacroDefinitionRemap.end() &&
1887 "Invalid index into macro definition ID remap");
1888
1889 return LocalID + I->second;
Douglas Gregor035611e2011-07-28 22:16:57 +00001890}
1891
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001892/// \brief If we are loading a relocatable PCH file, and the filename is
1893/// not an absolute path, add the system root to the beginning of the file
1894/// name.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001895void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001896 // If this is not a relocatable PCH file, there's nothing to do.
1897 if (!RelocatablePCH)
1898 return;
Mike Stump11289f42009-09-09 15:08:12 +00001899
Michael J. Spencerf28df4c2010-12-17 21:22:22 +00001900 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001901 return;
1902
Douglas Gregorc567ba22011-07-22 16:35:34 +00001903 if (isysroot.empty()) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001904 // If no system root was given, default to '/'
1905 Filename.insert(Filename.begin(), '/');
1906 return;
1907 }
Mike Stump11289f42009-09-09 15:08:12 +00001908
Douglas Gregorc567ba22011-07-22 16:35:34 +00001909 unsigned Length = isysroot.size();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001910 if (isysroot[Length - 1] != '/')
1911 Filename.insert(Filename.begin(), '/');
Mike Stump11289f42009-09-09 15:08:12 +00001912
Douglas Gregorc567ba22011-07-22 16:35:34 +00001913 Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001914}
1915
Sebastian Redl2c499f62010-08-18 23:56:43 +00001916ASTReader::ASTReadResult
Douglas Gregora6895d82011-07-22 16:00:58 +00001917ASTReader::ReadASTBlock(Module &F) {
Sebastian Redl34522812010-07-16 17:50:48 +00001918 llvm::BitstreamCursor &Stream = F.Stream;
1919
Sebastian Redl539c5062010-08-18 23:57:32 +00001920 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001921 Error("malformed block record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001922 return Failure;
1923 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001924
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001925 // Read all of the records and blocks for the ASt file.
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001926 RecordData Record;
Sebastian Redl393f8b72010-07-19 20:52:06 +00001927 bool First = true;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001928 while (!Stream.AtEndOfStream()) {
1929 unsigned Code = Stream.ReadCode();
1930 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor55abb232009-04-10 20:39:37 +00001931 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001932 Error("error at end of module block in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001933 return Failure;
1934 }
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001935
Douglas Gregor55abb232009-04-10 20:39:37 +00001936 return Success;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001937 }
1938
1939 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1940 switch (Stream.ReadSubBlockID()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001941 case DECLTYPES_BLOCK_ID:
Chris Lattnere78a6be2009-04-27 01:05:14 +00001942 // We lazily load the decls block, but we want to set up the
1943 // DeclsCursor cursor to point into it. Clone our current bitcode
1944 // cursor to it, enter the block and read the abbrevs in that block.
1945 // With the main cursor, we just skip over it.
Sebastian Redl34522812010-07-16 17:50:48 +00001946 F.DeclsCursor = Stream;
Chris Lattnere78a6be2009-04-27 01:05:14 +00001947 if (Stream.SkipBlock() || // Skip with the main cursor.
1948 // Read the abbrevs.
Sebastian Redl539c5062010-08-18 23:57:32 +00001949 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001950 Error("malformed block record in AST file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001951 return Failure;
1952 }
1953 break;
Mike Stump11289f42009-09-09 15:08:12 +00001954
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00001955 case DECL_UPDATES_BLOCK_ID:
1956 if (Stream.SkipBlock()) {
1957 Error("malformed block record in AST file");
1958 return Failure;
1959 }
1960 break;
1961
Sebastian Redl539c5062010-08-18 23:57:32 +00001962 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl34522812010-07-16 17:50:48 +00001963 F.MacroCursor = Stream;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001964 if (PP)
1965 PP->setExternalSource(this);
1966
Douglas Gregor796d76a2010-10-20 22:00:55 +00001967 if (Stream.SkipBlock() ||
1968 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001969 Error("malformed block record in AST file");
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001970 return Failure;
1971 }
Douglas Gregor796d76a2010-10-20 22:00:55 +00001972 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001973 break;
Steve Naroff2ddea052009-04-23 10:39:46 +00001974
Douglas Gregor92a96f52011-02-08 21:58:10 +00001975 case PREPROCESSOR_DETAIL_BLOCK_ID:
1976 F.PreprocessorDetailCursor = Stream;
1977 if (Stream.SkipBlock() ||
1978 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
1979 PREPROCESSOR_DETAIL_BLOCK_ID)) {
1980 Error("malformed preprocessor detail record in AST file");
1981 return Failure;
1982 }
1983 F.PreprocessorDetailStartOffset
1984 = F.PreprocessorDetailCursor.GetCurrentBitNo();
1985 break;
1986
Sebastian Redl539c5062010-08-18 23:57:32 +00001987 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl393f8b72010-07-19 20:52:06 +00001988 switch (ReadSourceManagerBlock(F)) {
Douglas Gregor92863e42009-04-10 23:10:45 +00001989 case Success:
1990 break;
1991
1992 case Failure:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001993 Error("malformed source manager block in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001994 return Failure;
Douglas Gregor92863e42009-04-10 23:10:45 +00001995
1996 case IgnorePCH:
1997 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00001998 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001999 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002000 }
Sebastian Redl393f8b72010-07-19 20:52:06 +00002001 First = false;
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00002002 continue;
2003 }
2004
2005 if (Code == llvm::bitc::DEFINE_ABBREV) {
2006 Stream.ReadAbbrevRecord();
2007 continue;
2008 }
2009
2010 // Read and process a record.
2011 Record.clear();
Douglas Gregorbfbde532009-04-10 21:16:55 +00002012 const char *BlobStart = 0;
2013 unsigned BlobLen = 0;
Sebastian Redl539c5062010-08-18 23:57:32 +00002014 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redl2c373b92010-10-05 15:59:54 +00002015 &BlobStart, &BlobLen)) {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00002016 default: // Default behavior: ignore.
2017 break;
2018
Sebastian Redl539c5062010-08-18 23:57:32 +00002019 case METADATA: {
2020 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2021 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002022 : diag::warn_pch_version_too_new);
2023 return IgnorePCH;
2024 }
2025
2026 RelocatablePCH = Record[4];
2027 if (Listener) {
2028 std::string TargetTriple(BlobStart, BlobLen);
2029 if (Listener->ReadTargetTriple(TargetTriple))
2030 return IgnorePCH;
2031 }
2032 break;
2033 }
2034
Douglas Gregor29cc6422011-08-17 21:07:30 +00002035 case IMPORTS: {
2036 // Load each of the imported PCH files.
2037 unsigned Idx = 0, N = Record.size();
2038 while (Idx < N) {
2039 // Read information about the AST file.
2040 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2041 unsigned Length = Record[Idx++];
2042 llvm::SmallString<128> ImportedFile(Record.begin() + Idx,
2043 Record.begin() + Idx + Length);
2044 Idx += Length;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002045
Douglas Gregor29cc6422011-08-17 21:07:30 +00002046 // Load the AST file.
Douglas Gregordf0c1512011-08-18 04:12:04 +00002047 switch(ReadASTCore(ImportedFile, ImportedKind, &F)) {
Douglas Gregor29cc6422011-08-17 21:07:30 +00002048 case Failure: return Failure;
2049 // If we have to ignore the dependency, we'll have to ignore this too.
2050 case IgnorePCH: return IgnorePCH;
2051 case Success: break;
2052 }
2053 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002054 break;
2055 }
2056
Douglas Gregor5204bde2011-08-02 16:26:37 +00002057 case TYPE_OFFSET: {
Sebastian Redl9e687992010-07-19 22:06:55 +00002058 if (F.LocalNumTypes != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002059 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00002060 return Failure;
2061 }
Sebastian Redl9e687992010-07-19 22:06:55 +00002062 F.TypeOffsets = (const uint32_t *)BlobStart;
2063 F.LocalNumTypes = Record[0];
Douglas Gregor3b65ed02011-08-02 18:32:54 +00002064 unsigned LocalBaseTypeIndex = Record[1];
2065 F.BaseTypeIndex = getTotalNumTypes();
Douglas Gregor8ab4ea82011-07-29 00:21:44 +00002066
Douglas Gregor5204bde2011-08-02 16:26:37 +00002067 if (F.LocalNumTypes > 0) {
2068 // Introduce the global -> local mapping for types within this module.
2069 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
2070
2071 // Introduce the local -> global mapping for types within this module.
Douglas Gregor3b65ed02011-08-02 18:32:54 +00002072 F.TypeRemap.insert(std::make_pair(LocalBaseTypeIndex,
2073 F.BaseTypeIndex - LocalBaseTypeIndex));
Douglas Gregor5204bde2011-08-02 16:26:37 +00002074
2075 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
2076 }
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00002077 break;
Douglas Gregor5204bde2011-08-02 16:26:37 +00002078 }
2079
Douglas Gregorf7180622011-08-03 15:48:04 +00002080 case DECL_OFFSET: {
Sebastian Redl9e687992010-07-19 22:06:55 +00002081 if (F.LocalNumDecls != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002082 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00002083 return Failure;
2084 }
Sebastian Redl9e687992010-07-19 22:06:55 +00002085 F.DeclOffsets = (const uint32_t *)BlobStart;
2086 F.LocalNumDecls = Record[0];
Douglas Gregorf7180622011-08-03 15:48:04 +00002087 unsigned LocalBaseDeclID = Record[1];
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00002088 F.BaseDeclID = getTotalNumDecls();
Douglas Gregor047d2ef2011-07-20 00:27:43 +00002089
Douglas Gregorf7180622011-08-03 15:48:04 +00002090 if (F.LocalNumDecls > 0) {
2091 // Introduce the global -> local mapping for declarations within this
2092 // module.
Douglas Gregordab42432011-08-12 00:15:20 +00002093 GlobalDeclMap.insert(
2094 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
Douglas Gregorf7180622011-08-03 15:48:04 +00002095
2096 // Introduce the local -> global mapping for declarations within this
2097 // module.
2098 F.DeclRemap.insert(std::make_pair(LocalBaseDeclID,
2099 F.BaseDeclID - LocalBaseDeclID));
2100
2101 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
2102 }
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00002103 break;
Douglas Gregorf7180622011-08-03 15:48:04 +00002104 }
2105
Sebastian Redl539c5062010-08-18 23:57:32 +00002106 case TU_UPDATE_LEXICAL: {
Douglas Gregordab42432011-08-12 00:15:20 +00002107 DeclContext *TU = Context ? Context->getTranslationUnitDecl() : 0;
Douglas Gregor94619c82011-08-24 19:03:07 +00002108 DeclContextInfo &Info = F.DeclContextInfos[TU];
2109 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(BlobStart);
2110 Info.NumLexicalDecls
2111 = static_cast<unsigned int>(BlobLen / sizeof(KindDeclIDPair));
Douglas Gregordab42432011-08-12 00:15:20 +00002112 if (TU)
2113 TU->setHasExternalLexicalStorage(true);
2114
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002115 break;
2116 }
2117
Sebastian Redld7dce0a2010-08-24 00:50:04 +00002118 case UPDATE_VISIBLE: {
Douglas Gregorf7180622011-08-03 15:48:04 +00002119 unsigned Idx = 0;
2120 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
Sebastian Redld7dce0a2010-08-24 00:50:04 +00002121 void *Table = ASTDeclContextNameLookupTable::Create(
Douglas Gregorf7180622011-08-03 15:48:04 +00002122 (const unsigned char *)BlobStart + Record[Idx++],
Sebastian Redld7dce0a2010-08-24 00:50:04 +00002123 (const unsigned char *)BlobStart,
Douglas Gregor903b7e92011-07-22 00:38:23 +00002124 ASTDeclContextNameLookupTrait(*this, F));
Douglas Gregordab42432011-08-12 00:15:20 +00002125 if (ID == PREDEF_DECL_TRANSLATION_UNIT_ID && Context) { // Is it the TU?
Douglas Gregordab42432011-08-12 00:15:20 +00002126 DeclContext *TU = Context->getTranslationUnitDecl();
Douglas Gregor94619c82011-08-24 19:03:07 +00002127 F.DeclContextInfos[TU].NameLookupTableData = Table;
Douglas Gregordab42432011-08-12 00:15:20 +00002128 TU->setHasExternalVisibleStorage(true);
Sebastian Redld7dce0a2010-08-24 00:50:04 +00002129 } else
Douglas Gregorf7180622011-08-03 15:48:04 +00002130 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
Sebastian Redld7dce0a2010-08-24 00:50:04 +00002131 break;
2132 }
2133
Sebastian Redl539c5062010-08-18 23:57:32 +00002134 case REDECLS_UPDATE_LATEST: {
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002135 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
Douglas Gregorf7180622011-08-03 15:48:04 +00002136 for (unsigned i = 0, e = Record.size(); i < e; /* in loop */) {
2137 DeclID First = ReadDeclID(F, Record, i);
2138 DeclID Latest = ReadDeclID(F, Record, i);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002139 FirstLatestDeclIDs[First] = Latest;
2140 }
2141 break;
2142 }
2143
Sebastian Redl539c5062010-08-18 23:57:32 +00002144 case LANGUAGE_OPTIONS:
Douglas Gregorce3a8292010-07-27 00:27:13 +00002145 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor55abb232009-04-10 20:39:37 +00002146 return IgnorePCH;
2147 break;
Douglas Gregorbfbde532009-04-10 21:16:55 +00002148
Sebastian Redl539c5062010-08-18 23:57:32 +00002149 case IDENTIFIER_TABLE:
Sebastian Redl393f8b72010-07-19 20:52:06 +00002150 F.IdentifierTableData = BlobStart;
Douglas Gregor0e149972009-04-25 19:10:14 +00002151 if (Record[0]) {
Sebastian Redl393f8b72010-07-19 20:52:06 +00002152 F.IdentifierLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002153 = ASTIdentifierLookupTable::Create(
Sebastian Redl393f8b72010-07-19 20:52:06 +00002154 (const unsigned char *)F.IdentifierTableData + Record[0],
2155 (const unsigned char *)F.IdentifierTableData,
Sebastian Redl2c373b92010-10-05 15:59:54 +00002156 ASTIdentifierLookupTrait(*this, F));
Douglas Gregor49bf76b2011-07-21 18:46:38 +00002157 if (PP) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002158 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor49bf76b2011-07-21 18:46:38 +00002159 PP->getHeaderSearchInfo().SetExternalLookup(this);
2160 }
Douglas Gregor0e149972009-04-25 19:10:14 +00002161 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002162 break;
2163
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002164 case IDENTIFIER_OFFSET: {
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002165 if (F.LocalNumIdentifiers != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002166 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002167 return Failure;
2168 }
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002169 F.IdentifierOffsets = (const uint32_t *)BlobStart;
2170 F.LocalNumIdentifiers = Record[0];
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002171 unsigned LocalBaseIdentifierID = Record[1];
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00002172 F.BaseIdentifierID = getTotalNumIdentifiers();
Douglas Gregor19d26352011-07-20 00:59:32 +00002173
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002174 if (F.LocalNumIdentifiers > 0) {
2175 // Introduce the global -> local mapping for identifiers within this
2176 // module.
2177 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
2178 &F));
2179
2180 // Introduce the local -> global mapping for identifiers within this
2181 // module.
2182 F.IdentifierRemap.insert(
2183 std::make_pair(LocalBaseIdentifierID,
2184 F.BaseIdentifierID - LocalBaseIdentifierID));
2185
2186 IdentifiersLoaded.resize(IdentifiersLoaded.size()
2187 + F.LocalNumIdentifiers);
2188 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002189 break;
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002190 }
2191
Sebastian Redl539c5062010-08-18 23:57:32 +00002192 case EXTERNAL_DEFINITIONS:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002193 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2194 ExternalDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002195 break;
Douglas Gregor08f01292009-04-17 22:13:46 +00002196
Sebastian Redl539c5062010-08-18 23:57:32 +00002197 case SPECIAL_TYPES:
Douglas Gregor903b7e92011-07-22 00:38:23 +00002198 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2199 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
Douglas Gregor652d82a2009-04-18 05:55:16 +00002200 break;
2201
Sebastian Redl539c5062010-08-18 23:57:32 +00002202 case STATISTICS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002203 TotalNumStatements += Record[0];
2204 TotalNumMacros += Record[1];
2205 TotalLexicalDeclContexts += Record[2];
2206 TotalVisibleDeclContexts += Record[3];
Douglas Gregor08f01292009-04-17 22:13:46 +00002207 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00002208
Sebastian Redl539c5062010-08-18 23:57:32 +00002209 case UNUSED_FILESCOPED_DECLS:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002210 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2211 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
Tanya Lattner90073802010-02-12 00:07:30 +00002212 break;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00002213
Alexis Hunt27a761d2011-05-04 23:29:54 +00002214 case DELEGATING_CTORS:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002215 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2216 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
Alexis Hunt27a761d2011-05-04 23:29:54 +00002217 break;
2218
Sebastian Redl539c5062010-08-18 23:57:32 +00002219 case WEAK_UNDECLARED_IDENTIFIERS:
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00002220 if (Record.size() % 4 != 0) {
2221 Error("invalid weak identifiers record");
2222 return Failure;
2223 }
2224
2225 // FIXME: Ignore weak undeclared identifiers from non-original PCH
2226 // files. This isn't the way to do it :)
2227 WeakUndeclaredIdentifiers.clear();
2228
2229 // Translate the weak, undeclared identifiers into global IDs.
2230 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2231 WeakUndeclaredIdentifiers.push_back(
2232 getGlobalIdentifierID(F, Record[I++]));
2233 WeakUndeclaredIdentifiers.push_back(
2234 getGlobalIdentifierID(F, Record[I++]));
2235 WeakUndeclaredIdentifiers.push_back(
2236 ReadSourceLocation(F, Record, I).getRawEncoding());
2237 WeakUndeclaredIdentifiers.push_back(Record[I++]);
2238 }
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002239 break;
2240
Sebastian Redl539c5062010-08-18 23:57:32 +00002241 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002242 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2243 LocallyScopedExternalDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002244 break;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002245
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002246 case SELECTOR_OFFSETS: {
Sebastian Redla19a67f2010-08-03 21:58:15 +00002247 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redlada023c2010-08-04 20:40:17 +00002248 F.LocalNumSelectors = Record[0];
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002249 unsigned LocalBaseSelectorID = Record[1];
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00002250 F.BaseSelectorID = getTotalNumSelectors();
Douglas Gregor2262d282011-07-20 01:10:58 +00002251
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002252 if (F.LocalNumSelectors > 0) {
2253 // Introduce the global -> local mapping for selectors within this
2254 // module.
2255 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2256
2257 // Introduce the local -> global mapping for selectors within this
2258 // module.
2259 F.SelectorRemap.insert(std::make_pair(LocalBaseSelectorID,
2260 F.BaseSelectorID - LocalBaseSelectorID));
Douglas Gregor95c13f52009-04-25 17:48:32 +00002261
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002262 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
2263 }
2264 break;
2265 }
2266
Sebastian Redl539c5062010-08-18 23:57:32 +00002267 case METHOD_POOL:
Sebastian Redlada023c2010-08-04 20:40:17 +00002268 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002269 if (Record[0])
Sebastian Redlada023c2010-08-04 20:40:17 +00002270 F.SelectorLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002271 = ASTSelectorLookupTable::Create(
Sebastian Redlada023c2010-08-04 20:40:17 +00002272 F.SelectorLookupTableData + Record[0],
2273 F.SelectorLookupTableData,
Douglas Gregor7fb09192011-07-21 22:35:25 +00002274 ASTSelectorLookupTrait(*this, F));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002275 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorc78d3462009-04-24 21:10:55 +00002276 break;
Douglas Gregoreda6a892009-04-26 00:07:37 +00002277
Sebastian Redl96371b42010-09-22 00:42:30 +00002278 case REFERENCED_SELECTOR_POOL:
Douglas Gregor3f8f04f2011-07-28 14:41:43 +00002279 if (!Record.empty()) {
2280 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2281 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2282 Record[Idx++]));
2283 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2284 getRawEncoding());
2285 }
2286 }
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002287 break;
2288
Sebastian Redl539c5062010-08-18 23:57:32 +00002289 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002290 if (!Record.empty() && Listener)
2291 Listener->ReadCounter(Record[0]);
Douglas Gregoreda6a892009-04-26 00:07:37 +00002292 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00002293
Douglas Gregor925296b2011-07-19 16:10:42 +00002294 case SOURCE_LOCATION_OFFSETS: {
2295 F.SLocEntryOffsets = (const uint32_t *)BlobStart;
Sebastian Redlb293a452010-07-20 21:20:32 +00002296 F.LocalNumSLocEntries = Record[0];
Douglas Gregor925296b2011-07-19 16:10:42 +00002297 llvm::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
2298 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries, Record[1]);
2299 // Make our entry in the range map. BaseID is negative and growing, so
2300 // we invert it. Because we invert it, though, we need the other end of
2301 // the range.
2302 unsigned RangeStart =
2303 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2304 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2305 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2306
2307 // Initialize the remapping table.
2308 // Invalid stays invalid.
2309 F.SLocRemap.insert(std::make_pair(0U, 0));
2310 // This module. Base was 2 when being compiled.
2311 F.SLocRemap.insert(std::make_pair(2U,
2312 static_cast<int>(F.SLocEntryBaseOffset - 2)));
Douglas Gregor49bf76b2011-07-21 18:46:38 +00002313
2314 TotalNumSLocEntries += F.LocalNumSLocEntries;
Douglas Gregor925296b2011-07-19 16:10:42 +00002315 break;
2316 }
2317
Douglas Gregor5a1797c2011-08-01 16:01:55 +00002318 case MODULE_OFFSET_MAP: {
Douglas Gregor925296b2011-07-19 16:10:42 +00002319 // Additional remapping information.
2320 const unsigned char *Data = (const unsigned char*)BlobStart;
2321 const unsigned char *DataEnd = Data + BlobLen;
Douglas Gregor00659902011-08-02 10:56:51 +00002322
2323 // Continuous range maps we may be updating in our module.
2324 ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap);
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002325 ContinuousRangeMap<uint32_t, int, 2>::Builder
2326 IdentifierRemap(F.IdentifierRemap);
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002327 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002328 PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2329 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregora863b4b2011-08-04 16:36:56 +00002330 MacroDefinitionRemap(F.MacroDefinitionRemap);
2331 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002332 SelectorRemap(F.SelectorRemap);
Douglas Gregorf7180622011-08-03 15:48:04 +00002333 ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap);
Douglas Gregor5204bde2011-08-02 16:26:37 +00002334 ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap);
2335
Douglas Gregor925296b2011-07-19 16:10:42 +00002336 while(Data < DataEnd) {
Douglas Gregor925296b2011-07-19 16:10:42 +00002337 uint16_t Len = io::ReadUnalignedLE16(Data);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002338 StringRef Name = StringRef((const char*)Data, Len);
Douglas Gregor00659902011-08-02 10:56:51 +00002339 Data += Len;
Jonathan D. Turnerb2b08232011-07-26 18:21:30 +00002340 Module *OM = ModuleMgr.lookup(Name);
Douglas Gregor925296b2011-07-19 16:10:42 +00002341 if (!OM) {
2342 Error("SourceLocation remap refers to unknown module");
2343 return Failure;
2344 }
Douglas Gregor00659902011-08-02 10:56:51 +00002345
2346 uint32_t SLocOffset = io::ReadUnalignedLE32(Data);
2347 uint32_t IdentifierIDOffset = io::ReadUnalignedLE32(Data);
2348 uint32_t PreprocessedEntityIDOffset = io::ReadUnalignedLE32(Data);
2349 uint32_t MacroDefinitionIDOffset = io::ReadUnalignedLE32(Data);
2350 uint32_t SelectorIDOffset = io::ReadUnalignedLE32(Data);
2351 uint32_t DeclIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregor5204bde2011-08-02 16:26:37 +00002352 uint32_t TypeIndexOffset = io::ReadUnalignedLE32(Data);
Douglas Gregor00659902011-08-02 10:56:51 +00002353
2354 // Source location offset is mapped to OM->SLocEntryBaseOffset.
2355 SLocRemap.insert(std::make_pair(SLocOffset,
2356 static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset)));
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002357 IdentifierRemap.insert(
2358 std::make_pair(IdentifierIDOffset,
2359 OM->BaseIdentifierID - IdentifierIDOffset));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002360 PreprocessedEntityRemap.insert(
2361 std::make_pair(PreprocessedEntityIDOffset,
2362 OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset));
Douglas Gregora863b4b2011-08-04 16:36:56 +00002363 MacroDefinitionRemap.insert(
2364 std::make_pair(MacroDefinitionIDOffset,
2365 OM->BaseMacroDefinitionID - MacroDefinitionIDOffset));
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002366 SelectorRemap.insert(std::make_pair(SelectorIDOffset,
2367 OM->BaseSelectorID - SelectorIDOffset));
Douglas Gregorf7180622011-08-03 15:48:04 +00002368 DeclRemap.insert(std::make_pair(DeclIDOffset,
2369 OM->BaseDeclID - DeclIDOffset));
2370
Douglas Gregor5204bde2011-08-02 16:26:37 +00002371 TypeRemap.insert(std::make_pair(TypeIndexOffset,
Douglas Gregor3b65ed02011-08-02 18:32:54 +00002372 OM->BaseTypeIndex - TypeIndexOffset));
Douglas Gregor925296b2011-07-19 16:10:42 +00002373 }
2374 break;
2375 }
2376
Douglas Gregor925296b2011-07-19 16:10:42 +00002377 case SOURCE_MANAGER_LINE_TABLE:
2378 if (ParseLineTable(F, Record))
2379 return Failure;
Douglas Gregor258ae542009-04-27 06:38:32 +00002380 break;
2381
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00002382 case FILE_SOURCE_LOCATION_OFFSETS:
2383 F.SLocFileOffsets = (const uint32_t *)BlobStart;
2384 F.LocalNumSLocFileEntries = Record[0];
2385 break;
2386
Douglas Gregor925296b2011-07-19 16:10:42 +00002387 case SOURCE_LOCATION_PRELOADS: {
2388 // Need to transform from the local view (1-based IDs) to the global view,
2389 // which is based off F.SLocEntryBaseID.
2390 PreloadSLocEntries.reserve(PreloadSLocEntries.size() + Record.size());
2391 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2392 PreloadSLocEntries.push_back(int(Record[I] - 1) + F.SLocEntryBaseID);
Douglas Gregor258ae542009-04-27 06:38:32 +00002393 break;
Douglas Gregor925296b2011-07-19 16:10:42 +00002394 }
Douglas Gregorc5046832009-04-27 18:38:38 +00002395
Sebastian Redl539c5062010-08-18 23:57:32 +00002396 case STAT_CACHE: {
Douglas Gregor606c4ac2011-02-05 19:42:43 +00002397 if (!DisableStatCache) {
2398 ASTStatCache *MyStatCache =
2399 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
2400 (const unsigned char *)BlobStart,
2401 NumStatHits, NumStatMisses);
2402 FileMgr.addStatCache(MyStatCache);
2403 F.StatCache = MyStatCache;
2404 }
Douglas Gregorc5046832009-04-27 18:38:38 +00002405 break;
Douglas Gregord2eb58a2009-10-16 18:18:30 +00002406 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00002407
Sebastian Redl539c5062010-08-18 23:57:32 +00002408 case EXT_VECTOR_DECLS:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002409 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2410 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002411 break;
2412
Sebastian Redl539c5062010-08-18 23:57:32 +00002413 case VTABLE_USES:
Douglas Gregor4daf6a32011-07-28 19:11:31 +00002414 if (Record.size() % 3 != 0) {
2415 Error("Invalid VTABLE_USES record");
2416 return Failure;
2417 }
2418
Sebastian Redl08aca90252010-08-05 18:21:25 +00002419 // Later tables overwrite earlier ones.
Douglas Gregor4daf6a32011-07-28 19:11:31 +00002420 // FIXME: Modules will have some trouble with this. This is clearly not
2421 // the right way to do this.
Douglas Gregor7fb09192011-07-21 22:35:25 +00002422 VTableUses.clear();
Douglas Gregor4daf6a32011-07-28 19:11:31 +00002423
2424 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
2425 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
2426 VTableUses.push_back(
2427 ReadSourceLocation(F, Record, Idx).getRawEncoding());
2428 VTableUses.push_back(Record[Idx++]);
2429 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002430 break;
2431
Sebastian Redl539c5062010-08-18 23:57:32 +00002432 case DYNAMIC_CLASSES:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002433 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2434 DynamicClasses.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002435 break;
2436
Sebastian Redl539c5062010-08-18 23:57:32 +00002437 case PENDING_IMPLICIT_INSTANTIATIONS:
Douglas Gregorbbbc3672011-07-28 19:26:52 +00002438 if (PendingInstantiations.size() % 2 != 0) {
2439 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
2440 return Failure;
2441 }
2442
2443 // Later lists of pending instantiations overwrite earlier ones.
2444 // FIXME: This is most certainly wrong for modules.
2445 PendingInstantiations.clear();
2446 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2447 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
2448 PendingInstantiations.push_back(
2449 ReadSourceLocation(F, Record, I).getRawEncoding());
2450 }
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002451 break;
2452
Sebastian Redl539c5062010-08-18 23:57:32 +00002453 case SEMA_DECL_REFS:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002454 // Later tables overwrite earlier ones.
Douglas Gregor7fb09192011-07-21 22:35:25 +00002455 // FIXME: Modules will have some trouble with this.
2456 SemaDeclRefs.clear();
2457 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2458 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002459 break;
2460
Sebastian Redl539c5062010-08-18 23:57:32 +00002461 case ORIGINAL_FILE_NAME:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002462 // The primary AST will be the last to get here, so it will be the one
Sebastian Redlb293a452010-07-20 21:20:32 +00002463 // that's used.
Daniel Dunbar000c4ff2009-11-11 05:29:04 +00002464 ActualOriginalFileName.assign(BlobStart, BlobLen);
2465 OriginalFileName = ActualOriginalFileName;
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002466 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregor45fe0362009-05-12 01:31:05 +00002467 break;
Mike Stump11289f42009-09-09 15:08:12 +00002468
Douglas Gregora3b20262011-05-06 21:43:30 +00002469 case ORIGINAL_FILE_ID:
2470 OriginalFileID = FileID::get(Record[0]);
2471 break;
2472
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002473 case ORIGINAL_PCH_DIR:
2474 // The primary AST will be the last to get here, so it will be the one
2475 // that's used.
2476 OriginalDir.assign(BlobStart, BlobLen);
2477 break;
2478
Sebastian Redl539c5062010-08-18 23:57:32 +00002479 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek8bd09292010-02-12 23:31:14 +00002480 const std::string &CurBranch = getClangFullRepositoryVersion();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002481 StringRef ASTBranch(BlobStart, BlobLen);
2482 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002483 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregord54f3a12009-10-05 21:07:28 +00002484 return IgnorePCH;
2485 }
2486 break;
2487 }
Sebastian Redlfa061442010-07-21 20:07:32 +00002488
Douglas Gregor4a9c39a2011-07-21 00:47:40 +00002489 case MACRO_DEFINITION_OFFSETS: {
Sebastian Redlfa061442010-07-21 20:07:32 +00002490 F.MacroDefinitionOffsets = (const uint32_t *)BlobStart;
2491 F.NumPreallocatedPreprocessingEntities = Record[0];
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002492 unsigned LocalBasePreprocessedEntityID = Record[1];
2493 F.LocalNumMacroDefinitions = Record[2];
2494 unsigned LocalBaseMacroID = Record[3];
Douglas Gregora863b4b2011-08-04 16:36:56 +00002495
Douglas Gregor4a9c39a2011-07-21 00:47:40 +00002496 unsigned StartingID;
2497 if (PP) {
2498 if (!PP->getPreprocessingRecord())
2499 PP->createPreprocessingRecord(true);
2500 if (!PP->getPreprocessingRecord()->getExternalSource())
2501 PP->getPreprocessingRecord()->SetExternalSource(*this);
2502 StartingID
2503 = PP->getPreprocessingRecord()
2504 ->allocateLoadedEntities(F.NumPreallocatedPreprocessingEntities);
2505 } else {
2506 // FIXME: We'll eventually want to kill this path, since it assumes
2507 // a particular allocation strategy in the preprocessing record.
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002508 StartingID = getTotalNumPreprocessedEntities()
2509 - F.NumPreallocatedPreprocessingEntities;
Douglas Gregor4a9c39a2011-07-21 00:47:40 +00002510 }
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00002511 F.BaseMacroDefinitionID = getTotalNumMacroDefinitions();
2512 F.BasePreprocessedEntityID = StartingID;
Douglas Gregor4a9c39a2011-07-21 00:47:40 +00002513
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002514 if (F.NumPreallocatedPreprocessingEntities > 0) {
2515 // Introduce the global -> local mapping for preprocessed entities in
2516 // this module.
2517 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
2518
2519 // Introduce the local -> global mapping for preprocessed entities in
2520 // this module.
2521 F.PreprocessedEntityRemap.insert(
2522 std::make_pair(LocalBasePreprocessedEntityID,
2523 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
2524 }
2525
2526
Douglas Gregora863b4b2011-08-04 16:36:56 +00002527 if (F.LocalNumMacroDefinitions > 0) {
2528 // Introduce the global -> local mapping for macro definitions within
2529 // this module.
2530 GlobalMacroDefinitionMap.insert(
2531 std::make_pair(getTotalNumMacroDefinitions() + 1, &F));
2532
2533 // Introduce the local -> global mapping for macro definitions within
2534 // this module.
2535 F.MacroDefinitionRemap.insert(
2536 std::make_pair(LocalBaseMacroID,
2537 F.BaseMacroDefinitionID - LocalBaseMacroID));
2538
2539 MacroDefinitionsLoaded.resize(
Douglas Gregor270e0142011-07-20 01:29:15 +00002540 MacroDefinitionsLoaded.size() + F.LocalNumMacroDefinitions);
Douglas Gregora863b4b2011-08-04 16:36:56 +00002541 }
2542
Douglas Gregoraae92242010-03-19 21:51:54 +00002543 break;
Douglas Gregor4a9c39a2011-07-21 00:47:40 +00002544 }
2545
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002546 case DECL_UPDATE_OFFSETS: {
2547 if (Record.size() % 2 != 0) {
2548 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2549 return Failure;
2550 }
2551 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Douglas Gregorf7180622011-08-03 15:48:04 +00002552 DeclUpdateOffsets[getGlobalDeclID(F, Record[I])]
2553 .push_back(std::make_pair(&F, Record[I+1]));
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002554 break;
2555 }
2556
Sebastian Redl539c5062010-08-18 23:57:32 +00002557 case DECL_REPLACEMENTS: {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002558 if (Record.size() % 2 != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002559 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002560 return Failure;
2561 }
2562 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Douglas Gregorf7180622011-08-03 15:48:04 +00002563 ReplacedDecls[getGlobalDeclID(F, Record[I])]
2564 = std::make_pair(&F, Record[I+1]);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002565 break;
2566 }
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002567
2568 case CXX_BASE_SPECIFIER_OFFSETS: {
2569 if (F.LocalNumCXXBaseSpecifiers != 0) {
2570 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2571 return Failure;
2572 }
2573
2574 F.LocalNumCXXBaseSpecifiers = Record[0];
2575 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
Jonathan D. Turner3766fdb2011-07-21 21:15:19 +00002576 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002577 break;
2578 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002579
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002580 case DIAG_PRAGMA_MAPPINGS:
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002581 if (Record.size() % 2 != 0) {
2582 Error("invalid DIAG_USER_MAPPINGS block in AST file");
2583 return Failure;
2584 }
Douglas Gregor925296b2011-07-19 16:10:42 +00002585
2586 if (F.PragmaDiagMappings.empty())
2587 F.PragmaDiagMappings.swap(Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002588 else
Douglas Gregor925296b2011-07-19 16:10:42 +00002589 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
2590 Record.begin(), Record.end());
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002591 break;
Douglas Gregor09b69892011-02-10 17:09:37 +00002592
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002593 case CUDA_SPECIAL_DECL_REFS:
2594 // Later tables overwrite earlier ones.
Douglas Gregor7fb09192011-07-21 22:35:25 +00002595 // FIXME: Modules will have trouble with this.
2596 CUDASpecialDeclRefs.clear();
2597 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2598 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002599 break;
Douglas Gregor09b69892011-02-10 17:09:37 +00002600
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002601 case HEADER_SEARCH_TABLE: {
Douglas Gregor09b69892011-02-10 17:09:37 +00002602 F.HeaderFileInfoTableData = BlobStart;
2603 F.LocalNumHeaderFileInfos = Record[1];
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002604 F.HeaderFileFrameworkStrings = BlobStart + Record[2];
Douglas Gregor09b69892011-02-10 17:09:37 +00002605 if (Record[0]) {
2606 F.HeaderFileInfoTable
2607 = HeaderFileInfoLookupTable::Create(
2608 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002609 (const unsigned char *)F.HeaderFileInfoTableData,
Douglas Gregora3e41532011-07-28 20:55:49 +00002610 HeaderFileInfoTrait(*this, F,
2611 PP? &PP->getHeaderSearchInfo() : 0,
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002612 BlobStart + Record[2]));
Douglas Gregor09b69892011-02-10 17:09:37 +00002613 if (PP)
2614 PP->getHeaderSearchInfo().SetExternalSource(this);
2615 }
2616 break;
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002617 }
2618
Peter Collingbourne5df20e02011-02-15 19:46:30 +00002619 case FP_PRAGMA_OPTIONS:
2620 // Later tables overwrite earlier ones.
2621 FPPragmaOptions.swap(Record);
2622 break;
2623
2624 case OPENCL_EXTENSIONS:
2625 // Later tables overwrite earlier ones.
2626 OpenCLExtensions.swap(Record);
2627 break;
Alexis Hunt27a761d2011-05-04 23:29:54 +00002628
2629 case TENTATIVE_DEFINITIONS:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002630 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2631 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Alexis Hunt27a761d2011-05-04 23:29:54 +00002632 break;
Douglas Gregorc2fa1692011-06-28 16:20:02 +00002633
2634 case KNOWN_NAMESPACES:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002635 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2636 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregorc2fa1692011-06-28 16:20:02 +00002637 break;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002638 }
Sebastian Redl393f8b72010-07-19 20:52:06 +00002639 First = false;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002640 }
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002641 Error("premature end of bitstream in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00002642 return Failure;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002643}
2644
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00002645ASTReader::ASTReadResult ASTReader::validateFileEntries() {
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00002646 for (ModuleIterator I = ModuleMgr.begin(),
2647 E = ModuleMgr.end(); I != E; ++I) {
2648 Module *F = *I;
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00002649 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
2650
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00002651 for (unsigned i = 0, e = F->LocalNumSLocFileEntries; i != e; ++i) {
2652 SLocEntryCursor.JumpToBit(F->SLocFileOffsets[i]);
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00002653 unsigned Code = SLocEntryCursor.ReadCode();
2654 if (Code == llvm::bitc::END_BLOCK ||
2655 Code == llvm::bitc::ENTER_SUBBLOCK ||
2656 Code == llvm::bitc::DEFINE_ABBREV) {
2657 Error("incorrectly-formatted source location entry in AST file");
2658 return Failure;
2659 }
2660
2661 RecordData Record;
2662 const char *BlobStart;
2663 unsigned BlobLen;
2664 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
2665 default:
2666 Error("incorrectly-formatted source location entry in AST file");
2667 return Failure;
2668
2669 case SM_SLOC_FILE_ENTRY: {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002670 StringRef Filename(BlobStart, BlobLen);
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00002671 const FileEntry *File = getFileEntry(Filename);
2672
2673 if (File == 0) {
2674 std::string ErrorStr = "could not find file '";
2675 ErrorStr += Filename;
2676 ErrorStr += "' referenced by AST file";
2677 Error(ErrorStr.c_str());
2678 return IgnorePCH;
2679 }
2680
2681 if (Record.size() < 6) {
2682 Error("source location entry is incorrect");
2683 return Failure;
2684 }
2685
2686 // The stat info from the FileEntry came from the cached stat
2687 // info of the PCH, so we cannot trust it.
2688 struct stat StatBuf;
2689 if (::stat(File->getName(), &StatBuf) != 0) {
2690 StatBuf.st_size = File->getSize();
2691 StatBuf.st_mtime = File->getModificationTime();
2692 }
2693
2694 if (((off_t)Record[4] != StatBuf.st_size
2695#if !defined(LLVM_ON_WIN32)
2696 // In our regression testing, the Windows file system seems to
2697 // have inconsistent modification times that sometimes
2698 // erroneously trigger this error-handling path.
2699 || (time_t)Record[5] != StatBuf.st_mtime
2700#endif
2701 )) {
2702 Error(diag::err_fe_pch_file_modified, Filename);
2703 return IgnorePCH;
2704 }
2705
2706 break;
2707 }
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00002708 }
2709 }
2710 }
2711
2712 return Success;
2713}
2714
Sebastian Redl009e7f22010-10-05 16:15:19 +00002715ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
Douglas Gregora6895d82011-07-22 16:00:58 +00002716 ModuleKind Type) {
Douglas Gregordf0c1512011-08-18 04:12:04 +00002717 switch(ReadASTCore(FileName, Type, /*ImportedBy=*/0)) {
Sebastian Redl2abc0382010-07-16 20:41:52 +00002718 case Failure: return Failure;
2719 case IgnorePCH: return IgnorePCH;
2720 case Success: break;
2721 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002722
2723 // Here comes stuff that we only do once the entire chain is loaded.
2724
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00002725 if (!DisableValidation) {
2726 switch(validateFileEntries()) {
2727 case Failure: return Failure;
2728 case IgnorePCH: return IgnorePCH;
2729 case Success: break;
2730 }
2731 }
2732
Sebastian Redl96371b42010-09-22 00:42:30 +00002733 // Preload SLocEntries.
2734 for (unsigned I = 0, N = PreloadSLocEntries.size(); I != N; ++I) {
2735 ASTReadResult Result = ReadSLocEntryRecord(PreloadSLocEntries[I]);
2736 if (Result != Success)
Douglas Gregor925296b2011-07-19 16:10:42 +00002737 return Failure;
Sebastian Redl96371b42010-09-22 00:42:30 +00002738 }
Douglas Gregor49bf76b2011-07-21 18:46:38 +00002739 PreloadSLocEntries.clear();
2740
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002741 // Check the predefines buffers.
Douglas Gregor9125bd62011-07-27 16:30:06 +00002742 if (!DisableValidation && Type != MK_Module && CheckPredefinesBuffers())
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002743 return IgnorePCH;
2744
2745 if (PP) {
2746 // Initialization of keywords and pragmas occurs before the
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002747 // AST file is read, so there may be some identifiers that were
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002748 // loaded into the IdentifierTable before we intercepted the
2749 // creation of identifiers. Iterate through the list of known
2750 // identifiers and determine whether we have to establish
2751 // preprocessor definitions or top-level identifier declaration
2752 // chains for those identifiers.
2753 //
2754 // We copy the IdentifierInfo pointers to a small vector first,
2755 // since de-serializing declarations or macro definitions can add
2756 // new entries into the identifier table, invalidating the
2757 // iterators.
Douglas Gregor9125bd62011-07-27 16:30:06 +00002758 //
2759 // FIXME: We need a lazier way to load this information, e.g., by marking
2760 // the identifier data as 'dirty', so that it will be looked up in the
2761 // AST file(s) if it is uttered in the source. This could save us some
2762 // module load time.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002763 SmallVector<IdentifierInfo *, 128> Identifiers;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002764 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
2765 IdEnd = PP->getIdentifierTable().end();
2766 Id != IdEnd; ++Id)
2767 Identifiers.push_back(Id->second);
Sebastian Redlfa061442010-07-21 20:07:32 +00002768 // We need to search the tables in all files.
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00002769 for (ModuleIterator J = ModuleMgr.begin(),
2770 M = ModuleMgr.end(); J != M; ++J) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002771 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00002772 = (ASTIdentifierLookupTable *)(*J)->IdentifierLookupTable;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002773 // Not all AST files necessarily have identifier tables, only the useful
Sebastian Redl5c415f32010-07-22 17:01:13 +00002774 // ones.
2775 if (!IdTable)
2776 continue;
Sebastian Redlfa061442010-07-21 20:07:32 +00002777 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2778 IdentifierInfo *II = Identifiers[I];
2779 // Look in the on-disk hash tables for an entry for this identifier
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00002780 ASTIdentifierLookupTrait Info(*this, *(*J), II);
Sebastian Redlfa061442010-07-21 20:07:32 +00002781 std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength());
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002782 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
Sebastian Redlb293a452010-07-20 21:20:32 +00002783 if (Pos == IdTable->end())
2784 continue;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002785
Sebastian Redlb293a452010-07-20 21:20:32 +00002786 // Dereferencing the iterator has the effect of populating the
2787 // IdentifierInfo node with the various declarations it needs.
2788 (void)*Pos;
2789 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002790 }
2791 }
2792
2793 if (Context)
2794 InitializeContext(*Context);
2795
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002796 if (DeserializationListener)
2797 DeserializationListener->ReaderInitialized(this);
2798
Douglas Gregor936a5b42010-11-30 05:23:00 +00002799 // If this AST file is a precompiled preamble, then set the main file ID of
2800 // the source manager to the file source file from which the preamble was
2801 // built. This is the only valid way to use a precompiled preamble.
Douglas Gregora6895d82011-07-22 16:00:58 +00002802 if (Type == MK_Preamble) {
Douglas Gregora3b20262011-05-06 21:43:30 +00002803 if (OriginalFileID.isInvalid()) {
2804 SourceLocation Loc
2805 = SourceMgr.getLocation(FileMgr.getFile(getOriginalSourceFile()), 1, 1);
2806 if (Loc.isValid())
2807 OriginalFileID = SourceMgr.getDecomposedLoc(Loc).first;
Douglas Gregor936a5b42010-11-30 05:23:00 +00002808 }
Douglas Gregor925296b2011-07-19 16:10:42 +00002809 else {
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00002810 OriginalFileID = FileID::get(ModuleMgr.getPrimaryModule().SLocEntryBaseID
Douglas Gregor925296b2011-07-19 16:10:42 +00002811 + OriginalFileID.getOpaqueValue() - 1);
2812 }
2813
Douglas Gregora3b20262011-05-06 21:43:30 +00002814 if (!OriginalFileID.isInvalid())
2815 SourceMgr.SetPreambleFileID(OriginalFileID);
Douglas Gregor936a5b42010-11-30 05:23:00 +00002816 }
2817
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002818 return Success;
2819}
2820
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002821ASTReader::ASTReadResult ASTReader::ReadASTCore(StringRef FileName,
Douglas Gregordf0c1512011-08-18 04:12:04 +00002822 ModuleKind Type,
2823 Module *ImportedBy) {
Douglas Gregor4dd3e942011-08-19 02:29:29 +00002824 Module *M;
2825 bool NewModule;
2826 std::string ErrorStr;
2827 llvm::tie(M, NewModule) = ModuleMgr.addModule(FileName, Type, ImportedBy,
2828 ErrorStr);
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002829
Douglas Gregor4dd3e942011-08-19 02:29:29 +00002830 if (!M) {
2831 // We couldn't load the module.
2832 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
2833 + ErrorStr;
2834 Error(Msg);
2835 return Failure;
2836 }
2837
2838 if (!NewModule) {
2839 // We've already loaded this module.
2840 return Success;
2841 }
2842
2843 // FIXME: This seems rather a hack. Should CurrentDir be part of the
2844 // module?
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002845 if (FileName != "-") {
2846 CurrentDir = llvm::sys::path::parent_path(FileName);
2847 if (CurrentDir.empty()) CurrentDir = ".";
2848 }
2849
Douglas Gregor4dd3e942011-08-19 02:29:29 +00002850 Module &F = *M;
Sebastian Redl34522812010-07-16 17:50:48 +00002851 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002852 Stream.init(F.StreamFile);
Sebastian Redlfa061442010-07-21 20:07:32 +00002853 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Douglas Gregord32f0352011-07-22 06:10:01 +00002854
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002855 // Sniff for the signature.
2856 if (Stream.Read(8) != 'C' ||
2857 Stream.Read(8) != 'P' ||
2858 Stream.Read(8) != 'C' ||
2859 Stream.Read(8) != 'H') {
2860 Diag(diag::err_not_a_pch_file) << FileName;
2861 return Failure;
2862 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002863
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002864 while (!Stream.AtEndOfStream()) {
2865 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00002866
Douglas Gregor92863e42009-04-10 23:10:45 +00002867 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002868 Error("invalid record at top-level of AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002869 return Failure;
2870 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002871
2872 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregora868bbd2009-04-21 22:25:48 +00002873
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002874 // We only know the AST subblock ID.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002875 switch (BlockID) {
2876 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregor92863e42009-04-10 23:10:45 +00002877 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002878 Error("malformed BlockInfoBlock in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002879 return Failure;
2880 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002881 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00002882 case AST_BLOCK_ID:
Sebastian Redl3e31c722010-08-18 23:56:56 +00002883 switch (ReadASTBlock(F)) {
Douglas Gregor55abb232009-04-10 20:39:37 +00002884 case Success:
2885 break;
2886
2887 case Failure:
Douglas Gregor92863e42009-04-10 23:10:45 +00002888 return Failure;
Douglas Gregor55abb232009-04-10 20:39:37 +00002889
2890 case IgnorePCH:
Douglas Gregorbfbde532009-04-10 21:16:55 +00002891 // FIXME: We could consider reading through to the end of this
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002892 // AST block, skipping subblocks, to see if there are other
2893 // AST blocks elsewhere.
Douglas Gregor0bc12932009-04-27 21:28:04 +00002894
Douglas Gregor925296b2011-07-19 16:10:42 +00002895 // FIXME: We can't clear loaded slocentries anymore.
2896 //SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor0bc12932009-04-27 21:28:04 +00002897
2898 // Remove the stat cache.
Sebastian Redl34522812010-07-16 17:50:48 +00002899 if (F.StatCache)
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002900 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor0bc12932009-04-27 21:28:04 +00002901
Douglas Gregor92863e42009-04-10 23:10:45 +00002902 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00002903 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002904 break;
2905 default:
Douglas Gregor92863e42009-04-10 23:10:45 +00002906 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002907 Error("malformed block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002908 return Failure;
2909 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002910 break;
2911 }
Mike Stump11289f42009-09-09 15:08:12 +00002912 }
Douglas Gregord32f0352011-07-22 06:10:01 +00002913
Douglas Gregora6895d82011-07-22 16:00:58 +00002914 // Once read, set the Module bit base offset and update the size in
Douglas Gregord32f0352011-07-22 06:10:01 +00002915 // bits of all files we've seen.
2916 F.GlobalBitOffset = TotalModulesSizeInBits;
2917 TotalModulesSizeInBits += F.SizeInBits;
2918 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
Sebastian Redl2abc0382010-07-16 20:41:52 +00002919 return Success;
2920}
2921
Sebastian Redl2c499f62010-08-18 23:56:43 +00002922void ASTReader::setPreprocessor(Preprocessor &pp) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002923 PP = &pp;
Douglas Gregor4a9c39a2011-07-21 00:47:40 +00002924
2925 if (unsigned N = getTotalNumPreprocessedEntities()) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002926 if (!PP->getPreprocessingRecord())
Douglas Gregor998caea2011-05-06 16:33:08 +00002927 PP->createPreprocessingRecord(true);
Douglas Gregor4a9c39a2011-07-21 00:47:40 +00002928 PP->getPreprocessingRecord()->SetExternalSource(*this);
2929 PP->getPreprocessingRecord()->allocateLoadedEntities(N);
Douglas Gregoraae92242010-03-19 21:51:54 +00002930 }
Douglas Gregor4a9c39a2011-07-21 00:47:40 +00002931
2932 PP->getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor49bf76b2011-07-21 18:46:38 +00002933 PP->getHeaderSearchInfo().SetExternalSource(this);
Douglas Gregoraae92242010-03-19 21:51:54 +00002934}
2935
Sebastian Redl2c499f62010-08-18 23:56:43 +00002936void ASTReader::InitializeContext(ASTContext &Ctx) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002937 Context = &Ctx;
2938 assert(Context && "Passed null context!");
2939
2940 assert(PP && "Forgot to set Preprocessor ?");
2941 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor9882a5a2010-01-04 19:18:44 +00002942 PP->setExternalSource(this);
Douglas Gregor09b69892011-02-10 17:09:37 +00002943
Douglas Gregor94619c82011-08-24 19:03:07 +00002944 // If we have any update blocks for the TU waiting, we have to add
2945 // them before we deserialize anything.
Douglas Gregordab42432011-08-12 00:15:20 +00002946 TranslationUnitDecl *TU = Ctx.getTranslationUnitDecl();
Douglas Gregor94619c82011-08-24 19:03:07 +00002947 for (ModuleIterator M = ModuleMgr.begin(), MEnd = ModuleMgr.end();
2948 M != MEnd; ++M) {
2949 Module::DeclContextInfosMap::iterator DCU
2950 = (*M)->DeclContextInfos.find(0);
2951 if (DCU != (*M)->DeclContextInfos.end()) {
2952 // Insertion could invalidate map, so grab value first.
2953 DeclContextInfo Info = DCU->second;
2954 (*M)->DeclContextInfos.erase(DCU);
2955 (*M)->DeclContextInfos[TU] = Info;
2956 }
Douglas Gregoraa433012010-10-01 01:18:02 +00002957 }
Douglas Gregordab42432011-08-12 00:15:20 +00002958
2959 // If there's a listener, notify them that we "read" the translation unit.
2960 if (DeserializationListener)
2961 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID, TU);
Douglas Gregoraa433012010-10-01 01:18:02 +00002962
Douglas Gregordab42432011-08-12 00:15:20 +00002963 // Make sure we load the declaration update records for the translation unit,
2964 // if there are any.
2965 loadDeclUpdateRecords(PREDEF_DECL_TRANSLATION_UNIT_ID, TU);
2966
2967 // Note that the translation unit has external lexical and visible storage.
2968 TU->setHasExternalLexicalStorage(true);
2969 TU->setHasExternalVisibleStorage(true);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002970
Douglas Gregoraa8a8272011-08-11 22:18:49 +00002971 // FIXME: Find a better way to deal with collisions between these
2972 // built-in types. Right now, we just ignore the problem.
2973
2974 // Load the special types.
Jonathan D. Turnerf07f1312011-08-05 23:07:10 +00002975 if (Context->getBuiltinVaListType().isNull()) {
Jonathan D. Turnerf07f1312011-08-05 23:07:10 +00002976 Context->setBuiltinVaListType(
2977 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
Douglas Gregoraa8a8272011-08-11 22:18:49 +00002978 }
2979
Douglas Gregoraa8a8272011-08-11 22:18:49 +00002980 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL]) {
2981 if (Context->ObjCProtoType.isNull())
Douglas Gregor09c4aa82011-08-11 22:04:35 +00002982 Context->ObjCProtoType = GetType(Proto);
Douglas Gregoraa8a8272011-08-11 22:18:49 +00002983 }
2984
Douglas Gregoraa8a8272011-08-11 22:18:49 +00002985 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
2986 if (!Context->CFConstantStringTypeDecl)
Jonathan D. Turnerf07f1312011-08-05 23:07:10 +00002987 Context->setCFConstantStringType(GetType(String));
Douglas Gregoraa8a8272011-08-11 22:18:49 +00002988 }
2989
2990 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
2991 QualType FileType = GetType(File);
2992 if (FileType.isNull()) {
2993 Error("FILE type is NULL");
2994 return;
2995 }
2996
2997 if (!Context->FILEDecl) {
Jonathan D. Turnerf07f1312011-08-05 23:07:10 +00002998 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
2999 Context->setFILEDecl(Typedef->getDecl());
3000 else {
3001 const TagType *Tag = FileType->getAs<TagType>();
3002 if (!Tag) {
3003 Error("Invalid FILE type in AST file");
3004 return;
3005 }
3006 Context->setFILEDecl(Tag->getDecl());
3007 }
Douglas Gregor27821ce2009-07-07 16:35:42 +00003008 }
Douglas Gregoraa8a8272011-08-11 22:18:49 +00003009 }
3010
3011 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
3012 QualType Jmp_bufType = GetType(Jmp_buf);
3013 if (Jmp_bufType.isNull()) {
3014 Error("jmp_buf type is NULL");
3015 return;
3016 }
3017
3018 if (!Context->jmp_bufDecl) {
Jonathan D. Turnerf07f1312011-08-05 23:07:10 +00003019 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
3020 Context->setjmp_bufDecl(Typedef->getDecl());
3021 else {
3022 const TagType *Tag = Jmp_bufType->getAs<TagType>();
3023 if (!Tag) {
3024 Error("Invalid jmp_buf type in AST file");
3025 return;
3026 }
3027 Context->setjmp_bufDecl(Tag->getDecl());
3028 }
Mike Stumpa4de80b2009-07-28 02:25:19 +00003029 }
Douglas Gregoraa8a8272011-08-11 22:18:49 +00003030 }
3031
3032 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
3033 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
3034 if (Sigjmp_bufType.isNull()) {
3035 Error("sigjmp_buf type is NULL");
3036 return;
3037 }
3038
3039 if (!Context->sigjmp_bufDecl) {
Jonathan D. Turnerf07f1312011-08-05 23:07:10 +00003040 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
3041 Context->setsigjmp_bufDecl(Typedef->getDecl());
3042 else {
3043 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
3044 assert(Tag && "Invalid sigjmp_buf type in AST file");
3045 Context->setsigjmp_bufDecl(Tag->getDecl());
3046 }
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003047 }
Jonathan D. Turnerf07f1312011-08-05 23:07:10 +00003048 }
Richard Smith02e85f32011-04-14 22:09:26 +00003049
Douglas Gregoraa8a8272011-08-11 22:18:49 +00003050 if (unsigned ObjCIdRedef
3051 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
3052 if (Context->ObjCIdRedefinitionType.isNull())
3053 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
3054 }
3055
3056 if (unsigned ObjCClassRedef
3057 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
3058 if (Context->ObjCClassRedefinitionType.isNull())
3059 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
3060 }
3061
3062 if (unsigned ObjCSelRedef
3063 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
3064 if (Context->ObjCSelRedefinitionType.isNull())
3065 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
3066 }
3067
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00003068 ReadPragmaDiagnosticMappings(Context->getDiagnostics());
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00003069
3070 // If there were any CUDA special declarations, deserialize them.
3071 if (!CUDASpecialDeclRefs.empty()) {
3072 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
3073 Context->setcudaConfigureCallDecl(
3074 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
3075 }
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003076}
3077
Douglas Gregor45fe0362009-05-12 01:31:05 +00003078/// \brief Retrieve the name of the original source file name
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003079/// directly from the AST file, without actually loading the AST
Douglas Gregor45fe0362009-05-12 01:31:05 +00003080/// file.
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003081std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +00003082 FileManager &FileMgr,
Daniel Dunbar3b951482009-12-03 09:13:06 +00003083 Diagnostic &Diags) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003084 // Open the AST file.
Douglas Gregor45fe0362009-05-12 01:31:05 +00003085 std::string ErrStr;
3086 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner5159f612010-11-23 08:35:12 +00003087 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregor45fe0362009-05-12 01:31:05 +00003088 if (!Buffer) {
Daniel Dunbar3b951482009-12-03 09:13:06 +00003089 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregor45fe0362009-05-12 01:31:05 +00003090 return std::string();
3091 }
3092
3093 // Initialize the stream
3094 llvm::BitstreamReader StreamFile;
3095 llvm::BitstreamCursor Stream;
Mike Stump11289f42009-09-09 15:08:12 +00003096 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregor45fe0362009-05-12 01:31:05 +00003097 (const unsigned char *)Buffer->getBufferEnd());
3098 Stream.init(StreamFile);
3099
3100 // Sniff for the signature.
3101 if (Stream.Read(8) != 'C' ||
3102 Stream.Read(8) != 'P' ||
3103 Stream.Read(8) != 'C' ||
3104 Stream.Read(8) != 'H') {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003105 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00003106 return std::string();
3107 }
3108
3109 RecordData Record;
3110 while (!Stream.AtEndOfStream()) {
3111 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00003112
Douglas Gregor45fe0362009-05-12 01:31:05 +00003113 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3114 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump11289f42009-09-09 15:08:12 +00003115
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003116 // We only know the AST subblock ID.
Douglas Gregor45fe0362009-05-12 01:31:05 +00003117 switch (BlockID) {
Sebastian Redl539c5062010-08-18 23:57:32 +00003118 case AST_BLOCK_ID:
3119 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003120 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00003121 return std::string();
3122 }
3123 break;
Mike Stump11289f42009-09-09 15:08:12 +00003124
Douglas Gregor45fe0362009-05-12 01:31:05 +00003125 default:
3126 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003127 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00003128 return std::string();
3129 }
3130 break;
3131 }
3132 continue;
3133 }
3134
3135 if (Code == llvm::bitc::END_BLOCK) {
3136 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003137 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00003138 return std::string();
3139 }
3140 continue;
3141 }
3142
3143 if (Code == llvm::bitc::DEFINE_ABBREV) {
3144 Stream.ReadAbbrevRecord();
3145 continue;
3146 }
3147
3148 Record.clear();
3149 const char *BlobStart = 0;
3150 unsigned BlobLen = 0;
Mike Stump11289f42009-09-09 15:08:12 +00003151 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl539c5062010-08-18 23:57:32 +00003152 == ORIGINAL_FILE_NAME)
Douglas Gregor45fe0362009-05-12 01:31:05 +00003153 return std::string(BlobStart, BlobLen);
Mike Stump11289f42009-09-09 15:08:12 +00003154 }
Douglas Gregor45fe0362009-05-12 01:31:05 +00003155
3156 return std::string();
3157}
3158
Douglas Gregor55abb232009-04-10 20:39:37 +00003159/// \brief Parse the record that corresponds to a LangOptions data
3160/// structure.
3161///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003162/// This routine parses the language options from the AST file and then gives
3163/// them to the AST listener if one is set.
Douglas Gregor55abb232009-04-10 20:39:37 +00003164///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003165/// \returns true if the listener deems the file unacceptable, false otherwise.
Sebastian Redl2c499f62010-08-18 23:56:43 +00003166bool ASTReader::ParseLanguageOptions(
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003167 const SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003168 if (Listener) {
3169 LangOptions LangOpts;
Mike Stump11289f42009-09-09 15:08:12 +00003170
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003171 #define PARSE_LANGOPT(Option) \
3172 LangOpts.Option = Record[Idx]; \
3173 ++Idx
Mike Stump11289f42009-09-09 15:08:12 +00003174
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003175 unsigned Idx = 0;
3176 PARSE_LANGOPT(Trigraphs);
3177 PARSE_LANGOPT(BCPLComment);
3178 PARSE_LANGOPT(DollarIdents);
3179 PARSE_LANGOPT(AsmPreprocessor);
3180 PARSE_LANGOPT(GNUMode);
Chandler Carruthe03aa552010-04-17 20:17:31 +00003181 PARSE_LANGOPT(GNUKeywords);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003182 PARSE_LANGOPT(ImplicitInt);
3183 PARSE_LANGOPT(Digraphs);
3184 PARSE_LANGOPT(HexFloats);
3185 PARSE_LANGOPT(C99);
Peter Collingbournea686b5f2011-04-15 00:35:23 +00003186 PARSE_LANGOPT(C1X);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003187 PARSE_LANGOPT(Microsoft);
3188 PARSE_LANGOPT(CPlusPlus);
3189 PARSE_LANGOPT(CPlusPlus0x);
3190 PARSE_LANGOPT(CXXOperatorNames);
3191 PARSE_LANGOPT(ObjC1);
3192 PARSE_LANGOPT(ObjC2);
3193 PARSE_LANGOPT(ObjCNonFragileABI);
Fariborz Jahanian45878032010-02-09 19:31:38 +00003194 PARSE_LANGOPT(ObjCNonFragileABI2);
Fariborz Jahanian13f3b2f2011-01-07 18:59:25 +00003195 PARSE_LANGOPT(AppleKext);
Ted Kremenek1d56c9e2010-12-23 21:35:43 +00003196 PARSE_LANGOPT(ObjCDefaultSynthProperties);
Douglas Gregora860e6a2011-06-14 23:20:43 +00003197 PARSE_LANGOPT(ObjCInferRelatedResultType);
Fariborz Jahanian62c56022010-04-22 21:01:59 +00003198 PARSE_LANGOPT(NoConstantCFStrings);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003199 PARSE_LANGOPT(PascalStrings);
3200 PARSE_LANGOPT(WritableStrings);
3201 PARSE_LANGOPT(LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +00003202 PARSE_LANGOPT(AltiVec);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003203 PARSE_LANGOPT(Exceptions);
Anders Carlssonce8dd3a2011-02-19 23:53:54 +00003204 PARSE_LANGOPT(ObjCExceptions);
Anders Carlsson6bbd2682011-02-23 03:04:54 +00003205 PARSE_LANGOPT(CXXExceptions);
3206 PARSE_LANGOPT(SjLjExceptions);
Douglas Gregordbe39272011-02-01 15:15:22 +00003207 PARSE_LANGOPT(MSBitfields);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003208 PARSE_LANGOPT(NeXTRuntime);
3209 PARSE_LANGOPT(Freestanding);
3210 PARSE_LANGOPT(NoBuiltin);
3211 PARSE_LANGOPT(ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +00003212 PARSE_LANGOPT(POSIXThreads);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003213 PARSE_LANGOPT(Blocks);
3214 PARSE_LANGOPT(EmitAllDecls);
3215 PARSE_LANGOPT(MathErrno);
Chris Lattner51924e512010-06-26 21:25:03 +00003216 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
3217 Record[Idx++]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003218 PARSE_LANGOPT(HeinousExtensions);
3219 PARSE_LANGOPT(Optimize);
3220 PARSE_LANGOPT(OptimizeSize);
3221 PARSE_LANGOPT(Static);
3222 PARSE_LANGOPT(PICLevel);
3223 PARSE_LANGOPT(GNUInline);
3224 PARSE_LANGOPT(NoInline);
Chandler Carruth7ffce732011-04-23 20:05:38 +00003225 PARSE_LANGOPT(Deprecated);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003226 PARSE_LANGOPT(AccessControl);
3227 PARSE_LANGOPT(CharIsSigned);
John Thompsoned4e2952009-11-05 20:14:16 +00003228 PARSE_LANGOPT(ShortWChar);
Argyrios Kyrtzidisa88942a2011-01-15 02:56:16 +00003229 PARSE_LANGOPT(ShortEnums);
Chris Lattner51924e512010-06-26 21:25:03 +00003230 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
John McCall457a04e2010-10-22 21:05:15 +00003231 LangOpts.setVisibilityMode((Visibility)Record[Idx++]);
Daniel Dunbar143021e2009-09-21 04:16:19 +00003232 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
Chris Lattner51924e512010-06-26 21:25:03 +00003233 Record[Idx++]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003234 PARSE_LANGOPT(InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +00003235 PARSE_LANGOPT(OpenCL);
Peter Collingbourne546d0792010-12-01 19:14:57 +00003236 PARSE_LANGOPT(CUDA);
Mike Stumpd9546382009-12-12 01:27:46 +00003237 PARSE_LANGOPT(CatchUndefined);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003238 PARSE_LANGOPT(DefaultFPContract);
Roman Divackydc1f68d2011-03-01 17:36:40 +00003239 PARSE_LANGOPT(ElideConstructors);
3240 PARSE_LANGOPT(SpellChecking);
Roman Divacky65b88cd2011-03-01 17:40:53 +00003241 PARSE_LANGOPT(MRTD);
John McCall31168b02011-06-15 23:02:42 +00003242 PARSE_LANGOPT(ObjCAutoRefCount);
Douglas Gregor49b236a2011-08-04 15:46:00 +00003243 PARSE_LANGOPT(ObjCInferRelatedReturnType);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003244 #undef PARSE_LANGOPT
Douglas Gregor55abb232009-04-10 20:39:37 +00003245
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003246 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor55abb232009-04-10 20:39:37 +00003247 }
Douglas Gregor55abb232009-04-10 20:39:37 +00003248
3249 return false;
3250}
3251
Sebastian Redl2c499f62010-08-18 23:56:43 +00003252void ASTReader::ReadPreprocessedEntities() {
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00003253 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
3254 Module &F = *(*I);
Douglas Gregor92a96f52011-02-08 21:58:10 +00003255 if (!F.PreprocessorDetailCursor.getBitStreamReader())
3256 continue;
3257
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00003258 SavedStreamPosition SavedPosition(F.PreprocessorDetailCursor);
Douglas Gregor92a96f52011-02-08 21:58:10 +00003259 F.PreprocessorDetailCursor.JumpToBit(F.PreprocessorDetailStartOffset);
3260 while (LoadPreprocessedEntity(F)) { }
3261 }
Douglas Gregoraae92242010-03-19 21:51:54 +00003262}
3263
Douglas Gregor46c50012011-02-11 19:46:30 +00003264PreprocessedEntity *ASTReader::ReadPreprocessedEntityAtOffset(uint64_t Offset) {
Douglas Gregord32f0352011-07-22 06:10:01 +00003265 RecordLocation Loc = getLocalBitOffset(Offset);
Douglas Gregorf88e35b2010-11-30 06:16:57 +00003266
Douglas Gregor92a96f52011-02-08 21:58:10 +00003267 // Keep track of where we are in the stream, then jump back there
3268 // after reading this entity.
Douglas Gregord32f0352011-07-22 06:10:01 +00003269 SavedStreamPosition SavedPosition(Loc.F->PreprocessorDetailCursor);
3270 Loc.F->PreprocessorDetailCursor.JumpToBit(Loc.Offset);
3271 return LoadPreprocessedEntity(*Loc.F);
Douglas Gregorf88e35b2010-11-30 06:16:57 +00003272}
3273
Douglas Gregor09b69892011-02-10 17:09:37 +00003274HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00003275 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
3276 Module &F = *(*I);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00003277
Douglas Gregora3e41532011-07-28 20:55:49 +00003278 HeaderFileInfoTrait Trait(*this, F, &PP->getHeaderSearchInfo(),
Douglas Gregor4b123cb2011-07-28 04:50:02 +00003279 F.HeaderFileFrameworkStrings,
3280 FE->getName());
3281
Douglas Gregor09b69892011-02-10 17:09:37 +00003282 HeaderFileInfoLookupTable *Table
3283 = static_cast<HeaderFileInfoLookupTable *>(F.HeaderFileInfoTable);
3284 if (!Table)
3285 continue;
3286
3287 // Look in the on-disk hash table for an entry for this file name.
3288 HeaderFileInfoLookupTable::iterator Pos = Table->find(FE->getName(),
3289 &Trait);
3290 if (Pos == Table->end())
3291 continue;
3292
3293 HeaderFileInfo HFI = *Pos;
3294 if (Listener)
3295 Listener->ReadHeaderFileInfo(HFI, FE->getUID());
3296
3297 return HFI;
3298 }
3299
3300 return HeaderFileInfo();
3301}
3302
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00003303void ASTReader::ReadPragmaDiagnosticMappings(Diagnostic &Diag) {
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00003304 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
3305 Module &F = *(*I);
Douglas Gregor925296b2011-07-19 16:10:42 +00003306 unsigned Idx = 0;
3307 while (Idx < F.PragmaDiagMappings.size()) {
3308 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
3309 while (1) {
3310 assert(Idx < F.PragmaDiagMappings.size() &&
3311 "Invalid data, didn't find '-1' marking end of diag/map pairs");
3312 if (Idx >= F.PragmaDiagMappings.size()) {
3313 break; // Something is messed up but at least avoid infinite loop in
3314 // release build.
3315 }
3316 unsigned DiagID = F.PragmaDiagMappings[Idx++];
3317 if (DiagID == (unsigned)-1) {
3318 break; // no more diag/map pairs for this location.
3319 }
3320 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++];
3321 Diag.setDiagnosticMapping(DiagID, Map, Loc);
3322 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00003323 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00003324 }
3325}
3326
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003327/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redl2c499f62010-08-18 23:56:43 +00003328ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Douglas Gregor5204bde2011-08-02 16:26:37 +00003329 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
Jonathan D. Turner35005682011-07-20 21:31:32 +00003330 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
Douglas Gregor8ab4ea82011-07-29 00:21:44 +00003331 Module *M = I->second;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003332 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003333}
3334
3335/// \brief Read and return the type with the given index..
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003336///
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003337/// The index is the type ID, shifted and minus the number of predefs. This
3338/// routine actually reads the record corresponding to the type at the given
3339/// location. It is a helper routine for GetType, which deals with reading type
3340/// IDs.
Douglas Gregor903b7e92011-07-22 00:38:23 +00003341QualType ASTReader::readTypeRecord(unsigned Index) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003342 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003343 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00003344
Douglas Gregorfeb84b02009-04-14 21:18:50 +00003345 // Keep track of where we are in the stream, then jump back there
3346 // after reading this type.
Douglas Gregor12bfa382009-10-17 00:13:19 +00003347 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00003348
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003349 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redleaa4ade2010-08-11 18:52:41 +00003350
Douglas Gregor1342e842009-07-06 18:54:52 +00003351 // Note that we are loading a type record.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00003352 Deserializing AType(this);
Mike Stump11289f42009-09-09 15:08:12 +00003353
Douglas Gregor903b7e92011-07-22 00:38:23 +00003354 unsigned Idx = 0;
Sebastian Redl2c373b92010-10-05 15:59:54 +00003355 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003356 RecordData Record;
Douglas Gregor12bfa382009-10-17 00:13:19 +00003357 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl539c5062010-08-18 23:57:32 +00003358 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
3359 case TYPE_EXT_QUAL: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003360 if (Record.size() != 2) {
3361 Error("Incorrect encoding of extended qualifier type");
3362 return QualType();
3363 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003364 QualType Base = readType(*Loc.F, Record, Idx);
3365 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
John McCall8ccfcb52009-09-24 19:53:00 +00003366 return Context->getQualifiedType(Base, Quals);
Douglas Gregor455b8f42009-04-15 22:00:08 +00003367 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003368
Sebastian Redl539c5062010-08-18 23:57:32 +00003369 case TYPE_COMPLEX: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003370 if (Record.size() != 1) {
3371 Error("Incorrect encoding of complex type");
3372 return QualType();
3373 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003374 QualType ElemType = readType(*Loc.F, Record, Idx);
Chris Lattner8575daa2009-04-27 21:45:14 +00003375 return Context->getComplexType(ElemType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003376 }
3377
Sebastian Redl539c5062010-08-18 23:57:32 +00003378 case TYPE_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003379 if (Record.size() != 1) {
3380 Error("Incorrect encoding of pointer type");
3381 return QualType();
3382 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003383 QualType PointeeType = readType(*Loc.F, Record, Idx);
Chris Lattner8575daa2009-04-27 21:45:14 +00003384 return Context->getPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003385 }
3386
Sebastian Redl539c5062010-08-18 23:57:32 +00003387 case TYPE_BLOCK_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003388 if (Record.size() != 1) {
3389 Error("Incorrect encoding of block pointer type");
3390 return QualType();
3391 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003392 QualType PointeeType = readType(*Loc.F, Record, Idx);
Chris Lattner8575daa2009-04-27 21:45:14 +00003393 return Context->getBlockPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003394 }
3395
Sebastian Redl539c5062010-08-18 23:57:32 +00003396 case TYPE_LVALUE_REFERENCE: {
Richard Smith0f538462011-04-12 10:38:03 +00003397 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003398 Error("Incorrect encoding of lvalue reference type");
3399 return QualType();
3400 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003401 QualType PointeeType = readType(*Loc.F, Record, Idx);
Richard Smith0f538462011-04-12 10:38:03 +00003402 return Context->getLValueReferenceType(PointeeType, Record[1]);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003403 }
3404
Sebastian Redl539c5062010-08-18 23:57:32 +00003405 case TYPE_RVALUE_REFERENCE: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003406 if (Record.size() != 1) {
3407 Error("Incorrect encoding of rvalue reference type");
3408 return QualType();
3409 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003410 QualType PointeeType = readType(*Loc.F, Record, Idx);
Chris Lattner8575daa2009-04-27 21:45:14 +00003411 return Context->getRValueReferenceType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003412 }
3413
Sebastian Redl539c5062010-08-18 23:57:32 +00003414 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidisee776bc2010-07-02 11:55:15 +00003415 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003416 Error("Incorrect encoding of member pointer type");
3417 return QualType();
3418 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003419 QualType PointeeType = readType(*Loc.F, Record, Idx);
3420 QualType ClassType = readType(*Loc.F, Record, Idx);
Douglas Gregor0cdc8322010-12-10 17:03:06 +00003421 if (PointeeType.isNull() || ClassType.isNull())
3422 return QualType();
3423
Chris Lattner8575daa2009-04-27 21:45:14 +00003424 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003425 }
3426
Sebastian Redl539c5062010-08-18 23:57:32 +00003427 case TYPE_CONSTANT_ARRAY: {
Douglas Gregor903b7e92011-07-22 00:38:23 +00003428 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003429 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3430 unsigned IndexTypeQuals = Record[2];
3431 unsigned Idx = 3;
3432 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor04318252009-07-06 15:59:29 +00003433 return Context->getConstantArrayType(ElementType, Size,
3434 ASM, IndexTypeQuals);
3435 }
3436
Sebastian Redl539c5062010-08-18 23:57:32 +00003437 case TYPE_INCOMPLETE_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];
Chris Lattner8575daa2009-04-27 21:45:14 +00003441 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003442 }
3443
Sebastian Redl539c5062010-08-18 23:57:32 +00003444 case TYPE_VARIABLE_ARRAY: {
Douglas Gregor903b7e92011-07-22 00:38:23 +00003445 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00003446 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3447 unsigned IndexTypeQuals = Record[2];
Sebastian Redl2c373b92010-10-05 15:59:54 +00003448 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
3449 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
3450 return Context->getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor04318252009-07-06 15:59:29 +00003451 ASM, IndexTypeQuals,
3452 SourceRange(LBLoc, RBLoc));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003453 }
3454
Sebastian Redl539c5062010-08-18 23:57:32 +00003455 case TYPE_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00003456 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003457 Error("incorrect encoding of vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003458 return QualType();
3459 }
3460
Douglas Gregor903b7e92011-07-22 00:38:23 +00003461 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003462 unsigned NumElements = Record[1];
Bob Wilsonaeb56442010-11-10 21:56:12 +00003463 unsigned VecKind = Record[2];
Chris Lattner37141f42010-06-23 06:00:24 +00003464 return Context->getVectorType(ElementType, NumElements,
Bob Wilsonaeb56442010-11-10 21:56:12 +00003465 (VectorType::VectorKind)VecKind);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003466 }
3467
Sebastian Redl539c5062010-08-18 23:57:32 +00003468 case TYPE_EXT_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00003469 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003470 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003471 return QualType();
3472 }
3473
Douglas Gregor903b7e92011-07-22 00:38:23 +00003474 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003475 unsigned NumElements = Record[1];
Chris Lattner8575daa2009-04-27 21:45:14 +00003476 return Context->getExtVectorType(ElementType, NumElements);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003477 }
3478
Sebastian Redl539c5062010-08-18 23:57:32 +00003479 case TYPE_FUNCTION_NO_PROTO: {
John McCall31168b02011-06-15 23:02:42 +00003480 if (Record.size() != 6) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00003481 Error("incorrect encoding of no-proto function type");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003482 return QualType();
3483 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003484 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCall31168b02011-06-15 23:02:42 +00003485 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
3486 (CallingConv)Record[4], Record[5]);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00003487 return Context->getFunctionNoProtoType(ResultType, Info);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003488 }
3489
Sebastian Redl539c5062010-08-18 23:57:32 +00003490 case TYPE_FUNCTION_PROTO: {
Douglas Gregor903b7e92011-07-22 00:38:23 +00003491 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCalldb40c7f2010-12-14 08:05:40 +00003492
3493 FunctionProtoType::ExtProtoInfo EPI;
3494 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
Eli Friedmanc5b20b52011-04-09 08:18:08 +00003495 /*hasregparm*/ Record[2],
3496 /*regparm*/ Record[3],
John McCall31168b02011-06-15 23:02:42 +00003497 static_cast<CallingConv>(Record[4]),
3498 /*produces*/ Record[5]);
John McCalldb40c7f2010-12-14 08:05:40 +00003499
John McCall31168b02011-06-15 23:02:42 +00003500 unsigned Idx = 6;
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003501 unsigned NumParams = Record[Idx++];
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003502 SmallVector<QualType, 16> ParamTypes;
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003503 for (unsigned I = 0; I != NumParams; ++I)
Douglas Gregor903b7e92011-07-22 00:38:23 +00003504 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
John McCalldb40c7f2010-12-14 08:05:40 +00003505
3506 EPI.Variadic = Record[Idx++];
3507 EPI.TypeQuals = Record[Idx++];
Douglas Gregordb9d6642011-01-26 05:01:58 +00003508 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003509 ExceptionSpecificationType EST =
3510 static_cast<ExceptionSpecificationType>(Record[Idx++]);
3511 EPI.ExceptionSpecType = EST;
3512 if (EST == EST_Dynamic) {
3513 EPI.NumExceptions = Record[Idx++];
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003514 SmallVector<QualType, 2> Exceptions;
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003515 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
Douglas Gregor903b7e92011-07-22 00:38:23 +00003516 Exceptions.push_back(readType(*Loc.F, Record, Idx));
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003517 EPI.Exceptions = Exceptions.data();
3518 } else if (EST == EST_ComputedNoexcept) {
3519 EPI.NoexceptExpr = ReadExpr(*Loc.F);
3520 }
Jay Foad7d0479f2009-05-21 09:52:38 +00003521 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
John McCalldb40c7f2010-12-14 08:05:40 +00003522 EPI);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003523 }
3524
Douglas Gregor7fb09192011-07-21 22:35:25 +00003525 case TYPE_UNRESOLVED_USING: {
3526 unsigned Idx = 0;
John McCallb96ec562009-12-04 22:46:56 +00003527 return Context->getTypeDeclType(
Douglas Gregor7fb09192011-07-21 22:35:25 +00003528 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
3529 }
3530
Sebastian Redl539c5062010-08-18 23:57:32 +00003531 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003532 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003533 Error("incorrect encoding of typedef type");
3534 return QualType();
3535 }
Douglas Gregor7fb09192011-07-21 22:35:25 +00003536 unsigned Idx = 0;
3537 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
Douglas Gregor903b7e92011-07-22 00:38:23 +00003538 QualType Canonical = readType(*Loc.F, Record, Idx);
Douglas Gregorf86c9392010-10-26 00:51:02 +00003539 if (!Canonical.isNull())
3540 Canonical = Context->getCanonicalType(Canonical);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003541 return Context->getTypedefType(Decl, Canonical);
3542 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003543
Sebastian Redl539c5062010-08-18 23:57:32 +00003544 case TYPE_TYPEOF_EXPR:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003545 return Context->getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003546
Sebastian Redl539c5062010-08-18 23:57:32 +00003547 case TYPE_TYPEOF: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003548 if (Record.size() != 1) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003549 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003550 return QualType();
3551 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003552 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Chris Lattner8575daa2009-04-27 21:45:14 +00003553 return Context->getTypeOfType(UnderlyingType);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003554 }
Mike Stump11289f42009-09-09 15:08:12 +00003555
Sebastian Redl539c5062010-08-18 23:57:32 +00003556 case TYPE_DECLTYPE:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003557 return Context->getDecltypeType(ReadExpr(*Loc.F));
Anders Carlsson81df7b82009-06-24 19:06:50 +00003558
Alexis Hunte852b102011-05-24 22:41:36 +00003559 case TYPE_UNARY_TRANSFORM: {
Douglas Gregor903b7e92011-07-22 00:38:23 +00003560 QualType BaseType = readType(*Loc.F, Record, Idx);
3561 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Alexis Hunte852b102011-05-24 22:41:36 +00003562 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
3563 return Context->getUnaryTransformType(BaseType, UnderlyingType, UKind);
3564 }
3565
Richard Smith30482bc2011-02-20 03:19:35 +00003566 case TYPE_AUTO:
Douglas Gregor903b7e92011-07-22 00:38:23 +00003567 return Context->getAutoType(readType(*Loc.F, Record, Idx));
Richard Smith30482bc2011-02-20 03:19:35 +00003568
Sebastian Redl539c5062010-08-18 23:57:32 +00003569 case TYPE_RECORD: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003570 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003571 Error("incorrect encoding of record type");
3572 return QualType();
3573 }
Douglas Gregor7fb09192011-07-21 22:35:25 +00003574 unsigned Idx = 0;
3575 bool IsDependent = Record[Idx++];
3576 QualType T
3577 = Context->getRecordType(ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx));
John McCall424cec92011-01-19 06:33:43 +00003578 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003579 return T;
3580 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003581
Sebastian Redl539c5062010-08-18 23:57:32 +00003582 case TYPE_ENUM: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003583 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003584 Error("incorrect encoding of enum type");
3585 return QualType();
3586 }
Douglas Gregor7fb09192011-07-21 22:35:25 +00003587 unsigned Idx = 0;
3588 bool IsDependent = Record[Idx++];
3589 QualType T
3590 = Context->getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
John McCall424cec92011-01-19 06:33:43 +00003591 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003592 return T;
3593 }
Douglas Gregor1daeb692009-04-13 18:14:40 +00003594
John McCall81904512011-01-06 01:58:22 +00003595 case TYPE_ATTRIBUTED: {
3596 if (Record.size() != 3) {
3597 Error("incorrect encoding of attributed type");
3598 return QualType();
3599 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003600 QualType modifiedType = readType(*Loc.F, Record, Idx);
3601 QualType equivalentType = readType(*Loc.F, Record, Idx);
John McCall81904512011-01-06 01:58:22 +00003602 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
3603 return Context->getAttributedType(kind, modifiedType, equivalentType);
3604 }
3605
Abramo Bagnara924a8f32010-12-10 16:29:40 +00003606 case TYPE_PAREN: {
3607 if (Record.size() != 1) {
3608 Error("incorrect encoding of paren type");
3609 return QualType();
3610 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003611 QualType InnerType = readType(*Loc.F, Record, Idx);
Abramo Bagnara924a8f32010-12-10 16:29:40 +00003612 return Context->getParenType(InnerType);
3613 }
3614
Douglas Gregord2fa7662010-12-20 02:24:11 +00003615 case TYPE_PACK_EXPANSION: {
Douglas Gregor17328502011-02-01 15:24:58 +00003616 if (Record.size() != 2) {
Douglas Gregord2fa7662010-12-20 02:24:11 +00003617 Error("incorrect encoding of pack expansion type");
3618 return QualType();
3619 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00003620 QualType Pattern = readType(*Loc.F, Record, Idx);
Douglas Gregord2fa7662010-12-20 02:24:11 +00003621 if (Pattern.isNull())
3622 return QualType();
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00003623 llvm::Optional<unsigned> NumExpansions;
3624 if (Record[1])
3625 NumExpansions = Record[1] - 1;
3626 return Context->getPackExpansionType(Pattern, NumExpansions);
Douglas Gregord2fa7662010-12-20 02:24:11 +00003627 }
3628
Sebastian Redl539c5062010-08-18 23:57:32 +00003629 case TYPE_ELABORATED: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003630 unsigned Idx = 0;
3631 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor7fb09192011-07-21 22:35:25 +00003632 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor903b7e92011-07-22 00:38:23 +00003633 QualType NamedType = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003634 return Context->getElaboratedType(Keyword, NNS, NamedType);
John McCallfcc33b02009-09-05 00:15:47 +00003635 }
3636
Sebastian Redl539c5062010-08-18 23:57:32 +00003637 case TYPE_OBJC_INTERFACE: {
Chris Lattner587cbe12009-04-22 06:45:28 +00003638 unsigned Idx = 0;
Douglas Gregor7fb09192011-07-21 22:35:25 +00003639 ObjCInterfaceDecl *ItfD
3640 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
John McCall8b07ec22010-05-15 11:32:37 +00003641 return Context->getObjCInterfaceType(ItfD);
3642 }
3643
Sebastian Redl539c5062010-08-18 23:57:32 +00003644 case TYPE_OBJC_OBJECT: {
John McCall8b07ec22010-05-15 11:32:37 +00003645 unsigned Idx = 0;
Douglas Gregor903b7e92011-07-22 00:38:23 +00003646 QualType Base = readType(*Loc.F, Record, Idx);
Chris Lattner587cbe12009-04-22 06:45:28 +00003647 unsigned NumProtos = Record[Idx++];
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003648 SmallVector<ObjCProtocolDecl*, 4> Protos;
Chris Lattner587cbe12009-04-22 06:45:28 +00003649 for (unsigned I = 0; I != NumProtos; ++I)
Douglas Gregor7fb09192011-07-21 22:35:25 +00003650 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003651 return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattner587cbe12009-04-22 06:45:28 +00003652 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003653
Sebastian Redl539c5062010-08-18 23:57:32 +00003654 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattner6e054af2009-04-22 06:40:03 +00003655 unsigned Idx = 0;
Douglas Gregor903b7e92011-07-22 00:38:23 +00003656 QualType Pointee = readType(*Loc.F, Record, Idx);
John McCall8b07ec22010-05-15 11:32:37 +00003657 return Context->getObjCObjectPointerType(Pointee);
Chris Lattner6e054af2009-04-22 06:40:03 +00003658 }
Argyrios Kyrtzidisa7a36df2009-09-29 19:42:55 +00003659
Sebastian Redl539c5062010-08-18 23:57:32 +00003660 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCallcebee162009-10-18 09:09:24 +00003661 unsigned Idx = 0;
Douglas Gregor903b7e92011-07-22 00:38:23 +00003662 QualType Parm = readType(*Loc.F, Record, Idx);
3663 QualType Replacement = readType(*Loc.F, Record, Idx);
John McCallcebee162009-10-18 09:09:24 +00003664 return
3665 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
3666 Replacement);
3667 }
John McCalle78aac42010-03-10 03:28:59 +00003668
Douglas Gregorada4b792011-01-14 02:55:32 +00003669 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
3670 unsigned Idx = 0;
Douglas Gregor903b7e92011-07-22 00:38:23 +00003671 QualType Parm = readType(*Loc.F, Record, Idx);
Douglas Gregorada4b792011-01-14 02:55:32 +00003672 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
3673 return Context->getSubstTemplateTypeParmPackType(
3674 cast<TemplateTypeParmType>(Parm),
3675 ArgPack);
3676 }
3677
Sebastian Redl539c5062010-08-18 23:57:32 +00003678 case TYPE_INJECTED_CLASS_NAME: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00003679 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
Douglas Gregor903b7e92011-07-22 00:38:23 +00003680 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00003681 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003682 // for AST reading, too much interdependencies.
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00003683 return
3684 QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCalle78aac42010-03-10 03:28:59 +00003685 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003686
Sebastian Redl539c5062010-08-18 23:57:32 +00003687 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003688 unsigned Idx = 0;
3689 unsigned Depth = Record[Idx++];
3690 unsigned Index = Record[Idx++];
3691 bool Pack = Record[Idx++];
Douglas Gregor7fb09192011-07-21 22:35:25 +00003692 TemplateTypeParmDecl *D
3693 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
Chandler Carruth08836322011-05-01 00:51:33 +00003694 return Context->getTemplateTypeParmType(Depth, Index, Pack, D);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003695 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003696
Sebastian Redl539c5062010-08-18 23:57:32 +00003697 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00003698 unsigned Idx = 0;
3699 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor7fb09192011-07-21 22:35:25 +00003700 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregora3e41532011-07-28 20:55:49 +00003701 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Douglas Gregor903b7e92011-07-22 00:38:23 +00003702 QualType Canon = readType(*Loc.F, Record, Idx);
Douglas Gregorf86c9392010-10-26 00:51:02 +00003703 if (!Canon.isNull())
3704 Canon = Context->getCanonicalType(Canon);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +00003705 return Context->getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00003706 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003707
Sebastian Redl539c5062010-08-18 23:57:32 +00003708 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003709 unsigned Idx = 0;
3710 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor7fb09192011-07-21 22:35:25 +00003711 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregora3e41532011-07-28 20:55:49 +00003712 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003713 unsigned NumArgs = Record[Idx++];
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003714 SmallVector<TemplateArgument, 8> Args;
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003715 Args.reserve(NumArgs);
3716 while (NumArgs--)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003717 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003718 return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
3719 Args.size(), Args.data());
3720 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003721
Sebastian Redl539c5062010-08-18 23:57:32 +00003722 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00003723 unsigned Idx = 0;
3724
3725 // ArrayType
Douglas Gregor903b7e92011-07-22 00:38:23 +00003726 QualType ElementType = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00003727 ArrayType::ArraySizeModifier ASM
3728 = (ArrayType::ArraySizeModifier)Record[Idx++];
3729 unsigned IndexTypeQuals = Record[Idx++];
3730
3731 // DependentSizedArrayType
Sebastian Redl2c373b92010-10-05 15:59:54 +00003732 Expr *NumElts = ReadExpr(*Loc.F);
3733 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00003734
3735 return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
3736 IndexTypeQuals, Brackets);
3737 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003738
Sebastian Redl539c5062010-08-18 23:57:32 +00003739 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003740 unsigned Idx = 0;
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003741 bool IsDependent = Record[Idx++];
Douglas Gregor5590be02011-01-15 06:45:20 +00003742 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003743 SmallVector<TemplateArgument, 8> Args;
Sebastian Redl2c373b92010-10-05 15:59:54 +00003744 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Douglas Gregor903b7e92011-07-22 00:38:23 +00003745 QualType Underlying = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003746 QualType T;
Richard Smith3f1b5d02011-05-05 21:57:07 +00003747 if (Underlying.isNull())
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003748 T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(),
3749 Args.size());
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003750 else
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003751 T = Context->getTemplateSpecializationType(Name, Args.data(),
Richard Smith3f1b5d02011-05-05 21:57:07 +00003752 Args.size(), Underlying);
John McCall424cec92011-01-19 06:33:43 +00003753 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003754 return T;
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003755 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003756 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003757 // Suppress a GCC warning
3758 return QualType();
3759}
3760
Sebastian Redl2c373b92010-10-05 15:59:54 +00003761class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redl2c499f62010-08-18 23:56:43 +00003762 ASTReader &Reader;
Douglas Gregora6895d82011-07-22 16:00:58 +00003763 Module &F;
Sebastian Redlc67764e2010-07-22 22:43:28 +00003764 llvm::BitstreamCursor &DeclsCursor;
Sebastian Redl2c499f62010-08-18 23:56:43 +00003765 const ASTReader::RecordData &Record;
John McCall8f115c62009-10-16 21:56:05 +00003766 unsigned &Idx;
3767
Sebastian Redl2c373b92010-10-05 15:59:54 +00003768 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
3769 unsigned &I) {
3770 return Reader.ReadSourceLocation(F, R, I);
3771 }
3772
Douglas Gregor7fb09192011-07-21 22:35:25 +00003773 template<typename T>
3774 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
3775 return Reader.ReadDeclAs<T>(F, Record, Idx);
3776 }
3777
John McCall8f115c62009-10-16 21:56:05 +00003778public:
Douglas Gregora6895d82011-07-22 16:00:58 +00003779 TypeLocReader(ASTReader &Reader, Module &F,
Sebastian Redl2c499f62010-08-18 23:56:43 +00003780 const ASTReader::RecordData &Record, unsigned &Idx)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003781 : Reader(Reader), F(F), DeclsCursor(F.DeclsCursor), Record(Record), Idx(Idx)
3782 { }
John McCall8f115c62009-10-16 21:56:05 +00003783
John McCall17001972009-10-18 01:05:36 +00003784 // We want compile-time assurance that we've enumerated all of
3785 // these, so unfortunately we have to declare them first, then
3786 // define them out-of-line.
3787#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +00003788#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +00003789 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +00003790#include "clang/AST/TypeLocNodes.def"
3791
John McCall17001972009-10-18 01:05:36 +00003792 void VisitFunctionTypeLoc(FunctionTypeLoc);
3793 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCall8f115c62009-10-16 21:56:05 +00003794};
3795
John McCall17001972009-10-18 01:05:36 +00003796void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCall8f115c62009-10-16 21:56:05 +00003797 // nothing to do
3798}
John McCall17001972009-10-18 01:05:36 +00003799void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003800 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorc9b7a592010-01-18 18:04:31 +00003801 if (TL.needsExtraLocalData()) {
3802 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
3803 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
3804 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
3805 TL.setModeAttr(Record[Idx++]);
3806 }
John McCall8f115c62009-10-16 21:56:05 +00003807}
John McCall17001972009-10-18 01:05:36 +00003808void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003809 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003810}
John McCall17001972009-10-18 01:05:36 +00003811void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003812 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003813}
John McCall17001972009-10-18 01:05:36 +00003814void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003815 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003816}
John McCall17001972009-10-18 01:05:36 +00003817void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003818 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003819}
John McCall17001972009-10-18 01:05:36 +00003820void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003821 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003822}
John McCall17001972009-10-18 01:05:36 +00003823void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003824 TL.setStarLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnara509357842011-03-05 14:42:21 +00003825 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003826}
John McCall17001972009-10-18 01:05:36 +00003827void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003828 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
3829 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003830 if (Record[Idx++])
Sebastian Redl2c373b92010-10-05 15:59:54 +00003831 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor12bfa382009-10-17 00:13:19 +00003832 else
John McCall17001972009-10-18 01:05:36 +00003833 TL.setSizeExpr(0);
3834}
3835void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
3836 VisitArrayTypeLoc(TL);
3837}
3838void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
3839 VisitArrayTypeLoc(TL);
3840}
3841void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
3842 VisitArrayTypeLoc(TL);
3843}
3844void TypeLocReader::VisitDependentSizedArrayTypeLoc(
3845 DependentSizedArrayTypeLoc TL) {
3846 VisitArrayTypeLoc(TL);
3847}
3848void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
3849 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003850 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003851}
3852void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003853 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003854}
3855void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003856 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003857}
3858void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003859 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
3860 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
Douglas Gregor7fb25412010-10-01 18:44:50 +00003861 TL.setTrailingReturn(Record[Idx++]);
John McCall17001972009-10-18 01:05:36 +00003862 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
Douglas Gregor7fb09192011-07-21 22:35:25 +00003863 TL.setArg(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003864 }
3865}
3866void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
3867 VisitFunctionTypeLoc(TL);
3868}
3869void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
3870 VisitFunctionTypeLoc(TL);
3871}
John McCallb96ec562009-12-04 22:46:56 +00003872void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003873 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallb96ec562009-12-04 22:46:56 +00003874}
John McCall17001972009-10-18 01:05:36 +00003875void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003876 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003877}
3878void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003879 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3880 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3881 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003882}
3883void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003884 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3885 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3886 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3887 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003888}
3889void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003890 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003891}
Alexis Hunte852b102011-05-24 22:41:36 +00003892void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
3893 TL.setKWLoc(ReadSourceLocation(Record, Idx));
3894 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3895 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3896 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
3897}
Richard Smith30482bc2011-02-20 03:19:35 +00003898void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
3899 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3900}
John McCall17001972009-10-18 01:05:36 +00003901void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003902 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003903}
3904void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003905 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003906}
John McCall81904512011-01-06 01:58:22 +00003907void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
3908 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
3909 if (TL.hasAttrOperand()) {
3910 SourceRange range;
3911 range.setBegin(ReadSourceLocation(Record, Idx));
3912 range.setEnd(ReadSourceLocation(Record, Idx));
3913 TL.setAttrOperandParensRange(range);
3914 }
3915 if (TL.hasAttrExprOperand()) {
3916 if (Record[Idx++])
3917 TL.setAttrExprOperand(Reader.ReadExpr(F));
3918 else
3919 TL.setAttrExprOperand(0);
3920 } else if (TL.hasAttrEnumOperand())
3921 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
3922}
John McCall17001972009-10-18 01:05:36 +00003923void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003924 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003925}
John McCallcebee162009-10-18 09:09:24 +00003926void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
3927 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003928 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallcebee162009-10-18 09:09:24 +00003929}
Douglas Gregorada4b792011-01-14 02:55:32 +00003930void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
3931 SubstTemplateTypeParmPackTypeLoc TL) {
3932 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3933}
John McCall17001972009-10-18 01:05:36 +00003934void TypeLocReader::VisitTemplateSpecializationTypeLoc(
3935 TemplateSpecializationTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003936 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
3937 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3938 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall0ad16662009-10-29 08:12:44 +00003939 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
3940 TL.setArgLocInfo(i,
Sebastian Redl2c373b92010-10-05 15:59:54 +00003941 Reader.GetTemplateArgumentLocInfo(F,
3942 TL.getTypePtr()->getArg(i).getKind(),
3943 Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003944}
Abramo Bagnara924a8f32010-12-10 16:29:40 +00003945void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
3946 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3947 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3948}
Abramo Bagnara6150c882010-05-11 21:36:43 +00003949void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003950 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor844cb502011-03-01 18:12:44 +00003951 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003952}
John McCalle78aac42010-03-10 03:28:59 +00003953void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003954 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalle78aac42010-03-10 03:28:59 +00003955}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00003956void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003957 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor3d0da5f2011-03-01 01:34:45 +00003958 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redl2c373b92010-10-05 15:59:54 +00003959 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003960}
John McCallc392f372010-06-11 00:33:02 +00003961void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
3962 DependentTemplateSpecializationTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003963 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregora7a795b2011-03-01 20:11:18 +00003964 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redl2c373b92010-10-05 15:59:54 +00003965 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3966 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3967 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCallc392f372010-06-11 00:33:02 +00003968 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
3969 TL.setArgLocInfo(I,
Sebastian Redl2c373b92010-10-05 15:59:54 +00003970 Reader.GetTemplateArgumentLocInfo(F,
3971 TL.getTypePtr()->getArg(I).getKind(),
3972 Record, Idx));
John McCallc392f372010-06-11 00:33:02 +00003973}
Douglas Gregord2fa7662010-12-20 02:24:11 +00003974void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
3975 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
3976}
John McCall17001972009-10-18 01:05:36 +00003977void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003978 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall8b07ec22010-05-15 11:32:37 +00003979}
3980void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3981 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003982 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3983 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003984 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003985 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003986}
John McCallfc93cf92009-10-22 22:37:11 +00003987void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003988 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCallfc93cf92009-10-22 22:37:11 +00003989}
John McCall8f115c62009-10-16 21:56:05 +00003990
Douglas Gregora6895d82011-07-22 16:00:58 +00003991TypeSourceInfo *ASTReader::GetTypeSourceInfo(Module &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003992 const RecordData &Record,
John McCall8f115c62009-10-16 21:56:05 +00003993 unsigned &Idx) {
Douglas Gregor903b7e92011-07-22 00:38:23 +00003994 QualType InfoTy = readType(F, Record, Idx);
John McCall8f115c62009-10-16 21:56:05 +00003995 if (InfoTy.isNull())
3996 return 0;
3997
John McCallbcd03502009-12-07 02:54:59 +00003998 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003999 TypeLocReader TLR(*this, F, Record, Idx);
John McCallbcd03502009-12-07 02:54:59 +00004000 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCall8f115c62009-10-16 21:56:05 +00004001 TLR.Visit(TL);
John McCallbcd03502009-12-07 02:54:59 +00004002 return TInfo;
John McCall8f115c62009-10-16 21:56:05 +00004003}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004004
Sebastian Redl539c5062010-08-18 23:57:32 +00004005QualType ASTReader::GetType(TypeID ID) {
John McCall8ccfcb52009-09-24 19:53:00 +00004006 unsigned FastQuals = ID & Qualifiers::FastMask;
4007 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004008
Sebastian Redl539c5062010-08-18 23:57:32 +00004009 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004010 QualType T;
Sebastian Redl539c5062010-08-18 23:57:32 +00004011 switch ((PredefinedTypeIDs)Index) {
4012 case PREDEF_TYPE_NULL_ID: return QualType();
4013 case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
4014 case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004015
Sebastian Redl539c5062010-08-18 23:57:32 +00004016 case PREDEF_TYPE_CHAR_U_ID:
4017 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004018 // FIXME: Check that the signedness of CharTy is correct!
Chris Lattner8575daa2009-04-27 21:45:14 +00004019 T = Context->CharTy;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004020 break;
4021
Sebastian Redl539c5062010-08-18 23:57:32 +00004022 case PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break;
4023 case PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break;
4024 case PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break;
4025 case PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break;
4026 case PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break;
4027 case PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break;
4028 case PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break;
4029 case PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break;
4030 case PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break;
4031 case PREDEF_TYPE_INT_ID: T = Context->IntTy; break;
4032 case PREDEF_TYPE_LONG_ID: T = Context->LongTy; break;
4033 case PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break;
4034 case PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break;
4035 case PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break;
4036 case PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break;
4037 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break;
4038 case PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break;
John McCall0009fcc2011-04-26 20:42:42 +00004039 case PREDEF_TYPE_BOUND_MEMBER: T = Context->BoundMemberTy; break;
Sebastian Redl539c5062010-08-18 23:57:32 +00004040 case PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break;
John McCall31996342011-04-07 08:22:57 +00004041 case PREDEF_TYPE_UNKNOWN_ANY: T = Context->UnknownAnyTy; break;
Sebastian Redl539c5062010-08-18 23:57:32 +00004042 case PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break;
4043 case PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break;
4044 case PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break;
4045 case PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break;
4046 case PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break;
4047 case PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break;
Douglas Gregoreda8e122011-08-09 15:13:55 +00004048 case PREDEF_TYPE_AUTO_DEDUCT: T = Context->getAutoDeductType(); break;
4049
4050 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
4051 T = Context->getAutoRRefDeductType();
4052 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004053 }
4054
4055 assert(!T.isNull() && "Unknown predefined type");
John McCall8ccfcb52009-09-24 19:53:00 +00004056 return T.withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004057 }
4058
Sebastian Redl539c5062010-08-18 23:57:32 +00004059 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redl837a6cb2010-07-20 22:37:49 +00004060 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl409183f2010-07-14 20:26:45 +00004061 if (TypesLoaded[Index].isNull()) {
Douglas Gregor903b7e92011-07-22 00:38:23 +00004062 TypesLoaded[Index] = readTypeRecord(Index);
Douglas Gregor9b3932c2010-10-05 18:37:06 +00004063 if (TypesLoaded[Index].isNull())
4064 return QualType();
4065
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004066 TypesLoaded[Index]->setFromAST();
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00004067 if (DeserializationListener)
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00004068 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00004069 TypesLoaded[Index]);
Sebastian Redl409183f2010-07-14 20:26:45 +00004070 }
Mike Stump11289f42009-09-09 15:08:12 +00004071
John McCall8ccfcb52009-09-24 19:53:00 +00004072 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004073}
4074
Douglas Gregora6895d82011-07-22 16:00:58 +00004075QualType ASTReader::getLocalType(Module &F, unsigned LocalID) {
Douglas Gregor903b7e92011-07-22 00:38:23 +00004076 return GetType(getGlobalTypeID(F, LocalID));
4077}
4078
4079serialization::TypeID
Douglas Gregora6895d82011-07-22 16:00:58 +00004080ASTReader::getGlobalTypeID(Module &F, unsigned LocalID) const {
Douglas Gregor5204bde2011-08-02 16:26:37 +00004081 unsigned FastQuals = LocalID & Qualifiers::FastMask;
4082 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
4083
4084 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
4085 return LocalID;
4086
4087 ContinuousRangeMap<uint32_t, int, 2>::iterator I
4088 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
4089 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
4090
4091 unsigned GlobalIndex = LocalIndex + I->second;
4092 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
4093}
4094
John McCall0ad16662009-10-29 08:12:44 +00004095TemplateArgumentLocInfo
Douglas Gregora6895d82011-07-22 16:00:58 +00004096ASTReader::GetTemplateArgumentLocInfo(Module &F,
Sebastian Redl2c373b92010-10-05 15:59:54 +00004097 TemplateArgument::ArgKind Kind,
John McCall0ad16662009-10-29 08:12:44 +00004098 const RecordData &Record,
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00004099 unsigned &Index) {
John McCall0ad16662009-10-29 08:12:44 +00004100 switch (Kind) {
4101 case TemplateArgument::Expression:
Sebastian Redl2c373b92010-10-05 15:59:54 +00004102 return ReadExpr(F);
John McCall0ad16662009-10-29 08:12:44 +00004103 case TemplateArgument::Type:
Sebastian Redl2c373b92010-10-05 15:59:54 +00004104 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004105 case TemplateArgument::Template: {
Douglas Gregor9d802122011-03-02 17:09:35 +00004106 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
4107 Index);
Sebastian Redl2c373b92010-10-05 15:59:54 +00004108 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregor9d802122011-03-02 17:09:35 +00004109 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004110 SourceLocation());
4111 }
4112 case TemplateArgument::TemplateExpansion: {
Douglas Gregor9d802122011-03-02 17:09:35 +00004113 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
4114 Index);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004115 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregoreb29d182011-01-05 17:40:24 +00004116 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregor9d802122011-03-02 17:09:35 +00004117 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregoreb29d182011-01-05 17:40:24 +00004118 EllipsisLoc);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004119 }
John McCall0ad16662009-10-29 08:12:44 +00004120 case TemplateArgument::Null:
4121 case TemplateArgument::Integral:
4122 case TemplateArgument::Declaration:
4123 case TemplateArgument::Pack:
4124 return TemplateArgumentLocInfo();
4125 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +00004126 llvm_unreachable("unexpected template argument loc");
John McCall0ad16662009-10-29 08:12:44 +00004127 return TemplateArgumentLocInfo();
4128}
4129
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004130TemplateArgumentLoc
Douglas Gregora6895d82011-07-22 16:00:58 +00004131ASTReader::ReadTemplateArgumentLoc(Module &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00004132 const RecordData &Record, unsigned &Index) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004133 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004134
4135 if (Arg.getKind() == TemplateArgument::Expression) {
4136 if (Record[Index++]) // bool InfoHasSameExpr.
4137 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
4138 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00004139 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00004140 Record, Index));
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004141}
4142
Sebastian Redl2c499f62010-08-18 23:56:43 +00004143Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall75b960e2010-06-01 09:23:16 +00004144 return GetDecl(ID);
4145}
4146
Douglas Gregorc27b2872011-08-04 00:01:48 +00004147uint64_t ASTReader::readCXXBaseSpecifiers(Module &M, const RecordData &Record,
4148 unsigned &Idx){
4149 if (Idx >= Record.size())
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004150 return 0;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004151
Douglas Gregorc27b2872011-08-04 00:01:48 +00004152 unsigned LocalID = Record[Idx++];
4153 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004154}
4155
4156CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
Douglas Gregord32f0352011-07-22 06:10:01 +00004157 RecordLocation Loc = getLocalBitOffset(Offset);
4158 llvm::BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004159 SavedStreamPosition SavedPosition(Cursor);
Douglas Gregord32f0352011-07-22 06:10:01 +00004160 Cursor.JumpToBit(Loc.Offset);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004161 ReadingKindTracker ReadingKind(Read_Decl, *this);
4162 RecordData Record;
4163 unsigned Code = Cursor.ReadCode();
4164 unsigned RecCode = Cursor.ReadRecord(Code, Record);
4165 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
4166 Error("Malformed AST file: missing C++ base specifiers");
4167 return 0;
4168 }
4169
4170 unsigned Idx = 0;
4171 unsigned NumBases = Record[Idx++];
4172 void *Mem = Context->Allocate(sizeof(CXXBaseSpecifier) * NumBases);
4173 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
4174 for (unsigned I = 0; I != NumBases; ++I)
Douglas Gregord32f0352011-07-22 06:10:01 +00004175 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004176 return Bases;
4177}
4178
Douglas Gregor7fb09192011-07-21 22:35:25 +00004179serialization::DeclID
Douglas Gregora6895d82011-07-22 16:00:58 +00004180ASTReader::getGlobalDeclID(Module &F, unsigned LocalID) const {
Douglas Gregor6f8912e2011-08-03 16:05:40 +00004181 if (LocalID < NUM_PREDEF_DECL_IDS)
Douglas Gregorf7180622011-08-03 15:48:04 +00004182 return LocalID;
4183
4184 ContinuousRangeMap<uint32_t, int, 2>::iterator I
Douglas Gregor6f8912e2011-08-03 16:05:40 +00004185 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
Douglas Gregorf7180622011-08-03 15:48:04 +00004186 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
4187
4188 return LocalID + I->second;
Douglas Gregor7fb09192011-07-21 22:35:25 +00004189}
4190
Sebastian Redl539c5062010-08-18 23:57:32 +00004191Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregor6f8912e2011-08-03 16:05:40 +00004192 if (ID < NUM_PREDEF_DECL_IDS) {
4193 switch ((PredefinedDeclIDs)ID) {
Douglas Gregordab42432011-08-12 00:15:20 +00004194 case PREDEF_DECL_NULL_ID:
Douglas Gregor6f8912e2011-08-03 16:05:40 +00004195 return 0;
Douglas Gregordab42432011-08-12 00:15:20 +00004196
4197 case PREDEF_DECL_TRANSLATION_UNIT_ID:
4198 assert(Context && "No context available?");
4199 return Context->getTranslationUnitDecl();
Douglas Gregor3ea72692011-08-12 05:46:01 +00004200
4201 case PREDEF_DECL_OBJC_ID_ID:
4202 assert(Context && "No context available?");
4203 return Context->getObjCIdDecl();
Douglas Gregor0a586182011-08-12 05:59:41 +00004204
Douglas Gregor52e02802011-08-12 06:17:30 +00004205 case PREDEF_DECL_OBJC_SEL_ID:
4206 assert(Context && "No context available?");
4207 return Context->getObjCSelDecl();
4208
Douglas Gregor0a586182011-08-12 05:59:41 +00004209 case PREDEF_DECL_OBJC_CLASS_ID:
4210 assert(Context && "No context available?");
4211 return Context->getObjCClassDecl();
Douglas Gregor801c99d2011-08-12 06:49:56 +00004212
4213 case PREDEF_DECL_INT_128_ID:
4214 assert(Context && "No context available?");
4215 return Context->getInt128Decl();
4216
4217 case PREDEF_DECL_UNSIGNED_INT_128_ID:
4218 assert(Context && "No context available?");
4219 return Context->getUInt128Decl();
Douglas Gregor6f8912e2011-08-03 16:05:40 +00004220 }
4221
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004222 return 0;
Douglas Gregor6f8912e2011-08-03 16:05:40 +00004223 }
4224
Douglas Gregordab42432011-08-12 00:15:20 +00004225 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
4226
4227 if (Index > DeclsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004228 Error("declaration ID out-of-range for AST file");
Douglas Gregor745ed142009-04-25 18:35:21 +00004229 return 0;
4230 }
Douglas Gregordab42432011-08-12 00:15:20 +00004231
4232if (!DeclsLoaded[Index]) {
Douglas Gregorf7180622011-08-03 15:48:04 +00004233 ReadDeclRecord(ID);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00004234 if (DeserializationListener)
4235 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
4236 }
Douglas Gregor745ed142009-04-25 18:35:21 +00004237
4238 return DeclsLoaded[Index];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004239}
4240
Douglas Gregora6895d82011-07-22 16:00:58 +00004241serialization::DeclID ASTReader::ReadDeclID(Module &F,
Douglas Gregor7fb09192011-07-21 22:35:25 +00004242 const RecordData &Record,
4243 unsigned &Idx) {
4244 if (Idx >= Record.size()) {
4245 Error("Corrupted AST file");
4246 return 0;
4247 }
4248
4249 return getGlobalDeclID(F, Record[Idx++]);
4250}
4251
Chris Lattner9c28af02009-04-27 05:46:25 +00004252/// \brief Resolve the offset of a statement into a statement.
4253///
4254/// This operation will read a new statement from the external
4255/// source each time it is called, and is meant to be used via a
4256/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redl2c499f62010-08-18 23:56:43 +00004257Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidisd9f526f2010-10-28 09:29:32 +00004258 // Switch case IDs are per Decl.
4259 ClearSwitchCaseIDs();
4260
Sebastian Redl5c415f32010-07-22 17:01:13 +00004261 // Offset here is a global offset across the entire chain.
Douglas Gregord32f0352011-07-22 06:10:01 +00004262 RecordLocation Loc = getLocalBitOffset(Offset);
4263 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
4264 return ReadStmtFromStream(*Loc.F);
Douglas Gregor3c3aa612009-04-18 00:07:54 +00004265}
4266
Douglas Gregor3d0adb32011-07-15 21:46:17 +00004267ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00004268 bool (*isKindWeWant)(Decl::Kind),
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004269 SmallVectorImpl<Decl*> &Decls) {
Douglas Gregor94619c82011-08-24 19:03:07 +00004270 // There might be lexical decls in multiple modules, for the TU at
4271 // least.
4272 // FIXME: We might want a faster way to zero
4273 // FIXME: Going backwards through the chain does the right thing for
4274 // chained PCH; for modules, it isn't clear what the right thing is.
4275 for (ModuleReverseIterator M = ModuleMgr.rbegin(), MEnd = ModuleMgr.rend();
4276 M != MEnd; ++M) {
4277 Module::DeclContextInfosMap::iterator Info
4278 = (*M)->DeclContextInfos.find(DC);
4279 if (Info == (*M)->DeclContextInfos.end() || !Info->second.LexicalDecls)
Sebastian Redl5c415f32010-07-22 17:01:13 +00004280 continue;
Sebastian Redl5c415f32010-07-22 17:01:13 +00004281
4282 // Load all of the declaration IDs
Douglas Gregor94619c82011-08-24 19:03:07 +00004283 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
4284 *IDE = ID + Info->second.NumLexicalDecls;
4285 ID != IDE; ++ID) {
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00004286 if (isKindWeWant && !isKindWeWant((Decl::Kind)ID->first))
4287 continue;
Douglas Gregor7fb09192011-07-21 22:35:25 +00004288
Douglas Gregor94619c82011-08-24 19:03:07 +00004289 Decl *D = GetLocalDecl(**M, ID->second);
Sebastian Redlda6a21c2010-09-28 02:24:44 +00004290 assert(D && "Null decl in lexical decls");
4291 Decls.push_back(D);
4292 }
Ted Kremenek1ff615c2010-03-18 00:56:54 +00004293 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004294
Douglas Gregora57c3ab2009-04-22 22:34:57 +00004295 ++NumLexicalDeclContextsRead;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00004296 return ELR_Success;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004297}
4298
Douglas Gregor94619c82011-08-24 19:03:07 +00004299namespace {
4300 /// \brief Module visitor used to perform name lookup into a
4301 /// declaration context.
4302 class DeclContextNameLookupVisitor {
4303 ASTReader &Reader;
4304 const DeclContext *DC;
4305 DeclarationName Name;
4306 SmallVectorImpl<NamedDecl *> &Decls;
4307
4308 public:
4309 DeclContextNameLookupVisitor(ASTReader &Reader,
4310 const DeclContext *DC, DeclarationName Name,
4311 SmallVectorImpl<NamedDecl *> &Decls)
4312 : Reader(Reader), DC(DC), Name(Name), Decls(Decls) { }
4313
4314 static bool visit(Module &M, void *UserData) {
4315 DeclContextNameLookupVisitor *This
4316 = static_cast<DeclContextNameLookupVisitor *>(UserData);
4317
4318 // Check whether we have any visible declaration information for
4319 // this context in this module.
4320 Module::DeclContextInfosMap::iterator Info
4321 = M.DeclContextInfos.find(This->DC);
4322 if (Info == M.DeclContextInfos.end() || !Info->second.NameLookupTableData)
4323 return false;
4324
4325 // Look for this name within this module.
4326 ASTDeclContextNameLookupTable *LookupTable =
4327 (ASTDeclContextNameLookupTable*)Info->second.NameLookupTableData;
4328 ASTDeclContextNameLookupTable::iterator Pos
4329 = LookupTable->find(This->Name);
4330 if (Pos == LookupTable->end())
4331 return false;
4332
4333 bool FoundAnything = false;
4334 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
4335 for (; Data.first != Data.second; ++Data.first) {
4336 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
4337 if (!ND)
4338 continue;
4339
4340 if (ND->getDeclName() != This->Name) {
4341 assert(!This->Name.getCXXNameType().isNull() &&
4342 "Name mismatch without a type");
4343 continue;
4344 }
4345
4346 // Record this declaration.
4347 FoundAnything = true;
4348 This->Decls.push_back(ND);
4349 }
4350
4351 return FoundAnything;
4352 }
4353 };
4354}
4355
John McCall75b960e2010-06-01 09:23:16 +00004356DeclContext::lookup_result
Sebastian Redl2c499f62010-08-18 23:56:43 +00004357ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall75b960e2010-06-01 09:23:16 +00004358 DeclarationName Name) {
Mike Stump11289f42009-09-09 15:08:12 +00004359 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004360 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004361 if (!Name)
4362 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
4363 DeclContext::lookup_iterator(0));
Ted Kremenek1ff615c2010-03-18 00:56:54 +00004364
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004365 SmallVector<NamedDecl *, 64> Decls;
Douglas Gregor94619c82011-08-24 19:03:07 +00004366 DeclContextNameLookupVisitor Visitor(*this, DC, Name, Decls);
4367 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00004368 ++NumVisibleDeclContextsRead;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004369 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall75b960e2010-06-01 09:23:16 +00004370 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004371}
4372
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00004373void ASTReader::MaterializeVisibleDecls(const DeclContext *DC) {
4374 assert(DC->hasExternalVisibleStorage() &&
4375 "DeclContext has no visible decls in storage");
4376
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004377 SmallVector<NamedDecl *, 64> Decls;
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00004378 // There might be visible decls in multiple parts of the chain, for the TU
4379 // and namespaces.
Douglas Gregor94619c82011-08-24 19:03:07 +00004380 // There might be lexical decls in multiple modules, for the TU at
4381 // least.
4382 // FIXME: We might want a faster way to zero
4383 // FIXME: Going backwards through the chain does the right thing for
4384 // chained PCH; for modules, it isn't clear what the right thing is.
4385 for (ModuleReverseIterator M = ModuleMgr.rbegin(), MEnd = ModuleMgr.rend();
4386 M != MEnd; ++M) {
4387 Module::DeclContextInfosMap::iterator Info
4388 = (*M)->DeclContextInfos.find(DC);
4389 if (Info == (*M)->DeclContextInfos.end() || !Info->second.LexicalDecls)
4390 continue;
4391
4392 if (!Info->second.NameLookupTableData)
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00004393 continue;
4394
4395 ASTDeclContextNameLookupTable *LookupTable =
Douglas Gregor94619c82011-08-24 19:03:07 +00004396 (ASTDeclContextNameLookupTable*)Info->second.NameLookupTableData;
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00004397 for (ASTDeclContextNameLookupTable::item_iterator
4398 ItemI = LookupTable->item_begin(),
4399 ItemEnd = LookupTable->item_end() ; ItemI != ItemEnd; ++ItemI) {
4400 ASTDeclContextNameLookupTable::item_iterator::value_type Val
4401 = *ItemI;
4402 ASTDeclContextNameLookupTrait::data_type Data = Val.second;
4403 Decls.clear();
Douglas Gregor94619c82011-08-24 19:03:07 +00004404 for (; Data.first != Data.second; ++Data.first) {
4405 if (NamedDecl *ND = GetLocalDeclAs<NamedDecl>(**M, *Data.first))
4406 Decls.push_back(ND);
4407 }
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00004408 MaterializeVisibleDeclsForName(DC, Val.first, Decls);
4409 }
4410 }
4411}
4412
Sebastian Redl2c499f62010-08-18 23:56:43 +00004413void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004414 assert(Consumer);
4415 while (!InterestingDecls.empty()) {
4416 DeclGroupRef DG(InterestingDecls.front());
4417 InterestingDecls.pop_front();
Sebastian Redleaa4ade2010-08-11 18:52:41 +00004418 Consumer->HandleInterestingDecl(DG);
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004419 }
4420}
4421
Sebastian Redl2c499f62010-08-18 23:56:43 +00004422void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregorb985eeb2009-04-22 19:09:20 +00004423 this->Consumer = Consumer;
4424
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00004425 if (!Consumer)
4426 return;
4427
4428 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004429 // Force deserialization of this decl, which will cause it to be queued for
4430 // passing to the consumer.
Daniel Dunbar865c2a72009-09-17 03:06:44 +00004431 GetDecl(ExternalDefinitions[I]);
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00004432 }
Douglas Gregorf005eac2009-04-25 00:41:30 +00004433
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004434 PassInterestingDeclsToConsumer();
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00004435}
4436
Sebastian Redl2c499f62010-08-18 23:56:43 +00004437void ASTReader::PrintStats() {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004438 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004439
Mike Stump11289f42009-09-09 15:08:12 +00004440 unsigned NumTypesLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00004441 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall8ccfcb52009-09-24 19:53:00 +00004442 QualType());
Douglas Gregor0e149972009-04-25 19:10:14 +00004443 unsigned NumDeclsLoaded
4444 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
4445 (Decl *)0);
4446 unsigned NumIdentifiersLoaded
4447 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
4448 IdentifiersLoaded.end(),
4449 (IdentifierInfo *)0);
Mike Stump11289f42009-09-09 15:08:12 +00004450 unsigned NumSelectorsLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00004451 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
4452 SelectorsLoaded.end(),
4453 Selector());
Douglas Gregorc3b1dd12009-04-13 20:50:16 +00004454
Douglas Gregorc5046832009-04-27 18:38:38 +00004455 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
4456 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor49bf76b2011-07-21 18:46:38 +00004457 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
Douglas Gregor258ae542009-04-27 06:38:32 +00004458 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
4459 NumSLocEntriesRead, TotalNumSLocEntries,
4460 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor745ed142009-04-25 18:35:21 +00004461 if (!TypesLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00004462 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00004463 NumTypesLoaded, (unsigned)TypesLoaded.size(),
4464 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
4465 if (!DeclsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00004466 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00004467 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
4468 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor0e149972009-04-25 19:10:14 +00004469 if (!IdentifiersLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00004470 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor0e149972009-04-25 19:10:14 +00004471 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
4472 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redlada023c2010-08-04 20:40:17 +00004473 if (!SelectorsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00004474 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redlada023c2010-08-04 20:40:17 +00004475 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
4476 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor95c13f52009-04-25 17:48:32 +00004477 if (TotalNumStatements)
4478 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
4479 NumStatementsRead, TotalNumStatements,
4480 ((float)NumStatementsRead/TotalNumStatements * 100));
4481 if (TotalNumMacros)
4482 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
4483 NumMacrosRead, TotalNumMacros,
4484 ((float)NumMacrosRead/TotalNumMacros * 100));
4485 if (TotalLexicalDeclContexts)
4486 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
4487 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
4488 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
4489 * 100));
4490 if (TotalVisibleDeclContexts)
4491 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
4492 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
4493 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
4494 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004495 if (TotalNumMethodPoolEntries) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004496 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004497 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
4498 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor95c13f52009-04-25 17:48:32 +00004499 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004500 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor95c13f52009-04-25 17:48:32 +00004501 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004502 std::fprintf(stderr, "\n");
Douglas Gregor204b8712011-07-21 19:50:14 +00004503 dump();
4504 std::fprintf(stderr, "\n");
4505}
4506
Douglas Gregora6895d82011-07-22 16:00:58 +00004507template<typename Key, typename Module, unsigned InitialCapacity>
Douglas Gregor204b8712011-07-21 19:50:14 +00004508static void
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004509dumpModuleIDMap(StringRef Name,
Douglas Gregora6895d82011-07-22 16:00:58 +00004510 const ContinuousRangeMap<Key, Module *,
Douglas Gregor204b8712011-07-21 19:50:14 +00004511 InitialCapacity> &Map) {
4512 if (Map.begin() == Map.end())
4513 return;
4514
Douglas Gregora6895d82011-07-22 16:00:58 +00004515 typedef ContinuousRangeMap<Key, Module *, InitialCapacity> MapType;
Douglas Gregor204b8712011-07-21 19:50:14 +00004516 llvm::errs() << Name << ":\n";
4517 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
4518 I != IEnd; ++I) {
4519 llvm::errs() << " " << I->first << " -> " << I->second->FileName
4520 << "\n";
4521 }
4522}
4523
Douglas Gregor204b8712011-07-21 19:50:14 +00004524void ASTReader::dump() {
Douglas Gregor1cc9c062011-08-02 11:12:41 +00004525 llvm::errs() << "*** PCH/Module Remappings:\n";
Douglas Gregord32f0352011-07-22 06:10:01 +00004526 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
Douglas Gregor204b8712011-07-21 19:50:14 +00004527 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
Douglas Gregor8ab4ea82011-07-29 00:21:44 +00004528 dumpModuleIDMap("Global type map", GlobalTypeMap);
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00004529 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00004530 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
4531 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
4532 dumpModuleIDMap("Global macro definition map", GlobalMacroDefinitionMap);
4533 dumpModuleIDMap("Global preprocessed entity map",
4534 GlobalPreprocessedEntityMap);
Douglas Gregor1cc9c062011-08-02 11:12:41 +00004535
4536 llvm::errs() << "\n*** PCH/Modules Loaded:";
4537 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
4538 MEnd = ModuleMgr.end();
4539 M != MEnd; ++M)
4540 (*M)->dump();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004541}
4542
Ted Kremenek5e1ed7b2011-04-28 23:46:20 +00004543/// Return the amount of memory used by memory buffers, breaking down
4544/// by heap-backed versus mmap'ed memory.
4545void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00004546 for (ModuleConstIterator I = ModuleMgr.begin(),
4547 E = ModuleMgr.end(); I != E; ++I) {
4548 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
Ted Kremenek5e1ed7b2011-04-28 23:46:20 +00004549 size_t bytes = buf->getBufferSize();
4550 switch (buf->getBufferKind()) {
4551 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
4552 sizes.malloc_bytes += bytes;
4553 break;
4554 case llvm::MemoryBuffer::MemoryBuffer_MMap:
4555 sizes.mmap_bytes += bytes;
4556 break;
4557 }
4558 }
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00004559 }
Ted Kremenek5e1ed7b2011-04-28 23:46:20 +00004560}
4561
Sebastian Redl2c499f62010-08-18 23:56:43 +00004562void ASTReader::InitializeSema(Sema &S) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00004563 SemaObj = &S;
Douglas Gregorc78d3462009-04-24 21:10:55 +00004564 S.ExternalSource = this;
4565
Douglas Gregor7cd60f72009-04-22 21:15:06 +00004566 // Makes sure any declarations that were deserialized "too early"
4567 // still get added to the identifier's declaration chains.
Douglas Gregor2fb99df2010-09-24 23:29:12 +00004568 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
4569 if (SemaObj->TUScope)
John McCall48871652010-08-21 09:40:31 +00004570 SemaObj->TUScope->AddDecl(PreloadedDecls[I]);
Douglas Gregor2fb99df2010-09-24 23:29:12 +00004571
4572 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregora868bbd2009-04-21 22:25:48 +00004573 }
Douglas Gregor7cd60f72009-04-22 21:15:06 +00004574 PreloadedDecls.clear();
Douglas Gregord4df8652009-04-22 22:02:47 +00004575
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004576 // Load the offsets of the declarations that Sema references.
4577 // They will be lazily deserialized when needed.
4578 if (!SemaDeclRefs.empty()) {
4579 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
Douglas Gregorb0f3ae62011-07-28 00:57:24 +00004580 if (!SemaObj->StdNamespace)
4581 SemaObj->StdNamespace = SemaDeclRefs[0];
4582 if (!SemaObj->StdBadAlloc)
4583 SemaObj->StdBadAlloc = SemaDeclRefs[1];
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004584 }
4585
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004586 if (!FPPragmaOptions.empty()) {
4587 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
4588 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
4589 }
4590
4591 if (!OpenCLExtensions.empty()) {
4592 unsigned I = 0;
4593#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
4594#include "clang/Basic/OpenCLExtensions.def"
4595
4596 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
4597 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00004598}
4599
Douglas Gregorab443b92011-08-20 04:39:52 +00004600namespace {
4601 /// \brief Visitor class used to look up identifirs in
4602 class IdentifierLookupVisitor {
4603 StringRef Name;
4604 IdentifierInfo *Found;
4605 public:
4606 explicit IdentifierLookupVisitor(StringRef Name) : Name(Name), Found() { }
Douglas Gregora868bbd2009-04-21 22:25:48 +00004607
Douglas Gregorab443b92011-08-20 04:39:52 +00004608 static bool visit(Module &M, void *UserData) {
4609 IdentifierLookupVisitor *This
4610 = static_cast<IdentifierLookupVisitor *>(UserData);
4611
4612 ASTIdentifierLookupTable *IdTable
4613 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
4614 if (!IdTable)
4615 return false;
4616
4617 std::pair<const char*, unsigned> Key(This->Name.begin(),
4618 This->Name.size());
4619 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
4620 if (Pos == IdTable->end())
4621 return false;
4622
4623 // Dereferencing the iterator has the effect of building the
4624 // IdentifierInfo node and populating it with the various
4625 // declarations it needs.
4626 This->Found = *Pos;
4627 return true;
4628 }
4629
4630 // \brief Retrieve the identifier info found within the module
4631 // files.
4632 IdentifierInfo *getIdentifierInfo() const { return Found; }
4633 };
4634}
4635
4636IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Douglas Gregorab443b92011-08-20 04:39:52 +00004637 IdentifierLookupVisitor Visitor(StringRef(NameStart, NameEnd - NameStart));
4638 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
4639 return Visitor.getIdentifierInfo();
Douglas Gregora868bbd2009-04-21 22:25:48 +00004640}
4641
Douglas Gregor57756ea2010-10-14 22:11:03 +00004642namespace clang {
4643 /// \brief An identifier-lookup iterator that enumerates all of the
4644 /// identifiers stored within a set of AST files.
4645 class ASTIdentifierIterator : public IdentifierIterator {
4646 /// \brief The AST reader whose identifiers are being enumerated.
4647 const ASTReader &Reader;
4648
4649 /// \brief The current index into the chain of AST files stored in
4650 /// the AST reader.
4651 unsigned Index;
4652
4653 /// \brief The current position within the identifier lookup table
4654 /// of the current AST file.
4655 ASTIdentifierLookupTable::key_iterator Current;
4656
4657 /// \brief The end position within the identifier lookup table of
4658 /// the current AST file.
4659 ASTIdentifierLookupTable::key_iterator End;
4660
4661 public:
4662 explicit ASTIdentifierIterator(const ASTReader &Reader);
4663
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004664 virtual StringRef Next();
Douglas Gregor57756ea2010-10-14 22:11:03 +00004665 };
4666}
4667
4668ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00004669 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
Douglas Gregor57756ea2010-10-14 22:11:03 +00004670 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00004671 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
Douglas Gregor57756ea2010-10-14 22:11:03 +00004672 Current = IdTable->key_begin();
4673 End = IdTable->key_end();
4674}
4675
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004676StringRef ASTIdentifierIterator::Next() {
Douglas Gregor57756ea2010-10-14 22:11:03 +00004677 while (Current == End) {
4678 // If we have exhausted all of our AST files, we're done.
4679 if (Index == 0)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004680 return StringRef();
Douglas Gregor57756ea2010-10-14 22:11:03 +00004681
4682 --Index;
4683 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00004684 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
4685 IdentifierLookupTable;
Douglas Gregor57756ea2010-10-14 22:11:03 +00004686 Current = IdTable->key_begin();
4687 End = IdTable->key_end();
4688 }
4689
4690 // We have any identifiers remaining in the current AST file; return
4691 // the next one.
4692 std::pair<const char*, unsigned> Key = *Current;
4693 ++Current;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004694 return StringRef(Key.first, Key.second);
Douglas Gregor57756ea2010-10-14 22:11:03 +00004695}
4696
4697IdentifierIterator *ASTReader::getIdentifiers() const {
4698 return new ASTIdentifierIterator(*this);
4699}
4700
Mike Stump11289f42009-09-09 15:08:12 +00004701std::pair<ObjCMethodList, ObjCMethodList>
Sebastian Redl2c499f62010-08-18 23:56:43 +00004702ASTReader::ReadMethodPool(Selector Sel) {
Sebastian Redlada023c2010-08-04 20:40:17 +00004703 // Find this selector in a hash table. We want to find the most recent entry.
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00004704 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
4705 Module &F = *(*I);
Sebastian Redlada023c2010-08-04 20:40:17 +00004706 if (!F.SelectorLookupTable)
4707 continue;
Douglas Gregorc78d3462009-04-24 21:10:55 +00004708
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004709 ASTSelectorLookupTable *PoolTable
4710 = (ASTSelectorLookupTable*)F.SelectorLookupTable;
4711 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
Sebastian Redlada023c2010-08-04 20:40:17 +00004712 if (Pos != PoolTable->end()) {
4713 ++NumSelectorsRead;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004714 // FIXME: Not quite happy with the statistics here. We probably should
4715 // disable this tracking when called via LoadSelector.
4716 // Also, should entries without methods count as misses?
4717 ++NumMethodPoolEntriesRead;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004718 ASTSelectorLookupTrait::data_type Data = *Pos;
Sebastian Redlada023c2010-08-04 20:40:17 +00004719 if (DeserializationListener)
4720 DeserializationListener->SelectorRead(Data.ID, Sel);
4721 return std::make_pair(Data.Instance, Data.Factory);
4722 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00004723 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00004724
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004725 ++NumMethodPoolMisses;
Sebastian Redlada023c2010-08-04 20:40:17 +00004726 return std::pair<ObjCMethodList, ObjCMethodList>();
Douglas Gregorc78d3462009-04-24 21:10:55 +00004727}
4728
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004729void ASTReader::ReadKnownNamespaces(
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004730 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004731 Namespaces.clear();
4732
4733 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
4734 if (NamespaceDecl *Namespace
4735 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
4736 Namespaces.push_back(Namespace);
4737 }
4738}
4739
Douglas Gregoreb08bd42011-07-27 20:58:46 +00004740void ASTReader::ReadTentativeDefinitions(
4741 SmallVectorImpl<VarDecl *> &TentativeDefs) {
4742 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
4743 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
4744 if (Var)
4745 TentativeDefs.push_back(Var);
4746 }
4747 TentativeDefinitions.clear();
4748}
4749
Douglas Gregora94a1542011-07-27 21:45:57 +00004750void ASTReader::ReadUnusedFileScopedDecls(
4751 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
4752 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
4753 DeclaratorDecl *D
4754 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
4755 if (D)
4756 Decls.push_back(D);
4757 }
4758 UnusedFileScopedDecls.clear();
4759}
4760
Douglas Gregorbae31202011-07-27 21:57:17 +00004761void ASTReader::ReadDelegatingConstructors(
4762 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
4763 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
4764 CXXConstructorDecl *D
4765 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
4766 if (D)
4767 Decls.push_back(D);
4768 }
4769 DelegatingCtorDecls.clear();
4770}
4771
Douglas Gregorb7098a32011-07-28 00:39:29 +00004772void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
4773 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
4774 TypedefNameDecl *D
4775 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
4776 if (D)
4777 Decls.push_back(D);
4778 }
4779 ExtVectorDecls.clear();
4780}
4781
Douglas Gregor32002192011-07-28 00:53:40 +00004782void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
4783 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
4784 CXXRecordDecl *D
4785 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
4786 if (D)
4787 Decls.push_back(D);
4788 }
4789 DynamicClasses.clear();
4790}
4791
Douglas Gregordc5c9582011-07-28 14:20:37 +00004792void
4793ASTReader::ReadLocallyScopedExternalDecls(SmallVectorImpl<NamedDecl *> &Decls) {
4794 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
4795 NamedDecl *D
4796 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
4797 if (D)
4798 Decls.push_back(D);
4799 }
4800 LocallyScopedExternalDecls.clear();
4801}
4802
Douglas Gregor72e357f2011-07-28 14:54:22 +00004803void ASTReader::ReadReferencedSelectors(
4804 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
4805 if (ReferencedSelectorsData.empty())
4806 return;
4807
4808 // If there are @selector references added them to its pool. This is for
4809 // implementation of -Wselector.
4810 unsigned int DataSize = ReferencedSelectorsData.size()-1;
4811 unsigned I = 0;
4812 while (I < DataSize) {
4813 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
4814 SourceLocation SelLoc
4815 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
4816 Sels.push_back(std::make_pair(Sel, SelLoc));
4817 }
4818 ReferencedSelectorsData.clear();
4819}
4820
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00004821void ASTReader::ReadWeakUndeclaredIdentifiers(
4822 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
4823 if (WeakUndeclaredIdentifiers.empty())
4824 return;
4825
4826 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
4827 IdentifierInfo *WeakId
4828 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
4829 IdentifierInfo *AliasId
4830 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
4831 SourceLocation Loc
4832 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
4833 bool Used = WeakUndeclaredIdentifiers[I++];
4834 WeakInfo WI(AliasId, Loc);
4835 WI.setUsed(Used);
4836 WeakIDs.push_back(std::make_pair(WeakId, WI));
4837 }
4838 WeakUndeclaredIdentifiers.clear();
4839}
4840
Douglas Gregor4daf6a32011-07-28 19:11:31 +00004841void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
4842 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
4843 ExternalVTableUse VT;
4844 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
4845 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
4846 VT.DefinitionRequired = VTableUses[Idx++];
4847 VTables.push_back(VT);
4848 }
4849
4850 VTableUses.clear();
4851}
4852
Douglas Gregore39f97c2011-07-28 19:49:54 +00004853void ASTReader::ReadPendingInstantiations(
4854 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
4855 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
4856 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
4857 SourceLocation Loc
4858 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
4859 Pending.push_back(std::make_pair(D, Loc));
4860 }
4861 PendingInstantiations.clear();
4862}
4863
Sebastian Redl2c499f62010-08-18 23:56:43 +00004864void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00004865 // It would be complicated to avoid reading the methods anyway. So don't.
4866 ReadMethodPool(Sel);
4867}
4868
Douglas Gregora3e41532011-07-28 20:55:49 +00004869void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00004870 assert(ID && "Non-zero identifier ID required");
Douglas Gregor6f00bf82009-04-28 21:53:25 +00004871 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor0e149972009-04-25 19:10:14 +00004872 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlff4a2952010-07-23 23:49:55 +00004873 if (DeserializationListener)
4874 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregora868bbd2009-04-21 22:25:48 +00004875}
4876
Douglas Gregor1342e842009-07-06 18:54:52 +00004877/// \brief Set the globally-visible declarations associated with the given
4878/// identifier.
4879///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004880/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump11289f42009-09-09 15:08:12 +00004881/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregor1342e842009-07-06 18:54:52 +00004882/// them.
4883///
4884/// \param II an IdentifierInfo that refers to one or more globally-visible
4885/// declarations.
4886///
4887/// \param DeclIDs the set of declaration IDs with the name @p II that are
4888/// visible at global scope.
4889///
4890/// \param Nonrecursive should be true to indicate that the caller knows that
4891/// this call is non-recursive, and therefore the globally-visible declarations
4892/// will not be placed onto the pending queue.
Mike Stump11289f42009-09-09 15:08:12 +00004893void
Sebastian Redl2c499f62010-08-18 23:56:43 +00004894ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004895 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregor1342e842009-07-06 18:54:52 +00004896 bool Nonrecursive) {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004897 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregor1342e842009-07-06 18:54:52 +00004898 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
4899 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
4900 PII.II = II;
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00004901 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregor1342e842009-07-06 18:54:52 +00004902 return;
4903 }
Mike Stump11289f42009-09-09 15:08:12 +00004904
Douglas Gregor1342e842009-07-06 18:54:52 +00004905 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
4906 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
4907 if (SemaObj) {
Douglas Gregor6fd55e02010-08-13 03:15:25 +00004908 if (SemaObj->TUScope) {
4909 // Introduce this declaration into the translation-unit scope
4910 // and add it to the declaration chain for this identifier, so
4911 // that (unqualified) name lookup will find it.
John McCall48871652010-08-21 09:40:31 +00004912 SemaObj->TUScope->AddDecl(D);
Douglas Gregor6fd55e02010-08-13 03:15:25 +00004913 }
Douglas Gregor2fb99df2010-09-24 23:29:12 +00004914 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
Douglas Gregor1342e842009-07-06 18:54:52 +00004915 } else {
4916 // Queue this declaration so that it will be added to the
4917 // translation unit scope and identifier's declaration chain
4918 // once a Sema object is known.
4919 PreloadedDecls.push_back(D);
4920 }
4921 }
4922}
4923
Douglas Gregora3e41532011-07-28 20:55:49 +00004924IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004925 if (ID == 0)
4926 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00004927
Sebastian Redlc713b962010-07-21 00:46:22 +00004928 if (IdentifiersLoaded.empty()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004929 Error("no identifier table in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004930 return 0;
4931 }
Mike Stump11289f42009-09-09 15:08:12 +00004932
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00004933 assert(PP && "Forgot to set Preprocessor ?");
Sebastian Redlc713b962010-07-21 00:46:22 +00004934 ID -= 1;
4935 if (!IdentifiersLoaded[ID]) {
Douglas Gregor19d26352011-07-20 00:59:32 +00004936 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
4937 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00004938 Module *M = I->second;
4939 unsigned Index = ID - M->BaseIdentifierID;
4940 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
Douglas Gregor5287b4e2009-04-25 21:04:17 +00004941
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004942 // All of the strings in the AST file are preceded by a 16-bit length.
4943 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenekca42a512009-10-23 04:45:31 +00004944 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
4945 // unsigned integers. This is important to avoid integer overflow when
4946 // we cast them to 'unsigned'.
Ted Kremenek49c52322009-10-23 03:57:22 +00004947 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregorab4df582009-04-28 20:01:51 +00004948 unsigned StrLen = (((unsigned) StrLenPtr[0])
4949 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redlc713b962010-07-21 00:46:22 +00004950 IdentifiersLoaded[ID]
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004951 = &PP->getIdentifierTable().get(StringRef(Str, StrLen));
Sebastian Redlff4a2952010-07-23 23:49:55 +00004952 if (DeserializationListener)
4953 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004954 }
Mike Stump11289f42009-09-09 15:08:12 +00004955
Sebastian Redlc713b962010-07-21 00:46:22 +00004956 return IdentifiersLoaded[ID];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004957}
4958
Douglas Gregora3e41532011-07-28 20:55:49 +00004959IdentifierInfo *ASTReader::getLocalIdentifier(Module &M, unsigned LocalID) {
4960 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
4961}
4962
4963IdentifierID ASTReader::getGlobalIdentifierID(Module &M, unsigned LocalID) {
Douglas Gregor1ab036c2011-08-03 21:49:18 +00004964 if (LocalID < NUM_PREDEF_IDENT_IDS)
4965 return LocalID;
4966
4967 ContinuousRangeMap<uint32_t, int, 2>::iterator I
4968 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
4969 assert(I != M.IdentifierRemap.end()
4970 && "Invalid index into identifier index remap");
4971
4972 return LocalID + I->second;
Douglas Gregora3e41532011-07-28 20:55:49 +00004973}
4974
Douglas Gregor925296b2011-07-19 16:10:42 +00004975bool ASTReader::ReadSLocEntry(int ID) {
Douglas Gregor49f754f2011-04-20 00:21:03 +00004976 return ReadSLocEntryRecord(ID) != Success;
Douglas Gregor258ae542009-04-27 06:38:32 +00004977}
4978
Douglas Gregor074fdc52011-07-28 21:16:51 +00004979Selector ASTReader::getLocalSelector(Module &M, unsigned LocalID) {
4980 return DecodeSelector(getGlobalSelectorID(M, LocalID));
4981}
4982
4983Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
Steve Naroff2ddea052009-04-23 10:39:46 +00004984 if (ID == 0)
4985 return Selector();
Mike Stump11289f42009-09-09 15:08:12 +00004986
Sebastian Redlada023c2010-08-04 20:40:17 +00004987 if (ID > SelectorsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004988 Error("selector ID out of range in AST file");
Steve Naroff2ddea052009-04-23 10:39:46 +00004989 return Selector();
4990 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00004991
Sebastian Redlada023c2010-08-04 20:40:17 +00004992 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004993 // Load this selector from the selector table.
Douglas Gregor2262d282011-07-20 01:10:58 +00004994 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
4995 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00004996 Module &M = *I->second;
4997 ASTSelectorLookupTrait Trait(*this, M);
Douglas Gregor8f364fb2011-08-03 23:28:44 +00004998 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
Douglas Gregor2262d282011-07-20 01:10:58 +00004999 SelectorsLoaded[ID - 1] =
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00005000 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
Douglas Gregor2262d282011-07-20 01:10:58 +00005001 if (DeserializationListener)
5002 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
Douglas Gregor95c13f52009-04-25 17:48:32 +00005003 }
5004
Sebastian Redlada023c2010-08-04 20:40:17 +00005005 return SelectorsLoaded[ID - 1];
Steve Naroff2ddea052009-04-23 10:39:46 +00005006}
5007
Douglas Gregor3f8f04f2011-07-28 14:41:43 +00005008Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
Douglas Gregord720daf2010-04-06 17:30:22 +00005009 return DecodeSelector(ID);
5010}
5011
Sebastian Redl2c499f62010-08-18 23:56:43 +00005012uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redlada023c2010-08-04 20:40:17 +00005013 // ID 0 (the null selector) is considered an external selector.
5014 return getTotalNumSelectors() + 1;
Douglas Gregord720daf2010-04-06 17:30:22 +00005015}
5016
Douglas Gregor8f364fb2011-08-03 23:28:44 +00005017serialization::SelectorID
5018ASTReader::getGlobalSelectorID(Module &M, unsigned LocalID) const {
5019 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
5020 return LocalID;
5021
5022 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5023 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
5024 assert(I != M.SelectorRemap.end()
5025 && "Invalid index into identifier index remap");
5026
5027 return LocalID + I->second;
Douglas Gregor3f8f04f2011-07-28 14:41:43 +00005028}
5029
Mike Stump11289f42009-09-09 15:08:12 +00005030DeclarationName
Douglas Gregora6895d82011-07-22 16:00:58 +00005031ASTReader::ReadDeclarationName(Module &F,
Douglas Gregor903b7e92011-07-22 00:38:23 +00005032 const RecordData &Record, unsigned &Idx) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005033 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
5034 switch (Kind) {
5035 case DeclarationName::Identifier:
Douglas Gregora3e41532011-07-28 20:55:49 +00005036 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005037
5038 case DeclarationName::ObjCZeroArgSelector:
5039 case DeclarationName::ObjCOneArgSelector:
5040 case DeclarationName::ObjCMultiArgSelector:
Douglas Gregor074fdc52011-07-28 21:16:51 +00005041 return DeclarationName(ReadSelector(F, Record, Idx));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005042
5043 case DeclarationName::CXXConstructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00005044 return Context->DeclarationNames.getCXXConstructorName(
Douglas Gregor903b7e92011-07-22 00:38:23 +00005045 Context->getCanonicalType(readType(F, Record, Idx)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005046
5047 case DeclarationName::CXXDestructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00005048 return Context->DeclarationNames.getCXXDestructorName(
Douglas Gregor903b7e92011-07-22 00:38:23 +00005049 Context->getCanonicalType(readType(F, Record, Idx)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005050
5051 case DeclarationName::CXXConversionFunctionName:
Chris Lattner8575daa2009-04-27 21:45:14 +00005052 return Context->DeclarationNames.getCXXConversionFunctionName(
Douglas Gregor903b7e92011-07-22 00:38:23 +00005053 Context->getCanonicalType(readType(F, Record, Idx)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005054
5055 case DeclarationName::CXXOperatorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00005056 return Context->DeclarationNames.getCXXOperatorName(
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005057 (OverloadedOperatorKind)Record[Idx++]);
5058
Alexis Hunt3d221f22009-11-29 07:34:05 +00005059 case DeclarationName::CXXLiteralOperatorName:
5060 return Context->DeclarationNames.getCXXLiteralOperatorName(
Douglas Gregora3e41532011-07-28 20:55:49 +00005061 GetIdentifierInfo(F, Record, Idx));
Alexis Hunt3d221f22009-11-29 07:34:05 +00005062
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005063 case DeclarationName::CXXUsingDirective:
5064 return DeclarationName::getUsingDirectiveName();
5065 }
5066
5067 // Required to silence GCC warning
5068 return DeclarationName();
5069}
Douglas Gregor55abb232009-04-10 20:39:37 +00005070
Douglas Gregora6895d82011-07-22 16:00:58 +00005071void ASTReader::ReadDeclarationNameLoc(Module &F,
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005072 DeclarationNameLoc &DNLoc,
5073 DeclarationName Name,
5074 const RecordData &Record, unsigned &Idx) {
5075 switch (Name.getNameKind()) {
5076 case DeclarationName::CXXConstructorName:
5077 case DeclarationName::CXXDestructorName:
5078 case DeclarationName::CXXConversionFunctionName:
5079 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
5080 break;
5081
5082 case DeclarationName::CXXOperatorName:
5083 DNLoc.CXXOperatorName.BeginOpNameLoc
5084 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5085 DNLoc.CXXOperatorName.EndOpNameLoc
5086 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5087 break;
5088
5089 case DeclarationName::CXXLiteralOperatorName:
5090 DNLoc.CXXLiteralOperatorName.OpNameLoc
5091 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5092 break;
5093
5094 case DeclarationName::Identifier:
5095 case DeclarationName::ObjCZeroArgSelector:
5096 case DeclarationName::ObjCOneArgSelector:
5097 case DeclarationName::ObjCMultiArgSelector:
5098 case DeclarationName::CXXUsingDirective:
5099 break;
5100 }
5101}
5102
Douglas Gregora6895d82011-07-22 16:00:58 +00005103void ASTReader::ReadDeclarationNameInfo(Module &F,
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005104 DeclarationNameInfo &NameInfo,
5105 const RecordData &Record, unsigned &Idx) {
Douglas Gregor903b7e92011-07-22 00:38:23 +00005106 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005107 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
5108 DeclarationNameLoc DNLoc;
5109 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
5110 NameInfo.setInfo(DNLoc);
5111}
5112
Douglas Gregora6895d82011-07-22 16:00:58 +00005113void ASTReader::ReadQualifierInfo(Module &F, QualifierInfo &Info,
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005114 const RecordData &Record, unsigned &Idx) {
Douglas Gregor14454802011-02-25 02:25:35 +00005115 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005116 unsigned NumTPLists = Record[Idx++];
5117 Info.NumTemplParamLists = NumTPLists;
5118 if (NumTPLists) {
5119 Info.TemplParamLists = new (*Context) TemplateParameterList*[NumTPLists];
5120 for (unsigned i=0; i != NumTPLists; ++i)
5121 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
5122 }
5123}
5124
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005125TemplateName
Douglas Gregora6895d82011-07-22 16:00:58 +00005126ASTReader::ReadTemplateName(Module &F, const RecordData &Record,
Douglas Gregor5590be02011-01-15 06:45:20 +00005127 unsigned &Idx) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005128 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005129 switch (Kind) {
5130 case TemplateName::Template:
Douglas Gregor7fb09192011-07-21 22:35:25 +00005131 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005132
5133 case TemplateName::OverloadedTemplate: {
5134 unsigned size = Record[Idx++];
5135 UnresolvedSet<8> Decls;
5136 while (size--)
Douglas Gregor7fb09192011-07-21 22:35:25 +00005137 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005138
5139 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
5140 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005141
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005142 case TemplateName::QualifiedTemplate: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00005143 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005144 bool hasTemplKeyword = Record[Idx++];
Douglas Gregor7fb09192011-07-21 22:35:25 +00005145 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005146 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
5147 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005148
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005149 case TemplateName::DependentTemplate: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00005150 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005151 if (Record[Idx++]) // isIdentifier
5152 return Context->getDependentTemplateName(NNS,
Douglas Gregora3e41532011-07-28 20:55:49 +00005153 GetIdentifierInfo(F, Record,
5154 Idx));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005155 return Context->getDependentTemplateName(NNS,
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00005156 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005157 }
John McCalld9dfe3a2011-06-30 08:33:18 +00005158
5159 case TemplateName::SubstTemplateTemplateParm: {
5160 TemplateTemplateParmDecl *param
Douglas Gregor7fb09192011-07-21 22:35:25 +00005161 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
John McCalld9dfe3a2011-06-30 08:33:18 +00005162 if (!param) return TemplateName();
5163 TemplateName replacement = ReadTemplateName(F, Record, Idx);
5164 return Context->getSubstTemplateTemplateParm(param, replacement);
5165 }
Douglas Gregor5590be02011-01-15 06:45:20 +00005166
5167 case TemplateName::SubstTemplateTemplateParmPack: {
5168 TemplateTemplateParmDecl *Param
Douglas Gregor7fb09192011-07-21 22:35:25 +00005169 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
Douglas Gregor5590be02011-01-15 06:45:20 +00005170 if (!Param)
5171 return TemplateName();
5172
5173 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
5174 if (ArgPack.getKind() != TemplateArgument::Pack)
5175 return TemplateName();
5176
5177 return Context->getSubstTemplateTemplateParmPack(Param, ArgPack);
5178 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005179 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005180
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005181 assert(0 && "Unhandled template name kind!");
5182 return TemplateName();
5183}
5184
5185TemplateArgument
Douglas Gregora6895d82011-07-22 16:00:58 +00005186ASTReader::ReadTemplateArgument(Module &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00005187 const RecordData &Record, unsigned &Idx) {
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005188 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
5189 switch (Kind) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005190 case TemplateArgument::Null:
5191 return TemplateArgument();
5192 case TemplateArgument::Type:
Douglas Gregor903b7e92011-07-22 00:38:23 +00005193 return TemplateArgument(readType(F, Record, Idx));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005194 case TemplateArgument::Declaration:
Douglas Gregor7fb09192011-07-21 22:35:25 +00005195 return TemplateArgument(ReadDecl(F, Record, Idx));
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +00005196 case TemplateArgument::Integral: {
5197 llvm::APSInt Value = ReadAPSInt(Record, Idx);
Douglas Gregor903b7e92011-07-22 00:38:23 +00005198 QualType T = readType(F, Record, Idx);
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +00005199 return TemplateArgument(Value, T);
5200 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005201 case TemplateArgument::Template:
Douglas Gregor5590be02011-01-15 06:45:20 +00005202 return TemplateArgument(ReadTemplateName(F, Record, Idx));
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005203 case TemplateArgument::TemplateExpansion: {
Douglas Gregor5590be02011-01-15 06:45:20 +00005204 TemplateName Name = ReadTemplateName(F, Record, Idx);
Douglas Gregore1d60df2011-01-14 23:41:42 +00005205 llvm::Optional<unsigned> NumTemplateExpansions;
5206 if (unsigned NumExpansions = Record[Idx++])
5207 NumTemplateExpansions = NumExpansions - 1;
5208 return TemplateArgument(Name, NumTemplateExpansions);
Douglas Gregoreb29d182011-01-05 17:40:24 +00005209 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005210 case TemplateArgument::Expression:
Sebastian Redl2c373b92010-10-05 15:59:54 +00005211 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005212 case TemplateArgument::Pack: {
5213 unsigned NumArgs = Record[Idx++];
Douglas Gregor1ccc8412010-11-07 23:05:16 +00005214 TemplateArgument *Args = new (*Context) TemplateArgument[NumArgs];
5215 for (unsigned I = 0; I != NumArgs; ++I)
5216 Args[I] = ReadTemplateArgument(F, Record, Idx);
5217 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005218 }
5219 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005220
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005221 assert(0 && "Unhandled template argument kind!");
5222 return TemplateArgument();
5223}
5224
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005225TemplateParameterList *
Douglas Gregora6895d82011-07-22 16:00:58 +00005226ASTReader::ReadTemplateParameterList(Module &F,
Sebastian Redl2c373b92010-10-05 15:59:54 +00005227 const RecordData &Record, unsigned &Idx) {
5228 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
5229 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
5230 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005231
5232 unsigned NumParams = Record[Idx++];
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005233 SmallVector<NamedDecl *, 16> Params;
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005234 Params.reserve(NumParams);
5235 while (NumParams--)
Douglas Gregor7fb09192011-07-21 22:35:25 +00005236 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005237
5238 TemplateParameterList* TemplateParams =
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005239 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
5240 Params.data(), Params.size(), RAngleLoc);
5241 return TemplateParams;
5242}
5243
5244void
Sebastian Redl2c499f62010-08-18 23:56:43 +00005245ASTReader::
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005246ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs,
Douglas Gregora6895d82011-07-22 16:00:58 +00005247 Module &F, const RecordData &Record,
Sebastian Redl2c373b92010-10-05 15:59:54 +00005248 unsigned &Idx) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005249 unsigned NumTemplateArgs = Record[Idx++];
5250 TemplArgs.reserve(NumTemplateArgs);
5251 while (NumTemplateArgs--)
Sebastian Redl2c373b92010-10-05 15:59:54 +00005252 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005253}
5254
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005255/// \brief Read a UnresolvedSet structure.
Douglas Gregora6895d82011-07-22 16:00:58 +00005256void ASTReader::ReadUnresolvedSet(Module &F, UnresolvedSetImpl &Set,
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005257 const RecordData &Record, unsigned &Idx) {
5258 unsigned NumDecls = Record[Idx++];
5259 while (NumDecls--) {
Douglas Gregor7fb09192011-07-21 22:35:25 +00005260 NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx);
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005261 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
5262 Set.addDecl(D, AS);
5263 }
5264}
5265
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005266CXXBaseSpecifier
Douglas Gregora6895d82011-07-22 16:00:58 +00005267ASTReader::ReadCXXBaseSpecifier(Module &F,
Nick Lewycky19b9f952010-07-26 16:56:01 +00005268 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005269 bool isVirtual = static_cast<bool>(Record[Idx++]);
5270 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
5271 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redl08905022011-02-05 19:23:19 +00005272 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00005273 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
5274 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor752a5952011-01-03 22:36:02 +00005275 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redl08905022011-02-05 19:23:19 +00005276 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
Douglas Gregor752a5952011-01-03 22:36:02 +00005277 EllipsisLoc);
Sebastian Redl08905022011-02-05 19:23:19 +00005278 Result.setInheritConstructors(inheritConstructors);
5279 return Result;
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005280}
5281
Alexis Hunt1d792652011-01-08 20:30:50 +00005282std::pair<CXXCtorInitializer **, unsigned>
Douglas Gregora6895d82011-07-22 16:00:58 +00005283ASTReader::ReadCXXCtorInitializers(Module &F, const RecordData &Record,
Alexis Hunt1d792652011-01-08 20:30:50 +00005284 unsigned &Idx) {
5285 CXXCtorInitializer **CtorInitializers = 0;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005286 unsigned NumInitializers = Record[Idx++];
5287 if (NumInitializers) {
5288 ASTContext &C = *getContext();
5289
Alexis Hunt1d792652011-01-08 20:30:50 +00005290 CtorInitializers
5291 = new (C) CXXCtorInitializer*[NumInitializers];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005292 for (unsigned i=0; i != NumInitializers; ++i) {
5293 TypeSourceInfo *BaseClassInfo = 0;
5294 bool IsBaseVirtual = false;
5295 FieldDecl *Member = 0;
Francois Pichetd583da02010-12-04 09:14:42 +00005296 IndirectFieldDecl *IndirectMember = 0;
Alexis Hunt37a477f2011-05-04 01:19:08 +00005297 CXXConstructorDecl *Target = 0;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005298
Alexis Hunt37a477f2011-05-04 01:19:08 +00005299 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
5300 switch (Type) {
5301 case CTOR_INITIALIZER_BASE:
Sebastian Redl2c373b92010-10-05 15:59:54 +00005302 BaseClassInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005303 IsBaseVirtual = Record[Idx++];
Alexis Hunt37a477f2011-05-04 01:19:08 +00005304 break;
5305
5306 case CTOR_INITIALIZER_DELEGATING:
Douglas Gregor7fb09192011-07-21 22:35:25 +00005307 Target = ReadDeclAs<CXXConstructorDecl>(F, Record, Idx);
Alexis Hunt37a477f2011-05-04 01:19:08 +00005308 break;
5309
5310 case CTOR_INITIALIZER_MEMBER:
Douglas Gregor7fb09192011-07-21 22:35:25 +00005311 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
Alexis Hunt37a477f2011-05-04 01:19:08 +00005312 break;
5313
5314 case CTOR_INITIALIZER_INDIRECT_MEMBER:
Douglas Gregor7fb09192011-07-21 22:35:25 +00005315 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
Alexis Hunt37a477f2011-05-04 01:19:08 +00005316 break;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005317 }
Alexis Hunt37a477f2011-05-04 01:19:08 +00005318
Douglas Gregor44e7df62011-01-04 00:32:56 +00005319 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redl2c373b92010-10-05 15:59:54 +00005320 Expr *Init = ReadExpr(F);
Sebastian Redl2c373b92010-10-05 15:59:54 +00005321 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
5322 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005323 bool IsWritten = Record[Idx++];
5324 unsigned SourceOrderOrNumArrayIndices;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005325 SmallVector<VarDecl *, 8> Indices;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005326 if (IsWritten) {
5327 SourceOrderOrNumArrayIndices = Record[Idx++];
5328 } else {
5329 SourceOrderOrNumArrayIndices = Record[Idx++];
5330 Indices.reserve(SourceOrderOrNumArrayIndices);
5331 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
Douglas Gregor7fb09192011-07-21 22:35:25 +00005332 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005333 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005334
Alexis Hunt1d792652011-01-08 20:30:50 +00005335 CXXCtorInitializer *BOMInit;
Alexis Hunt37a477f2011-05-04 01:19:08 +00005336 if (Type == CTOR_INITIALIZER_BASE) {
Alexis Hunt1d792652011-01-08 20:30:50 +00005337 BOMInit = new (C) CXXCtorInitializer(C, BaseClassInfo, IsBaseVirtual,
5338 LParenLoc, Init, RParenLoc,
5339 MemberOrEllipsisLoc);
Alexis Hunt37a477f2011-05-04 01:19:08 +00005340 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
5341 BOMInit = new (C) CXXCtorInitializer(C, MemberOrEllipsisLoc, LParenLoc,
5342 Target, Init, RParenLoc);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005343 } else if (IsWritten) {
Francois Pichetd583da02010-12-04 09:14:42 +00005344 if (Member)
Alexis Hunt1d792652011-01-08 20:30:50 +00005345 BOMInit = new (C) CXXCtorInitializer(C, Member, MemberOrEllipsisLoc,
5346 LParenLoc, Init, RParenLoc);
Francois Pichetd583da02010-12-04 09:14:42 +00005347 else
Alexis Hunt1d792652011-01-08 20:30:50 +00005348 BOMInit = new (C) CXXCtorInitializer(C, IndirectMember,
5349 MemberOrEllipsisLoc, LParenLoc,
5350 Init, RParenLoc);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005351 } else {
Alexis Hunt1d792652011-01-08 20:30:50 +00005352 BOMInit = CXXCtorInitializer::Create(C, Member, MemberOrEllipsisLoc,
5353 LParenLoc, Init, RParenLoc,
5354 Indices.data(), Indices.size());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005355 }
5356
Argyrios Kyrtzidisd05f3e32010-09-06 19:04:27 +00005357 if (IsWritten)
5358 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Alexis Hunt1d792652011-01-08 20:30:50 +00005359 CtorInitializers[i] = BOMInit;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005360 }
5361 }
5362
Alexis Hunt1d792652011-01-08 20:30:50 +00005363 return std::make_pair(CtorInitializers, NumInitializers);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005364}
5365
Chris Lattnerca025db2010-05-07 21:43:38 +00005366NestedNameSpecifier *
Douglas Gregora6895d82011-07-22 16:00:58 +00005367ASTReader::ReadNestedNameSpecifier(Module &F,
Douglas Gregor7fb09192011-07-21 22:35:25 +00005368 const RecordData &Record, unsigned &Idx) {
Chris Lattnerca025db2010-05-07 21:43:38 +00005369 unsigned N = Record[Idx++];
5370 NestedNameSpecifier *NNS = 0, *Prev = 0;
5371 for (unsigned I = 0; I != N; ++I) {
5372 NestedNameSpecifier::SpecifierKind Kind
5373 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
5374 switch (Kind) {
5375 case NestedNameSpecifier::Identifier: {
Douglas Gregora3e41532011-07-28 20:55:49 +00005376 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Chris Lattnerca025db2010-05-07 21:43:38 +00005377 NNS = NestedNameSpecifier::Create(*Context, Prev, II);
5378 break;
5379 }
5380
5381 case NestedNameSpecifier::Namespace: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00005382 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Chris Lattnerca025db2010-05-07 21:43:38 +00005383 NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
5384 break;
5385 }
5386
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005387 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00005388 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005389 NNS = NestedNameSpecifier::Create(*Context, Prev, Alias);
5390 break;
5391 }
5392
Chris Lattnerca025db2010-05-07 21:43:38 +00005393 case NestedNameSpecifier::TypeSpec:
5394 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregor903b7e92011-07-22 00:38:23 +00005395 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
Douglas Gregor0cdc8322010-12-10 17:03:06 +00005396 if (!T)
5397 return 0;
5398
Chris Lattnerca025db2010-05-07 21:43:38 +00005399 bool Template = Record[Idx++];
5400 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
5401 break;
5402 }
5403
5404 case NestedNameSpecifier::Global: {
5405 NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
5406 // No associated value, and there can't be a prefix.
5407 break;
5408 }
Chris Lattnerca025db2010-05-07 21:43:38 +00005409 }
Argyrios Kyrtzidisad65c692010-07-07 15:46:30 +00005410 Prev = NNS;
Chris Lattnerca025db2010-05-07 21:43:38 +00005411 }
5412 return NNS;
5413}
5414
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005415NestedNameSpecifierLoc
Douglas Gregora6895d82011-07-22 16:00:58 +00005416ASTReader::ReadNestedNameSpecifierLoc(Module &F, const RecordData &Record,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005417 unsigned &Idx) {
5418 unsigned N = Record[Idx++];
Douglas Gregor9b272512011-02-28 23:58:31 +00005419 NestedNameSpecifierLocBuilder Builder;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005420 for (unsigned I = 0; I != N; ++I) {
5421 NestedNameSpecifier::SpecifierKind Kind
5422 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
5423 switch (Kind) {
5424 case NestedNameSpecifier::Identifier: {
Douglas Gregora3e41532011-07-28 20:55:49 +00005425 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005426 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00005427 Builder.Extend(*Context, II, Range.getBegin(), Range.getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005428 break;
5429 }
5430
5431 case NestedNameSpecifier::Namespace: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00005432 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005433 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00005434 Builder.Extend(*Context, NS, Range.getBegin(), Range.getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005435 break;
5436 }
5437
5438 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00005439 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005440 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00005441 Builder.Extend(*Context, Alias, Range.getBegin(), Range.getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005442 break;
5443 }
5444
5445 case NestedNameSpecifier::TypeSpec:
5446 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005447 bool Template = Record[Idx++];
5448 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
5449 if (!T)
5450 return NestedNameSpecifierLoc();
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005451 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00005452
5453 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
5454 Builder.Extend(*Context,
5455 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
5456 T->getTypeLoc(), ColonColonLoc);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005457 break;
5458 }
5459
5460 case NestedNameSpecifier::Global: {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005461 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00005462 Builder.MakeGlobal(*Context, ColonColonLoc);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005463 break;
5464 }
5465 }
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005466 }
5467
Douglas Gregor9b272512011-02-28 23:58:31 +00005468 return Builder.getWithLocInContext(*Context);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005469}
5470
Chris Lattnerca025db2010-05-07 21:43:38 +00005471SourceRange
Douglas Gregora6895d82011-07-22 16:00:58 +00005472ASTReader::ReadSourceRange(Module &F, const RecordData &Record,
Sebastian Redl2c373b92010-10-05 15:59:54 +00005473 unsigned &Idx) {
5474 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
5475 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar6d3bc082010-06-02 15:47:10 +00005476 return SourceRange(beg, end);
Chris Lattnerca025db2010-05-07 21:43:38 +00005477}
5478
Douglas Gregor1daeb692009-04-13 18:14:40 +00005479/// \brief Read an integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00005480llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00005481 unsigned BitWidth = Record[Idx++];
5482 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
5483 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
5484 Idx += NumWords;
5485 return Result;
5486}
5487
5488/// \brief Read a signed integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00005489llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00005490 bool isUnsigned = Record[Idx++];
5491 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
5492}
5493
Douglas Gregore0a3a512009-04-14 21:55:33 +00005494/// \brief Read a floating-point value
Sebastian Redl2c499f62010-08-18 23:56:43 +00005495llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00005496 return llvm::APFloat(ReadAPInt(Record, Idx));
5497}
5498
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00005499// \brief Read a string
Sebastian Redl2c499f62010-08-18 23:56:43 +00005500std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00005501 unsigned Len = Record[Idx++];
Jay Foad7d0479f2009-05-21 09:52:38 +00005502 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00005503 Idx += Len;
5504 return Result;
5505}
5506
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005507VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
5508 unsigned &Idx) {
5509 unsigned Major = Record[Idx++];
5510 unsigned Minor = Record[Idx++];
5511 unsigned Subminor = Record[Idx++];
5512 if (Minor == 0)
5513 return VersionTuple(Major);
5514 if (Subminor == 0)
5515 return VersionTuple(Major, Minor - 1);
5516 return VersionTuple(Major, Minor - 1, Subminor - 1);
5517}
5518
Douglas Gregora6895d82011-07-22 16:00:58 +00005519CXXTemporary *ASTReader::ReadCXXTemporary(Module &F,
Douglas Gregor7fb09192011-07-21 22:35:25 +00005520 const RecordData &Record,
Chris Lattnercba86142010-05-10 00:25:06 +00005521 unsigned &Idx) {
Douglas Gregor7fb09192011-07-21 22:35:25 +00005522 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
Chris Lattnercba86142010-05-10 00:25:06 +00005523 return CXXTemporary::Create(*Context, Decl);
5524}
5525
Sebastian Redl2c499f62010-08-18 23:56:43 +00005526DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregor92863e42009-04-10 23:10:45 +00005527 return Diag(SourceLocation(), DiagID);
5528}
5529
Sebastian Redl2c499f62010-08-18 23:56:43 +00005530DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00005531 return Diags.Report(Loc, DiagID);
Douglas Gregor55abb232009-04-10 20:39:37 +00005532}
Douglas Gregora9af1d12009-04-17 00:04:06 +00005533
Douglas Gregora868bbd2009-04-21 22:25:48 +00005534/// \brief Retrieve the identifier table associated with the
5535/// preprocessor.
Sebastian Redl2c499f62010-08-18 23:56:43 +00005536IdentifierTable &ASTReader::getIdentifierTable() {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00005537 assert(PP && "Forgot to set Preprocessor ?");
5538 return PP->getIdentifierTable();
Douglas Gregora868bbd2009-04-21 22:25:48 +00005539}
5540
Douglas Gregora9af1d12009-04-17 00:04:06 +00005541/// \brief Record that the given ID maps to the given switch-case
5542/// statement.
Sebastian Redl2c499f62010-08-18 23:56:43 +00005543void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Douglas Gregora9af1d12009-04-17 00:04:06 +00005544 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
5545 SwitchCaseStmts[ID] = SC;
5546}
5547
5548/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00005549SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Douglas Gregora9af1d12009-04-17 00:04:06 +00005550 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
5551 return SwitchCaseStmts[ID];
5552}
Douglas Gregor6cc68a42009-04-17 18:18:49 +00005553
Argyrios Kyrtzidisd9f526f2010-10-28 09:29:32 +00005554void ASTReader::ClearSwitchCaseIDs() {
5555 SwitchCaseStmts.clear();
5556}
5557
Sebastian Redl2c499f62010-08-18 23:56:43 +00005558void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00005559 assert(NumCurrentElementsDeserializing &&
5560 "FinishedDeserializing not paired with StartedDeserializing");
5561 if (NumCurrentElementsDeserializing == 1) {
Douglas Gregor1342e842009-07-06 18:54:52 +00005562 // If any identifiers with corresponding top-level declarations have
5563 // been loaded, load those declarations now.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00005564 while (!PendingIdentifierInfos.empty()) {
5565 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
5566 PendingIdentifierInfos.front().DeclIDs, true);
5567 PendingIdentifierInfos.pop_front();
Douglas Gregor1342e842009-07-06 18:54:52 +00005568 }
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00005569
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00005570 // Ready to load previous declarations of Decls that were delayed.
5571 while (!PendingPreviousDecls.empty()) {
5572 loadAndAttachPreviousDecl(PendingPreviousDecls.front().first,
5573 PendingPreviousDecls.front().second);
5574 PendingPreviousDecls.pop_front();
5575 }
5576
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00005577 // We are not in recursive loading, so it's safe to pass the "interesting"
5578 // decls to the consumer.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00005579 if (Consumer)
5580 PassInterestingDeclsToConsumer();
Argyrios Kyrtzidisad5f95c2010-10-24 17:26:31 +00005581
5582 assert(PendingForwardRefs.size() == 0 &&
5583 "Some forward refs did not get linked to the definition!");
Douglas Gregor1342e842009-07-06 18:54:52 +00005584 }
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00005585 --NumCurrentElementsDeserializing;
Douglas Gregor1342e842009-07-06 18:54:52 +00005586}
Douglas Gregorb473b072010-08-19 00:28:17 +00005587
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005588ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
Douglas Gregorc567ba22011-07-22 16:35:34 +00005589 StringRef isysroot, bool DisableValidation,
Douglas Gregor606c4ac2011-02-05 19:42:43 +00005590 bool DisableStatCache)
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005591 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
5592 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
5593 Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
Jonathan D. Turnerecc27402011-07-28 17:20:23 +00005594 Consumer(0), ModuleMgr(FileMgr.getFileSystemOptions()),
5595 RelocatablePCH(false), isysroot(isysroot),
Douglas Gregor925296b2011-07-19 16:10:42 +00005596 DisableValidation(DisableValidation),
Douglas Gregor606c4ac2011-02-05 19:42:43 +00005597 DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0),
Douglas Gregor925296b2011-07-19 16:10:42 +00005598 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Douglas Gregor606c4ac2011-02-05 19:42:43 +00005599 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
5600 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
5601 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
5602 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
Jonathan D. Turner3766fdb2011-07-21 21:15:19 +00005603 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
5604 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
5605 NumCXXBaseSpecifiersLoaded(0)
Douglas Gregor606c4ac2011-02-05 19:42:43 +00005606{
Douglas Gregor925296b2011-07-19 16:10:42 +00005607 SourceMgr.setExternalSLocEntrySource(this);
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005608}
5609
5610ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
Douglas Gregorc567ba22011-07-22 16:35:34 +00005611 Diagnostic &Diags, StringRef isysroot,
Douglas Gregor606c4ac2011-02-05 19:42:43 +00005612 bool DisableValidation, bool DisableStatCache)
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005613 : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
Jonathan D. Turnerecc27402011-07-28 17:20:23 +00005614 Diags(Diags), SemaObj(0), PP(0), Context(0),
5615 Consumer(0), ModuleMgr(FileMgr.getFileSystemOptions()),
Douglas Gregor925296b2011-07-19 16:10:42 +00005616 RelocatablePCH(false), isysroot(isysroot),
5617 DisableValidation(DisableValidation), DisableStatCache(DisableStatCache),
5618 NumStatHits(0), NumStatMisses(0), NumSLocEntriesRead(0),
5619 TotalNumSLocEntries(0), NumStatementsRead(0),
5620 TotalNumStatements(0), NumMacrosRead(0), TotalNumMacros(0),
5621 NumSelectorsRead(0), NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
Sebastian Redlc1d035f2010-09-22 20:19:08 +00005622 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
5623 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
Jonathan D. Turner3766fdb2011-07-21 21:15:19 +00005624 TotalVisibleDeclContexts(0), TotalModulesSizeInBits(0),
5625 NumCurrentElementsDeserializing(0), NumCXXBaseSpecifiersLoaded(0)
Douglas Gregor925296b2011-07-19 16:10:42 +00005626{
5627 SourceMgr.setExternalSLocEntrySource(this);
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005628}
5629
5630ASTReader::~ASTReader() {
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005631 for (DeclContextVisibleUpdatesPending::iterator
5632 I = PendingVisibleUpdates.begin(),
5633 E = PendingVisibleUpdates.end();
5634 I != E; ++I) {
5635 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
5636 F = I->second.end();
5637 J != F; ++J)
Douglas Gregorf7180622011-08-03 15:48:04 +00005638 delete static_cast<ASTDeclContextNameLookupTable*>(J->first);
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005639 }
5640}
5641
Douglas Gregorab443b92011-08-20 04:39:52 +00005642//===----------------------------------------------------------------------===//
5643// Module implementation
5644//===----------------------------------------------------------------------===//
Douglas Gregora6895d82011-07-22 16:00:58 +00005645Module::Module(ModuleKind Kind)
Douglas Gregordf0c1512011-08-18 04:12:04 +00005646 : Kind(Kind), DirectlyImported(false), SizeInBits(0),
5647 LocalNumSLocEntries(0), SLocEntryBaseID(0),
Douglas Gregor925296b2011-07-19 16:10:42 +00005648 SLocEntryBaseOffset(0), SLocEntryOffsets(0),
5649 SLocFileOffsets(0), LocalNumIdentifiers(0),
Douglas Gregor00659902011-08-02 10:56:51 +00005650 IdentifierOffsets(0), BaseIdentifierID(0), IdentifierTableData(0),
5651 IdentifierLookupTable(0), BasePreprocessedEntityID(0),
5652 LocalNumMacroDefinitions(0), MacroDefinitionOffsets(0),
5653 BaseMacroDefinitionID(0), LocalNumHeaderFileInfos(0),
Douglas Gregor925296b2011-07-19 16:10:42 +00005654 HeaderFileInfoTableData(0), HeaderFileInfoTable(0),
Douglas Gregor4b123cb2011-07-28 04:50:02 +00005655 HeaderFileFrameworkStrings(0),
Douglas Gregor00659902011-08-02 10:56:51 +00005656 LocalNumSelectors(0), SelectorOffsets(0), BaseSelectorID(0),
Sebastian Redl949fe9e2010-09-22 00:42:27 +00005657 SelectorLookupTableData(0), SelectorLookupTable(0), LocalNumDecls(0),
Douglas Gregor00659902011-08-02 10:56:51 +00005658 DeclOffsets(0), BaseDeclID(0),
5659 LocalNumCXXBaseSpecifiers(0), CXXBaseSpecifiersOffsets(0),
Douglas Gregor3b65ed02011-08-02 18:32:54 +00005660 LocalNumTypes(0), TypeOffsets(0), BaseTypeIndex(0), StatCache(0),
Jonathan D. Turner269f2562011-07-28 23:15:22 +00005661 NumPreallocatedPreprocessingEntities(0)
Douglas Gregorb473b072010-08-19 00:28:17 +00005662{}
5663
Douglas Gregora6895d82011-07-22 16:00:58 +00005664Module::~Module() {
Douglas Gregor94619c82011-08-24 19:03:07 +00005665 for (DeclContextInfosMap::iterator I = DeclContextInfos.begin(),
5666 E = DeclContextInfos.end();
5667 I != E; ++I) {
5668 if (I->second.NameLookupTableData)
5669 delete static_cast<ASTDeclContextNameLookupTable*>(
5670 I->second.NameLookupTableData);
5671 }
5672
Douglas Gregorb473b072010-08-19 00:28:17 +00005673 delete static_cast<ASTIdentifierLookupTable *>(IdentifierLookupTable);
Douglas Gregor09b69892011-02-10 17:09:37 +00005674 delete static_cast<HeaderFileInfoLookupTable *>(HeaderFileInfoTable);
Douglas Gregorb473b072010-08-19 00:28:17 +00005675 delete static_cast<ASTSelectorLookupTable *>(SelectorLookupTable);
5676}
Jonathan D. Turnerb2b08232011-07-26 18:21:30 +00005677
Douglas Gregor1cc9c062011-08-02 11:12:41 +00005678template<typename Key, typename Offset, unsigned InitialCapacity>
5679static void
5680dumpLocalRemap(StringRef Name,
5681 const ContinuousRangeMap<Key, Offset, InitialCapacity> &Map) {
5682 if (Map.begin() == Map.end())
5683 return;
5684
5685 typedef ContinuousRangeMap<Key, Offset, InitialCapacity> MapType;
5686 llvm::errs() << " " << Name << ":\n";
5687 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
5688 I != IEnd; ++I) {
5689 llvm::errs() << " " << I->first << " -> " << I->second
5690 << "\n";
5691 }
5692}
5693
5694void Module::dump() {
5695 llvm::errs() << "\nModule: " << FileName << "\n";
5696 if (!Imports.empty()) {
5697 llvm::errs() << " Imports: ";
5698 for (unsigned I = 0, N = Imports.size(); I != N; ++I) {
5699 if (I)
5700 llvm::errs() << ", ";
5701 llvm::errs() << Imports[I]->FileName;
5702 }
5703 llvm::errs() << "\n";
5704 }
5705
5706 // Remapping tables.
5707 llvm::errs() << " Base source location offset: " << SLocEntryBaseOffset
5708 << '\n';
Douglas Gregorfddf5302011-08-04 19:00:50 +00005709 dumpLocalRemap("Source location offset local -> global map", SLocRemap);
5710
Douglas Gregor1ab036c2011-08-03 21:49:18 +00005711 llvm::errs() << " Base identifier ID: " << BaseIdentifierID << '\n'
Douglas Gregor2f555fc2011-08-04 18:56:47 +00005712 << " Number of identifiers: " << LocalNumIdentifiers << '\n';
Douglas Gregorfddf5302011-08-04 19:00:50 +00005713 dumpLocalRemap("Identifier ID local -> global map", IdentifierRemap);
5714
5715 llvm::errs() << " Base selector ID: " << BaseSelectorID << '\n'
5716 << " Number of selectors: " << LocalNumSelectors << '\n';
5717 dumpLocalRemap("Selector ID local -> global map", SelectorRemap);
5718
Douglas Gregor2f555fc2011-08-04 18:56:47 +00005719 llvm::errs() << " Base preprocessed entity ID: " << BasePreprocessedEntityID
5720 << '\n'
Douglas Gregordab42432011-08-12 00:15:20 +00005721 << " Number of preprocessed entities: "
Douglas Gregor2f555fc2011-08-04 18:56:47 +00005722 << NumPreallocatedPreprocessingEntities << '\n';
Douglas Gregorfddf5302011-08-04 19:00:50 +00005723 dumpLocalRemap("Preprocessed entity ID local -> global map",
5724 PreprocessedEntityRemap);
5725
Douglas Gregora863b4b2011-08-04 16:36:56 +00005726 llvm::errs() << " Base macro definition ID: " << BaseMacroDefinitionID
5727 << '\n'
5728 << " Number of macro definitions: " << LocalNumMacroDefinitions
5729 << '\n';
Douglas Gregorfddf5302011-08-04 19:00:50 +00005730 dumpLocalRemap("Macro definition ID local -> global map",
5731 MacroDefinitionRemap);
5732
5733 llvm::errs() << " Base type index: " << BaseTypeIndex << '\n'
5734 << " Number of types: " << LocalNumTypes << '\n';
5735 dumpLocalRemap("Type index local -> global map", TypeRemap);
5736
Douglas Gregorf7180622011-08-03 15:48:04 +00005737 llvm::errs() << " Base decl ID: " << BaseDeclID << '\n'
5738 << " Number of decls: " << LocalNumDecls << '\n';
Douglas Gregorfddf5302011-08-04 19:00:50 +00005739 dumpLocalRemap("Decl ID local -> global map", DeclRemap);
Douglas Gregor1cc9c062011-08-02 11:12:41 +00005740}
5741
Douglas Gregorab443b92011-08-20 04:39:52 +00005742//===----------------------------------------------------------------------===//
5743// Module manager implementation
5744//===----------------------------------------------------------------------===//
5745
Jonathan D. Turnerecc27402011-07-28 17:20:23 +00005746Module *ModuleManager::lookup(StringRef Name) {
5747 const FileEntry *Entry = FileMgr.getFile(Name);
5748 return Modules[Entry];
5749}
5750
Jonathan D. Turnerdb1c9e32011-08-02 17:40:32 +00005751llvm::MemoryBuffer *ModuleManager::lookupBuffer(StringRef Name) {
5752 const FileEntry *Entry = FileMgr.getFile(Name);
5753 return InMemoryBuffers[Entry];
5754}
5755
Douglas Gregor4dd3e942011-08-19 02:29:29 +00005756std::pair<Module *, bool>
5757ModuleManager::addModule(StringRef FileName, ModuleKind Type,
5758 Module *ImportedBy, std::string &ErrorStr) {
Chad Rosier222e1872011-08-18 19:06:24 +00005759 const FileEntry *Entry = FileMgr.getFile(FileName);
Douglas Gregor4dd3e942011-08-19 02:29:29 +00005760 if (!Entry && FileName != "-") {
5761 ErrorStr = "file not found";
5762 return std::make_pair(static_cast<Module*>(0), false);
5763 }
5764
5765 // Check whether we already loaded this module, before
5766 Module *&ModuleEntry = Modules[Entry];
5767 bool NewModule = false;
5768 if (!ModuleEntry) {
5769 // Allocate a new module.
5770 Module *New = new Module(Type);
5771 New->FileName = FileName.str();
5772 Chain.push_back(New);
5773 NewModule = true;
5774 ModuleEntry = New;
5775
5776 // Load the contents of the module
5777 if (llvm::MemoryBuffer *Buffer = lookupBuffer(FileName)) {
5778 // The buffer was already provided for us.
5779 assert(Buffer && "Passed null buffer");
5780 New->Buffer.reset(Buffer);
5781 } else {
5782 // Open the AST file.
5783 llvm::error_code ec;
5784 if (FileName == "-") {
5785 ec = llvm::MemoryBuffer::getSTDIN(New->Buffer);
5786 if (ec)
5787 ErrorStr = ec.message();
5788 } else
5789 New->Buffer.reset(FileMgr.getBufferForFile(FileName, &ErrorStr));
5790
5791 if (!New->Buffer)
5792 return std::make_pair(static_cast<Module*>(0), false);
5793 }
5794
5795 // Initialize the stream
5796 New->StreamFile.init((const unsigned char *)New->Buffer->getBufferStart(),
5797 (const unsigned char *)New->Buffer->getBufferEnd()); }
Jonathan D. Turnerb2b08232011-07-26 18:21:30 +00005798
Douglas Gregordf0c1512011-08-18 04:12:04 +00005799 if (ImportedBy) {
Douglas Gregor4dd3e942011-08-19 02:29:29 +00005800 ModuleEntry->ImportedBy.insert(ImportedBy);
5801 ImportedBy->Imports.insert(ModuleEntry);
Douglas Gregordf0c1512011-08-18 04:12:04 +00005802 } else {
Douglas Gregor4dd3e942011-08-19 02:29:29 +00005803 ModuleEntry->DirectlyImported = true;
Jonathan D. Turner10d52012011-07-29 18:09:09 +00005804 }
5805
Douglas Gregor4dd3e942011-08-19 02:29:29 +00005806 return std::make_pair(ModuleEntry, NewModule);
Jonathan D. Turnerb2b08232011-07-26 18:21:30 +00005807}
5808
Jonathan D. Turnerdb1c9e32011-08-02 17:40:32 +00005809void ModuleManager::addInMemoryBuffer(StringRef FileName,
5810 llvm::MemoryBuffer *Buffer) {
5811
5812 const FileEntry *Entry = FileMgr.getVirtualFile(FileName,
5813 Buffer->getBufferSize(), 0);
5814 InMemoryBuffers[Entry] = Buffer;
5815}
Jonathan D. Turnerb2b08232011-07-26 18:21:30 +00005816
Jonathan D. Turnerecc27402011-07-28 17:20:23 +00005817ModuleManager::ModuleManager(const FileSystemOptions &FSO) : FileMgr(FSO) { }
5818
Jonathan D. Turnerb2b08232011-07-26 18:21:30 +00005819ModuleManager::~ModuleManager() {
5820 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
5821 delete Chain[e - i - 1];
5822}
Douglas Gregorab443b92011-08-20 04:39:52 +00005823
5824void ModuleManager::visit(bool (*Visitor)(Module &M, void *UserData),
5825 void *UserData) {
5826 unsigned N = size();
5827
5828 // Record the number of incoming edges for each module. When we
5829 // encounter a module with no incoming edges, push it into the queue
5830 // to seed the queue.
5831 SmallVector<Module *, 4> Queue;
5832 Queue.reserve(N);
5833 llvm::DenseMap<Module *, unsigned> UnusedIncomingEdges;
5834 for (ModuleIterator M = begin(), MEnd = end(); M != MEnd; ++M) {
5835 if (unsigned Size = (*M)->ImportedBy.size())
5836 UnusedIncomingEdges[*M] = Size;
5837 else
5838 Queue.push_back(*M);
5839 }
5840
5841 llvm::SmallPtrSet<Module *, 4> Skipped;
5842 unsigned QueueStart = 0;
5843 while (QueueStart < Queue.size()) {
5844 Module *CurrentModule = Queue[QueueStart++];
5845
5846 // Check whether this module should be skipped.
5847 if (Skipped.count(CurrentModule))
5848 continue;
5849
5850 if (Visitor(*CurrentModule, UserData)) {
5851 // The visitor has requested that cut off visitation of any
5852 // module that the current module depends on. To indicate this
5853 // behavior, we mark all of the reachable modules as having N
5854 // incoming edges (which is impossible otherwise).
5855 SmallVector<Module *, 4> Stack;
5856 Stack.push_back(CurrentModule);
5857 Skipped.insert(CurrentModule);
5858 while (!Stack.empty()) {
5859 Module *NextModule = Stack.back();
5860 Stack.pop_back();
5861
5862 // For any module that this module depends on, push it on the
5863 // stack (if it hasn't already been marked as visited).
5864 for (llvm::SetVector<Module *>::iterator
5865 M = NextModule->Imports.begin(),
5866 MEnd = NextModule->Imports.end();
5867 M != MEnd; ++M) {
5868 if (Skipped.insert(*M))
5869 Stack.push_back(*M);
5870 }
5871 }
5872 continue;
5873 }
5874
5875 // For any module that this module depends on, push it on the
5876 // stack (if it hasn't already been marked as visited).
5877 for (llvm::SetVector<Module *>::iterator M = CurrentModule->Imports.begin(),
5878 MEnd = CurrentModule->Imports.end();
5879 M != MEnd; ++M) {
5880
5881 // Remove our current module as an impediment to visiting the
5882 // module we depend on. If we were the last unvisited module
5883 // that depends on this particular module, push it into the
5884 // queue to be visited.
5885 unsigned &NumUnusedEdges = UnusedIncomingEdges[*M];
5886 if (NumUnusedEdges && (--NumUnusedEdges == 0))
5887 Queue.push_back(*M);
5888 }
5889 }
5890}