blob: 9acc759fc945ee761db8ff902304642f91bc1143 [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"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000023#include "clang/AST/Expr.h"
John McCallbfd822c2010-08-24 07:32:53 +000024#include "clang/AST/ExprCXX.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000025#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000026#include "clang/AST/TypeLocVisitor.h"
Chris Lattner34321bc2009-04-10 21:41:48 +000027#include "clang/Lex/MacroInfo.h"
Douglas Gregoraae92242010-03-19 21:51:54 +000028#include "clang/Lex/PreprocessingRecord.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000029#include "clang/Lex/Preprocessor.h"
Steve Naroff3fa455a2009-04-24 20:03:17 +000030#include "clang/Lex/HeaderSearch.h"
Douglas Gregora868bbd2009-04-21 22:25:48 +000031#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000032#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000033#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000034#include "clang/Basic/FileManager.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000035#include "clang/Basic/TargetInfo.h"
Douglas Gregord54f3a12009-10-05 21:07:28 +000036#include "clang/Basic/Version.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000037#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000038#include "llvm/Bitcode/BitstreamReader.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000039#include "llvm/Support/MemoryBuffer.h"
John McCall0ad16662009-10-29 08:12:44 +000040#include "llvm/Support/ErrorHandling.h"
Daniel Dunbarf2ce9a22009-11-18 19:50:41 +000041#include "llvm/System/Path.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000042#include <algorithm>
Douglas Gregorc379c072009-04-28 18:58:38 +000043#include <iterator>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000044#include <cstdio>
Douglas Gregorc5046832009-04-27 18:38:38 +000045#include <sys/stat.h>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000046using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000047using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000048
49//===----------------------------------------------------------------------===//
Sebastian Redld44cd6a2010-08-18 23:57:06 +000050// PCH validator implementation
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000051//===----------------------------------------------------------------------===//
52
Sebastian Redl3e31c722010-08-18 23:56:56 +000053ASTReaderListener::~ASTReaderListener() {}
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000054
55bool
56PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts) {
57 const LangOptions &PPLangOpts = PP.getLangOptions();
58#define PARSE_LANGOPT_BENIGN(Option)
59#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
60 if (PPLangOpts.Option != LangOpts.Option) { \
61 Reader.Diag(DiagID) << LangOpts.Option << PPLangOpts.Option; \
62 return true; \
63 }
64
65 PARSE_LANGOPT_BENIGN(Trigraphs);
66 PARSE_LANGOPT_BENIGN(BCPLComment);
67 PARSE_LANGOPT_BENIGN(DollarIdents);
68 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
69 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
Chandler Carruthe03aa552010-04-17 20:17:31 +000070 PARSE_LANGOPT_IMPORTANT(GNUKeywords, diag::warn_pch_gnu_keywords);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000071 PARSE_LANGOPT_BENIGN(ImplicitInt);
72 PARSE_LANGOPT_BENIGN(Digraphs);
73 PARSE_LANGOPT_BENIGN(HexFloats);
74 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
75 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
76 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
77 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
78 PARSE_LANGOPT_BENIGN(CXXOperatorName);
79 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
80 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
81 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
Fariborz Jahanian45878032010-02-09 19:31:38 +000082 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI2, diag::warn_pch_nonfragile_abi2);
Fariborz Jahanian62c56022010-04-22 21:01:59 +000083 PARSE_LANGOPT_IMPORTANT(NoConstantCFStrings,
84 diag::warn_pch_no_constant_cfstrings);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000085 PARSE_LANGOPT_BENIGN(PascalStrings);
86 PARSE_LANGOPT_BENIGN(WritableStrings);
Mike Stump11289f42009-09-09 15:08:12 +000087 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000088 diag::warn_pch_lax_vector_conversions);
Nate Begeman9d905792009-06-25 22:57:40 +000089 PARSE_LANGOPT_IMPORTANT(AltiVec, diag::warn_pch_altivec);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000090 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
Daniel Dunbar925152c2010-02-10 18:48:44 +000091 PARSE_LANGOPT_IMPORTANT(SjLjExceptions, diag::warn_pch_sjlj_exceptions);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000092 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
93 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
94 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
Mike Stump11289f42009-09-09 15:08:12 +000095 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000096 diag::warn_pch_thread_safe_statics);
Daniel Dunbara77eaeb2009-09-03 04:54:28 +000097 PARSE_LANGOPT_IMPORTANT(POSIXThreads, diag::warn_pch_posix_threads);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000098 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
99 PARSE_LANGOPT_BENIGN(EmitAllDecls);
100 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
Chris Lattner51924e512010-06-26 21:25:03 +0000101 PARSE_LANGOPT_BENIGN(getSignedOverflowBehavior());
Mike Stump11289f42009-09-09 15:08:12 +0000102 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000103 diag::warn_pch_heinous_extensions);
104 // FIXME: Most of the options below are benign if the macro wasn't
105 // used. Unfortunately, this means that a PCH compiled without
106 // optimization can't be used with optimization turned on, even
107 // though the only thing that changes is whether __OPTIMIZE__ was
108 // defined... but if __OPTIMIZE__ never showed up in the header, it
109 // doesn't matter. We could consider making this some special kind
110 // of check.
111 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
112 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
113 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
114 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
115 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
116 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
117 PARSE_LANGOPT_IMPORTANT(AccessControl, diag::warn_pch_access_control);
118 PARSE_LANGOPT_IMPORTANT(CharIsSigned, diag::warn_pch_char_signed);
John Thompsoned4e2952009-11-05 20:14:16 +0000119 PARSE_LANGOPT_IMPORTANT(ShortWChar, diag::warn_pch_short_wchar);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000120 if ((PPLangOpts.getGCMode() != 0) != (LangOpts.getGCMode() != 0)) {
Mike Stump11289f42009-09-09 15:08:12 +0000121 Reader.Diag(diag::warn_pch_gc_mode)
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000122 << LangOpts.getGCMode() << PPLangOpts.getGCMode();
123 return true;
124 }
125 PARSE_LANGOPT_BENIGN(getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +0000126 PARSE_LANGOPT_IMPORTANT(getStackProtectorMode(),
127 diag::warn_pch_stack_protector);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000128 PARSE_LANGOPT_BENIGN(InstantiationDepth);
Nate Begeman9d905792009-06-25 22:57:40 +0000129 PARSE_LANGOPT_IMPORTANT(OpenCL, diag::warn_pch_opencl);
Mike Stumpd9546382009-12-12 01:27:46 +0000130 PARSE_LANGOPT_BENIGN(CatchUndefined);
Daniel Dunbar143021e2009-09-21 04:16:19 +0000131 PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors);
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +0000132 PARSE_LANGOPT_BENIGN(SpellChecking);
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +0000133#undef PARSE_LANGOPT_IMPORTANT
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000134#undef PARSE_LANGOPT_BENIGN
135
136 return false;
137}
138
Daniel Dunbar20a682d2009-11-11 00:52:11 +0000139bool PCHValidator::ReadTargetTriple(llvm::StringRef Triple) {
140 if (Triple == PP.getTargetInfo().getTriple().str())
141 return false;
142
143 Reader.Diag(diag::warn_pch_target_triple)
144 << Triple << PP.getTargetInfo().getTriple().str();
145 return true;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000146}
147
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000148struct EmptyStringRef {
Benjamin Kramer8d5609b2010-07-14 23:19:41 +0000149 bool operator ()(llvm::StringRef r) const { return r.empty(); }
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000150};
151struct EmptyBlock {
152 bool operator ()(const PCHPredefinesBlock &r) const { return r.Data.empty(); }
153};
154
155static bool EqualConcatenations(llvm::SmallVector<llvm::StringRef, 2> L,
156 PCHPredefinesBlocks R) {
157 // First, sum up the lengths.
158 unsigned LL = 0, RL = 0;
159 for (unsigned I = 0, N = L.size(); I != N; ++I) {
160 LL += L[I].size();
161 }
162 for (unsigned I = 0, N = R.size(); I != N; ++I) {
163 RL += R[I].Data.size();
164 }
165 if (LL != RL)
166 return false;
167 if (LL == 0 && RL == 0)
168 return true;
169
170 // Kick out empty parts, they confuse the algorithm below.
171 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
172 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
173
174 // Do it the hard way. At this point, both vectors must be non-empty.
175 llvm::StringRef LR = L[0], RR = R[0].Data;
176 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
Daniel Dunbar01ad0a72010-07-16 00:00:11 +0000177 (void) RN;
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000178 for (;;) {
179 // Compare the current pieces.
180 if (LR.size() == RR.size()) {
181 // If they're the same length, it's pretty easy.
182 if (LR != RR)
183 return false;
184 // Both pieces are done, advance.
185 ++LI;
186 ++RI;
187 // If either string is done, they're both done, since they're the same
188 // length.
189 if (LI == LN) {
190 assert(RI == RN && "Strings not the same length after all?");
191 return true;
192 }
193 LR = L[LI];
194 RR = R[RI].Data;
195 } else if (LR.size() < RR.size()) {
196 // Right piece is longer.
197 if (!RR.startswith(LR))
198 return false;
199 ++LI;
200 assert(LI != LN && "Strings not the same length after all?");
201 RR = RR.substr(LR.size());
202 LR = L[LI];
203 } else {
204 // Left piece is longer.
205 if (!LR.startswith(RR))
206 return false;
207 ++RI;
208 assert(RI != RN && "Strings not the same length after all?");
209 LR = LR.substr(RR.size());
210 RR = R[RI].Data;
211 }
212 }
213}
214
215static std::pair<FileID, llvm::StringRef::size_type>
216FindMacro(const PCHPredefinesBlocks &Buffers, llvm::StringRef MacroDef) {
217 std::pair<FileID, llvm::StringRef::size_type> Res;
218 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
219 Res.second = Buffers[I].Data.find(MacroDef);
220 if (Res.second != llvm::StringRef::npos) {
221 Res.first = Buffers[I].BufferID;
222 break;
223 }
224 }
225 return Res;
226}
227
228bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000229 llvm::StringRef OriginalFileName,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000230 std::string &SuggestedPredefines) {
Daniel Dunbar732ef8a2009-11-11 23:58:53 +0000231 // We are in the context of an implicit include, so the predefines buffer will
232 // have a #include entry for the PCH file itself (as normalized by the
233 // preprocessor initialization). Find it and skip over it in the checking
234 // below.
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000235 llvm::SmallString<256> PCHInclude;
236 PCHInclude += "#include \"";
Daniel Dunbar732ef8a2009-11-11 23:58:53 +0000237 PCHInclude += NormalizeDashIncludePath(OriginalFileName);
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000238 PCHInclude += "\"\n";
239 std::pair<llvm::StringRef,llvm::StringRef> Split =
240 llvm::StringRef(PP.getPredefines()).split(PCHInclude.str());
241 llvm::StringRef Left = Split.first, Right = Split.second;
Ted Kremenek1ff615c2010-03-18 00:56:54 +0000242 if (Left == PP.getPredefines()) {
243 Error("Missing PCH include entry!");
244 return true;
245 }
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000246
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000247 // If the concatenation of all the PCH buffers is equal to the adjusted
248 // command line, we're done.
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000249 llvm::SmallVector<llvm::StringRef, 2> CommandLine;
250 CommandLine.push_back(Left);
251 CommandLine.push_back(Right);
252 if (EqualConcatenations(CommandLine, Buffers))
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000253 return false;
254
255 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump11289f42009-09-09 15:08:12 +0000256
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000257 // The predefines buffers are different. Determine what the differences are,
258 // and whether they require us to reject the PCH file.
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000259 llvm::SmallVector<llvm::StringRef, 8> PCHLines;
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000260 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
261 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000262
263 llvm::SmallVector<llvm::StringRef, 8> CmdLineLines;
264 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
265 Right.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000266
Daniel Dunbar499baed2009-11-11 05:26:28 +0000267 // Sort both sets of predefined buffer lines, since we allow some extra
268 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000269 std::sort(CmdLineLines.begin(), CmdLineLines.end());
270 std::sort(PCHLines.begin(), PCHLines.end());
271
Daniel Dunbar499baed2009-11-11 05:26:28 +0000272 // Determine which predefines that were used to build the PCH file are missing
273 // from the command line.
274 std::vector<llvm::StringRef> MissingPredefines;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000275 std::set_difference(PCHLines.begin(), PCHLines.end(),
276 CmdLineLines.begin(), CmdLineLines.end(),
277 std::back_inserter(MissingPredefines));
278
279 bool MissingDefines = false;
280 bool ConflictingDefines = false;
281 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Daniel Dunbar499baed2009-11-11 05:26:28 +0000282 llvm::StringRef Missing = MissingPredefines[I];
283 if (!Missing.startswith("#define ")) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000284 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
285 return true;
286 }
Mike Stump11289f42009-09-09 15:08:12 +0000287
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000288 // This is a macro definition. Determine the name of the macro we're
289 // defining.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000290 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump11289f42009-09-09 15:08:12 +0000291 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000292 = Missing.find_first_of("( \n\r", StartOfMacroName);
293 assert(EndOfMacroName != std::string::npos &&
294 "Couldn't find the end of the macro name");
Daniel Dunbar499baed2009-11-11 05:26:28 +0000295 llvm::StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000296
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000297 // Determine whether this macro was given a different definition on the
298 // command line.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000299 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000300 std::string::size_type MacroDefLen = MacroDefStart.size();
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000301 llvm::SmallVector<llvm::StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000302 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
303 MacroDefStart);
304 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000305 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000306 // Different macro; we're done.
307 ConflictPos = CmdLineLines.end();
Mike Stump11289f42009-09-09 15:08:12 +0000308 break;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000309 }
Mike Stump11289f42009-09-09 15:08:12 +0000310
311 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000312 "Invalid #define in predefines buffer?");
Mike Stump11289f42009-09-09 15:08:12 +0000313 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000314 (*ConflictPos)[MacroDefLen] != '(')
315 continue; // Longer macro name; keep trying.
Mike Stump11289f42009-09-09 15:08:12 +0000316
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000317 // We found a conflicting macro definition.
318 break;
319 }
Mike Stump11289f42009-09-09 15:08:12 +0000320
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000321 if (ConflictPos != CmdLineLines.end()) {
322 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
323 << MacroName;
324
325 // Show the definition of this macro within the PCH file.
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000326 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
327 FindMacro(Buffers, Missing);
328 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
329 SourceLocation PCHMissingLoc =
330 SourceMgr.getLocForStartOfFile(MacroLoc.first)
331 .getFileLocWithOffset(MacroLoc.second);
Daniel Dunbar499baed2009-11-11 05:26:28 +0000332 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000333
334 ConflictingDefines = true;
335 continue;
336 }
Mike Stump11289f42009-09-09 15:08:12 +0000337
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000338 // If the macro doesn't conflict, then we'll just pick up the macro
339 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000340 if (ConflictingDefines)
341 continue; // Don't complain if there are already conflicting defs
Mike Stump11289f42009-09-09 15:08:12 +0000342
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000343 if (!MissingDefines) {
344 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
345 MissingDefines = true;
346 }
347
348 // Show the definition of this macro within the PCH file.
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000349 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
350 FindMacro(Buffers, Missing);
351 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
352 SourceLocation PCHMissingLoc =
353 SourceMgr.getLocForStartOfFile(MacroLoc.first)
354 .getFileLocWithOffset(MacroLoc.second);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000355 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
356 }
Mike Stump11289f42009-09-09 15:08:12 +0000357
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000358 if (ConflictingDefines)
359 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000360
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000361 // Determine what predefines were introduced based on command-line
362 // parameters that were not present when building the PCH
363 // file. Extra #defines are okay, so long as the identifiers being
364 // defined were not used within the precompiled header.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000365 std::vector<llvm::StringRef> ExtraPredefines;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000366 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
367 PCHLines.begin(), PCHLines.end(),
Mike Stump11289f42009-09-09 15:08:12 +0000368 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000369 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Daniel Dunbar499baed2009-11-11 05:26:28 +0000370 llvm::StringRef &Extra = ExtraPredefines[I];
371 if (!Extra.startswith("#define ")) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000372 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
373 return true;
374 }
375
376 // This is an extra macro definition. Determine the name of the
377 // macro we're defining.
378 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump11289f42009-09-09 15:08:12 +0000379 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000380 = Extra.find_first_of("( \n\r", StartOfMacroName);
381 assert(EndOfMacroName != std::string::npos &&
382 "Couldn't find the end of the macro name");
Daniel Dunbar499baed2009-11-11 05:26:28 +0000383 llvm::StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000384
385 // Check whether this name was used somewhere in the PCH file. If
386 // so, defining it as a macro could change behavior, so we reject
387 // the PCH file.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000388 if (IdentifierInfo *II = Reader.get(MacroName)) {
Daniel Dunbar045c92f2009-11-11 00:52:00 +0000389 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000390 return true;
391 }
392
393 // Add this definition to the suggested predefines buffer.
394 SuggestedPredefines += Extra;
395 SuggestedPredefines += '\n';
396 }
397
398 // If we get here, it's because the predefines buffer had compatible
399 // contents. Accept the PCH file.
400 return false;
401}
402
Douglas Gregor5712ebc2010-03-16 16:35:32 +0000403void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
404 unsigned ID) {
405 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
406 ++NumHeaderInfos;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000407}
408
409void PCHValidator::ReadCounter(unsigned Value) {
410 PP.setCounterValue(Value);
411}
412
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000413//===----------------------------------------------------------------------===//
Sebastian Redl2c499f62010-08-18 23:56:43 +0000414// AST reader implementation
Douglas Gregora868bbd2009-04-21 22:25:48 +0000415//===----------------------------------------------------------------------===//
416
Sebastian Redl07a89a82010-07-30 00:29:29 +0000417void
Sebastian Redl3e31c722010-08-18 23:56:56 +0000418ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redl07a89a82010-07-30 00:29:29 +0000419 DeserializationListener = Listener;
420 if (DeserializationListener)
421 DeserializationListener->SetReader(this);
422}
423
Chris Lattner92ba5ff2009-04-27 05:14:47 +0000424
Douglas Gregora868bbd2009-04-21 22:25:48 +0000425namespace {
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000426class ASTSelectorLookupTrait {
Sebastian Redl2c499f62010-08-18 23:56:43 +0000427 ASTReader &Reader;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000428
429public:
Sebastian Redl834bb972010-08-04 17:20:04 +0000430 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +0000431 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +0000432 ObjCMethodList Instance, Factory;
433 };
Douglas Gregorc78d3462009-04-24 21:10:55 +0000434
435 typedef Selector external_key_type;
436 typedef external_key_type internal_key_type;
437
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000438 explicit ASTSelectorLookupTrait(ASTReader &Reader) : Reader(Reader) { }
Mike Stump11289f42009-09-09 15:08:12 +0000439
Douglas Gregorc78d3462009-04-24 21:10:55 +0000440 static bool EqualKey(const internal_key_type& a,
441 const internal_key_type& b) {
442 return a == b;
443 }
Mike Stump11289f42009-09-09 15:08:12 +0000444
Douglas Gregorc78d3462009-04-24 21:10:55 +0000445 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +0000446 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000447 }
Mike Stump11289f42009-09-09 15:08:12 +0000448
Douglas Gregorc78d3462009-04-24 21:10:55 +0000449 // This hopefully will just get inlined and removed by the optimizer.
450 static const internal_key_type&
451 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump11289f42009-09-09 15:08:12 +0000452
Douglas Gregorc78d3462009-04-24 21:10:55 +0000453 static std::pair<unsigned, unsigned>
454 ReadKeyDataLength(const unsigned char*& d) {
455 using namespace clang::io;
456 unsigned KeyLen = ReadUnalignedLE16(d);
457 unsigned DataLen = ReadUnalignedLE16(d);
458 return std::make_pair(KeyLen, DataLen);
459 }
Mike Stump11289f42009-09-09 15:08:12 +0000460
Douglas Gregor95c13f52009-04-25 17:48:32 +0000461 internal_key_type ReadKey(const unsigned char* d, unsigned) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000462 using namespace clang::io;
Chris Lattner8575daa2009-04-27 21:45:14 +0000463 SelectorTable &SelTable = Reader.getContext()->Selectors;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000464 unsigned N = ReadUnalignedLE16(d);
Mike Stump11289f42009-09-09 15:08:12 +0000465 IdentifierInfo *FirstII
Douglas Gregorc78d3462009-04-24 21:10:55 +0000466 = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
467 if (N == 0)
468 return SelTable.getNullarySelector(FirstII);
469 else if (N == 1)
470 return SelTable.getUnarySelector(FirstII);
471
472 llvm::SmallVector<IdentifierInfo *, 16> Args;
473 Args.push_back(FirstII);
474 for (unsigned I = 1; I != N; ++I)
475 Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)));
476
Douglas Gregor038c3382009-05-22 22:45:36 +0000477 return SelTable.getSelector(N, Args.data());
Douglas Gregorc78d3462009-04-24 21:10:55 +0000478 }
Mike Stump11289f42009-09-09 15:08:12 +0000479
Douglas Gregorc78d3462009-04-24 21:10:55 +0000480 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
481 using namespace clang::io;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000482
483 data_type Result;
484
Sebastian Redl834bb972010-08-04 17:20:04 +0000485 Result.ID = ReadUnalignedLE32(d);
486 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
487 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
488
Douglas Gregorc78d3462009-04-24 21:10:55 +0000489 // Load instance methods
490 ObjCMethodList *Prev = 0;
491 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000492 ObjCMethodDecl *Method
Douglas Gregorc78d3462009-04-24 21:10:55 +0000493 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl834bb972010-08-04 17:20:04 +0000494 if (!Result.Instance.Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000495 // This is the first method, which is the easy case.
Sebastian Redl834bb972010-08-04 17:20:04 +0000496 Result.Instance.Method = Method;
497 Prev = &Result.Instance;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000498 continue;
499 }
500
Ted Kremenekda4abf12010-02-11 00:53:01 +0000501 ObjCMethodList *Mem =
502 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
503 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000504 Prev = Prev->Next;
505 }
506
507 // Load factory methods
508 Prev = 0;
509 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000510 ObjCMethodDecl *Method
Douglas Gregorc78d3462009-04-24 21:10:55 +0000511 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl834bb972010-08-04 17:20:04 +0000512 if (!Result.Factory.Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000513 // This is the first method, which is the easy case.
Sebastian Redl834bb972010-08-04 17:20:04 +0000514 Result.Factory.Method = Method;
515 Prev = &Result.Factory;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000516 continue;
517 }
518
Ted Kremenekda4abf12010-02-11 00:53:01 +0000519 ObjCMethodList *Mem =
520 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
521 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000522 Prev = Prev->Next;
523 }
524
525 return Result;
526 }
527};
Mike Stump11289f42009-09-09 15:08:12 +0000528
529} // end anonymous namespace
Douglas Gregorc78d3462009-04-24 21:10:55 +0000530
531/// \brief The on-disk hash table used for the global method pool.
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000532typedef OnDiskChainedHashTable<ASTSelectorLookupTrait>
533 ASTSelectorLookupTable;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000534
535namespace {
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000536class ASTIdentifierLookupTrait {
Sebastian Redl2c499f62010-08-18 23:56:43 +0000537 ASTReader &Reader;
Sebastian Redl4e6c5672010-07-21 22:31:37 +0000538 llvm::BitstreamCursor &Stream;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000539
540 // If we know the IdentifierInfo in advance, it is here and we will
541 // not build a new one. Used when deserializing information about an
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000542 // identifier that was constructed before the AST file was read.
Douglas Gregora868bbd2009-04-21 22:25:48 +0000543 IdentifierInfo *KnownII;
544
545public:
546 typedef IdentifierInfo * data_type;
547
548 typedef const std::pair<const char*, unsigned> external_key_type;
549
550 typedef external_key_type internal_key_type;
551
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000552 ASTIdentifierLookupTrait(ASTReader &Reader, llvm::BitstreamCursor &Stream,
Sebastian Redl4e6c5672010-07-21 22:31:37 +0000553 IdentifierInfo *II = 0)
554 : Reader(Reader), Stream(Stream), KnownII(II) { }
Mike Stump11289f42009-09-09 15:08:12 +0000555
Douglas Gregora868bbd2009-04-21 22:25:48 +0000556 static bool EqualKey(const internal_key_type& a,
557 const internal_key_type& b) {
558 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
559 : false;
560 }
Mike Stump11289f42009-09-09 15:08:12 +0000561
Douglas Gregora868bbd2009-04-21 22:25:48 +0000562 static unsigned ComputeHash(const internal_key_type& a) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000563 return llvm::HashString(llvm::StringRef(a.first, a.second));
Douglas Gregora868bbd2009-04-21 22:25:48 +0000564 }
Mike Stump11289f42009-09-09 15:08:12 +0000565
Douglas Gregora868bbd2009-04-21 22:25:48 +0000566 // This hopefully will just get inlined and removed by the optimizer.
567 static const internal_key_type&
568 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump11289f42009-09-09 15:08:12 +0000569
Douglas Gregora868bbd2009-04-21 22:25:48 +0000570 static std::pair<unsigned, unsigned>
571 ReadKeyDataLength(const unsigned char*& d) {
572 using namespace clang::io;
Douglas Gregor6b7bf5a2009-04-25 20:26:24 +0000573 unsigned DataLen = ReadUnalignedLE16(d);
Douglas Gregor5287b4e2009-04-25 21:04:17 +0000574 unsigned KeyLen = ReadUnalignedLE16(d);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000575 return std::make_pair(KeyLen, DataLen);
576 }
Mike Stump11289f42009-09-09 15:08:12 +0000577
Douglas Gregora868bbd2009-04-21 22:25:48 +0000578 static std::pair<const char*, unsigned>
579 ReadKey(const unsigned char* d, unsigned n) {
580 assert(n >= 2 && d[n-1] == '\0');
581 return std::make_pair((const char*) d, n-1);
582 }
Mike Stump11289f42009-09-09 15:08:12 +0000583
584 IdentifierInfo *ReadData(const internal_key_type& k,
Douglas Gregora868bbd2009-04-21 22:25:48 +0000585 const unsigned char* d,
586 unsigned DataLen) {
587 using namespace clang::io;
Sebastian Redl539c5062010-08-18 23:57:32 +0000588 IdentID ID = ReadUnalignedLE32(d);
Douglas Gregor1d583f22009-04-28 21:18:29 +0000589 bool IsInteresting = ID & 0x01;
590
591 // Wipe out the "is interesting" bit.
592 ID = ID >> 1;
593
594 if (!IsInteresting) {
Sebastian Redl98912122010-07-27 23:01:28 +0000595 // For uninteresting identifiers, just build the IdentifierInfo
Douglas Gregor1d583f22009-04-28 21:18:29 +0000596 // and associate it with the persistent ID.
597 IdentifierInfo *II = KnownII;
598 if (!II)
Sebastian Redl07a89a82010-07-30 00:29:29 +0000599 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregor1d583f22009-04-28 21:18:29 +0000600 Reader.SetIdentifierInfo(ID, II);
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000601 II->setIsFromAST();
Douglas Gregor1d583f22009-04-28 21:18:29 +0000602 return II;
603 }
604
Douglas Gregorb9256522009-04-28 21:32:13 +0000605 unsigned Bits = ReadUnalignedLE16(d);
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000606 bool CPlusPlusOperatorKeyword = Bits & 0x01;
607 Bits >>= 1;
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +0000608 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
609 Bits >>= 1;
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000610 bool Poisoned = Bits & 0x01;
611 Bits >>= 1;
612 bool ExtensionToken = Bits & 0x01;
613 Bits >>= 1;
614 bool hasMacroDefinition = Bits & 0x01;
615 Bits >>= 1;
616 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
617 Bits >>= 10;
Mike Stump11289f42009-09-09 15:08:12 +0000618
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000619 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregorb9256522009-04-28 21:32:13 +0000620 DataLen -= 6;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000621
622 // Build the IdentifierInfo itself and link the identifier ID with
623 // the new IdentifierInfo.
624 IdentifierInfo *II = KnownII;
625 if (!II)
Sebastian Redl07a89a82010-07-30 00:29:29 +0000626 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000627 Reader.SetIdentifierInfo(ID, II);
628
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000629 // Set or check the various bits in the IdentifierInfo structure.
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +0000630 // Token IDs are read-only.
631 if (HasRevertedTokenIDToIdentifier)
632 II->RevertTokenIDToIdentifier();
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000633 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
Mike Stump11289f42009-09-09 15:08:12 +0000634 assert(II->isExtensionToken() == ExtensionToken &&
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000635 "Incorrect extension token flag");
636 (void)ExtensionToken;
637 II->setIsPoisoned(Poisoned);
638 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
639 "Incorrect C++ operator keyword flag");
640 (void)CPlusPlusOperatorKeyword;
641
Douglas Gregorc3366a52009-04-21 23:56:24 +0000642 // If this identifier is a macro, deserialize the macro
643 // definition.
644 if (hasMacroDefinition) {
Douglas Gregorb9256522009-04-28 21:32:13 +0000645 uint32_t Offset = ReadUnalignedLE32(d);
Sebastian Redl4e6c5672010-07-21 22:31:37 +0000646 Reader.ReadMacroRecord(Stream, Offset);
Douglas Gregorb9256522009-04-28 21:32:13 +0000647 DataLen -= 4;
Douglas Gregorc3366a52009-04-21 23:56:24 +0000648 }
Douglas Gregora868bbd2009-04-21 22:25:48 +0000649
650 // Read all of the declarations visible at global scope with this
651 // name.
Chris Lattner1d728882009-04-27 22:17:41 +0000652 if (Reader.getContext() == 0) return II;
Douglas Gregor1342e842009-07-06 18:54:52 +0000653 if (DataLen > 0) {
654 llvm::SmallVector<uint32_t, 4> DeclIDs;
655 for (; DataLen > 0; DataLen -= 4)
656 DeclIDs.push_back(ReadUnalignedLE32(d));
657 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000658 }
Mike Stump11289f42009-09-09 15:08:12 +0000659
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000660 II->setIsFromAST();
Douglas Gregora868bbd2009-04-21 22:25:48 +0000661 return II;
662 }
663};
Mike Stump11289f42009-09-09 15:08:12 +0000664
665} // end anonymous namespace
Douglas Gregora868bbd2009-04-21 22:25:48 +0000666
667/// \brief The on-disk hash table used to contain information about
668/// all of the identifiers in the program.
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000669typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait>
670 ASTIdentifierLookupTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000671
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000672namespace {
673class ASTDeclContextNameLookupTrait {
674 ASTReader &Reader;
675
676public:
677 /// \brief Pair of begin/end iterators for DeclIDs.
678 typedef std::pair<DeclID *, DeclID *> data_type;
679
680 /// \brief Special internal key for declaration names.
681 /// The hash table creates keys for comparison; we do not create
682 /// a DeclarationName for the internal key to avoid deserializing types.
683 struct DeclNameKey {
684 DeclarationName::NameKind Kind;
685 uint64_t Data;
686 DeclNameKey() : Kind((DeclarationName::NameKind)0), Data(0) { }
687 };
688
689 typedef DeclarationName external_key_type;
690 typedef DeclNameKey internal_key_type;
691
692 explicit ASTDeclContextNameLookupTrait(ASTReader &Reader) : Reader(Reader) { }
693
694 static bool EqualKey(const internal_key_type& a,
695 const internal_key_type& b) {
696 return a.Kind == b.Kind && a.Data == b.Data;
697 }
698
699 unsigned ComputeHash(const DeclNameKey &Key) const {
700 llvm::FoldingSetNodeID ID;
701 ID.AddInteger(Key.Kind);
702
703 switch (Key.Kind) {
704 case DeclarationName::Identifier:
705 case DeclarationName::CXXLiteralOperatorName:
706 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
707 break;
708 case DeclarationName::ObjCZeroArgSelector:
709 case DeclarationName::ObjCOneArgSelector:
710 case DeclarationName::ObjCMultiArgSelector:
711 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
712 break;
713 case DeclarationName::CXXConstructorName:
714 case DeclarationName::CXXDestructorName:
715 case DeclarationName::CXXConversionFunctionName:
716 ID.AddInteger((TypeID)Key.Data);
717 break;
718 case DeclarationName::CXXOperatorName:
719 ID.AddInteger((OverloadedOperatorKind)Key.Data);
720 break;
721 case DeclarationName::CXXUsingDirective:
722 break;
723 }
724
725 return ID.ComputeHash();
726 }
727
728 internal_key_type GetInternalKey(const external_key_type& Name) const {
729 DeclNameKey Key;
730 Key.Kind = Name.getNameKind();
731 switch (Name.getNameKind()) {
732 case DeclarationName::Identifier:
733 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
734 break;
735 case DeclarationName::ObjCZeroArgSelector:
736 case DeclarationName::ObjCOneArgSelector:
737 case DeclarationName::ObjCMultiArgSelector:
738 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
739 break;
740 case DeclarationName::CXXConstructorName:
741 case DeclarationName::CXXDestructorName:
742 case DeclarationName::CXXConversionFunctionName:
743 Key.Data = Reader.GetTypeID(Name.getCXXNameType());
744 break;
745 case DeclarationName::CXXOperatorName:
746 Key.Data = Name.getCXXOverloadedOperator();
747 break;
748 case DeclarationName::CXXLiteralOperatorName:
749 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
750 break;
751 case DeclarationName::CXXUsingDirective:
752 break;
753 }
754
755 return Key;
756 }
757
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +0000758 external_key_type GetExternalKey(const internal_key_type& Key) const {
759 ASTContext *Context = Reader.getContext();
760 switch (Key.Kind) {
761 case DeclarationName::Identifier:
762 return DeclarationName((IdentifierInfo*)Key.Data);
763
764 case DeclarationName::ObjCZeroArgSelector:
765 case DeclarationName::ObjCOneArgSelector:
766 case DeclarationName::ObjCMultiArgSelector:
767 return DeclarationName(Selector(Key.Data));
768
769 case DeclarationName::CXXConstructorName:
770 return Context->DeclarationNames.getCXXConstructorName(
771 Context->getCanonicalType(Reader.GetType(Key.Data)));
772
773 case DeclarationName::CXXDestructorName:
774 return Context->DeclarationNames.getCXXDestructorName(
775 Context->getCanonicalType(Reader.GetType(Key.Data)));
776
777 case DeclarationName::CXXConversionFunctionName:
778 return Context->DeclarationNames.getCXXConversionFunctionName(
779 Context->getCanonicalType(Reader.GetType(Key.Data)));
780
781 case DeclarationName::CXXOperatorName:
782 return Context->DeclarationNames.getCXXOperatorName(
783 (OverloadedOperatorKind)Key.Data);
784
785 case DeclarationName::CXXLiteralOperatorName:
786 return Context->DeclarationNames.getCXXLiteralOperatorName(
787 (IdentifierInfo*)Key.Data);
788
789 case DeclarationName::CXXUsingDirective:
790 return DeclarationName::getUsingDirectiveName();
791 }
792
793 llvm_unreachable("Invalid Name Kind ?");
794 }
795
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000796 static std::pair<unsigned, unsigned>
797 ReadKeyDataLength(const unsigned char*& d) {
798 using namespace clang::io;
799 unsigned KeyLen = ReadUnalignedLE16(d);
800 unsigned DataLen = ReadUnalignedLE16(d);
801 return std::make_pair(KeyLen, DataLen);
802 }
803
804 internal_key_type ReadKey(const unsigned char* d, unsigned) {
805 using namespace clang::io;
806
807 DeclNameKey Key;
808 Key.Kind = (DeclarationName::NameKind)*d++;
809 switch (Key.Kind) {
810 case DeclarationName::Identifier:
811 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
812 break;
813 case DeclarationName::ObjCZeroArgSelector:
814 case DeclarationName::ObjCOneArgSelector:
815 case DeclarationName::ObjCMultiArgSelector:
816 Key.Data =
817 (uint64_t)Reader.DecodeSelector(ReadUnalignedLE32(d)).getAsOpaquePtr();
818 break;
819 case DeclarationName::CXXConstructorName:
820 case DeclarationName::CXXDestructorName:
821 case DeclarationName::CXXConversionFunctionName:
822 Key.Data = ReadUnalignedLE32(d); // TypeID
823 break;
824 case DeclarationName::CXXOperatorName:
825 Key.Data = *d++; // OverloadedOperatorKind
826 break;
827 case DeclarationName::CXXLiteralOperatorName:
828 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
829 break;
830 case DeclarationName::CXXUsingDirective:
831 break;
832 }
833
834 return Key;
835 }
836
837 data_type ReadData(internal_key_type, const unsigned char* d,
838 unsigned DataLen) {
839 using namespace clang::io;
840 unsigned NumDecls = ReadUnalignedLE16(d);
841 DeclID *Start = (DeclID *)d;
842 return std::make_pair(Start, Start + NumDecls);
843 }
844};
845
846} // end anonymous namespace
847
848/// \brief The on-disk hash table used for the DeclContext's Name lookup table.
849typedef OnDiskChainedHashTable<ASTDeclContextNameLookupTrait>
850 ASTDeclContextNameLookupTable;
851
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000852bool ASTReader::ReadDeclContextStorage(llvm::BitstreamCursor &Cursor,
853 const std::pair<uint64_t, uint64_t> &Offsets,
854 DeclContextInfo &Info) {
855 SavedStreamPosition SavedPosition(Cursor);
856 // First the lexical decls.
857 if (Offsets.first != 0) {
858 Cursor.JumpToBit(Offsets.first);
859
860 RecordData Record;
861 const char *Blob;
862 unsigned BlobLen;
863 unsigned Code = Cursor.ReadCode();
864 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
865 if (RecCode != DECL_CONTEXT_LEXICAL) {
866 Error("Expected lexical block");
867 return true;
868 }
869
870 Info.LexicalDecls = reinterpret_cast<const DeclID*>(Blob);
871 Info.NumLexicalDecls = BlobLen / sizeof(DeclID);
872 } else {
873 Info.LexicalDecls = 0;
874 Info.NumLexicalDecls = 0;
875 }
876
877 // Now the lookup table.
878 if (Offsets.second != 0) {
879 Cursor.JumpToBit(Offsets.second);
880
881 RecordData Record;
882 const char *Blob;
883 unsigned BlobLen;
884 unsigned Code = Cursor.ReadCode();
885 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
886 if (RecCode != DECL_CONTEXT_VISIBLE) {
887 Error("Expected visible lookup table block");
888 return true;
889 }
890 Info.NameLookupTableData
891 = ASTDeclContextNameLookupTable::Create(
892 (const unsigned char *)Blob + Record[0],
893 (const unsigned char *)Blob,
894 ASTDeclContextNameLookupTrait(*this));
Sebastian Redl9d8f58b2010-08-24 00:50:00 +0000895 } else {
896 Info.NameLookupTableData = 0;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000897 }
898
899 return false;
900}
901
Sebastian Redl2c499f62010-08-18 23:56:43 +0000902void ASTReader::Error(const char *Msg) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +0000903 Diag(diag::err_fe_pch_malformed) << Msg;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000904}
905
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000906/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redl2c499f62010-08-18 23:56:43 +0000907bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000908 if (Listener)
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000909 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000910 ActualOriginalFileName,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000911 SuggestedPredefines);
Douglas Gregorc379c072009-04-28 18:58:38 +0000912 return false;
Douglas Gregor92863e42009-04-10 23:10:45 +0000913}
914
Douglas Gregorc5046832009-04-27 18:38:38 +0000915//===----------------------------------------------------------------------===//
916// Source Manager Deserialization
917//===----------------------------------------------------------------------===//
918
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000919/// \brief Read the line table in the source manager block.
920/// \returns true if ther was an error.
Sebastian Redl2c499f62010-08-18 23:56:43 +0000921bool ASTReader::ParseLineTable(llvm::SmallVectorImpl<uint64_t> &Record) {
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000922 unsigned Idx = 0;
923 LineTableInfo &LineTable = SourceMgr.getLineTable();
924
925 // Parse the file names
Douglas Gregora8854652009-04-13 17:12:42 +0000926 std::map<int, int> FileIDs;
927 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000928 // Extract the file name
929 unsigned FilenameLen = Record[Idx++];
930 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
931 Idx += FilenameLen;
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000932 MaybeAddSystemRootToFilename(Filename);
Mike Stump11289f42009-09-09 15:08:12 +0000933 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
Douglas Gregora8854652009-04-13 17:12:42 +0000934 Filename.size());
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000935 }
936
937 // Parse the line entries
938 std::vector<LineEntry> Entries;
939 while (Idx < Record.size()) {
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000940 int FID = Record[Idx++];
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000941
942 // Extract the line entries
943 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000944 assert(NumEntries && "Numentries is 00000");
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000945 Entries.clear();
946 Entries.reserve(NumEntries);
947 for (unsigned I = 0; I != NumEntries; ++I) {
948 unsigned FileOffset = Record[Idx++];
949 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000950 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump11289f42009-09-09 15:08:12 +0000951 SrcMgr::CharacteristicKind FileKind
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000952 = (SrcMgr::CharacteristicKind)Record[Idx++];
953 unsigned IncludeOffset = Record[Idx++];
954 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
955 FileKind, IncludeOffset));
956 }
957 LineTable.AddEntry(FID, Entries);
958 }
959
960 return false;
961}
962
Douglas Gregorc5046832009-04-27 18:38:38 +0000963namespace {
964
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000965class ASTStatData {
Douglas Gregorc5046832009-04-27 18:38:38 +0000966public:
967 const bool hasStat;
968 const ino_t ino;
969 const dev_t dev;
970 const mode_t mode;
971 const time_t mtime;
972 const off_t size;
Mike Stump11289f42009-09-09 15:08:12 +0000973
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000974 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Mike Stump11289f42009-09-09 15:08:12 +0000975 : hasStat(true), ino(i), dev(d), mode(mo), mtime(m), size(s) {}
976
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000977 ASTStatData()
Douglas Gregorc5046832009-04-27 18:38:38 +0000978 : hasStat(false), ino(0), dev(0), mode(0), mtime(0), size(0) {}
979};
980
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000981class ASTStatLookupTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +0000982 public:
983 typedef const char *external_key_type;
984 typedef const char *internal_key_type;
985
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000986 typedef ASTStatData data_type;
Douglas Gregorc5046832009-04-27 18:38:38 +0000987
988 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000989 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +0000990 }
991
992 static internal_key_type GetInternalKey(const char *path) { return path; }
993
994 static bool EqualKey(internal_key_type a, internal_key_type b) {
995 return strcmp(a, b) == 0;
996 }
997
998 static std::pair<unsigned, unsigned>
999 ReadKeyDataLength(const unsigned char*& d) {
1000 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1001 unsigned DataLen = (unsigned) *d++;
1002 return std::make_pair(KeyLen + 1, DataLen);
1003 }
1004
1005 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1006 return (const char *)d;
1007 }
1008
1009 static data_type ReadData(const internal_key_type, const unsigned char *d,
1010 unsigned /*DataLen*/) {
1011 using namespace clang::io;
1012
1013 if (*d++ == 1)
1014 return data_type();
1015
1016 ino_t ino = (ino_t) ReadUnalignedLE32(d);
1017 dev_t dev = (dev_t) ReadUnalignedLE32(d);
1018 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump11289f42009-09-09 15:08:12 +00001019 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregorc5046832009-04-27 18:38:38 +00001020 off_t size = (off_t) ReadUnalignedLE64(d);
1021 return data_type(ino, dev, mode, mtime, size);
1022 }
1023};
1024
1025/// \brief stat() cache for precompiled headers.
1026///
1027/// This cache is very similar to the stat cache used by pretokenized
1028/// headers.
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001029class ASTStatCache : public StatSysCallCache {
1030 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregorc5046832009-04-27 18:38:38 +00001031 CacheTy *Cache;
1032
1033 unsigned &NumStatHits, &NumStatMisses;
Mike Stump11289f42009-09-09 15:08:12 +00001034public:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001035 ASTStatCache(const unsigned char *Buckets,
Douglas Gregorc5046832009-04-27 18:38:38 +00001036 const unsigned char *Base,
1037 unsigned &NumStatHits,
Mike Stump11289f42009-09-09 15:08:12 +00001038 unsigned &NumStatMisses)
Douglas Gregorc5046832009-04-27 18:38:38 +00001039 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
1040 Cache = CacheTy::Create(Buckets, Base);
1041 }
1042
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001043 ~ASTStatCache() { delete Cache; }
Mike Stump11289f42009-09-09 15:08:12 +00001044
Douglas Gregorc5046832009-04-27 18:38:38 +00001045 int stat(const char *path, struct stat *buf) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001046 // Do the lookup for the file's data in the AST file.
Douglas Gregorc5046832009-04-27 18:38:38 +00001047 CacheTy::iterator I = Cache->find(path);
1048
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001049 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregorc5046832009-04-27 18:38:38 +00001050 if (I == Cache->end()) {
1051 ++NumStatMisses;
Douglas Gregord2eb58a2009-10-16 18:18:30 +00001052 return StatSysCallCache::stat(path, buf);
Douglas Gregorc5046832009-04-27 18:38:38 +00001053 }
Mike Stump11289f42009-09-09 15:08:12 +00001054
Douglas Gregorc5046832009-04-27 18:38:38 +00001055 ++NumStatHits;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001056 ASTStatData Data = *I;
Mike Stump11289f42009-09-09 15:08:12 +00001057
Douglas Gregorc5046832009-04-27 18:38:38 +00001058 if (!Data.hasStat)
1059 return 1;
1060
1061 buf->st_ino = Data.ino;
1062 buf->st_dev = Data.dev;
1063 buf->st_mtime = Data.mtime;
1064 buf->st_mode = Data.mode;
1065 buf->st_size = Data.size;
1066 return 0;
1067 }
1068};
1069} // end anonymous namespace
1070
1071
Sebastian Redl393f8b72010-07-19 20:52:06 +00001072/// \brief Read a source manager block
Sebastian Redl2c499f62010-08-18 23:56:43 +00001073ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(PerFileData &F) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001074 using namespace SrcMgr;
Douglas Gregor258ae542009-04-27 06:38:32 +00001075
Sebastian Redl393f8b72010-07-19 20:52:06 +00001076 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001077
Douglas Gregor258ae542009-04-27 06:38:32 +00001078 // Set the source-location entry cursor to the current position in
1079 // the stream. This cursor will be used to read the contents of the
1080 // source manager block initially, and then lazily read
1081 // source-location entries as needed.
Sebastian Redl393f8b72010-07-19 20:52:06 +00001082 SLocEntryCursor = F.Stream;
Douglas Gregor258ae542009-04-27 06:38:32 +00001083
1084 // The stream itself is going to skip over the source manager block.
Sebastian Redl393f8b72010-07-19 20:52:06 +00001085 if (F.Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001086 Error("malformed block record in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001087 return Failure;
1088 }
1089
1090 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001091 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001092 Error("malformed source manager block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001093 return Failure;
1094 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001095
Douglas Gregora7f71a92009-04-10 03:52:48 +00001096 RecordData Record;
1097 while (true) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001098 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregora7f71a92009-04-10 03:52:48 +00001099 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001100 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001101 Error("error at end of Source Manager block in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001102 return Failure;
1103 }
Douglas Gregor92863e42009-04-10 23:10:45 +00001104 return Success;
Douglas Gregora7f71a92009-04-10 03:52:48 +00001105 }
Mike Stump11289f42009-09-09 15:08:12 +00001106
Douglas Gregora7f71a92009-04-10 03:52:48 +00001107 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1108 // No known subblocks, always skip them.
Douglas Gregor258ae542009-04-27 06:38:32 +00001109 SLocEntryCursor.ReadSubBlockID();
1110 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001111 Error("malformed block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001112 return Failure;
1113 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001114 continue;
1115 }
Mike Stump11289f42009-09-09 15:08:12 +00001116
Douglas Gregora7f71a92009-04-10 03:52:48 +00001117 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001118 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregora7f71a92009-04-10 03:52:48 +00001119 continue;
1120 }
Mike Stump11289f42009-09-09 15:08:12 +00001121
Douglas Gregora7f71a92009-04-10 03:52:48 +00001122 // Read a record.
1123 const char *BlobStart;
1124 unsigned BlobLen;
1125 Record.clear();
Douglas Gregor258ae542009-04-27 06:38:32 +00001126 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001127 default: // Default behavior: ignore.
1128 break;
1129
Sebastian Redl539c5062010-08-18 23:57:32 +00001130 case SM_LINE_TABLE:
Sebastian Redlb293a452010-07-20 21:20:32 +00001131 if (ParseLineTable(Record))
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001132 return Failure;
Chris Lattner184e65d2009-04-14 23:22:57 +00001133 break;
Douglas Gregoreda6a892009-04-26 00:07:37 +00001134
Sebastian Redl539c5062010-08-18 23:57:32 +00001135 case SM_SLOC_FILE_ENTRY:
1136 case SM_SLOC_BUFFER_ENTRY:
1137 case SM_SLOC_INSTANTIATION_ENTRY:
Douglas Gregor258ae542009-04-27 06:38:32 +00001138 // Once we hit one of the source location entries, we're done.
1139 return Success;
Douglas Gregora7f71a92009-04-10 03:52:48 +00001140 }
1141 }
1142}
1143
Sebastian Redl06750302010-07-20 21:50:20 +00001144/// \brief Get a cursor that's correctly positioned for reading the source
1145/// location entry with the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001146llvm::BitstreamCursor &ASTReader::SLocCursorForID(unsigned ID) {
Sebastian Redl06750302010-07-20 21:50:20 +00001147 assert(ID != 0 && ID <= TotalNumSLocEntries &&
1148 "SLocCursorForID should only be called for real IDs.");
1149
1150 ID -= 1;
1151 PerFileData *F = 0;
1152 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1153 F = Chain[N - I - 1];
1154 if (ID < F->LocalNumSLocEntries)
1155 break;
1156 ID -= F->LocalNumSLocEntries;
1157 }
1158 assert(F && F->LocalNumSLocEntries > ID && "Chain corrupted");
1159
1160 F->SLocEntryCursor.JumpToBit(F->SLocOffsets[ID]);
1161 return F->SLocEntryCursor;
1162}
1163
Douglas Gregor258ae542009-04-27 06:38:32 +00001164/// \brief Read in the source location entry with the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001165ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(unsigned ID) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001166 if (ID == 0)
1167 return Success;
1168
1169 if (ID > TotalNumSLocEntries) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001170 Error("source location entry ID out-of-range for AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001171 return Failure;
1172 }
1173
Sebastian Redl06750302010-07-20 21:50:20 +00001174 llvm::BitstreamCursor &SLocEntryCursor = SLocCursorForID(ID);
Sebastian Redl34522812010-07-16 17:50:48 +00001175
Douglas Gregor258ae542009-04-27 06:38:32 +00001176 ++NumSLocEntriesRead;
Douglas Gregor258ae542009-04-27 06:38:32 +00001177 unsigned Code = SLocEntryCursor.ReadCode();
1178 if (Code == llvm::bitc::END_BLOCK ||
1179 Code == llvm::bitc::ENTER_SUBBLOCK ||
1180 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001181 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001182 return Failure;
1183 }
1184
Douglas Gregor258ae542009-04-27 06:38:32 +00001185 RecordData Record;
1186 const char *BlobStart;
1187 unsigned BlobLen;
1188 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1189 default:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001190 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001191 return Failure;
1192
Sebastian Redl539c5062010-08-18 23:57:32 +00001193 case SM_SLOC_FILE_ENTRY: {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001194 std::string Filename(BlobStart, BlobStart + BlobLen);
1195 MaybeAddSystemRootToFilename(Filename);
1196 const FileEntry *File = FileMgr.getFile(Filename);
Chris Lattnerd20dc872009-06-15 04:35:16 +00001197 if (File == 0) {
1198 std::string ErrorStr = "could not find file '";
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001199 ErrorStr += Filename;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001200 ErrorStr += "' referenced by AST file";
Chris Lattnerd20dc872009-06-15 04:35:16 +00001201 Error(ErrorStr.c_str());
1202 return Failure;
1203 }
Mike Stump11289f42009-09-09 15:08:12 +00001204
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001205 if (Record.size() < 10) {
Ted Kremenekabb1ddd2010-03-18 21:23:05 +00001206 Error("source location entry is incorrect");
1207 return Failure;
1208 }
1209
Douglas Gregorce3a8292010-07-27 00:27:13 +00001210 if (!DisableValidation &&
1211 ((off_t)Record[4] != File->getSize()
Douglas Gregor08288f22010-04-09 15:54:22 +00001212#if !defined(LLVM_ON_WIN32)
1213 // In our regression testing, the Windows file system seems to
1214 // have inconsistent modification times that sometimes
1215 // erroneously trigger this error-handling path.
Douglas Gregorce3a8292010-07-27 00:27:13 +00001216 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor08288f22010-04-09 15:54:22 +00001217#endif
Douglas Gregorce3a8292010-07-27 00:27:13 +00001218 )) {
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001219 Diag(diag::err_fe_pch_file_modified)
1220 << Filename;
1221 return Failure;
1222 }
1223
Douglas Gregor258ae542009-04-27 06:38:32 +00001224 FileID FID = SourceMgr.createFileID(File,
1225 SourceLocation::getFromRawEncoding(Record[1]),
1226 (SrcMgr::CharacteristicKind)Record[2],
1227 ID, Record[0]);
1228 if (Record[3])
1229 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile())
1230 .setHasLineDirectives();
1231
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001232 // Reconstruct header-search information for this file.
1233 HeaderFileInfo HFI;
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001234 HFI.isImport = Record[6];
1235 HFI.DirInfo = Record[7];
1236 HFI.NumIncludes = Record[8];
1237 HFI.ControllingMacroID = Record[9];
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001238 if (Listener)
1239 Listener->ReadHeaderFileInfo(HFI, File->getUID());
Douglas Gregor258ae542009-04-27 06:38:32 +00001240 break;
1241 }
1242
Sebastian Redl539c5062010-08-18 23:57:32 +00001243 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor258ae542009-04-27 06:38:32 +00001244 const char *Name = BlobStart;
1245 unsigned Offset = Record[0];
1246 unsigned Code = SLocEntryCursor.ReadCode();
1247 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00001248 unsigned RecCode
Douglas Gregor258ae542009-04-27 06:38:32 +00001249 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001250
Sebastian Redl539c5062010-08-18 23:57:32 +00001251 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001252 Error("AST record has invalid code");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001253 return Failure;
1254 }
1255
Douglas Gregor258ae542009-04-27 06:38:32 +00001256 llvm::MemoryBuffer *Buffer
Chris Lattner58c79342010-04-05 22:42:27 +00001257 = llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(BlobStart, BlobLen - 1),
1258 Name);
Douglas Gregor258ae542009-04-27 06:38:32 +00001259 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset);
Mike Stump11289f42009-09-09 15:08:12 +00001260
Douglas Gregore6648fb2009-04-28 20:33:11 +00001261 if (strcmp(Name, "<built-in>") == 0) {
Sebastian Redl75fbb3b2010-07-14 17:49:11 +00001262 PCHPredefinesBlock Block = {
1263 BufferID,
1264 llvm::StringRef(BlobStart, BlobLen - 1)
1265 };
1266 PCHPredefinesBuffers.push_back(Block);
Douglas Gregore6648fb2009-04-28 20:33:11 +00001267 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001268
1269 break;
1270 }
1271
Sebastian Redl539c5062010-08-18 23:57:32 +00001272 case SM_SLOC_INSTANTIATION_ENTRY: {
Mike Stump11289f42009-09-09 15:08:12 +00001273 SourceLocation SpellingLoc
Douglas Gregor258ae542009-04-27 06:38:32 +00001274 = SourceLocation::getFromRawEncoding(Record[1]);
1275 SourceMgr.createInstantiationLoc(SpellingLoc,
1276 SourceLocation::getFromRawEncoding(Record[2]),
1277 SourceLocation::getFromRawEncoding(Record[3]),
1278 Record[4],
1279 ID,
1280 Record[0]);
1281 break;
Mike Stump11289f42009-09-09 15:08:12 +00001282 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001283 }
1284
1285 return Success;
1286}
1287
Chris Lattnere78a6be2009-04-27 01:05:14 +00001288/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1289/// specified cursor. Read the abbreviations that are at the top of the block
1290/// and then leave the cursor pointing into the block.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001291bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattnere78a6be2009-04-27 01:05:14 +00001292 unsigned BlockID) {
1293 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001294 Error("malformed block record in AST file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001295 return Failure;
1296 }
Mike Stump11289f42009-09-09 15:08:12 +00001297
Chris Lattnere78a6be2009-04-27 01:05:14 +00001298 while (true) {
1299 unsigned Code = Cursor.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00001300
Chris Lattnere78a6be2009-04-27 01:05:14 +00001301 // We expect all abbrevs to be at the start of the block.
1302 if (Code != llvm::bitc::DEFINE_ABBREV)
1303 return false;
1304 Cursor.ReadAbbrevRecord();
1305 }
1306}
1307
Sebastian Redl2c499f62010-08-18 23:56:43 +00001308void ASTReader::ReadMacroRecord(llvm::BitstreamCursor &Stream, uint64_t Offset){
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001309 assert(PP && "Forgot to set Preprocessor ?");
Mike Stump11289f42009-09-09 15:08:12 +00001310
Douglas Gregorc3366a52009-04-21 23:56:24 +00001311 // Keep track of where we are in the stream, then jump back there
1312 // after reading this macro.
1313 SavedStreamPosition SavedPosition(Stream);
1314
1315 Stream.JumpToBit(Offset);
1316 RecordData Record;
1317 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1318 MacroInfo *Macro = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001319
Douglas Gregorc3366a52009-04-21 23:56:24 +00001320 while (true) {
1321 unsigned Code = Stream.ReadCode();
1322 switch (Code) {
1323 case llvm::bitc::END_BLOCK:
1324 return;
1325
1326 case llvm::bitc::ENTER_SUBBLOCK:
1327 // No known subblocks, always skip them.
1328 Stream.ReadSubBlockID();
1329 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001330 Error("malformed block record in AST file");
Douglas Gregorc3366a52009-04-21 23:56:24 +00001331 return;
1332 }
1333 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001334
Douglas Gregorc3366a52009-04-21 23:56:24 +00001335 case llvm::bitc::DEFINE_ABBREV:
1336 Stream.ReadAbbrevRecord();
1337 continue;
1338 default: break;
1339 }
1340
1341 // Read a record.
1342 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001343 PreprocessorRecordTypes RecType =
1344 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001345 switch (RecType) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001346 case PP_MACRO_OBJECT_LIKE:
1347 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001348 // If we already have a macro, that means that we've hit the end
1349 // of the definition of the macro we were looking for. We're
1350 // done.
1351 if (Macro)
1352 return;
1353
1354 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1355 if (II == 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001356 Error("macro must have a name in AST file");
Douglas Gregorc3366a52009-04-21 23:56:24 +00001357 return;
1358 }
1359 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
1360 bool isUsed = Record[2];
Mike Stump11289f42009-09-09 15:08:12 +00001361
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001362 MacroInfo *MI = PP->AllocateMacroInfo(Loc);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001363 MI->setIsUsed(isUsed);
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001364 MI->setIsFromAST();
Mike Stump11289f42009-09-09 15:08:12 +00001365
Douglas Gregoraae92242010-03-19 21:51:54 +00001366 unsigned NextIndex = 3;
Sebastian Redl539c5062010-08-18 23:57:32 +00001367 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001368 // Decode function-like macro info.
1369 bool isC99VarArgs = Record[3];
1370 bool isGNUVarArgs = Record[4];
1371 MacroArgs.clear();
1372 unsigned NumArgs = Record[5];
Douglas Gregoraae92242010-03-19 21:51:54 +00001373 NextIndex = 6 + NumArgs;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001374 for (unsigned i = 0; i != NumArgs; ++i)
1375 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1376
1377 // Install function-like macro info.
1378 MI->setIsFunctionLike();
1379 if (isC99VarArgs) MI->setIsC99Varargs();
1380 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor038c3382009-05-22 22:45:36 +00001381 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001382 PP->getPreprocessorAllocator());
Douglas Gregorc3366a52009-04-21 23:56:24 +00001383 }
1384
1385 // Finally, install the macro.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001386 PP->setMacroInfo(II, MI);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001387
1388 // Remember that we saw this macro last so that we add the tokens that
1389 // form its body to it.
1390 Macro = MI;
Douglas Gregoraae92242010-03-19 21:51:54 +00001391
1392 if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) {
1393 // We have a macro definition. Load it now.
1394 PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro,
1395 getMacroDefinition(Record[NextIndex]));
1396 }
1397
Douglas Gregorc3366a52009-04-21 23:56:24 +00001398 ++NumMacrosRead;
1399 break;
1400 }
Mike Stump11289f42009-09-09 15:08:12 +00001401
Sebastian Redl539c5062010-08-18 23:57:32 +00001402 case PP_TOKEN: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001403 // If we see a TOKEN before a PP_MACRO_*, then the file is
1404 // erroneous, just pretend we didn't see this.
1405 if (Macro == 0) break;
Mike Stump11289f42009-09-09 15:08:12 +00001406
Douglas Gregorc3366a52009-04-21 23:56:24 +00001407 Token Tok;
1408 Tok.startToken();
1409 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
1410 Tok.setLength(Record[1]);
1411 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1412 Tok.setIdentifierInfo(II);
1413 Tok.setKind((tok::TokenKind)Record[3]);
1414 Tok.setFlag((Token::TokenFlags)Record[4]);
1415 Macro->AddTokenToBody(Tok);
1416 break;
1417 }
Douglas Gregoraae92242010-03-19 21:51:54 +00001418
Sebastian Redl539c5062010-08-18 23:57:32 +00001419 case PP_MACRO_INSTANTIATION: {
Douglas Gregoraae92242010-03-19 21:51:54 +00001420 // If we already have a macro, that means that we've hit the end
1421 // of the definition of the macro we were looking for. We're
1422 // done.
1423 if (Macro)
1424 return;
1425
1426 if (!PP->getPreprocessingRecord()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001427 Error("missing preprocessing record in AST file");
Douglas Gregoraae92242010-03-19 21:51:54 +00001428 return;
1429 }
1430
1431 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1432 if (PPRec.getPreprocessedEntity(Record[0]))
1433 return;
1434
1435 MacroInstantiation *MI
1436 = new (PPRec) MacroInstantiation(DecodeIdentifierInfo(Record[3]),
1437 SourceRange(
1438 SourceLocation::getFromRawEncoding(Record[1]),
1439 SourceLocation::getFromRawEncoding(Record[2])),
1440 getMacroDefinition(Record[4]));
1441 PPRec.SetPreallocatedEntity(Record[0], MI);
1442 return;
1443 }
1444
Sebastian Redl539c5062010-08-18 23:57:32 +00001445 case PP_MACRO_DEFINITION: {
Douglas Gregoraae92242010-03-19 21:51:54 +00001446 // If we already have a macro, that means that we've hit the end
1447 // of the definition of the macro we were looking for. We're
1448 // done.
1449 if (Macro)
1450 return;
1451
1452 if (!PP->getPreprocessingRecord()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001453 Error("missing preprocessing record in AST file");
Douglas Gregoraae92242010-03-19 21:51:54 +00001454 return;
1455 }
1456
1457 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1458 if (PPRec.getPreprocessedEntity(Record[0]))
1459 return;
1460
1461 if (Record[1] >= MacroDefinitionsLoaded.size()) {
1462 Error("out-of-bounds macro definition record");
1463 return;
1464 }
1465
1466 MacroDefinition *MD
1467 = new (PPRec) MacroDefinition(DecodeIdentifierInfo(Record[4]),
1468 SourceLocation::getFromRawEncoding(Record[5]),
1469 SourceRange(
1470 SourceLocation::getFromRawEncoding(Record[2]),
1471 SourceLocation::getFromRawEncoding(Record[3])));
1472 PPRec.SetPreallocatedEntity(Record[0], MD);
1473 MacroDefinitionsLoaded[Record[1]] = MD;
1474 return;
1475 }
Steve Naroff3fa455a2009-04-24 20:03:17 +00001476 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001477 }
1478}
1479
Sebastian Redl2c499f62010-08-18 23:56:43 +00001480void ASTReader::ReadDefinedMacros() {
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001481 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1482 llvm::BitstreamCursor &MacroCursor = Chain[N - I - 1]->MacroCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001483
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001484 // If there was no preprocessor block, skip this file.
1485 if (!MacroCursor.getBitStreamReader())
1486 continue;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001487
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001488 llvm::BitstreamCursor Cursor = MacroCursor;
Sebastian Redl539c5062010-08-18 23:57:32 +00001489 if (Cursor.EnterSubBlock(PREPROCESSOR_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001490 Error("malformed preprocessor block record in AST file");
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001491 return;
1492 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001493
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001494 RecordData Record;
1495 while (true) {
1496 unsigned Code = Cursor.ReadCode();
1497 if (Code == llvm::bitc::END_BLOCK) {
1498 if (Cursor.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001499 Error("error at end of preprocessor block in AST file");
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001500 return;
1501 }
1502 break;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001503 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001504
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001505 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1506 // No known subblocks, always skip them.
1507 Cursor.ReadSubBlockID();
1508 if (Cursor.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001509 Error("malformed block record in AST file");
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001510 return;
1511 }
1512 continue;
1513 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001514
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001515 if (Code == llvm::bitc::DEFINE_ABBREV) {
1516 Cursor.ReadAbbrevRecord();
1517 continue;
1518 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001519
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001520 // Read a record.
1521 const char *BlobStart;
1522 unsigned BlobLen;
1523 Record.clear();
1524 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1525 default: // Default behavior: ignore.
1526 break;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001527
Sebastian Redl539c5062010-08-18 23:57:32 +00001528 case PP_MACRO_OBJECT_LIKE:
1529 case PP_MACRO_FUNCTION_LIKE:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001530 DecodeIdentifierInfo(Record[0]);
1531 break;
1532
Sebastian Redl539c5062010-08-18 23:57:32 +00001533 case PP_TOKEN:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001534 // Ignore tokens.
1535 break;
Douglas Gregoraae92242010-03-19 21:51:54 +00001536
Sebastian Redl539c5062010-08-18 23:57:32 +00001537 case PP_MACRO_INSTANTIATION:
1538 case PP_MACRO_DEFINITION:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001539 // Read the macro record.
1540 ReadMacroRecord(Chain[N - I - 1]->Stream, Cursor.GetCurrentBitNo());
1541 break;
1542 }
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001543 }
1544 }
1545}
1546
Sebastian Redl539c5062010-08-18 23:57:32 +00001547MacroDefinition *ASTReader::getMacroDefinition(IdentID ID) {
Douglas Gregoraae92242010-03-19 21:51:54 +00001548 if (ID == 0 || ID >= MacroDefinitionsLoaded.size())
1549 return 0;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001550
1551 if (!MacroDefinitionsLoaded[ID]) {
1552 unsigned Index = ID;
1553 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1554 PerFileData &F = *Chain[N - I - 1];
1555 if (Index < F.LocalNumMacroDefinitions) {
1556 ReadMacroRecord(F.Stream, F.MacroDefinitionOffsets[Index]);
1557 break;
1558 }
1559 Index -= F.LocalNumMacroDefinitions;
1560 }
1561 assert(MacroDefinitionsLoaded[ID] && "Broken chain");
1562 }
1563
Douglas Gregoraae92242010-03-19 21:51:54 +00001564 return MacroDefinitionsLoaded[ID];
1565}
1566
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001567/// \brief If we are loading a relocatable PCH file, and the filename is
1568/// not an absolute path, add the system root to the beginning of the file
1569/// name.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001570void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001571 // If this is not a relocatable PCH file, there's nothing to do.
1572 if (!RelocatablePCH)
1573 return;
Mike Stump11289f42009-09-09 15:08:12 +00001574
Daniel Dunbarf2ce9a22009-11-18 19:50:41 +00001575 if (Filename.empty() || llvm::sys::Path(Filename).isAbsolute())
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001576 return;
1577
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001578 if (isysroot == 0) {
1579 // If no system root was given, default to '/'
1580 Filename.insert(Filename.begin(), '/');
1581 return;
1582 }
Mike Stump11289f42009-09-09 15:08:12 +00001583
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001584 unsigned Length = strlen(isysroot);
1585 if (isysroot[Length - 1] != '/')
1586 Filename.insert(Filename.begin(), '/');
Mike Stump11289f42009-09-09 15:08:12 +00001587
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001588 Filename.insert(Filename.begin(), isysroot, isysroot + Length);
1589}
1590
Sebastian Redl2c499f62010-08-18 23:56:43 +00001591ASTReader::ASTReadResult
Sebastian Redl3e31c722010-08-18 23:56:56 +00001592ASTReader::ReadASTBlock(PerFileData &F) {
Sebastian Redl34522812010-07-16 17:50:48 +00001593 llvm::BitstreamCursor &Stream = F.Stream;
1594
Sebastian Redl539c5062010-08-18 23:57:32 +00001595 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001596 Error("malformed block record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001597 return Failure;
1598 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001599
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001600 // Read all of the records and blocks for the ASt file.
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001601 RecordData Record;
Sebastian Redl393f8b72010-07-19 20:52:06 +00001602 bool First = true;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001603 while (!Stream.AtEndOfStream()) {
1604 unsigned Code = Stream.ReadCode();
1605 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor55abb232009-04-10 20:39:37 +00001606 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001607 Error("error at end of module block in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001608 return Failure;
1609 }
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001610
Douglas Gregor55abb232009-04-10 20:39:37 +00001611 return Success;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001612 }
1613
1614 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1615 switch (Stream.ReadSubBlockID()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001616 case DECLTYPES_BLOCK_ID:
Chris Lattnere78a6be2009-04-27 01:05:14 +00001617 // We lazily load the decls block, but we want to set up the
1618 // DeclsCursor cursor to point into it. Clone our current bitcode
1619 // cursor to it, enter the block and read the abbrevs in that block.
1620 // With the main cursor, we just skip over it.
Sebastian Redl34522812010-07-16 17:50:48 +00001621 F.DeclsCursor = Stream;
Chris Lattnere78a6be2009-04-27 01:05:14 +00001622 if (Stream.SkipBlock() || // Skip with the main cursor.
1623 // Read the abbrevs.
Sebastian Redl539c5062010-08-18 23:57:32 +00001624 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001625 Error("malformed block record in AST file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001626 return Failure;
1627 }
1628 break;
Mike Stump11289f42009-09-09 15:08:12 +00001629
Sebastian Redl539c5062010-08-18 23:57:32 +00001630 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl34522812010-07-16 17:50:48 +00001631 F.MacroCursor = Stream;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001632 if (PP)
1633 PP->setExternalSource(this);
1634
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001635 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001636 Error("malformed block record in AST file");
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001637 return Failure;
1638 }
1639 break;
Steve Naroff2ddea052009-04-23 10:39:46 +00001640
Sebastian Redl539c5062010-08-18 23:57:32 +00001641 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl393f8b72010-07-19 20:52:06 +00001642 switch (ReadSourceManagerBlock(F)) {
Douglas Gregor92863e42009-04-10 23:10:45 +00001643 case Success:
1644 break;
1645
1646 case Failure:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001647 Error("malformed source manager block in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001648 return Failure;
Douglas Gregor92863e42009-04-10 23:10:45 +00001649
1650 case IgnorePCH:
1651 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00001652 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001653 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001654 }
Sebastian Redl393f8b72010-07-19 20:52:06 +00001655 First = false;
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001656 continue;
1657 }
1658
1659 if (Code == llvm::bitc::DEFINE_ABBREV) {
1660 Stream.ReadAbbrevRecord();
1661 continue;
1662 }
1663
1664 // Read and process a record.
1665 Record.clear();
Douglas Gregorbfbde532009-04-10 21:16:55 +00001666 const char *BlobStart = 0;
1667 unsigned BlobLen = 0;
Sebastian Redl539c5062010-08-18 23:57:32 +00001668 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Douglas Gregorbfbde532009-04-10 21:16:55 +00001669 &BlobStart, &BlobLen)) {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001670 default: // Default behavior: ignore.
1671 break;
1672
Sebastian Redl539c5062010-08-18 23:57:32 +00001673 case METADATA: {
1674 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1675 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001676 : diag::warn_pch_version_too_new);
1677 return IgnorePCH;
1678 }
1679
1680 RelocatablePCH = Record[4];
1681 if (Listener) {
1682 std::string TargetTriple(BlobStart, BlobLen);
1683 if (Listener->ReadTargetTriple(TargetTriple))
1684 return IgnorePCH;
1685 }
1686 break;
1687 }
1688
Sebastian Redl539c5062010-08-18 23:57:32 +00001689 case CHAINED_METADATA: {
Sebastian Redl393f8b72010-07-19 20:52:06 +00001690 if (!First) {
1691 Error("CHAINED_METADATA is not first record in block");
1692 return Failure;
1693 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001694 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1695 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001696 : diag::warn_pch_version_too_new);
1697 return IgnorePCH;
1698 }
1699
1700 // Load the chained file.
Sebastian Redl3e31c722010-08-18 23:56:56 +00001701 switch(ReadASTCore(llvm::StringRef(BlobStart, BlobLen))) {
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001702 case Failure: return Failure;
1703 // If we have to ignore the dependency, we'll have to ignore this too.
1704 case IgnorePCH: return IgnorePCH;
1705 case Success: break;
1706 }
1707 break;
1708 }
1709
Sebastian Redl539c5062010-08-18 23:57:32 +00001710 case TYPE_OFFSET:
Sebastian Redl9e687992010-07-19 22:06:55 +00001711 if (F.LocalNumTypes != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001712 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001713 return Failure;
1714 }
Sebastian Redl9e687992010-07-19 22:06:55 +00001715 F.TypeOffsets = (const uint32_t *)BlobStart;
1716 F.LocalNumTypes = Record[0];
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001717 break;
1718
Sebastian Redl539c5062010-08-18 23:57:32 +00001719 case DECL_OFFSET:
Sebastian Redl9e687992010-07-19 22:06:55 +00001720 if (F.LocalNumDecls != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001721 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001722 return Failure;
1723 }
Sebastian Redl9e687992010-07-19 22:06:55 +00001724 F.DeclOffsets = (const uint32_t *)BlobStart;
1725 F.LocalNumDecls = Record[0];
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001726 break;
Douglas Gregor55abb232009-04-10 20:39:37 +00001727
Sebastian Redl539c5062010-08-18 23:57:32 +00001728 case TU_UPDATE_LEXICAL: {
Sebastian Redl4b1f4902010-07-27 18:24:41 +00001729 DeclContextInfo Info = {
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001730 /* No visible information */ 0,
Sebastian Redl539c5062010-08-18 23:57:32 +00001731 reinterpret_cast<const DeclID *>(BlobStart),
1732 BlobLen / sizeof(DeclID)
Sebastian Redl4b1f4902010-07-27 18:24:41 +00001733 };
1734 DeclContextOffsets[Context->getTranslationUnitDecl()].push_back(Info);
1735 break;
1736 }
1737
Sebastian Redld7dce0a2010-08-24 00:50:04 +00001738 case UPDATE_VISIBLE: {
1739 serialization::DeclID ID = Record[0];
1740 void *Table = ASTDeclContextNameLookupTable::Create(
1741 (const unsigned char *)BlobStart + Record[1],
1742 (const unsigned char *)BlobStart,
1743 ASTDeclContextNameLookupTrait(*this));
1744 if (ID == 1) { // Is it the TU?
1745 DeclContextInfo Info = {
1746 Table, /* No lexical inforamtion */ 0, 0
1747 };
1748 DeclContextOffsets[Context->getTranslationUnitDecl()].push_back(Info);
1749 } else
1750 PendingVisibleUpdates[ID].push_back(Table);
1751 break;
1752 }
1753
Sebastian Redl539c5062010-08-18 23:57:32 +00001754 case REDECLS_UPDATE_LATEST: {
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00001755 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
1756 for (unsigned i = 0, e = Record.size(); i < e; i += 2) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001757 DeclID First = Record[i], Latest = Record[i+1];
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00001758 assert((FirstLatestDeclIDs.find(First) == FirstLatestDeclIDs.end() ||
1759 Latest > FirstLatestDeclIDs[First]) &&
1760 "The new latest is supposed to come after the previous latest");
1761 FirstLatestDeclIDs[First] = Latest;
1762 }
1763 break;
1764 }
1765
Sebastian Redl539c5062010-08-18 23:57:32 +00001766 case LANGUAGE_OPTIONS:
Douglas Gregorce3a8292010-07-27 00:27:13 +00001767 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor55abb232009-04-10 20:39:37 +00001768 return IgnorePCH;
1769 break;
Douglas Gregorbfbde532009-04-10 21:16:55 +00001770
Sebastian Redl539c5062010-08-18 23:57:32 +00001771 case IDENTIFIER_TABLE:
Sebastian Redl393f8b72010-07-19 20:52:06 +00001772 F.IdentifierTableData = BlobStart;
Douglas Gregor0e149972009-04-25 19:10:14 +00001773 if (Record[0]) {
Sebastian Redl393f8b72010-07-19 20:52:06 +00001774 F.IdentifierLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001775 = ASTIdentifierLookupTable::Create(
Sebastian Redl393f8b72010-07-19 20:52:06 +00001776 (const unsigned char *)F.IdentifierTableData + Record[0],
1777 (const unsigned char *)F.IdentifierTableData,
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001778 ASTIdentifierLookupTrait(*this, F.Stream));
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001779 if (PP)
1780 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor0e149972009-04-25 19:10:14 +00001781 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001782 break;
1783
Sebastian Redl539c5062010-08-18 23:57:32 +00001784 case IDENTIFIER_OFFSET:
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00001785 if (F.LocalNumIdentifiers != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001786 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001787 return Failure;
1788 }
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00001789 F.IdentifierOffsets = (const uint32_t *)BlobStart;
1790 F.LocalNumIdentifiers = Record[0];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001791 break;
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00001792
Sebastian Redl539c5062010-08-18 23:57:32 +00001793 case EXTERNAL_DEFINITIONS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001794 // Optimization for the first block.
1795 if (ExternalDefinitions.empty())
1796 ExternalDefinitions.swap(Record);
1797 else
1798 ExternalDefinitions.insert(ExternalDefinitions.end(),
1799 Record.begin(), Record.end());
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00001800 break;
Douglas Gregor08f01292009-04-17 22:13:46 +00001801
Sebastian Redl539c5062010-08-18 23:57:32 +00001802 case SPECIAL_TYPES:
Sebastian Redlb293a452010-07-20 21:20:32 +00001803 // Optimization for the first block
1804 if (SpecialTypes.empty())
1805 SpecialTypes.swap(Record);
1806 else
1807 SpecialTypes.insert(SpecialTypes.end(), Record.begin(), Record.end());
Douglas Gregor652d82a2009-04-18 05:55:16 +00001808 break;
1809
Sebastian Redl539c5062010-08-18 23:57:32 +00001810 case STATISTICS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001811 TotalNumStatements += Record[0];
1812 TotalNumMacros += Record[1];
1813 TotalLexicalDeclContexts += Record[2];
1814 TotalVisibleDeclContexts += Record[3];
Douglas Gregor08f01292009-04-17 22:13:46 +00001815 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00001816
Sebastian Redl539c5062010-08-18 23:57:32 +00001817 case TENTATIVE_DEFINITIONS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001818 // Optimization for the first block.
1819 if (TentativeDefinitions.empty())
1820 TentativeDefinitions.swap(Record);
1821 else
1822 TentativeDefinitions.insert(TentativeDefinitions.end(),
1823 Record.begin(), Record.end());
Douglas Gregord4df8652009-04-22 22:02:47 +00001824 break;
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001825
Sebastian Redl539c5062010-08-18 23:57:32 +00001826 case UNUSED_FILESCOPED_DECLS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001827 // Optimization for the first block.
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00001828 if (UnusedFileScopedDecls.empty())
1829 UnusedFileScopedDecls.swap(Record);
Sebastian Redlb293a452010-07-20 21:20:32 +00001830 else
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00001831 UnusedFileScopedDecls.insert(UnusedFileScopedDecls.end(),
1832 Record.begin(), Record.end());
Tanya Lattner90073802010-02-12 00:07:30 +00001833 break;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001834
Sebastian Redl539c5062010-08-18 23:57:32 +00001835 case WEAK_UNDECLARED_IDENTIFIERS:
Sebastian Redl08aca90252010-08-05 18:21:25 +00001836 // Later blocks overwrite earlier ones.
1837 WeakUndeclaredIdentifiers.swap(Record);
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00001838 break;
1839
Sebastian Redl539c5062010-08-18 23:57:32 +00001840 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001841 // Optimization for the first block.
1842 if (LocallyScopedExternalDecls.empty())
1843 LocallyScopedExternalDecls.swap(Record);
1844 else
1845 LocallyScopedExternalDecls.insert(LocallyScopedExternalDecls.end(),
1846 Record.begin(), Record.end());
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001847 break;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001848
Sebastian Redl539c5062010-08-18 23:57:32 +00001849 case SELECTOR_OFFSETS:
Sebastian Redla19a67f2010-08-03 21:58:15 +00001850 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redlada023c2010-08-04 20:40:17 +00001851 F.LocalNumSelectors = Record[0];
Douglas Gregor95c13f52009-04-25 17:48:32 +00001852 break;
1853
Sebastian Redl539c5062010-08-18 23:57:32 +00001854 case METHOD_POOL:
Sebastian Redlada023c2010-08-04 20:40:17 +00001855 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor95c13f52009-04-25 17:48:32 +00001856 if (Record[0])
Sebastian Redlada023c2010-08-04 20:40:17 +00001857 F.SelectorLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001858 = ASTSelectorLookupTable::Create(
Sebastian Redlada023c2010-08-04 20:40:17 +00001859 F.SelectorLookupTableData + Record[0],
1860 F.SelectorLookupTableData,
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001861 ASTSelectorLookupTrait(*this));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00001862 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorc78d3462009-04-24 21:10:55 +00001863 break;
Douglas Gregoreda6a892009-04-26 00:07:37 +00001864
Sebastian Redl539c5062010-08-18 23:57:32 +00001865 case REFERENCED_SELECTOR_POOL: {
Sebastian Redlada023c2010-08-04 20:40:17 +00001866 ReferencedSelectorsData.insert(ReferencedSelectorsData.end(),
1867 Record.begin(), Record.end());
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001868 break;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001869 }
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001870
Sebastian Redl539c5062010-08-18 23:57:32 +00001871 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001872 if (!Record.empty() && Listener)
1873 Listener->ReadCounter(Record[0]);
Douglas Gregoreda6a892009-04-26 00:07:37 +00001874 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00001875
Sebastian Redl539c5062010-08-18 23:57:32 +00001876 case SOURCE_LOCATION_OFFSETS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001877 F.SLocOffsets = (const uint32_t *)BlobStart;
1878 F.LocalNumSLocEntries = Record[0];
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001879 // We cannot delay this until the entire chain is loaded, because then
1880 // source location preloads would also have to be delayed.
1881 // FIXME: Is there a reason not to do that?
Sebastian Redlb293a452010-07-20 21:20:32 +00001882 TotalNumSLocEntries += F.LocalNumSLocEntries;
Douglas Gregord54f3a12009-10-05 21:07:28 +00001883 SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, Record[1]);
Douglas Gregor258ae542009-04-27 06:38:32 +00001884 break;
1885
Sebastian Redl539c5062010-08-18 23:57:32 +00001886 case SOURCE_LOCATION_PRELOADS:
Douglas Gregor258ae542009-04-27 06:38:32 +00001887 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
Sebastian Redl2c499f62010-08-18 23:56:43 +00001888 ASTReadResult Result = ReadSLocEntryRecord(Record[I]);
Douglas Gregor258ae542009-04-27 06:38:32 +00001889 if (Result != Success)
1890 return Result;
1891 }
1892 break;
Douglas Gregorc5046832009-04-27 18:38:38 +00001893
Sebastian Redl539c5062010-08-18 23:57:32 +00001894 case STAT_CACHE: {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001895 ASTStatCache *MyStatCache =
1896 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
Douglas Gregord2eb58a2009-10-16 18:18:30 +00001897 (const unsigned char *)BlobStart,
1898 NumStatHits, NumStatMisses);
1899 FileMgr.addStatCache(MyStatCache);
Sebastian Redl34522812010-07-16 17:50:48 +00001900 F.StatCache = MyStatCache;
Douglas Gregorc5046832009-04-27 18:38:38 +00001901 break;
Douglas Gregord2eb58a2009-10-16 18:18:30 +00001902 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001903
Sebastian Redl539c5062010-08-18 23:57:32 +00001904 case EXT_VECTOR_DECLS:
Sebastian Redl04f5c312010-07-28 21:38:49 +00001905 // Optimization for the first block.
1906 if (ExtVectorDecls.empty())
1907 ExtVectorDecls.swap(Record);
1908 else
1909 ExtVectorDecls.insert(ExtVectorDecls.end(),
1910 Record.begin(), Record.end());
Douglas Gregor61cac2b2009-04-27 20:06:05 +00001911 break;
1912
Sebastian Redl539c5062010-08-18 23:57:32 +00001913 case VTABLE_USES:
Sebastian Redl08aca90252010-08-05 18:21:25 +00001914 // Later tables overwrite earlier ones.
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00001915 VTableUses.swap(Record);
1916 break;
1917
Sebastian Redl539c5062010-08-18 23:57:32 +00001918 case DYNAMIC_CLASSES:
Sebastian Redl08aca90252010-08-05 18:21:25 +00001919 // Optimization for the first block.
1920 if (DynamicClasses.empty())
1921 DynamicClasses.swap(Record);
1922 else
1923 DynamicClasses.insert(DynamicClasses.end(),
1924 Record.begin(), Record.end());
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00001925 break;
1926
Sebastian Redl539c5062010-08-18 23:57:32 +00001927 case PENDING_IMPLICIT_INSTANTIATIONS:
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00001928 // Optimization for the first block.
1929 if (PendingImplicitInstantiations.empty())
1930 PendingImplicitInstantiations.swap(Record);
1931 else
1932 PendingImplicitInstantiations.insert(
1933 PendingImplicitInstantiations.end(), Record.begin(), Record.end());
1934 break;
1935
Sebastian Redl539c5062010-08-18 23:57:32 +00001936 case SEMA_DECL_REFS:
Sebastian Redl08aca90252010-08-05 18:21:25 +00001937 // Later tables overwrite earlier ones.
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00001938 SemaDeclRefs.swap(Record);
1939 break;
1940
Sebastian Redl539c5062010-08-18 23:57:32 +00001941 case ORIGINAL_FILE_NAME:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001942 // The primary AST will be the last to get here, so it will be the one
Sebastian Redlb293a452010-07-20 21:20:32 +00001943 // that's used.
Daniel Dunbar000c4ff2009-11-11 05:29:04 +00001944 ActualOriginalFileName.assign(BlobStart, BlobLen);
1945 OriginalFileName = ActualOriginalFileName;
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001946 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001947 break;
Mike Stump11289f42009-09-09 15:08:12 +00001948
Sebastian Redl539c5062010-08-18 23:57:32 +00001949 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek8bd09292010-02-12 23:31:14 +00001950 const std::string &CurBranch = getClangFullRepositoryVersion();
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001951 llvm::StringRef ASTBranch(BlobStart, BlobLen);
1952 if (llvm::StringRef(CurBranch) != ASTBranch && !DisableValidation) {
1953 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregord54f3a12009-10-05 21:07:28 +00001954 return IgnorePCH;
1955 }
1956 break;
1957 }
Sebastian Redlfa061442010-07-21 20:07:32 +00001958
Sebastian Redl539c5062010-08-18 23:57:32 +00001959 case MACRO_DEFINITION_OFFSETS:
Sebastian Redlfa061442010-07-21 20:07:32 +00001960 F.MacroDefinitionOffsets = (const uint32_t *)BlobStart;
1961 F.NumPreallocatedPreprocessingEntities = Record[0];
1962 F.LocalNumMacroDefinitions = Record[1];
Douglas Gregoraae92242010-03-19 21:51:54 +00001963 break;
Sebastian Redle7c1fe62010-08-13 00:28:03 +00001964
Sebastian Redl539c5062010-08-18 23:57:32 +00001965 case DECL_REPLACEMENTS: {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00001966 if (Record.size() % 2 != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001967 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redle7c1fe62010-08-13 00:28:03 +00001968 return Failure;
1969 }
1970 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Sebastian Redl539c5062010-08-18 23:57:32 +00001971 ReplacedDecls[static_cast<DeclID>(Record[I])] =
Sebastian Redle7c1fe62010-08-13 00:28:03 +00001972 std::make_pair(&F, Record[I+1]);
1973 break;
1974 }
Sebastian Redlaba202b2010-08-24 22:50:19 +00001975
1976 case ADDITIONAL_TEMPLATE_SPECIALIZATIONS: {
1977 AdditionalTemplateSpecializations &ATS =
1978 AdditionalTemplateSpecializationsPending[Record[0]];
1979 ATS.insert(ATS.end(), Record.begin()+1, Record.end());
1980 break;
1981 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001982 }
Sebastian Redl393f8b72010-07-19 20:52:06 +00001983 First = false;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001984 }
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001985 Error("premature end of bitstream in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001986 return Failure;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001987}
1988
Sebastian Redl3e31c722010-08-18 23:56:56 +00001989ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName) {
1990 switch(ReadASTCore(FileName)) {
Sebastian Redl2abc0382010-07-16 20:41:52 +00001991 case Failure: return Failure;
1992 case IgnorePCH: return IgnorePCH;
1993 case Success: break;
1994 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001995
1996 // Here comes stuff that we only do once the entire chain is loaded.
1997
Sebastian Redlb293a452010-07-20 21:20:32 +00001998 // Allocate space for loaded identifiers, decls and types.
Sebastian Redlfa061442010-07-21 20:07:32 +00001999 unsigned TotalNumIdentifiers = 0, TotalNumTypes = 0, TotalNumDecls = 0,
Sebastian Redlada023c2010-08-04 20:40:17 +00002000 TotalNumPreallocatedPreprocessingEntities = 0, TotalNumMacroDefs = 0,
2001 TotalNumSelectors = 0;
Sebastian Redl9e687992010-07-19 22:06:55 +00002002 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002003 TotalNumIdentifiers += Chain[I]->LocalNumIdentifiers;
Sebastian Redl9e687992010-07-19 22:06:55 +00002004 TotalNumTypes += Chain[I]->LocalNumTypes;
2005 TotalNumDecls += Chain[I]->LocalNumDecls;
Sebastian Redlfa061442010-07-21 20:07:32 +00002006 TotalNumPreallocatedPreprocessingEntities +=
2007 Chain[I]->NumPreallocatedPreprocessingEntities;
2008 TotalNumMacroDefs += Chain[I]->LocalNumMacroDefinitions;
Sebastian Redlada023c2010-08-04 20:40:17 +00002009 TotalNumSelectors += Chain[I]->LocalNumSelectors;
Sebastian Redl9e687992010-07-19 22:06:55 +00002010 }
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002011 IdentifiersLoaded.resize(TotalNumIdentifiers);
Sebastian Redl9e687992010-07-19 22:06:55 +00002012 TypesLoaded.resize(TotalNumTypes);
2013 DeclsLoaded.resize(TotalNumDecls);
Sebastian Redlfa061442010-07-21 20:07:32 +00002014 MacroDefinitionsLoaded.resize(TotalNumMacroDefs);
2015 if (PP) {
2016 if (TotalNumIdentifiers > 0)
2017 PP->getHeaderSearchInfo().SetExternalLookup(this);
2018 if (TotalNumPreallocatedPreprocessingEntities > 0) {
2019 if (!PP->getPreprocessingRecord())
2020 PP->createPreprocessingRecord();
2021 PP->getPreprocessingRecord()->SetExternalSource(*this,
2022 TotalNumPreallocatedPreprocessingEntities);
2023 }
2024 }
Sebastian Redlada023c2010-08-04 20:40:17 +00002025 SelectorsLoaded.resize(TotalNumSelectors);
Sebastian Redl9e687992010-07-19 22:06:55 +00002026
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002027 // Check the predefines buffers.
Douglas Gregorce3a8292010-07-27 00:27:13 +00002028 if (!DisableValidation && CheckPredefinesBuffers())
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002029 return IgnorePCH;
2030
2031 if (PP) {
2032 // Initialization of keywords and pragmas occurs before the
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002033 // AST file is read, so there may be some identifiers that were
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002034 // loaded into the IdentifierTable before we intercepted the
2035 // creation of identifiers. Iterate through the list of known
2036 // identifiers and determine whether we have to establish
2037 // preprocessor definitions or top-level identifier declaration
2038 // chains for those identifiers.
2039 //
2040 // We copy the IdentifierInfo pointers to a small vector first,
2041 // since de-serializing declarations or macro definitions can add
2042 // new entries into the identifier table, invalidating the
2043 // iterators.
2044 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
2045 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
2046 IdEnd = PP->getIdentifierTable().end();
2047 Id != IdEnd; ++Id)
2048 Identifiers.push_back(Id->second);
Sebastian Redlfa061442010-07-21 20:07:32 +00002049 // We need to search the tables in all files.
Sebastian Redlfa061442010-07-21 20:07:32 +00002050 for (unsigned J = 0, M = Chain.size(); J != M; ++J) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002051 ASTIdentifierLookupTable *IdTable
2052 = (ASTIdentifierLookupTable *)Chain[J]->IdentifierLookupTable;
2053 // Not all AST files necessarily have identifier tables, only the useful
Sebastian Redl5c415f32010-07-22 17:01:13 +00002054 // ones.
2055 if (!IdTable)
2056 continue;
Sebastian Redlfa061442010-07-21 20:07:32 +00002057 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2058 IdentifierInfo *II = Identifiers[I];
2059 // Look in the on-disk hash tables for an entry for this identifier
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002060 ASTIdentifierLookupTrait Info(*this, Chain[J]->Stream, II);
Sebastian Redlfa061442010-07-21 20:07:32 +00002061 std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength());
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002062 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
Sebastian Redlb293a452010-07-20 21:20:32 +00002063 if (Pos == IdTable->end())
2064 continue;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002065
Sebastian Redlb293a452010-07-20 21:20:32 +00002066 // Dereferencing the iterator has the effect of populating the
2067 // IdentifierInfo node with the various declarations it needs.
2068 (void)*Pos;
2069 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002070 }
2071 }
2072
2073 if (Context)
2074 InitializeContext(*Context);
2075
2076 return Success;
2077}
2078
Sebastian Redl3e31c722010-08-18 23:56:56 +00002079ASTReader::ASTReadResult ASTReader::ReadASTCore(llvm::StringRef FileName) {
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002080 Chain.push_back(new PerFileData());
Sebastian Redl34522812010-07-16 17:50:48 +00002081 PerFileData &F = *Chain.back();
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002082
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002083 // Set the AST file name.
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002084 F.FileName = FileName;
2085
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002086 // Open the AST file.
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002087 //
2088 // FIXME: This shouldn't be here, we should just take a raw_ostream.
2089 std::string ErrStr;
2090 F.Buffer.reset(llvm::MemoryBuffer::getFileOrSTDIN(FileName, &ErrStr));
2091 if (!F.Buffer) {
2092 Error(ErrStr.c_str());
2093 return IgnorePCH;
2094 }
2095
2096 // Initialize the stream
2097 F.StreamFile.init((const unsigned char *)F.Buffer->getBufferStart(),
2098 (const unsigned char *)F.Buffer->getBufferEnd());
Sebastian Redl34522812010-07-16 17:50:48 +00002099 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002100 Stream.init(F.StreamFile);
Sebastian Redlfa061442010-07-21 20:07:32 +00002101 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002102
2103 // Sniff for the signature.
2104 if (Stream.Read(8) != 'C' ||
2105 Stream.Read(8) != 'P' ||
2106 Stream.Read(8) != 'C' ||
2107 Stream.Read(8) != 'H') {
2108 Diag(diag::err_not_a_pch_file) << FileName;
2109 return Failure;
2110 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002111
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002112 while (!Stream.AtEndOfStream()) {
2113 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00002114
Douglas Gregor92863e42009-04-10 23:10:45 +00002115 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002116 Error("invalid record at top-level of AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002117 return Failure;
2118 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002119
2120 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregora868bbd2009-04-21 22:25:48 +00002121
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002122 // We only know the AST subblock ID.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002123 switch (BlockID) {
2124 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregor92863e42009-04-10 23:10:45 +00002125 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002126 Error("malformed BlockInfoBlock in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002127 return Failure;
2128 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002129 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00002130 case AST_BLOCK_ID:
Sebastian Redl3e31c722010-08-18 23:56:56 +00002131 switch (ReadASTBlock(F)) {
Douglas Gregor55abb232009-04-10 20:39:37 +00002132 case Success:
2133 break;
2134
2135 case Failure:
Douglas Gregor92863e42009-04-10 23:10:45 +00002136 return Failure;
Douglas Gregor55abb232009-04-10 20:39:37 +00002137
2138 case IgnorePCH:
Douglas Gregorbfbde532009-04-10 21:16:55 +00002139 // FIXME: We could consider reading through to the end of this
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002140 // AST block, skipping subblocks, to see if there are other
2141 // AST blocks elsewhere.
Douglas Gregor0bc12932009-04-27 21:28:04 +00002142
2143 // Clear out any preallocated source location entries, so that
2144 // the source manager does not try to resolve them later.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002145 SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor0bc12932009-04-27 21:28:04 +00002146
2147 // Remove the stat cache.
Sebastian Redl34522812010-07-16 17:50:48 +00002148 if (F.StatCache)
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002149 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor0bc12932009-04-27 21:28:04 +00002150
Douglas Gregor92863e42009-04-10 23:10:45 +00002151 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00002152 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002153 break;
2154 default:
Douglas Gregor92863e42009-04-10 23:10:45 +00002155 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002156 Error("malformed block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002157 return Failure;
2158 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002159 break;
2160 }
Mike Stump11289f42009-09-09 15:08:12 +00002161 }
2162
Sebastian Redl2abc0382010-07-16 20:41:52 +00002163 return Success;
2164}
2165
Sebastian Redl2c499f62010-08-18 23:56:43 +00002166void ASTReader::setPreprocessor(Preprocessor &pp) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002167 PP = &pp;
Sebastian Redlfa061442010-07-21 20:07:32 +00002168
2169 unsigned TotalNum = 0;
2170 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
2171 TotalNum += Chain[I]->NumPreallocatedPreprocessingEntities;
2172 if (TotalNum) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002173 if (!PP->getPreprocessingRecord())
2174 PP->createPreprocessingRecord();
Sebastian Redlfa061442010-07-21 20:07:32 +00002175 PP->getPreprocessingRecord()->SetExternalSource(*this, TotalNum);
Douglas Gregoraae92242010-03-19 21:51:54 +00002176 }
2177}
2178
Sebastian Redl2c499f62010-08-18 23:56:43 +00002179void ASTReader::InitializeContext(ASTContext &Ctx) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002180 Context = &Ctx;
2181 assert(Context && "Passed null context!");
2182
2183 assert(PP && "Forgot to set Preprocessor ?");
2184 PP->getIdentifierTable().setExternalIdentifierLookup(this);
2185 PP->getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor9882a5a2010-01-04 19:18:44 +00002186 PP->setExternalSource(this);
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00002187
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002188 // Load the translation unit declaration
Argyrios Kyrtzidis7e8996c2010-07-08 17:13:02 +00002189 GetTranslationUnitDecl();
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002190
2191 // Load the special types.
2192 Context->setBuiltinVaListType(
Sebastian Redl539c5062010-08-18 23:57:32 +00002193 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
2194 if (unsigned Id = SpecialTypes[SPECIAL_TYPE_OBJC_ID])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002195 Context->setObjCIdType(GetType(Id));
Sebastian Redl539c5062010-08-18 23:57:32 +00002196 if (unsigned Sel = SpecialTypes[SPECIAL_TYPE_OBJC_SELECTOR])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002197 Context->setObjCSelType(GetType(Sel));
Sebastian Redl539c5062010-08-18 23:57:32 +00002198 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002199 Context->setObjCProtoType(GetType(Proto));
Sebastian Redl539c5062010-08-18 23:57:32 +00002200 if (unsigned Class = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002201 Context->setObjCClassType(GetType(Class));
Steve Naroff7cae42b2009-07-10 23:34:53 +00002202
Sebastian Redl539c5062010-08-18 23:57:32 +00002203 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002204 Context->setCFConstantStringType(GetType(String));
Mike Stump11289f42009-09-09 15:08:12 +00002205 if (unsigned FastEnum
Sebastian Redl539c5062010-08-18 23:57:32 +00002206 = SpecialTypes[SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002207 Context->setObjCFastEnumerationStateType(GetType(FastEnum));
Sebastian Redl539c5062010-08-18 23:57:32 +00002208 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
Douglas Gregor27821ce2009-07-07 16:35:42 +00002209 QualType FileType = GetType(File);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002210 if (FileType.isNull()) {
2211 Error("FILE type is NULL");
2212 return;
2213 }
John McCall9dd450b2009-09-21 23:43:11 +00002214 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
Douglas Gregor27821ce2009-07-07 16:35:42 +00002215 Context->setFILEDecl(Typedef->getDecl());
2216 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002217 const TagType *Tag = FileType->getAs<TagType>();
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002218 if (!Tag) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002219 Error("Invalid FILE type in AST file");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002220 return;
2221 }
Douglas Gregor27821ce2009-07-07 16:35:42 +00002222 Context->setFILEDecl(Tag->getDecl());
2223 }
2224 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002225 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
Mike Stumpa4de80b2009-07-28 02:25:19 +00002226 QualType Jmp_bufType = GetType(Jmp_buf);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002227 if (Jmp_bufType.isNull()) {
2228 Error("jmp_bug type is NULL");
2229 return;
2230 }
John McCall9dd450b2009-09-21 23:43:11 +00002231 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
Mike Stumpa4de80b2009-07-28 02:25:19 +00002232 Context->setjmp_bufDecl(Typedef->getDecl());
2233 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002234 const TagType *Tag = Jmp_bufType->getAs<TagType>();
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002235 if (!Tag) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002236 Error("Invalid jmp_buf type in AST file");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002237 return;
2238 }
Mike Stumpa4de80b2009-07-28 02:25:19 +00002239 Context->setjmp_bufDecl(Tag->getDecl());
2240 }
2241 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002242 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
Mike Stumpa4de80b2009-07-28 02:25:19 +00002243 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002244 if (Sigjmp_bufType.isNull()) {
2245 Error("sigjmp_buf type is NULL");
2246 return;
2247 }
John McCall9dd450b2009-09-21 23:43:11 +00002248 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
Mike Stumpa4de80b2009-07-28 02:25:19 +00002249 Context->setsigjmp_bufDecl(Typedef->getDecl());
2250 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002251 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002252 assert(Tag && "Invalid sigjmp_buf type in AST file");
Mike Stumpa4de80b2009-07-28 02:25:19 +00002253 Context->setsigjmp_bufDecl(Tag->getDecl());
2254 }
2255 }
Mike Stump11289f42009-09-09 15:08:12 +00002256 if (unsigned ObjCIdRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002257 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION])
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002258 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
Mike Stump11289f42009-09-09 15:08:12 +00002259 if (unsigned ObjCClassRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002260 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION])
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002261 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
Sebastian Redl539c5062010-08-18 23:57:32 +00002262 if (unsigned String = SpecialTypes[SPECIAL_TYPE_BLOCK_DESCRIPTOR])
Mike Stumpd0153282009-10-20 02:12:22 +00002263 Context->setBlockDescriptorType(GetType(String));
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002264 if (unsigned String
Sebastian Redl539c5062010-08-18 23:57:32 +00002265 = SpecialTypes[SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR])
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002266 Context->setBlockDescriptorExtendedType(GetType(String));
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002267 if (unsigned ObjCSelRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002268 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION])
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002269 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
Sebastian Redl539c5062010-08-18 23:57:32 +00002270 if (unsigned String = SpecialTypes[SPECIAL_TYPE_NS_CONSTANT_STRING])
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002271 Context->setNSConstantStringType(GetType(String));
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002272
Sebastian Redl539c5062010-08-18 23:57:32 +00002273 if (SpecialTypes[SPECIAL_TYPE_INT128_INSTALLED])
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002274 Context->setInt128Installed();
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002275}
2276
Douglas Gregor45fe0362009-05-12 01:31:05 +00002277/// \brief Retrieve the name of the original source file name
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002278/// directly from the AST file, without actually loading the AST
Douglas Gregor45fe0362009-05-12 01:31:05 +00002279/// file.
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002280std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Daniel Dunbar3b951482009-12-03 09:13:06 +00002281 Diagnostic &Diags) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002282 // Open the AST file.
Douglas Gregor45fe0362009-05-12 01:31:05 +00002283 std::string ErrStr;
2284 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002285 Buffer.reset(llvm::MemoryBuffer::getFile(ASTFileName.c_str(), &ErrStr));
Douglas Gregor45fe0362009-05-12 01:31:05 +00002286 if (!Buffer) {
Daniel Dunbar3b951482009-12-03 09:13:06 +00002287 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002288 return std::string();
2289 }
2290
2291 // Initialize the stream
2292 llvm::BitstreamReader StreamFile;
2293 llvm::BitstreamCursor Stream;
Mike Stump11289f42009-09-09 15:08:12 +00002294 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregor45fe0362009-05-12 01:31:05 +00002295 (const unsigned char *)Buffer->getBufferEnd());
2296 Stream.init(StreamFile);
2297
2298 // Sniff for the signature.
2299 if (Stream.Read(8) != 'C' ||
2300 Stream.Read(8) != 'P' ||
2301 Stream.Read(8) != 'C' ||
2302 Stream.Read(8) != 'H') {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002303 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002304 return std::string();
2305 }
2306
2307 RecordData Record;
2308 while (!Stream.AtEndOfStream()) {
2309 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00002310
Douglas Gregor45fe0362009-05-12 01:31:05 +00002311 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2312 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump11289f42009-09-09 15:08:12 +00002313
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002314 // We only know the AST subblock ID.
Douglas Gregor45fe0362009-05-12 01:31:05 +00002315 switch (BlockID) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002316 case AST_BLOCK_ID:
2317 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002318 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002319 return std::string();
2320 }
2321 break;
Mike Stump11289f42009-09-09 15:08:12 +00002322
Douglas Gregor45fe0362009-05-12 01:31:05 +00002323 default:
2324 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002325 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002326 return std::string();
2327 }
2328 break;
2329 }
2330 continue;
2331 }
2332
2333 if (Code == llvm::bitc::END_BLOCK) {
2334 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002335 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002336 return std::string();
2337 }
2338 continue;
2339 }
2340
2341 if (Code == llvm::bitc::DEFINE_ABBREV) {
2342 Stream.ReadAbbrevRecord();
2343 continue;
2344 }
2345
2346 Record.clear();
2347 const char *BlobStart = 0;
2348 unsigned BlobLen = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002349 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl539c5062010-08-18 23:57:32 +00002350 == ORIGINAL_FILE_NAME)
Douglas Gregor45fe0362009-05-12 01:31:05 +00002351 return std::string(BlobStart, BlobLen);
Mike Stump11289f42009-09-09 15:08:12 +00002352 }
Douglas Gregor45fe0362009-05-12 01:31:05 +00002353
2354 return std::string();
2355}
2356
Douglas Gregor55abb232009-04-10 20:39:37 +00002357/// \brief Parse the record that corresponds to a LangOptions data
2358/// structure.
2359///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002360/// This routine parses the language options from the AST file and then gives
2361/// them to the AST listener if one is set.
Douglas Gregor55abb232009-04-10 20:39:37 +00002362///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002363/// \returns true if the listener deems the file unacceptable, false otherwise.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002364bool ASTReader::ParseLanguageOptions(
Douglas Gregor55abb232009-04-10 20:39:37 +00002365 const llvm::SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002366 if (Listener) {
2367 LangOptions LangOpts;
Mike Stump11289f42009-09-09 15:08:12 +00002368
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002369 #define PARSE_LANGOPT(Option) \
2370 LangOpts.Option = Record[Idx]; \
2371 ++Idx
Mike Stump11289f42009-09-09 15:08:12 +00002372
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002373 unsigned Idx = 0;
2374 PARSE_LANGOPT(Trigraphs);
2375 PARSE_LANGOPT(BCPLComment);
2376 PARSE_LANGOPT(DollarIdents);
2377 PARSE_LANGOPT(AsmPreprocessor);
2378 PARSE_LANGOPT(GNUMode);
Chandler Carruthe03aa552010-04-17 20:17:31 +00002379 PARSE_LANGOPT(GNUKeywords);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002380 PARSE_LANGOPT(ImplicitInt);
2381 PARSE_LANGOPT(Digraphs);
2382 PARSE_LANGOPT(HexFloats);
2383 PARSE_LANGOPT(C99);
2384 PARSE_LANGOPT(Microsoft);
2385 PARSE_LANGOPT(CPlusPlus);
2386 PARSE_LANGOPT(CPlusPlus0x);
2387 PARSE_LANGOPT(CXXOperatorNames);
2388 PARSE_LANGOPT(ObjC1);
2389 PARSE_LANGOPT(ObjC2);
2390 PARSE_LANGOPT(ObjCNonFragileABI);
Fariborz Jahanian45878032010-02-09 19:31:38 +00002391 PARSE_LANGOPT(ObjCNonFragileABI2);
Fariborz Jahanian62c56022010-04-22 21:01:59 +00002392 PARSE_LANGOPT(NoConstantCFStrings);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002393 PARSE_LANGOPT(PascalStrings);
2394 PARSE_LANGOPT(WritableStrings);
2395 PARSE_LANGOPT(LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +00002396 PARSE_LANGOPT(AltiVec);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002397 PARSE_LANGOPT(Exceptions);
Daniel Dunbar925152c2010-02-10 18:48:44 +00002398 PARSE_LANGOPT(SjLjExceptions);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002399 PARSE_LANGOPT(NeXTRuntime);
2400 PARSE_LANGOPT(Freestanding);
2401 PARSE_LANGOPT(NoBuiltin);
2402 PARSE_LANGOPT(ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +00002403 PARSE_LANGOPT(POSIXThreads);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002404 PARSE_LANGOPT(Blocks);
2405 PARSE_LANGOPT(EmitAllDecls);
2406 PARSE_LANGOPT(MathErrno);
Chris Lattner51924e512010-06-26 21:25:03 +00002407 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
2408 Record[Idx++]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002409 PARSE_LANGOPT(HeinousExtensions);
2410 PARSE_LANGOPT(Optimize);
2411 PARSE_LANGOPT(OptimizeSize);
2412 PARSE_LANGOPT(Static);
2413 PARSE_LANGOPT(PICLevel);
2414 PARSE_LANGOPT(GNUInline);
2415 PARSE_LANGOPT(NoInline);
2416 PARSE_LANGOPT(AccessControl);
2417 PARSE_LANGOPT(CharIsSigned);
John Thompsoned4e2952009-11-05 20:14:16 +00002418 PARSE_LANGOPT(ShortWChar);
Chris Lattner51924e512010-06-26 21:25:03 +00002419 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
2420 LangOpts.setVisibilityMode((LangOptions::VisibilityMode)Record[Idx++]);
Daniel Dunbar143021e2009-09-21 04:16:19 +00002421 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
Chris Lattner51924e512010-06-26 21:25:03 +00002422 Record[Idx++]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002423 PARSE_LANGOPT(InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +00002424 PARSE_LANGOPT(OpenCL);
Mike Stumpd9546382009-12-12 01:27:46 +00002425 PARSE_LANGOPT(CatchUndefined);
2426 // FIXME: Missing ElideConstructors?!
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002427 #undef PARSE_LANGOPT
Douglas Gregor55abb232009-04-10 20:39:37 +00002428
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002429 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor55abb232009-04-10 20:39:37 +00002430 }
Douglas Gregor55abb232009-04-10 20:39:37 +00002431
2432 return false;
2433}
2434
Sebastian Redl2c499f62010-08-18 23:56:43 +00002435void ASTReader::ReadPreprocessedEntities() {
Douglas Gregoraae92242010-03-19 21:51:54 +00002436 ReadDefinedMacros();
2437}
2438
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002439/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002440ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002441 PerFileData *F = 0;
2442 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2443 F = Chain[N - I - 1];
2444 if (Index < F->LocalNumTypes)
2445 break;
2446 Index -= F->LocalNumTypes;
2447 }
2448 assert(F && F->LocalNumTypes > Index && "Broken chain");
Sebastian Redlb2831db2010-07-20 22:55:31 +00002449 return RecordLocation(&F->DeclsCursor, F->TypeOffsets[Index]);
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002450}
2451
2452/// \brief Read and return the type with the given index..
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002453///
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002454/// The index is the type ID, shifted and minus the number of predefs. This
2455/// routine actually reads the record corresponding to the type at the given
2456/// location. It is a helper routine for GetType, which deals with reading type
2457/// IDs.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002458QualType ASTReader::ReadTypeRecord(unsigned Index) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002459 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redlb2831db2010-07-20 22:55:31 +00002460 llvm::BitstreamCursor &DeclsCursor = *Loc.first;
Sebastian Redl34522812010-07-16 17:50:48 +00002461
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002462 // Keep track of where we are in the stream, then jump back there
2463 // after reading this type.
Douglas Gregor12bfa382009-10-17 00:13:19 +00002464 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002465
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00002466 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redleaa4ade2010-08-11 18:52:41 +00002467
Douglas Gregor1342e842009-07-06 18:54:52 +00002468 // Note that we are loading a type record.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00002469 Deserializing AType(this);
Mike Stump11289f42009-09-09 15:08:12 +00002470
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002471 DeclsCursor.JumpToBit(Loc.second);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002472 RecordData Record;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002473 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl539c5062010-08-18 23:57:32 +00002474 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
2475 case TYPE_EXT_QUAL: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002476 if (Record.size() != 2) {
2477 Error("Incorrect encoding of extended qualifier type");
2478 return QualType();
2479 }
Douglas Gregor455b8f42009-04-15 22:00:08 +00002480 QualType Base = GetType(Record[0]);
John McCall8ccfcb52009-09-24 19:53:00 +00002481 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]);
2482 return Context->getQualifiedType(Base, Quals);
Douglas Gregor455b8f42009-04-15 22:00:08 +00002483 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002484
Sebastian Redl539c5062010-08-18 23:57:32 +00002485 case TYPE_COMPLEX: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002486 if (Record.size() != 1) {
2487 Error("Incorrect encoding of complex type");
2488 return QualType();
2489 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002490 QualType ElemType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002491 return Context->getComplexType(ElemType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002492 }
2493
Sebastian Redl539c5062010-08-18 23:57:32 +00002494 case TYPE_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002495 if (Record.size() != 1) {
2496 Error("Incorrect encoding of pointer type");
2497 return QualType();
2498 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002499 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002500 return Context->getPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002501 }
2502
Sebastian Redl539c5062010-08-18 23:57:32 +00002503 case TYPE_BLOCK_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002504 if (Record.size() != 1) {
2505 Error("Incorrect encoding of block pointer type");
2506 return QualType();
2507 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002508 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002509 return Context->getBlockPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002510 }
2511
Sebastian Redl539c5062010-08-18 23:57:32 +00002512 case TYPE_LVALUE_REFERENCE: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002513 if (Record.size() != 1) {
2514 Error("Incorrect encoding of lvalue reference type");
2515 return QualType();
2516 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002517 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002518 return Context->getLValueReferenceType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002519 }
2520
Sebastian Redl539c5062010-08-18 23:57:32 +00002521 case TYPE_RVALUE_REFERENCE: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002522 if (Record.size() != 1) {
2523 Error("Incorrect encoding of rvalue reference type");
2524 return QualType();
2525 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002526 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002527 return Context->getRValueReferenceType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002528 }
2529
Sebastian Redl539c5062010-08-18 23:57:32 +00002530 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidisee776bc2010-07-02 11:55:15 +00002531 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002532 Error("Incorrect encoding of member pointer type");
2533 return QualType();
2534 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002535 QualType PointeeType = GetType(Record[0]);
2536 QualType ClassType = GetType(Record[1]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002537 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002538 }
2539
Sebastian Redl539c5062010-08-18 23:57:32 +00002540 case TYPE_CONSTANT_ARRAY: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002541 QualType ElementType = GetType(Record[0]);
2542 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2543 unsigned IndexTypeQuals = Record[2];
2544 unsigned Idx = 3;
2545 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor04318252009-07-06 15:59:29 +00002546 return Context->getConstantArrayType(ElementType, Size,
2547 ASM, IndexTypeQuals);
2548 }
2549
Sebastian Redl539c5062010-08-18 23:57:32 +00002550 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002551 QualType ElementType = GetType(Record[0]);
2552 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2553 unsigned IndexTypeQuals = Record[2];
Chris Lattner8575daa2009-04-27 21:45:14 +00002554 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002555 }
2556
Sebastian Redl539c5062010-08-18 23:57:32 +00002557 case TYPE_VARIABLE_ARRAY: {
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002558 QualType ElementType = GetType(Record[0]);
2559 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2560 unsigned IndexTypeQuals = Record[2];
Douglas Gregor04318252009-07-06 15:59:29 +00002561 SourceLocation LBLoc = SourceLocation::getFromRawEncoding(Record[3]);
2562 SourceLocation RBLoc = SourceLocation::getFromRawEncoding(Record[4]);
Sebastian Redlc67764e2010-07-22 22:43:28 +00002563 return Context->getVariableArrayType(ElementType, ReadExpr(DeclsCursor),
Douglas Gregor04318252009-07-06 15:59:29 +00002564 ASM, IndexTypeQuals,
2565 SourceRange(LBLoc, RBLoc));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002566 }
2567
Sebastian Redl539c5062010-08-18 23:57:32 +00002568 case TYPE_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00002569 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002570 Error("incorrect encoding of vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002571 return QualType();
2572 }
2573
2574 QualType ElementType = GetType(Record[0]);
2575 unsigned NumElements = Record[1];
Chris Lattner37141f42010-06-23 06:00:24 +00002576 unsigned AltiVecSpec = Record[2];
2577 return Context->getVectorType(ElementType, NumElements,
2578 (VectorType::AltiVecSpecific)AltiVecSpec);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002579 }
2580
Sebastian Redl539c5062010-08-18 23:57:32 +00002581 case TYPE_EXT_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00002582 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002583 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002584 return QualType();
2585 }
2586
2587 QualType ElementType = GetType(Record[0]);
2588 unsigned NumElements = Record[1];
Chris Lattner8575daa2009-04-27 21:45:14 +00002589 return Context->getExtVectorType(ElementType, NumElements);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002590 }
2591
Sebastian Redl539c5062010-08-18 23:57:32 +00002592 case TYPE_FUNCTION_NO_PROTO: {
Rafael Espindola49b85ab2010-03-30 22:15:11 +00002593 if (Record.size() != 4) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00002594 Error("incorrect encoding of no-proto function type");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002595 return QualType();
2596 }
2597 QualType ResultType = GetType(Record[0]);
Rafael Espindola49b85ab2010-03-30 22:15:11 +00002598 FunctionType::ExtInfo Info(Record[1], Record[2], (CallingConv)Record[3]);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00002599 return Context->getFunctionNoProtoType(ResultType, Info);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002600 }
2601
Sebastian Redl539c5062010-08-18 23:57:32 +00002602 case TYPE_FUNCTION_PROTO: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002603 QualType ResultType = GetType(Record[0]);
Douglas Gregordc728752009-12-22 18:11:50 +00002604 bool NoReturn = Record[1];
Rafael Espindola49b85ab2010-03-30 22:15:11 +00002605 unsigned RegParm = Record[2];
2606 CallingConv CallConv = (CallingConv)Record[3];
2607 unsigned Idx = 4;
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002608 unsigned NumParams = Record[Idx++];
2609 llvm::SmallVector<QualType, 16> ParamTypes;
2610 for (unsigned I = 0; I != NumParams; ++I)
2611 ParamTypes.push_back(GetType(Record[Idx++]));
2612 bool isVariadic = Record[Idx++];
2613 unsigned Quals = Record[Idx++];
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00002614 bool hasExceptionSpec = Record[Idx++];
2615 bool hasAnyExceptionSpec = Record[Idx++];
2616 unsigned NumExceptions = Record[Idx++];
2617 llvm::SmallVector<QualType, 2> Exceptions;
2618 for (unsigned I = 0; I != NumExceptions; ++I)
2619 Exceptions.push_back(GetType(Record[Idx++]));
Jay Foad7d0479f2009-05-21 09:52:38 +00002620 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00002621 isVariadic, Quals, hasExceptionSpec,
2622 hasAnyExceptionSpec, NumExceptions,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00002623 Exceptions.data(),
Rafael Espindola49b85ab2010-03-30 22:15:11 +00002624 FunctionType::ExtInfo(NoReturn, RegParm,
2625 CallConv));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002626 }
2627
Sebastian Redl539c5062010-08-18 23:57:32 +00002628 case TYPE_UNRESOLVED_USING:
John McCallb96ec562009-12-04 22:46:56 +00002629 return Context->getTypeDeclType(
2630 cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0])));
2631
Sebastian Redl539c5062010-08-18 23:57:32 +00002632 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002633 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002634 Error("incorrect encoding of typedef type");
2635 return QualType();
2636 }
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002637 TypedefDecl *Decl = cast<TypedefDecl>(GetDecl(Record[0]));
2638 QualType Canonical = GetType(Record[1]);
2639 return Context->getTypedefType(Decl, Canonical);
2640 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002641
Sebastian Redl539c5062010-08-18 23:57:32 +00002642 case TYPE_TYPEOF_EXPR:
Sebastian Redlc67764e2010-07-22 22:43:28 +00002643 return Context->getTypeOfExprType(ReadExpr(DeclsCursor));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002644
Sebastian Redl539c5062010-08-18 23:57:32 +00002645 case TYPE_TYPEOF: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002646 if (Record.size() != 1) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002647 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002648 return QualType();
2649 }
2650 QualType UnderlyingType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002651 return Context->getTypeOfType(UnderlyingType);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002652 }
Mike Stump11289f42009-09-09 15:08:12 +00002653
Sebastian Redl539c5062010-08-18 23:57:32 +00002654 case TYPE_DECLTYPE:
Sebastian Redlc67764e2010-07-22 22:43:28 +00002655 return Context->getDecltypeType(ReadExpr(DeclsCursor));
Anders Carlsson81df7b82009-06-24 19:06:50 +00002656
Sebastian Redl539c5062010-08-18 23:57:32 +00002657 case TYPE_RECORD: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002658 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002659 Error("incorrect encoding of record type");
2660 return QualType();
2661 }
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002662 bool IsDependent = Record[0];
2663 QualType T = Context->getRecordType(cast<RecordDecl>(GetDecl(Record[1])));
2664 T->Dependent = IsDependent;
2665 return T;
2666 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002667
Sebastian Redl539c5062010-08-18 23:57:32 +00002668 case TYPE_ENUM: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002669 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002670 Error("incorrect encoding of enum type");
2671 return QualType();
2672 }
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002673 bool IsDependent = Record[0];
2674 QualType T = Context->getEnumType(cast<EnumDecl>(GetDecl(Record[1])));
2675 T->Dependent = IsDependent;
2676 return T;
2677 }
Douglas Gregor1daeb692009-04-13 18:14:40 +00002678
Sebastian Redl539c5062010-08-18 23:57:32 +00002679 case TYPE_ELABORATED: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00002680 unsigned Idx = 0;
2681 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2682 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2683 QualType NamedType = GetType(Record[Idx++]);
2684 return Context->getElaboratedType(Keyword, NNS, NamedType);
John McCallfcc33b02009-09-05 00:15:47 +00002685 }
2686
Sebastian Redl539c5062010-08-18 23:57:32 +00002687 case TYPE_OBJC_INTERFACE: {
Chris Lattner587cbe12009-04-22 06:45:28 +00002688 unsigned Idx = 0;
2689 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
John McCall8b07ec22010-05-15 11:32:37 +00002690 return Context->getObjCInterfaceType(ItfD);
2691 }
2692
Sebastian Redl539c5062010-08-18 23:57:32 +00002693 case TYPE_OBJC_OBJECT: {
John McCall8b07ec22010-05-15 11:32:37 +00002694 unsigned Idx = 0;
2695 QualType Base = GetType(Record[Idx++]);
Chris Lattner587cbe12009-04-22 06:45:28 +00002696 unsigned NumProtos = Record[Idx++];
2697 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2698 for (unsigned I = 0; I != NumProtos; ++I)
2699 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
John McCall8b07ec22010-05-15 11:32:37 +00002700 return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattner587cbe12009-04-22 06:45:28 +00002701 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002702
Sebastian Redl539c5062010-08-18 23:57:32 +00002703 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattner6e054af2009-04-22 06:40:03 +00002704 unsigned Idx = 0;
John McCall8b07ec22010-05-15 11:32:37 +00002705 QualType Pointee = GetType(Record[Idx++]);
2706 return Context->getObjCObjectPointerType(Pointee);
Chris Lattner6e054af2009-04-22 06:40:03 +00002707 }
Argyrios Kyrtzidisa7a36df2009-09-29 19:42:55 +00002708
Sebastian Redl539c5062010-08-18 23:57:32 +00002709 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCallcebee162009-10-18 09:09:24 +00002710 unsigned Idx = 0;
2711 QualType Parm = GetType(Record[Idx++]);
2712 QualType Replacement = GetType(Record[Idx++]);
2713 return
2714 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
2715 Replacement);
2716 }
John McCalle78aac42010-03-10 03:28:59 +00002717
Sebastian Redl539c5062010-08-18 23:57:32 +00002718 case TYPE_INJECTED_CLASS_NAME: {
John McCalle78aac42010-03-10 03:28:59 +00002719 CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0]));
2720 QualType TST = GetType(Record[1]); // probably derivable
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00002721 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002722 // for AST reading, too much interdependencies.
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00002723 return
2724 QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCalle78aac42010-03-10 03:28:59 +00002725 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00002726
Sebastian Redl539c5062010-08-18 23:57:32 +00002727 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002728 unsigned Idx = 0;
2729 unsigned Depth = Record[Idx++];
2730 unsigned Index = Record[Idx++];
2731 bool Pack = Record[Idx++];
2732 IdentifierInfo *Name = GetIdentifierInfo(Record, Idx);
2733 return Context->getTemplateTypeParmType(Depth, Index, Pack, Name);
2734 }
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00002735
Sebastian Redl539c5062010-08-18 23:57:32 +00002736 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00002737 unsigned Idx = 0;
2738 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2739 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2740 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +00002741 QualType Canon = GetType(Record[Idx++]);
2742 return Context->getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00002743 }
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00002744
Sebastian Redl539c5062010-08-18 23:57:32 +00002745 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00002746 unsigned Idx = 0;
2747 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2748 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2749 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
2750 unsigned NumArgs = Record[Idx++];
2751 llvm::SmallVector<TemplateArgument, 8> Args;
2752 Args.reserve(NumArgs);
2753 while (NumArgs--)
Sebastian Redlc67764e2010-07-22 22:43:28 +00002754 Args.push_back(ReadTemplateArgument(DeclsCursor, Record, Idx));
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00002755 return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
2756 Args.size(), Args.data());
2757 }
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00002758
Sebastian Redl539c5062010-08-18 23:57:32 +00002759 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00002760 unsigned Idx = 0;
2761
2762 // ArrayType
2763 QualType ElementType = GetType(Record[Idx++]);
2764 ArrayType::ArraySizeModifier ASM
2765 = (ArrayType::ArraySizeModifier)Record[Idx++];
2766 unsigned IndexTypeQuals = Record[Idx++];
2767
2768 // DependentSizedArrayType
Sebastian Redlc67764e2010-07-22 22:43:28 +00002769 Expr *NumElts = ReadExpr(DeclsCursor);
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00002770 SourceRange Brackets = ReadSourceRange(Record, Idx);
2771
2772 return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
2773 IndexTypeQuals, Brackets);
2774 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00002775
Sebastian Redl539c5062010-08-18 23:57:32 +00002776 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002777 unsigned Idx = 0;
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002778 bool IsDependent = Record[Idx++];
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002779 TemplateName Name = ReadTemplateName(Record, Idx);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002780 llvm::SmallVector<TemplateArgument, 8> Args;
Sebastian Redlc67764e2010-07-22 22:43:28 +00002781 ReadTemplateArgumentList(Args, DeclsCursor, Record, Idx);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002782 QualType Canon = GetType(Record[Idx++]);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002783 QualType T;
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002784 if (Canon.isNull())
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002785 T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(),
2786 Args.size());
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002787 else
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002788 T = Context->getTemplateSpecializationType(Name, Args.data(),
2789 Args.size(), Canon);
2790 T->Dependent = IsDependent;
2791 return T;
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002792 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002793 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002794 // Suppress a GCC warning
2795 return QualType();
2796}
2797
John McCall8f115c62009-10-16 21:56:05 +00002798namespace {
2799
2800class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redl2c499f62010-08-18 23:56:43 +00002801 ASTReader &Reader;
Sebastian Redlc67764e2010-07-22 22:43:28 +00002802 llvm::BitstreamCursor &DeclsCursor;
Sebastian Redl2c499f62010-08-18 23:56:43 +00002803 const ASTReader::RecordData &Record;
John McCall8f115c62009-10-16 21:56:05 +00002804 unsigned &Idx;
2805
2806public:
Sebastian Redl2c499f62010-08-18 23:56:43 +00002807 TypeLocReader(ASTReader &Reader, llvm::BitstreamCursor &Cursor,
2808 const ASTReader::RecordData &Record, unsigned &Idx)
Sebastian Redlc67764e2010-07-22 22:43:28 +00002809 : Reader(Reader), DeclsCursor(Cursor), Record(Record), Idx(Idx) { }
John McCall8f115c62009-10-16 21:56:05 +00002810
John McCall17001972009-10-18 01:05:36 +00002811 // We want compile-time assurance that we've enumerated all of
2812 // these, so unfortunately we have to declare them first, then
2813 // define them out-of-line.
2814#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +00002815#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +00002816 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +00002817#include "clang/AST/TypeLocNodes.def"
2818
John McCall17001972009-10-18 01:05:36 +00002819 void VisitFunctionTypeLoc(FunctionTypeLoc);
2820 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCall8f115c62009-10-16 21:56:05 +00002821};
2822
2823}
2824
John McCall17001972009-10-18 01:05:36 +00002825void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCall8f115c62009-10-16 21:56:05 +00002826 // nothing to do
2827}
John McCall17001972009-10-18 01:05:36 +00002828void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +00002829 TL.setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2830 if (TL.needsExtraLocalData()) {
2831 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
2832 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
2833 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
2834 TL.setModeAttr(Record[Idx++]);
2835 }
John McCall8f115c62009-10-16 21:56:05 +00002836}
John McCall17001972009-10-18 01:05:36 +00002837void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
2838 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall8f115c62009-10-16 21:56:05 +00002839}
John McCall17001972009-10-18 01:05:36 +00002840void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
2841 TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall8f115c62009-10-16 21:56:05 +00002842}
John McCall17001972009-10-18 01:05:36 +00002843void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
2844 TL.setCaretLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall8f115c62009-10-16 21:56:05 +00002845}
John McCall17001972009-10-18 01:05:36 +00002846void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
2847 TL.setAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall8f115c62009-10-16 21:56:05 +00002848}
John McCall17001972009-10-18 01:05:36 +00002849void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
2850 TL.setAmpAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall8f115c62009-10-16 21:56:05 +00002851}
John McCall17001972009-10-18 01:05:36 +00002852void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
2853 TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall8f115c62009-10-16 21:56:05 +00002854}
John McCall17001972009-10-18 01:05:36 +00002855void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
2856 TL.setLBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2857 TL.setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall8f115c62009-10-16 21:56:05 +00002858 if (Record[Idx++])
Sebastian Redlc67764e2010-07-22 22:43:28 +00002859 TL.setSizeExpr(Reader.ReadExpr(DeclsCursor));
Douglas Gregor12bfa382009-10-17 00:13:19 +00002860 else
John McCall17001972009-10-18 01:05:36 +00002861 TL.setSizeExpr(0);
2862}
2863void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
2864 VisitArrayTypeLoc(TL);
2865}
2866void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
2867 VisitArrayTypeLoc(TL);
2868}
2869void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
2870 VisitArrayTypeLoc(TL);
2871}
2872void TypeLocReader::VisitDependentSizedArrayTypeLoc(
2873 DependentSizedArrayTypeLoc TL) {
2874 VisitArrayTypeLoc(TL);
2875}
2876void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
2877 DependentSizedExtVectorTypeLoc TL) {
2878 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2879}
2880void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
2881 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2882}
2883void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
2884 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2885}
2886void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
2887 TL.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2888 TL.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2889 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
John McCalle6347002009-10-23 01:28:53 +00002890 TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
John McCall17001972009-10-18 01:05:36 +00002891 }
2892}
2893void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
2894 VisitFunctionTypeLoc(TL);
2895}
2896void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
2897 VisitFunctionTypeLoc(TL);
2898}
John McCallb96ec562009-12-04 22:46:56 +00002899void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
2900 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2901}
John McCall17001972009-10-18 01:05:36 +00002902void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
2903 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2904}
2905void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +00002906 TL.setTypeofLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2907 TL.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2908 TL.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall17001972009-10-18 01:05:36 +00002909}
2910void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +00002911 TL.setTypeofLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2912 TL.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2913 TL.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Sebastian Redlc67764e2010-07-22 22:43:28 +00002914 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(DeclsCursor, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00002915}
2916void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
2917 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2918}
2919void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
2920 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2921}
2922void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
2923 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2924}
John McCall17001972009-10-18 01:05:36 +00002925void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
2926 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2927}
John McCallcebee162009-10-18 09:09:24 +00002928void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
2929 SubstTemplateTypeParmTypeLoc TL) {
2930 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2931}
John McCall17001972009-10-18 01:05:36 +00002932void TypeLocReader::VisitTemplateSpecializationTypeLoc(
2933 TemplateSpecializationTypeLoc TL) {
John McCall0ad16662009-10-29 08:12:44 +00002934 TL.setTemplateNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2935 TL.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2936 TL.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2937 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
2938 TL.setArgLocInfo(i,
2939 Reader.GetTemplateArgumentLocInfo(TL.getTypePtr()->getArg(i).getKind(),
Sebastian Redlc67764e2010-07-22 22:43:28 +00002940 DeclsCursor, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00002941}
Abramo Bagnara6150c882010-05-11 21:36:43 +00002942void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +00002943 TL.setKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2944 TL.setQualifierRange(Reader.ReadSourceRange(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00002945}
John McCalle78aac42010-03-10 03:28:59 +00002946void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
2947 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2948}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00002949void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +00002950 TL.setKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2951 TL.setQualifierRange(Reader.ReadSourceRange(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00002952 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2953}
John McCallc392f372010-06-11 00:33:02 +00002954void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
2955 DependentTemplateSpecializationTypeLoc TL) {
2956 TL.setKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2957 TL.setQualifierRange(Reader.ReadSourceRange(Record, Idx));
2958 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2959 TL.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2960 TL.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2961 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
2962 TL.setArgLocInfo(I,
2963 Reader.GetTemplateArgumentLocInfo(TL.getTypePtr()->getArg(I).getKind(),
Sebastian Redlc67764e2010-07-22 22:43:28 +00002964 DeclsCursor, Record, Idx));
John McCallc392f372010-06-11 00:33:02 +00002965}
John McCall17001972009-10-18 01:05:36 +00002966void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
2967 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall8b07ec22010-05-15 11:32:37 +00002968}
2969void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
2970 TL.setHasBaseTypeAsWritten(Record[Idx++]);
John McCall17001972009-10-18 01:05:36 +00002971 TL.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2972 TL.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2973 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
2974 TL.setProtocolLoc(i, SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall8f115c62009-10-16 21:56:05 +00002975}
John McCallfc93cf92009-10-22 22:37:11 +00002976void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
2977 TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCallfc93cf92009-10-22 22:37:11 +00002978}
John McCall8f115c62009-10-16 21:56:05 +00002979
Sebastian Redl2c499f62010-08-18 23:56:43 +00002980TypeSourceInfo *ASTReader::GetTypeSourceInfo(llvm::BitstreamCursor &DeclsCursor,
Sebastian Redlc67764e2010-07-22 22:43:28 +00002981 const RecordData &Record,
John McCall8f115c62009-10-16 21:56:05 +00002982 unsigned &Idx) {
2983 QualType InfoTy = GetType(Record[Idx++]);
2984 if (InfoTy.isNull())
2985 return 0;
2986
John McCallbcd03502009-12-07 02:54:59 +00002987 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
Sebastian Redlc67764e2010-07-22 22:43:28 +00002988 TypeLocReader TLR(*this, DeclsCursor, Record, Idx);
John McCallbcd03502009-12-07 02:54:59 +00002989 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCall8f115c62009-10-16 21:56:05 +00002990 TLR.Visit(TL);
John McCallbcd03502009-12-07 02:54:59 +00002991 return TInfo;
John McCall8f115c62009-10-16 21:56:05 +00002992}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002993
Sebastian Redl539c5062010-08-18 23:57:32 +00002994QualType ASTReader::GetType(TypeID ID) {
John McCall8ccfcb52009-09-24 19:53:00 +00002995 unsigned FastQuals = ID & Qualifiers::FastMask;
2996 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002997
Sebastian Redl539c5062010-08-18 23:57:32 +00002998 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002999 QualType T;
Sebastian Redl539c5062010-08-18 23:57:32 +00003000 switch ((PredefinedTypeIDs)Index) {
3001 case PREDEF_TYPE_NULL_ID: return QualType();
3002 case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
3003 case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003004
Sebastian Redl539c5062010-08-18 23:57:32 +00003005 case PREDEF_TYPE_CHAR_U_ID:
3006 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003007 // FIXME: Check that the signedness of CharTy is correct!
Chris Lattner8575daa2009-04-27 21:45:14 +00003008 T = Context->CharTy;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003009 break;
3010
Sebastian Redl539c5062010-08-18 23:57:32 +00003011 case PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break;
3012 case PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break;
3013 case PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break;
3014 case PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break;
3015 case PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break;
3016 case PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break;
3017 case PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break;
3018 case PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break;
3019 case PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break;
3020 case PREDEF_TYPE_INT_ID: T = Context->IntTy; break;
3021 case PREDEF_TYPE_LONG_ID: T = Context->LongTy; break;
3022 case PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break;
3023 case PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break;
3024 case PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break;
3025 case PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break;
3026 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break;
3027 case PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break;
3028 case PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break;
3029 case PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break;
3030 case PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break;
3031 case PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break;
3032 case PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break;
3033 case PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break;
3034 case PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003035 }
3036
3037 assert(!T.isNull() && "Unknown predefined type");
John McCall8ccfcb52009-09-24 19:53:00 +00003038 return T.withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003039 }
3040
Sebastian Redl539c5062010-08-18 23:57:32 +00003041 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003042 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl409183f2010-07-14 20:26:45 +00003043 if (TypesLoaded[Index].isNull()) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003044 TypesLoaded[Index] = ReadTypeRecord(Index);
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003045 TypesLoaded[Index]->setFromAST();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003046 TypeIdxs[TypesLoaded[Index]] = TypeIdx::fromTypeID(ID);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003047 if (DeserializationListener)
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003048 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003049 TypesLoaded[Index]);
Sebastian Redl409183f2010-07-14 20:26:45 +00003050 }
Mike Stump11289f42009-09-09 15:08:12 +00003051
John McCall8ccfcb52009-09-24 19:53:00 +00003052 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003053}
3054
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003055TypeID ASTReader::GetTypeID(QualType T) const {
3056 return MakeTypeID(T,
3057 std::bind1st(std::mem_fun(&ASTReader::GetTypeIdx), this));
3058}
3059
3060TypeIdx ASTReader::GetTypeIdx(QualType T) const {
3061 if (T.isNull())
3062 return TypeIdx();
3063 assert(!T.getLocalFastQualifiers());
3064
3065 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3066 // GetTypeIdx is mostly used for computing the hash of DeclarationNames and
3067 // comparing keys of ASTDeclContextNameLookupTable.
3068 // If the type didn't come from the AST file use a specially marked index
3069 // so that any hash/key comparison fail since no such index is stored
3070 // in a AST file.
3071 if (I == TypeIdxs.end())
3072 return TypeIdx(-1);
3073 return I->second;
3074}
3075
John McCall0ad16662009-10-29 08:12:44 +00003076TemplateArgumentLocInfo
Sebastian Redl2c499f62010-08-18 23:56:43 +00003077ASTReader::GetTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003078 llvm::BitstreamCursor &DeclsCursor,
John McCall0ad16662009-10-29 08:12:44 +00003079 const RecordData &Record,
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003080 unsigned &Index) {
John McCall0ad16662009-10-29 08:12:44 +00003081 switch (Kind) {
3082 case TemplateArgument::Expression:
Sebastian Redlc67764e2010-07-22 22:43:28 +00003083 return ReadExpr(DeclsCursor);
John McCall0ad16662009-10-29 08:12:44 +00003084 case TemplateArgument::Type:
Sebastian Redlc67764e2010-07-22 22:43:28 +00003085 return GetTypeSourceInfo(DeclsCursor, Record, Index);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003086 case TemplateArgument::Template: {
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003087 SourceRange QualifierRange = ReadSourceRange(Record, Index);
3088 SourceLocation TemplateNameLoc = ReadSourceLocation(Record, Index);
3089 return TemplateArgumentLocInfo(QualifierRange, TemplateNameLoc);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003090 }
John McCall0ad16662009-10-29 08:12:44 +00003091 case TemplateArgument::Null:
3092 case TemplateArgument::Integral:
3093 case TemplateArgument::Declaration:
3094 case TemplateArgument::Pack:
3095 return TemplateArgumentLocInfo();
3096 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +00003097 llvm_unreachable("unexpected template argument loc");
John McCall0ad16662009-10-29 08:12:44 +00003098 return TemplateArgumentLocInfo();
3099}
3100
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003101TemplateArgumentLoc
Sebastian Redl2c499f62010-08-18 23:56:43 +00003102ASTReader::ReadTemplateArgumentLoc(llvm::BitstreamCursor &DeclsCursor,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003103 const RecordData &Record, unsigned &Index) {
3104 TemplateArgument Arg = ReadTemplateArgument(DeclsCursor, Record, Index);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003105
3106 if (Arg.getKind() == TemplateArgument::Expression) {
3107 if (Record[Index++]) // bool InfoHasSameExpr.
3108 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
3109 }
3110 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(Arg.getKind(),
Sebastian Redlc67764e2010-07-22 22:43:28 +00003111 DeclsCursor,
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003112 Record, Index));
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003113}
3114
Sebastian Redl2c499f62010-08-18 23:56:43 +00003115Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall75b960e2010-06-01 09:23:16 +00003116 return GetDecl(ID);
3117}
3118
Sebastian Redl2c499f62010-08-18 23:56:43 +00003119TranslationUnitDecl *ASTReader::GetTranslationUnitDecl() {
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003120 if (!DeclsLoaded[0]) {
Sebastian Redld7dce0a2010-08-24 00:50:04 +00003121 ReadDeclRecord(0, 1);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003122 if (DeserializationListener)
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003123 DeserializationListener->DeclRead(1, DeclsLoaded[0]);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003124 }
Argyrios Kyrtzidis7e8996c2010-07-08 17:13:02 +00003125
3126 return cast<TranslationUnitDecl>(DeclsLoaded[0]);
3127}
3128
Sebastian Redl539c5062010-08-18 23:57:32 +00003129Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003130 if (ID == 0)
3131 return 0;
3132
Douglas Gregor745ed142009-04-25 18:35:21 +00003133 if (ID > DeclsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003134 Error("declaration ID out-of-range for AST file");
Douglas Gregor745ed142009-04-25 18:35:21 +00003135 return 0;
3136 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003137
Douglas Gregor745ed142009-04-25 18:35:21 +00003138 unsigned Index = ID - 1;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003139 if (!DeclsLoaded[Index]) {
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003140 ReadDeclRecord(Index, ID);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003141 if (DeserializationListener)
3142 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
3143 }
Douglas Gregor745ed142009-04-25 18:35:21 +00003144
3145 return DeclsLoaded[Index];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003146}
3147
Chris Lattner9c28af02009-04-27 05:46:25 +00003148/// \brief Resolve the offset of a statement into a statement.
3149///
3150/// This operation will read a new statement from the external
3151/// source each time it is called, and is meant to be used via a
3152/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redl2c499f62010-08-18 23:56:43 +00003153Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Sebastian Redl5c415f32010-07-22 17:01:13 +00003154 // Offset here is a global offset across the entire chain.
3155 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3156 PerFileData &F = *Chain[N - I - 1];
3157 if (Offset < F.SizeInBits) {
3158 // Since we know that this statement is part of a decl, make sure to use
3159 // the decl cursor to read it.
3160 F.DeclsCursor.JumpToBit(Offset);
3161 return ReadStmtFromStream(F.DeclsCursor);
3162 }
3163 Offset -= F.SizeInBits;
3164 }
3165 llvm_unreachable("Broken chain");
Douglas Gregor3c3aa612009-04-18 00:07:54 +00003166}
3167
Sebastian Redl2c499f62010-08-18 23:56:43 +00003168bool ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
John McCall75b960e2010-06-01 09:23:16 +00003169 llvm::SmallVectorImpl<Decl*> &Decls) {
Mike Stump11289f42009-09-09 15:08:12 +00003170 assert(DC->hasExternalLexicalStorage() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003171 "DeclContext has no lexical decls in storage");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003172
Sebastian Redl5c415f32010-07-22 17:01:13 +00003173 // There might be lexical decls in multiple parts of the chain, for the TU
3174 // at least.
3175 DeclContextInfos &Infos = DeclContextOffsets[DC];
3176 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3177 I != E; ++I) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003178 // IDs can be 0 if this context doesn't contain declarations.
3179 if (!I->LexicalDecls)
Sebastian Redl5c415f32010-07-22 17:01:13 +00003180 continue;
Sebastian Redl5c415f32010-07-22 17:01:13 +00003181
3182 // Load all of the declaration IDs
Sebastian Redl539c5062010-08-18 23:57:32 +00003183 for (const DeclID *ID = I->LexicalDecls,
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003184 *IDE = ID + I->NumLexicalDecls;
3185 ID != IDE; ++ID)
3186 Decls.push_back(GetDecl(*ID));
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003187 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003188
Douglas Gregora57c3ab2009-04-22 22:34:57 +00003189 ++NumLexicalDeclContextsRead;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003190 return false;
3191}
3192
John McCall75b960e2010-06-01 09:23:16 +00003193DeclContext::lookup_result
Sebastian Redl2c499f62010-08-18 23:56:43 +00003194ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall75b960e2010-06-01 09:23:16 +00003195 DeclarationName Name) {
Mike Stump11289f42009-09-09 15:08:12 +00003196 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003197 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003198 if (!Name)
3199 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
3200 DeclContext::lookup_iterator(0));
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003201
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003202 llvm::SmallVector<NamedDecl *, 64> Decls;
Sebastian Redl471ac2f2010-08-24 00:49:55 +00003203 // There might be visible decls in multiple parts of the chain, for the TU
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003204 // and namespaces. For any given name, the last available results replace
3205 // all earlier ones. For this reason, we walk in reverse.
Sebastian Redl5c415f32010-07-22 17:01:13 +00003206 DeclContextInfos &Infos = DeclContextOffsets[DC];
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003207 for (DeclContextInfos::reverse_iterator I = Infos.rbegin(), E = Infos.rend();
Sebastian Redl5c415f32010-07-22 17:01:13 +00003208 I != E; ++I) {
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003209 if (!I->NameLookupTableData)
Sebastian Redl5c415f32010-07-22 17:01:13 +00003210 continue;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003211
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003212 ASTDeclContextNameLookupTable *LookupTable =
3213 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3214 ASTDeclContextNameLookupTable::iterator Pos = LookupTable->find(Name);
3215 if (Pos == LookupTable->end())
Sebastian Redl5c415f32010-07-22 17:01:13 +00003216 continue;
3217
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003218 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
3219 for (; Data.first != Data.second; ++Data.first)
3220 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003221 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003222 }
3223
Douglas Gregora57c3ab2009-04-22 22:34:57 +00003224 ++NumVisibleDeclContextsRead;
John McCall75b960e2010-06-01 09:23:16 +00003225
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003226 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall75b960e2010-06-01 09:23:16 +00003227 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003228}
3229
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00003230void ASTReader::MaterializeVisibleDecls(const DeclContext *DC) {
3231 assert(DC->hasExternalVisibleStorage() &&
3232 "DeclContext has no visible decls in storage");
3233
3234 llvm::SmallVector<NamedDecl *, 64> Decls;
3235 // There might be visible decls in multiple parts of the chain, for the TU
3236 // and namespaces.
3237 DeclContextInfos &Infos = DeclContextOffsets[DC];
3238 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3239 I != E; ++I) {
3240 if (!I->NameLookupTableData)
3241 continue;
3242
3243 ASTDeclContextNameLookupTable *LookupTable =
3244 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3245 for (ASTDeclContextNameLookupTable::item_iterator
3246 ItemI = LookupTable->item_begin(),
3247 ItemEnd = LookupTable->item_end() ; ItemI != ItemEnd; ++ItemI) {
3248 ASTDeclContextNameLookupTable::item_iterator::value_type Val
3249 = *ItemI;
3250 ASTDeclContextNameLookupTrait::data_type Data = Val.second;
3251 Decls.clear();
3252 for (; Data.first != Data.second; ++Data.first)
3253 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
3254 MaterializeVisibleDeclsForName(DC, Val.first, Decls);
3255 }
3256 }
3257}
3258
Sebastian Redl2c499f62010-08-18 23:56:43 +00003259void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003260 assert(Consumer);
3261 while (!InterestingDecls.empty()) {
3262 DeclGroupRef DG(InterestingDecls.front());
3263 InterestingDecls.pop_front();
Sebastian Redleaa4ade2010-08-11 18:52:41 +00003264 Consumer->HandleInterestingDecl(DG);
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003265 }
3266}
3267
Sebastian Redl2c499f62010-08-18 23:56:43 +00003268void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregorb985eeb2009-04-22 19:09:20 +00003269 this->Consumer = Consumer;
3270
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003271 if (!Consumer)
3272 return;
3273
3274 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003275 // Force deserialization of this decl, which will cause it to be queued for
3276 // passing to the consumer.
Daniel Dunbar865c2a72009-09-17 03:06:44 +00003277 GetDecl(ExternalDefinitions[I]);
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003278 }
Douglas Gregorf005eac2009-04-25 00:41:30 +00003279
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003280 PassInterestingDeclsToConsumer();
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003281}
3282
Sebastian Redl2c499f62010-08-18 23:56:43 +00003283void ASTReader::PrintStats() {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003284 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003285
Mike Stump11289f42009-09-09 15:08:12 +00003286 unsigned NumTypesLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00003287 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall8ccfcb52009-09-24 19:53:00 +00003288 QualType());
Douglas Gregor0e149972009-04-25 19:10:14 +00003289 unsigned NumDeclsLoaded
3290 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
3291 (Decl *)0);
3292 unsigned NumIdentifiersLoaded
3293 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
3294 IdentifiersLoaded.end(),
3295 (IdentifierInfo *)0);
Mike Stump11289f42009-09-09 15:08:12 +00003296 unsigned NumSelectorsLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00003297 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
3298 SelectorsLoaded.end(),
3299 Selector());
Douglas Gregorc3b1dd12009-04-13 20:50:16 +00003300
Douglas Gregorc5046832009-04-27 18:38:38 +00003301 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
3302 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor258ae542009-04-27 06:38:32 +00003303 if (TotalNumSLocEntries)
3304 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
3305 NumSLocEntriesRead, TotalNumSLocEntries,
3306 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor745ed142009-04-25 18:35:21 +00003307 if (!TypesLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003308 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00003309 NumTypesLoaded, (unsigned)TypesLoaded.size(),
3310 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
3311 if (!DeclsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003312 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00003313 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
3314 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor0e149972009-04-25 19:10:14 +00003315 if (!IdentifiersLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003316 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor0e149972009-04-25 19:10:14 +00003317 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
3318 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redlada023c2010-08-04 20:40:17 +00003319 if (!SelectorsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003320 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redlada023c2010-08-04 20:40:17 +00003321 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
3322 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003323 if (TotalNumStatements)
3324 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
3325 NumStatementsRead, TotalNumStatements,
3326 ((float)NumStatementsRead/TotalNumStatements * 100));
3327 if (TotalNumMacros)
3328 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
3329 NumMacrosRead, TotalNumMacros,
3330 ((float)NumMacrosRead/TotalNumMacros * 100));
3331 if (TotalLexicalDeclContexts)
3332 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
3333 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
3334 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
3335 * 100));
3336 if (TotalVisibleDeclContexts)
3337 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
3338 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
3339 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
3340 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003341 if (TotalNumMethodPoolEntries) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00003342 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003343 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
3344 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor95c13f52009-04-25 17:48:32 +00003345 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003346 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor95c13f52009-04-25 17:48:32 +00003347 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003348 std::fprintf(stderr, "\n");
3349}
3350
Sebastian Redl2c499f62010-08-18 23:56:43 +00003351void ASTReader::InitializeSema(Sema &S) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00003352 SemaObj = &S;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003353 S.ExternalSource = this;
3354
Douglas Gregor7cd60f72009-04-22 21:15:06 +00003355 // Makes sure any declarations that were deserialized "too early"
3356 // still get added to the identifier's declaration chains.
Douglas Gregor6fd55e02010-08-13 03:15:25 +00003357 if (SemaObj->TUScope) {
3358 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
John McCall48871652010-08-21 09:40:31 +00003359 SemaObj->TUScope->AddDecl(PreloadedDecls[I]);
Douglas Gregor6fd55e02010-08-13 03:15:25 +00003360 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
3361 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00003362 }
Douglas Gregor7cd60f72009-04-22 21:15:06 +00003363 PreloadedDecls.clear();
Douglas Gregord4df8652009-04-22 22:02:47 +00003364
3365 // If there were any tentative definitions, deserialize them and add
Sebastian Redl35351a92010-01-31 22:27:38 +00003366 // them to Sema's list of tentative definitions.
Douglas Gregord4df8652009-04-22 22:02:47 +00003367 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
3368 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
Sebastian Redl35351a92010-01-31 22:27:38 +00003369 SemaObj->TentativeDefinitions.push_back(Var);
Douglas Gregord4df8652009-04-22 22:02:47 +00003370 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00003371
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003372 // If there were any unused file scoped decls, deserialize them and add to
3373 // Sema's list of unused file scoped decls.
3374 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
3375 DeclaratorDecl *D = cast<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
3376 SemaObj->UnusedFileScopedDecls.push_back(D);
Tanya Lattner90073802010-02-12 00:07:30 +00003377 }
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003378
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003379 // If there were any weak undeclared identifiers, deserialize them and add to
3380 // Sema's list of weak undeclared identifiers.
3381 if (!WeakUndeclaredIdentifiers.empty()) {
3382 unsigned Idx = 0;
3383 for (unsigned I = 0, N = WeakUndeclaredIdentifiers[Idx++]; I != N; ++I) {
3384 IdentifierInfo *WeakId = GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3385 IdentifierInfo *AliasId=GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3386 SourceLocation Loc = ReadSourceLocation(WeakUndeclaredIdentifiers, Idx);
3387 bool Used = WeakUndeclaredIdentifiers[Idx++];
3388 Sema::WeakInfo WI(AliasId, Loc);
3389 WI.setUsed(Used);
3390 SemaObj->WeakUndeclaredIdentifiers.insert(std::make_pair(WeakId, WI));
3391 }
3392 }
3393
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003394 // If there were any locally-scoped external declarations,
3395 // deserialize them and add them to Sema's table of locally-scoped
3396 // external declarations.
3397 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
3398 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
3399 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
3400 }
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003401
3402 // If there were any ext_vector type declarations, deserialize them
3403 // and add them to Sema's vector of such declarations.
3404 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I)
3405 SemaObj->ExtVectorDecls.push_back(
3406 cast<TypedefDecl>(GetDecl(ExtVectorDecls[I])));
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003407
3408 // FIXME: Do VTable uses and dynamic classes deserialize too much ?
3409 // Can we cut them down before writing them ?
3410
3411 // If there were any VTable uses, deserialize the information and add it
3412 // to Sema's vector and map of VTable uses.
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00003413 if (!VTableUses.empty()) {
3414 unsigned Idx = 0;
3415 for (unsigned I = 0, N = VTableUses[Idx++]; I != N; ++I) {
3416 CXXRecordDecl *Class = cast<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
3417 SourceLocation Loc = ReadSourceLocation(VTableUses, Idx);
3418 bool DefinitionRequired = VTableUses[Idx++];
3419 SemaObj->VTableUses.push_back(std::make_pair(Class, Loc));
3420 SemaObj->VTablesUsed[Class] = DefinitionRequired;
3421 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003422 }
3423
3424 // If there were any dynamic classes declarations, deserialize them
3425 // and add them to Sema's vector of such declarations.
3426 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I)
3427 SemaObj->DynamicClasses.push_back(
3428 cast<CXXRecordDecl>(GetDecl(DynamicClasses[I])));
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003429
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003430 // If there were any pending implicit instantiations, deserialize them
3431 // and add them to Sema's queue of such instantiations.
3432 assert(PendingImplicitInstantiations.size() % 2 == 0 &&
3433 "Expected pairs of entries");
3434 for (unsigned Idx = 0, N = PendingImplicitInstantiations.size(); Idx < N;) {
3435 ValueDecl *D=cast<ValueDecl>(GetDecl(PendingImplicitInstantiations[Idx++]));
3436 SourceLocation Loc = ReadSourceLocation(PendingImplicitInstantiations, Idx);
3437 SemaObj->PendingImplicitInstantiations.push_back(std::make_pair(D, Loc));
3438 }
3439
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00003440 // Load the offsets of the declarations that Sema references.
3441 // They will be lazily deserialized when needed.
3442 if (!SemaDeclRefs.empty()) {
3443 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
3444 SemaObj->StdNamespace = SemaDeclRefs[0];
3445 SemaObj->StdBadAlloc = SemaDeclRefs[1];
3446 }
3447
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003448 // If there are @selector references added them to its pool. This is for
3449 // implementation of -Wselector.
Sebastian Redlada023c2010-08-04 20:40:17 +00003450 if (!ReferencedSelectorsData.empty()) {
3451 unsigned int DataSize = ReferencedSelectorsData.size()-1;
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003452 unsigned I = 0;
3453 while (I < DataSize) {
Sebastian Redlada023c2010-08-04 20:40:17 +00003454 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003455 SourceLocation SelLoc =
Sebastian Redlada023c2010-08-04 20:40:17 +00003456 SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003457 SemaObj->ReferencedSelectors.insert(std::make_pair(Sel, SelLoc));
3458 }
3459 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00003460}
3461
Sebastian Redl2c499f62010-08-18 23:56:43 +00003462IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Sebastian Redl78f51772010-08-02 18:30:12 +00003463 // Try to find this name within our on-disk hash tables. We start with the
3464 // most recent one, since that one contains the most up-to-date info.
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003465 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003466 ASTIdentifierLookupTable *IdTable
3467 = (ASTIdentifierLookupTable *)Chain[I]->IdentifierLookupTable;
Sebastian Redl5c415f32010-07-22 17:01:13 +00003468 if (!IdTable)
3469 continue;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003470 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003471 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003472 if (Pos == IdTable->end())
3473 continue;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003474
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003475 // Dereferencing the iterator has the effect of building the
3476 // IdentifierInfo node and populating it with the various
3477 // declarations it needs.
Sebastian Redl78f51772010-08-02 18:30:12 +00003478 return *Pos;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003479 }
Sebastian Redl78f51772010-08-02 18:30:12 +00003480 return 0;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003481}
3482
Mike Stump11289f42009-09-09 15:08:12 +00003483std::pair<ObjCMethodList, ObjCMethodList>
Sebastian Redl2c499f62010-08-18 23:56:43 +00003484ASTReader::ReadMethodPool(Selector Sel) {
Sebastian Redlada023c2010-08-04 20:40:17 +00003485 // Find this selector in a hash table. We want to find the most recent entry.
3486 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3487 PerFileData &F = *Chain[I];
3488 if (!F.SelectorLookupTable)
3489 continue;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003490
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003491 ASTSelectorLookupTable *PoolTable
3492 = (ASTSelectorLookupTable*)F.SelectorLookupTable;
3493 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
Sebastian Redlada023c2010-08-04 20:40:17 +00003494 if (Pos != PoolTable->end()) {
3495 ++NumSelectorsRead;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003496 // FIXME: Not quite happy with the statistics here. We probably should
3497 // disable this tracking when called via LoadSelector.
3498 // Also, should entries without methods count as misses?
3499 ++NumMethodPoolEntriesRead;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003500 ASTSelectorLookupTrait::data_type Data = *Pos;
Sebastian Redlada023c2010-08-04 20:40:17 +00003501 if (DeserializationListener)
3502 DeserializationListener->SelectorRead(Data.ID, Sel);
3503 return std::make_pair(Data.Instance, Data.Factory);
3504 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00003505 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00003506
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003507 ++NumMethodPoolMisses;
Sebastian Redlada023c2010-08-04 20:40:17 +00003508 return std::pair<ObjCMethodList, ObjCMethodList>();
Douglas Gregorc78d3462009-04-24 21:10:55 +00003509}
3510
Sebastian Redl2c499f62010-08-18 23:56:43 +00003511void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003512 // It would be complicated to avoid reading the methods anyway. So don't.
3513 ReadMethodPool(Sel);
3514}
3515
Sebastian Redl2c499f62010-08-18 23:56:43 +00003516void ASTReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00003517 assert(ID && "Non-zero identifier ID required");
Douglas Gregor6f00bf82009-04-28 21:53:25 +00003518 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor0e149972009-04-25 19:10:14 +00003519 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlff4a2952010-07-23 23:49:55 +00003520 if (DeserializationListener)
3521 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003522}
3523
Douglas Gregor1342e842009-07-06 18:54:52 +00003524/// \brief Set the globally-visible declarations associated with the given
3525/// identifier.
3526///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003527/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump11289f42009-09-09 15:08:12 +00003528/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregor1342e842009-07-06 18:54:52 +00003529/// them.
3530///
3531/// \param II an IdentifierInfo that refers to one or more globally-visible
3532/// declarations.
3533///
3534/// \param DeclIDs the set of declaration IDs with the name @p II that are
3535/// visible at global scope.
3536///
3537/// \param Nonrecursive should be true to indicate that the caller knows that
3538/// this call is non-recursive, and therefore the globally-visible declarations
3539/// will not be placed onto the pending queue.
Mike Stump11289f42009-09-09 15:08:12 +00003540void
Sebastian Redl2c499f62010-08-18 23:56:43 +00003541ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Douglas Gregor1342e842009-07-06 18:54:52 +00003542 const llvm::SmallVectorImpl<uint32_t> &DeclIDs,
3543 bool Nonrecursive) {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00003544 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregor1342e842009-07-06 18:54:52 +00003545 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
3546 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
3547 PII.II = II;
3548 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I)
3549 PII.DeclIDs.push_back(DeclIDs[I]);
3550 return;
3551 }
Mike Stump11289f42009-09-09 15:08:12 +00003552
Douglas Gregor1342e842009-07-06 18:54:52 +00003553 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
3554 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
3555 if (SemaObj) {
Douglas Gregor6fd55e02010-08-13 03:15:25 +00003556 if (SemaObj->TUScope) {
3557 // Introduce this declaration into the translation-unit scope
3558 // and add it to the declaration chain for this identifier, so
3559 // that (unqualified) name lookup will find it.
John McCall48871652010-08-21 09:40:31 +00003560 SemaObj->TUScope->AddDecl(D);
Douglas Gregor6fd55e02010-08-13 03:15:25 +00003561 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
3562 }
Douglas Gregor1342e842009-07-06 18:54:52 +00003563 } else {
3564 // Queue this declaration so that it will be added to the
3565 // translation unit scope and identifier's declaration chain
3566 // once a Sema object is known.
3567 PreloadedDecls.push_back(D);
3568 }
3569 }
3570}
3571
Sebastian Redl2c499f62010-08-18 23:56:43 +00003572IdentifierInfo *ASTReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003573 if (ID == 0)
3574 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00003575
Sebastian Redlc713b962010-07-21 00:46:22 +00003576 if (IdentifiersLoaded.empty()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003577 Error("no identifier table in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003578 return 0;
3579 }
Mike Stump11289f42009-09-09 15:08:12 +00003580
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003581 assert(PP && "Forgot to set Preprocessor ?");
Sebastian Redlc713b962010-07-21 00:46:22 +00003582 ID -= 1;
3583 if (!IdentifiersLoaded[ID]) {
3584 unsigned Index = ID;
3585 const char *Str = 0;
3586 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3587 PerFileData *F = Chain[N - I - 1];
3588 if (Index < F->LocalNumIdentifiers) {
3589 uint32_t Offset = F->IdentifierOffsets[Index];
3590 Str = F->IdentifierTableData + Offset;
3591 break;
3592 }
3593 Index -= F->LocalNumIdentifiers;
3594 }
3595 assert(Str && "Broken Chain");
Douglas Gregor5287b4e2009-04-25 21:04:17 +00003596
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003597 // All of the strings in the AST file are preceded by a 16-bit length.
3598 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenekca42a512009-10-23 04:45:31 +00003599 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
3600 // unsigned integers. This is important to avoid integer overflow when
3601 // we cast them to 'unsigned'.
Ted Kremenek49c52322009-10-23 03:57:22 +00003602 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregorab4df582009-04-28 20:01:51 +00003603 unsigned StrLen = (((unsigned) StrLenPtr[0])
3604 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redlc713b962010-07-21 00:46:22 +00003605 IdentifiersLoaded[ID]
Kovarththanan Rajaratnama3b09592010-03-12 10:32:27 +00003606 = &PP->getIdentifierTable().get(Str, StrLen);
Sebastian Redlff4a2952010-07-23 23:49:55 +00003607 if (DeserializationListener)
3608 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003609 }
Mike Stump11289f42009-09-09 15:08:12 +00003610
Sebastian Redlc713b962010-07-21 00:46:22 +00003611 return IdentifiersLoaded[ID];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003612}
3613
Sebastian Redl2c499f62010-08-18 23:56:43 +00003614void ASTReader::ReadSLocEntry(unsigned ID) {
Douglas Gregor258ae542009-04-27 06:38:32 +00003615 ReadSLocEntryRecord(ID);
3616}
3617
Sebastian Redl2c499f62010-08-18 23:56:43 +00003618Selector ASTReader::DecodeSelector(unsigned ID) {
Steve Naroff2ddea052009-04-23 10:39:46 +00003619 if (ID == 0)
3620 return Selector();
Mike Stump11289f42009-09-09 15:08:12 +00003621
Sebastian Redlada023c2010-08-04 20:40:17 +00003622 if (ID > SelectorsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003623 Error("selector ID out of range in AST file");
Steve Naroff2ddea052009-04-23 10:39:46 +00003624 return Selector();
3625 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00003626
Sebastian Redlada023c2010-08-04 20:40:17 +00003627 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00003628 // Load this selector from the selector table.
Sebastian Redlada023c2010-08-04 20:40:17 +00003629 unsigned Idx = ID - 1;
3630 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3631 PerFileData &F = *Chain[N - I - 1];
3632 if (Idx < F.LocalNumSelectors) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003633 ASTSelectorLookupTrait Trait(*this);
Sebastian Redlada023c2010-08-04 20:40:17 +00003634 SelectorsLoaded[ID - 1] =
3635 Trait.ReadKey(F.SelectorLookupTableData + F.SelectorOffsets[Idx], 0);
3636 if (DeserializationListener)
3637 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
3638 break;
3639 }
3640 Idx -= F.LocalNumSelectors;
3641 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00003642 }
3643
Sebastian Redlada023c2010-08-04 20:40:17 +00003644 return SelectorsLoaded[ID - 1];
Steve Naroff2ddea052009-04-23 10:39:46 +00003645}
3646
Sebastian Redl2c499f62010-08-18 23:56:43 +00003647Selector ASTReader::GetExternalSelector(uint32_t ID) {
Douglas Gregord720daf2010-04-06 17:30:22 +00003648 return DecodeSelector(ID);
3649}
3650
Sebastian Redl2c499f62010-08-18 23:56:43 +00003651uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redlada023c2010-08-04 20:40:17 +00003652 // ID 0 (the null selector) is considered an external selector.
3653 return getTotalNumSelectors() + 1;
Douglas Gregord720daf2010-04-06 17:30:22 +00003654}
3655
Mike Stump11289f42009-09-09 15:08:12 +00003656DeclarationName
Sebastian Redl2c499f62010-08-18 23:56:43 +00003657ASTReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003658 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
3659 switch (Kind) {
3660 case DeclarationName::Identifier:
3661 return DeclarationName(GetIdentifierInfo(Record, Idx));
3662
3663 case DeclarationName::ObjCZeroArgSelector:
3664 case DeclarationName::ObjCOneArgSelector:
3665 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff3c301dc2009-04-23 15:15:40 +00003666 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003667
3668 case DeclarationName::CXXConstructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00003669 return Context->DeclarationNames.getCXXConstructorName(
Douglas Gregor2211d342009-08-05 05:36:45 +00003670 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003671
3672 case DeclarationName::CXXDestructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00003673 return Context->DeclarationNames.getCXXDestructorName(
Douglas Gregor2211d342009-08-05 05:36:45 +00003674 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003675
3676 case DeclarationName::CXXConversionFunctionName:
Chris Lattner8575daa2009-04-27 21:45:14 +00003677 return Context->DeclarationNames.getCXXConversionFunctionName(
Douglas Gregor2211d342009-08-05 05:36:45 +00003678 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003679
3680 case DeclarationName::CXXOperatorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00003681 return Context->DeclarationNames.getCXXOperatorName(
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003682 (OverloadedOperatorKind)Record[Idx++]);
3683
Alexis Hunt3d221f22009-11-29 07:34:05 +00003684 case DeclarationName::CXXLiteralOperatorName:
3685 return Context->DeclarationNames.getCXXLiteralOperatorName(
3686 GetIdentifierInfo(Record, Idx));
3687
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003688 case DeclarationName::CXXUsingDirective:
3689 return DeclarationName::getUsingDirectiveName();
3690 }
3691
3692 // Required to silence GCC warning
3693 return DeclarationName();
3694}
Douglas Gregor55abb232009-04-10 20:39:37 +00003695
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003696TemplateName
Sebastian Redl2c499f62010-08-18 23:56:43 +00003697ASTReader::ReadTemplateName(const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003698 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
3699 switch (Kind) {
3700 case TemplateName::Template:
3701 return TemplateName(cast_or_null<TemplateDecl>(GetDecl(Record[Idx++])));
3702
3703 case TemplateName::OverloadedTemplate: {
3704 unsigned size = Record[Idx++];
3705 UnresolvedSet<8> Decls;
3706 while (size--)
3707 Decls.addDecl(cast<NamedDecl>(GetDecl(Record[Idx++])));
3708
3709 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
3710 }
3711
3712 case TemplateName::QualifiedTemplate: {
3713 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3714 bool hasTemplKeyword = Record[Idx++];
3715 TemplateDecl *Template = cast<TemplateDecl>(GetDecl(Record[Idx++]));
3716 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
3717 }
3718
3719 case TemplateName::DependentTemplate: {
3720 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3721 if (Record[Idx++]) // isIdentifier
3722 return Context->getDependentTemplateName(NNS,
3723 GetIdentifierInfo(Record, Idx));
3724 return Context->getDependentTemplateName(NNS,
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003725 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003726 }
3727 }
3728
3729 assert(0 && "Unhandled template name kind!");
3730 return TemplateName();
3731}
3732
3733TemplateArgument
Sebastian Redl2c499f62010-08-18 23:56:43 +00003734ASTReader::ReadTemplateArgument(llvm::BitstreamCursor &DeclsCursor,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003735 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003736 switch ((TemplateArgument::ArgKind)Record[Idx++]) {
3737 case TemplateArgument::Null:
3738 return TemplateArgument();
3739 case TemplateArgument::Type:
3740 return TemplateArgument(GetType(Record[Idx++]));
3741 case TemplateArgument::Declaration:
3742 return TemplateArgument(GetDecl(Record[Idx++]));
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +00003743 case TemplateArgument::Integral: {
3744 llvm::APSInt Value = ReadAPSInt(Record, Idx);
3745 QualType T = GetType(Record[Idx++]);
3746 return TemplateArgument(Value, T);
3747 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003748 case TemplateArgument::Template:
3749 return TemplateArgument(ReadTemplateName(Record, Idx));
3750 case TemplateArgument::Expression:
Sebastian Redlc67764e2010-07-22 22:43:28 +00003751 return TemplateArgument(ReadExpr(DeclsCursor));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003752 case TemplateArgument::Pack: {
3753 unsigned NumArgs = Record[Idx++];
3754 llvm::SmallVector<TemplateArgument, 8> Args;
3755 Args.reserve(NumArgs);
3756 while (NumArgs--)
Sebastian Redlc67764e2010-07-22 22:43:28 +00003757 Args.push_back(ReadTemplateArgument(DeclsCursor, Record, Idx));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003758 TemplateArgument TemplArg;
3759 TemplArg.setArgumentPack(Args.data(), Args.size(), /*CopyArgs=*/true);
3760 return TemplArg;
3761 }
3762 }
3763
3764 assert(0 && "Unhandled template argument kind!");
3765 return TemplateArgument();
3766}
3767
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003768TemplateParameterList *
Sebastian Redl2c499f62010-08-18 23:56:43 +00003769ASTReader::ReadTemplateParameterList(const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003770 SourceLocation TemplateLoc = ReadSourceLocation(Record, Idx);
3771 SourceLocation LAngleLoc = ReadSourceLocation(Record, Idx);
3772 SourceLocation RAngleLoc = ReadSourceLocation(Record, Idx);
3773
3774 unsigned NumParams = Record[Idx++];
3775 llvm::SmallVector<NamedDecl *, 16> Params;
3776 Params.reserve(NumParams);
3777 while (NumParams--)
3778 Params.push_back(cast<NamedDecl>(GetDecl(Record[Idx++])));
3779
3780 TemplateParameterList* TemplateParams =
3781 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
3782 Params.data(), Params.size(), RAngleLoc);
3783 return TemplateParams;
3784}
3785
3786void
Sebastian Redl2c499f62010-08-18 23:56:43 +00003787ASTReader::
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003788ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003789 llvm::BitstreamCursor &DeclsCursor,
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003790 const RecordData &Record, unsigned &Idx) {
3791 unsigned NumTemplateArgs = Record[Idx++];
3792 TemplArgs.reserve(NumTemplateArgs);
3793 while (NumTemplateArgs--)
Sebastian Redlc67764e2010-07-22 22:43:28 +00003794 TemplArgs.push_back(ReadTemplateArgument(DeclsCursor, Record, Idx));
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003795}
3796
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003797/// \brief Read a UnresolvedSet structure.
Sebastian Redl2c499f62010-08-18 23:56:43 +00003798void ASTReader::ReadUnresolvedSet(UnresolvedSetImpl &Set,
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003799 const RecordData &Record, unsigned &Idx) {
3800 unsigned NumDecls = Record[Idx++];
3801 while (NumDecls--) {
3802 NamedDecl *D = cast<NamedDecl>(GetDecl(Record[Idx++]));
3803 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
3804 Set.addDecl(D, AS);
3805 }
3806}
3807
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003808CXXBaseSpecifier
Sebastian Redl2c499f62010-08-18 23:56:43 +00003809ASTReader::ReadCXXBaseSpecifier(llvm::BitstreamCursor &DeclsCursor,
Nick Lewycky19b9f952010-07-26 16:56:01 +00003810 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003811 bool isVirtual = static_cast<bool>(Record[Idx++]);
3812 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
3813 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Nick Lewycky19b9f952010-07-26 16:56:01 +00003814 TypeSourceInfo *TInfo = GetTypeSourceInfo(DeclsCursor, Record, Idx);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003815 SourceRange Range = ReadSourceRange(Record, Idx);
Nick Lewycky19b9f952010-07-26 16:56:01 +00003816 return CXXBaseSpecifier(Range, isVirtual, isBaseOfClass, AS, TInfo);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003817}
3818
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003819std::pair<CXXBaseOrMemberInitializer **, unsigned>
Sebastian Redl2c499f62010-08-18 23:56:43 +00003820ASTReader::ReadCXXBaseOrMemberInitializers(llvm::BitstreamCursor &Cursor,
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003821 const RecordData &Record,
3822 unsigned &Idx) {
3823 CXXBaseOrMemberInitializer **BaseOrMemberInitializers = 0;
3824 unsigned NumInitializers = Record[Idx++];
3825 if (NumInitializers) {
3826 ASTContext &C = *getContext();
3827
3828 BaseOrMemberInitializers
3829 = new (C) CXXBaseOrMemberInitializer*[NumInitializers];
3830 for (unsigned i=0; i != NumInitializers; ++i) {
3831 TypeSourceInfo *BaseClassInfo = 0;
3832 bool IsBaseVirtual = false;
3833 FieldDecl *Member = 0;
3834
3835 bool IsBaseInitializer = Record[Idx++];
3836 if (IsBaseInitializer) {
3837 BaseClassInfo = GetTypeSourceInfo(Cursor, Record, Idx);
3838 IsBaseVirtual = Record[Idx++];
3839 } else {
3840 Member = cast<FieldDecl>(GetDecl(Record[Idx++]));
3841 }
3842 SourceLocation MemberLoc = ReadSourceLocation(Record, Idx);
3843 Expr *Init = ReadExpr(Cursor);
3844 FieldDecl *AnonUnionMember
3845 = cast_or_null<FieldDecl>(GetDecl(Record[Idx++]));
3846 SourceLocation LParenLoc = ReadSourceLocation(Record, Idx);
3847 SourceLocation RParenLoc = ReadSourceLocation(Record, Idx);
3848 bool IsWritten = Record[Idx++];
3849 unsigned SourceOrderOrNumArrayIndices;
3850 llvm::SmallVector<VarDecl *, 8> Indices;
3851 if (IsWritten) {
3852 SourceOrderOrNumArrayIndices = Record[Idx++];
3853 } else {
3854 SourceOrderOrNumArrayIndices = Record[Idx++];
3855 Indices.reserve(SourceOrderOrNumArrayIndices);
3856 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
3857 Indices.push_back(cast<VarDecl>(GetDecl(Record[Idx++])));
3858 }
3859
3860 CXXBaseOrMemberInitializer *BOMInit;
3861 if (IsBaseInitializer) {
3862 BOMInit = new (C) CXXBaseOrMemberInitializer(C, BaseClassInfo,
3863 IsBaseVirtual, LParenLoc,
3864 Init, RParenLoc);
3865 } else if (IsWritten) {
3866 BOMInit = new (C) CXXBaseOrMemberInitializer(C, Member, MemberLoc,
3867 LParenLoc, Init, RParenLoc);
3868 } else {
3869 BOMInit = CXXBaseOrMemberInitializer::Create(C, Member, MemberLoc,
3870 LParenLoc, Init, RParenLoc,
3871 Indices.data(),
3872 Indices.size());
3873 }
3874
3875 BOMInit->setAnonUnionMember(AnonUnionMember);
3876 BaseOrMemberInitializers[i] = BOMInit;
3877 }
3878 }
3879
3880 return std::make_pair(BaseOrMemberInitializers, NumInitializers);
3881}
3882
Chris Lattnerca025db2010-05-07 21:43:38 +00003883NestedNameSpecifier *
Sebastian Redl2c499f62010-08-18 23:56:43 +00003884ASTReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003885 unsigned N = Record[Idx++];
3886 NestedNameSpecifier *NNS = 0, *Prev = 0;
3887 for (unsigned I = 0; I != N; ++I) {
3888 NestedNameSpecifier::SpecifierKind Kind
3889 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
3890 switch (Kind) {
3891 case NestedNameSpecifier::Identifier: {
3892 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
3893 NNS = NestedNameSpecifier::Create(*Context, Prev, II);
3894 break;
3895 }
3896
3897 case NestedNameSpecifier::Namespace: {
3898 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
3899 NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
3900 break;
3901 }
3902
3903 case NestedNameSpecifier::TypeSpec:
3904 case NestedNameSpecifier::TypeSpecWithTemplate: {
3905 Type *T = GetType(Record[Idx++]).getTypePtr();
3906 bool Template = Record[Idx++];
3907 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
3908 break;
3909 }
3910
3911 case NestedNameSpecifier::Global: {
3912 NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
3913 // No associated value, and there can't be a prefix.
3914 break;
3915 }
Chris Lattnerca025db2010-05-07 21:43:38 +00003916 }
Argyrios Kyrtzidisad65c692010-07-07 15:46:30 +00003917 Prev = NNS;
Chris Lattnerca025db2010-05-07 21:43:38 +00003918 }
3919 return NNS;
3920}
3921
3922SourceRange
Sebastian Redl2c499f62010-08-18 23:56:43 +00003923ASTReader::ReadSourceRange(const RecordData &Record, unsigned &Idx) {
Daniel Dunbar6d3bc082010-06-02 15:47:10 +00003924 SourceLocation beg = SourceLocation::getFromRawEncoding(Record[Idx++]);
3925 SourceLocation end = SourceLocation::getFromRawEncoding(Record[Idx++]);
3926 return SourceRange(beg, end);
Chris Lattnerca025db2010-05-07 21:43:38 +00003927}
3928
Douglas Gregor1daeb692009-04-13 18:14:40 +00003929/// \brief Read an integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00003930llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00003931 unsigned BitWidth = Record[Idx++];
3932 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
3933 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
3934 Idx += NumWords;
3935 return Result;
3936}
3937
3938/// \brief Read a signed integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00003939llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00003940 bool isUnsigned = Record[Idx++];
3941 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
3942}
3943
Douglas Gregore0a3a512009-04-14 21:55:33 +00003944/// \brief Read a floating-point value
Sebastian Redl2c499f62010-08-18 23:56:43 +00003945llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00003946 return llvm::APFloat(ReadAPInt(Record, Idx));
3947}
3948
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003949// \brief Read a string
Sebastian Redl2c499f62010-08-18 23:56:43 +00003950std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003951 unsigned Len = Record[Idx++];
Jay Foad7d0479f2009-05-21 09:52:38 +00003952 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003953 Idx += Len;
3954 return Result;
3955}
3956
Sebastian Redl2c499f62010-08-18 23:56:43 +00003957CXXTemporary *ASTReader::ReadCXXTemporary(const RecordData &Record,
Chris Lattnercba86142010-05-10 00:25:06 +00003958 unsigned &Idx) {
3959 CXXDestructorDecl *Decl = cast<CXXDestructorDecl>(GetDecl(Record[Idx++]));
3960 return CXXTemporary::Create(*Context, Decl);
3961}
3962
Sebastian Redl2c499f62010-08-18 23:56:43 +00003963DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregor92863e42009-04-10 23:10:45 +00003964 return Diag(SourceLocation(), DiagID);
3965}
3966
Sebastian Redl2c499f62010-08-18 23:56:43 +00003967DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003968 return Diags.Report(FullSourceLoc(Loc, SourceMgr), DiagID);
Douglas Gregor55abb232009-04-10 20:39:37 +00003969}
Douglas Gregora9af1d12009-04-17 00:04:06 +00003970
Douglas Gregora868bbd2009-04-21 22:25:48 +00003971/// \brief Retrieve the identifier table associated with the
3972/// preprocessor.
Sebastian Redl2c499f62010-08-18 23:56:43 +00003973IdentifierTable &ASTReader::getIdentifierTable() {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003974 assert(PP && "Forgot to set Preprocessor ?");
3975 return PP->getIdentifierTable();
Douglas Gregora868bbd2009-04-21 22:25:48 +00003976}
3977
Douglas Gregora9af1d12009-04-17 00:04:06 +00003978/// \brief Record that the given ID maps to the given switch-case
3979/// statement.
Sebastian Redl2c499f62010-08-18 23:56:43 +00003980void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Douglas Gregora9af1d12009-04-17 00:04:06 +00003981 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
3982 SwitchCaseStmts[ID] = SC;
3983}
3984
3985/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00003986SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Douglas Gregora9af1d12009-04-17 00:04:06 +00003987 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
3988 return SwitchCaseStmts[ID];
3989}
Douglas Gregor6cc68a42009-04-17 18:18:49 +00003990
3991/// \brief Record that the given label statement has been
3992/// deserialized and has the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00003993void ASTReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
Mike Stump11289f42009-09-09 15:08:12 +00003994 assert(LabelStmts.find(ID) == LabelStmts.end() &&
Douglas Gregor6cc68a42009-04-17 18:18:49 +00003995 "Deserialized label twice");
3996 LabelStmts[ID] = S;
3997
3998 // If we've already seen any goto statements that point to this
3999 // label, resolve them now.
4000 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
4001 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
4002 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
4003 Goto->second->setLabel(S);
4004 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor779d8652009-04-17 18:58:21 +00004005
4006 // If we've already seen any address-label statements that point to
4007 // this label, resolve them now.
4008 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
Mike Stump11289f42009-09-09 15:08:12 +00004009 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
Douglas Gregor779d8652009-04-17 18:58:21 +00004010 = UnresolvedAddrLabelExprs.equal_range(ID);
Mike Stump11289f42009-09-09 15:08:12 +00004011 for (AddrLabelIter AddrLabel = AddrLabels.first;
Douglas Gregor779d8652009-04-17 18:58:21 +00004012 AddrLabel != AddrLabels.second; ++AddrLabel)
4013 AddrLabel->second->setLabel(S);
4014 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004015}
4016
4017/// \brief Set the label of the given statement to the label
4018/// identified by ID.
4019///
4020/// Depending on the order in which the label and other statements
4021/// referencing that label occur, this operation may complete
4022/// immediately (updating the statement) or it may queue the
4023/// statement to be back-patched later.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004024void ASTReader::SetLabelOf(GotoStmt *S, unsigned ID) {
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004025 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
4026 if (Label != LabelStmts.end()) {
4027 // We've already seen this label, so set the label of the goto and
4028 // we're done.
4029 S->setLabel(Label->second);
4030 } else {
4031 // We haven't seen this label yet, so add this goto to the set of
4032 // unresolved goto statements.
4033 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
4034 }
4035}
Douglas Gregor779d8652009-04-17 18:58:21 +00004036
4037/// \brief Set the label of the given expression to the label
4038/// identified by ID.
4039///
4040/// Depending on the order in which the label and other statements
4041/// referencing that label occur, this operation may complete
4042/// immediately (updating the statement) or it may queue the
4043/// statement to be back-patched later.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004044void ASTReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
Douglas Gregor779d8652009-04-17 18:58:21 +00004045 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
4046 if (Label != LabelStmts.end()) {
4047 // We've already seen this label, so set the label of the
4048 // label-address expression and we're done.
4049 S->setLabel(Label->second);
4050 } else {
4051 // We haven't seen this label yet, so add this label-address
4052 // expression to the set of unresolved label-address expressions.
4053 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
4054 }
4055}
Douglas Gregor1342e842009-07-06 18:54:52 +00004056
Sebastian Redl2c499f62010-08-18 23:56:43 +00004057void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004058 assert(NumCurrentElementsDeserializing &&
4059 "FinishedDeserializing not paired with StartedDeserializing");
4060 if (NumCurrentElementsDeserializing == 1) {
Douglas Gregor1342e842009-07-06 18:54:52 +00004061 // If any identifiers with corresponding top-level declarations have
4062 // been loaded, load those declarations now.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004063 while (!PendingIdentifierInfos.empty()) {
4064 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
4065 PendingIdentifierInfos.front().DeclIDs, true);
4066 PendingIdentifierInfos.pop_front();
Douglas Gregor1342e842009-07-06 18:54:52 +00004067 }
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004068
4069 // We are not in recursive loading, so it's safe to pass the "interesting"
4070 // decls to the consumer.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004071 if (Consumer)
4072 PassInterestingDeclsToConsumer();
Douglas Gregor1342e842009-07-06 18:54:52 +00004073 }
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004074 --NumCurrentElementsDeserializing;
Douglas Gregor1342e842009-07-06 18:54:52 +00004075}
Douglas Gregorb473b072010-08-19 00:28:17 +00004076
Sebastian Redld7dce0a2010-08-24 00:50:04 +00004077ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
4078 const char *isysroot, bool DisableValidation)
4079 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
4080 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
4081 Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
4082 Consumer(0), isysroot(isysroot), DisableValidation(DisableValidation),
4083 NumStatHits(0), NumStatMisses(0), NumSLocEntriesRead(0),
4084 TotalNumSLocEntries(0), NumStatementsRead(0), TotalNumStatements(0),
4085 NumMacrosRead(0), TotalNumMacros(0), NumSelectorsRead(0),
4086 NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
4087 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
4088 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
4089 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
4090 RelocatablePCH = false;
4091}
4092
4093ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
4094 Diagnostic &Diags, const char *isysroot,
4095 bool DisableValidation)
4096 : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
4097 Diags(Diags), SemaObj(0), PP(0), Context(0), Consumer(0),
4098 isysroot(isysroot), DisableValidation(DisableValidation), NumStatHits(0),
4099 NumStatMisses(0), NumSLocEntriesRead(0), TotalNumSLocEntries(0),
4100 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
4101 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
4102 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
4103 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
4104 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
4105 NumCurrentElementsDeserializing(0) {
4106 RelocatablePCH = false;
4107}
4108
4109ASTReader::~ASTReader() {
4110 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
4111 delete Chain[e - i - 1];
4112 // Delete all visible decl lookup tables
4113 for (DeclContextOffsetsMap::iterator I = DeclContextOffsets.begin(),
4114 E = DeclContextOffsets.end();
4115 I != E; ++I) {
4116 for (DeclContextInfos::iterator J = I->second.begin(), F = I->second.end();
4117 J != F; ++J) {
4118 if (J->NameLookupTableData)
4119 delete static_cast<ASTDeclContextNameLookupTable*>(
4120 J->NameLookupTableData);
4121 }
4122 }
4123 for (DeclContextVisibleUpdatesPending::iterator
4124 I = PendingVisibleUpdates.begin(),
4125 E = PendingVisibleUpdates.end();
4126 I != E; ++I) {
4127 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
4128 F = I->second.end();
4129 J != F; ++J)
4130 delete static_cast<ASTDeclContextNameLookupTable*>(*J);
4131 }
4132}
4133
Douglas Gregorb473b072010-08-19 00:28:17 +00004134ASTReader::PerFileData::PerFileData()
4135 : StatCache(0), LocalNumSLocEntries(0), LocalNumTypes(0), TypeOffsets(0),
4136 LocalNumDecls(0), DeclOffsets(0), LocalNumIdentifiers(0),
4137 IdentifierOffsets(0), IdentifierTableData(0), IdentifierLookupTable(0),
4138 LocalNumMacroDefinitions(0), MacroDefinitionOffsets(0),
4139 NumPreallocatedPreprocessingEntities(0), SelectorLookupTable(0),
4140 SelectorLookupTableData(0), SelectorOffsets(0), LocalNumSelectors(0)
4141{}
4142
4143ASTReader::PerFileData::~PerFileData() {
4144 delete static_cast<ASTIdentifierLookupTable *>(IdentifierLookupTable);
4145 delete static_cast<ASTSelectorLookupTable *>(SelectorLookupTable);
4146}
4147