blob: b24b35309a65f770b404e17cc05efb4cbbfd33a8 [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"
Douglas Gregor1a0d0b92009-04-14 00:24:19 +000020#include "clang/AST/ASTConsumer.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000021#include "clang/AST/ASTContext.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000022#include "clang/AST/Expr.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000023#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000024#include "clang/AST/TypeLocVisitor.h"
Chris Lattner34321bc2009-04-10 21:41:48 +000025#include "clang/Lex/MacroInfo.h"
Douglas Gregoraae92242010-03-19 21:51:54 +000026#include "clang/Lex/PreprocessingRecord.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000027#include "clang/Lex/Preprocessor.h"
Steve Naroff3fa455a2009-04-24 20:03:17 +000028#include "clang/Lex/HeaderSearch.h"
Douglas Gregora868bbd2009-04-21 22:25:48 +000029#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000030#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000031#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000032#include "clang/Basic/FileManager.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000033#include "clang/Basic/TargetInfo.h"
Douglas Gregord54f3a12009-10-05 21:07:28 +000034#include "clang/Basic/Version.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000035#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000036#include "llvm/Bitcode/BitstreamReader.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000037#include "llvm/Support/MemoryBuffer.h"
John McCall0ad16662009-10-29 08:12:44 +000038#include "llvm/Support/ErrorHandling.h"
Daniel Dunbarf2ce9a22009-11-18 19:50:41 +000039#include "llvm/System/Path.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000040#include <algorithm>
Douglas Gregorc379c072009-04-28 18:58:38 +000041#include <iterator>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000042#include <cstdio>
Douglas Gregorc5046832009-04-27 18:38:38 +000043#include <sys/stat.h>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000044using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000045using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000046
47//===----------------------------------------------------------------------===//
Sebastian Redld44cd6a2010-08-18 23:57:06 +000048// PCH validator implementation
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000049//===----------------------------------------------------------------------===//
50
Sebastian Redl3e31c722010-08-18 23:56:56 +000051ASTReaderListener::~ASTReaderListener() {}
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000052
53bool
54PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts) {
55 const LangOptions &PPLangOpts = PP.getLangOptions();
56#define PARSE_LANGOPT_BENIGN(Option)
57#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
58 if (PPLangOpts.Option != LangOpts.Option) { \
59 Reader.Diag(DiagID) << LangOpts.Option << PPLangOpts.Option; \
60 return true; \
61 }
62
63 PARSE_LANGOPT_BENIGN(Trigraphs);
64 PARSE_LANGOPT_BENIGN(BCPLComment);
65 PARSE_LANGOPT_BENIGN(DollarIdents);
66 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
67 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
Chandler Carruthe03aa552010-04-17 20:17:31 +000068 PARSE_LANGOPT_IMPORTANT(GNUKeywords, diag::warn_pch_gnu_keywords);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000069 PARSE_LANGOPT_BENIGN(ImplicitInt);
70 PARSE_LANGOPT_BENIGN(Digraphs);
71 PARSE_LANGOPT_BENIGN(HexFloats);
72 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
73 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
74 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
75 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
76 PARSE_LANGOPT_BENIGN(CXXOperatorName);
77 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
78 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
79 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
Fariborz Jahanian45878032010-02-09 19:31:38 +000080 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI2, diag::warn_pch_nonfragile_abi2);
Fariborz Jahanian62c56022010-04-22 21:01:59 +000081 PARSE_LANGOPT_IMPORTANT(NoConstantCFStrings,
82 diag::warn_pch_no_constant_cfstrings);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000083 PARSE_LANGOPT_BENIGN(PascalStrings);
84 PARSE_LANGOPT_BENIGN(WritableStrings);
Mike Stump11289f42009-09-09 15:08:12 +000085 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000086 diag::warn_pch_lax_vector_conversions);
Nate Begeman9d905792009-06-25 22:57:40 +000087 PARSE_LANGOPT_IMPORTANT(AltiVec, diag::warn_pch_altivec);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000088 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
Daniel Dunbar925152c2010-02-10 18:48:44 +000089 PARSE_LANGOPT_IMPORTANT(SjLjExceptions, diag::warn_pch_sjlj_exceptions);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000090 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
91 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
92 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
Mike Stump11289f42009-09-09 15:08:12 +000093 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000094 diag::warn_pch_thread_safe_statics);
Daniel Dunbara77eaeb2009-09-03 04:54:28 +000095 PARSE_LANGOPT_IMPORTANT(POSIXThreads, diag::warn_pch_posix_threads);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000096 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
97 PARSE_LANGOPT_BENIGN(EmitAllDecls);
98 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
Chris Lattner51924e512010-06-26 21:25:03 +000099 PARSE_LANGOPT_BENIGN(getSignedOverflowBehavior());
Mike Stump11289f42009-09-09 15:08:12 +0000100 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000101 diag::warn_pch_heinous_extensions);
102 // FIXME: Most of the options below are benign if the macro wasn't
103 // used. Unfortunately, this means that a PCH compiled without
104 // optimization can't be used with optimization turned on, even
105 // though the only thing that changes is whether __OPTIMIZE__ was
106 // defined... but if __OPTIMIZE__ never showed up in the header, it
107 // doesn't matter. We could consider making this some special kind
108 // of check.
109 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
110 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
111 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
112 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
113 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
114 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
115 PARSE_LANGOPT_IMPORTANT(AccessControl, diag::warn_pch_access_control);
116 PARSE_LANGOPT_IMPORTANT(CharIsSigned, diag::warn_pch_char_signed);
John Thompsoned4e2952009-11-05 20:14:16 +0000117 PARSE_LANGOPT_IMPORTANT(ShortWChar, diag::warn_pch_short_wchar);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000118 if ((PPLangOpts.getGCMode() != 0) != (LangOpts.getGCMode() != 0)) {
Mike Stump11289f42009-09-09 15:08:12 +0000119 Reader.Diag(diag::warn_pch_gc_mode)
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000120 << LangOpts.getGCMode() << PPLangOpts.getGCMode();
121 return true;
122 }
123 PARSE_LANGOPT_BENIGN(getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +0000124 PARSE_LANGOPT_IMPORTANT(getStackProtectorMode(),
125 diag::warn_pch_stack_protector);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000126 PARSE_LANGOPT_BENIGN(InstantiationDepth);
Nate Begeman9d905792009-06-25 22:57:40 +0000127 PARSE_LANGOPT_IMPORTANT(OpenCL, diag::warn_pch_opencl);
Mike Stumpd9546382009-12-12 01:27:46 +0000128 PARSE_LANGOPT_BENIGN(CatchUndefined);
Daniel Dunbar143021e2009-09-21 04:16:19 +0000129 PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors);
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +0000130 PARSE_LANGOPT_BENIGN(SpellChecking);
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +0000131#undef PARSE_LANGOPT_IMPORTANT
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000132#undef PARSE_LANGOPT_BENIGN
133
134 return false;
135}
136
Daniel Dunbar20a682d2009-11-11 00:52:11 +0000137bool PCHValidator::ReadTargetTriple(llvm::StringRef Triple) {
138 if (Triple == PP.getTargetInfo().getTriple().str())
139 return false;
140
141 Reader.Diag(diag::warn_pch_target_triple)
142 << Triple << PP.getTargetInfo().getTriple().str();
143 return true;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000144}
145
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000146struct EmptyStringRef {
Benjamin Kramer8d5609b2010-07-14 23:19:41 +0000147 bool operator ()(llvm::StringRef r) const { return r.empty(); }
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000148};
149struct EmptyBlock {
150 bool operator ()(const PCHPredefinesBlock &r) const { return r.Data.empty(); }
151};
152
153static bool EqualConcatenations(llvm::SmallVector<llvm::StringRef, 2> L,
154 PCHPredefinesBlocks R) {
155 // First, sum up the lengths.
156 unsigned LL = 0, RL = 0;
157 for (unsigned I = 0, N = L.size(); I != N; ++I) {
158 LL += L[I].size();
159 }
160 for (unsigned I = 0, N = R.size(); I != N; ++I) {
161 RL += R[I].Data.size();
162 }
163 if (LL != RL)
164 return false;
165 if (LL == 0 && RL == 0)
166 return true;
167
168 // Kick out empty parts, they confuse the algorithm below.
169 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
170 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
171
172 // Do it the hard way. At this point, both vectors must be non-empty.
173 llvm::StringRef LR = L[0], RR = R[0].Data;
174 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
Daniel Dunbar01ad0a72010-07-16 00:00:11 +0000175 (void) RN;
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000176 for (;;) {
177 // Compare the current pieces.
178 if (LR.size() == RR.size()) {
179 // If they're the same length, it's pretty easy.
180 if (LR != RR)
181 return false;
182 // Both pieces are done, advance.
183 ++LI;
184 ++RI;
185 // If either string is done, they're both done, since they're the same
186 // length.
187 if (LI == LN) {
188 assert(RI == RN && "Strings not the same length after all?");
189 return true;
190 }
191 LR = L[LI];
192 RR = R[RI].Data;
193 } else if (LR.size() < RR.size()) {
194 // Right piece is longer.
195 if (!RR.startswith(LR))
196 return false;
197 ++LI;
198 assert(LI != LN && "Strings not the same length after all?");
199 RR = RR.substr(LR.size());
200 LR = L[LI];
201 } else {
202 // Left piece is longer.
203 if (!LR.startswith(RR))
204 return false;
205 ++RI;
206 assert(RI != RN && "Strings not the same length after all?");
207 LR = LR.substr(RR.size());
208 RR = R[RI].Data;
209 }
210 }
211}
212
213static std::pair<FileID, llvm::StringRef::size_type>
214FindMacro(const PCHPredefinesBlocks &Buffers, llvm::StringRef MacroDef) {
215 std::pair<FileID, llvm::StringRef::size_type> Res;
216 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
217 Res.second = Buffers[I].Data.find(MacroDef);
218 if (Res.second != llvm::StringRef::npos) {
219 Res.first = Buffers[I].BufferID;
220 break;
221 }
222 }
223 return Res;
224}
225
226bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000227 llvm::StringRef OriginalFileName,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000228 std::string &SuggestedPredefines) {
Daniel Dunbar732ef8a2009-11-11 23:58:53 +0000229 // We are in the context of an implicit include, so the predefines buffer will
230 // have a #include entry for the PCH file itself (as normalized by the
231 // preprocessor initialization). Find it and skip over it in the checking
232 // below.
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000233 llvm::SmallString<256> PCHInclude;
234 PCHInclude += "#include \"";
Daniel Dunbar732ef8a2009-11-11 23:58:53 +0000235 PCHInclude += NormalizeDashIncludePath(OriginalFileName);
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000236 PCHInclude += "\"\n";
237 std::pair<llvm::StringRef,llvm::StringRef> Split =
238 llvm::StringRef(PP.getPredefines()).split(PCHInclude.str());
239 llvm::StringRef Left = Split.first, Right = Split.second;
Ted Kremenek1ff615c2010-03-18 00:56:54 +0000240 if (Left == PP.getPredefines()) {
241 Error("Missing PCH include entry!");
242 return true;
243 }
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000244
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000245 // If the concatenation of all the PCH buffers is equal to the adjusted
246 // command line, we're done.
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000247 llvm::SmallVector<llvm::StringRef, 2> CommandLine;
248 CommandLine.push_back(Left);
249 CommandLine.push_back(Right);
250 if (EqualConcatenations(CommandLine, Buffers))
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000251 return false;
252
253 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump11289f42009-09-09 15:08:12 +0000254
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000255 // The predefines buffers are different. Determine what the differences are,
256 // and whether they require us to reject the PCH file.
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000257 llvm::SmallVector<llvm::StringRef, 8> PCHLines;
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000258 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
259 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000260
261 llvm::SmallVector<llvm::StringRef, 8> CmdLineLines;
262 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
263 Right.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000264
Daniel Dunbar499baed2009-11-11 05:26:28 +0000265 // Sort both sets of predefined buffer lines, since we allow some extra
266 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000267 std::sort(CmdLineLines.begin(), CmdLineLines.end());
268 std::sort(PCHLines.begin(), PCHLines.end());
269
Daniel Dunbar499baed2009-11-11 05:26:28 +0000270 // Determine which predefines that were used to build the PCH file are missing
271 // from the command line.
272 std::vector<llvm::StringRef> MissingPredefines;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000273 std::set_difference(PCHLines.begin(), PCHLines.end(),
274 CmdLineLines.begin(), CmdLineLines.end(),
275 std::back_inserter(MissingPredefines));
276
277 bool MissingDefines = false;
278 bool ConflictingDefines = false;
279 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Daniel Dunbar499baed2009-11-11 05:26:28 +0000280 llvm::StringRef Missing = MissingPredefines[I];
281 if (!Missing.startswith("#define ")) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000282 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
283 return true;
284 }
Mike Stump11289f42009-09-09 15:08:12 +0000285
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000286 // This is a macro definition. Determine the name of the macro we're
287 // defining.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000288 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump11289f42009-09-09 15:08:12 +0000289 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000290 = Missing.find_first_of("( \n\r", StartOfMacroName);
291 assert(EndOfMacroName != std::string::npos &&
292 "Couldn't find the end of the macro name");
Daniel Dunbar499baed2009-11-11 05:26:28 +0000293 llvm::StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000294
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000295 // Determine whether this macro was given a different definition on the
296 // command line.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000297 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000298 std::string::size_type MacroDefLen = MacroDefStart.size();
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000299 llvm::SmallVector<llvm::StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000300 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
301 MacroDefStart);
302 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000303 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000304 // Different macro; we're done.
305 ConflictPos = CmdLineLines.end();
Mike Stump11289f42009-09-09 15:08:12 +0000306 break;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000307 }
Mike Stump11289f42009-09-09 15:08:12 +0000308
309 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000310 "Invalid #define in predefines buffer?");
Mike Stump11289f42009-09-09 15:08:12 +0000311 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000312 (*ConflictPos)[MacroDefLen] != '(')
313 continue; // Longer macro name; keep trying.
Mike Stump11289f42009-09-09 15:08:12 +0000314
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000315 // We found a conflicting macro definition.
316 break;
317 }
Mike Stump11289f42009-09-09 15:08:12 +0000318
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000319 if (ConflictPos != CmdLineLines.end()) {
320 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
321 << MacroName;
322
323 // Show the definition of this macro within the PCH file.
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000324 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
325 FindMacro(Buffers, Missing);
326 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
327 SourceLocation PCHMissingLoc =
328 SourceMgr.getLocForStartOfFile(MacroLoc.first)
329 .getFileLocWithOffset(MacroLoc.second);
Daniel Dunbar499baed2009-11-11 05:26:28 +0000330 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000331
332 ConflictingDefines = true;
333 continue;
334 }
Mike Stump11289f42009-09-09 15:08:12 +0000335
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000336 // If the macro doesn't conflict, then we'll just pick up the macro
337 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000338 if (ConflictingDefines)
339 continue; // Don't complain if there are already conflicting defs
Mike Stump11289f42009-09-09 15:08:12 +0000340
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000341 if (!MissingDefines) {
342 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
343 MissingDefines = true;
344 }
345
346 // Show the definition of this macro within the PCH file.
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000347 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
348 FindMacro(Buffers, Missing);
349 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
350 SourceLocation PCHMissingLoc =
351 SourceMgr.getLocForStartOfFile(MacroLoc.first)
352 .getFileLocWithOffset(MacroLoc.second);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000353 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
354 }
Mike Stump11289f42009-09-09 15:08:12 +0000355
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000356 if (ConflictingDefines)
357 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000358
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000359 // Determine what predefines were introduced based on command-line
360 // parameters that were not present when building the PCH
361 // file. Extra #defines are okay, so long as the identifiers being
362 // defined were not used within the precompiled header.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000363 std::vector<llvm::StringRef> ExtraPredefines;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000364 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
365 PCHLines.begin(), PCHLines.end(),
Mike Stump11289f42009-09-09 15:08:12 +0000366 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000367 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Daniel Dunbar499baed2009-11-11 05:26:28 +0000368 llvm::StringRef &Extra = ExtraPredefines[I];
369 if (!Extra.startswith("#define ")) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000370 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
371 return true;
372 }
373
374 // This is an extra macro definition. Determine the name of the
375 // macro we're defining.
376 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump11289f42009-09-09 15:08:12 +0000377 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000378 = Extra.find_first_of("( \n\r", StartOfMacroName);
379 assert(EndOfMacroName != std::string::npos &&
380 "Couldn't find the end of the macro name");
Daniel Dunbar499baed2009-11-11 05:26:28 +0000381 llvm::StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000382
383 // Check whether this name was used somewhere in the PCH file. If
384 // so, defining it as a macro could change behavior, so we reject
385 // the PCH file.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000386 if (IdentifierInfo *II = Reader.get(MacroName)) {
Daniel Dunbar045c92f2009-11-11 00:52:00 +0000387 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000388 return true;
389 }
390
391 // Add this definition to the suggested predefines buffer.
392 SuggestedPredefines += Extra;
393 SuggestedPredefines += '\n';
394 }
395
396 // If we get here, it's because the predefines buffer had compatible
397 // contents. Accept the PCH file.
398 return false;
399}
400
Douglas Gregor5712ebc2010-03-16 16:35:32 +0000401void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
402 unsigned ID) {
403 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
404 ++NumHeaderInfos;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000405}
406
407void PCHValidator::ReadCounter(unsigned Value) {
408 PP.setCounterValue(Value);
409}
410
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000411//===----------------------------------------------------------------------===//
Sebastian Redl2c499f62010-08-18 23:56:43 +0000412// AST reader implementation
Douglas Gregora868bbd2009-04-21 22:25:48 +0000413//===----------------------------------------------------------------------===//
414
Sebastian Redl2c499f62010-08-18 23:56:43 +0000415ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
Douglas Gregorce3a8292010-07-27 00:27:13 +0000416 const char *isysroot, bool DisableValidation)
Sebastian Redl85b2a6a2010-07-14 23:45:08 +0000417 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
418 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
419 Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
Sebastian Redlada023c2010-08-04 20:40:17 +0000420 Consumer(0), isysroot(isysroot), DisableValidation(DisableValidation),
421 NumStatHits(0), NumStatMisses(0), NumSLocEntriesRead(0),
422 TotalNumSLocEntries(0), NumStatementsRead(0), TotalNumStatements(0),
Sebastian Redl6e1a2a02010-08-04 21:22:45 +0000423 NumMacrosRead(0), TotalNumMacros(0), NumSelectorsRead(0),
424 NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
425 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
Sebastian Redlada023c2010-08-04 20:40:17 +0000426 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
427 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000428 RelocatablePCH = false;
429}
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000430
Sebastian Redl2c499f62010-08-18 23:56:43 +0000431ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
Douglas Gregorce3a8292010-07-27 00:27:13 +0000432 Diagnostic &Diags, const char *isysroot,
433 bool DisableValidation)
Sebastian Redl85b2a6a2010-07-14 23:45:08 +0000434 : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
Sebastian Redl34522812010-07-16 17:50:48 +0000435 Diags(Diags), SemaObj(0), PP(0), Context(0), Consumer(0),
Sebastian Redlada023c2010-08-04 20:40:17 +0000436 isysroot(isysroot), DisableValidation(DisableValidation), NumStatHits(0),
437 NumStatMisses(0), NumSLocEntriesRead(0), TotalNumSLocEntries(0),
438 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
Sebastian Redl6e1a2a02010-08-04 21:22:45 +0000439 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
440 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
Sebastian Redlada023c2010-08-04 20:40:17 +0000441 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
442 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
443 NumCurrentElementsDeserializing(0) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000444 RelocatablePCH = false;
445}
Chris Lattner92ba5ff2009-04-27 05:14:47 +0000446
Sebastian Redl2c499f62010-08-18 23:56:43 +0000447ASTReader::~ASTReader() {
Sebastian Redl34522812010-07-16 17:50:48 +0000448 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
449 delete Chain[e - i - 1];
450}
451
Sebastian Redl07a89a82010-07-30 00:29:29 +0000452void
Sebastian Redl3e31c722010-08-18 23:56:56 +0000453ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redl07a89a82010-07-30 00:29:29 +0000454 DeserializationListener = Listener;
455 if (DeserializationListener)
456 DeserializationListener->SetReader(this);
457}
458
Chris Lattner92ba5ff2009-04-27 05:14:47 +0000459
Douglas Gregora868bbd2009-04-21 22:25:48 +0000460namespace {
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000461class ASTSelectorLookupTrait {
Sebastian Redl2c499f62010-08-18 23:56:43 +0000462 ASTReader &Reader;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000463
464public:
Sebastian Redl834bb972010-08-04 17:20:04 +0000465 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +0000466 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +0000467 ObjCMethodList Instance, Factory;
468 };
Douglas Gregorc78d3462009-04-24 21:10:55 +0000469
470 typedef Selector external_key_type;
471 typedef external_key_type internal_key_type;
472
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000473 explicit ASTSelectorLookupTrait(ASTReader &Reader) : Reader(Reader) { }
Mike Stump11289f42009-09-09 15:08:12 +0000474
Douglas Gregorc78d3462009-04-24 21:10:55 +0000475 static bool EqualKey(const internal_key_type& a,
476 const internal_key_type& b) {
477 return a == b;
478 }
Mike Stump11289f42009-09-09 15:08:12 +0000479
Douglas Gregorc78d3462009-04-24 21:10:55 +0000480 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +0000481 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000482 }
Mike Stump11289f42009-09-09 15:08:12 +0000483
Douglas Gregorc78d3462009-04-24 21:10:55 +0000484 // This hopefully will just get inlined and removed by the optimizer.
485 static const internal_key_type&
486 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump11289f42009-09-09 15:08:12 +0000487
Douglas Gregorc78d3462009-04-24 21:10:55 +0000488 static std::pair<unsigned, unsigned>
489 ReadKeyDataLength(const unsigned char*& d) {
490 using namespace clang::io;
491 unsigned KeyLen = ReadUnalignedLE16(d);
492 unsigned DataLen = ReadUnalignedLE16(d);
493 return std::make_pair(KeyLen, DataLen);
494 }
Mike Stump11289f42009-09-09 15:08:12 +0000495
Douglas Gregor95c13f52009-04-25 17:48:32 +0000496 internal_key_type ReadKey(const unsigned char* d, unsigned) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000497 using namespace clang::io;
Chris Lattner8575daa2009-04-27 21:45:14 +0000498 SelectorTable &SelTable = Reader.getContext()->Selectors;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000499 unsigned N = ReadUnalignedLE16(d);
Mike Stump11289f42009-09-09 15:08:12 +0000500 IdentifierInfo *FirstII
Douglas Gregorc78d3462009-04-24 21:10:55 +0000501 = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
502 if (N == 0)
503 return SelTable.getNullarySelector(FirstII);
504 else if (N == 1)
505 return SelTable.getUnarySelector(FirstII);
506
507 llvm::SmallVector<IdentifierInfo *, 16> Args;
508 Args.push_back(FirstII);
509 for (unsigned I = 1; I != N; ++I)
510 Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)));
511
Douglas Gregor038c3382009-05-22 22:45:36 +0000512 return SelTable.getSelector(N, Args.data());
Douglas Gregorc78d3462009-04-24 21:10:55 +0000513 }
Mike Stump11289f42009-09-09 15:08:12 +0000514
Douglas Gregorc78d3462009-04-24 21:10:55 +0000515 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
516 using namespace clang::io;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000517
518 data_type Result;
519
Sebastian Redl834bb972010-08-04 17:20:04 +0000520 Result.ID = ReadUnalignedLE32(d);
521 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
522 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
523
Douglas Gregorc78d3462009-04-24 21:10:55 +0000524 // Load instance methods
525 ObjCMethodList *Prev = 0;
526 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000527 ObjCMethodDecl *Method
Douglas Gregorc78d3462009-04-24 21:10:55 +0000528 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl834bb972010-08-04 17:20:04 +0000529 if (!Result.Instance.Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000530 // This is the first method, which is the easy case.
Sebastian Redl834bb972010-08-04 17:20:04 +0000531 Result.Instance.Method = Method;
532 Prev = &Result.Instance;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000533 continue;
534 }
535
Ted Kremenekda4abf12010-02-11 00:53:01 +0000536 ObjCMethodList *Mem =
537 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
538 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000539 Prev = Prev->Next;
540 }
541
542 // Load factory methods
543 Prev = 0;
544 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000545 ObjCMethodDecl *Method
Douglas Gregorc78d3462009-04-24 21:10:55 +0000546 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl834bb972010-08-04 17:20:04 +0000547 if (!Result.Factory.Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000548 // This is the first method, which is the easy case.
Sebastian Redl834bb972010-08-04 17:20:04 +0000549 Result.Factory.Method = Method;
550 Prev = &Result.Factory;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000551 continue;
552 }
553
Ted Kremenekda4abf12010-02-11 00:53:01 +0000554 ObjCMethodList *Mem =
555 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
556 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000557 Prev = Prev->Next;
558 }
559
560 return Result;
561 }
562};
Mike Stump11289f42009-09-09 15:08:12 +0000563
564} // end anonymous namespace
Douglas Gregorc78d3462009-04-24 21:10:55 +0000565
566/// \brief The on-disk hash table used for the global method pool.
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000567typedef OnDiskChainedHashTable<ASTSelectorLookupTrait>
568 ASTSelectorLookupTable;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000569
570namespace {
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000571class ASTIdentifierLookupTrait {
Sebastian Redl2c499f62010-08-18 23:56:43 +0000572 ASTReader &Reader;
Sebastian Redl4e6c5672010-07-21 22:31:37 +0000573 llvm::BitstreamCursor &Stream;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000574
575 // If we know the IdentifierInfo in advance, it is here and we will
576 // not build a new one. Used when deserializing information about an
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000577 // identifier that was constructed before the AST file was read.
Douglas Gregora868bbd2009-04-21 22:25:48 +0000578 IdentifierInfo *KnownII;
579
580public:
581 typedef IdentifierInfo * data_type;
582
583 typedef const std::pair<const char*, unsigned> external_key_type;
584
585 typedef external_key_type internal_key_type;
586
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000587 ASTIdentifierLookupTrait(ASTReader &Reader, llvm::BitstreamCursor &Stream,
Sebastian Redl4e6c5672010-07-21 22:31:37 +0000588 IdentifierInfo *II = 0)
589 : Reader(Reader), Stream(Stream), KnownII(II) { }
Mike Stump11289f42009-09-09 15:08:12 +0000590
Douglas Gregora868bbd2009-04-21 22:25:48 +0000591 static bool EqualKey(const internal_key_type& a,
592 const internal_key_type& b) {
593 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
594 : false;
595 }
Mike Stump11289f42009-09-09 15:08:12 +0000596
Douglas Gregora868bbd2009-04-21 22:25:48 +0000597 static unsigned ComputeHash(const internal_key_type& a) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000598 return llvm::HashString(llvm::StringRef(a.first, a.second));
Douglas Gregora868bbd2009-04-21 22:25:48 +0000599 }
Mike Stump11289f42009-09-09 15:08:12 +0000600
Douglas Gregora868bbd2009-04-21 22:25:48 +0000601 // This hopefully will just get inlined and removed by the optimizer.
602 static const internal_key_type&
603 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump11289f42009-09-09 15:08:12 +0000604
Douglas Gregora868bbd2009-04-21 22:25:48 +0000605 static std::pair<unsigned, unsigned>
606 ReadKeyDataLength(const unsigned char*& d) {
607 using namespace clang::io;
Douglas Gregor6b7bf5a2009-04-25 20:26:24 +0000608 unsigned DataLen = ReadUnalignedLE16(d);
Douglas Gregor5287b4e2009-04-25 21:04:17 +0000609 unsigned KeyLen = ReadUnalignedLE16(d);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000610 return std::make_pair(KeyLen, DataLen);
611 }
Mike Stump11289f42009-09-09 15:08:12 +0000612
Douglas Gregora868bbd2009-04-21 22:25:48 +0000613 static std::pair<const char*, unsigned>
614 ReadKey(const unsigned char* d, unsigned n) {
615 assert(n >= 2 && d[n-1] == '\0');
616 return std::make_pair((const char*) d, n-1);
617 }
Mike Stump11289f42009-09-09 15:08:12 +0000618
619 IdentifierInfo *ReadData(const internal_key_type& k,
Douglas Gregora868bbd2009-04-21 22:25:48 +0000620 const unsigned char* d,
621 unsigned DataLen) {
622 using namespace clang::io;
Sebastian Redl539c5062010-08-18 23:57:32 +0000623 IdentID ID = ReadUnalignedLE32(d);
Douglas Gregor1d583f22009-04-28 21:18:29 +0000624 bool IsInteresting = ID & 0x01;
625
626 // Wipe out the "is interesting" bit.
627 ID = ID >> 1;
628
629 if (!IsInteresting) {
Sebastian Redl98912122010-07-27 23:01:28 +0000630 // For uninteresting identifiers, just build the IdentifierInfo
Douglas Gregor1d583f22009-04-28 21:18:29 +0000631 // and associate it with the persistent ID.
632 IdentifierInfo *II = KnownII;
633 if (!II)
Sebastian Redl07a89a82010-07-30 00:29:29 +0000634 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregor1d583f22009-04-28 21:18:29 +0000635 Reader.SetIdentifierInfo(ID, II);
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000636 II->setIsFromAST();
Douglas Gregor1d583f22009-04-28 21:18:29 +0000637 return II;
638 }
639
Douglas Gregorb9256522009-04-28 21:32:13 +0000640 unsigned Bits = ReadUnalignedLE16(d);
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000641 bool CPlusPlusOperatorKeyword = Bits & 0x01;
642 Bits >>= 1;
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +0000643 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
644 Bits >>= 1;
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000645 bool Poisoned = Bits & 0x01;
646 Bits >>= 1;
647 bool ExtensionToken = Bits & 0x01;
648 Bits >>= 1;
649 bool hasMacroDefinition = Bits & 0x01;
650 Bits >>= 1;
651 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
652 Bits >>= 10;
Mike Stump11289f42009-09-09 15:08:12 +0000653
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000654 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregorb9256522009-04-28 21:32:13 +0000655 DataLen -= 6;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000656
657 // Build the IdentifierInfo itself and link the identifier ID with
658 // the new IdentifierInfo.
659 IdentifierInfo *II = KnownII;
660 if (!II)
Sebastian Redl07a89a82010-07-30 00:29:29 +0000661 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000662 Reader.SetIdentifierInfo(ID, II);
663
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000664 // Set or check the various bits in the IdentifierInfo structure.
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +0000665 // Token IDs are read-only.
666 if (HasRevertedTokenIDToIdentifier)
667 II->RevertTokenIDToIdentifier();
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000668 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
Mike Stump11289f42009-09-09 15:08:12 +0000669 assert(II->isExtensionToken() == ExtensionToken &&
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000670 "Incorrect extension token flag");
671 (void)ExtensionToken;
672 II->setIsPoisoned(Poisoned);
673 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
674 "Incorrect C++ operator keyword flag");
675 (void)CPlusPlusOperatorKeyword;
676
Douglas Gregorc3366a52009-04-21 23:56:24 +0000677 // If this identifier is a macro, deserialize the macro
678 // definition.
679 if (hasMacroDefinition) {
Douglas Gregorb9256522009-04-28 21:32:13 +0000680 uint32_t Offset = ReadUnalignedLE32(d);
Sebastian Redl4e6c5672010-07-21 22:31:37 +0000681 Reader.ReadMacroRecord(Stream, Offset);
Douglas Gregorb9256522009-04-28 21:32:13 +0000682 DataLen -= 4;
Douglas Gregorc3366a52009-04-21 23:56:24 +0000683 }
Douglas Gregora868bbd2009-04-21 22:25:48 +0000684
685 // Read all of the declarations visible at global scope with this
686 // name.
Chris Lattner1d728882009-04-27 22:17:41 +0000687 if (Reader.getContext() == 0) return II;
Douglas Gregor1342e842009-07-06 18:54:52 +0000688 if (DataLen > 0) {
689 llvm::SmallVector<uint32_t, 4> DeclIDs;
690 for (; DataLen > 0; DataLen -= 4)
691 DeclIDs.push_back(ReadUnalignedLE32(d));
692 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000693 }
Mike Stump11289f42009-09-09 15:08:12 +0000694
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000695 II->setIsFromAST();
Douglas Gregora868bbd2009-04-21 22:25:48 +0000696 return II;
697 }
698};
Mike Stump11289f42009-09-09 15:08:12 +0000699
700} // end anonymous namespace
Douglas Gregora868bbd2009-04-21 22:25:48 +0000701
702/// \brief The on-disk hash table used to contain information about
703/// all of the identifiers in the program.
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000704typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait>
705 ASTIdentifierLookupTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000706
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000707namespace {
708class ASTDeclContextNameLookupTrait {
709 ASTReader &Reader;
710
711public:
712 /// \brief Pair of begin/end iterators for DeclIDs.
713 typedef std::pair<DeclID *, DeclID *> data_type;
714
715 /// \brief Special internal key for declaration names.
716 /// The hash table creates keys for comparison; we do not create
717 /// a DeclarationName for the internal key to avoid deserializing types.
718 struct DeclNameKey {
719 DeclarationName::NameKind Kind;
720 uint64_t Data;
721 DeclNameKey() : Kind((DeclarationName::NameKind)0), Data(0) { }
722 };
723
724 typedef DeclarationName external_key_type;
725 typedef DeclNameKey internal_key_type;
726
727 explicit ASTDeclContextNameLookupTrait(ASTReader &Reader) : Reader(Reader) { }
728
729 static bool EqualKey(const internal_key_type& a,
730 const internal_key_type& b) {
731 return a.Kind == b.Kind && a.Data == b.Data;
732 }
733
734 unsigned ComputeHash(const DeclNameKey &Key) const {
735 llvm::FoldingSetNodeID ID;
736 ID.AddInteger(Key.Kind);
737
738 switch (Key.Kind) {
739 case DeclarationName::Identifier:
740 case DeclarationName::CXXLiteralOperatorName:
741 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
742 break;
743 case DeclarationName::ObjCZeroArgSelector:
744 case DeclarationName::ObjCOneArgSelector:
745 case DeclarationName::ObjCMultiArgSelector:
746 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
747 break;
748 case DeclarationName::CXXConstructorName:
749 case DeclarationName::CXXDestructorName:
750 case DeclarationName::CXXConversionFunctionName:
751 ID.AddInteger((TypeID)Key.Data);
752 break;
753 case DeclarationName::CXXOperatorName:
754 ID.AddInteger((OverloadedOperatorKind)Key.Data);
755 break;
756 case DeclarationName::CXXUsingDirective:
757 break;
758 }
759
760 return ID.ComputeHash();
761 }
762
763 internal_key_type GetInternalKey(const external_key_type& Name) const {
764 DeclNameKey Key;
765 Key.Kind = Name.getNameKind();
766 switch (Name.getNameKind()) {
767 case DeclarationName::Identifier:
768 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
769 break;
770 case DeclarationName::ObjCZeroArgSelector:
771 case DeclarationName::ObjCOneArgSelector:
772 case DeclarationName::ObjCMultiArgSelector:
773 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
774 break;
775 case DeclarationName::CXXConstructorName:
776 case DeclarationName::CXXDestructorName:
777 case DeclarationName::CXXConversionFunctionName:
778 Key.Data = Reader.GetTypeID(Name.getCXXNameType());
779 break;
780 case DeclarationName::CXXOperatorName:
781 Key.Data = Name.getCXXOverloadedOperator();
782 break;
783 case DeclarationName::CXXLiteralOperatorName:
784 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
785 break;
786 case DeclarationName::CXXUsingDirective:
787 break;
788 }
789
790 return Key;
791 }
792
793 static std::pair<unsigned, unsigned>
794 ReadKeyDataLength(const unsigned char*& d) {
795 using namespace clang::io;
796 unsigned KeyLen = ReadUnalignedLE16(d);
797 unsigned DataLen = ReadUnalignedLE16(d);
798 return std::make_pair(KeyLen, DataLen);
799 }
800
801 internal_key_type ReadKey(const unsigned char* d, unsigned) {
802 using namespace clang::io;
803
804 DeclNameKey Key;
805 Key.Kind = (DeclarationName::NameKind)*d++;
806 switch (Key.Kind) {
807 case DeclarationName::Identifier:
808 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
809 break;
810 case DeclarationName::ObjCZeroArgSelector:
811 case DeclarationName::ObjCOneArgSelector:
812 case DeclarationName::ObjCMultiArgSelector:
813 Key.Data =
814 (uint64_t)Reader.DecodeSelector(ReadUnalignedLE32(d)).getAsOpaquePtr();
815 break;
816 case DeclarationName::CXXConstructorName:
817 case DeclarationName::CXXDestructorName:
818 case DeclarationName::CXXConversionFunctionName:
819 Key.Data = ReadUnalignedLE32(d); // TypeID
820 break;
821 case DeclarationName::CXXOperatorName:
822 Key.Data = *d++; // OverloadedOperatorKind
823 break;
824 case DeclarationName::CXXLiteralOperatorName:
825 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
826 break;
827 case DeclarationName::CXXUsingDirective:
828 break;
829 }
830
831 return Key;
832 }
833
834 data_type ReadData(internal_key_type, const unsigned char* d,
835 unsigned DataLen) {
836 using namespace clang::io;
837 unsigned NumDecls = ReadUnalignedLE16(d);
838 DeclID *Start = (DeclID *)d;
839 return std::make_pair(Start, Start + NumDecls);
840 }
841};
842
843} // end anonymous namespace
844
845/// \brief The on-disk hash table used for the DeclContext's Name lookup table.
846typedef OnDiskChainedHashTable<ASTDeclContextNameLookupTrait>
847 ASTDeclContextNameLookupTable;
848
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000849bool ASTReader::ReadDeclContextStorage(llvm::BitstreamCursor &Cursor,
850 const std::pair<uint64_t, uint64_t> &Offsets,
851 DeclContextInfo &Info) {
852 SavedStreamPosition SavedPosition(Cursor);
853 // First the lexical decls.
854 if (Offsets.first != 0) {
855 Cursor.JumpToBit(Offsets.first);
856
857 RecordData Record;
858 const char *Blob;
859 unsigned BlobLen;
860 unsigned Code = Cursor.ReadCode();
861 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
862 if (RecCode != DECL_CONTEXT_LEXICAL) {
863 Error("Expected lexical block");
864 return true;
865 }
866
867 Info.LexicalDecls = reinterpret_cast<const DeclID*>(Blob);
868 Info.NumLexicalDecls = BlobLen / sizeof(DeclID);
869 } else {
870 Info.LexicalDecls = 0;
871 Info.NumLexicalDecls = 0;
872 }
873
874 // Now the lookup table.
875 if (Offsets.second != 0) {
876 Cursor.JumpToBit(Offsets.second);
877
878 RecordData Record;
879 const char *Blob;
880 unsigned BlobLen;
881 unsigned Code = Cursor.ReadCode();
882 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
883 if (RecCode != DECL_CONTEXT_VISIBLE) {
884 Error("Expected visible lookup table block");
885 return true;
886 }
887 Info.NameLookupTableData
888 = ASTDeclContextNameLookupTable::Create(
889 (const unsigned char *)Blob + Record[0],
890 (const unsigned char *)Blob,
891 ASTDeclContextNameLookupTrait(*this));
892 }
893
894 return false;
895}
896
Sebastian Redl2c499f62010-08-18 23:56:43 +0000897void ASTReader::Error(const char *Msg) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +0000898 Diag(diag::err_fe_pch_malformed) << Msg;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000899}
900
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000901/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redl2c499f62010-08-18 23:56:43 +0000902bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000903 if (Listener)
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000904 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000905 ActualOriginalFileName,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000906 SuggestedPredefines);
Douglas Gregorc379c072009-04-28 18:58:38 +0000907 return false;
Douglas Gregor92863e42009-04-10 23:10:45 +0000908}
909
Douglas Gregorc5046832009-04-27 18:38:38 +0000910//===----------------------------------------------------------------------===//
911// Source Manager Deserialization
912//===----------------------------------------------------------------------===//
913
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000914/// \brief Read the line table in the source manager block.
915/// \returns true if ther was an error.
Sebastian Redl2c499f62010-08-18 23:56:43 +0000916bool ASTReader::ParseLineTable(llvm::SmallVectorImpl<uint64_t> &Record) {
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000917 unsigned Idx = 0;
918 LineTableInfo &LineTable = SourceMgr.getLineTable();
919
920 // Parse the file names
Douglas Gregora8854652009-04-13 17:12:42 +0000921 std::map<int, int> FileIDs;
922 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000923 // Extract the file name
924 unsigned FilenameLen = Record[Idx++];
925 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
926 Idx += FilenameLen;
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000927 MaybeAddSystemRootToFilename(Filename);
Mike Stump11289f42009-09-09 15:08:12 +0000928 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
Douglas Gregora8854652009-04-13 17:12:42 +0000929 Filename.size());
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000930 }
931
932 // Parse the line entries
933 std::vector<LineEntry> Entries;
934 while (Idx < Record.size()) {
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000935 int FID = Record[Idx++];
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000936
937 // Extract the line entries
938 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000939 assert(NumEntries && "Numentries is 00000");
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000940 Entries.clear();
941 Entries.reserve(NumEntries);
942 for (unsigned I = 0; I != NumEntries; ++I) {
943 unsigned FileOffset = Record[Idx++];
944 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000945 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump11289f42009-09-09 15:08:12 +0000946 SrcMgr::CharacteristicKind FileKind
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000947 = (SrcMgr::CharacteristicKind)Record[Idx++];
948 unsigned IncludeOffset = Record[Idx++];
949 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
950 FileKind, IncludeOffset));
951 }
952 LineTable.AddEntry(FID, Entries);
953 }
954
955 return false;
956}
957
Douglas Gregorc5046832009-04-27 18:38:38 +0000958namespace {
959
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000960class ASTStatData {
Douglas Gregorc5046832009-04-27 18:38:38 +0000961public:
962 const bool hasStat;
963 const ino_t ino;
964 const dev_t dev;
965 const mode_t mode;
966 const time_t mtime;
967 const off_t size;
Mike Stump11289f42009-09-09 15:08:12 +0000968
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000969 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Mike Stump11289f42009-09-09 15:08:12 +0000970 : hasStat(true), ino(i), dev(d), mode(mo), mtime(m), size(s) {}
971
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000972 ASTStatData()
Douglas Gregorc5046832009-04-27 18:38:38 +0000973 : hasStat(false), ino(0), dev(0), mode(0), mtime(0), size(0) {}
974};
975
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000976class ASTStatLookupTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +0000977 public:
978 typedef const char *external_key_type;
979 typedef const char *internal_key_type;
980
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000981 typedef ASTStatData data_type;
Douglas Gregorc5046832009-04-27 18:38:38 +0000982
983 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000984 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +0000985 }
986
987 static internal_key_type GetInternalKey(const char *path) { return path; }
988
989 static bool EqualKey(internal_key_type a, internal_key_type b) {
990 return strcmp(a, b) == 0;
991 }
992
993 static std::pair<unsigned, unsigned>
994 ReadKeyDataLength(const unsigned char*& d) {
995 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
996 unsigned DataLen = (unsigned) *d++;
997 return std::make_pair(KeyLen + 1, DataLen);
998 }
999
1000 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1001 return (const char *)d;
1002 }
1003
1004 static data_type ReadData(const internal_key_type, const unsigned char *d,
1005 unsigned /*DataLen*/) {
1006 using namespace clang::io;
1007
1008 if (*d++ == 1)
1009 return data_type();
1010
1011 ino_t ino = (ino_t) ReadUnalignedLE32(d);
1012 dev_t dev = (dev_t) ReadUnalignedLE32(d);
1013 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump11289f42009-09-09 15:08:12 +00001014 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregorc5046832009-04-27 18:38:38 +00001015 off_t size = (off_t) ReadUnalignedLE64(d);
1016 return data_type(ino, dev, mode, mtime, size);
1017 }
1018};
1019
1020/// \brief stat() cache for precompiled headers.
1021///
1022/// This cache is very similar to the stat cache used by pretokenized
1023/// headers.
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001024class ASTStatCache : public StatSysCallCache {
1025 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregorc5046832009-04-27 18:38:38 +00001026 CacheTy *Cache;
1027
1028 unsigned &NumStatHits, &NumStatMisses;
Mike Stump11289f42009-09-09 15:08:12 +00001029public:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001030 ASTStatCache(const unsigned char *Buckets,
Douglas Gregorc5046832009-04-27 18:38:38 +00001031 const unsigned char *Base,
1032 unsigned &NumStatHits,
Mike Stump11289f42009-09-09 15:08:12 +00001033 unsigned &NumStatMisses)
Douglas Gregorc5046832009-04-27 18:38:38 +00001034 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
1035 Cache = CacheTy::Create(Buckets, Base);
1036 }
1037
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001038 ~ASTStatCache() { delete Cache; }
Mike Stump11289f42009-09-09 15:08:12 +00001039
Douglas Gregorc5046832009-04-27 18:38:38 +00001040 int stat(const char *path, struct stat *buf) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001041 // Do the lookup for the file's data in the AST file.
Douglas Gregorc5046832009-04-27 18:38:38 +00001042 CacheTy::iterator I = Cache->find(path);
1043
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001044 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregorc5046832009-04-27 18:38:38 +00001045 if (I == Cache->end()) {
1046 ++NumStatMisses;
Douglas Gregord2eb58a2009-10-16 18:18:30 +00001047 return StatSysCallCache::stat(path, buf);
Douglas Gregorc5046832009-04-27 18:38:38 +00001048 }
Mike Stump11289f42009-09-09 15:08:12 +00001049
Douglas Gregorc5046832009-04-27 18:38:38 +00001050 ++NumStatHits;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001051 ASTStatData Data = *I;
Mike Stump11289f42009-09-09 15:08:12 +00001052
Douglas Gregorc5046832009-04-27 18:38:38 +00001053 if (!Data.hasStat)
1054 return 1;
1055
1056 buf->st_ino = Data.ino;
1057 buf->st_dev = Data.dev;
1058 buf->st_mtime = Data.mtime;
1059 buf->st_mode = Data.mode;
1060 buf->st_size = Data.size;
1061 return 0;
1062 }
1063};
1064} // end anonymous namespace
1065
1066
Sebastian Redl393f8b72010-07-19 20:52:06 +00001067/// \brief Read a source manager block
Sebastian Redl2c499f62010-08-18 23:56:43 +00001068ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(PerFileData &F) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001069 using namespace SrcMgr;
Douglas Gregor258ae542009-04-27 06:38:32 +00001070
Sebastian Redl393f8b72010-07-19 20:52:06 +00001071 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001072
Douglas Gregor258ae542009-04-27 06:38:32 +00001073 // Set the source-location entry cursor to the current position in
1074 // the stream. This cursor will be used to read the contents of the
1075 // source manager block initially, and then lazily read
1076 // source-location entries as needed.
Sebastian Redl393f8b72010-07-19 20:52:06 +00001077 SLocEntryCursor = F.Stream;
Douglas Gregor258ae542009-04-27 06:38:32 +00001078
1079 // The stream itself is going to skip over the source manager block.
Sebastian Redl393f8b72010-07-19 20:52:06 +00001080 if (F.Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001081 Error("malformed block record in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001082 return Failure;
1083 }
1084
1085 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001086 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001087 Error("malformed source manager block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001088 return Failure;
1089 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001090
Douglas Gregora7f71a92009-04-10 03:52:48 +00001091 RecordData Record;
1092 while (true) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001093 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregora7f71a92009-04-10 03:52:48 +00001094 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001095 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001096 Error("error at end of Source Manager block in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001097 return Failure;
1098 }
Douglas Gregor92863e42009-04-10 23:10:45 +00001099 return Success;
Douglas Gregora7f71a92009-04-10 03:52:48 +00001100 }
Mike Stump11289f42009-09-09 15:08:12 +00001101
Douglas Gregora7f71a92009-04-10 03:52:48 +00001102 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1103 // No known subblocks, always skip them.
Douglas Gregor258ae542009-04-27 06:38:32 +00001104 SLocEntryCursor.ReadSubBlockID();
1105 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001106 Error("malformed block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001107 return Failure;
1108 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001109 continue;
1110 }
Mike Stump11289f42009-09-09 15:08:12 +00001111
Douglas Gregora7f71a92009-04-10 03:52:48 +00001112 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001113 SLocEntryCursor.ReadAbbrevRecord();
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 // Read a record.
1118 const char *BlobStart;
1119 unsigned BlobLen;
1120 Record.clear();
Douglas Gregor258ae542009-04-27 06:38:32 +00001121 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001122 default: // Default behavior: ignore.
1123 break;
1124
Sebastian Redl539c5062010-08-18 23:57:32 +00001125 case SM_LINE_TABLE:
Sebastian Redlb293a452010-07-20 21:20:32 +00001126 if (ParseLineTable(Record))
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001127 return Failure;
Chris Lattner184e65d2009-04-14 23:22:57 +00001128 break;
Douglas Gregoreda6a892009-04-26 00:07:37 +00001129
Sebastian Redl539c5062010-08-18 23:57:32 +00001130 case SM_SLOC_FILE_ENTRY:
1131 case SM_SLOC_BUFFER_ENTRY:
1132 case SM_SLOC_INSTANTIATION_ENTRY:
Douglas Gregor258ae542009-04-27 06:38:32 +00001133 // Once we hit one of the source location entries, we're done.
1134 return Success;
Douglas Gregora7f71a92009-04-10 03:52:48 +00001135 }
1136 }
1137}
1138
Sebastian Redl06750302010-07-20 21:50:20 +00001139/// \brief Get a cursor that's correctly positioned for reading the source
1140/// location entry with the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001141llvm::BitstreamCursor &ASTReader::SLocCursorForID(unsigned ID) {
Sebastian Redl06750302010-07-20 21:50:20 +00001142 assert(ID != 0 && ID <= TotalNumSLocEntries &&
1143 "SLocCursorForID should only be called for real IDs.");
1144
1145 ID -= 1;
1146 PerFileData *F = 0;
1147 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1148 F = Chain[N - I - 1];
1149 if (ID < F->LocalNumSLocEntries)
1150 break;
1151 ID -= F->LocalNumSLocEntries;
1152 }
1153 assert(F && F->LocalNumSLocEntries > ID && "Chain corrupted");
1154
1155 F->SLocEntryCursor.JumpToBit(F->SLocOffsets[ID]);
1156 return F->SLocEntryCursor;
1157}
1158
Douglas Gregor258ae542009-04-27 06:38:32 +00001159/// \brief Read in the source location entry with the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001160ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(unsigned ID) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001161 if (ID == 0)
1162 return Success;
1163
1164 if (ID > TotalNumSLocEntries) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001165 Error("source location entry ID out-of-range for AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001166 return Failure;
1167 }
1168
Sebastian Redl06750302010-07-20 21:50:20 +00001169 llvm::BitstreamCursor &SLocEntryCursor = SLocCursorForID(ID);
Sebastian Redl34522812010-07-16 17:50:48 +00001170
Douglas Gregor258ae542009-04-27 06:38:32 +00001171 ++NumSLocEntriesRead;
Douglas Gregor258ae542009-04-27 06:38:32 +00001172 unsigned Code = SLocEntryCursor.ReadCode();
1173 if (Code == llvm::bitc::END_BLOCK ||
1174 Code == llvm::bitc::ENTER_SUBBLOCK ||
1175 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001176 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001177 return Failure;
1178 }
1179
Douglas Gregor258ae542009-04-27 06:38:32 +00001180 RecordData Record;
1181 const char *BlobStart;
1182 unsigned BlobLen;
1183 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1184 default:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001185 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001186 return Failure;
1187
Sebastian Redl539c5062010-08-18 23:57:32 +00001188 case SM_SLOC_FILE_ENTRY: {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001189 std::string Filename(BlobStart, BlobStart + BlobLen);
1190 MaybeAddSystemRootToFilename(Filename);
1191 const FileEntry *File = FileMgr.getFile(Filename);
Chris Lattnerd20dc872009-06-15 04:35:16 +00001192 if (File == 0) {
1193 std::string ErrorStr = "could not find file '";
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001194 ErrorStr += Filename;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001195 ErrorStr += "' referenced by AST file";
Chris Lattnerd20dc872009-06-15 04:35:16 +00001196 Error(ErrorStr.c_str());
1197 return Failure;
1198 }
Mike Stump11289f42009-09-09 15:08:12 +00001199
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001200 if (Record.size() < 10) {
Ted Kremenekabb1ddd2010-03-18 21:23:05 +00001201 Error("source location entry is incorrect");
1202 return Failure;
1203 }
1204
Douglas Gregorce3a8292010-07-27 00:27:13 +00001205 if (!DisableValidation &&
1206 ((off_t)Record[4] != File->getSize()
Douglas Gregor08288f22010-04-09 15:54:22 +00001207#if !defined(LLVM_ON_WIN32)
1208 // In our regression testing, the Windows file system seems to
1209 // have inconsistent modification times that sometimes
1210 // erroneously trigger this error-handling path.
Douglas Gregorce3a8292010-07-27 00:27:13 +00001211 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor08288f22010-04-09 15:54:22 +00001212#endif
Douglas Gregorce3a8292010-07-27 00:27:13 +00001213 )) {
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001214 Diag(diag::err_fe_pch_file_modified)
1215 << Filename;
1216 return Failure;
1217 }
1218
Douglas Gregor258ae542009-04-27 06:38:32 +00001219 FileID FID = SourceMgr.createFileID(File,
1220 SourceLocation::getFromRawEncoding(Record[1]),
1221 (SrcMgr::CharacteristicKind)Record[2],
1222 ID, Record[0]);
1223 if (Record[3])
1224 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile())
1225 .setHasLineDirectives();
1226
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001227 // Reconstruct header-search information for this file.
1228 HeaderFileInfo HFI;
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001229 HFI.isImport = Record[6];
1230 HFI.DirInfo = Record[7];
1231 HFI.NumIncludes = Record[8];
1232 HFI.ControllingMacroID = Record[9];
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001233 if (Listener)
1234 Listener->ReadHeaderFileInfo(HFI, File->getUID());
Douglas Gregor258ae542009-04-27 06:38:32 +00001235 break;
1236 }
1237
Sebastian Redl539c5062010-08-18 23:57:32 +00001238 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor258ae542009-04-27 06:38:32 +00001239 const char *Name = BlobStart;
1240 unsigned Offset = Record[0];
1241 unsigned Code = SLocEntryCursor.ReadCode();
1242 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00001243 unsigned RecCode
Douglas Gregor258ae542009-04-27 06:38:32 +00001244 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001245
Sebastian Redl539c5062010-08-18 23:57:32 +00001246 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001247 Error("AST record has invalid code");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001248 return Failure;
1249 }
1250
Douglas Gregor258ae542009-04-27 06:38:32 +00001251 llvm::MemoryBuffer *Buffer
Chris Lattner58c79342010-04-05 22:42:27 +00001252 = llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(BlobStart, BlobLen - 1),
1253 Name);
Douglas Gregor258ae542009-04-27 06:38:32 +00001254 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset);
Mike Stump11289f42009-09-09 15:08:12 +00001255
Douglas Gregore6648fb2009-04-28 20:33:11 +00001256 if (strcmp(Name, "<built-in>") == 0) {
Sebastian Redl75fbb3b2010-07-14 17:49:11 +00001257 PCHPredefinesBlock Block = {
1258 BufferID,
1259 llvm::StringRef(BlobStart, BlobLen - 1)
1260 };
1261 PCHPredefinesBuffers.push_back(Block);
Douglas Gregore6648fb2009-04-28 20:33:11 +00001262 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001263
1264 break;
1265 }
1266
Sebastian Redl539c5062010-08-18 23:57:32 +00001267 case SM_SLOC_INSTANTIATION_ENTRY: {
Mike Stump11289f42009-09-09 15:08:12 +00001268 SourceLocation SpellingLoc
Douglas Gregor258ae542009-04-27 06:38:32 +00001269 = SourceLocation::getFromRawEncoding(Record[1]);
1270 SourceMgr.createInstantiationLoc(SpellingLoc,
1271 SourceLocation::getFromRawEncoding(Record[2]),
1272 SourceLocation::getFromRawEncoding(Record[3]),
1273 Record[4],
1274 ID,
1275 Record[0]);
1276 break;
Mike Stump11289f42009-09-09 15:08:12 +00001277 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001278 }
1279
1280 return Success;
1281}
1282
Chris Lattnere78a6be2009-04-27 01:05:14 +00001283/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1284/// specified cursor. Read the abbreviations that are at the top of the block
1285/// and then leave the cursor pointing into the block.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001286bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattnere78a6be2009-04-27 01:05:14 +00001287 unsigned BlockID) {
1288 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001289 Error("malformed block record in AST file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001290 return Failure;
1291 }
Mike Stump11289f42009-09-09 15:08:12 +00001292
Chris Lattnere78a6be2009-04-27 01:05:14 +00001293 while (true) {
1294 unsigned Code = Cursor.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00001295
Chris Lattnere78a6be2009-04-27 01:05:14 +00001296 // We expect all abbrevs to be at the start of the block.
1297 if (Code != llvm::bitc::DEFINE_ABBREV)
1298 return false;
1299 Cursor.ReadAbbrevRecord();
1300 }
1301}
1302
Sebastian Redl2c499f62010-08-18 23:56:43 +00001303void ASTReader::ReadMacroRecord(llvm::BitstreamCursor &Stream, uint64_t Offset){
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001304 assert(PP && "Forgot to set Preprocessor ?");
Mike Stump11289f42009-09-09 15:08:12 +00001305
Douglas Gregorc3366a52009-04-21 23:56:24 +00001306 // Keep track of where we are in the stream, then jump back there
1307 // after reading this macro.
1308 SavedStreamPosition SavedPosition(Stream);
1309
1310 Stream.JumpToBit(Offset);
1311 RecordData Record;
1312 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1313 MacroInfo *Macro = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001314
Douglas Gregorc3366a52009-04-21 23:56:24 +00001315 while (true) {
1316 unsigned Code = Stream.ReadCode();
1317 switch (Code) {
1318 case llvm::bitc::END_BLOCK:
1319 return;
1320
1321 case llvm::bitc::ENTER_SUBBLOCK:
1322 // No known subblocks, always skip them.
1323 Stream.ReadSubBlockID();
1324 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001325 Error("malformed block record in AST file");
Douglas Gregorc3366a52009-04-21 23:56:24 +00001326 return;
1327 }
1328 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001329
Douglas Gregorc3366a52009-04-21 23:56:24 +00001330 case llvm::bitc::DEFINE_ABBREV:
1331 Stream.ReadAbbrevRecord();
1332 continue;
1333 default: break;
1334 }
1335
1336 // Read a record.
1337 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001338 PreprocessorRecordTypes RecType =
1339 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001340 switch (RecType) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001341 case PP_MACRO_OBJECT_LIKE:
1342 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001343 // If we already have a macro, that means that we've hit the end
1344 // of the definition of the macro we were looking for. We're
1345 // done.
1346 if (Macro)
1347 return;
1348
1349 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1350 if (II == 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001351 Error("macro must have a name in AST file");
Douglas Gregorc3366a52009-04-21 23:56:24 +00001352 return;
1353 }
1354 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
1355 bool isUsed = Record[2];
Mike Stump11289f42009-09-09 15:08:12 +00001356
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001357 MacroInfo *MI = PP->AllocateMacroInfo(Loc);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001358 MI->setIsUsed(isUsed);
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001359 MI->setIsFromAST();
Mike Stump11289f42009-09-09 15:08:12 +00001360
Douglas Gregoraae92242010-03-19 21:51:54 +00001361 unsigned NextIndex = 3;
Sebastian Redl539c5062010-08-18 23:57:32 +00001362 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001363 // Decode function-like macro info.
1364 bool isC99VarArgs = Record[3];
1365 bool isGNUVarArgs = Record[4];
1366 MacroArgs.clear();
1367 unsigned NumArgs = Record[5];
Douglas Gregoraae92242010-03-19 21:51:54 +00001368 NextIndex = 6 + NumArgs;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001369 for (unsigned i = 0; i != NumArgs; ++i)
1370 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1371
1372 // Install function-like macro info.
1373 MI->setIsFunctionLike();
1374 if (isC99VarArgs) MI->setIsC99Varargs();
1375 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor038c3382009-05-22 22:45:36 +00001376 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001377 PP->getPreprocessorAllocator());
Douglas Gregorc3366a52009-04-21 23:56:24 +00001378 }
1379
1380 // Finally, install the macro.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001381 PP->setMacroInfo(II, MI);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001382
1383 // Remember that we saw this macro last so that we add the tokens that
1384 // form its body to it.
1385 Macro = MI;
Douglas Gregoraae92242010-03-19 21:51:54 +00001386
1387 if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) {
1388 // We have a macro definition. Load it now.
1389 PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro,
1390 getMacroDefinition(Record[NextIndex]));
1391 }
1392
Douglas Gregorc3366a52009-04-21 23:56:24 +00001393 ++NumMacrosRead;
1394 break;
1395 }
Mike Stump11289f42009-09-09 15:08:12 +00001396
Sebastian Redl539c5062010-08-18 23:57:32 +00001397 case PP_TOKEN: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001398 // If we see a TOKEN before a PP_MACRO_*, then the file is
1399 // erroneous, just pretend we didn't see this.
1400 if (Macro == 0) break;
Mike Stump11289f42009-09-09 15:08:12 +00001401
Douglas Gregorc3366a52009-04-21 23:56:24 +00001402 Token Tok;
1403 Tok.startToken();
1404 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
1405 Tok.setLength(Record[1]);
1406 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1407 Tok.setIdentifierInfo(II);
1408 Tok.setKind((tok::TokenKind)Record[3]);
1409 Tok.setFlag((Token::TokenFlags)Record[4]);
1410 Macro->AddTokenToBody(Tok);
1411 break;
1412 }
Douglas Gregoraae92242010-03-19 21:51:54 +00001413
Sebastian Redl539c5062010-08-18 23:57:32 +00001414 case PP_MACRO_INSTANTIATION: {
Douglas Gregoraae92242010-03-19 21:51:54 +00001415 // If we already have a macro, that means that we've hit the end
1416 // of the definition of the macro we were looking for. We're
1417 // done.
1418 if (Macro)
1419 return;
1420
1421 if (!PP->getPreprocessingRecord()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001422 Error("missing preprocessing record in AST file");
Douglas Gregoraae92242010-03-19 21:51:54 +00001423 return;
1424 }
1425
1426 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1427 if (PPRec.getPreprocessedEntity(Record[0]))
1428 return;
1429
1430 MacroInstantiation *MI
1431 = new (PPRec) MacroInstantiation(DecodeIdentifierInfo(Record[3]),
1432 SourceRange(
1433 SourceLocation::getFromRawEncoding(Record[1]),
1434 SourceLocation::getFromRawEncoding(Record[2])),
1435 getMacroDefinition(Record[4]));
1436 PPRec.SetPreallocatedEntity(Record[0], MI);
1437 return;
1438 }
1439
Sebastian Redl539c5062010-08-18 23:57:32 +00001440 case PP_MACRO_DEFINITION: {
Douglas Gregoraae92242010-03-19 21:51:54 +00001441 // If we already have a macro, that means that we've hit the end
1442 // of the definition of the macro we were looking for. We're
1443 // done.
1444 if (Macro)
1445 return;
1446
1447 if (!PP->getPreprocessingRecord()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001448 Error("missing preprocessing record in AST file");
Douglas Gregoraae92242010-03-19 21:51:54 +00001449 return;
1450 }
1451
1452 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1453 if (PPRec.getPreprocessedEntity(Record[0]))
1454 return;
1455
1456 if (Record[1] >= MacroDefinitionsLoaded.size()) {
1457 Error("out-of-bounds macro definition record");
1458 return;
1459 }
1460
1461 MacroDefinition *MD
1462 = new (PPRec) MacroDefinition(DecodeIdentifierInfo(Record[4]),
1463 SourceLocation::getFromRawEncoding(Record[5]),
1464 SourceRange(
1465 SourceLocation::getFromRawEncoding(Record[2]),
1466 SourceLocation::getFromRawEncoding(Record[3])));
1467 PPRec.SetPreallocatedEntity(Record[0], MD);
1468 MacroDefinitionsLoaded[Record[1]] = MD;
1469 return;
1470 }
Steve Naroff3fa455a2009-04-24 20:03:17 +00001471 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001472 }
1473}
1474
Sebastian Redl2c499f62010-08-18 23:56:43 +00001475void ASTReader::ReadDefinedMacros() {
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001476 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1477 llvm::BitstreamCursor &MacroCursor = Chain[N - I - 1]->MacroCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001478
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001479 // If there was no preprocessor block, skip this file.
1480 if (!MacroCursor.getBitStreamReader())
1481 continue;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001482
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001483 llvm::BitstreamCursor Cursor = MacroCursor;
Sebastian Redl539c5062010-08-18 23:57:32 +00001484 if (Cursor.EnterSubBlock(PREPROCESSOR_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001485 Error("malformed preprocessor block record in AST file");
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001486 return;
1487 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001488
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001489 RecordData Record;
1490 while (true) {
1491 unsigned Code = Cursor.ReadCode();
1492 if (Code == llvm::bitc::END_BLOCK) {
1493 if (Cursor.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001494 Error("error at end of preprocessor block in AST file");
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001495 return;
1496 }
1497 break;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001498 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001499
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001500 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1501 // No known subblocks, always skip them.
1502 Cursor.ReadSubBlockID();
1503 if (Cursor.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001504 Error("malformed block record in AST file");
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001505 return;
1506 }
1507 continue;
1508 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001509
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001510 if (Code == llvm::bitc::DEFINE_ABBREV) {
1511 Cursor.ReadAbbrevRecord();
1512 continue;
1513 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001514
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001515 // Read a record.
1516 const char *BlobStart;
1517 unsigned BlobLen;
1518 Record.clear();
1519 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1520 default: // Default behavior: ignore.
1521 break;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001522
Sebastian Redl539c5062010-08-18 23:57:32 +00001523 case PP_MACRO_OBJECT_LIKE:
1524 case PP_MACRO_FUNCTION_LIKE:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001525 DecodeIdentifierInfo(Record[0]);
1526 break;
1527
Sebastian Redl539c5062010-08-18 23:57:32 +00001528 case PP_TOKEN:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001529 // Ignore tokens.
1530 break;
Douglas Gregoraae92242010-03-19 21:51:54 +00001531
Sebastian Redl539c5062010-08-18 23:57:32 +00001532 case PP_MACRO_INSTANTIATION:
1533 case PP_MACRO_DEFINITION:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001534 // Read the macro record.
1535 ReadMacroRecord(Chain[N - I - 1]->Stream, Cursor.GetCurrentBitNo());
1536 break;
1537 }
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001538 }
1539 }
1540}
1541
Sebastian Redl539c5062010-08-18 23:57:32 +00001542MacroDefinition *ASTReader::getMacroDefinition(IdentID ID) {
Douglas Gregoraae92242010-03-19 21:51:54 +00001543 if (ID == 0 || ID >= MacroDefinitionsLoaded.size())
1544 return 0;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001545
1546 if (!MacroDefinitionsLoaded[ID]) {
1547 unsigned Index = ID;
1548 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1549 PerFileData &F = *Chain[N - I - 1];
1550 if (Index < F.LocalNumMacroDefinitions) {
1551 ReadMacroRecord(F.Stream, F.MacroDefinitionOffsets[Index]);
1552 break;
1553 }
1554 Index -= F.LocalNumMacroDefinitions;
1555 }
1556 assert(MacroDefinitionsLoaded[ID] && "Broken chain");
1557 }
1558
Douglas Gregoraae92242010-03-19 21:51:54 +00001559 return MacroDefinitionsLoaded[ID];
1560}
1561
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001562/// \brief If we are loading a relocatable PCH file, and the filename is
1563/// not an absolute path, add the system root to the beginning of the file
1564/// name.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001565void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001566 // If this is not a relocatable PCH file, there's nothing to do.
1567 if (!RelocatablePCH)
1568 return;
Mike Stump11289f42009-09-09 15:08:12 +00001569
Daniel Dunbarf2ce9a22009-11-18 19:50:41 +00001570 if (Filename.empty() || llvm::sys::Path(Filename).isAbsolute())
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001571 return;
1572
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001573 if (isysroot == 0) {
1574 // If no system root was given, default to '/'
1575 Filename.insert(Filename.begin(), '/');
1576 return;
1577 }
Mike Stump11289f42009-09-09 15:08:12 +00001578
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001579 unsigned Length = strlen(isysroot);
1580 if (isysroot[Length - 1] != '/')
1581 Filename.insert(Filename.begin(), '/');
Mike Stump11289f42009-09-09 15:08:12 +00001582
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001583 Filename.insert(Filename.begin(), isysroot, isysroot + Length);
1584}
1585
Sebastian Redl2c499f62010-08-18 23:56:43 +00001586ASTReader::ASTReadResult
Sebastian Redl3e31c722010-08-18 23:56:56 +00001587ASTReader::ReadASTBlock(PerFileData &F) {
Sebastian Redl34522812010-07-16 17:50:48 +00001588 llvm::BitstreamCursor &Stream = F.Stream;
1589
Sebastian Redl539c5062010-08-18 23:57:32 +00001590 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001591 Error("malformed block record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001592 return Failure;
1593 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001594
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001595 // Read all of the records and blocks for the ASt file.
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001596 RecordData Record;
Sebastian Redl393f8b72010-07-19 20:52:06 +00001597 bool First = true;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001598 while (!Stream.AtEndOfStream()) {
1599 unsigned Code = Stream.ReadCode();
1600 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor55abb232009-04-10 20:39:37 +00001601 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001602 Error("error at end of module block in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001603 return Failure;
1604 }
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001605
Douglas Gregor55abb232009-04-10 20:39:37 +00001606 return Success;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001607 }
1608
1609 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1610 switch (Stream.ReadSubBlockID()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001611 case DECLTYPES_BLOCK_ID:
Chris Lattnere78a6be2009-04-27 01:05:14 +00001612 // We lazily load the decls block, but we want to set up the
1613 // DeclsCursor cursor to point into it. Clone our current bitcode
1614 // cursor to it, enter the block and read the abbrevs in that block.
1615 // With the main cursor, we just skip over it.
Sebastian Redl34522812010-07-16 17:50:48 +00001616 F.DeclsCursor = Stream;
Chris Lattnere78a6be2009-04-27 01:05:14 +00001617 if (Stream.SkipBlock() || // Skip with the main cursor.
1618 // Read the abbrevs.
Sebastian Redl539c5062010-08-18 23:57:32 +00001619 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001620 Error("malformed block record in AST file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001621 return Failure;
1622 }
1623 break;
Mike Stump11289f42009-09-09 15:08:12 +00001624
Sebastian Redl539c5062010-08-18 23:57:32 +00001625 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl34522812010-07-16 17:50:48 +00001626 F.MacroCursor = Stream;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001627 if (PP)
1628 PP->setExternalSource(this);
1629
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001630 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001631 Error("malformed block record in AST file");
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001632 return Failure;
1633 }
1634 break;
Steve Naroff2ddea052009-04-23 10:39:46 +00001635
Sebastian Redl539c5062010-08-18 23:57:32 +00001636 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl393f8b72010-07-19 20:52:06 +00001637 switch (ReadSourceManagerBlock(F)) {
Douglas Gregor92863e42009-04-10 23:10:45 +00001638 case Success:
1639 break;
1640
1641 case Failure:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001642 Error("malformed source manager block in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001643 return Failure;
Douglas Gregor92863e42009-04-10 23:10:45 +00001644
1645 case IgnorePCH:
1646 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00001647 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001648 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001649 }
Sebastian Redl393f8b72010-07-19 20:52:06 +00001650 First = false;
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001651 continue;
1652 }
1653
1654 if (Code == llvm::bitc::DEFINE_ABBREV) {
1655 Stream.ReadAbbrevRecord();
1656 continue;
1657 }
1658
1659 // Read and process a record.
1660 Record.clear();
Douglas Gregorbfbde532009-04-10 21:16:55 +00001661 const char *BlobStart = 0;
1662 unsigned BlobLen = 0;
Sebastian Redl539c5062010-08-18 23:57:32 +00001663 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Douglas Gregorbfbde532009-04-10 21:16:55 +00001664 &BlobStart, &BlobLen)) {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001665 default: // Default behavior: ignore.
1666 break;
1667
Sebastian Redl539c5062010-08-18 23:57:32 +00001668 case METADATA: {
1669 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1670 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001671 : diag::warn_pch_version_too_new);
1672 return IgnorePCH;
1673 }
1674
1675 RelocatablePCH = Record[4];
1676 if (Listener) {
1677 std::string TargetTriple(BlobStart, BlobLen);
1678 if (Listener->ReadTargetTriple(TargetTriple))
1679 return IgnorePCH;
1680 }
1681 break;
1682 }
1683
Sebastian Redl539c5062010-08-18 23:57:32 +00001684 case CHAINED_METADATA: {
Sebastian Redl393f8b72010-07-19 20:52:06 +00001685 if (!First) {
1686 Error("CHAINED_METADATA is not first record in block");
1687 return Failure;
1688 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001689 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1690 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001691 : diag::warn_pch_version_too_new);
1692 return IgnorePCH;
1693 }
1694
1695 // Load the chained file.
Sebastian Redl3e31c722010-08-18 23:56:56 +00001696 switch(ReadASTCore(llvm::StringRef(BlobStart, BlobLen))) {
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001697 case Failure: return Failure;
1698 // If we have to ignore the dependency, we'll have to ignore this too.
1699 case IgnorePCH: return IgnorePCH;
1700 case Success: break;
1701 }
1702 break;
1703 }
1704
Sebastian Redl539c5062010-08-18 23:57:32 +00001705 case TYPE_OFFSET:
Sebastian Redl9e687992010-07-19 22:06:55 +00001706 if (F.LocalNumTypes != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001707 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001708 return Failure;
1709 }
Sebastian Redl9e687992010-07-19 22:06:55 +00001710 F.TypeOffsets = (const uint32_t *)BlobStart;
1711 F.LocalNumTypes = Record[0];
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001712 break;
1713
Sebastian Redl539c5062010-08-18 23:57:32 +00001714 case DECL_OFFSET:
Sebastian Redl9e687992010-07-19 22:06:55 +00001715 if (F.LocalNumDecls != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001716 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001717 return Failure;
1718 }
Sebastian Redl9e687992010-07-19 22:06:55 +00001719 F.DeclOffsets = (const uint32_t *)BlobStart;
1720 F.LocalNumDecls = Record[0];
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001721 break;
Douglas Gregor55abb232009-04-10 20:39:37 +00001722
Sebastian Redl539c5062010-08-18 23:57:32 +00001723 case TU_UPDATE_LEXICAL: {
Sebastian Redl4b1f4902010-07-27 18:24:41 +00001724 DeclContextInfo Info = {
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001725 /* No visible information */ 0,
Sebastian Redl539c5062010-08-18 23:57:32 +00001726 reinterpret_cast<const DeclID *>(BlobStart),
1727 BlobLen / sizeof(DeclID)
Sebastian Redl4b1f4902010-07-27 18:24:41 +00001728 };
1729 DeclContextOffsets[Context->getTranslationUnitDecl()].push_back(Info);
1730 break;
1731 }
1732
Sebastian Redl539c5062010-08-18 23:57:32 +00001733 case REDECLS_UPDATE_LATEST: {
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00001734 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
1735 for (unsigned i = 0, e = Record.size(); i < e; i += 2) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001736 DeclID First = Record[i], Latest = Record[i+1];
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00001737 assert((FirstLatestDeclIDs.find(First) == FirstLatestDeclIDs.end() ||
1738 Latest > FirstLatestDeclIDs[First]) &&
1739 "The new latest is supposed to come after the previous latest");
1740 FirstLatestDeclIDs[First] = Latest;
1741 }
1742 break;
1743 }
1744
Sebastian Redl539c5062010-08-18 23:57:32 +00001745 case LANGUAGE_OPTIONS:
Douglas Gregorce3a8292010-07-27 00:27:13 +00001746 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor55abb232009-04-10 20:39:37 +00001747 return IgnorePCH;
1748 break;
Douglas Gregorbfbde532009-04-10 21:16:55 +00001749
Sebastian Redl539c5062010-08-18 23:57:32 +00001750 case IDENTIFIER_TABLE:
Sebastian Redl393f8b72010-07-19 20:52:06 +00001751 F.IdentifierTableData = BlobStart;
Douglas Gregor0e149972009-04-25 19:10:14 +00001752 if (Record[0]) {
Sebastian Redl393f8b72010-07-19 20:52:06 +00001753 F.IdentifierLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001754 = ASTIdentifierLookupTable::Create(
Sebastian Redl393f8b72010-07-19 20:52:06 +00001755 (const unsigned char *)F.IdentifierTableData + Record[0],
1756 (const unsigned char *)F.IdentifierTableData,
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001757 ASTIdentifierLookupTrait(*this, F.Stream));
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001758 if (PP)
1759 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor0e149972009-04-25 19:10:14 +00001760 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001761 break;
1762
Sebastian Redl539c5062010-08-18 23:57:32 +00001763 case IDENTIFIER_OFFSET:
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00001764 if (F.LocalNumIdentifiers != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001765 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001766 return Failure;
1767 }
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00001768 F.IdentifierOffsets = (const uint32_t *)BlobStart;
1769 F.LocalNumIdentifiers = Record[0];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001770 break;
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00001771
Sebastian Redl539c5062010-08-18 23:57:32 +00001772 case EXTERNAL_DEFINITIONS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001773 // Optimization for the first block.
1774 if (ExternalDefinitions.empty())
1775 ExternalDefinitions.swap(Record);
1776 else
1777 ExternalDefinitions.insert(ExternalDefinitions.end(),
1778 Record.begin(), Record.end());
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00001779 break;
Douglas Gregor08f01292009-04-17 22:13:46 +00001780
Sebastian Redl539c5062010-08-18 23:57:32 +00001781 case SPECIAL_TYPES:
Sebastian Redlb293a452010-07-20 21:20:32 +00001782 // Optimization for the first block
1783 if (SpecialTypes.empty())
1784 SpecialTypes.swap(Record);
1785 else
1786 SpecialTypes.insert(SpecialTypes.end(), Record.begin(), Record.end());
Douglas Gregor652d82a2009-04-18 05:55:16 +00001787 break;
1788
Sebastian Redl539c5062010-08-18 23:57:32 +00001789 case STATISTICS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001790 TotalNumStatements += Record[0];
1791 TotalNumMacros += Record[1];
1792 TotalLexicalDeclContexts += Record[2];
1793 TotalVisibleDeclContexts += Record[3];
Douglas Gregor08f01292009-04-17 22:13:46 +00001794 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00001795
Sebastian Redl539c5062010-08-18 23:57:32 +00001796 case TENTATIVE_DEFINITIONS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001797 // Optimization for the first block.
1798 if (TentativeDefinitions.empty())
1799 TentativeDefinitions.swap(Record);
1800 else
1801 TentativeDefinitions.insert(TentativeDefinitions.end(),
1802 Record.begin(), Record.end());
Douglas Gregord4df8652009-04-22 22:02:47 +00001803 break;
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001804
Sebastian Redl539c5062010-08-18 23:57:32 +00001805 case UNUSED_FILESCOPED_DECLS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001806 // Optimization for the first block.
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00001807 if (UnusedFileScopedDecls.empty())
1808 UnusedFileScopedDecls.swap(Record);
Sebastian Redlb293a452010-07-20 21:20:32 +00001809 else
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00001810 UnusedFileScopedDecls.insert(UnusedFileScopedDecls.end(),
1811 Record.begin(), Record.end());
Tanya Lattner90073802010-02-12 00:07:30 +00001812 break;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001813
Sebastian Redl539c5062010-08-18 23:57:32 +00001814 case WEAK_UNDECLARED_IDENTIFIERS:
Sebastian Redl08aca90252010-08-05 18:21:25 +00001815 // Later blocks overwrite earlier ones.
1816 WeakUndeclaredIdentifiers.swap(Record);
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00001817 break;
1818
Sebastian Redl539c5062010-08-18 23:57:32 +00001819 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001820 // Optimization for the first block.
1821 if (LocallyScopedExternalDecls.empty())
1822 LocallyScopedExternalDecls.swap(Record);
1823 else
1824 LocallyScopedExternalDecls.insert(LocallyScopedExternalDecls.end(),
1825 Record.begin(), Record.end());
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001826 break;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001827
Sebastian Redl539c5062010-08-18 23:57:32 +00001828 case SELECTOR_OFFSETS:
Sebastian Redla19a67f2010-08-03 21:58:15 +00001829 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redlada023c2010-08-04 20:40:17 +00001830 F.LocalNumSelectors = Record[0];
Douglas Gregor95c13f52009-04-25 17:48:32 +00001831 break;
1832
Sebastian Redl539c5062010-08-18 23:57:32 +00001833 case METHOD_POOL:
Sebastian Redlada023c2010-08-04 20:40:17 +00001834 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor95c13f52009-04-25 17:48:32 +00001835 if (Record[0])
Sebastian Redlada023c2010-08-04 20:40:17 +00001836 F.SelectorLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001837 = ASTSelectorLookupTable::Create(
Sebastian Redlada023c2010-08-04 20:40:17 +00001838 F.SelectorLookupTableData + Record[0],
1839 F.SelectorLookupTableData,
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001840 ASTSelectorLookupTrait(*this));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00001841 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorc78d3462009-04-24 21:10:55 +00001842 break;
Douglas Gregoreda6a892009-04-26 00:07:37 +00001843
Sebastian Redl539c5062010-08-18 23:57:32 +00001844 case REFERENCED_SELECTOR_POOL: {
Sebastian Redlada023c2010-08-04 20:40:17 +00001845 ReferencedSelectorsData.insert(ReferencedSelectorsData.end(),
1846 Record.begin(), Record.end());
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001847 break;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001848 }
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001849
Sebastian Redl539c5062010-08-18 23:57:32 +00001850 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001851 if (!Record.empty() && Listener)
1852 Listener->ReadCounter(Record[0]);
Douglas Gregoreda6a892009-04-26 00:07:37 +00001853 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00001854
Sebastian Redl539c5062010-08-18 23:57:32 +00001855 case SOURCE_LOCATION_OFFSETS:
Sebastian Redlb293a452010-07-20 21:20:32 +00001856 F.SLocOffsets = (const uint32_t *)BlobStart;
1857 F.LocalNumSLocEntries = Record[0];
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001858 // We cannot delay this until the entire chain is loaded, because then
1859 // source location preloads would also have to be delayed.
1860 // FIXME: Is there a reason not to do that?
Sebastian Redlb293a452010-07-20 21:20:32 +00001861 TotalNumSLocEntries += F.LocalNumSLocEntries;
Douglas Gregord54f3a12009-10-05 21:07:28 +00001862 SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, Record[1]);
Douglas Gregor258ae542009-04-27 06:38:32 +00001863 break;
1864
Sebastian Redl539c5062010-08-18 23:57:32 +00001865 case SOURCE_LOCATION_PRELOADS:
Douglas Gregor258ae542009-04-27 06:38:32 +00001866 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
Sebastian Redl2c499f62010-08-18 23:56:43 +00001867 ASTReadResult Result = ReadSLocEntryRecord(Record[I]);
Douglas Gregor258ae542009-04-27 06:38:32 +00001868 if (Result != Success)
1869 return Result;
1870 }
1871 break;
Douglas Gregorc5046832009-04-27 18:38:38 +00001872
Sebastian Redl539c5062010-08-18 23:57:32 +00001873 case STAT_CACHE: {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001874 ASTStatCache *MyStatCache =
1875 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
Douglas Gregord2eb58a2009-10-16 18:18:30 +00001876 (const unsigned char *)BlobStart,
1877 NumStatHits, NumStatMisses);
1878 FileMgr.addStatCache(MyStatCache);
Sebastian Redl34522812010-07-16 17:50:48 +00001879 F.StatCache = MyStatCache;
Douglas Gregorc5046832009-04-27 18:38:38 +00001880 break;
Douglas Gregord2eb58a2009-10-16 18:18:30 +00001881 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001882
Sebastian Redl539c5062010-08-18 23:57:32 +00001883 case EXT_VECTOR_DECLS:
Sebastian Redl04f5c312010-07-28 21:38:49 +00001884 // Optimization for the first block.
1885 if (ExtVectorDecls.empty())
1886 ExtVectorDecls.swap(Record);
1887 else
1888 ExtVectorDecls.insert(ExtVectorDecls.end(),
1889 Record.begin(), Record.end());
Douglas Gregor61cac2b2009-04-27 20:06:05 +00001890 break;
1891
Sebastian Redl539c5062010-08-18 23:57:32 +00001892 case VTABLE_USES:
Sebastian Redl08aca90252010-08-05 18:21:25 +00001893 // Later tables overwrite earlier ones.
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00001894 VTableUses.swap(Record);
1895 break;
1896
Sebastian Redl539c5062010-08-18 23:57:32 +00001897 case DYNAMIC_CLASSES:
Sebastian Redl08aca90252010-08-05 18:21:25 +00001898 // Optimization for the first block.
1899 if (DynamicClasses.empty())
1900 DynamicClasses.swap(Record);
1901 else
1902 DynamicClasses.insert(DynamicClasses.end(),
1903 Record.begin(), Record.end());
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00001904 break;
1905
Sebastian Redl539c5062010-08-18 23:57:32 +00001906 case PENDING_IMPLICIT_INSTANTIATIONS:
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00001907 // Optimization for the first block.
1908 if (PendingImplicitInstantiations.empty())
1909 PendingImplicitInstantiations.swap(Record);
1910 else
1911 PendingImplicitInstantiations.insert(
1912 PendingImplicitInstantiations.end(), Record.begin(), Record.end());
1913 break;
1914
Sebastian Redl539c5062010-08-18 23:57:32 +00001915 case SEMA_DECL_REFS:
Sebastian Redl08aca90252010-08-05 18:21:25 +00001916 // Later tables overwrite earlier ones.
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00001917 SemaDeclRefs.swap(Record);
1918 break;
1919
Sebastian Redl539c5062010-08-18 23:57:32 +00001920 case ORIGINAL_FILE_NAME:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001921 // The primary AST will be the last to get here, so it will be the one
Sebastian Redlb293a452010-07-20 21:20:32 +00001922 // that's used.
Daniel Dunbar000c4ff2009-11-11 05:29:04 +00001923 ActualOriginalFileName.assign(BlobStart, BlobLen);
1924 OriginalFileName = ActualOriginalFileName;
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001925 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001926 break;
Mike Stump11289f42009-09-09 15:08:12 +00001927
Sebastian Redl539c5062010-08-18 23:57:32 +00001928 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek8bd09292010-02-12 23:31:14 +00001929 const std::string &CurBranch = getClangFullRepositoryVersion();
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001930 llvm::StringRef ASTBranch(BlobStart, BlobLen);
1931 if (llvm::StringRef(CurBranch) != ASTBranch && !DisableValidation) {
1932 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregord54f3a12009-10-05 21:07:28 +00001933 return IgnorePCH;
1934 }
1935 break;
1936 }
Sebastian Redlfa061442010-07-21 20:07:32 +00001937
Sebastian Redl539c5062010-08-18 23:57:32 +00001938 case MACRO_DEFINITION_OFFSETS:
Sebastian Redlfa061442010-07-21 20:07:32 +00001939 F.MacroDefinitionOffsets = (const uint32_t *)BlobStart;
1940 F.NumPreallocatedPreprocessingEntities = Record[0];
1941 F.LocalNumMacroDefinitions = Record[1];
Douglas Gregoraae92242010-03-19 21:51:54 +00001942 break;
Sebastian Redle7c1fe62010-08-13 00:28:03 +00001943
Sebastian Redl539c5062010-08-18 23:57:32 +00001944 case DECL_REPLACEMENTS: {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00001945 if (Record.size() % 2 != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001946 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redle7c1fe62010-08-13 00:28:03 +00001947 return Failure;
1948 }
1949 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Sebastian Redl539c5062010-08-18 23:57:32 +00001950 ReplacedDecls[static_cast<DeclID>(Record[I])] =
Sebastian Redle7c1fe62010-08-13 00:28:03 +00001951 std::make_pair(&F, Record[I+1]);
1952 break;
1953 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001954 }
Sebastian Redl393f8b72010-07-19 20:52:06 +00001955 First = false;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001956 }
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001957 Error("premature end of bitstream in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001958 return Failure;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001959}
1960
Sebastian Redl3e31c722010-08-18 23:56:56 +00001961ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName) {
1962 switch(ReadASTCore(FileName)) {
Sebastian Redl2abc0382010-07-16 20:41:52 +00001963 case Failure: return Failure;
1964 case IgnorePCH: return IgnorePCH;
1965 case Success: break;
1966 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001967
1968 // Here comes stuff that we only do once the entire chain is loaded.
1969
Sebastian Redlb293a452010-07-20 21:20:32 +00001970 // Allocate space for loaded identifiers, decls and types.
Sebastian Redlfa061442010-07-21 20:07:32 +00001971 unsigned TotalNumIdentifiers = 0, TotalNumTypes = 0, TotalNumDecls = 0,
Sebastian Redlada023c2010-08-04 20:40:17 +00001972 TotalNumPreallocatedPreprocessingEntities = 0, TotalNumMacroDefs = 0,
1973 TotalNumSelectors = 0;
Sebastian Redl9e687992010-07-19 22:06:55 +00001974 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00001975 TotalNumIdentifiers += Chain[I]->LocalNumIdentifiers;
Sebastian Redl9e687992010-07-19 22:06:55 +00001976 TotalNumTypes += Chain[I]->LocalNumTypes;
1977 TotalNumDecls += Chain[I]->LocalNumDecls;
Sebastian Redlfa061442010-07-21 20:07:32 +00001978 TotalNumPreallocatedPreprocessingEntities +=
1979 Chain[I]->NumPreallocatedPreprocessingEntities;
1980 TotalNumMacroDefs += Chain[I]->LocalNumMacroDefinitions;
Sebastian Redlada023c2010-08-04 20:40:17 +00001981 TotalNumSelectors += Chain[I]->LocalNumSelectors;
Sebastian Redl9e687992010-07-19 22:06:55 +00001982 }
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00001983 IdentifiersLoaded.resize(TotalNumIdentifiers);
Sebastian Redl9e687992010-07-19 22:06:55 +00001984 TypesLoaded.resize(TotalNumTypes);
1985 DeclsLoaded.resize(TotalNumDecls);
Sebastian Redlfa061442010-07-21 20:07:32 +00001986 MacroDefinitionsLoaded.resize(TotalNumMacroDefs);
1987 if (PP) {
1988 if (TotalNumIdentifiers > 0)
1989 PP->getHeaderSearchInfo().SetExternalLookup(this);
1990 if (TotalNumPreallocatedPreprocessingEntities > 0) {
1991 if (!PP->getPreprocessingRecord())
1992 PP->createPreprocessingRecord();
1993 PP->getPreprocessingRecord()->SetExternalSource(*this,
1994 TotalNumPreallocatedPreprocessingEntities);
1995 }
1996 }
Sebastian Redlada023c2010-08-04 20:40:17 +00001997 SelectorsLoaded.resize(TotalNumSelectors);
Sebastian Redl9e687992010-07-19 22:06:55 +00001998
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001999 // Check the predefines buffers.
Douglas Gregorce3a8292010-07-27 00:27:13 +00002000 if (!DisableValidation && CheckPredefinesBuffers())
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002001 return IgnorePCH;
2002
2003 if (PP) {
2004 // Initialization of keywords and pragmas occurs before the
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002005 // AST file is read, so there may be some identifiers that were
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002006 // loaded into the IdentifierTable before we intercepted the
2007 // creation of identifiers. Iterate through the list of known
2008 // identifiers and determine whether we have to establish
2009 // preprocessor definitions or top-level identifier declaration
2010 // chains for those identifiers.
2011 //
2012 // We copy the IdentifierInfo pointers to a small vector first,
2013 // since de-serializing declarations or macro definitions can add
2014 // new entries into the identifier table, invalidating the
2015 // iterators.
2016 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
2017 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
2018 IdEnd = PP->getIdentifierTable().end();
2019 Id != IdEnd; ++Id)
2020 Identifiers.push_back(Id->second);
Sebastian Redlfa061442010-07-21 20:07:32 +00002021 // We need to search the tables in all files.
Sebastian Redlfa061442010-07-21 20:07:32 +00002022 for (unsigned J = 0, M = Chain.size(); J != M; ++J) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002023 ASTIdentifierLookupTable *IdTable
2024 = (ASTIdentifierLookupTable *)Chain[J]->IdentifierLookupTable;
2025 // Not all AST files necessarily have identifier tables, only the useful
Sebastian Redl5c415f32010-07-22 17:01:13 +00002026 // ones.
2027 if (!IdTable)
2028 continue;
Sebastian Redlfa061442010-07-21 20:07:32 +00002029 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2030 IdentifierInfo *II = Identifiers[I];
2031 // Look in the on-disk hash tables for an entry for this identifier
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002032 ASTIdentifierLookupTrait Info(*this, Chain[J]->Stream, II);
Sebastian Redlfa061442010-07-21 20:07:32 +00002033 std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength());
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002034 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
Sebastian Redlb293a452010-07-20 21:20:32 +00002035 if (Pos == IdTable->end())
2036 continue;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002037
Sebastian Redlb293a452010-07-20 21:20:32 +00002038 // Dereferencing the iterator has the effect of populating the
2039 // IdentifierInfo node with the various declarations it needs.
2040 (void)*Pos;
2041 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002042 }
2043 }
2044
2045 if (Context)
2046 InitializeContext(*Context);
2047
2048 return Success;
2049}
2050
Sebastian Redl3e31c722010-08-18 23:56:56 +00002051ASTReader::ASTReadResult ASTReader::ReadASTCore(llvm::StringRef FileName) {
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002052 Chain.push_back(new PerFileData());
Sebastian Redl34522812010-07-16 17:50:48 +00002053 PerFileData &F = *Chain.back();
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002054
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002055 // Set the AST file name.
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002056 F.FileName = FileName;
2057
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002058 // Open the AST file.
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002059 //
2060 // FIXME: This shouldn't be here, we should just take a raw_ostream.
2061 std::string ErrStr;
2062 F.Buffer.reset(llvm::MemoryBuffer::getFileOrSTDIN(FileName, &ErrStr));
2063 if (!F.Buffer) {
2064 Error(ErrStr.c_str());
2065 return IgnorePCH;
2066 }
2067
2068 // Initialize the stream
2069 F.StreamFile.init((const unsigned char *)F.Buffer->getBufferStart(),
2070 (const unsigned char *)F.Buffer->getBufferEnd());
Sebastian Redl34522812010-07-16 17:50:48 +00002071 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002072 Stream.init(F.StreamFile);
Sebastian Redlfa061442010-07-21 20:07:32 +00002073 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002074
2075 // Sniff for the signature.
2076 if (Stream.Read(8) != 'C' ||
2077 Stream.Read(8) != 'P' ||
2078 Stream.Read(8) != 'C' ||
2079 Stream.Read(8) != 'H') {
2080 Diag(diag::err_not_a_pch_file) << FileName;
2081 return Failure;
2082 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002083
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002084 while (!Stream.AtEndOfStream()) {
2085 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00002086
Douglas Gregor92863e42009-04-10 23:10:45 +00002087 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002088 Error("invalid record at top-level of AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002089 return Failure;
2090 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002091
2092 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregora868bbd2009-04-21 22:25:48 +00002093
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002094 // We only know the AST subblock ID.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002095 switch (BlockID) {
2096 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregor92863e42009-04-10 23:10:45 +00002097 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002098 Error("malformed BlockInfoBlock in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002099 return Failure;
2100 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002101 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00002102 case AST_BLOCK_ID:
Sebastian Redl3e31c722010-08-18 23:56:56 +00002103 switch (ReadASTBlock(F)) {
Douglas Gregor55abb232009-04-10 20:39:37 +00002104 case Success:
2105 break;
2106
2107 case Failure:
Douglas Gregor92863e42009-04-10 23:10:45 +00002108 return Failure;
Douglas Gregor55abb232009-04-10 20:39:37 +00002109
2110 case IgnorePCH:
Douglas Gregorbfbde532009-04-10 21:16:55 +00002111 // FIXME: We could consider reading through to the end of this
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002112 // AST block, skipping subblocks, to see if there are other
2113 // AST blocks elsewhere.
Douglas Gregor0bc12932009-04-27 21:28:04 +00002114
2115 // Clear out any preallocated source location entries, so that
2116 // the source manager does not try to resolve them later.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002117 SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor0bc12932009-04-27 21:28:04 +00002118
2119 // Remove the stat cache.
Sebastian Redl34522812010-07-16 17:50:48 +00002120 if (F.StatCache)
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002121 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor0bc12932009-04-27 21:28:04 +00002122
Douglas Gregor92863e42009-04-10 23:10:45 +00002123 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00002124 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002125 break;
2126 default:
Douglas Gregor92863e42009-04-10 23:10:45 +00002127 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002128 Error("malformed block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002129 return Failure;
2130 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002131 break;
2132 }
Mike Stump11289f42009-09-09 15:08:12 +00002133 }
2134
Sebastian Redl2abc0382010-07-16 20:41:52 +00002135 return Success;
2136}
2137
Sebastian Redl2c499f62010-08-18 23:56:43 +00002138void ASTReader::setPreprocessor(Preprocessor &pp) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002139 PP = &pp;
Sebastian Redlfa061442010-07-21 20:07:32 +00002140
2141 unsigned TotalNum = 0;
2142 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
2143 TotalNum += Chain[I]->NumPreallocatedPreprocessingEntities;
2144 if (TotalNum) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002145 if (!PP->getPreprocessingRecord())
2146 PP->createPreprocessingRecord();
Sebastian Redlfa061442010-07-21 20:07:32 +00002147 PP->getPreprocessingRecord()->SetExternalSource(*this, TotalNum);
Douglas Gregoraae92242010-03-19 21:51:54 +00002148 }
2149}
2150
Sebastian Redl2c499f62010-08-18 23:56:43 +00002151void ASTReader::InitializeContext(ASTContext &Ctx) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002152 Context = &Ctx;
2153 assert(Context && "Passed null context!");
2154
2155 assert(PP && "Forgot to set Preprocessor ?");
2156 PP->getIdentifierTable().setExternalIdentifierLookup(this);
2157 PP->getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor9882a5a2010-01-04 19:18:44 +00002158 PP->setExternalSource(this);
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00002159
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002160 // Load the translation unit declaration
Argyrios Kyrtzidis7e8996c2010-07-08 17:13:02 +00002161 GetTranslationUnitDecl();
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002162
2163 // Load the special types.
2164 Context->setBuiltinVaListType(
Sebastian Redl539c5062010-08-18 23:57:32 +00002165 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
2166 if (unsigned Id = SpecialTypes[SPECIAL_TYPE_OBJC_ID])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002167 Context->setObjCIdType(GetType(Id));
Sebastian Redl539c5062010-08-18 23:57:32 +00002168 if (unsigned Sel = SpecialTypes[SPECIAL_TYPE_OBJC_SELECTOR])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002169 Context->setObjCSelType(GetType(Sel));
Sebastian Redl539c5062010-08-18 23:57:32 +00002170 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002171 Context->setObjCProtoType(GetType(Proto));
Sebastian Redl539c5062010-08-18 23:57:32 +00002172 if (unsigned Class = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002173 Context->setObjCClassType(GetType(Class));
Steve Naroff7cae42b2009-07-10 23:34:53 +00002174
Sebastian Redl539c5062010-08-18 23:57:32 +00002175 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002176 Context->setCFConstantStringType(GetType(String));
Mike Stump11289f42009-09-09 15:08:12 +00002177 if (unsigned FastEnum
Sebastian Redl539c5062010-08-18 23:57:32 +00002178 = SpecialTypes[SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002179 Context->setObjCFastEnumerationStateType(GetType(FastEnum));
Sebastian Redl539c5062010-08-18 23:57:32 +00002180 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
Douglas Gregor27821ce2009-07-07 16:35:42 +00002181 QualType FileType = GetType(File);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002182 if (FileType.isNull()) {
2183 Error("FILE type is NULL");
2184 return;
2185 }
John McCall9dd450b2009-09-21 23:43:11 +00002186 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
Douglas Gregor27821ce2009-07-07 16:35:42 +00002187 Context->setFILEDecl(Typedef->getDecl());
2188 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002189 const TagType *Tag = FileType->getAs<TagType>();
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002190 if (!Tag) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002191 Error("Invalid FILE type in AST file");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002192 return;
2193 }
Douglas Gregor27821ce2009-07-07 16:35:42 +00002194 Context->setFILEDecl(Tag->getDecl());
2195 }
2196 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002197 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
Mike Stumpa4de80b2009-07-28 02:25:19 +00002198 QualType Jmp_bufType = GetType(Jmp_buf);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002199 if (Jmp_bufType.isNull()) {
2200 Error("jmp_bug type is NULL");
2201 return;
2202 }
John McCall9dd450b2009-09-21 23:43:11 +00002203 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
Mike Stumpa4de80b2009-07-28 02:25:19 +00002204 Context->setjmp_bufDecl(Typedef->getDecl());
2205 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002206 const TagType *Tag = Jmp_bufType->getAs<TagType>();
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002207 if (!Tag) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002208 Error("Invalid jmp_buf type in AST file");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002209 return;
2210 }
Mike Stumpa4de80b2009-07-28 02:25:19 +00002211 Context->setjmp_bufDecl(Tag->getDecl());
2212 }
2213 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002214 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
Mike Stumpa4de80b2009-07-28 02:25:19 +00002215 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002216 if (Sigjmp_bufType.isNull()) {
2217 Error("sigjmp_buf type is NULL");
2218 return;
2219 }
John McCall9dd450b2009-09-21 23:43:11 +00002220 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
Mike Stumpa4de80b2009-07-28 02:25:19 +00002221 Context->setsigjmp_bufDecl(Typedef->getDecl());
2222 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002223 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002224 assert(Tag && "Invalid sigjmp_buf type in AST file");
Mike Stumpa4de80b2009-07-28 02:25:19 +00002225 Context->setsigjmp_bufDecl(Tag->getDecl());
2226 }
2227 }
Mike Stump11289f42009-09-09 15:08:12 +00002228 if (unsigned ObjCIdRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002229 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION])
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002230 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
Mike Stump11289f42009-09-09 15:08:12 +00002231 if (unsigned ObjCClassRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002232 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION])
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002233 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
Sebastian Redl539c5062010-08-18 23:57:32 +00002234 if (unsigned String = SpecialTypes[SPECIAL_TYPE_BLOCK_DESCRIPTOR])
Mike Stumpd0153282009-10-20 02:12:22 +00002235 Context->setBlockDescriptorType(GetType(String));
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002236 if (unsigned String
Sebastian Redl539c5062010-08-18 23:57:32 +00002237 = SpecialTypes[SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR])
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002238 Context->setBlockDescriptorExtendedType(GetType(String));
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002239 if (unsigned ObjCSelRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002240 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION])
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002241 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
Sebastian Redl539c5062010-08-18 23:57:32 +00002242 if (unsigned String = SpecialTypes[SPECIAL_TYPE_NS_CONSTANT_STRING])
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002243 Context->setNSConstantStringType(GetType(String));
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002244
Sebastian Redl539c5062010-08-18 23:57:32 +00002245 if (SpecialTypes[SPECIAL_TYPE_INT128_INSTALLED])
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002246 Context->setInt128Installed();
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002247}
2248
Douglas Gregor45fe0362009-05-12 01:31:05 +00002249/// \brief Retrieve the name of the original source file name
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002250/// directly from the AST file, without actually loading the AST
Douglas Gregor45fe0362009-05-12 01:31:05 +00002251/// file.
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002252std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Daniel Dunbar3b951482009-12-03 09:13:06 +00002253 Diagnostic &Diags) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002254 // Open the AST file.
Douglas Gregor45fe0362009-05-12 01:31:05 +00002255 std::string ErrStr;
2256 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002257 Buffer.reset(llvm::MemoryBuffer::getFile(ASTFileName.c_str(), &ErrStr));
Douglas Gregor45fe0362009-05-12 01:31:05 +00002258 if (!Buffer) {
Daniel Dunbar3b951482009-12-03 09:13:06 +00002259 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002260 return std::string();
2261 }
2262
2263 // Initialize the stream
2264 llvm::BitstreamReader StreamFile;
2265 llvm::BitstreamCursor Stream;
Mike Stump11289f42009-09-09 15:08:12 +00002266 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregor45fe0362009-05-12 01:31:05 +00002267 (const unsigned char *)Buffer->getBufferEnd());
2268 Stream.init(StreamFile);
2269
2270 // Sniff for the signature.
2271 if (Stream.Read(8) != 'C' ||
2272 Stream.Read(8) != 'P' ||
2273 Stream.Read(8) != 'C' ||
2274 Stream.Read(8) != 'H') {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002275 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002276 return std::string();
2277 }
2278
2279 RecordData Record;
2280 while (!Stream.AtEndOfStream()) {
2281 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00002282
Douglas Gregor45fe0362009-05-12 01:31:05 +00002283 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2284 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump11289f42009-09-09 15:08:12 +00002285
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002286 // We only know the AST subblock ID.
Douglas Gregor45fe0362009-05-12 01:31:05 +00002287 switch (BlockID) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002288 case AST_BLOCK_ID:
2289 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002290 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002291 return std::string();
2292 }
2293 break;
Mike Stump11289f42009-09-09 15:08:12 +00002294
Douglas Gregor45fe0362009-05-12 01:31:05 +00002295 default:
2296 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002297 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002298 return std::string();
2299 }
2300 break;
2301 }
2302 continue;
2303 }
2304
2305 if (Code == llvm::bitc::END_BLOCK) {
2306 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002307 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002308 return std::string();
2309 }
2310 continue;
2311 }
2312
2313 if (Code == llvm::bitc::DEFINE_ABBREV) {
2314 Stream.ReadAbbrevRecord();
2315 continue;
2316 }
2317
2318 Record.clear();
2319 const char *BlobStart = 0;
2320 unsigned BlobLen = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002321 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl539c5062010-08-18 23:57:32 +00002322 == ORIGINAL_FILE_NAME)
Douglas Gregor45fe0362009-05-12 01:31:05 +00002323 return std::string(BlobStart, BlobLen);
Mike Stump11289f42009-09-09 15:08:12 +00002324 }
Douglas Gregor45fe0362009-05-12 01:31:05 +00002325
2326 return std::string();
2327}
2328
Douglas Gregor55abb232009-04-10 20:39:37 +00002329/// \brief Parse the record that corresponds to a LangOptions data
2330/// structure.
2331///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002332/// This routine parses the language options from the AST file and then gives
2333/// them to the AST listener if one is set.
Douglas Gregor55abb232009-04-10 20:39:37 +00002334///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002335/// \returns true if the listener deems the file unacceptable, false otherwise.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002336bool ASTReader::ParseLanguageOptions(
Douglas Gregor55abb232009-04-10 20:39:37 +00002337 const llvm::SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002338 if (Listener) {
2339 LangOptions LangOpts;
Mike Stump11289f42009-09-09 15:08:12 +00002340
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002341 #define PARSE_LANGOPT(Option) \
2342 LangOpts.Option = Record[Idx]; \
2343 ++Idx
Mike Stump11289f42009-09-09 15:08:12 +00002344
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002345 unsigned Idx = 0;
2346 PARSE_LANGOPT(Trigraphs);
2347 PARSE_LANGOPT(BCPLComment);
2348 PARSE_LANGOPT(DollarIdents);
2349 PARSE_LANGOPT(AsmPreprocessor);
2350 PARSE_LANGOPT(GNUMode);
Chandler Carruthe03aa552010-04-17 20:17:31 +00002351 PARSE_LANGOPT(GNUKeywords);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002352 PARSE_LANGOPT(ImplicitInt);
2353 PARSE_LANGOPT(Digraphs);
2354 PARSE_LANGOPT(HexFloats);
2355 PARSE_LANGOPT(C99);
2356 PARSE_LANGOPT(Microsoft);
2357 PARSE_LANGOPT(CPlusPlus);
2358 PARSE_LANGOPT(CPlusPlus0x);
2359 PARSE_LANGOPT(CXXOperatorNames);
2360 PARSE_LANGOPT(ObjC1);
2361 PARSE_LANGOPT(ObjC2);
2362 PARSE_LANGOPT(ObjCNonFragileABI);
Fariborz Jahanian45878032010-02-09 19:31:38 +00002363 PARSE_LANGOPT(ObjCNonFragileABI2);
Fariborz Jahanian62c56022010-04-22 21:01:59 +00002364 PARSE_LANGOPT(NoConstantCFStrings);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002365 PARSE_LANGOPT(PascalStrings);
2366 PARSE_LANGOPT(WritableStrings);
2367 PARSE_LANGOPT(LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +00002368 PARSE_LANGOPT(AltiVec);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002369 PARSE_LANGOPT(Exceptions);
Daniel Dunbar925152c2010-02-10 18:48:44 +00002370 PARSE_LANGOPT(SjLjExceptions);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002371 PARSE_LANGOPT(NeXTRuntime);
2372 PARSE_LANGOPT(Freestanding);
2373 PARSE_LANGOPT(NoBuiltin);
2374 PARSE_LANGOPT(ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +00002375 PARSE_LANGOPT(POSIXThreads);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002376 PARSE_LANGOPT(Blocks);
2377 PARSE_LANGOPT(EmitAllDecls);
2378 PARSE_LANGOPT(MathErrno);
Chris Lattner51924e512010-06-26 21:25:03 +00002379 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
2380 Record[Idx++]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002381 PARSE_LANGOPT(HeinousExtensions);
2382 PARSE_LANGOPT(Optimize);
2383 PARSE_LANGOPT(OptimizeSize);
2384 PARSE_LANGOPT(Static);
2385 PARSE_LANGOPT(PICLevel);
2386 PARSE_LANGOPT(GNUInline);
2387 PARSE_LANGOPT(NoInline);
2388 PARSE_LANGOPT(AccessControl);
2389 PARSE_LANGOPT(CharIsSigned);
John Thompsoned4e2952009-11-05 20:14:16 +00002390 PARSE_LANGOPT(ShortWChar);
Chris Lattner51924e512010-06-26 21:25:03 +00002391 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
2392 LangOpts.setVisibilityMode((LangOptions::VisibilityMode)Record[Idx++]);
Daniel Dunbar143021e2009-09-21 04:16:19 +00002393 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
Chris Lattner51924e512010-06-26 21:25:03 +00002394 Record[Idx++]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002395 PARSE_LANGOPT(InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +00002396 PARSE_LANGOPT(OpenCL);
Mike Stumpd9546382009-12-12 01:27:46 +00002397 PARSE_LANGOPT(CatchUndefined);
2398 // FIXME: Missing ElideConstructors?!
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002399 #undef PARSE_LANGOPT
Douglas Gregor55abb232009-04-10 20:39:37 +00002400
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002401 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor55abb232009-04-10 20:39:37 +00002402 }
Douglas Gregor55abb232009-04-10 20:39:37 +00002403
2404 return false;
2405}
2406
Sebastian Redl2c499f62010-08-18 23:56:43 +00002407void ASTReader::ReadPreprocessedEntities() {
Douglas Gregoraae92242010-03-19 21:51:54 +00002408 ReadDefinedMacros();
2409}
2410
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002411/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002412ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002413 PerFileData *F = 0;
2414 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2415 F = Chain[N - I - 1];
2416 if (Index < F->LocalNumTypes)
2417 break;
2418 Index -= F->LocalNumTypes;
2419 }
2420 assert(F && F->LocalNumTypes > Index && "Broken chain");
Sebastian Redlb2831db2010-07-20 22:55:31 +00002421 return RecordLocation(&F->DeclsCursor, F->TypeOffsets[Index]);
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002422}
2423
2424/// \brief Read and return the type with the given index..
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002425///
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002426/// The index is the type ID, shifted and minus the number of predefs. This
2427/// routine actually reads the record corresponding to the type at the given
2428/// location. It is a helper routine for GetType, which deals with reading type
2429/// IDs.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002430QualType ASTReader::ReadTypeRecord(unsigned Index) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002431 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redlb2831db2010-07-20 22:55:31 +00002432 llvm::BitstreamCursor &DeclsCursor = *Loc.first;
Sebastian Redl34522812010-07-16 17:50:48 +00002433
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002434 // Keep track of where we are in the stream, then jump back there
2435 // after reading this type.
Douglas Gregor12bfa382009-10-17 00:13:19 +00002436 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002437
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00002438 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redleaa4ade2010-08-11 18:52:41 +00002439
Douglas Gregor1342e842009-07-06 18:54:52 +00002440 // Note that we are loading a type record.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00002441 Deserializing AType(this);
Mike Stump11289f42009-09-09 15:08:12 +00002442
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002443 DeclsCursor.JumpToBit(Loc.second);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002444 RecordData Record;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002445 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl539c5062010-08-18 23:57:32 +00002446 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
2447 case TYPE_EXT_QUAL: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002448 if (Record.size() != 2) {
2449 Error("Incorrect encoding of extended qualifier type");
2450 return QualType();
2451 }
Douglas Gregor455b8f42009-04-15 22:00:08 +00002452 QualType Base = GetType(Record[0]);
John McCall8ccfcb52009-09-24 19:53:00 +00002453 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]);
2454 return Context->getQualifiedType(Base, Quals);
Douglas Gregor455b8f42009-04-15 22:00:08 +00002455 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002456
Sebastian Redl539c5062010-08-18 23:57:32 +00002457 case TYPE_COMPLEX: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002458 if (Record.size() != 1) {
2459 Error("Incorrect encoding of complex type");
2460 return QualType();
2461 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002462 QualType ElemType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002463 return Context->getComplexType(ElemType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002464 }
2465
Sebastian Redl539c5062010-08-18 23:57:32 +00002466 case TYPE_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002467 if (Record.size() != 1) {
2468 Error("Incorrect encoding of pointer type");
2469 return QualType();
2470 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002471 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002472 return Context->getPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002473 }
2474
Sebastian Redl539c5062010-08-18 23:57:32 +00002475 case TYPE_BLOCK_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002476 if (Record.size() != 1) {
2477 Error("Incorrect encoding of block pointer type");
2478 return QualType();
2479 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002480 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002481 return Context->getBlockPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002482 }
2483
Sebastian Redl539c5062010-08-18 23:57:32 +00002484 case TYPE_LVALUE_REFERENCE: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002485 if (Record.size() != 1) {
2486 Error("Incorrect encoding of lvalue reference type");
2487 return QualType();
2488 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002489 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002490 return Context->getLValueReferenceType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002491 }
2492
Sebastian Redl539c5062010-08-18 23:57:32 +00002493 case TYPE_RVALUE_REFERENCE: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002494 if (Record.size() != 1) {
2495 Error("Incorrect encoding of rvalue reference type");
2496 return QualType();
2497 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002498 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002499 return Context->getRValueReferenceType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002500 }
2501
Sebastian Redl539c5062010-08-18 23:57:32 +00002502 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidisee776bc2010-07-02 11:55:15 +00002503 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002504 Error("Incorrect encoding of member pointer type");
2505 return QualType();
2506 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002507 QualType PointeeType = GetType(Record[0]);
2508 QualType ClassType = GetType(Record[1]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002509 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002510 }
2511
Sebastian Redl539c5062010-08-18 23:57:32 +00002512 case TYPE_CONSTANT_ARRAY: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002513 QualType ElementType = GetType(Record[0]);
2514 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2515 unsigned IndexTypeQuals = Record[2];
2516 unsigned Idx = 3;
2517 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor04318252009-07-06 15:59:29 +00002518 return Context->getConstantArrayType(ElementType, Size,
2519 ASM, IndexTypeQuals);
2520 }
2521
Sebastian Redl539c5062010-08-18 23:57:32 +00002522 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002523 QualType ElementType = GetType(Record[0]);
2524 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2525 unsigned IndexTypeQuals = Record[2];
Chris Lattner8575daa2009-04-27 21:45:14 +00002526 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002527 }
2528
Sebastian Redl539c5062010-08-18 23:57:32 +00002529 case TYPE_VARIABLE_ARRAY: {
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002530 QualType ElementType = GetType(Record[0]);
2531 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2532 unsigned IndexTypeQuals = Record[2];
Douglas Gregor04318252009-07-06 15:59:29 +00002533 SourceLocation LBLoc = SourceLocation::getFromRawEncoding(Record[3]);
2534 SourceLocation RBLoc = SourceLocation::getFromRawEncoding(Record[4]);
Sebastian Redlc67764e2010-07-22 22:43:28 +00002535 return Context->getVariableArrayType(ElementType, ReadExpr(DeclsCursor),
Douglas Gregor04318252009-07-06 15:59:29 +00002536 ASM, IndexTypeQuals,
2537 SourceRange(LBLoc, RBLoc));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002538 }
2539
Sebastian Redl539c5062010-08-18 23:57:32 +00002540 case TYPE_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00002541 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002542 Error("incorrect encoding of vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002543 return QualType();
2544 }
2545
2546 QualType ElementType = GetType(Record[0]);
2547 unsigned NumElements = Record[1];
Chris Lattner37141f42010-06-23 06:00:24 +00002548 unsigned AltiVecSpec = Record[2];
2549 return Context->getVectorType(ElementType, NumElements,
2550 (VectorType::AltiVecSpecific)AltiVecSpec);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002551 }
2552
Sebastian Redl539c5062010-08-18 23:57:32 +00002553 case TYPE_EXT_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00002554 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002555 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002556 return QualType();
2557 }
2558
2559 QualType ElementType = GetType(Record[0]);
2560 unsigned NumElements = Record[1];
Chris Lattner8575daa2009-04-27 21:45:14 +00002561 return Context->getExtVectorType(ElementType, NumElements);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002562 }
2563
Sebastian Redl539c5062010-08-18 23:57:32 +00002564 case TYPE_FUNCTION_NO_PROTO: {
Rafael Espindola49b85ab2010-03-30 22:15:11 +00002565 if (Record.size() != 4) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00002566 Error("incorrect encoding of no-proto function type");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002567 return QualType();
2568 }
2569 QualType ResultType = GetType(Record[0]);
Rafael Espindola49b85ab2010-03-30 22:15:11 +00002570 FunctionType::ExtInfo Info(Record[1], Record[2], (CallingConv)Record[3]);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00002571 return Context->getFunctionNoProtoType(ResultType, Info);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002572 }
2573
Sebastian Redl539c5062010-08-18 23:57:32 +00002574 case TYPE_FUNCTION_PROTO: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002575 QualType ResultType = GetType(Record[0]);
Douglas Gregordc728752009-12-22 18:11:50 +00002576 bool NoReturn = Record[1];
Rafael Espindola49b85ab2010-03-30 22:15:11 +00002577 unsigned RegParm = Record[2];
2578 CallingConv CallConv = (CallingConv)Record[3];
2579 unsigned Idx = 4;
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002580 unsigned NumParams = Record[Idx++];
2581 llvm::SmallVector<QualType, 16> ParamTypes;
2582 for (unsigned I = 0; I != NumParams; ++I)
2583 ParamTypes.push_back(GetType(Record[Idx++]));
2584 bool isVariadic = Record[Idx++];
2585 unsigned Quals = Record[Idx++];
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00002586 bool hasExceptionSpec = Record[Idx++];
2587 bool hasAnyExceptionSpec = Record[Idx++];
2588 unsigned NumExceptions = Record[Idx++];
2589 llvm::SmallVector<QualType, 2> Exceptions;
2590 for (unsigned I = 0; I != NumExceptions; ++I)
2591 Exceptions.push_back(GetType(Record[Idx++]));
Jay Foad7d0479f2009-05-21 09:52:38 +00002592 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00002593 isVariadic, Quals, hasExceptionSpec,
2594 hasAnyExceptionSpec, NumExceptions,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00002595 Exceptions.data(),
Rafael Espindola49b85ab2010-03-30 22:15:11 +00002596 FunctionType::ExtInfo(NoReturn, RegParm,
2597 CallConv));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002598 }
2599
Sebastian Redl539c5062010-08-18 23:57:32 +00002600 case TYPE_UNRESOLVED_USING:
John McCallb96ec562009-12-04 22:46:56 +00002601 return Context->getTypeDeclType(
2602 cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0])));
2603
Sebastian Redl539c5062010-08-18 23:57:32 +00002604 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002605 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002606 Error("incorrect encoding of typedef type");
2607 return QualType();
2608 }
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002609 TypedefDecl *Decl = cast<TypedefDecl>(GetDecl(Record[0]));
2610 QualType Canonical = GetType(Record[1]);
2611 return Context->getTypedefType(Decl, Canonical);
2612 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002613
Sebastian Redl539c5062010-08-18 23:57:32 +00002614 case TYPE_TYPEOF_EXPR:
Sebastian Redlc67764e2010-07-22 22:43:28 +00002615 return Context->getTypeOfExprType(ReadExpr(DeclsCursor));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002616
Sebastian Redl539c5062010-08-18 23:57:32 +00002617 case TYPE_TYPEOF: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002618 if (Record.size() != 1) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002619 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002620 return QualType();
2621 }
2622 QualType UnderlyingType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002623 return Context->getTypeOfType(UnderlyingType);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002624 }
Mike Stump11289f42009-09-09 15:08:12 +00002625
Sebastian Redl539c5062010-08-18 23:57:32 +00002626 case TYPE_DECLTYPE:
Sebastian Redlc67764e2010-07-22 22:43:28 +00002627 return Context->getDecltypeType(ReadExpr(DeclsCursor));
Anders Carlsson81df7b82009-06-24 19:06:50 +00002628
Sebastian Redl539c5062010-08-18 23:57:32 +00002629 case TYPE_RECORD: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002630 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002631 Error("incorrect encoding of record type");
2632 return QualType();
2633 }
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002634 bool IsDependent = Record[0];
2635 QualType T = Context->getRecordType(cast<RecordDecl>(GetDecl(Record[1])));
2636 T->Dependent = IsDependent;
2637 return T;
2638 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002639
Sebastian Redl539c5062010-08-18 23:57:32 +00002640 case TYPE_ENUM: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002641 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002642 Error("incorrect encoding of enum type");
2643 return QualType();
2644 }
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002645 bool IsDependent = Record[0];
2646 QualType T = Context->getEnumType(cast<EnumDecl>(GetDecl(Record[1])));
2647 T->Dependent = IsDependent;
2648 return T;
2649 }
Douglas Gregor1daeb692009-04-13 18:14:40 +00002650
Sebastian Redl539c5062010-08-18 23:57:32 +00002651 case TYPE_ELABORATED: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00002652 unsigned Idx = 0;
2653 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2654 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2655 QualType NamedType = GetType(Record[Idx++]);
2656 return Context->getElaboratedType(Keyword, NNS, NamedType);
John McCallfcc33b02009-09-05 00:15:47 +00002657 }
2658
Sebastian Redl539c5062010-08-18 23:57:32 +00002659 case TYPE_OBJC_INTERFACE: {
Chris Lattner587cbe12009-04-22 06:45:28 +00002660 unsigned Idx = 0;
2661 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
John McCall8b07ec22010-05-15 11:32:37 +00002662 return Context->getObjCInterfaceType(ItfD);
2663 }
2664
Sebastian Redl539c5062010-08-18 23:57:32 +00002665 case TYPE_OBJC_OBJECT: {
John McCall8b07ec22010-05-15 11:32:37 +00002666 unsigned Idx = 0;
2667 QualType Base = GetType(Record[Idx++]);
Chris Lattner587cbe12009-04-22 06:45:28 +00002668 unsigned NumProtos = Record[Idx++];
2669 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2670 for (unsigned I = 0; I != NumProtos; ++I)
2671 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
John McCall8b07ec22010-05-15 11:32:37 +00002672 return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattner587cbe12009-04-22 06:45:28 +00002673 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002674
Sebastian Redl539c5062010-08-18 23:57:32 +00002675 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattner6e054af2009-04-22 06:40:03 +00002676 unsigned Idx = 0;
John McCall8b07ec22010-05-15 11:32:37 +00002677 QualType Pointee = GetType(Record[Idx++]);
2678 return Context->getObjCObjectPointerType(Pointee);
Chris Lattner6e054af2009-04-22 06:40:03 +00002679 }
Argyrios Kyrtzidisa7a36df2009-09-29 19:42:55 +00002680
Sebastian Redl539c5062010-08-18 23:57:32 +00002681 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCallcebee162009-10-18 09:09:24 +00002682 unsigned Idx = 0;
2683 QualType Parm = GetType(Record[Idx++]);
2684 QualType Replacement = GetType(Record[Idx++]);
2685 return
2686 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
2687 Replacement);
2688 }
John McCalle78aac42010-03-10 03:28:59 +00002689
Sebastian Redl539c5062010-08-18 23:57:32 +00002690 case TYPE_INJECTED_CLASS_NAME: {
John McCalle78aac42010-03-10 03:28:59 +00002691 CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0]));
2692 QualType TST = GetType(Record[1]); // probably derivable
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00002693 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002694 // for AST reading, too much interdependencies.
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00002695 return
2696 QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCalle78aac42010-03-10 03:28:59 +00002697 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00002698
Sebastian Redl539c5062010-08-18 23:57:32 +00002699 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002700 unsigned Idx = 0;
2701 unsigned Depth = Record[Idx++];
2702 unsigned Index = Record[Idx++];
2703 bool Pack = Record[Idx++];
2704 IdentifierInfo *Name = GetIdentifierInfo(Record, Idx);
2705 return Context->getTemplateTypeParmType(Depth, Index, Pack, Name);
2706 }
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00002707
Sebastian Redl539c5062010-08-18 23:57:32 +00002708 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00002709 unsigned Idx = 0;
2710 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2711 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2712 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +00002713 QualType Canon = GetType(Record[Idx++]);
2714 return Context->getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00002715 }
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00002716
Sebastian Redl539c5062010-08-18 23:57:32 +00002717 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00002718 unsigned Idx = 0;
2719 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2720 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2721 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
2722 unsigned NumArgs = Record[Idx++];
2723 llvm::SmallVector<TemplateArgument, 8> Args;
2724 Args.reserve(NumArgs);
2725 while (NumArgs--)
Sebastian Redlc67764e2010-07-22 22:43:28 +00002726 Args.push_back(ReadTemplateArgument(DeclsCursor, Record, Idx));
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00002727 return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
2728 Args.size(), Args.data());
2729 }
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00002730
Sebastian Redl539c5062010-08-18 23:57:32 +00002731 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00002732 unsigned Idx = 0;
2733
2734 // ArrayType
2735 QualType ElementType = GetType(Record[Idx++]);
2736 ArrayType::ArraySizeModifier ASM
2737 = (ArrayType::ArraySizeModifier)Record[Idx++];
2738 unsigned IndexTypeQuals = Record[Idx++];
2739
2740 // DependentSizedArrayType
Sebastian Redlc67764e2010-07-22 22:43:28 +00002741 Expr *NumElts = ReadExpr(DeclsCursor);
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00002742 SourceRange Brackets = ReadSourceRange(Record, Idx);
2743
2744 return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
2745 IndexTypeQuals, Brackets);
2746 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00002747
Sebastian Redl539c5062010-08-18 23:57:32 +00002748 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002749 unsigned Idx = 0;
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002750 bool IsDependent = Record[Idx++];
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002751 TemplateName Name = ReadTemplateName(Record, Idx);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002752 llvm::SmallVector<TemplateArgument, 8> Args;
Sebastian Redlc67764e2010-07-22 22:43:28 +00002753 ReadTemplateArgumentList(Args, DeclsCursor, Record, Idx);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00002754 QualType Canon = GetType(Record[Idx++]);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002755 QualType T;
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002756 if (Canon.isNull())
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002757 T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(),
2758 Args.size());
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002759 else
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00002760 T = Context->getTemplateSpecializationType(Name, Args.data(),
2761 Args.size(), Canon);
2762 T->Dependent = IsDependent;
2763 return T;
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00002764 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002765 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002766 // Suppress a GCC warning
2767 return QualType();
2768}
2769
John McCall8f115c62009-10-16 21:56:05 +00002770namespace {
2771
2772class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redl2c499f62010-08-18 23:56:43 +00002773 ASTReader &Reader;
Sebastian Redlc67764e2010-07-22 22:43:28 +00002774 llvm::BitstreamCursor &DeclsCursor;
Sebastian Redl2c499f62010-08-18 23:56:43 +00002775 const ASTReader::RecordData &Record;
John McCall8f115c62009-10-16 21:56:05 +00002776 unsigned &Idx;
2777
2778public:
Sebastian Redl2c499f62010-08-18 23:56:43 +00002779 TypeLocReader(ASTReader &Reader, llvm::BitstreamCursor &Cursor,
2780 const ASTReader::RecordData &Record, unsigned &Idx)
Sebastian Redlc67764e2010-07-22 22:43:28 +00002781 : Reader(Reader), DeclsCursor(Cursor), Record(Record), Idx(Idx) { }
John McCall8f115c62009-10-16 21:56:05 +00002782
John McCall17001972009-10-18 01:05:36 +00002783 // We want compile-time assurance that we've enumerated all of
2784 // these, so unfortunately we have to declare them first, then
2785 // define them out-of-line.
2786#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +00002787#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +00002788 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +00002789#include "clang/AST/TypeLocNodes.def"
2790
John McCall17001972009-10-18 01:05:36 +00002791 void VisitFunctionTypeLoc(FunctionTypeLoc);
2792 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCall8f115c62009-10-16 21:56:05 +00002793};
2794
2795}
2796
John McCall17001972009-10-18 01:05:36 +00002797void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCall8f115c62009-10-16 21:56:05 +00002798 // nothing to do
2799}
John McCall17001972009-10-18 01:05:36 +00002800void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +00002801 TL.setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2802 if (TL.needsExtraLocalData()) {
2803 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
2804 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
2805 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
2806 TL.setModeAttr(Record[Idx++]);
2807 }
John McCall8f115c62009-10-16 21:56:05 +00002808}
John McCall17001972009-10-18 01:05:36 +00002809void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
2810 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall8f115c62009-10-16 21:56:05 +00002811}
John McCall17001972009-10-18 01:05:36 +00002812void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
2813 TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall8f115c62009-10-16 21:56:05 +00002814}
John McCall17001972009-10-18 01:05:36 +00002815void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
2816 TL.setCaretLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall8f115c62009-10-16 21:56:05 +00002817}
John McCall17001972009-10-18 01:05:36 +00002818void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
2819 TL.setAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall8f115c62009-10-16 21:56:05 +00002820}
John McCall17001972009-10-18 01:05:36 +00002821void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
2822 TL.setAmpAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall8f115c62009-10-16 21:56:05 +00002823}
John McCall17001972009-10-18 01:05:36 +00002824void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
2825 TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall8f115c62009-10-16 21:56:05 +00002826}
John McCall17001972009-10-18 01:05:36 +00002827void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
2828 TL.setLBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2829 TL.setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall8f115c62009-10-16 21:56:05 +00002830 if (Record[Idx++])
Sebastian Redlc67764e2010-07-22 22:43:28 +00002831 TL.setSizeExpr(Reader.ReadExpr(DeclsCursor));
Douglas Gregor12bfa382009-10-17 00:13:19 +00002832 else
John McCall17001972009-10-18 01:05:36 +00002833 TL.setSizeExpr(0);
2834}
2835void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
2836 VisitArrayTypeLoc(TL);
2837}
2838void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
2839 VisitArrayTypeLoc(TL);
2840}
2841void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
2842 VisitArrayTypeLoc(TL);
2843}
2844void TypeLocReader::VisitDependentSizedArrayTypeLoc(
2845 DependentSizedArrayTypeLoc TL) {
2846 VisitArrayTypeLoc(TL);
2847}
2848void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
2849 DependentSizedExtVectorTypeLoc TL) {
2850 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2851}
2852void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
2853 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2854}
2855void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
2856 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2857}
2858void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
2859 TL.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2860 TL.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2861 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
John McCalle6347002009-10-23 01:28:53 +00002862 TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
John McCall17001972009-10-18 01:05:36 +00002863 }
2864}
2865void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
2866 VisitFunctionTypeLoc(TL);
2867}
2868void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
2869 VisitFunctionTypeLoc(TL);
2870}
John McCallb96ec562009-12-04 22:46:56 +00002871void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
2872 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2873}
John McCall17001972009-10-18 01:05:36 +00002874void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
2875 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2876}
2877void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +00002878 TL.setTypeofLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2879 TL.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2880 TL.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall17001972009-10-18 01:05:36 +00002881}
2882void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +00002883 TL.setTypeofLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2884 TL.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2885 TL.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Sebastian Redlc67764e2010-07-22 22:43:28 +00002886 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(DeclsCursor, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00002887}
2888void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
2889 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2890}
2891void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
2892 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2893}
2894void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
2895 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2896}
John McCall17001972009-10-18 01:05:36 +00002897void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
2898 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2899}
John McCallcebee162009-10-18 09:09:24 +00002900void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
2901 SubstTemplateTypeParmTypeLoc TL) {
2902 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2903}
John McCall17001972009-10-18 01:05:36 +00002904void TypeLocReader::VisitTemplateSpecializationTypeLoc(
2905 TemplateSpecializationTypeLoc TL) {
John McCall0ad16662009-10-29 08:12:44 +00002906 TL.setTemplateNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2907 TL.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2908 TL.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2909 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
2910 TL.setArgLocInfo(i,
2911 Reader.GetTemplateArgumentLocInfo(TL.getTypePtr()->getArg(i).getKind(),
Sebastian Redlc67764e2010-07-22 22:43:28 +00002912 DeclsCursor, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00002913}
Abramo Bagnara6150c882010-05-11 21:36:43 +00002914void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +00002915 TL.setKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2916 TL.setQualifierRange(Reader.ReadSourceRange(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00002917}
John McCalle78aac42010-03-10 03:28:59 +00002918void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
2919 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2920}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00002921void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +00002922 TL.setKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2923 TL.setQualifierRange(Reader.ReadSourceRange(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00002924 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2925}
John McCallc392f372010-06-11 00:33:02 +00002926void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
2927 DependentTemplateSpecializationTypeLoc TL) {
2928 TL.setKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2929 TL.setQualifierRange(Reader.ReadSourceRange(Record, Idx));
2930 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2931 TL.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2932 TL.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2933 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
2934 TL.setArgLocInfo(I,
2935 Reader.GetTemplateArgumentLocInfo(TL.getTypePtr()->getArg(I).getKind(),
Sebastian Redlc67764e2010-07-22 22:43:28 +00002936 DeclsCursor, Record, Idx));
John McCallc392f372010-06-11 00:33:02 +00002937}
John McCall17001972009-10-18 01:05:36 +00002938void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
2939 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall8b07ec22010-05-15 11:32:37 +00002940}
2941void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
2942 TL.setHasBaseTypeAsWritten(Record[Idx++]);
John McCall17001972009-10-18 01:05:36 +00002943 TL.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2944 TL.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2945 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
2946 TL.setProtocolLoc(i, SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall8f115c62009-10-16 21:56:05 +00002947}
John McCallfc93cf92009-10-22 22:37:11 +00002948void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
2949 TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCallfc93cf92009-10-22 22:37:11 +00002950}
John McCall8f115c62009-10-16 21:56:05 +00002951
Sebastian Redl2c499f62010-08-18 23:56:43 +00002952TypeSourceInfo *ASTReader::GetTypeSourceInfo(llvm::BitstreamCursor &DeclsCursor,
Sebastian Redlc67764e2010-07-22 22:43:28 +00002953 const RecordData &Record,
John McCall8f115c62009-10-16 21:56:05 +00002954 unsigned &Idx) {
2955 QualType InfoTy = GetType(Record[Idx++]);
2956 if (InfoTy.isNull())
2957 return 0;
2958
John McCallbcd03502009-12-07 02:54:59 +00002959 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
Sebastian Redlc67764e2010-07-22 22:43:28 +00002960 TypeLocReader TLR(*this, DeclsCursor, Record, Idx);
John McCallbcd03502009-12-07 02:54:59 +00002961 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCall8f115c62009-10-16 21:56:05 +00002962 TLR.Visit(TL);
John McCallbcd03502009-12-07 02:54:59 +00002963 return TInfo;
John McCall8f115c62009-10-16 21:56:05 +00002964}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002965
Sebastian Redl539c5062010-08-18 23:57:32 +00002966QualType ASTReader::GetType(TypeID ID) {
John McCall8ccfcb52009-09-24 19:53:00 +00002967 unsigned FastQuals = ID & Qualifiers::FastMask;
2968 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002969
Sebastian Redl539c5062010-08-18 23:57:32 +00002970 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002971 QualType T;
Sebastian Redl539c5062010-08-18 23:57:32 +00002972 switch ((PredefinedTypeIDs)Index) {
2973 case PREDEF_TYPE_NULL_ID: return QualType();
2974 case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
2975 case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002976
Sebastian Redl539c5062010-08-18 23:57:32 +00002977 case PREDEF_TYPE_CHAR_U_ID:
2978 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002979 // FIXME: Check that the signedness of CharTy is correct!
Chris Lattner8575daa2009-04-27 21:45:14 +00002980 T = Context->CharTy;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002981 break;
2982
Sebastian Redl539c5062010-08-18 23:57:32 +00002983 case PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break;
2984 case PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break;
2985 case PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break;
2986 case PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break;
2987 case PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break;
2988 case PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break;
2989 case PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break;
2990 case PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break;
2991 case PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break;
2992 case PREDEF_TYPE_INT_ID: T = Context->IntTy; break;
2993 case PREDEF_TYPE_LONG_ID: T = Context->LongTy; break;
2994 case PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break;
2995 case PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break;
2996 case PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break;
2997 case PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break;
2998 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break;
2999 case PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break;
3000 case PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break;
3001 case PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break;
3002 case PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break;
3003 case PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break;
3004 case PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break;
3005 case PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break;
3006 case PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003007 }
3008
3009 assert(!T.isNull() && "Unknown predefined type");
John McCall8ccfcb52009-09-24 19:53:00 +00003010 return T.withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003011 }
3012
Sebastian Redl539c5062010-08-18 23:57:32 +00003013 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003014 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl409183f2010-07-14 20:26:45 +00003015 if (TypesLoaded[Index].isNull()) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003016 TypesLoaded[Index] = ReadTypeRecord(Index);
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003017 TypesLoaded[Index]->setFromAST();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003018 TypeIdxs[TypesLoaded[Index]] = TypeIdx::fromTypeID(ID);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003019 if (DeserializationListener)
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003020 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003021 TypesLoaded[Index]);
Sebastian Redl409183f2010-07-14 20:26:45 +00003022 }
Mike Stump11289f42009-09-09 15:08:12 +00003023
John McCall8ccfcb52009-09-24 19:53:00 +00003024 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003025}
3026
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003027TypeID ASTReader::GetTypeID(QualType T) const {
3028 return MakeTypeID(T,
3029 std::bind1st(std::mem_fun(&ASTReader::GetTypeIdx), this));
3030}
3031
3032TypeIdx ASTReader::GetTypeIdx(QualType T) const {
3033 if (T.isNull())
3034 return TypeIdx();
3035 assert(!T.getLocalFastQualifiers());
3036
3037 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3038 // GetTypeIdx is mostly used for computing the hash of DeclarationNames and
3039 // comparing keys of ASTDeclContextNameLookupTable.
3040 // If the type didn't come from the AST file use a specially marked index
3041 // so that any hash/key comparison fail since no such index is stored
3042 // in a AST file.
3043 if (I == TypeIdxs.end())
3044 return TypeIdx(-1);
3045 return I->second;
3046}
3047
John McCall0ad16662009-10-29 08:12:44 +00003048TemplateArgumentLocInfo
Sebastian Redl2c499f62010-08-18 23:56:43 +00003049ASTReader::GetTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003050 llvm::BitstreamCursor &DeclsCursor,
John McCall0ad16662009-10-29 08:12:44 +00003051 const RecordData &Record,
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003052 unsigned &Index) {
John McCall0ad16662009-10-29 08:12:44 +00003053 switch (Kind) {
3054 case TemplateArgument::Expression:
Sebastian Redlc67764e2010-07-22 22:43:28 +00003055 return ReadExpr(DeclsCursor);
John McCall0ad16662009-10-29 08:12:44 +00003056 case TemplateArgument::Type:
Sebastian Redlc67764e2010-07-22 22:43:28 +00003057 return GetTypeSourceInfo(DeclsCursor, Record, Index);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003058 case TemplateArgument::Template: {
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003059 SourceRange QualifierRange = ReadSourceRange(Record, Index);
3060 SourceLocation TemplateNameLoc = ReadSourceLocation(Record, Index);
3061 return TemplateArgumentLocInfo(QualifierRange, TemplateNameLoc);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003062 }
John McCall0ad16662009-10-29 08:12:44 +00003063 case TemplateArgument::Null:
3064 case TemplateArgument::Integral:
3065 case TemplateArgument::Declaration:
3066 case TemplateArgument::Pack:
3067 return TemplateArgumentLocInfo();
3068 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +00003069 llvm_unreachable("unexpected template argument loc");
John McCall0ad16662009-10-29 08:12:44 +00003070 return TemplateArgumentLocInfo();
3071}
3072
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003073TemplateArgumentLoc
Sebastian Redl2c499f62010-08-18 23:56:43 +00003074ASTReader::ReadTemplateArgumentLoc(llvm::BitstreamCursor &DeclsCursor,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003075 const RecordData &Record, unsigned &Index) {
3076 TemplateArgument Arg = ReadTemplateArgument(DeclsCursor, Record, Index);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003077
3078 if (Arg.getKind() == TemplateArgument::Expression) {
3079 if (Record[Index++]) // bool InfoHasSameExpr.
3080 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
3081 }
3082 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(Arg.getKind(),
Sebastian Redlc67764e2010-07-22 22:43:28 +00003083 DeclsCursor,
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003084 Record, Index));
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003085}
3086
Sebastian Redl2c499f62010-08-18 23:56:43 +00003087Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall75b960e2010-06-01 09:23:16 +00003088 return GetDecl(ID);
3089}
3090
Sebastian Redl2c499f62010-08-18 23:56:43 +00003091TranslationUnitDecl *ASTReader::GetTranslationUnitDecl() {
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003092 if (!DeclsLoaded[0]) {
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003093 ReadDeclRecord(0, 0);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003094 if (DeserializationListener)
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003095 DeserializationListener->DeclRead(1, DeclsLoaded[0]);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003096 }
Argyrios Kyrtzidis7e8996c2010-07-08 17:13:02 +00003097
3098 return cast<TranslationUnitDecl>(DeclsLoaded[0]);
3099}
3100
Sebastian Redl539c5062010-08-18 23:57:32 +00003101Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003102 if (ID == 0)
3103 return 0;
3104
Douglas Gregor745ed142009-04-25 18:35:21 +00003105 if (ID > DeclsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003106 Error("declaration ID out-of-range for AST file");
Douglas Gregor745ed142009-04-25 18:35:21 +00003107 return 0;
3108 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003109
Douglas Gregor745ed142009-04-25 18:35:21 +00003110 unsigned Index = ID - 1;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003111 if (!DeclsLoaded[Index]) {
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003112 ReadDeclRecord(Index, ID);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003113 if (DeserializationListener)
3114 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
3115 }
Douglas Gregor745ed142009-04-25 18:35:21 +00003116
3117 return DeclsLoaded[Index];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003118}
3119
Chris Lattner9c28af02009-04-27 05:46:25 +00003120/// \brief Resolve the offset of a statement into a statement.
3121///
3122/// This operation will read a new statement from the external
3123/// source each time it is called, and is meant to be used via a
3124/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redl2c499f62010-08-18 23:56:43 +00003125Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Sebastian Redl5c415f32010-07-22 17:01:13 +00003126 // Offset here is a global offset across the entire chain.
3127 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3128 PerFileData &F = *Chain[N - I - 1];
3129 if (Offset < F.SizeInBits) {
3130 // Since we know that this statement is part of a decl, make sure to use
3131 // the decl cursor to read it.
3132 F.DeclsCursor.JumpToBit(Offset);
3133 return ReadStmtFromStream(F.DeclsCursor);
3134 }
3135 Offset -= F.SizeInBits;
3136 }
3137 llvm_unreachable("Broken chain");
Douglas Gregor3c3aa612009-04-18 00:07:54 +00003138}
3139
Sebastian Redl2c499f62010-08-18 23:56:43 +00003140bool ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
John McCall75b960e2010-06-01 09:23:16 +00003141 llvm::SmallVectorImpl<Decl*> &Decls) {
Mike Stump11289f42009-09-09 15:08:12 +00003142 assert(DC->hasExternalLexicalStorage() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003143 "DeclContext has no lexical decls in storage");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003144
Sebastian Redl5c415f32010-07-22 17:01:13 +00003145 // There might be lexical decls in multiple parts of the chain, for the TU
3146 // at least.
3147 DeclContextInfos &Infos = DeclContextOffsets[DC];
3148 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3149 I != E; ++I) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003150 // IDs can be 0 if this context doesn't contain declarations.
3151 if (!I->LexicalDecls)
Sebastian Redl5c415f32010-07-22 17:01:13 +00003152 continue;
Sebastian Redl5c415f32010-07-22 17:01:13 +00003153
3154 // Load all of the declaration IDs
Sebastian Redl539c5062010-08-18 23:57:32 +00003155 for (const DeclID *ID = I->LexicalDecls,
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003156 *IDE = ID + I->NumLexicalDecls;
3157 ID != IDE; ++ID)
3158 Decls.push_back(GetDecl(*ID));
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003159 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003160
Douglas Gregora57c3ab2009-04-22 22:34:57 +00003161 ++NumLexicalDeclContextsRead;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003162 return false;
3163}
3164
John McCall75b960e2010-06-01 09:23:16 +00003165DeclContext::lookup_result
Sebastian Redl2c499f62010-08-18 23:56:43 +00003166ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall75b960e2010-06-01 09:23:16 +00003167 DeclarationName Name) {
Mike Stump11289f42009-09-09 15:08:12 +00003168 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003169 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003170 if (!Name)
3171 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
3172 DeclContext::lookup_iterator(0));
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003173
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003174 llvm::SmallVector<NamedDecl *, 64> Decls;
Sebastian Redl5c415f32010-07-22 17:01:13 +00003175 // There might be lexical decls in multiple parts of the chain, for the TU
3176 // and namespaces.
3177 DeclContextInfos &Infos = DeclContextOffsets[DC];
3178 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3179 I != E; ++I) {
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003180 if (!I->NameLookupTableData)
Sebastian Redl5c415f32010-07-22 17:01:13 +00003181 continue;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003182
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003183 ASTDeclContextNameLookupTable *LookupTable =
3184 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3185 ASTDeclContextNameLookupTable::iterator Pos = LookupTable->find(Name);
3186 if (Pos == LookupTable->end())
Sebastian Redl5c415f32010-07-22 17:01:13 +00003187 continue;
3188
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003189 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
3190 for (; Data.first != Data.second; ++Data.first)
3191 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003192 }
3193
Douglas Gregora57c3ab2009-04-22 22:34:57 +00003194 ++NumVisibleDeclContextsRead;
John McCall75b960e2010-06-01 09:23:16 +00003195
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003196 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall75b960e2010-06-01 09:23:16 +00003197 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003198}
3199
Sebastian Redl2c499f62010-08-18 23:56:43 +00003200void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003201 assert(Consumer);
3202 while (!InterestingDecls.empty()) {
3203 DeclGroupRef DG(InterestingDecls.front());
3204 InterestingDecls.pop_front();
Sebastian Redleaa4ade2010-08-11 18:52:41 +00003205 Consumer->HandleInterestingDecl(DG);
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003206 }
3207}
3208
Sebastian Redl2c499f62010-08-18 23:56:43 +00003209void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregorb985eeb2009-04-22 19:09:20 +00003210 this->Consumer = Consumer;
3211
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003212 if (!Consumer)
3213 return;
3214
3215 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003216 // Force deserialization of this decl, which will cause it to be queued for
3217 // passing to the consumer.
Daniel Dunbar865c2a72009-09-17 03:06:44 +00003218 GetDecl(ExternalDefinitions[I]);
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003219 }
Douglas Gregorf005eac2009-04-25 00:41:30 +00003220
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003221 PassInterestingDeclsToConsumer();
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003222}
3223
Sebastian Redl2c499f62010-08-18 23:56:43 +00003224void ASTReader::PrintStats() {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003225 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003226
Mike Stump11289f42009-09-09 15:08:12 +00003227 unsigned NumTypesLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00003228 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall8ccfcb52009-09-24 19:53:00 +00003229 QualType());
Douglas Gregor0e149972009-04-25 19:10:14 +00003230 unsigned NumDeclsLoaded
3231 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
3232 (Decl *)0);
3233 unsigned NumIdentifiersLoaded
3234 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
3235 IdentifiersLoaded.end(),
3236 (IdentifierInfo *)0);
Mike Stump11289f42009-09-09 15:08:12 +00003237 unsigned NumSelectorsLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00003238 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
3239 SelectorsLoaded.end(),
3240 Selector());
Douglas Gregorc3b1dd12009-04-13 20:50:16 +00003241
Douglas Gregorc5046832009-04-27 18:38:38 +00003242 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
3243 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor258ae542009-04-27 06:38:32 +00003244 if (TotalNumSLocEntries)
3245 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
3246 NumSLocEntriesRead, TotalNumSLocEntries,
3247 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor745ed142009-04-25 18:35:21 +00003248 if (!TypesLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003249 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00003250 NumTypesLoaded, (unsigned)TypesLoaded.size(),
3251 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
3252 if (!DeclsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003253 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00003254 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
3255 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor0e149972009-04-25 19:10:14 +00003256 if (!IdentifiersLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003257 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor0e149972009-04-25 19:10:14 +00003258 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
3259 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redlada023c2010-08-04 20:40:17 +00003260 if (!SelectorsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003261 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redlada023c2010-08-04 20:40:17 +00003262 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
3263 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003264 if (TotalNumStatements)
3265 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
3266 NumStatementsRead, TotalNumStatements,
3267 ((float)NumStatementsRead/TotalNumStatements * 100));
3268 if (TotalNumMacros)
3269 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
3270 NumMacrosRead, TotalNumMacros,
3271 ((float)NumMacrosRead/TotalNumMacros * 100));
3272 if (TotalLexicalDeclContexts)
3273 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
3274 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
3275 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
3276 * 100));
3277 if (TotalVisibleDeclContexts)
3278 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
3279 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
3280 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
3281 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003282 if (TotalNumMethodPoolEntries) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00003283 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003284 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
3285 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor95c13f52009-04-25 17:48:32 +00003286 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003287 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor95c13f52009-04-25 17:48:32 +00003288 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003289 std::fprintf(stderr, "\n");
3290}
3291
Sebastian Redl2c499f62010-08-18 23:56:43 +00003292void ASTReader::InitializeSema(Sema &S) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00003293 SemaObj = &S;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003294 S.ExternalSource = this;
3295
Douglas Gregor7cd60f72009-04-22 21:15:06 +00003296 // Makes sure any declarations that were deserialized "too early"
3297 // still get added to the identifier's declaration chains.
Douglas Gregor6fd55e02010-08-13 03:15:25 +00003298 if (SemaObj->TUScope) {
3299 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
3300 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(PreloadedDecls[I]));
3301 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
3302 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00003303 }
Douglas Gregor7cd60f72009-04-22 21:15:06 +00003304 PreloadedDecls.clear();
Douglas Gregord4df8652009-04-22 22:02:47 +00003305
3306 // If there were any tentative definitions, deserialize them and add
Sebastian Redl35351a92010-01-31 22:27:38 +00003307 // them to Sema's list of tentative definitions.
Douglas Gregord4df8652009-04-22 22:02:47 +00003308 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
3309 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
Sebastian Redl35351a92010-01-31 22:27:38 +00003310 SemaObj->TentativeDefinitions.push_back(Var);
Douglas Gregord4df8652009-04-22 22:02:47 +00003311 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00003312
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003313 // If there were any unused file scoped decls, deserialize them and add to
3314 // Sema's list of unused file scoped decls.
3315 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
3316 DeclaratorDecl *D = cast<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
3317 SemaObj->UnusedFileScopedDecls.push_back(D);
Tanya Lattner90073802010-02-12 00:07:30 +00003318 }
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003319
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003320 // If there were any weak undeclared identifiers, deserialize them and add to
3321 // Sema's list of weak undeclared identifiers.
3322 if (!WeakUndeclaredIdentifiers.empty()) {
3323 unsigned Idx = 0;
3324 for (unsigned I = 0, N = WeakUndeclaredIdentifiers[Idx++]; I != N; ++I) {
3325 IdentifierInfo *WeakId = GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3326 IdentifierInfo *AliasId=GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3327 SourceLocation Loc = ReadSourceLocation(WeakUndeclaredIdentifiers, Idx);
3328 bool Used = WeakUndeclaredIdentifiers[Idx++];
3329 Sema::WeakInfo WI(AliasId, Loc);
3330 WI.setUsed(Used);
3331 SemaObj->WeakUndeclaredIdentifiers.insert(std::make_pair(WeakId, WI));
3332 }
3333 }
3334
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003335 // If there were any locally-scoped external declarations,
3336 // deserialize them and add them to Sema's table of locally-scoped
3337 // external declarations.
3338 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
3339 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
3340 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
3341 }
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003342
3343 // If there were any ext_vector type declarations, deserialize them
3344 // and add them to Sema's vector of such declarations.
3345 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I)
3346 SemaObj->ExtVectorDecls.push_back(
3347 cast<TypedefDecl>(GetDecl(ExtVectorDecls[I])));
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003348
3349 // FIXME: Do VTable uses and dynamic classes deserialize too much ?
3350 // Can we cut them down before writing them ?
3351
3352 // If there were any VTable uses, deserialize the information and add it
3353 // to Sema's vector and map of VTable uses.
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00003354 if (!VTableUses.empty()) {
3355 unsigned Idx = 0;
3356 for (unsigned I = 0, N = VTableUses[Idx++]; I != N; ++I) {
3357 CXXRecordDecl *Class = cast<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
3358 SourceLocation Loc = ReadSourceLocation(VTableUses, Idx);
3359 bool DefinitionRequired = VTableUses[Idx++];
3360 SemaObj->VTableUses.push_back(std::make_pair(Class, Loc));
3361 SemaObj->VTablesUsed[Class] = DefinitionRequired;
3362 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003363 }
3364
3365 // If there were any dynamic classes declarations, deserialize them
3366 // and add them to Sema's vector of such declarations.
3367 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I)
3368 SemaObj->DynamicClasses.push_back(
3369 cast<CXXRecordDecl>(GetDecl(DynamicClasses[I])));
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003370
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003371 // If there were any pending implicit instantiations, deserialize them
3372 // and add them to Sema's queue of such instantiations.
3373 assert(PendingImplicitInstantiations.size() % 2 == 0 &&
3374 "Expected pairs of entries");
3375 for (unsigned Idx = 0, N = PendingImplicitInstantiations.size(); Idx < N;) {
3376 ValueDecl *D=cast<ValueDecl>(GetDecl(PendingImplicitInstantiations[Idx++]));
3377 SourceLocation Loc = ReadSourceLocation(PendingImplicitInstantiations, Idx);
3378 SemaObj->PendingImplicitInstantiations.push_back(std::make_pair(D, Loc));
3379 }
3380
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00003381 // Load the offsets of the declarations that Sema references.
3382 // They will be lazily deserialized when needed.
3383 if (!SemaDeclRefs.empty()) {
3384 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
3385 SemaObj->StdNamespace = SemaDeclRefs[0];
3386 SemaObj->StdBadAlloc = SemaDeclRefs[1];
3387 }
3388
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003389 // If there are @selector references added them to its pool. This is for
3390 // implementation of -Wselector.
Sebastian Redlada023c2010-08-04 20:40:17 +00003391 if (!ReferencedSelectorsData.empty()) {
3392 unsigned int DataSize = ReferencedSelectorsData.size()-1;
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003393 unsigned I = 0;
3394 while (I < DataSize) {
Sebastian Redlada023c2010-08-04 20:40:17 +00003395 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003396 SourceLocation SelLoc =
Sebastian Redlada023c2010-08-04 20:40:17 +00003397 SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003398 SemaObj->ReferencedSelectors.insert(std::make_pair(Sel, SelLoc));
3399 }
3400 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00003401}
3402
Sebastian Redl2c499f62010-08-18 23:56:43 +00003403IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Sebastian Redl78f51772010-08-02 18:30:12 +00003404 // Try to find this name within our on-disk hash tables. We start with the
3405 // most recent one, since that one contains the most up-to-date info.
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003406 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003407 ASTIdentifierLookupTable *IdTable
3408 = (ASTIdentifierLookupTable *)Chain[I]->IdentifierLookupTable;
Sebastian Redl5c415f32010-07-22 17:01:13 +00003409 if (!IdTable)
3410 continue;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003411 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003412 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003413 if (Pos == IdTable->end())
3414 continue;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003415
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003416 // Dereferencing the iterator has the effect of building the
3417 // IdentifierInfo node and populating it with the various
3418 // declarations it needs.
Sebastian Redl78f51772010-08-02 18:30:12 +00003419 return *Pos;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00003420 }
Sebastian Redl78f51772010-08-02 18:30:12 +00003421 return 0;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003422}
3423
Mike Stump11289f42009-09-09 15:08:12 +00003424std::pair<ObjCMethodList, ObjCMethodList>
Sebastian Redl2c499f62010-08-18 23:56:43 +00003425ASTReader::ReadMethodPool(Selector Sel) {
Sebastian Redlada023c2010-08-04 20:40:17 +00003426 // Find this selector in a hash table. We want to find the most recent entry.
3427 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3428 PerFileData &F = *Chain[I];
3429 if (!F.SelectorLookupTable)
3430 continue;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003431
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003432 ASTSelectorLookupTable *PoolTable
3433 = (ASTSelectorLookupTable*)F.SelectorLookupTable;
3434 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
Sebastian Redlada023c2010-08-04 20:40:17 +00003435 if (Pos != PoolTable->end()) {
3436 ++NumSelectorsRead;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003437 // FIXME: Not quite happy with the statistics here. We probably should
3438 // disable this tracking when called via LoadSelector.
3439 // Also, should entries without methods count as misses?
3440 ++NumMethodPoolEntriesRead;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003441 ASTSelectorLookupTrait::data_type Data = *Pos;
Sebastian Redlada023c2010-08-04 20:40:17 +00003442 if (DeserializationListener)
3443 DeserializationListener->SelectorRead(Data.ID, Sel);
3444 return std::make_pair(Data.Instance, Data.Factory);
3445 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00003446 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00003447
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003448 ++NumMethodPoolMisses;
Sebastian Redlada023c2010-08-04 20:40:17 +00003449 return std::pair<ObjCMethodList, ObjCMethodList>();
Douglas Gregorc78d3462009-04-24 21:10:55 +00003450}
3451
Sebastian Redl2c499f62010-08-18 23:56:43 +00003452void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003453 // It would be complicated to avoid reading the methods anyway. So don't.
3454 ReadMethodPool(Sel);
3455}
3456
Sebastian Redl2c499f62010-08-18 23:56:43 +00003457void ASTReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00003458 assert(ID && "Non-zero identifier ID required");
Douglas Gregor6f00bf82009-04-28 21:53:25 +00003459 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor0e149972009-04-25 19:10:14 +00003460 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlff4a2952010-07-23 23:49:55 +00003461 if (DeserializationListener)
3462 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003463}
3464
Douglas Gregor1342e842009-07-06 18:54:52 +00003465/// \brief Set the globally-visible declarations associated with the given
3466/// identifier.
3467///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003468/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump11289f42009-09-09 15:08:12 +00003469/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregor1342e842009-07-06 18:54:52 +00003470/// them.
3471///
3472/// \param II an IdentifierInfo that refers to one or more globally-visible
3473/// declarations.
3474///
3475/// \param DeclIDs the set of declaration IDs with the name @p II that are
3476/// visible at global scope.
3477///
3478/// \param Nonrecursive should be true to indicate that the caller knows that
3479/// this call is non-recursive, and therefore the globally-visible declarations
3480/// will not be placed onto the pending queue.
Mike Stump11289f42009-09-09 15:08:12 +00003481void
Sebastian Redl2c499f62010-08-18 23:56:43 +00003482ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Douglas Gregor1342e842009-07-06 18:54:52 +00003483 const llvm::SmallVectorImpl<uint32_t> &DeclIDs,
3484 bool Nonrecursive) {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00003485 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregor1342e842009-07-06 18:54:52 +00003486 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
3487 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
3488 PII.II = II;
3489 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I)
3490 PII.DeclIDs.push_back(DeclIDs[I]);
3491 return;
3492 }
Mike Stump11289f42009-09-09 15:08:12 +00003493
Douglas Gregor1342e842009-07-06 18:54:52 +00003494 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
3495 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
3496 if (SemaObj) {
Douglas Gregor6fd55e02010-08-13 03:15:25 +00003497 if (SemaObj->TUScope) {
3498 // Introduce this declaration into the translation-unit scope
3499 // and add it to the declaration chain for this identifier, so
3500 // that (unqualified) name lookup will find it.
3501 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(D));
3502 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
3503 }
Douglas Gregor1342e842009-07-06 18:54:52 +00003504 } else {
3505 // Queue this declaration so that it will be added to the
3506 // translation unit scope and identifier's declaration chain
3507 // once a Sema object is known.
3508 PreloadedDecls.push_back(D);
3509 }
3510 }
3511}
3512
Sebastian Redl2c499f62010-08-18 23:56:43 +00003513IdentifierInfo *ASTReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003514 if (ID == 0)
3515 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00003516
Sebastian Redlc713b962010-07-21 00:46:22 +00003517 if (IdentifiersLoaded.empty()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003518 Error("no identifier table in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003519 return 0;
3520 }
Mike Stump11289f42009-09-09 15:08:12 +00003521
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003522 assert(PP && "Forgot to set Preprocessor ?");
Sebastian Redlc713b962010-07-21 00:46:22 +00003523 ID -= 1;
3524 if (!IdentifiersLoaded[ID]) {
3525 unsigned Index = ID;
3526 const char *Str = 0;
3527 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3528 PerFileData *F = Chain[N - I - 1];
3529 if (Index < F->LocalNumIdentifiers) {
3530 uint32_t Offset = F->IdentifierOffsets[Index];
3531 Str = F->IdentifierTableData + Offset;
3532 break;
3533 }
3534 Index -= F->LocalNumIdentifiers;
3535 }
3536 assert(Str && "Broken Chain");
Douglas Gregor5287b4e2009-04-25 21:04:17 +00003537
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003538 // All of the strings in the AST file are preceded by a 16-bit length.
3539 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenekca42a512009-10-23 04:45:31 +00003540 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
3541 // unsigned integers. This is important to avoid integer overflow when
3542 // we cast them to 'unsigned'.
Ted Kremenek49c52322009-10-23 03:57:22 +00003543 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregorab4df582009-04-28 20:01:51 +00003544 unsigned StrLen = (((unsigned) StrLenPtr[0])
3545 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redlc713b962010-07-21 00:46:22 +00003546 IdentifiersLoaded[ID]
Kovarththanan Rajaratnama3b09592010-03-12 10:32:27 +00003547 = &PP->getIdentifierTable().get(Str, StrLen);
Sebastian Redlff4a2952010-07-23 23:49:55 +00003548 if (DeserializationListener)
3549 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003550 }
Mike Stump11289f42009-09-09 15:08:12 +00003551
Sebastian Redlc713b962010-07-21 00:46:22 +00003552 return IdentifiersLoaded[ID];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003553}
3554
Sebastian Redl2c499f62010-08-18 23:56:43 +00003555void ASTReader::ReadSLocEntry(unsigned ID) {
Douglas Gregor258ae542009-04-27 06:38:32 +00003556 ReadSLocEntryRecord(ID);
3557}
3558
Sebastian Redl2c499f62010-08-18 23:56:43 +00003559Selector ASTReader::DecodeSelector(unsigned ID) {
Steve Naroff2ddea052009-04-23 10:39:46 +00003560 if (ID == 0)
3561 return Selector();
Mike Stump11289f42009-09-09 15:08:12 +00003562
Sebastian Redlada023c2010-08-04 20:40:17 +00003563 if (ID > SelectorsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003564 Error("selector ID out of range in AST file");
Steve Naroff2ddea052009-04-23 10:39:46 +00003565 return Selector();
3566 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00003567
Sebastian Redlada023c2010-08-04 20:40:17 +00003568 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00003569 // Load this selector from the selector table.
Sebastian Redlada023c2010-08-04 20:40:17 +00003570 unsigned Idx = ID - 1;
3571 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3572 PerFileData &F = *Chain[N - I - 1];
3573 if (Idx < F.LocalNumSelectors) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003574 ASTSelectorLookupTrait Trait(*this);
Sebastian Redlada023c2010-08-04 20:40:17 +00003575 SelectorsLoaded[ID - 1] =
3576 Trait.ReadKey(F.SelectorLookupTableData + F.SelectorOffsets[Idx], 0);
3577 if (DeserializationListener)
3578 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
3579 break;
3580 }
3581 Idx -= F.LocalNumSelectors;
3582 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00003583 }
3584
Sebastian Redlada023c2010-08-04 20:40:17 +00003585 return SelectorsLoaded[ID - 1];
Steve Naroff2ddea052009-04-23 10:39:46 +00003586}
3587
Sebastian Redl2c499f62010-08-18 23:56:43 +00003588Selector ASTReader::GetExternalSelector(uint32_t ID) {
Douglas Gregord720daf2010-04-06 17:30:22 +00003589 return DecodeSelector(ID);
3590}
3591
Sebastian Redl2c499f62010-08-18 23:56:43 +00003592uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redlada023c2010-08-04 20:40:17 +00003593 // ID 0 (the null selector) is considered an external selector.
3594 return getTotalNumSelectors() + 1;
Douglas Gregord720daf2010-04-06 17:30:22 +00003595}
3596
Mike Stump11289f42009-09-09 15:08:12 +00003597DeclarationName
Sebastian Redl2c499f62010-08-18 23:56:43 +00003598ASTReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003599 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
3600 switch (Kind) {
3601 case DeclarationName::Identifier:
3602 return DeclarationName(GetIdentifierInfo(Record, Idx));
3603
3604 case DeclarationName::ObjCZeroArgSelector:
3605 case DeclarationName::ObjCOneArgSelector:
3606 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff3c301dc2009-04-23 15:15:40 +00003607 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003608
3609 case DeclarationName::CXXConstructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00003610 return Context->DeclarationNames.getCXXConstructorName(
Douglas Gregor2211d342009-08-05 05:36:45 +00003611 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003612
3613 case DeclarationName::CXXDestructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00003614 return Context->DeclarationNames.getCXXDestructorName(
Douglas Gregor2211d342009-08-05 05:36:45 +00003615 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003616
3617 case DeclarationName::CXXConversionFunctionName:
Chris Lattner8575daa2009-04-27 21:45:14 +00003618 return Context->DeclarationNames.getCXXConversionFunctionName(
Douglas Gregor2211d342009-08-05 05:36:45 +00003619 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003620
3621 case DeclarationName::CXXOperatorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00003622 return Context->DeclarationNames.getCXXOperatorName(
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003623 (OverloadedOperatorKind)Record[Idx++]);
3624
Alexis Hunt3d221f22009-11-29 07:34:05 +00003625 case DeclarationName::CXXLiteralOperatorName:
3626 return Context->DeclarationNames.getCXXLiteralOperatorName(
3627 GetIdentifierInfo(Record, Idx));
3628
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003629 case DeclarationName::CXXUsingDirective:
3630 return DeclarationName::getUsingDirectiveName();
3631 }
3632
3633 // Required to silence GCC warning
3634 return DeclarationName();
3635}
Douglas Gregor55abb232009-04-10 20:39:37 +00003636
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003637TemplateName
Sebastian Redl2c499f62010-08-18 23:56:43 +00003638ASTReader::ReadTemplateName(const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003639 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
3640 switch (Kind) {
3641 case TemplateName::Template:
3642 return TemplateName(cast_or_null<TemplateDecl>(GetDecl(Record[Idx++])));
3643
3644 case TemplateName::OverloadedTemplate: {
3645 unsigned size = Record[Idx++];
3646 UnresolvedSet<8> Decls;
3647 while (size--)
3648 Decls.addDecl(cast<NamedDecl>(GetDecl(Record[Idx++])));
3649
3650 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
3651 }
3652
3653 case TemplateName::QualifiedTemplate: {
3654 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3655 bool hasTemplKeyword = Record[Idx++];
3656 TemplateDecl *Template = cast<TemplateDecl>(GetDecl(Record[Idx++]));
3657 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
3658 }
3659
3660 case TemplateName::DependentTemplate: {
3661 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3662 if (Record[Idx++]) // isIdentifier
3663 return Context->getDependentTemplateName(NNS,
3664 GetIdentifierInfo(Record, Idx));
3665 return Context->getDependentTemplateName(NNS,
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003666 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003667 }
3668 }
3669
3670 assert(0 && "Unhandled template name kind!");
3671 return TemplateName();
3672}
3673
3674TemplateArgument
Sebastian Redl2c499f62010-08-18 23:56:43 +00003675ASTReader::ReadTemplateArgument(llvm::BitstreamCursor &DeclsCursor,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003676 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003677 switch ((TemplateArgument::ArgKind)Record[Idx++]) {
3678 case TemplateArgument::Null:
3679 return TemplateArgument();
3680 case TemplateArgument::Type:
3681 return TemplateArgument(GetType(Record[Idx++]));
3682 case TemplateArgument::Declaration:
3683 return TemplateArgument(GetDecl(Record[Idx++]));
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +00003684 case TemplateArgument::Integral: {
3685 llvm::APSInt Value = ReadAPSInt(Record, Idx);
3686 QualType T = GetType(Record[Idx++]);
3687 return TemplateArgument(Value, T);
3688 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003689 case TemplateArgument::Template:
3690 return TemplateArgument(ReadTemplateName(Record, Idx));
3691 case TemplateArgument::Expression:
Sebastian Redlc67764e2010-07-22 22:43:28 +00003692 return TemplateArgument(ReadExpr(DeclsCursor));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003693 case TemplateArgument::Pack: {
3694 unsigned NumArgs = Record[Idx++];
3695 llvm::SmallVector<TemplateArgument, 8> Args;
3696 Args.reserve(NumArgs);
3697 while (NumArgs--)
Sebastian Redlc67764e2010-07-22 22:43:28 +00003698 Args.push_back(ReadTemplateArgument(DeclsCursor, Record, Idx));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003699 TemplateArgument TemplArg;
3700 TemplArg.setArgumentPack(Args.data(), Args.size(), /*CopyArgs=*/true);
3701 return TemplArg;
3702 }
3703 }
3704
3705 assert(0 && "Unhandled template argument kind!");
3706 return TemplateArgument();
3707}
3708
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003709TemplateParameterList *
Sebastian Redl2c499f62010-08-18 23:56:43 +00003710ASTReader::ReadTemplateParameterList(const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003711 SourceLocation TemplateLoc = ReadSourceLocation(Record, Idx);
3712 SourceLocation LAngleLoc = ReadSourceLocation(Record, Idx);
3713 SourceLocation RAngleLoc = ReadSourceLocation(Record, Idx);
3714
3715 unsigned NumParams = Record[Idx++];
3716 llvm::SmallVector<NamedDecl *, 16> Params;
3717 Params.reserve(NumParams);
3718 while (NumParams--)
3719 Params.push_back(cast<NamedDecl>(GetDecl(Record[Idx++])));
3720
3721 TemplateParameterList* TemplateParams =
3722 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
3723 Params.data(), Params.size(), RAngleLoc);
3724 return TemplateParams;
3725}
3726
3727void
Sebastian Redl2c499f62010-08-18 23:56:43 +00003728ASTReader::
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003729ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003730 llvm::BitstreamCursor &DeclsCursor,
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003731 const RecordData &Record, unsigned &Idx) {
3732 unsigned NumTemplateArgs = Record[Idx++];
3733 TemplArgs.reserve(NumTemplateArgs);
3734 while (NumTemplateArgs--)
Sebastian Redlc67764e2010-07-22 22:43:28 +00003735 TemplArgs.push_back(ReadTemplateArgument(DeclsCursor, Record, Idx));
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003736}
3737
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003738/// \brief Read a UnresolvedSet structure.
Sebastian Redl2c499f62010-08-18 23:56:43 +00003739void ASTReader::ReadUnresolvedSet(UnresolvedSetImpl &Set,
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003740 const RecordData &Record, unsigned &Idx) {
3741 unsigned NumDecls = Record[Idx++];
3742 while (NumDecls--) {
3743 NamedDecl *D = cast<NamedDecl>(GetDecl(Record[Idx++]));
3744 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
3745 Set.addDecl(D, AS);
3746 }
3747}
3748
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003749CXXBaseSpecifier
Sebastian Redl2c499f62010-08-18 23:56:43 +00003750ASTReader::ReadCXXBaseSpecifier(llvm::BitstreamCursor &DeclsCursor,
Nick Lewycky19b9f952010-07-26 16:56:01 +00003751 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003752 bool isVirtual = static_cast<bool>(Record[Idx++]);
3753 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
3754 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Nick Lewycky19b9f952010-07-26 16:56:01 +00003755 TypeSourceInfo *TInfo = GetTypeSourceInfo(DeclsCursor, Record, Idx);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003756 SourceRange Range = ReadSourceRange(Record, Idx);
Nick Lewycky19b9f952010-07-26 16:56:01 +00003757 return CXXBaseSpecifier(Range, isVirtual, isBaseOfClass, AS, TInfo);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003758}
3759
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003760std::pair<CXXBaseOrMemberInitializer **, unsigned>
Sebastian Redl2c499f62010-08-18 23:56:43 +00003761ASTReader::ReadCXXBaseOrMemberInitializers(llvm::BitstreamCursor &Cursor,
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003762 const RecordData &Record,
3763 unsigned &Idx) {
3764 CXXBaseOrMemberInitializer **BaseOrMemberInitializers = 0;
3765 unsigned NumInitializers = Record[Idx++];
3766 if (NumInitializers) {
3767 ASTContext &C = *getContext();
3768
3769 BaseOrMemberInitializers
3770 = new (C) CXXBaseOrMemberInitializer*[NumInitializers];
3771 for (unsigned i=0; i != NumInitializers; ++i) {
3772 TypeSourceInfo *BaseClassInfo = 0;
3773 bool IsBaseVirtual = false;
3774 FieldDecl *Member = 0;
3775
3776 bool IsBaseInitializer = Record[Idx++];
3777 if (IsBaseInitializer) {
3778 BaseClassInfo = GetTypeSourceInfo(Cursor, Record, Idx);
3779 IsBaseVirtual = Record[Idx++];
3780 } else {
3781 Member = cast<FieldDecl>(GetDecl(Record[Idx++]));
3782 }
3783 SourceLocation MemberLoc = ReadSourceLocation(Record, Idx);
3784 Expr *Init = ReadExpr(Cursor);
3785 FieldDecl *AnonUnionMember
3786 = cast_or_null<FieldDecl>(GetDecl(Record[Idx++]));
3787 SourceLocation LParenLoc = ReadSourceLocation(Record, Idx);
3788 SourceLocation RParenLoc = ReadSourceLocation(Record, Idx);
3789 bool IsWritten = Record[Idx++];
3790 unsigned SourceOrderOrNumArrayIndices;
3791 llvm::SmallVector<VarDecl *, 8> Indices;
3792 if (IsWritten) {
3793 SourceOrderOrNumArrayIndices = Record[Idx++];
3794 } else {
3795 SourceOrderOrNumArrayIndices = Record[Idx++];
3796 Indices.reserve(SourceOrderOrNumArrayIndices);
3797 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
3798 Indices.push_back(cast<VarDecl>(GetDecl(Record[Idx++])));
3799 }
3800
3801 CXXBaseOrMemberInitializer *BOMInit;
3802 if (IsBaseInitializer) {
3803 BOMInit = new (C) CXXBaseOrMemberInitializer(C, BaseClassInfo,
3804 IsBaseVirtual, LParenLoc,
3805 Init, RParenLoc);
3806 } else if (IsWritten) {
3807 BOMInit = new (C) CXXBaseOrMemberInitializer(C, Member, MemberLoc,
3808 LParenLoc, Init, RParenLoc);
3809 } else {
3810 BOMInit = CXXBaseOrMemberInitializer::Create(C, Member, MemberLoc,
3811 LParenLoc, Init, RParenLoc,
3812 Indices.data(),
3813 Indices.size());
3814 }
3815
3816 BOMInit->setAnonUnionMember(AnonUnionMember);
3817 BaseOrMemberInitializers[i] = BOMInit;
3818 }
3819 }
3820
3821 return std::make_pair(BaseOrMemberInitializers, NumInitializers);
3822}
3823
Chris Lattnerca025db2010-05-07 21:43:38 +00003824NestedNameSpecifier *
Sebastian Redl2c499f62010-08-18 23:56:43 +00003825ASTReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003826 unsigned N = Record[Idx++];
3827 NestedNameSpecifier *NNS = 0, *Prev = 0;
3828 for (unsigned I = 0; I != N; ++I) {
3829 NestedNameSpecifier::SpecifierKind Kind
3830 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
3831 switch (Kind) {
3832 case NestedNameSpecifier::Identifier: {
3833 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
3834 NNS = NestedNameSpecifier::Create(*Context, Prev, II);
3835 break;
3836 }
3837
3838 case NestedNameSpecifier::Namespace: {
3839 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
3840 NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
3841 break;
3842 }
3843
3844 case NestedNameSpecifier::TypeSpec:
3845 case NestedNameSpecifier::TypeSpecWithTemplate: {
3846 Type *T = GetType(Record[Idx++]).getTypePtr();
3847 bool Template = Record[Idx++];
3848 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
3849 break;
3850 }
3851
3852 case NestedNameSpecifier::Global: {
3853 NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
3854 // No associated value, and there can't be a prefix.
3855 break;
3856 }
Chris Lattnerca025db2010-05-07 21:43:38 +00003857 }
Argyrios Kyrtzidisad65c692010-07-07 15:46:30 +00003858 Prev = NNS;
Chris Lattnerca025db2010-05-07 21:43:38 +00003859 }
3860 return NNS;
3861}
3862
3863SourceRange
Sebastian Redl2c499f62010-08-18 23:56:43 +00003864ASTReader::ReadSourceRange(const RecordData &Record, unsigned &Idx) {
Daniel Dunbar6d3bc082010-06-02 15:47:10 +00003865 SourceLocation beg = SourceLocation::getFromRawEncoding(Record[Idx++]);
3866 SourceLocation end = SourceLocation::getFromRawEncoding(Record[Idx++]);
3867 return SourceRange(beg, end);
Chris Lattnerca025db2010-05-07 21:43:38 +00003868}
3869
Douglas Gregor1daeb692009-04-13 18:14:40 +00003870/// \brief Read an integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00003871llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00003872 unsigned BitWidth = Record[Idx++];
3873 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
3874 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
3875 Idx += NumWords;
3876 return Result;
3877}
3878
3879/// \brief Read a signed integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00003880llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00003881 bool isUnsigned = Record[Idx++];
3882 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
3883}
3884
Douglas Gregore0a3a512009-04-14 21:55:33 +00003885/// \brief Read a floating-point value
Sebastian Redl2c499f62010-08-18 23:56:43 +00003886llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00003887 return llvm::APFloat(ReadAPInt(Record, Idx));
3888}
3889
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003890// \brief Read a string
Sebastian Redl2c499f62010-08-18 23:56:43 +00003891std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003892 unsigned Len = Record[Idx++];
Jay Foad7d0479f2009-05-21 09:52:38 +00003893 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003894 Idx += Len;
3895 return Result;
3896}
3897
Sebastian Redl2c499f62010-08-18 23:56:43 +00003898CXXTemporary *ASTReader::ReadCXXTemporary(const RecordData &Record,
Chris Lattnercba86142010-05-10 00:25:06 +00003899 unsigned &Idx) {
3900 CXXDestructorDecl *Decl = cast<CXXDestructorDecl>(GetDecl(Record[Idx++]));
3901 return CXXTemporary::Create(*Context, Decl);
3902}
3903
Sebastian Redl2c499f62010-08-18 23:56:43 +00003904DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregor92863e42009-04-10 23:10:45 +00003905 return Diag(SourceLocation(), DiagID);
3906}
3907
Sebastian Redl2c499f62010-08-18 23:56:43 +00003908DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003909 return Diags.Report(FullSourceLoc(Loc, SourceMgr), DiagID);
Douglas Gregor55abb232009-04-10 20:39:37 +00003910}
Douglas Gregora9af1d12009-04-17 00:04:06 +00003911
Douglas Gregora868bbd2009-04-21 22:25:48 +00003912/// \brief Retrieve the identifier table associated with the
3913/// preprocessor.
Sebastian Redl2c499f62010-08-18 23:56:43 +00003914IdentifierTable &ASTReader::getIdentifierTable() {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003915 assert(PP && "Forgot to set Preprocessor ?");
3916 return PP->getIdentifierTable();
Douglas Gregora868bbd2009-04-21 22:25:48 +00003917}
3918
Douglas Gregora9af1d12009-04-17 00:04:06 +00003919/// \brief Record that the given ID maps to the given switch-case
3920/// statement.
Sebastian Redl2c499f62010-08-18 23:56:43 +00003921void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Douglas Gregora9af1d12009-04-17 00:04:06 +00003922 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
3923 SwitchCaseStmts[ID] = SC;
3924}
3925
3926/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00003927SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Douglas Gregora9af1d12009-04-17 00:04:06 +00003928 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
3929 return SwitchCaseStmts[ID];
3930}
Douglas Gregor6cc68a42009-04-17 18:18:49 +00003931
3932/// \brief Record that the given label statement has been
3933/// deserialized and has the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00003934void ASTReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
Mike Stump11289f42009-09-09 15:08:12 +00003935 assert(LabelStmts.find(ID) == LabelStmts.end() &&
Douglas Gregor6cc68a42009-04-17 18:18:49 +00003936 "Deserialized label twice");
3937 LabelStmts[ID] = S;
3938
3939 // If we've already seen any goto statements that point to this
3940 // label, resolve them now.
3941 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
3942 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
3943 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
3944 Goto->second->setLabel(S);
3945 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor779d8652009-04-17 18:58:21 +00003946
3947 // If we've already seen any address-label statements that point to
3948 // this label, resolve them now.
3949 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
Mike Stump11289f42009-09-09 15:08:12 +00003950 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
Douglas Gregor779d8652009-04-17 18:58:21 +00003951 = UnresolvedAddrLabelExprs.equal_range(ID);
Mike Stump11289f42009-09-09 15:08:12 +00003952 for (AddrLabelIter AddrLabel = AddrLabels.first;
Douglas Gregor779d8652009-04-17 18:58:21 +00003953 AddrLabel != AddrLabels.second; ++AddrLabel)
3954 AddrLabel->second->setLabel(S);
3955 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor6cc68a42009-04-17 18:18:49 +00003956}
3957
3958/// \brief Set the label of the given statement to the label
3959/// identified by ID.
3960///
3961/// Depending on the order in which the label and other statements
3962/// referencing that label occur, this operation may complete
3963/// immediately (updating the statement) or it may queue the
3964/// statement to be back-patched later.
Sebastian Redl2c499f62010-08-18 23:56:43 +00003965void ASTReader::SetLabelOf(GotoStmt *S, unsigned ID) {
Douglas Gregor6cc68a42009-04-17 18:18:49 +00003966 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3967 if (Label != LabelStmts.end()) {
3968 // We've already seen this label, so set the label of the goto and
3969 // we're done.
3970 S->setLabel(Label->second);
3971 } else {
3972 // We haven't seen this label yet, so add this goto to the set of
3973 // unresolved goto statements.
3974 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
3975 }
3976}
Douglas Gregor779d8652009-04-17 18:58:21 +00003977
3978/// \brief Set the label of the given expression to the label
3979/// identified by ID.
3980///
3981/// Depending on the order in which the label and other statements
3982/// referencing that label occur, this operation may complete
3983/// immediately (updating the statement) or it may queue the
3984/// statement to be back-patched later.
Sebastian Redl2c499f62010-08-18 23:56:43 +00003985void ASTReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
Douglas Gregor779d8652009-04-17 18:58:21 +00003986 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3987 if (Label != LabelStmts.end()) {
3988 // We've already seen this label, so set the label of the
3989 // label-address expression and we're done.
3990 S->setLabel(Label->second);
3991 } else {
3992 // We haven't seen this label yet, so add this label-address
3993 // expression to the set of unresolved label-address expressions.
3994 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
3995 }
3996}
Douglas Gregor1342e842009-07-06 18:54:52 +00003997
Sebastian Redl2c499f62010-08-18 23:56:43 +00003998void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00003999 assert(NumCurrentElementsDeserializing &&
4000 "FinishedDeserializing not paired with StartedDeserializing");
4001 if (NumCurrentElementsDeserializing == 1) {
Douglas Gregor1342e842009-07-06 18:54:52 +00004002 // If any identifiers with corresponding top-level declarations have
4003 // been loaded, load those declarations now.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004004 while (!PendingIdentifierInfos.empty()) {
4005 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
4006 PendingIdentifierInfos.front().DeclIDs, true);
4007 PendingIdentifierInfos.pop_front();
Douglas Gregor1342e842009-07-06 18:54:52 +00004008 }
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004009
4010 // We are not in recursive loading, so it's safe to pass the "interesting"
4011 // decls to the consumer.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004012 if (Consumer)
4013 PassInterestingDeclsToConsumer();
Douglas Gregor1342e842009-07-06 18:54:52 +00004014 }
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004015 --NumCurrentElementsDeserializing;
Douglas Gregor1342e842009-07-06 18:54:52 +00004016}
Douglas Gregorb473b072010-08-19 00:28:17 +00004017
4018ASTReader::PerFileData::PerFileData()
4019 : StatCache(0), LocalNumSLocEntries(0), LocalNumTypes(0), TypeOffsets(0),
4020 LocalNumDecls(0), DeclOffsets(0), LocalNumIdentifiers(0),
4021 IdentifierOffsets(0), IdentifierTableData(0), IdentifierLookupTable(0),
4022 LocalNumMacroDefinitions(0), MacroDefinitionOffsets(0),
4023 NumPreallocatedPreprocessingEntities(0), SelectorLookupTable(0),
4024 SelectorLookupTableData(0), SelectorOffsets(0), LocalNumSelectors(0)
4025{}
4026
4027ASTReader::PerFileData::~PerFileData() {
4028 delete static_cast<ASTIdentifierLookupTable *>(IdentifierLookupTable);
4029 delete static_cast<ASTSelectorLookupTable *>(SelectorLookupTable);
4030}
4031