blob: 0a2992baa57a70f449609dacb9ae573a9f4cc266 [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 Gregor1257f972011-08-24 21:27:34 +00004267namespace {
4268 class FindExternalLexicalDeclsVisitor {
4269 ASTReader &Reader;
4270 const DeclContext *DC;
4271 bool (*isKindWeWant)(Decl::Kind);
4272 SmallVectorImpl<Decl*> &Decls;
4273 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
4274
4275 public:
4276 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
4277 bool (*isKindWeWant)(Decl::Kind),
4278 SmallVectorImpl<Decl*> &Decls)
4279 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
4280 {
4281 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
4282 PredefsVisited[I] = false;
4283 }
4284
4285 static bool visit(Module &M, bool Preorder, void *UserData) {
4286 if (Preorder)
4287 return false;
4288
4289 FindExternalLexicalDeclsVisitor *This
4290 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
4291
4292 Module::DeclContextInfosMap::iterator Info
4293 = M.DeclContextInfos.find(This->DC);
4294 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
4295 return false;
4296
4297 // Load all of the declaration IDs
4298 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
4299 *IDE = ID + Info->second.NumLexicalDecls;
4300 ID != IDE; ++ID) {
4301 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
4302 continue;
4303
4304 // Don't add predefined declarations to the lexical context more
4305 // than once.
4306 if (ID->second < NUM_PREDEF_DECL_IDS) {
4307 if (This->PredefsVisited[ID->second])
4308 continue;
4309
4310 This->PredefsVisited[ID->second] = true;
4311 }
4312
4313 Decl *D = This->Reader.GetLocalDecl(M, ID->second);
4314 assert(D && "Null decl in lexical decls");
4315 This->Decls.push_back(D);
4316 }
4317
4318 return false;
4319 }
4320 };
4321}
4322
Douglas Gregor3d0adb32011-07-15 21:46:17 +00004323ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00004324 bool (*isKindWeWant)(Decl::Kind),
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004325 SmallVectorImpl<Decl*> &Decls) {
Douglas Gregor94619c82011-08-24 19:03:07 +00004326 // There might be lexical decls in multiple modules, for the TU at
Douglas Gregor1257f972011-08-24 21:27:34 +00004327 // least. Walk all of the modules in the order they were loaded.
4328 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
4329 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00004330 ++NumLexicalDeclContextsRead;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00004331 return ELR_Success;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004332}
4333
Douglas Gregor94619c82011-08-24 19:03:07 +00004334namespace {
4335 /// \brief Module visitor used to perform name lookup into a
4336 /// declaration context.
4337 class DeclContextNameLookupVisitor {
4338 ASTReader &Reader;
4339 const DeclContext *DC;
4340 DeclarationName Name;
4341 SmallVectorImpl<NamedDecl *> &Decls;
4342
4343 public:
4344 DeclContextNameLookupVisitor(ASTReader &Reader,
4345 const DeclContext *DC, DeclarationName Name,
4346 SmallVectorImpl<NamedDecl *> &Decls)
4347 : Reader(Reader), DC(DC), Name(Name), Decls(Decls) { }
4348
4349 static bool visit(Module &M, void *UserData) {
4350 DeclContextNameLookupVisitor *This
4351 = static_cast<DeclContextNameLookupVisitor *>(UserData);
4352
4353 // Check whether we have any visible declaration information for
4354 // this context in this module.
4355 Module::DeclContextInfosMap::iterator Info
4356 = M.DeclContextInfos.find(This->DC);
4357 if (Info == M.DeclContextInfos.end() || !Info->second.NameLookupTableData)
4358 return false;
4359
4360 // Look for this name within this module.
4361 ASTDeclContextNameLookupTable *LookupTable =
4362 (ASTDeclContextNameLookupTable*)Info->second.NameLookupTableData;
4363 ASTDeclContextNameLookupTable::iterator Pos
4364 = LookupTable->find(This->Name);
4365 if (Pos == LookupTable->end())
4366 return false;
4367
4368 bool FoundAnything = false;
4369 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
4370 for (; Data.first != Data.second; ++Data.first) {
4371 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
4372 if (!ND)
4373 continue;
4374
4375 if (ND->getDeclName() != This->Name) {
4376 assert(!This->Name.getCXXNameType().isNull() &&
4377 "Name mismatch without a type");
4378 continue;
4379 }
4380
4381 // Record this declaration.
4382 FoundAnything = true;
4383 This->Decls.push_back(ND);
4384 }
4385
4386 return FoundAnything;
4387 }
4388 };
4389}
4390
John McCall75b960e2010-06-01 09:23:16 +00004391DeclContext::lookup_result
Sebastian Redl2c499f62010-08-18 23:56:43 +00004392ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall75b960e2010-06-01 09:23:16 +00004393 DeclarationName Name) {
Mike Stump11289f42009-09-09 15:08:12 +00004394 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004395 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004396 if (!Name)
4397 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
4398 DeclContext::lookup_iterator(0));
Ted Kremenek1ff615c2010-03-18 00:56:54 +00004399
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004400 SmallVector<NamedDecl *, 64> Decls;
Douglas Gregor94619c82011-08-24 19:03:07 +00004401 DeclContextNameLookupVisitor Visitor(*this, DC, Name, Decls);
4402 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00004403 ++NumVisibleDeclContextsRead;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004404 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall75b960e2010-06-01 09:23:16 +00004405 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004406}
4407
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00004408void ASTReader::MaterializeVisibleDecls(const DeclContext *DC) {
4409 assert(DC->hasExternalVisibleStorage() &&
4410 "DeclContext has no visible decls in storage");
4411
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004412 SmallVector<NamedDecl *, 64> Decls;
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00004413 // There might be visible decls in multiple parts of the chain, for the TU
4414 // and namespaces.
Douglas Gregor94619c82011-08-24 19:03:07 +00004415 // There might be lexical decls in multiple modules, for the TU at
4416 // least.
4417 // FIXME: We might want a faster way to zero
4418 // FIXME: Going backwards through the chain does the right thing for
4419 // chained PCH; for modules, it isn't clear what the right thing is.
4420 for (ModuleReverseIterator M = ModuleMgr.rbegin(), MEnd = ModuleMgr.rend();
4421 M != MEnd; ++M) {
4422 Module::DeclContextInfosMap::iterator Info
4423 = (*M)->DeclContextInfos.find(DC);
4424 if (Info == (*M)->DeclContextInfos.end() || !Info->second.LexicalDecls)
4425 continue;
4426
4427 if (!Info->second.NameLookupTableData)
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00004428 continue;
4429
4430 ASTDeclContextNameLookupTable *LookupTable =
Douglas Gregor94619c82011-08-24 19:03:07 +00004431 (ASTDeclContextNameLookupTable*)Info->second.NameLookupTableData;
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00004432 for (ASTDeclContextNameLookupTable::item_iterator
4433 ItemI = LookupTable->item_begin(),
4434 ItemEnd = LookupTable->item_end() ; ItemI != ItemEnd; ++ItemI) {
4435 ASTDeclContextNameLookupTable::item_iterator::value_type Val
4436 = *ItemI;
4437 ASTDeclContextNameLookupTrait::data_type Data = Val.second;
4438 Decls.clear();
Douglas Gregor94619c82011-08-24 19:03:07 +00004439 for (; Data.first != Data.second; ++Data.first) {
4440 if (NamedDecl *ND = GetLocalDeclAs<NamedDecl>(**M, *Data.first))
4441 Decls.push_back(ND);
4442 }
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00004443 MaterializeVisibleDeclsForName(DC, Val.first, Decls);
4444 }
4445 }
4446}
4447
Sebastian Redl2c499f62010-08-18 23:56:43 +00004448void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004449 assert(Consumer);
4450 while (!InterestingDecls.empty()) {
4451 DeclGroupRef DG(InterestingDecls.front());
4452 InterestingDecls.pop_front();
Sebastian Redleaa4ade2010-08-11 18:52:41 +00004453 Consumer->HandleInterestingDecl(DG);
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004454 }
4455}
4456
Sebastian Redl2c499f62010-08-18 23:56:43 +00004457void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregorb985eeb2009-04-22 19:09:20 +00004458 this->Consumer = Consumer;
4459
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00004460 if (!Consumer)
4461 return;
4462
4463 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004464 // Force deserialization of this decl, which will cause it to be queued for
4465 // passing to the consumer.
Daniel Dunbar865c2a72009-09-17 03:06:44 +00004466 GetDecl(ExternalDefinitions[I]);
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00004467 }
Douglas Gregorf005eac2009-04-25 00:41:30 +00004468
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004469 PassInterestingDeclsToConsumer();
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00004470}
4471
Sebastian Redl2c499f62010-08-18 23:56:43 +00004472void ASTReader::PrintStats() {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004473 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004474
Mike Stump11289f42009-09-09 15:08:12 +00004475 unsigned NumTypesLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00004476 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall8ccfcb52009-09-24 19:53:00 +00004477 QualType());
Douglas Gregor0e149972009-04-25 19:10:14 +00004478 unsigned NumDeclsLoaded
4479 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
4480 (Decl *)0);
4481 unsigned NumIdentifiersLoaded
4482 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
4483 IdentifiersLoaded.end(),
4484 (IdentifierInfo *)0);
Mike Stump11289f42009-09-09 15:08:12 +00004485 unsigned NumSelectorsLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00004486 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
4487 SelectorsLoaded.end(),
4488 Selector());
Douglas Gregorc3b1dd12009-04-13 20:50:16 +00004489
Douglas Gregorc5046832009-04-27 18:38:38 +00004490 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
4491 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor49bf76b2011-07-21 18:46:38 +00004492 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
Douglas Gregor258ae542009-04-27 06:38:32 +00004493 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
4494 NumSLocEntriesRead, TotalNumSLocEntries,
4495 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor745ed142009-04-25 18:35:21 +00004496 if (!TypesLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00004497 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00004498 NumTypesLoaded, (unsigned)TypesLoaded.size(),
4499 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
4500 if (!DeclsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00004501 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00004502 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
4503 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor0e149972009-04-25 19:10:14 +00004504 if (!IdentifiersLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00004505 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor0e149972009-04-25 19:10:14 +00004506 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
4507 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redlada023c2010-08-04 20:40:17 +00004508 if (!SelectorsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00004509 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redlada023c2010-08-04 20:40:17 +00004510 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
4511 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor95c13f52009-04-25 17:48:32 +00004512 if (TotalNumStatements)
4513 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
4514 NumStatementsRead, TotalNumStatements,
4515 ((float)NumStatementsRead/TotalNumStatements * 100));
4516 if (TotalNumMacros)
4517 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
4518 NumMacrosRead, TotalNumMacros,
4519 ((float)NumMacrosRead/TotalNumMacros * 100));
4520 if (TotalLexicalDeclContexts)
4521 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
4522 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
4523 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
4524 * 100));
4525 if (TotalVisibleDeclContexts)
4526 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
4527 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
4528 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
4529 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004530 if (TotalNumMethodPoolEntries) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004531 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004532 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
4533 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor95c13f52009-04-25 17:48:32 +00004534 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004535 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor95c13f52009-04-25 17:48:32 +00004536 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004537 std::fprintf(stderr, "\n");
Douglas Gregor204b8712011-07-21 19:50:14 +00004538 dump();
4539 std::fprintf(stderr, "\n");
4540}
4541
Douglas Gregora6895d82011-07-22 16:00:58 +00004542template<typename Key, typename Module, unsigned InitialCapacity>
Douglas Gregor204b8712011-07-21 19:50:14 +00004543static void
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004544dumpModuleIDMap(StringRef Name,
Douglas Gregora6895d82011-07-22 16:00:58 +00004545 const ContinuousRangeMap<Key, Module *,
Douglas Gregor204b8712011-07-21 19:50:14 +00004546 InitialCapacity> &Map) {
4547 if (Map.begin() == Map.end())
4548 return;
4549
Douglas Gregora6895d82011-07-22 16:00:58 +00004550 typedef ContinuousRangeMap<Key, Module *, InitialCapacity> MapType;
Douglas Gregor204b8712011-07-21 19:50:14 +00004551 llvm::errs() << Name << ":\n";
4552 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
4553 I != IEnd; ++I) {
4554 llvm::errs() << " " << I->first << " -> " << I->second->FileName
4555 << "\n";
4556 }
4557}
4558
Douglas Gregor204b8712011-07-21 19:50:14 +00004559void ASTReader::dump() {
Douglas Gregor1cc9c062011-08-02 11:12:41 +00004560 llvm::errs() << "*** PCH/Module Remappings:\n";
Douglas Gregord32f0352011-07-22 06:10:01 +00004561 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
Douglas Gregor204b8712011-07-21 19:50:14 +00004562 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
Douglas Gregor8ab4ea82011-07-29 00:21:44 +00004563 dumpModuleIDMap("Global type map", GlobalTypeMap);
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00004564 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00004565 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
4566 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
4567 dumpModuleIDMap("Global macro definition map", GlobalMacroDefinitionMap);
4568 dumpModuleIDMap("Global preprocessed entity map",
4569 GlobalPreprocessedEntityMap);
Douglas Gregor1cc9c062011-08-02 11:12:41 +00004570
4571 llvm::errs() << "\n*** PCH/Modules Loaded:";
4572 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
4573 MEnd = ModuleMgr.end();
4574 M != MEnd; ++M)
4575 (*M)->dump();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004576}
4577
Ted Kremenek5e1ed7b2011-04-28 23:46:20 +00004578/// Return the amount of memory used by memory buffers, breaking down
4579/// by heap-backed versus mmap'ed memory.
4580void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00004581 for (ModuleConstIterator I = ModuleMgr.begin(),
4582 E = ModuleMgr.end(); I != E; ++I) {
4583 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
Ted Kremenek5e1ed7b2011-04-28 23:46:20 +00004584 size_t bytes = buf->getBufferSize();
4585 switch (buf->getBufferKind()) {
4586 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
4587 sizes.malloc_bytes += bytes;
4588 break;
4589 case llvm::MemoryBuffer::MemoryBuffer_MMap:
4590 sizes.mmap_bytes += bytes;
4591 break;
4592 }
4593 }
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00004594 }
Ted Kremenek5e1ed7b2011-04-28 23:46:20 +00004595}
4596
Sebastian Redl2c499f62010-08-18 23:56:43 +00004597void ASTReader::InitializeSema(Sema &S) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00004598 SemaObj = &S;
Douglas Gregorc78d3462009-04-24 21:10:55 +00004599 S.ExternalSource = this;
4600
Douglas Gregor7cd60f72009-04-22 21:15:06 +00004601 // Makes sure any declarations that were deserialized "too early"
4602 // still get added to the identifier's declaration chains.
Douglas Gregor2fb99df2010-09-24 23:29:12 +00004603 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
4604 if (SemaObj->TUScope)
John McCall48871652010-08-21 09:40:31 +00004605 SemaObj->TUScope->AddDecl(PreloadedDecls[I]);
Douglas Gregor2fb99df2010-09-24 23:29:12 +00004606
4607 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregora868bbd2009-04-21 22:25:48 +00004608 }
Douglas Gregor7cd60f72009-04-22 21:15:06 +00004609 PreloadedDecls.clear();
Douglas Gregord4df8652009-04-22 22:02:47 +00004610
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004611 // Load the offsets of the declarations that Sema references.
4612 // They will be lazily deserialized when needed.
4613 if (!SemaDeclRefs.empty()) {
4614 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
Douglas Gregorb0f3ae62011-07-28 00:57:24 +00004615 if (!SemaObj->StdNamespace)
4616 SemaObj->StdNamespace = SemaDeclRefs[0];
4617 if (!SemaObj->StdBadAlloc)
4618 SemaObj->StdBadAlloc = SemaDeclRefs[1];
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004619 }
4620
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004621 if (!FPPragmaOptions.empty()) {
4622 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
4623 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
4624 }
4625
4626 if (!OpenCLExtensions.empty()) {
4627 unsigned I = 0;
4628#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
4629#include "clang/Basic/OpenCLExtensions.def"
4630
4631 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
4632 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00004633}
4634
Douglas Gregorab443b92011-08-20 04:39:52 +00004635namespace {
4636 /// \brief Visitor class used to look up identifirs in
4637 class IdentifierLookupVisitor {
4638 StringRef Name;
4639 IdentifierInfo *Found;
4640 public:
4641 explicit IdentifierLookupVisitor(StringRef Name) : Name(Name), Found() { }
Douglas Gregora868bbd2009-04-21 22:25:48 +00004642
Douglas Gregorab443b92011-08-20 04:39:52 +00004643 static bool visit(Module &M, void *UserData) {
4644 IdentifierLookupVisitor *This
4645 = static_cast<IdentifierLookupVisitor *>(UserData);
4646
4647 ASTIdentifierLookupTable *IdTable
4648 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
4649 if (!IdTable)
4650 return false;
4651
4652 std::pair<const char*, unsigned> Key(This->Name.begin(),
4653 This->Name.size());
4654 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
4655 if (Pos == IdTable->end())
4656 return false;
4657
4658 // Dereferencing the iterator has the effect of building the
4659 // IdentifierInfo node and populating it with the various
4660 // declarations it needs.
4661 This->Found = *Pos;
4662 return true;
4663 }
4664
4665 // \brief Retrieve the identifier info found within the module
4666 // files.
4667 IdentifierInfo *getIdentifierInfo() const { return Found; }
4668 };
4669}
4670
4671IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Douglas Gregorab443b92011-08-20 04:39:52 +00004672 IdentifierLookupVisitor Visitor(StringRef(NameStart, NameEnd - NameStart));
4673 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
4674 return Visitor.getIdentifierInfo();
Douglas Gregora868bbd2009-04-21 22:25:48 +00004675}
4676
Douglas Gregor57756ea2010-10-14 22:11:03 +00004677namespace clang {
4678 /// \brief An identifier-lookup iterator that enumerates all of the
4679 /// identifiers stored within a set of AST files.
4680 class ASTIdentifierIterator : public IdentifierIterator {
4681 /// \brief The AST reader whose identifiers are being enumerated.
4682 const ASTReader &Reader;
4683
4684 /// \brief The current index into the chain of AST files stored in
4685 /// the AST reader.
4686 unsigned Index;
4687
4688 /// \brief The current position within the identifier lookup table
4689 /// of the current AST file.
4690 ASTIdentifierLookupTable::key_iterator Current;
4691
4692 /// \brief The end position within the identifier lookup table of
4693 /// the current AST file.
4694 ASTIdentifierLookupTable::key_iterator End;
4695
4696 public:
4697 explicit ASTIdentifierIterator(const ASTReader &Reader);
4698
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004699 virtual StringRef Next();
Douglas Gregor57756ea2010-10-14 22:11:03 +00004700 };
4701}
4702
4703ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00004704 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
Douglas Gregor57756ea2010-10-14 22:11:03 +00004705 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00004706 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
Douglas Gregor57756ea2010-10-14 22:11:03 +00004707 Current = IdTable->key_begin();
4708 End = IdTable->key_end();
4709}
4710
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004711StringRef ASTIdentifierIterator::Next() {
Douglas Gregor57756ea2010-10-14 22:11:03 +00004712 while (Current == End) {
4713 // If we have exhausted all of our AST files, we're done.
4714 if (Index == 0)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004715 return StringRef();
Douglas Gregor57756ea2010-10-14 22:11:03 +00004716
4717 --Index;
4718 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00004719 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
4720 IdentifierLookupTable;
Douglas Gregor57756ea2010-10-14 22:11:03 +00004721 Current = IdTable->key_begin();
4722 End = IdTable->key_end();
4723 }
4724
4725 // We have any identifiers remaining in the current AST file; return
4726 // the next one.
4727 std::pair<const char*, unsigned> Key = *Current;
4728 ++Current;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004729 return StringRef(Key.first, Key.second);
Douglas Gregor57756ea2010-10-14 22:11:03 +00004730}
4731
4732IdentifierIterator *ASTReader::getIdentifiers() const {
4733 return new ASTIdentifierIterator(*this);
4734}
4735
Mike Stump11289f42009-09-09 15:08:12 +00004736std::pair<ObjCMethodList, ObjCMethodList>
Sebastian Redl2c499f62010-08-18 23:56:43 +00004737ASTReader::ReadMethodPool(Selector Sel) {
Sebastian Redlada023c2010-08-04 20:40:17 +00004738 // Find this selector in a hash table. We want to find the most recent entry.
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00004739 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
4740 Module &F = *(*I);
Sebastian Redlada023c2010-08-04 20:40:17 +00004741 if (!F.SelectorLookupTable)
4742 continue;
Douglas Gregorc78d3462009-04-24 21:10:55 +00004743
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004744 ASTSelectorLookupTable *PoolTable
4745 = (ASTSelectorLookupTable*)F.SelectorLookupTable;
4746 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
Sebastian Redlada023c2010-08-04 20:40:17 +00004747 if (Pos != PoolTable->end()) {
4748 ++NumSelectorsRead;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004749 // FIXME: Not quite happy with the statistics here. We probably should
4750 // disable this tracking when called via LoadSelector.
4751 // Also, should entries without methods count as misses?
4752 ++NumMethodPoolEntriesRead;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004753 ASTSelectorLookupTrait::data_type Data = *Pos;
Sebastian Redlada023c2010-08-04 20:40:17 +00004754 if (DeserializationListener)
4755 DeserializationListener->SelectorRead(Data.ID, Sel);
4756 return std::make_pair(Data.Instance, Data.Factory);
4757 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00004758 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00004759
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004760 ++NumMethodPoolMisses;
Sebastian Redlada023c2010-08-04 20:40:17 +00004761 return std::pair<ObjCMethodList, ObjCMethodList>();
Douglas Gregorc78d3462009-04-24 21:10:55 +00004762}
4763
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004764void ASTReader::ReadKnownNamespaces(
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004765 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004766 Namespaces.clear();
4767
4768 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
4769 if (NamespaceDecl *Namespace
4770 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
4771 Namespaces.push_back(Namespace);
4772 }
4773}
4774
Douglas Gregoreb08bd42011-07-27 20:58:46 +00004775void ASTReader::ReadTentativeDefinitions(
4776 SmallVectorImpl<VarDecl *> &TentativeDefs) {
4777 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
4778 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
4779 if (Var)
4780 TentativeDefs.push_back(Var);
4781 }
4782 TentativeDefinitions.clear();
4783}
4784
Douglas Gregora94a1542011-07-27 21:45:57 +00004785void ASTReader::ReadUnusedFileScopedDecls(
4786 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
4787 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
4788 DeclaratorDecl *D
4789 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
4790 if (D)
4791 Decls.push_back(D);
4792 }
4793 UnusedFileScopedDecls.clear();
4794}
4795
Douglas Gregorbae31202011-07-27 21:57:17 +00004796void ASTReader::ReadDelegatingConstructors(
4797 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
4798 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
4799 CXXConstructorDecl *D
4800 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
4801 if (D)
4802 Decls.push_back(D);
4803 }
4804 DelegatingCtorDecls.clear();
4805}
4806
Douglas Gregorb7098a32011-07-28 00:39:29 +00004807void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
4808 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
4809 TypedefNameDecl *D
4810 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
4811 if (D)
4812 Decls.push_back(D);
4813 }
4814 ExtVectorDecls.clear();
4815}
4816
Douglas Gregor32002192011-07-28 00:53:40 +00004817void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
4818 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
4819 CXXRecordDecl *D
4820 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
4821 if (D)
4822 Decls.push_back(D);
4823 }
4824 DynamicClasses.clear();
4825}
4826
Douglas Gregordc5c9582011-07-28 14:20:37 +00004827void
4828ASTReader::ReadLocallyScopedExternalDecls(SmallVectorImpl<NamedDecl *> &Decls) {
4829 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
4830 NamedDecl *D
4831 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
4832 if (D)
4833 Decls.push_back(D);
4834 }
4835 LocallyScopedExternalDecls.clear();
4836}
4837
Douglas Gregor72e357f2011-07-28 14:54:22 +00004838void ASTReader::ReadReferencedSelectors(
4839 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
4840 if (ReferencedSelectorsData.empty())
4841 return;
4842
4843 // If there are @selector references added them to its pool. This is for
4844 // implementation of -Wselector.
4845 unsigned int DataSize = ReferencedSelectorsData.size()-1;
4846 unsigned I = 0;
4847 while (I < DataSize) {
4848 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
4849 SourceLocation SelLoc
4850 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
4851 Sels.push_back(std::make_pair(Sel, SelLoc));
4852 }
4853 ReferencedSelectorsData.clear();
4854}
4855
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00004856void ASTReader::ReadWeakUndeclaredIdentifiers(
4857 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
4858 if (WeakUndeclaredIdentifiers.empty())
4859 return;
4860
4861 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
4862 IdentifierInfo *WeakId
4863 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
4864 IdentifierInfo *AliasId
4865 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
4866 SourceLocation Loc
4867 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
4868 bool Used = WeakUndeclaredIdentifiers[I++];
4869 WeakInfo WI(AliasId, Loc);
4870 WI.setUsed(Used);
4871 WeakIDs.push_back(std::make_pair(WeakId, WI));
4872 }
4873 WeakUndeclaredIdentifiers.clear();
4874}
4875
Douglas Gregor4daf6a32011-07-28 19:11:31 +00004876void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
4877 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
4878 ExternalVTableUse VT;
4879 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
4880 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
4881 VT.DefinitionRequired = VTableUses[Idx++];
4882 VTables.push_back(VT);
4883 }
4884
4885 VTableUses.clear();
4886}
4887
Douglas Gregore39f97c2011-07-28 19:49:54 +00004888void ASTReader::ReadPendingInstantiations(
4889 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
4890 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
4891 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
4892 SourceLocation Loc
4893 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
4894 Pending.push_back(std::make_pair(D, Loc));
4895 }
4896 PendingInstantiations.clear();
4897}
4898
Sebastian Redl2c499f62010-08-18 23:56:43 +00004899void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00004900 // It would be complicated to avoid reading the methods anyway. So don't.
4901 ReadMethodPool(Sel);
4902}
4903
Douglas Gregora3e41532011-07-28 20:55:49 +00004904void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00004905 assert(ID && "Non-zero identifier ID required");
Douglas Gregor6f00bf82009-04-28 21:53:25 +00004906 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor0e149972009-04-25 19:10:14 +00004907 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlff4a2952010-07-23 23:49:55 +00004908 if (DeserializationListener)
4909 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregora868bbd2009-04-21 22:25:48 +00004910}
4911
Douglas Gregor1342e842009-07-06 18:54:52 +00004912/// \brief Set the globally-visible declarations associated with the given
4913/// identifier.
4914///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004915/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump11289f42009-09-09 15:08:12 +00004916/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregor1342e842009-07-06 18:54:52 +00004917/// them.
4918///
4919/// \param II an IdentifierInfo that refers to one or more globally-visible
4920/// declarations.
4921///
4922/// \param DeclIDs the set of declaration IDs with the name @p II that are
4923/// visible at global scope.
4924///
4925/// \param Nonrecursive should be true to indicate that the caller knows that
4926/// this call is non-recursive, and therefore the globally-visible declarations
4927/// will not be placed onto the pending queue.
Mike Stump11289f42009-09-09 15:08:12 +00004928void
Sebastian Redl2c499f62010-08-18 23:56:43 +00004929ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004930 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregor1342e842009-07-06 18:54:52 +00004931 bool Nonrecursive) {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004932 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregor1342e842009-07-06 18:54:52 +00004933 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
4934 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
4935 PII.II = II;
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00004936 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregor1342e842009-07-06 18:54:52 +00004937 return;
4938 }
Mike Stump11289f42009-09-09 15:08:12 +00004939
Douglas Gregor1342e842009-07-06 18:54:52 +00004940 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
4941 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
4942 if (SemaObj) {
Douglas Gregor6fd55e02010-08-13 03:15:25 +00004943 if (SemaObj->TUScope) {
4944 // Introduce this declaration into the translation-unit scope
4945 // and add it to the declaration chain for this identifier, so
4946 // that (unqualified) name lookup will find it.
John McCall48871652010-08-21 09:40:31 +00004947 SemaObj->TUScope->AddDecl(D);
Douglas Gregor6fd55e02010-08-13 03:15:25 +00004948 }
Douglas Gregor2fb99df2010-09-24 23:29:12 +00004949 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
Douglas Gregor1342e842009-07-06 18:54:52 +00004950 } else {
4951 // Queue this declaration so that it will be added to the
4952 // translation unit scope and identifier's declaration chain
4953 // once a Sema object is known.
4954 PreloadedDecls.push_back(D);
4955 }
4956 }
4957}
4958
Douglas Gregora3e41532011-07-28 20:55:49 +00004959IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004960 if (ID == 0)
4961 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00004962
Sebastian Redlc713b962010-07-21 00:46:22 +00004963 if (IdentifiersLoaded.empty()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004964 Error("no identifier table in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004965 return 0;
4966 }
Mike Stump11289f42009-09-09 15:08:12 +00004967
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00004968 assert(PP && "Forgot to set Preprocessor ?");
Sebastian Redlc713b962010-07-21 00:46:22 +00004969 ID -= 1;
4970 if (!IdentifiersLoaded[ID]) {
Douglas Gregor19d26352011-07-20 00:59:32 +00004971 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
4972 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00004973 Module *M = I->second;
4974 unsigned Index = ID - M->BaseIdentifierID;
4975 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
Douglas Gregor5287b4e2009-04-25 21:04:17 +00004976
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004977 // All of the strings in the AST file are preceded by a 16-bit length.
4978 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenekca42a512009-10-23 04:45:31 +00004979 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
4980 // unsigned integers. This is important to avoid integer overflow when
4981 // we cast them to 'unsigned'.
Ted Kremenek49c52322009-10-23 03:57:22 +00004982 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregorab4df582009-04-28 20:01:51 +00004983 unsigned StrLen = (((unsigned) StrLenPtr[0])
4984 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redlc713b962010-07-21 00:46:22 +00004985 IdentifiersLoaded[ID]
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004986 = &PP->getIdentifierTable().get(StringRef(Str, StrLen));
Sebastian Redlff4a2952010-07-23 23:49:55 +00004987 if (DeserializationListener)
4988 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004989 }
Mike Stump11289f42009-09-09 15:08:12 +00004990
Sebastian Redlc713b962010-07-21 00:46:22 +00004991 return IdentifiersLoaded[ID];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004992}
4993
Douglas Gregora3e41532011-07-28 20:55:49 +00004994IdentifierInfo *ASTReader::getLocalIdentifier(Module &M, unsigned LocalID) {
4995 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
4996}
4997
4998IdentifierID ASTReader::getGlobalIdentifierID(Module &M, unsigned LocalID) {
Douglas Gregor1ab036c2011-08-03 21:49:18 +00004999 if (LocalID < NUM_PREDEF_IDENT_IDS)
5000 return LocalID;
5001
5002 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5003 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
5004 assert(I != M.IdentifierRemap.end()
5005 && "Invalid index into identifier index remap");
5006
5007 return LocalID + I->second;
Douglas Gregora3e41532011-07-28 20:55:49 +00005008}
5009
Douglas Gregor925296b2011-07-19 16:10:42 +00005010bool ASTReader::ReadSLocEntry(int ID) {
Douglas Gregor49f754f2011-04-20 00:21:03 +00005011 return ReadSLocEntryRecord(ID) != Success;
Douglas Gregor258ae542009-04-27 06:38:32 +00005012}
5013
Douglas Gregor074fdc52011-07-28 21:16:51 +00005014Selector ASTReader::getLocalSelector(Module &M, unsigned LocalID) {
5015 return DecodeSelector(getGlobalSelectorID(M, LocalID));
5016}
5017
5018Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
Steve Naroff2ddea052009-04-23 10:39:46 +00005019 if (ID == 0)
5020 return Selector();
Mike Stump11289f42009-09-09 15:08:12 +00005021
Sebastian Redlada023c2010-08-04 20:40:17 +00005022 if (ID > SelectorsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00005023 Error("selector ID out of range in AST file");
Steve Naroff2ddea052009-04-23 10:39:46 +00005024 return Selector();
5025 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00005026
Sebastian Redlada023c2010-08-04 20:40:17 +00005027 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00005028 // Load this selector from the selector table.
Douglas Gregor2262d282011-07-20 01:10:58 +00005029 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
5030 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00005031 Module &M = *I->second;
5032 ASTSelectorLookupTrait Trait(*this, M);
Douglas Gregor8f364fb2011-08-03 23:28:44 +00005033 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
Douglas Gregor2262d282011-07-20 01:10:58 +00005034 SelectorsLoaded[ID - 1] =
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00005035 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
Douglas Gregor2262d282011-07-20 01:10:58 +00005036 if (DeserializationListener)
5037 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
Douglas Gregor95c13f52009-04-25 17:48:32 +00005038 }
5039
Sebastian Redlada023c2010-08-04 20:40:17 +00005040 return SelectorsLoaded[ID - 1];
Steve Naroff2ddea052009-04-23 10:39:46 +00005041}
5042
Douglas Gregor3f8f04f2011-07-28 14:41:43 +00005043Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
Douglas Gregord720daf2010-04-06 17:30:22 +00005044 return DecodeSelector(ID);
5045}
5046
Sebastian Redl2c499f62010-08-18 23:56:43 +00005047uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redlada023c2010-08-04 20:40:17 +00005048 // ID 0 (the null selector) is considered an external selector.
5049 return getTotalNumSelectors() + 1;
Douglas Gregord720daf2010-04-06 17:30:22 +00005050}
5051
Douglas Gregor8f364fb2011-08-03 23:28:44 +00005052serialization::SelectorID
5053ASTReader::getGlobalSelectorID(Module &M, unsigned LocalID) const {
5054 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
5055 return LocalID;
5056
5057 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5058 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
5059 assert(I != M.SelectorRemap.end()
5060 && "Invalid index into identifier index remap");
5061
5062 return LocalID + I->second;
Douglas Gregor3f8f04f2011-07-28 14:41:43 +00005063}
5064
Mike Stump11289f42009-09-09 15:08:12 +00005065DeclarationName
Douglas Gregora6895d82011-07-22 16:00:58 +00005066ASTReader::ReadDeclarationName(Module &F,
Douglas Gregor903b7e92011-07-22 00:38:23 +00005067 const RecordData &Record, unsigned &Idx) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005068 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
5069 switch (Kind) {
5070 case DeclarationName::Identifier:
Douglas Gregora3e41532011-07-28 20:55:49 +00005071 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005072
5073 case DeclarationName::ObjCZeroArgSelector:
5074 case DeclarationName::ObjCOneArgSelector:
5075 case DeclarationName::ObjCMultiArgSelector:
Douglas Gregor074fdc52011-07-28 21:16:51 +00005076 return DeclarationName(ReadSelector(F, Record, Idx));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005077
5078 case DeclarationName::CXXConstructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00005079 return Context->DeclarationNames.getCXXConstructorName(
Douglas Gregor903b7e92011-07-22 00:38:23 +00005080 Context->getCanonicalType(readType(F, Record, Idx)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005081
5082 case DeclarationName::CXXDestructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00005083 return Context->DeclarationNames.getCXXDestructorName(
Douglas Gregor903b7e92011-07-22 00:38:23 +00005084 Context->getCanonicalType(readType(F, Record, Idx)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005085
5086 case DeclarationName::CXXConversionFunctionName:
Chris Lattner8575daa2009-04-27 21:45:14 +00005087 return Context->DeclarationNames.getCXXConversionFunctionName(
Douglas Gregor903b7e92011-07-22 00:38:23 +00005088 Context->getCanonicalType(readType(F, Record, Idx)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005089
5090 case DeclarationName::CXXOperatorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00005091 return Context->DeclarationNames.getCXXOperatorName(
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005092 (OverloadedOperatorKind)Record[Idx++]);
5093
Alexis Hunt3d221f22009-11-29 07:34:05 +00005094 case DeclarationName::CXXLiteralOperatorName:
5095 return Context->DeclarationNames.getCXXLiteralOperatorName(
Douglas Gregora3e41532011-07-28 20:55:49 +00005096 GetIdentifierInfo(F, Record, Idx));
Alexis Hunt3d221f22009-11-29 07:34:05 +00005097
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005098 case DeclarationName::CXXUsingDirective:
5099 return DeclarationName::getUsingDirectiveName();
5100 }
5101
5102 // Required to silence GCC warning
5103 return DeclarationName();
5104}
Douglas Gregor55abb232009-04-10 20:39:37 +00005105
Douglas Gregora6895d82011-07-22 16:00:58 +00005106void ASTReader::ReadDeclarationNameLoc(Module &F,
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005107 DeclarationNameLoc &DNLoc,
5108 DeclarationName Name,
5109 const RecordData &Record, unsigned &Idx) {
5110 switch (Name.getNameKind()) {
5111 case DeclarationName::CXXConstructorName:
5112 case DeclarationName::CXXDestructorName:
5113 case DeclarationName::CXXConversionFunctionName:
5114 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
5115 break;
5116
5117 case DeclarationName::CXXOperatorName:
5118 DNLoc.CXXOperatorName.BeginOpNameLoc
5119 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5120 DNLoc.CXXOperatorName.EndOpNameLoc
5121 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5122 break;
5123
5124 case DeclarationName::CXXLiteralOperatorName:
5125 DNLoc.CXXLiteralOperatorName.OpNameLoc
5126 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5127 break;
5128
5129 case DeclarationName::Identifier:
5130 case DeclarationName::ObjCZeroArgSelector:
5131 case DeclarationName::ObjCOneArgSelector:
5132 case DeclarationName::ObjCMultiArgSelector:
5133 case DeclarationName::CXXUsingDirective:
5134 break;
5135 }
5136}
5137
Douglas Gregora6895d82011-07-22 16:00:58 +00005138void ASTReader::ReadDeclarationNameInfo(Module &F,
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005139 DeclarationNameInfo &NameInfo,
5140 const RecordData &Record, unsigned &Idx) {
Douglas Gregor903b7e92011-07-22 00:38:23 +00005141 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005142 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
5143 DeclarationNameLoc DNLoc;
5144 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
5145 NameInfo.setInfo(DNLoc);
5146}
5147
Douglas Gregora6895d82011-07-22 16:00:58 +00005148void ASTReader::ReadQualifierInfo(Module &F, QualifierInfo &Info,
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005149 const RecordData &Record, unsigned &Idx) {
Douglas Gregor14454802011-02-25 02:25:35 +00005150 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005151 unsigned NumTPLists = Record[Idx++];
5152 Info.NumTemplParamLists = NumTPLists;
5153 if (NumTPLists) {
5154 Info.TemplParamLists = new (*Context) TemplateParameterList*[NumTPLists];
5155 for (unsigned i=0; i != NumTPLists; ++i)
5156 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
5157 }
5158}
5159
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005160TemplateName
Douglas Gregora6895d82011-07-22 16:00:58 +00005161ASTReader::ReadTemplateName(Module &F, const RecordData &Record,
Douglas Gregor5590be02011-01-15 06:45:20 +00005162 unsigned &Idx) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005163 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005164 switch (Kind) {
5165 case TemplateName::Template:
Douglas Gregor7fb09192011-07-21 22:35:25 +00005166 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005167
5168 case TemplateName::OverloadedTemplate: {
5169 unsigned size = Record[Idx++];
5170 UnresolvedSet<8> Decls;
5171 while (size--)
Douglas Gregor7fb09192011-07-21 22:35:25 +00005172 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005173
5174 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
5175 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005176
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005177 case TemplateName::QualifiedTemplate: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00005178 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005179 bool hasTemplKeyword = Record[Idx++];
Douglas Gregor7fb09192011-07-21 22:35:25 +00005180 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005181 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
5182 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005183
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005184 case TemplateName::DependentTemplate: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00005185 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005186 if (Record[Idx++]) // isIdentifier
5187 return Context->getDependentTemplateName(NNS,
Douglas Gregora3e41532011-07-28 20:55:49 +00005188 GetIdentifierInfo(F, Record,
5189 Idx));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005190 return Context->getDependentTemplateName(NNS,
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00005191 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005192 }
John McCalld9dfe3a2011-06-30 08:33:18 +00005193
5194 case TemplateName::SubstTemplateTemplateParm: {
5195 TemplateTemplateParmDecl *param
Douglas Gregor7fb09192011-07-21 22:35:25 +00005196 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
John McCalld9dfe3a2011-06-30 08:33:18 +00005197 if (!param) return TemplateName();
5198 TemplateName replacement = ReadTemplateName(F, Record, Idx);
5199 return Context->getSubstTemplateTemplateParm(param, replacement);
5200 }
Douglas Gregor5590be02011-01-15 06:45:20 +00005201
5202 case TemplateName::SubstTemplateTemplateParmPack: {
5203 TemplateTemplateParmDecl *Param
Douglas Gregor7fb09192011-07-21 22:35:25 +00005204 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
Douglas Gregor5590be02011-01-15 06:45:20 +00005205 if (!Param)
5206 return TemplateName();
5207
5208 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
5209 if (ArgPack.getKind() != TemplateArgument::Pack)
5210 return TemplateName();
5211
5212 return Context->getSubstTemplateTemplateParmPack(Param, ArgPack);
5213 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005214 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005215
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005216 assert(0 && "Unhandled template name kind!");
5217 return TemplateName();
5218}
5219
5220TemplateArgument
Douglas Gregora6895d82011-07-22 16:00:58 +00005221ASTReader::ReadTemplateArgument(Module &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00005222 const RecordData &Record, unsigned &Idx) {
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005223 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
5224 switch (Kind) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005225 case TemplateArgument::Null:
5226 return TemplateArgument();
5227 case TemplateArgument::Type:
Douglas Gregor903b7e92011-07-22 00:38:23 +00005228 return TemplateArgument(readType(F, Record, Idx));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005229 case TemplateArgument::Declaration:
Douglas Gregor7fb09192011-07-21 22:35:25 +00005230 return TemplateArgument(ReadDecl(F, Record, Idx));
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +00005231 case TemplateArgument::Integral: {
5232 llvm::APSInt Value = ReadAPSInt(Record, Idx);
Douglas Gregor903b7e92011-07-22 00:38:23 +00005233 QualType T = readType(F, Record, Idx);
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +00005234 return TemplateArgument(Value, T);
5235 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005236 case TemplateArgument::Template:
Douglas Gregor5590be02011-01-15 06:45:20 +00005237 return TemplateArgument(ReadTemplateName(F, Record, Idx));
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005238 case TemplateArgument::TemplateExpansion: {
Douglas Gregor5590be02011-01-15 06:45:20 +00005239 TemplateName Name = ReadTemplateName(F, Record, Idx);
Douglas Gregore1d60df2011-01-14 23:41:42 +00005240 llvm::Optional<unsigned> NumTemplateExpansions;
5241 if (unsigned NumExpansions = Record[Idx++])
5242 NumTemplateExpansions = NumExpansions - 1;
5243 return TemplateArgument(Name, NumTemplateExpansions);
Douglas Gregoreb29d182011-01-05 17:40:24 +00005244 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005245 case TemplateArgument::Expression:
Sebastian Redl2c373b92010-10-05 15:59:54 +00005246 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005247 case TemplateArgument::Pack: {
5248 unsigned NumArgs = Record[Idx++];
Douglas Gregor1ccc8412010-11-07 23:05:16 +00005249 TemplateArgument *Args = new (*Context) TemplateArgument[NumArgs];
5250 for (unsigned I = 0; I != NumArgs; ++I)
5251 Args[I] = ReadTemplateArgument(F, Record, Idx);
5252 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005253 }
5254 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005255
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00005256 assert(0 && "Unhandled template argument kind!");
5257 return TemplateArgument();
5258}
5259
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005260TemplateParameterList *
Douglas Gregora6895d82011-07-22 16:00:58 +00005261ASTReader::ReadTemplateParameterList(Module &F,
Sebastian Redl2c373b92010-10-05 15:59:54 +00005262 const RecordData &Record, unsigned &Idx) {
5263 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
5264 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
5265 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005266
5267 unsigned NumParams = Record[Idx++];
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005268 SmallVector<NamedDecl *, 16> Params;
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005269 Params.reserve(NumParams);
5270 while (NumParams--)
Douglas Gregor7fb09192011-07-21 22:35:25 +00005271 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005272
5273 TemplateParameterList* TemplateParams =
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005274 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
5275 Params.data(), Params.size(), RAngleLoc);
5276 return TemplateParams;
5277}
5278
5279void
Sebastian Redl2c499f62010-08-18 23:56:43 +00005280ASTReader::
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005281ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs,
Douglas Gregora6895d82011-07-22 16:00:58 +00005282 Module &F, const RecordData &Record,
Sebastian Redl2c373b92010-10-05 15:59:54 +00005283 unsigned &Idx) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005284 unsigned NumTemplateArgs = Record[Idx++];
5285 TemplArgs.reserve(NumTemplateArgs);
5286 while (NumTemplateArgs--)
Sebastian Redl2c373b92010-10-05 15:59:54 +00005287 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005288}
5289
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005290/// \brief Read a UnresolvedSet structure.
Douglas Gregora6895d82011-07-22 16:00:58 +00005291void ASTReader::ReadUnresolvedSet(Module &F, UnresolvedSetImpl &Set,
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005292 const RecordData &Record, unsigned &Idx) {
5293 unsigned NumDecls = Record[Idx++];
5294 while (NumDecls--) {
Douglas Gregor7fb09192011-07-21 22:35:25 +00005295 NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx);
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005296 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
5297 Set.addDecl(D, AS);
5298 }
5299}
5300
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005301CXXBaseSpecifier
Douglas Gregora6895d82011-07-22 16:00:58 +00005302ASTReader::ReadCXXBaseSpecifier(Module &F,
Nick Lewycky19b9f952010-07-26 16:56:01 +00005303 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005304 bool isVirtual = static_cast<bool>(Record[Idx++]);
5305 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
5306 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redl08905022011-02-05 19:23:19 +00005307 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00005308 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
5309 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor752a5952011-01-03 22:36:02 +00005310 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redl08905022011-02-05 19:23:19 +00005311 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
Douglas Gregor752a5952011-01-03 22:36:02 +00005312 EllipsisLoc);
Sebastian Redl08905022011-02-05 19:23:19 +00005313 Result.setInheritConstructors(inheritConstructors);
5314 return Result;
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005315}
5316
Alexis Hunt1d792652011-01-08 20:30:50 +00005317std::pair<CXXCtorInitializer **, unsigned>
Douglas Gregora6895d82011-07-22 16:00:58 +00005318ASTReader::ReadCXXCtorInitializers(Module &F, const RecordData &Record,
Alexis Hunt1d792652011-01-08 20:30:50 +00005319 unsigned &Idx) {
5320 CXXCtorInitializer **CtorInitializers = 0;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005321 unsigned NumInitializers = Record[Idx++];
5322 if (NumInitializers) {
5323 ASTContext &C = *getContext();
5324
Alexis Hunt1d792652011-01-08 20:30:50 +00005325 CtorInitializers
5326 = new (C) CXXCtorInitializer*[NumInitializers];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005327 for (unsigned i=0; i != NumInitializers; ++i) {
5328 TypeSourceInfo *BaseClassInfo = 0;
5329 bool IsBaseVirtual = false;
5330 FieldDecl *Member = 0;
Francois Pichetd583da02010-12-04 09:14:42 +00005331 IndirectFieldDecl *IndirectMember = 0;
Alexis Hunt37a477f2011-05-04 01:19:08 +00005332 CXXConstructorDecl *Target = 0;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005333
Alexis Hunt37a477f2011-05-04 01:19:08 +00005334 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
5335 switch (Type) {
5336 case CTOR_INITIALIZER_BASE:
Sebastian Redl2c373b92010-10-05 15:59:54 +00005337 BaseClassInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005338 IsBaseVirtual = Record[Idx++];
Alexis Hunt37a477f2011-05-04 01:19:08 +00005339 break;
5340
5341 case CTOR_INITIALIZER_DELEGATING:
Douglas Gregor7fb09192011-07-21 22:35:25 +00005342 Target = ReadDeclAs<CXXConstructorDecl>(F, Record, Idx);
Alexis Hunt37a477f2011-05-04 01:19:08 +00005343 break;
5344
5345 case CTOR_INITIALIZER_MEMBER:
Douglas Gregor7fb09192011-07-21 22:35:25 +00005346 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
Alexis Hunt37a477f2011-05-04 01:19:08 +00005347 break;
5348
5349 case CTOR_INITIALIZER_INDIRECT_MEMBER:
Douglas Gregor7fb09192011-07-21 22:35:25 +00005350 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
Alexis Hunt37a477f2011-05-04 01:19:08 +00005351 break;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005352 }
Alexis Hunt37a477f2011-05-04 01:19:08 +00005353
Douglas Gregor44e7df62011-01-04 00:32:56 +00005354 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redl2c373b92010-10-05 15:59:54 +00005355 Expr *Init = ReadExpr(F);
Sebastian Redl2c373b92010-10-05 15:59:54 +00005356 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
5357 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005358 bool IsWritten = Record[Idx++];
5359 unsigned SourceOrderOrNumArrayIndices;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005360 SmallVector<VarDecl *, 8> Indices;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005361 if (IsWritten) {
5362 SourceOrderOrNumArrayIndices = Record[Idx++];
5363 } else {
5364 SourceOrderOrNumArrayIndices = Record[Idx++];
5365 Indices.reserve(SourceOrderOrNumArrayIndices);
5366 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
Douglas Gregor7fb09192011-07-21 22:35:25 +00005367 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005368 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005369
Alexis Hunt1d792652011-01-08 20:30:50 +00005370 CXXCtorInitializer *BOMInit;
Alexis Hunt37a477f2011-05-04 01:19:08 +00005371 if (Type == CTOR_INITIALIZER_BASE) {
Alexis Hunt1d792652011-01-08 20:30:50 +00005372 BOMInit = new (C) CXXCtorInitializer(C, BaseClassInfo, IsBaseVirtual,
5373 LParenLoc, Init, RParenLoc,
5374 MemberOrEllipsisLoc);
Alexis Hunt37a477f2011-05-04 01:19:08 +00005375 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
5376 BOMInit = new (C) CXXCtorInitializer(C, MemberOrEllipsisLoc, LParenLoc,
5377 Target, Init, RParenLoc);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005378 } else if (IsWritten) {
Francois Pichetd583da02010-12-04 09:14:42 +00005379 if (Member)
Alexis Hunt1d792652011-01-08 20:30:50 +00005380 BOMInit = new (C) CXXCtorInitializer(C, Member, MemberOrEllipsisLoc,
5381 LParenLoc, Init, RParenLoc);
Francois Pichetd583da02010-12-04 09:14:42 +00005382 else
Alexis Hunt1d792652011-01-08 20:30:50 +00005383 BOMInit = new (C) CXXCtorInitializer(C, IndirectMember,
5384 MemberOrEllipsisLoc, LParenLoc,
5385 Init, RParenLoc);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005386 } else {
Alexis Hunt1d792652011-01-08 20:30:50 +00005387 BOMInit = CXXCtorInitializer::Create(C, Member, MemberOrEllipsisLoc,
5388 LParenLoc, Init, RParenLoc,
5389 Indices.data(), Indices.size());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005390 }
5391
Argyrios Kyrtzidisd05f3e32010-09-06 19:04:27 +00005392 if (IsWritten)
5393 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Alexis Hunt1d792652011-01-08 20:30:50 +00005394 CtorInitializers[i] = BOMInit;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005395 }
5396 }
5397
Alexis Hunt1d792652011-01-08 20:30:50 +00005398 return std::make_pair(CtorInitializers, NumInitializers);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005399}
5400
Chris Lattnerca025db2010-05-07 21:43:38 +00005401NestedNameSpecifier *
Douglas Gregora6895d82011-07-22 16:00:58 +00005402ASTReader::ReadNestedNameSpecifier(Module &F,
Douglas Gregor7fb09192011-07-21 22:35:25 +00005403 const RecordData &Record, unsigned &Idx) {
Chris Lattnerca025db2010-05-07 21:43:38 +00005404 unsigned N = Record[Idx++];
5405 NestedNameSpecifier *NNS = 0, *Prev = 0;
5406 for (unsigned I = 0; I != N; ++I) {
5407 NestedNameSpecifier::SpecifierKind Kind
5408 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
5409 switch (Kind) {
5410 case NestedNameSpecifier::Identifier: {
Douglas Gregora3e41532011-07-28 20:55:49 +00005411 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Chris Lattnerca025db2010-05-07 21:43:38 +00005412 NNS = NestedNameSpecifier::Create(*Context, Prev, II);
5413 break;
5414 }
5415
5416 case NestedNameSpecifier::Namespace: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00005417 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Chris Lattnerca025db2010-05-07 21:43:38 +00005418 NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
5419 break;
5420 }
5421
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005422 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00005423 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005424 NNS = NestedNameSpecifier::Create(*Context, Prev, Alias);
5425 break;
5426 }
5427
Chris Lattnerca025db2010-05-07 21:43:38 +00005428 case NestedNameSpecifier::TypeSpec:
5429 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregor903b7e92011-07-22 00:38:23 +00005430 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
Douglas Gregor0cdc8322010-12-10 17:03:06 +00005431 if (!T)
5432 return 0;
5433
Chris Lattnerca025db2010-05-07 21:43:38 +00005434 bool Template = Record[Idx++];
5435 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
5436 break;
5437 }
5438
5439 case NestedNameSpecifier::Global: {
5440 NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
5441 // No associated value, and there can't be a prefix.
5442 break;
5443 }
Chris Lattnerca025db2010-05-07 21:43:38 +00005444 }
Argyrios Kyrtzidisad65c692010-07-07 15:46:30 +00005445 Prev = NNS;
Chris Lattnerca025db2010-05-07 21:43:38 +00005446 }
5447 return NNS;
5448}
5449
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005450NestedNameSpecifierLoc
Douglas Gregora6895d82011-07-22 16:00:58 +00005451ASTReader::ReadNestedNameSpecifierLoc(Module &F, const RecordData &Record,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005452 unsigned &Idx) {
5453 unsigned N = Record[Idx++];
Douglas Gregor9b272512011-02-28 23:58:31 +00005454 NestedNameSpecifierLocBuilder Builder;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005455 for (unsigned I = 0; I != N; ++I) {
5456 NestedNameSpecifier::SpecifierKind Kind
5457 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
5458 switch (Kind) {
5459 case NestedNameSpecifier::Identifier: {
Douglas Gregora3e41532011-07-28 20:55:49 +00005460 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005461 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00005462 Builder.Extend(*Context, II, Range.getBegin(), Range.getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005463 break;
5464 }
5465
5466 case NestedNameSpecifier::Namespace: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00005467 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005468 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00005469 Builder.Extend(*Context, NS, Range.getBegin(), Range.getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005470 break;
5471 }
5472
5473 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00005474 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005475 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00005476 Builder.Extend(*Context, Alias, Range.getBegin(), Range.getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005477 break;
5478 }
5479
5480 case NestedNameSpecifier::TypeSpec:
5481 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005482 bool Template = Record[Idx++];
5483 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
5484 if (!T)
5485 return NestedNameSpecifierLoc();
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005486 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00005487
5488 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
5489 Builder.Extend(*Context,
5490 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
5491 T->getTypeLoc(), ColonColonLoc);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005492 break;
5493 }
5494
5495 case NestedNameSpecifier::Global: {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005496 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00005497 Builder.MakeGlobal(*Context, ColonColonLoc);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005498 break;
5499 }
5500 }
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005501 }
5502
Douglas Gregor9b272512011-02-28 23:58:31 +00005503 return Builder.getWithLocInContext(*Context);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005504}
5505
Chris Lattnerca025db2010-05-07 21:43:38 +00005506SourceRange
Douglas Gregora6895d82011-07-22 16:00:58 +00005507ASTReader::ReadSourceRange(Module &F, const RecordData &Record,
Sebastian Redl2c373b92010-10-05 15:59:54 +00005508 unsigned &Idx) {
5509 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
5510 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar6d3bc082010-06-02 15:47:10 +00005511 return SourceRange(beg, end);
Chris Lattnerca025db2010-05-07 21:43:38 +00005512}
5513
Douglas Gregor1daeb692009-04-13 18:14:40 +00005514/// \brief Read an integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00005515llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00005516 unsigned BitWidth = Record[Idx++];
5517 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
5518 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
5519 Idx += NumWords;
5520 return Result;
5521}
5522
5523/// \brief Read a signed integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00005524llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00005525 bool isUnsigned = Record[Idx++];
5526 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
5527}
5528
Douglas Gregore0a3a512009-04-14 21:55:33 +00005529/// \brief Read a floating-point value
Sebastian Redl2c499f62010-08-18 23:56:43 +00005530llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00005531 return llvm::APFloat(ReadAPInt(Record, Idx));
5532}
5533
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00005534// \brief Read a string
Sebastian Redl2c499f62010-08-18 23:56:43 +00005535std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00005536 unsigned Len = Record[Idx++];
Jay Foad7d0479f2009-05-21 09:52:38 +00005537 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00005538 Idx += Len;
5539 return Result;
5540}
5541
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005542VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
5543 unsigned &Idx) {
5544 unsigned Major = Record[Idx++];
5545 unsigned Minor = Record[Idx++];
5546 unsigned Subminor = Record[Idx++];
5547 if (Minor == 0)
5548 return VersionTuple(Major);
5549 if (Subminor == 0)
5550 return VersionTuple(Major, Minor - 1);
5551 return VersionTuple(Major, Minor - 1, Subminor - 1);
5552}
5553
Douglas Gregora6895d82011-07-22 16:00:58 +00005554CXXTemporary *ASTReader::ReadCXXTemporary(Module &F,
Douglas Gregor7fb09192011-07-21 22:35:25 +00005555 const RecordData &Record,
Chris Lattnercba86142010-05-10 00:25:06 +00005556 unsigned &Idx) {
Douglas Gregor7fb09192011-07-21 22:35:25 +00005557 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
Chris Lattnercba86142010-05-10 00:25:06 +00005558 return CXXTemporary::Create(*Context, Decl);
5559}
5560
Sebastian Redl2c499f62010-08-18 23:56:43 +00005561DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregor92863e42009-04-10 23:10:45 +00005562 return Diag(SourceLocation(), DiagID);
5563}
5564
Sebastian Redl2c499f62010-08-18 23:56:43 +00005565DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00005566 return Diags.Report(Loc, DiagID);
Douglas Gregor55abb232009-04-10 20:39:37 +00005567}
Douglas Gregora9af1d12009-04-17 00:04:06 +00005568
Douglas Gregora868bbd2009-04-21 22:25:48 +00005569/// \brief Retrieve the identifier table associated with the
5570/// preprocessor.
Sebastian Redl2c499f62010-08-18 23:56:43 +00005571IdentifierTable &ASTReader::getIdentifierTable() {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00005572 assert(PP && "Forgot to set Preprocessor ?");
5573 return PP->getIdentifierTable();
Douglas Gregora868bbd2009-04-21 22:25:48 +00005574}
5575
Douglas Gregora9af1d12009-04-17 00:04:06 +00005576/// \brief Record that the given ID maps to the given switch-case
5577/// statement.
Sebastian Redl2c499f62010-08-18 23:56:43 +00005578void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Douglas Gregora9af1d12009-04-17 00:04:06 +00005579 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
5580 SwitchCaseStmts[ID] = SC;
5581}
5582
5583/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00005584SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Douglas Gregora9af1d12009-04-17 00:04:06 +00005585 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
5586 return SwitchCaseStmts[ID];
5587}
Douglas Gregor6cc68a42009-04-17 18:18:49 +00005588
Argyrios Kyrtzidisd9f526f2010-10-28 09:29:32 +00005589void ASTReader::ClearSwitchCaseIDs() {
5590 SwitchCaseStmts.clear();
5591}
5592
Sebastian Redl2c499f62010-08-18 23:56:43 +00005593void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00005594 assert(NumCurrentElementsDeserializing &&
5595 "FinishedDeserializing not paired with StartedDeserializing");
5596 if (NumCurrentElementsDeserializing == 1) {
Douglas Gregor1342e842009-07-06 18:54:52 +00005597 // If any identifiers with corresponding top-level declarations have
5598 // been loaded, load those declarations now.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00005599 while (!PendingIdentifierInfos.empty()) {
5600 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
5601 PendingIdentifierInfos.front().DeclIDs, true);
5602 PendingIdentifierInfos.pop_front();
Douglas Gregor1342e842009-07-06 18:54:52 +00005603 }
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00005604
Argyrios Kyrtzidis9fdd2542011-02-12 07:50:47 +00005605 // Ready to load previous declarations of Decls that were delayed.
5606 while (!PendingPreviousDecls.empty()) {
5607 loadAndAttachPreviousDecl(PendingPreviousDecls.front().first,
5608 PendingPreviousDecls.front().second);
5609 PendingPreviousDecls.pop_front();
5610 }
5611
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00005612 // We are not in recursive loading, so it's safe to pass the "interesting"
5613 // decls to the consumer.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00005614 if (Consumer)
5615 PassInterestingDeclsToConsumer();
Argyrios Kyrtzidisad5f95c2010-10-24 17:26:31 +00005616
5617 assert(PendingForwardRefs.size() == 0 &&
5618 "Some forward refs did not get linked to the definition!");
Douglas Gregor1342e842009-07-06 18:54:52 +00005619 }
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00005620 --NumCurrentElementsDeserializing;
Douglas Gregor1342e842009-07-06 18:54:52 +00005621}
Douglas Gregorb473b072010-08-19 00:28:17 +00005622
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005623ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
Douglas Gregorc567ba22011-07-22 16:35:34 +00005624 StringRef isysroot, bool DisableValidation,
Douglas Gregor606c4ac2011-02-05 19:42:43 +00005625 bool DisableStatCache)
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005626 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
5627 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
5628 Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
Jonathan D. Turnerecc27402011-07-28 17:20:23 +00005629 Consumer(0), ModuleMgr(FileMgr.getFileSystemOptions()),
5630 RelocatablePCH(false), isysroot(isysroot),
Douglas Gregor925296b2011-07-19 16:10:42 +00005631 DisableValidation(DisableValidation),
Douglas Gregor606c4ac2011-02-05 19:42:43 +00005632 DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0),
Douglas Gregor925296b2011-07-19 16:10:42 +00005633 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Douglas Gregor606c4ac2011-02-05 19:42:43 +00005634 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
5635 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
5636 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
5637 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
Jonathan D. Turner3766fdb2011-07-21 21:15:19 +00005638 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
5639 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
5640 NumCXXBaseSpecifiersLoaded(0)
Douglas Gregor606c4ac2011-02-05 19:42:43 +00005641{
Douglas Gregor925296b2011-07-19 16:10:42 +00005642 SourceMgr.setExternalSLocEntrySource(this);
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005643}
5644
5645ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
Douglas Gregorc567ba22011-07-22 16:35:34 +00005646 Diagnostic &Diags, StringRef isysroot,
Douglas Gregor606c4ac2011-02-05 19:42:43 +00005647 bool DisableValidation, bool DisableStatCache)
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005648 : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
Jonathan D. Turnerecc27402011-07-28 17:20:23 +00005649 Diags(Diags), SemaObj(0), PP(0), Context(0),
5650 Consumer(0), ModuleMgr(FileMgr.getFileSystemOptions()),
Douglas Gregor925296b2011-07-19 16:10:42 +00005651 RelocatablePCH(false), isysroot(isysroot),
5652 DisableValidation(DisableValidation), DisableStatCache(DisableStatCache),
5653 NumStatHits(0), NumStatMisses(0), NumSLocEntriesRead(0),
5654 TotalNumSLocEntries(0), NumStatementsRead(0),
5655 TotalNumStatements(0), NumMacrosRead(0), TotalNumMacros(0),
5656 NumSelectorsRead(0), NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
Sebastian Redlc1d035f2010-09-22 20:19:08 +00005657 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
5658 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
Jonathan D. Turner3766fdb2011-07-21 21:15:19 +00005659 TotalVisibleDeclContexts(0), TotalModulesSizeInBits(0),
5660 NumCurrentElementsDeserializing(0), NumCXXBaseSpecifiersLoaded(0)
Douglas Gregor925296b2011-07-19 16:10:42 +00005661{
5662 SourceMgr.setExternalSLocEntrySource(this);
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005663}
5664
5665ASTReader::~ASTReader() {
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005666 for (DeclContextVisibleUpdatesPending::iterator
5667 I = PendingVisibleUpdates.begin(),
5668 E = PendingVisibleUpdates.end();
5669 I != E; ++I) {
5670 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
5671 F = I->second.end();
5672 J != F; ++J)
Douglas Gregorf7180622011-08-03 15:48:04 +00005673 delete static_cast<ASTDeclContextNameLookupTable*>(J->first);
Sebastian Redld7dce0a2010-08-24 00:50:04 +00005674 }
5675}
5676
Douglas Gregorab443b92011-08-20 04:39:52 +00005677//===----------------------------------------------------------------------===//
5678// Module implementation
5679//===----------------------------------------------------------------------===//
Douglas Gregora6895d82011-07-22 16:00:58 +00005680Module::Module(ModuleKind Kind)
Douglas Gregordf0c1512011-08-18 04:12:04 +00005681 : Kind(Kind), DirectlyImported(false), SizeInBits(0),
5682 LocalNumSLocEntries(0), SLocEntryBaseID(0),
Douglas Gregor925296b2011-07-19 16:10:42 +00005683 SLocEntryBaseOffset(0), SLocEntryOffsets(0),
5684 SLocFileOffsets(0), LocalNumIdentifiers(0),
Douglas Gregor00659902011-08-02 10:56:51 +00005685 IdentifierOffsets(0), BaseIdentifierID(0), IdentifierTableData(0),
5686 IdentifierLookupTable(0), BasePreprocessedEntityID(0),
5687 LocalNumMacroDefinitions(0), MacroDefinitionOffsets(0),
5688 BaseMacroDefinitionID(0), LocalNumHeaderFileInfos(0),
Douglas Gregor925296b2011-07-19 16:10:42 +00005689 HeaderFileInfoTableData(0), HeaderFileInfoTable(0),
Douglas Gregor4b123cb2011-07-28 04:50:02 +00005690 HeaderFileFrameworkStrings(0),
Douglas Gregor00659902011-08-02 10:56:51 +00005691 LocalNumSelectors(0), SelectorOffsets(0), BaseSelectorID(0),
Sebastian Redl949fe9e2010-09-22 00:42:27 +00005692 SelectorLookupTableData(0), SelectorLookupTable(0), LocalNumDecls(0),
Douglas Gregor00659902011-08-02 10:56:51 +00005693 DeclOffsets(0), BaseDeclID(0),
5694 LocalNumCXXBaseSpecifiers(0), CXXBaseSpecifiersOffsets(0),
Douglas Gregor3b65ed02011-08-02 18:32:54 +00005695 LocalNumTypes(0), TypeOffsets(0), BaseTypeIndex(0), StatCache(0),
Jonathan D. Turner269f2562011-07-28 23:15:22 +00005696 NumPreallocatedPreprocessingEntities(0)
Douglas Gregorb473b072010-08-19 00:28:17 +00005697{}
5698
Douglas Gregora6895d82011-07-22 16:00:58 +00005699Module::~Module() {
Douglas Gregor94619c82011-08-24 19:03:07 +00005700 for (DeclContextInfosMap::iterator I = DeclContextInfos.begin(),
5701 E = DeclContextInfos.end();
5702 I != E; ++I) {
5703 if (I->second.NameLookupTableData)
5704 delete static_cast<ASTDeclContextNameLookupTable*>(
5705 I->second.NameLookupTableData);
5706 }
5707
Douglas Gregorb473b072010-08-19 00:28:17 +00005708 delete static_cast<ASTIdentifierLookupTable *>(IdentifierLookupTable);
Douglas Gregor09b69892011-02-10 17:09:37 +00005709 delete static_cast<HeaderFileInfoLookupTable *>(HeaderFileInfoTable);
Douglas Gregorb473b072010-08-19 00:28:17 +00005710 delete static_cast<ASTSelectorLookupTable *>(SelectorLookupTable);
5711}
Jonathan D. Turnerb2b08232011-07-26 18:21:30 +00005712
Douglas Gregor1cc9c062011-08-02 11:12:41 +00005713template<typename Key, typename Offset, unsigned InitialCapacity>
5714static void
5715dumpLocalRemap(StringRef Name,
5716 const ContinuousRangeMap<Key, Offset, InitialCapacity> &Map) {
5717 if (Map.begin() == Map.end())
5718 return;
5719
5720 typedef ContinuousRangeMap<Key, Offset, InitialCapacity> MapType;
5721 llvm::errs() << " " << Name << ":\n";
5722 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
5723 I != IEnd; ++I) {
5724 llvm::errs() << " " << I->first << " -> " << I->second
5725 << "\n";
5726 }
5727}
5728
5729void Module::dump() {
5730 llvm::errs() << "\nModule: " << FileName << "\n";
5731 if (!Imports.empty()) {
5732 llvm::errs() << " Imports: ";
5733 for (unsigned I = 0, N = Imports.size(); I != N; ++I) {
5734 if (I)
5735 llvm::errs() << ", ";
5736 llvm::errs() << Imports[I]->FileName;
5737 }
5738 llvm::errs() << "\n";
5739 }
5740
5741 // Remapping tables.
5742 llvm::errs() << " Base source location offset: " << SLocEntryBaseOffset
5743 << '\n';
Douglas Gregorfddf5302011-08-04 19:00:50 +00005744 dumpLocalRemap("Source location offset local -> global map", SLocRemap);
5745
Douglas Gregor1ab036c2011-08-03 21:49:18 +00005746 llvm::errs() << " Base identifier ID: " << BaseIdentifierID << '\n'
Douglas Gregor2f555fc2011-08-04 18:56:47 +00005747 << " Number of identifiers: " << LocalNumIdentifiers << '\n';
Douglas Gregorfddf5302011-08-04 19:00:50 +00005748 dumpLocalRemap("Identifier ID local -> global map", IdentifierRemap);
5749
5750 llvm::errs() << " Base selector ID: " << BaseSelectorID << '\n'
5751 << " Number of selectors: " << LocalNumSelectors << '\n';
5752 dumpLocalRemap("Selector ID local -> global map", SelectorRemap);
5753
Douglas Gregor2f555fc2011-08-04 18:56:47 +00005754 llvm::errs() << " Base preprocessed entity ID: " << BasePreprocessedEntityID
5755 << '\n'
Douglas Gregordab42432011-08-12 00:15:20 +00005756 << " Number of preprocessed entities: "
Douglas Gregor2f555fc2011-08-04 18:56:47 +00005757 << NumPreallocatedPreprocessingEntities << '\n';
Douglas Gregorfddf5302011-08-04 19:00:50 +00005758 dumpLocalRemap("Preprocessed entity ID local -> global map",
5759 PreprocessedEntityRemap);
5760
Douglas Gregora863b4b2011-08-04 16:36:56 +00005761 llvm::errs() << " Base macro definition ID: " << BaseMacroDefinitionID
5762 << '\n'
5763 << " Number of macro definitions: " << LocalNumMacroDefinitions
5764 << '\n';
Douglas Gregorfddf5302011-08-04 19:00:50 +00005765 dumpLocalRemap("Macro definition ID local -> global map",
5766 MacroDefinitionRemap);
5767
5768 llvm::errs() << " Base type index: " << BaseTypeIndex << '\n'
5769 << " Number of types: " << LocalNumTypes << '\n';
5770 dumpLocalRemap("Type index local -> global map", TypeRemap);
5771
Douglas Gregorf7180622011-08-03 15:48:04 +00005772 llvm::errs() << " Base decl ID: " << BaseDeclID << '\n'
5773 << " Number of decls: " << LocalNumDecls << '\n';
Douglas Gregorfddf5302011-08-04 19:00:50 +00005774 dumpLocalRemap("Decl ID local -> global map", DeclRemap);
Douglas Gregor1cc9c062011-08-02 11:12:41 +00005775}
5776
Douglas Gregorab443b92011-08-20 04:39:52 +00005777//===----------------------------------------------------------------------===//
5778// Module manager implementation
5779//===----------------------------------------------------------------------===//
5780
Jonathan D. Turnerecc27402011-07-28 17:20:23 +00005781Module *ModuleManager::lookup(StringRef Name) {
5782 const FileEntry *Entry = FileMgr.getFile(Name);
5783 return Modules[Entry];
5784}
5785
Jonathan D. Turnerdb1c9e32011-08-02 17:40:32 +00005786llvm::MemoryBuffer *ModuleManager::lookupBuffer(StringRef Name) {
5787 const FileEntry *Entry = FileMgr.getFile(Name);
5788 return InMemoryBuffers[Entry];
5789}
5790
Douglas Gregor4dd3e942011-08-19 02:29:29 +00005791std::pair<Module *, bool>
5792ModuleManager::addModule(StringRef FileName, ModuleKind Type,
5793 Module *ImportedBy, std::string &ErrorStr) {
Chad Rosier222e1872011-08-18 19:06:24 +00005794 const FileEntry *Entry = FileMgr.getFile(FileName);
Douglas Gregor4dd3e942011-08-19 02:29:29 +00005795 if (!Entry && FileName != "-") {
5796 ErrorStr = "file not found";
5797 return std::make_pair(static_cast<Module*>(0), false);
5798 }
5799
5800 // Check whether we already loaded this module, before
5801 Module *&ModuleEntry = Modules[Entry];
5802 bool NewModule = false;
5803 if (!ModuleEntry) {
5804 // Allocate a new module.
5805 Module *New = new Module(Type);
5806 New->FileName = FileName.str();
5807 Chain.push_back(New);
5808 NewModule = true;
5809 ModuleEntry = New;
5810
5811 // Load the contents of the module
5812 if (llvm::MemoryBuffer *Buffer = lookupBuffer(FileName)) {
5813 // The buffer was already provided for us.
5814 assert(Buffer && "Passed null buffer");
5815 New->Buffer.reset(Buffer);
5816 } else {
5817 // Open the AST file.
5818 llvm::error_code ec;
5819 if (FileName == "-") {
5820 ec = llvm::MemoryBuffer::getSTDIN(New->Buffer);
5821 if (ec)
5822 ErrorStr = ec.message();
5823 } else
5824 New->Buffer.reset(FileMgr.getBufferForFile(FileName, &ErrorStr));
5825
5826 if (!New->Buffer)
5827 return std::make_pair(static_cast<Module*>(0), false);
5828 }
5829
5830 // Initialize the stream
5831 New->StreamFile.init((const unsigned char *)New->Buffer->getBufferStart(),
5832 (const unsigned char *)New->Buffer->getBufferEnd()); }
Jonathan D. Turnerb2b08232011-07-26 18:21:30 +00005833
Douglas Gregordf0c1512011-08-18 04:12:04 +00005834 if (ImportedBy) {
Douglas Gregor4dd3e942011-08-19 02:29:29 +00005835 ModuleEntry->ImportedBy.insert(ImportedBy);
5836 ImportedBy->Imports.insert(ModuleEntry);
Douglas Gregordf0c1512011-08-18 04:12:04 +00005837 } else {
Douglas Gregor4dd3e942011-08-19 02:29:29 +00005838 ModuleEntry->DirectlyImported = true;
Jonathan D. Turner10d52012011-07-29 18:09:09 +00005839 }
5840
Douglas Gregor4dd3e942011-08-19 02:29:29 +00005841 return std::make_pair(ModuleEntry, NewModule);
Jonathan D. Turnerb2b08232011-07-26 18:21:30 +00005842}
5843
Jonathan D. Turnerdb1c9e32011-08-02 17:40:32 +00005844void ModuleManager::addInMemoryBuffer(StringRef FileName,
5845 llvm::MemoryBuffer *Buffer) {
5846
5847 const FileEntry *Entry = FileMgr.getVirtualFile(FileName,
5848 Buffer->getBufferSize(), 0);
5849 InMemoryBuffers[Entry] = Buffer;
5850}
Jonathan D. Turnerb2b08232011-07-26 18:21:30 +00005851
Jonathan D. Turnerecc27402011-07-28 17:20:23 +00005852ModuleManager::ModuleManager(const FileSystemOptions &FSO) : FileMgr(FSO) { }
5853
Jonathan D. Turnerb2b08232011-07-26 18:21:30 +00005854ModuleManager::~ModuleManager() {
5855 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
5856 delete Chain[e - i - 1];
5857}
Douglas Gregorab443b92011-08-20 04:39:52 +00005858
5859void ModuleManager::visit(bool (*Visitor)(Module &M, void *UserData),
5860 void *UserData) {
5861 unsigned N = size();
5862
5863 // Record the number of incoming edges for each module. When we
5864 // encounter a module with no incoming edges, push it into the queue
5865 // to seed the queue.
5866 SmallVector<Module *, 4> Queue;
5867 Queue.reserve(N);
5868 llvm::DenseMap<Module *, unsigned> UnusedIncomingEdges;
5869 for (ModuleIterator M = begin(), MEnd = end(); M != MEnd; ++M) {
5870 if (unsigned Size = (*M)->ImportedBy.size())
5871 UnusedIncomingEdges[*M] = Size;
5872 else
5873 Queue.push_back(*M);
5874 }
5875
5876 llvm::SmallPtrSet<Module *, 4> Skipped;
5877 unsigned QueueStart = 0;
5878 while (QueueStart < Queue.size()) {
5879 Module *CurrentModule = Queue[QueueStart++];
5880
5881 // Check whether this module should be skipped.
5882 if (Skipped.count(CurrentModule))
5883 continue;
5884
5885 if (Visitor(*CurrentModule, UserData)) {
5886 // The visitor has requested that cut off visitation of any
5887 // module that the current module depends on. To indicate this
5888 // behavior, we mark all of the reachable modules as having N
5889 // incoming edges (which is impossible otherwise).
5890 SmallVector<Module *, 4> Stack;
5891 Stack.push_back(CurrentModule);
5892 Skipped.insert(CurrentModule);
5893 while (!Stack.empty()) {
5894 Module *NextModule = Stack.back();
5895 Stack.pop_back();
5896
5897 // For any module that this module depends on, push it on the
5898 // stack (if it hasn't already been marked as visited).
5899 for (llvm::SetVector<Module *>::iterator
5900 M = NextModule->Imports.begin(),
5901 MEnd = NextModule->Imports.end();
5902 M != MEnd; ++M) {
5903 if (Skipped.insert(*M))
5904 Stack.push_back(*M);
5905 }
5906 }
5907 continue;
5908 }
5909
5910 // For any module that this module depends on, push it on the
5911 // stack (if it hasn't already been marked as visited).
5912 for (llvm::SetVector<Module *>::iterator M = CurrentModule->Imports.begin(),
5913 MEnd = CurrentModule->Imports.end();
5914 M != MEnd; ++M) {
5915
5916 // Remove our current module as an impediment to visiting the
5917 // module we depend on. If we were the last unvisited module
5918 // that depends on this particular module, push it into the
5919 // queue to be visited.
5920 unsigned &NumUnusedEdges = UnusedIncomingEdges[*M];
5921 if (NumUnusedEdges && (--NumUnusedEdges == 0))
5922 Queue.push_back(*M);
5923 }
5924 }
5925}
Douglas Gregor1257f972011-08-24 21:27:34 +00005926
5927/// \brief Perform a depth-first visit of the current module.
5928static bool visitDepthFirst(Module &M,
5929 bool (*Visitor)(Module &M, bool Preorder,
5930 void *UserData),
5931 void *UserData,
5932 llvm::SmallPtrSet<Module *, 4> &Visited) {
5933 // Preorder visitation
5934 if (Visitor(M, /*Preorder=*/true, UserData))
5935 return true;
5936
5937 // Visit children
5938 for (llvm::SetVector<Module *>::iterator IM = M.Imports.begin(),
5939 IMEnd = M.Imports.end();
5940 IM != IMEnd; ++IM) {
5941 if (!Visited.insert(*IM))
5942 continue;
5943
5944 if (visitDepthFirst(**IM, Visitor, UserData, Visited))
5945 return true;
5946 }
5947
5948 // Postorder visitation
5949 return Visitor(M, /*Preorder=*/false, UserData);
5950}
5951
5952void ModuleManager::visitDepthFirst(bool (*Visitor)(Module &M, bool Preorder,
5953 void *UserData),
5954 void *UserData) {
5955 llvm::SmallPtrSet<Module *, 4> Visited;
5956 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
5957 if (!Visited.insert(Chain[I]))
5958 continue;
5959
5960 if (::visitDepthFirst(*Chain[I], Visitor, UserData, Visited))
5961 return;
5962 }
5963}