blob: 4461703f522afa0a7cf3f8ff661ddbde4edfbe39 [file] [log] [blame]
Sebastian Redl904c9c82010-08-18 23:57:11 +00001//===--- ASTReader.cpp - AST File Reader ------------------------*- C++ -*-===//
Douglas Gregor2cf26342009-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 Redlc43b54c2010-08-18 23:56:43 +000010// This file defines the ASTReader class, which reads AST files.
Douglas Gregor2cf26342009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
Chris Lattner4c6f9522009-04-27 05:14:47 +000013
Sebastian Redl6ab7cd82010-08-18 23:57:17 +000014#include "clang/Serialization/ASTReader.h"
15#include "clang/Serialization/ASTDeserializationListener.h"
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +000016#include "ASTCommon.h"
Douglas Gregor0a0428e2009-04-10 20:39:37 +000017#include "clang/Frontend/FrontendDiagnostic.h"
Daniel Dunbarc7162932009-11-11 23:58:53 +000018#include "clang/Frontend/Utils.h"
Douglas Gregore737f502010-08-12 20:07:10 +000019#include "clang/Sema/Sema.h"
John McCall5f1e0942010-08-24 08:50:51 +000020#include "clang/Sema/Scope.h"
Douglas Gregorfdd01722009-04-14 00:24:19 +000021#include "clang/AST/ASTConsumer.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000022#include "clang/AST/ASTContext.h"
John McCall2a7fb272010-08-25 05:32:35 +000023#include "clang/AST/DeclTemplate.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000024#include "clang/AST/Expr.h"
John McCall7a1fad32010-08-24 07:32:53 +000025#include "clang/AST/ExprCXX.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000026#include "clang/AST/Type.h"
John McCalla1ee0c52009-10-16 21:56:05 +000027#include "clang/AST/TypeLocVisitor.h"
Chris Lattner42d42b52009-04-10 21:41:48 +000028#include "clang/Lex/MacroInfo.h"
Douglas Gregor6a5a23f2010-03-19 21:51:54 +000029#include "clang/Lex/PreprocessingRecord.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000030#include "clang/Lex/Preprocessor.h"
Steve Naroff83d63c72009-04-24 20:03:17 +000031#include "clang/Lex/HeaderSearch.h"
Douglas Gregor668c1a42009-04-21 22:25:48 +000032#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000033#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000034#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000035#include "clang/Basic/FileManager.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000036#include "clang/Basic/TargetInfo.h"
Douglas Gregor445e23e2009-10-05 21:07:28 +000037#include "clang/Basic/Version.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000038#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000039#include "llvm/Bitcode/BitstreamReader.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000040#include "llvm/Support/MemoryBuffer.h"
John McCall833ca992009-10-29 08:12:44 +000041#include "llvm/Support/ErrorHandling.h"
Daniel Dunbard5b21972009-11-18 19:50:41 +000042#include "llvm/System/Path.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000043#include <algorithm>
Douglas Gregore721f952009-04-28 18:58:38 +000044#include <iterator>
Douglas Gregor2cf26342009-04-09 22:27:44 +000045#include <cstdio>
Douglas Gregor4fed3f42009-04-27 18:38:38 +000046#include <sys/stat.h>
Douglas Gregor2cf26342009-04-09 22:27:44 +000047using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000048using namespace clang::serialization;
Douglas Gregor2cf26342009-04-09 22:27:44 +000049
50//===----------------------------------------------------------------------===//
Sebastian Redl3c7f4132010-08-18 23:57:06 +000051// PCH validator implementation
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000052//===----------------------------------------------------------------------===//
53
Sebastian Redl571db7f2010-08-18 23:56:56 +000054ASTReaderListener::~ASTReaderListener() {}
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000055
56bool
57PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts) {
58 const LangOptions &PPLangOpts = PP.getLangOptions();
59#define PARSE_LANGOPT_BENIGN(Option)
60#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
61 if (PPLangOpts.Option != LangOpts.Option) { \
62 Reader.Diag(DiagID) << LangOpts.Option << PPLangOpts.Option; \
63 return true; \
64 }
65
66 PARSE_LANGOPT_BENIGN(Trigraphs);
67 PARSE_LANGOPT_BENIGN(BCPLComment);
68 PARSE_LANGOPT_BENIGN(DollarIdents);
69 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
70 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
Chandler Carrutheb5d7b72010-04-17 20:17:31 +000071 PARSE_LANGOPT_IMPORTANT(GNUKeywords, diag::warn_pch_gnu_keywords);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000072 PARSE_LANGOPT_BENIGN(ImplicitInt);
73 PARSE_LANGOPT_BENIGN(Digraphs);
74 PARSE_LANGOPT_BENIGN(HexFloats);
75 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
76 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
Michael J. Spencerdae4ac42010-10-21 05:21:48 +000077 PARSE_LANGOPT_BENIGN(MSCVersion);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000078 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
79 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
80 PARSE_LANGOPT_BENIGN(CXXOperatorName);
81 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
82 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
83 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
Fariborz Jahanian412e7982010-02-09 19:31:38 +000084 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI2, diag::warn_pch_nonfragile_abi2);
Michael J. Spencer20249a12010-10-21 03:16:25 +000085 PARSE_LANGOPT_IMPORTANT(NoConstantCFStrings,
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +000086 diag::warn_pch_no_constant_cfstrings);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000087 PARSE_LANGOPT_BENIGN(PascalStrings);
88 PARSE_LANGOPT_BENIGN(WritableStrings);
Mike Stump1eb44332009-09-09 15:08:12 +000089 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000090 diag::warn_pch_lax_vector_conversions);
Nate Begeman69cfb9b2009-06-25 22:57:40 +000091 PARSE_LANGOPT_IMPORTANT(AltiVec, diag::warn_pch_altivec);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000092 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
Daniel Dunbar73482882010-02-10 18:48:44 +000093 PARSE_LANGOPT_IMPORTANT(SjLjExceptions, diag::warn_pch_sjlj_exceptions);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000094 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
95 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
96 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
Mike Stump1eb44332009-09-09 15:08:12 +000097 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000098 diag::warn_pch_thread_safe_statics);
Daniel Dunbar5345c392009-09-03 04:54:28 +000099 PARSE_LANGOPT_IMPORTANT(POSIXThreads, diag::warn_pch_posix_threads);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000100 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
101 PARSE_LANGOPT_BENIGN(EmitAllDecls);
102 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
Chris Lattnera4d71452010-06-26 21:25:03 +0000103 PARSE_LANGOPT_BENIGN(getSignedOverflowBehavior());
Mike Stump1eb44332009-09-09 15:08:12 +0000104 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000105 diag::warn_pch_heinous_extensions);
106 // FIXME: Most of the options below are benign if the macro wasn't
107 // used. Unfortunately, this means that a PCH compiled without
108 // optimization can't be used with optimization turned on, even
109 // though the only thing that changes is whether __OPTIMIZE__ was
110 // defined... but if __OPTIMIZE__ never showed up in the header, it
111 // doesn't matter. We could consider making this some special kind
112 // of check.
113 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
114 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
115 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
116 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
117 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
118 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
119 PARSE_LANGOPT_IMPORTANT(AccessControl, diag::warn_pch_access_control);
120 PARSE_LANGOPT_IMPORTANT(CharIsSigned, diag::warn_pch_char_signed);
John Thompsona6fda122009-11-05 20:14:16 +0000121 PARSE_LANGOPT_IMPORTANT(ShortWChar, diag::warn_pch_short_wchar);
Argyrios Kyrtzidis9a2b9d72010-10-08 00:25:19 +0000122 PARSE_LANGOPT_IMPORTANT(ShortEnums, diag::warn_pch_short_enums);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000123 if ((PPLangOpts.getGCMode() != 0) != (LangOpts.getGCMode() != 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000124 Reader.Diag(diag::warn_pch_gc_mode)
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000125 << LangOpts.getGCMode() << PPLangOpts.getGCMode();
126 return true;
127 }
128 PARSE_LANGOPT_BENIGN(getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000129 PARSE_LANGOPT_IMPORTANT(getStackProtectorMode(),
130 diag::warn_pch_stack_protector);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000131 PARSE_LANGOPT_BENIGN(InstantiationDepth);
Nate Begeman69cfb9b2009-06-25 22:57:40 +0000132 PARSE_LANGOPT_IMPORTANT(OpenCL, diag::warn_pch_opencl);
Mike Stump9c276ae2009-12-12 01:27:46 +0000133 PARSE_LANGOPT_BENIGN(CatchUndefined);
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000134 PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors);
Douglas Gregora0068fc2010-07-09 17:35:33 +0000135 PARSE_LANGOPT_BENIGN(SpellChecking);
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +0000136#undef PARSE_LANGOPT_IMPORTANT
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000137#undef PARSE_LANGOPT_BENIGN
138
139 return false;
140}
141
Daniel Dunbardc3c0d22009-11-11 00:52:11 +0000142bool PCHValidator::ReadTargetTriple(llvm::StringRef Triple) {
143 if (Triple == PP.getTargetInfo().getTriple().str())
144 return false;
145
146 Reader.Diag(diag::warn_pch_target_triple)
147 << Triple << PP.getTargetInfo().getTriple().str();
148 return true;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000149}
150
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000151struct EmptyStringRef {
Benjamin Kramerec1b1cc2010-07-14 23:19:41 +0000152 bool operator ()(llvm::StringRef r) const { return r.empty(); }
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000153};
154struct EmptyBlock {
155 bool operator ()(const PCHPredefinesBlock &r) const { return r.Data.empty(); }
156};
157
158static bool EqualConcatenations(llvm::SmallVector<llvm::StringRef, 2> L,
159 PCHPredefinesBlocks R) {
160 // First, sum up the lengths.
161 unsigned LL = 0, RL = 0;
162 for (unsigned I = 0, N = L.size(); I != N; ++I) {
163 LL += L[I].size();
164 }
165 for (unsigned I = 0, N = R.size(); I != N; ++I) {
166 RL += R[I].Data.size();
167 }
168 if (LL != RL)
169 return false;
170 if (LL == 0 && RL == 0)
171 return true;
172
173 // Kick out empty parts, they confuse the algorithm below.
174 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
175 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
176
177 // Do it the hard way. At this point, both vectors must be non-empty.
178 llvm::StringRef LR = L[0], RR = R[0].Data;
179 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
Daniel Dunbarc76c9e02010-07-16 00:00:11 +0000180 (void) RN;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000181 for (;;) {
182 // Compare the current pieces.
183 if (LR.size() == RR.size()) {
184 // If they're the same length, it's pretty easy.
185 if (LR != RR)
186 return false;
187 // Both pieces are done, advance.
188 ++LI;
189 ++RI;
190 // If either string is done, they're both done, since they're the same
191 // length.
192 if (LI == LN) {
193 assert(RI == RN && "Strings not the same length after all?");
194 return true;
195 }
196 LR = L[LI];
197 RR = R[RI].Data;
198 } else if (LR.size() < RR.size()) {
199 // Right piece is longer.
200 if (!RR.startswith(LR))
201 return false;
202 ++LI;
203 assert(LI != LN && "Strings not the same length after all?");
204 RR = RR.substr(LR.size());
205 LR = L[LI];
206 } else {
207 // Left piece is longer.
208 if (!LR.startswith(RR))
209 return false;
210 ++RI;
211 assert(RI != RN && "Strings not the same length after all?");
212 LR = LR.substr(RR.size());
213 RR = R[RI].Data;
214 }
215 }
216}
217
218static std::pair<FileID, llvm::StringRef::size_type>
219FindMacro(const PCHPredefinesBlocks &Buffers, llvm::StringRef MacroDef) {
220 std::pair<FileID, llvm::StringRef::size_type> Res;
221 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
222 Res.second = Buffers[I].Data.find(MacroDef);
223 if (Res.second != llvm::StringRef::npos) {
224 Res.first = Buffers[I].BufferID;
225 break;
226 }
227 }
228 return Res;
229}
230
231bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000232 llvm::StringRef OriginalFileName,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000233 std::string &SuggestedPredefines) {
Daniel Dunbarc7162932009-11-11 23:58:53 +0000234 // We are in the context of an implicit include, so the predefines buffer will
235 // have a #include entry for the PCH file itself (as normalized by the
236 // preprocessor initialization). Find it and skip over it in the checking
237 // below.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000238 llvm::SmallString<256> PCHInclude;
239 PCHInclude += "#include \"";
Daniel Dunbarc7162932009-11-11 23:58:53 +0000240 PCHInclude += NormalizeDashIncludePath(OriginalFileName);
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000241 PCHInclude += "\"\n";
242 std::pair<llvm::StringRef,llvm::StringRef> Split =
243 llvm::StringRef(PP.getPredefines()).split(PCHInclude.str());
244 llvm::StringRef Left = Split.first, Right = Split.second;
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000245 if (Left == PP.getPredefines()) {
246 Error("Missing PCH include entry!");
247 return true;
248 }
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000249
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000250 // If the concatenation of all the PCH buffers is equal to the adjusted
251 // command line, we're done.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000252 llvm::SmallVector<llvm::StringRef, 2> CommandLine;
253 CommandLine.push_back(Left);
254 CommandLine.push_back(Right);
255 if (EqualConcatenations(CommandLine, Buffers))
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000256 return false;
257
258 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000259
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000260 // The predefines buffers are different. Determine what the differences are,
261 // and whether they require us to reject the PCH file.
Daniel Dunbare6750492009-11-13 16:46:11 +0000262 llvm::SmallVector<llvm::StringRef, 8> PCHLines;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000263 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
264 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Daniel Dunbare6750492009-11-13 16:46:11 +0000265
266 llvm::SmallVector<llvm::StringRef, 8> CmdLineLines;
267 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000268
269 // Pick out implicit #includes after the PCH and don't consider them for
270 // validation; we will insert them into SuggestedPredefines so that the
271 // preprocessor includes them.
272 std::string IncludesAfterPCH;
273 llvm::SmallVector<llvm::StringRef, 8> AfterPCHLines;
274 Right.split(AfterPCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
275 for (unsigned i = 0, e = AfterPCHLines.size(); i != e; ++i) {
276 if (AfterPCHLines[i].startswith("#include ")) {
277 IncludesAfterPCH += AfterPCHLines[i];
278 IncludesAfterPCH += '\n';
279 } else {
280 CmdLineLines.push_back(AfterPCHLines[i]);
281 }
282 }
283
284 // Make sure we add the includes last into SuggestedPredefines before we
285 // exit this function.
286 struct AddIncludesRAII {
287 std::string &SuggestedPredefines;
288 std::string &IncludesAfterPCH;
289
290 AddIncludesRAII(std::string &SuggestedPredefines,
291 std::string &IncludesAfterPCH)
292 : SuggestedPredefines(SuggestedPredefines),
293 IncludesAfterPCH(IncludesAfterPCH) { }
294 ~AddIncludesRAII() {
295 SuggestedPredefines += IncludesAfterPCH;
296 }
297 } AddIncludes(SuggestedPredefines, IncludesAfterPCH);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000298
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000299 // Sort both sets of predefined buffer lines, since we allow some extra
300 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000301 std::sort(CmdLineLines.begin(), CmdLineLines.end());
302 std::sort(PCHLines.begin(), PCHLines.end());
303
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000304 // Determine which predefines that were used to build the PCH file are missing
305 // from the command line.
306 std::vector<llvm::StringRef> MissingPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000307 std::set_difference(PCHLines.begin(), PCHLines.end(),
308 CmdLineLines.begin(), CmdLineLines.end(),
309 std::back_inserter(MissingPredefines));
310
311 bool MissingDefines = false;
312 bool ConflictingDefines = false;
313 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000314 llvm::StringRef Missing = MissingPredefines[I];
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000315 if (Missing.startswith("#include ")) {
316 // An -include was specified when generating the PCH; it is included in
317 // the PCH, just ignore it.
318 continue;
319 }
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000320 if (!Missing.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000321 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
322 return true;
323 }
Mike Stump1eb44332009-09-09 15:08:12 +0000324
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000325 // This is a macro definition. Determine the name of the macro we're
326 // defining.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000327 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000328 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000329 = Missing.find_first_of("( \n\r", StartOfMacroName);
330 assert(EndOfMacroName != std::string::npos &&
331 "Couldn't find the end of the macro name");
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000332 llvm::StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000333
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000334 // Determine whether this macro was given a different definition on the
335 // command line.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000336 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000337 std::string::size_type MacroDefLen = MacroDefStart.size();
Daniel Dunbare6750492009-11-13 16:46:11 +0000338 llvm::SmallVector<llvm::StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000339 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
340 MacroDefStart);
341 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000342 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000343 // Different macro; we're done.
344 ConflictPos = CmdLineLines.end();
Mike Stump1eb44332009-09-09 15:08:12 +0000345 break;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000346 }
Mike Stump1eb44332009-09-09 15:08:12 +0000347
348 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000349 "Invalid #define in predefines buffer?");
Mike Stump1eb44332009-09-09 15:08:12 +0000350 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000351 (*ConflictPos)[MacroDefLen] != '(')
352 continue; // Longer macro name; keep trying.
Mike Stump1eb44332009-09-09 15:08:12 +0000353
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000354 // We found a conflicting macro definition.
355 break;
356 }
Mike Stump1eb44332009-09-09 15:08:12 +0000357
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000358 if (ConflictPos != CmdLineLines.end()) {
359 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
360 << MacroName;
361
362 // Show the definition of this macro within the PCH file.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000363 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
364 FindMacro(Buffers, Missing);
365 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
366 SourceLocation PCHMissingLoc =
367 SourceMgr.getLocForStartOfFile(MacroLoc.first)
368 .getFileLocWithOffset(MacroLoc.second);
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000369 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000370
371 ConflictingDefines = true;
372 continue;
373 }
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000375 // If the macro doesn't conflict, then we'll just pick up the macro
376 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000377 if (ConflictingDefines)
378 continue; // Don't complain if there are already conflicting defs
Mike Stump1eb44332009-09-09 15:08:12 +0000379
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000380 if (!MissingDefines) {
381 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
382 MissingDefines = true;
383 }
384
385 // Show the definition of this macro within the PCH file.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000386 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
387 FindMacro(Buffers, Missing);
388 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
389 SourceLocation PCHMissingLoc =
390 SourceMgr.getLocForStartOfFile(MacroLoc.first)
391 .getFileLocWithOffset(MacroLoc.second);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000392 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
393 }
Mike Stump1eb44332009-09-09 15:08:12 +0000394
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000395 if (ConflictingDefines)
396 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000397
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000398 // Determine what predefines were introduced based on command-line
399 // parameters that were not present when building the PCH
400 // file. Extra #defines are okay, so long as the identifiers being
401 // defined were not used within the precompiled header.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000402 std::vector<llvm::StringRef> ExtraPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000403 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
404 PCHLines.begin(), PCHLines.end(),
Mike Stump1eb44332009-09-09 15:08:12 +0000405 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000406 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000407 llvm::StringRef &Extra = ExtraPredefines[I];
408 if (!Extra.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000409 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
410 return true;
411 }
412
413 // This is an extra macro definition. Determine the name of the
414 // macro we're defining.
415 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000416 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000417 = Extra.find_first_of("( \n\r", StartOfMacroName);
418 assert(EndOfMacroName != std::string::npos &&
419 "Couldn't find the end of the macro name");
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000420 llvm::StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000421
422 // Check whether this name was used somewhere in the PCH file. If
423 // so, defining it as a macro could change behavior, so we reject
424 // the PCH file.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000425 if (IdentifierInfo *II = Reader.get(MacroName)) {
Daniel Dunbar4fda42e2009-11-11 00:52:00 +0000426 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000427 return true;
428 }
429
430 // Add this definition to the suggested predefines buffer.
431 SuggestedPredefines += Extra;
432 SuggestedPredefines += '\n';
433 }
434
435 // If we get here, it's because the predefines buffer had compatible
436 // contents. Accept the PCH file.
437 return false;
438}
439
Douglas Gregor12fab312010-03-16 16:35:32 +0000440void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
441 unsigned ID) {
442 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
443 ++NumHeaderInfos;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000444}
445
446void PCHValidator::ReadCounter(unsigned Value) {
447 PP.setCounterValue(Value);
448}
449
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000450//===----------------------------------------------------------------------===//
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000451// AST reader implementation
Douglas Gregor668c1a42009-04-21 22:25:48 +0000452//===----------------------------------------------------------------------===//
453
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000454void
Sebastian Redl571db7f2010-08-18 23:56:56 +0000455ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000456 DeserializationListener = Listener;
457 if (DeserializationListener)
458 DeserializationListener->SetReader(this);
459}
460
Chris Lattner4c6f9522009-04-27 05:14:47 +0000461
Douglas Gregor668c1a42009-04-21 22:25:48 +0000462namespace {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000463class ASTSelectorLookupTrait {
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000464 ASTReader &Reader;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000465
466public:
Sebastian Redl5d050072010-08-04 17:20:04 +0000467 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000468 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +0000469 ObjCMethodList Instance, Factory;
470 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000471
472 typedef Selector external_key_type;
473 typedef external_key_type internal_key_type;
474
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000475 explicit ASTSelectorLookupTrait(ASTReader &Reader) : Reader(Reader) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000476
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000477 static bool EqualKey(const internal_key_type& a,
478 const internal_key_type& b) {
479 return a == b;
480 }
Mike Stump1eb44332009-09-09 15:08:12 +0000481
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000482 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +0000483 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000484 }
Mike Stump1eb44332009-09-09 15:08:12 +0000485
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000486 // This hopefully will just get inlined and removed by the optimizer.
487 static const internal_key_type&
488 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump1eb44332009-09-09 15:08:12 +0000489
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000490 static std::pair<unsigned, unsigned>
491 ReadKeyDataLength(const unsigned char*& d) {
492 using namespace clang::io;
493 unsigned KeyLen = ReadUnalignedLE16(d);
494 unsigned DataLen = ReadUnalignedLE16(d);
495 return std::make_pair(KeyLen, DataLen);
496 }
Mike Stump1eb44332009-09-09 15:08:12 +0000497
Douglas Gregor83941df2009-04-25 17:48:32 +0000498 internal_key_type ReadKey(const unsigned char* d, unsigned) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000499 using namespace clang::io;
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000500 SelectorTable &SelTable = Reader.getContext()->Selectors;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000501 unsigned N = ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +0000502 IdentifierInfo *FirstII
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000503 = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
504 if (N == 0)
505 return SelTable.getNullarySelector(FirstII);
506 else if (N == 1)
507 return SelTable.getUnarySelector(FirstII);
508
509 llvm::SmallVector<IdentifierInfo *, 16> Args;
510 Args.push_back(FirstII);
511 for (unsigned I = 1; I != N; ++I)
512 Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)));
513
Douglas Gregor75fdb232009-05-22 22:45:36 +0000514 return SelTable.getSelector(N, Args.data());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000515 }
Mike Stump1eb44332009-09-09 15:08:12 +0000516
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000517 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
518 using namespace clang::io;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000519
520 data_type Result;
521
Sebastian Redl5d050072010-08-04 17:20:04 +0000522 Result.ID = ReadUnalignedLE32(d);
523 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
524 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
525
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000526 // Load instance methods
527 ObjCMethodList *Prev = 0;
528 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000529 ObjCMethodDecl *Method
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000530 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl5d050072010-08-04 17:20:04 +0000531 if (!Result.Instance.Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000532 // This is the first method, which is the easy case.
Sebastian Redl5d050072010-08-04 17:20:04 +0000533 Result.Instance.Method = Method;
534 Prev = &Result.Instance;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000535 continue;
536 }
537
Ted Kremenek298ed872010-02-11 00:53:01 +0000538 ObjCMethodList *Mem =
539 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
540 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000541 Prev = Prev->Next;
542 }
543
544 // Load factory methods
545 Prev = 0;
546 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000547 ObjCMethodDecl *Method
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000548 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl5d050072010-08-04 17:20:04 +0000549 if (!Result.Factory.Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000550 // This is the first method, which is the easy case.
Sebastian Redl5d050072010-08-04 17:20:04 +0000551 Result.Factory.Method = Method;
552 Prev = &Result.Factory;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000553 continue;
554 }
555
Ted Kremenek298ed872010-02-11 00:53:01 +0000556 ObjCMethodList *Mem =
557 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
558 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000559 Prev = Prev->Next;
560 }
561
562 return Result;
563 }
564};
Mike Stump1eb44332009-09-09 15:08:12 +0000565
566} // end anonymous namespace
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000567
568/// \brief The on-disk hash table used for the global method pool.
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000569typedef OnDiskChainedHashTable<ASTSelectorLookupTrait>
570 ASTSelectorLookupTable;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000571
Sebastian Redlc3632732010-10-05 15:59:54 +0000572namespace clang {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000573class ASTIdentifierLookupTrait {
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000574 ASTReader &Reader;
Sebastian Redlc3632732010-10-05 15:59:54 +0000575 ASTReader::PerFileData &F;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000576
577 // If we know the IdentifierInfo in advance, it is here and we will
578 // not build a new one. Used when deserializing information about an
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000579 // identifier that was constructed before the AST file was read.
Douglas Gregor668c1a42009-04-21 22:25:48 +0000580 IdentifierInfo *KnownII;
581
582public:
583 typedef IdentifierInfo * data_type;
584
585 typedef const std::pair<const char*, unsigned> external_key_type;
586
587 typedef external_key_type internal_key_type;
588
Sebastian Redlc3632732010-10-05 15:59:54 +0000589 ASTIdentifierLookupTrait(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redld27d3fc2010-07-21 22:31:37 +0000590 IdentifierInfo *II = 0)
Sebastian Redlc3632732010-10-05 15:59:54 +0000591 : Reader(Reader), F(F), KnownII(II) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000592
Douglas Gregor668c1a42009-04-21 22:25:48 +0000593 static bool EqualKey(const internal_key_type& a,
594 const internal_key_type& b) {
595 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
596 : false;
597 }
Mike Stump1eb44332009-09-09 15:08:12 +0000598
Douglas Gregor668c1a42009-04-21 22:25:48 +0000599 static unsigned ComputeHash(const internal_key_type& a) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000600 return llvm::HashString(llvm::StringRef(a.first, a.second));
Douglas Gregor668c1a42009-04-21 22:25:48 +0000601 }
Mike Stump1eb44332009-09-09 15:08:12 +0000602
Douglas Gregor668c1a42009-04-21 22:25:48 +0000603 // This hopefully will just get inlined and removed by the optimizer.
604 static const internal_key_type&
605 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump1eb44332009-09-09 15:08:12 +0000606
Douglas Gregor95f42922010-10-14 22:11:03 +0000607 // This hopefully will just get inlined and removed by the optimizer.
608 static const external_key_type&
609 GetExternalKey(const internal_key_type& x) { return x; }
610
Douglas Gregor668c1a42009-04-21 22:25:48 +0000611 static std::pair<unsigned, unsigned>
612 ReadKeyDataLength(const unsigned char*& d) {
613 using namespace clang::io;
Douglas Gregor5f8e3302009-04-25 20:26:24 +0000614 unsigned DataLen = ReadUnalignedLE16(d);
Douglas Gregord6595a42009-04-25 21:04:17 +0000615 unsigned KeyLen = ReadUnalignedLE16(d);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000616 return std::make_pair(KeyLen, DataLen);
617 }
Mike Stump1eb44332009-09-09 15:08:12 +0000618
Douglas Gregor668c1a42009-04-21 22:25:48 +0000619 static std::pair<const char*, unsigned>
620 ReadKey(const unsigned char* d, unsigned n) {
621 assert(n >= 2 && d[n-1] == '\0');
622 return std::make_pair((const char*) d, n-1);
623 }
Mike Stump1eb44332009-09-09 15:08:12 +0000624
625 IdentifierInfo *ReadData(const internal_key_type& k,
Douglas Gregor668c1a42009-04-21 22:25:48 +0000626 const unsigned char* d,
627 unsigned DataLen) {
628 using namespace clang::io;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000629 IdentID ID = ReadUnalignedLE32(d);
Douglas Gregora92193e2009-04-28 21:18:29 +0000630 bool IsInteresting = ID & 0x01;
631
632 // Wipe out the "is interesting" bit.
633 ID = ID >> 1;
634
635 if (!IsInteresting) {
Sebastian Redl083abdf2010-07-27 23:01:28 +0000636 // For uninteresting identifiers, just build the IdentifierInfo
Douglas Gregora92193e2009-04-28 21:18:29 +0000637 // and associate it with the persistent ID.
638 IdentifierInfo *II = KnownII;
639 if (!II)
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000640 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregora92193e2009-04-28 21:18:29 +0000641 Reader.SetIdentifierInfo(ID, II);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000642 II->setIsFromAST();
Douglas Gregora92193e2009-04-28 21:18:29 +0000643 return II;
644 }
645
Douglas Gregor5998da52009-04-28 21:32:13 +0000646 unsigned Bits = ReadUnalignedLE16(d);
Douglas Gregor2deaea32009-04-22 18:49:13 +0000647 bool CPlusPlusOperatorKeyword = Bits & 0x01;
648 Bits >>= 1;
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000649 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
650 Bits >>= 1;
Douglas Gregor2deaea32009-04-22 18:49:13 +0000651 bool Poisoned = Bits & 0x01;
652 Bits >>= 1;
653 bool ExtensionToken = Bits & 0x01;
654 Bits >>= 1;
655 bool hasMacroDefinition = Bits & 0x01;
656 Bits >>= 1;
657 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
658 Bits >>= 10;
Mike Stump1eb44332009-09-09 15:08:12 +0000659
Douglas Gregor2deaea32009-04-22 18:49:13 +0000660 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregor5998da52009-04-28 21:32:13 +0000661 DataLen -= 6;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000662
663 // Build the IdentifierInfo itself and link the identifier ID with
664 // the new IdentifierInfo.
665 IdentifierInfo *II = KnownII;
666 if (!II)
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000667 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000668 Reader.SetIdentifierInfo(ID, II);
669
Douglas Gregor2deaea32009-04-22 18:49:13 +0000670 // Set or check the various bits in the IdentifierInfo structure.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000671 // Token IDs are read-only.
672 if (HasRevertedTokenIDToIdentifier)
673 II->RevertTokenIDToIdentifier();
Douglas Gregor2deaea32009-04-22 18:49:13 +0000674 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
Mike Stump1eb44332009-09-09 15:08:12 +0000675 assert(II->isExtensionToken() == ExtensionToken &&
Douglas Gregor2deaea32009-04-22 18:49:13 +0000676 "Incorrect extension token flag");
677 (void)ExtensionToken;
678 II->setIsPoisoned(Poisoned);
679 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
680 "Incorrect C++ operator keyword flag");
681 (void)CPlusPlusOperatorKeyword;
682
Douglas Gregor37e26842009-04-21 23:56:24 +0000683 // If this identifier is a macro, deserialize the macro
684 // definition.
685 if (hasMacroDefinition) {
Douglas Gregor5998da52009-04-28 21:32:13 +0000686 uint32_t Offset = ReadUnalignedLE32(d);
Sebastian Redlc3632732010-10-05 15:59:54 +0000687 Reader.ReadMacroRecord(F, Offset);
Douglas Gregor5998da52009-04-28 21:32:13 +0000688 DataLen -= 4;
Douglas Gregor37e26842009-04-21 23:56:24 +0000689 }
Douglas Gregor668c1a42009-04-21 22:25:48 +0000690
691 // Read all of the declarations visible at global scope with this
692 // name.
Chris Lattner6bf690f2009-04-27 22:17:41 +0000693 if (Reader.getContext() == 0) return II;
Douglas Gregord89275b2009-07-06 18:54:52 +0000694 if (DataLen > 0) {
695 llvm::SmallVector<uint32_t, 4> DeclIDs;
696 for (; DataLen > 0; DataLen -= 4)
697 DeclIDs.push_back(ReadUnalignedLE32(d));
698 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000699 }
Mike Stump1eb44332009-09-09 15:08:12 +0000700
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000701 II->setIsFromAST();
Douglas Gregor668c1a42009-04-21 22:25:48 +0000702 return II;
703 }
704};
Mike Stump1eb44332009-09-09 15:08:12 +0000705
706} // end anonymous namespace
Douglas Gregor668c1a42009-04-21 22:25:48 +0000707
708/// \brief The on-disk hash table used to contain information about
709/// all of the identifiers in the program.
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000710typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait>
711 ASTIdentifierLookupTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000712
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000713namespace {
714class ASTDeclContextNameLookupTrait {
715 ASTReader &Reader;
716
717public:
718 /// \brief Pair of begin/end iterators for DeclIDs.
719 typedef std::pair<DeclID *, DeclID *> data_type;
720
721 /// \brief Special internal key for declaration names.
722 /// The hash table creates keys for comparison; we do not create
723 /// a DeclarationName for the internal key to avoid deserializing types.
724 struct DeclNameKey {
725 DeclarationName::NameKind Kind;
726 uint64_t Data;
727 DeclNameKey() : Kind((DeclarationName::NameKind)0), Data(0) { }
728 };
729
730 typedef DeclarationName external_key_type;
731 typedef DeclNameKey internal_key_type;
732
733 explicit ASTDeclContextNameLookupTrait(ASTReader &Reader) : Reader(Reader) { }
734
735 static bool EqualKey(const internal_key_type& a,
736 const internal_key_type& b) {
737 return a.Kind == b.Kind && a.Data == b.Data;
738 }
739
740 unsigned ComputeHash(const DeclNameKey &Key) const {
741 llvm::FoldingSetNodeID ID;
742 ID.AddInteger(Key.Kind);
743
744 switch (Key.Kind) {
745 case DeclarationName::Identifier:
746 case DeclarationName::CXXLiteralOperatorName:
747 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
748 break;
749 case DeclarationName::ObjCZeroArgSelector:
750 case DeclarationName::ObjCOneArgSelector:
751 case DeclarationName::ObjCMultiArgSelector:
752 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
753 break;
754 case DeclarationName::CXXConstructorName:
755 case DeclarationName::CXXDestructorName:
756 case DeclarationName::CXXConversionFunctionName:
757 ID.AddInteger((TypeID)Key.Data);
758 break;
759 case DeclarationName::CXXOperatorName:
760 ID.AddInteger((OverloadedOperatorKind)Key.Data);
761 break;
762 case DeclarationName::CXXUsingDirective:
763 break;
764 }
765
766 return ID.ComputeHash();
767 }
768
769 internal_key_type GetInternalKey(const external_key_type& Name) const {
770 DeclNameKey Key;
771 Key.Kind = Name.getNameKind();
772 switch (Name.getNameKind()) {
773 case DeclarationName::Identifier:
774 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
775 break;
776 case DeclarationName::ObjCZeroArgSelector:
777 case DeclarationName::ObjCOneArgSelector:
778 case DeclarationName::ObjCMultiArgSelector:
779 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
780 break;
781 case DeclarationName::CXXConstructorName:
782 case DeclarationName::CXXDestructorName:
783 case DeclarationName::CXXConversionFunctionName:
784 Key.Data = Reader.GetTypeID(Name.getCXXNameType());
785 break;
786 case DeclarationName::CXXOperatorName:
787 Key.Data = Name.getCXXOverloadedOperator();
788 break;
789 case DeclarationName::CXXLiteralOperatorName:
790 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
791 break;
792 case DeclarationName::CXXUsingDirective:
793 break;
794 }
Michael J. Spencer20249a12010-10-21 03:16:25 +0000795
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000796 return Key;
797 }
798
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000799 external_key_type GetExternalKey(const internal_key_type& Key) const {
800 ASTContext *Context = Reader.getContext();
801 switch (Key.Kind) {
802 case DeclarationName::Identifier:
803 return DeclarationName((IdentifierInfo*)Key.Data);
804
805 case DeclarationName::ObjCZeroArgSelector:
806 case DeclarationName::ObjCOneArgSelector:
807 case DeclarationName::ObjCMultiArgSelector:
808 return DeclarationName(Selector(Key.Data));
809
810 case DeclarationName::CXXConstructorName:
811 return Context->DeclarationNames.getCXXConstructorName(
812 Context->getCanonicalType(Reader.GetType(Key.Data)));
813
814 case DeclarationName::CXXDestructorName:
815 return Context->DeclarationNames.getCXXDestructorName(
816 Context->getCanonicalType(Reader.GetType(Key.Data)));
817
818 case DeclarationName::CXXConversionFunctionName:
819 return Context->DeclarationNames.getCXXConversionFunctionName(
820 Context->getCanonicalType(Reader.GetType(Key.Data)));
821
822 case DeclarationName::CXXOperatorName:
823 return Context->DeclarationNames.getCXXOperatorName(
824 (OverloadedOperatorKind)Key.Data);
825
826 case DeclarationName::CXXLiteralOperatorName:
827 return Context->DeclarationNames.getCXXLiteralOperatorName(
828 (IdentifierInfo*)Key.Data);
829
830 case DeclarationName::CXXUsingDirective:
831 return DeclarationName::getUsingDirectiveName();
832 }
833
834 llvm_unreachable("Invalid Name Kind ?");
835 }
836
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000837 static std::pair<unsigned, unsigned>
838 ReadKeyDataLength(const unsigned char*& d) {
839 using namespace clang::io;
840 unsigned KeyLen = ReadUnalignedLE16(d);
841 unsigned DataLen = ReadUnalignedLE16(d);
842 return std::make_pair(KeyLen, DataLen);
843 }
844
845 internal_key_type ReadKey(const unsigned char* d, unsigned) {
846 using namespace clang::io;
847
848 DeclNameKey Key;
849 Key.Kind = (DeclarationName::NameKind)*d++;
850 switch (Key.Kind) {
851 case DeclarationName::Identifier:
852 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
853 break;
854 case DeclarationName::ObjCZeroArgSelector:
855 case DeclarationName::ObjCOneArgSelector:
856 case DeclarationName::ObjCMultiArgSelector:
Michael J. Spencer20249a12010-10-21 03:16:25 +0000857 Key.Data =
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000858 (uint64_t)Reader.DecodeSelector(ReadUnalignedLE32(d)).getAsOpaquePtr();
859 break;
860 case DeclarationName::CXXConstructorName:
861 case DeclarationName::CXXDestructorName:
862 case DeclarationName::CXXConversionFunctionName:
863 Key.Data = ReadUnalignedLE32(d); // TypeID
864 break;
865 case DeclarationName::CXXOperatorName:
866 Key.Data = *d++; // OverloadedOperatorKind
867 break;
868 case DeclarationName::CXXLiteralOperatorName:
869 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
870 break;
871 case DeclarationName::CXXUsingDirective:
872 break;
873 }
Michael J. Spencer20249a12010-10-21 03:16:25 +0000874
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000875 return Key;
876 }
877
878 data_type ReadData(internal_key_type, const unsigned char* d,
879 unsigned DataLen) {
880 using namespace clang::io;
881 unsigned NumDecls = ReadUnalignedLE16(d);
882 DeclID *Start = (DeclID *)d;
883 return std::make_pair(Start, Start + NumDecls);
884 }
885};
886
887} // end anonymous namespace
888
889/// \brief The on-disk hash table used for the DeclContext's Name lookup table.
890typedef OnDiskChainedHashTable<ASTDeclContextNameLookupTrait>
891 ASTDeclContextNameLookupTable;
892
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000893bool ASTReader::ReadDeclContextStorage(llvm::BitstreamCursor &Cursor,
894 const std::pair<uint64_t, uint64_t> &Offsets,
895 DeclContextInfo &Info) {
896 SavedStreamPosition SavedPosition(Cursor);
897 // First the lexical decls.
898 if (Offsets.first != 0) {
899 Cursor.JumpToBit(Offsets.first);
900
901 RecordData Record;
902 const char *Blob;
903 unsigned BlobLen;
904 unsigned Code = Cursor.ReadCode();
905 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
906 if (RecCode != DECL_CONTEXT_LEXICAL) {
907 Error("Expected lexical block");
908 return true;
909 }
910
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000911 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
912 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000913 } else {
914 Info.LexicalDecls = 0;
915 Info.NumLexicalDecls = 0;
916 }
917
918 // Now the lookup table.
919 if (Offsets.second != 0) {
920 Cursor.JumpToBit(Offsets.second);
921
922 RecordData Record;
923 const char *Blob;
924 unsigned BlobLen;
925 unsigned Code = Cursor.ReadCode();
926 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
927 if (RecCode != DECL_CONTEXT_VISIBLE) {
928 Error("Expected visible lookup table block");
929 return true;
930 }
931 Info.NameLookupTableData
932 = ASTDeclContextNameLookupTable::Create(
933 (const unsigned char *)Blob + Record[0],
934 (const unsigned char *)Blob,
935 ASTDeclContextNameLookupTrait(*this));
Sebastian Redl0ea8f7f2010-08-24 00:50:00 +0000936 } else {
937 Info.NameLookupTableData = 0;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000938 }
939
940 return false;
941}
942
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000943void ASTReader::Error(const char *Msg) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000944 Diag(diag::err_fe_pch_malformed) << Msg;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000945}
946
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000947/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000948bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000949 if (Listener)
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000950 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000951 ActualOriginalFileName,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000952 SuggestedPredefines);
Douglas Gregore721f952009-04-28 18:58:38 +0000953 return false;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000954}
955
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000956//===----------------------------------------------------------------------===//
957// Source Manager Deserialization
958//===----------------------------------------------------------------------===//
959
Douglas Gregorbd945002009-04-13 16:31:14 +0000960/// \brief Read the line table in the source manager block.
Sebastian Redlc3632732010-10-05 15:59:54 +0000961/// \returns true if there was an error.
962bool ASTReader::ParseLineTable(PerFileData &F,
963 llvm::SmallVectorImpl<uint64_t> &Record) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000964 unsigned Idx = 0;
965 LineTableInfo &LineTable = SourceMgr.getLineTable();
966
967 // Parse the file names
Douglas Gregorff0a9872009-04-13 17:12:42 +0000968 std::map<int, int> FileIDs;
969 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000970 // Extract the file name
971 unsigned FilenameLen = Record[Idx++];
972 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
973 Idx += FilenameLen;
Douglas Gregore650c8c2009-07-07 00:12:59 +0000974 MaybeAddSystemRootToFilename(Filename);
Mike Stump1eb44332009-09-09 15:08:12 +0000975 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
Douglas Gregorff0a9872009-04-13 17:12:42 +0000976 Filename.size());
Douglas Gregorbd945002009-04-13 16:31:14 +0000977 }
978
979 // Parse the line entries
980 std::vector<LineEntry> Entries;
981 while (Idx < Record.size()) {
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000982 int FID = Record[Idx++];
Douglas Gregorbd945002009-04-13 16:31:14 +0000983
984 // Extract the line entries
985 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000986 assert(NumEntries && "Numentries is 00000");
Douglas Gregorbd945002009-04-13 16:31:14 +0000987 Entries.clear();
988 Entries.reserve(NumEntries);
989 for (unsigned I = 0; I != NumEntries; ++I) {
990 unsigned FileOffset = Record[Idx++];
991 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000992 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump1eb44332009-09-09 15:08:12 +0000993 SrcMgr::CharacteristicKind FileKind
Douglas Gregorbd945002009-04-13 16:31:14 +0000994 = (SrcMgr::CharacteristicKind)Record[Idx++];
995 unsigned IncludeOffset = Record[Idx++];
996 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
997 FileKind, IncludeOffset));
998 }
999 LineTable.AddEntry(FID, Entries);
1000 }
1001
1002 return false;
1003}
1004
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001005namespace {
1006
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001007class ASTStatData {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001008public:
1009 const bool hasStat;
1010 const ino_t ino;
1011 const dev_t dev;
1012 const mode_t mode;
1013 const time_t mtime;
1014 const off_t size;
Mike Stump1eb44332009-09-09 15:08:12 +00001015
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001016 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Mike Stump1eb44332009-09-09 15:08:12 +00001017 : hasStat(true), ino(i), dev(d), mode(mo), mtime(m), size(s) {}
1018
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001019 ASTStatData()
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001020 : hasStat(false), ino(0), dev(0), mode(0), mtime(0), size(0) {}
1021};
1022
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001023class ASTStatLookupTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001024 public:
1025 typedef const char *external_key_type;
1026 typedef const char *internal_key_type;
1027
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001028 typedef ASTStatData data_type;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001029
1030 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001031 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001032 }
1033
1034 static internal_key_type GetInternalKey(const char *path) { return path; }
1035
1036 static bool EqualKey(internal_key_type a, internal_key_type b) {
1037 return strcmp(a, b) == 0;
1038 }
1039
1040 static std::pair<unsigned, unsigned>
1041 ReadKeyDataLength(const unsigned char*& d) {
1042 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1043 unsigned DataLen = (unsigned) *d++;
1044 return std::make_pair(KeyLen + 1, DataLen);
1045 }
1046
1047 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1048 return (const char *)d;
1049 }
1050
1051 static data_type ReadData(const internal_key_type, const unsigned char *d,
1052 unsigned /*DataLen*/) {
1053 using namespace clang::io;
1054
1055 if (*d++ == 1)
1056 return data_type();
1057
1058 ino_t ino = (ino_t) ReadUnalignedLE32(d);
1059 dev_t dev = (dev_t) ReadUnalignedLE32(d);
1060 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +00001061 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001062 off_t size = (off_t) ReadUnalignedLE64(d);
1063 return data_type(ino, dev, mode, mtime, size);
1064 }
1065};
1066
1067/// \brief stat() cache for precompiled headers.
1068///
1069/// This cache is very similar to the stat cache used by pretokenized
1070/// headers.
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001071class ASTStatCache : public StatSysCallCache {
1072 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001073 CacheTy *Cache;
1074
1075 unsigned &NumStatHits, &NumStatMisses;
Mike Stump1eb44332009-09-09 15:08:12 +00001076public:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001077 ASTStatCache(const unsigned char *Buckets,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001078 const unsigned char *Base,
1079 unsigned &NumStatHits,
Mike Stump1eb44332009-09-09 15:08:12 +00001080 unsigned &NumStatMisses)
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001081 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
1082 Cache = CacheTy::Create(Buckets, Base);
1083 }
1084
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001085 ~ASTStatCache() { delete Cache; }
Mike Stump1eb44332009-09-09 15:08:12 +00001086
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001087 int stat(const char *path, struct stat *buf) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001088 // Do the lookup for the file's data in the AST file.
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001089 CacheTy::iterator I = Cache->find(path);
1090
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001091 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001092 if (I == Cache->end()) {
1093 ++NumStatMisses;
Douglas Gregor52e71082009-10-16 18:18:30 +00001094 return StatSysCallCache::stat(path, buf);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001095 }
Mike Stump1eb44332009-09-09 15:08:12 +00001096
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001097 ++NumStatHits;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001098 ASTStatData Data = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001099
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001100 if (!Data.hasStat)
1101 return 1;
1102
1103 buf->st_ino = Data.ino;
1104 buf->st_dev = Data.dev;
1105 buf->st_mtime = Data.mtime;
1106 buf->st_mode = Data.mode;
1107 buf->st_size = Data.size;
1108 return 0;
1109 }
1110};
1111} // end anonymous namespace
1112
1113
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001114/// \brief Read a source manager block
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001115ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(PerFileData &F) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001116 using namespace SrcMgr;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001117
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001118 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001119
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001120 // Set the source-location entry cursor to the current position in
1121 // the stream. This cursor will be used to read the contents of the
1122 // source manager block initially, and then lazily read
1123 // source-location entries as needed.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001124 SLocEntryCursor = F.Stream;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001125
1126 // The stream itself is going to skip over the source manager block.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001127 if (F.Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001128 Error("malformed block record in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001129 return Failure;
1130 }
1131
1132 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001133 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001134 Error("malformed source manager block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001135 return Failure;
1136 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001137
Douglas Gregor14f79002009-04-10 03:52:48 +00001138 RecordData Record;
1139 while (true) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001140 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregor14f79002009-04-10 03:52:48 +00001141 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001142 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001143 Error("error at end of Source Manager block in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001144 return Failure;
1145 }
Douglas Gregore1d918e2009-04-10 23:10:45 +00001146 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +00001147 }
Mike Stump1eb44332009-09-09 15:08:12 +00001148
Douglas Gregor14f79002009-04-10 03:52:48 +00001149 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1150 // No known subblocks, always skip them.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001151 SLocEntryCursor.ReadSubBlockID();
1152 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001153 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001154 return Failure;
1155 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001156 continue;
1157 }
Mike Stump1eb44332009-09-09 15:08:12 +00001158
Douglas Gregor14f79002009-04-10 03:52:48 +00001159 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001160 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregor14f79002009-04-10 03:52:48 +00001161 continue;
1162 }
Mike Stump1eb44332009-09-09 15:08:12 +00001163
Douglas Gregor14f79002009-04-10 03:52:48 +00001164 // Read a record.
1165 const char *BlobStart;
1166 unsigned BlobLen;
1167 Record.clear();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001168 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001169 default: // Default behavior: ignore.
1170 break;
1171
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001172 case SM_LINE_TABLE:
Sebastian Redlc3632732010-10-05 15:59:54 +00001173 if (ParseLineTable(F, Record))
Douglas Gregorbd945002009-04-13 16:31:14 +00001174 return Failure;
Chris Lattner2c78b872009-04-14 23:22:57 +00001175 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001176
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001177 case SM_SLOC_FILE_ENTRY:
1178 case SM_SLOC_BUFFER_ENTRY:
1179 case SM_SLOC_INSTANTIATION_ENTRY:
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001180 // Once we hit one of the source location entries, we're done.
1181 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +00001182 }
1183 }
1184}
1185
Sebastian Redl190faf72010-07-20 21:50:20 +00001186/// \brief Get a cursor that's correctly positioned for reading the source
1187/// location entry with the given ID.
Sebastian Redlc3632732010-10-05 15:59:54 +00001188ASTReader::PerFileData *ASTReader::SLocCursorForID(unsigned ID) {
Sebastian Redl190faf72010-07-20 21:50:20 +00001189 assert(ID != 0 && ID <= TotalNumSLocEntries &&
1190 "SLocCursorForID should only be called for real IDs.");
1191
1192 ID -= 1;
1193 PerFileData *F = 0;
1194 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1195 F = Chain[N - I - 1];
1196 if (ID < F->LocalNumSLocEntries)
1197 break;
1198 ID -= F->LocalNumSLocEntries;
1199 }
1200 assert(F && F->LocalNumSLocEntries > ID && "Chain corrupted");
1201
1202 F->SLocEntryCursor.JumpToBit(F->SLocOffsets[ID]);
Sebastian Redlc3632732010-10-05 15:59:54 +00001203 return F;
Sebastian Redl190faf72010-07-20 21:50:20 +00001204}
1205
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001206/// \brief Read in the source location entry with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001207ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(unsigned ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001208 if (ID == 0)
1209 return Success;
1210
1211 if (ID > TotalNumSLocEntries) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001212 Error("source location entry ID out-of-range for AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001213 return Failure;
1214 }
1215
Sebastian Redlc3632732010-10-05 15:59:54 +00001216 PerFileData *F = SLocCursorForID(ID);
1217 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001218
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001219 ++NumSLocEntriesRead;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001220 unsigned Code = SLocEntryCursor.ReadCode();
1221 if (Code == llvm::bitc::END_BLOCK ||
1222 Code == llvm::bitc::ENTER_SUBBLOCK ||
1223 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001224 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001225 return Failure;
1226 }
1227
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001228 RecordData Record;
1229 const char *BlobStart;
1230 unsigned BlobLen;
1231 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1232 default:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001233 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001234 return Failure;
1235
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001236 case SM_SLOC_FILE_ENTRY: {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001237 std::string Filename(BlobStart, BlobStart + BlobLen);
1238 MaybeAddSystemRootToFilename(Filename);
1239 const FileEntry *File = FileMgr.getFile(Filename);
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001240 if (File == 0) {
1241 std::string ErrorStr = "could not find file '";
Douglas Gregore650c8c2009-07-07 00:12:59 +00001242 ErrorStr += Filename;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001243 ErrorStr += "' referenced by AST file";
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001244 Error(ErrorStr.c_str());
1245 return Failure;
1246 }
Mike Stump1eb44332009-09-09 15:08:12 +00001247
Douglas Gregor2d52be52010-03-21 22:49:54 +00001248 if (Record.size() < 10) {
Ted Kremenek1857f622010-03-18 21:23:05 +00001249 Error("source location entry is incorrect");
1250 return Failure;
1251 }
1252
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001253 if (!DisableValidation &&
1254 ((off_t)Record[4] != File->getSize()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001255#if !defined(LLVM_ON_WIN32)
1256 // In our regression testing, the Windows file system seems to
1257 // have inconsistent modification times that sometimes
1258 // erroneously trigger this error-handling path.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001259 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001260#endif
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001261 )) {
Douglas Gregor2d52be52010-03-21 22:49:54 +00001262 Diag(diag::err_fe_pch_file_modified)
1263 << Filename;
1264 return Failure;
1265 }
1266
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001267 FileID FID = SourceMgr.createFileID(File,
Sebastian Redlc3632732010-10-05 15:59:54 +00001268 ReadSourceLocation(*F, Record[1]),
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001269 (SrcMgr::CharacteristicKind)Record[2],
1270 ID, Record[0]);
1271 if (Record[3])
1272 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile())
1273 .setHasLineDirectives();
1274
Douglas Gregor12fab312010-03-16 16:35:32 +00001275 // Reconstruct header-search information for this file.
1276 HeaderFileInfo HFI;
Douglas Gregor2d52be52010-03-21 22:49:54 +00001277 HFI.isImport = Record[6];
1278 HFI.DirInfo = Record[7];
1279 HFI.NumIncludes = Record[8];
1280 HFI.ControllingMacroID = Record[9];
Douglas Gregor12fab312010-03-16 16:35:32 +00001281 if (Listener)
1282 Listener->ReadHeaderFileInfo(HFI, File->getUID());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001283 break;
1284 }
1285
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001286 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001287 const char *Name = BlobStart;
1288 unsigned Offset = Record[0];
1289 unsigned Code = SLocEntryCursor.ReadCode();
1290 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001291 unsigned RecCode
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001292 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001293
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001294 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001295 Error("AST record has invalid code");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001296 return Failure;
1297 }
1298
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001299 llvm::MemoryBuffer *Buffer
Chris Lattnera0a270c2010-04-05 22:42:27 +00001300 = llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(BlobStart, BlobLen - 1),
1301 Name);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001302 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset);
Mike Stump1eb44332009-09-09 15:08:12 +00001303
Douglas Gregor92b059e2009-04-28 20:33:11 +00001304 if (strcmp(Name, "<built-in>") == 0) {
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +00001305 PCHPredefinesBlock Block = {
1306 BufferID,
1307 llvm::StringRef(BlobStart, BlobLen - 1)
1308 };
1309 PCHPredefinesBuffers.push_back(Block);
Douglas Gregor92b059e2009-04-28 20:33:11 +00001310 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001311
1312 break;
1313 }
1314
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001315 case SM_SLOC_INSTANTIATION_ENTRY: {
Sebastian Redlc3632732010-10-05 15:59:54 +00001316 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001317 SourceMgr.createInstantiationLoc(SpellingLoc,
Sebastian Redlc3632732010-10-05 15:59:54 +00001318 ReadSourceLocation(*F, Record[2]),
1319 ReadSourceLocation(*F, Record[3]),
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001320 Record[4],
1321 ID,
1322 Record[0]);
1323 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001324 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001325 }
1326
1327 return Success;
1328}
1329
Chris Lattner6367f6d2009-04-27 01:05:14 +00001330/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1331/// specified cursor. Read the abbreviations that are at the top of the block
1332/// and then leave the cursor pointing into the block.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001333bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattner6367f6d2009-04-27 01:05:14 +00001334 unsigned BlockID) {
1335 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001336 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001337 return Failure;
1338 }
Mike Stump1eb44332009-09-09 15:08:12 +00001339
Chris Lattner6367f6d2009-04-27 01:05:14 +00001340 while (true) {
Douglas Gregorecdcb882010-10-20 22:00:55 +00001341 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattner6367f6d2009-04-27 01:05:14 +00001342 unsigned Code = Cursor.ReadCode();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001343
Chris Lattner6367f6d2009-04-27 01:05:14 +00001344 // We expect all abbrevs to be at the start of the block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001345 if (Code != llvm::bitc::DEFINE_ABBREV) {
1346 Cursor.JumpToBit(Offset);
Chris Lattner6367f6d2009-04-27 01:05:14 +00001347 return false;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001348 }
Chris Lattner6367f6d2009-04-27 01:05:14 +00001349 Cursor.ReadAbbrevRecord();
1350 }
1351}
1352
Sebastian Redlc3632732010-10-05 15:59:54 +00001353void ASTReader::ReadMacroRecord(PerFileData &F, uint64_t Offset) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001354 assert(PP && "Forgot to set Preprocessor ?");
Douglas Gregorecdcb882010-10-20 22:00:55 +00001355 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump1eb44332009-09-09 15:08:12 +00001356
Douglas Gregor37e26842009-04-21 23:56:24 +00001357 // Keep track of where we are in the stream, then jump back there
1358 // after reading this macro.
1359 SavedStreamPosition SavedPosition(Stream);
1360
1361 Stream.JumpToBit(Offset);
1362 RecordData Record;
1363 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1364 MacroInfo *Macro = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001365
Douglas Gregor37e26842009-04-21 23:56:24 +00001366 while (true) {
1367 unsigned Code = Stream.ReadCode();
1368 switch (Code) {
1369 case llvm::bitc::END_BLOCK:
1370 return;
1371
1372 case llvm::bitc::ENTER_SUBBLOCK:
1373 // No known subblocks, always skip them.
1374 Stream.ReadSubBlockID();
1375 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001376 Error("malformed block record in AST file");
Douglas Gregor37e26842009-04-21 23:56:24 +00001377 return;
1378 }
1379 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001380
Douglas Gregor37e26842009-04-21 23:56:24 +00001381 case llvm::bitc::DEFINE_ABBREV:
1382 Stream.ReadAbbrevRecord();
1383 continue;
1384 default: break;
1385 }
1386
1387 // Read a record.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001388 const char *BlobStart = 0;
1389 unsigned BlobLen = 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001390 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001391 PreprocessorRecordTypes RecType =
Michael J. Spencer20249a12010-10-21 03:16:25 +00001392 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregorecdcb882010-10-20 22:00:55 +00001393 BlobLen);
Douglas Gregor37e26842009-04-21 23:56:24 +00001394 switch (RecType) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001395 case PP_MACRO_OBJECT_LIKE:
1396 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001397 // If we already have a macro, that means that we've hit the end
1398 // of the definition of the macro we were looking for. We're
1399 // done.
1400 if (Macro)
1401 return;
1402
1403 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1404 if (II == 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001405 Error("macro must have a name in AST file");
Douglas Gregor37e26842009-04-21 23:56:24 +00001406 return;
1407 }
Sebastian Redlc3632732010-10-05 15:59:54 +00001408 SourceLocation Loc = ReadSourceLocation(F, Record[1]);
Douglas Gregor37e26842009-04-21 23:56:24 +00001409 bool isUsed = Record[2];
Mike Stump1eb44332009-09-09 15:08:12 +00001410
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001411 MacroInfo *MI = PP->AllocateMacroInfo(Loc);
Douglas Gregor37e26842009-04-21 23:56:24 +00001412 MI->setIsUsed(isUsed);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001413 MI->setIsFromAST();
Mike Stump1eb44332009-09-09 15:08:12 +00001414
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001415 unsigned NextIndex = 3;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001416 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregor37e26842009-04-21 23:56:24 +00001417 // Decode function-like macro info.
1418 bool isC99VarArgs = Record[3];
1419 bool isGNUVarArgs = Record[4];
1420 MacroArgs.clear();
1421 unsigned NumArgs = Record[5];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001422 NextIndex = 6 + NumArgs;
Douglas Gregor37e26842009-04-21 23:56:24 +00001423 for (unsigned i = 0; i != NumArgs; ++i)
1424 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1425
1426 // Install function-like macro info.
1427 MI->setIsFunctionLike();
1428 if (isC99VarArgs) MI->setIsC99Varargs();
1429 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor75fdb232009-05-22 22:45:36 +00001430 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001431 PP->getPreprocessorAllocator());
Douglas Gregor37e26842009-04-21 23:56:24 +00001432 }
1433
1434 // Finally, install the macro.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001435 PP->setMacroInfo(II, MI);
Douglas Gregor37e26842009-04-21 23:56:24 +00001436
1437 // Remember that we saw this macro last so that we add the tokens that
1438 // form its body to it.
1439 Macro = MI;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001440
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001441 if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) {
1442 // We have a macro definition. Load it now.
1443 PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro,
1444 getMacroDefinition(Record[NextIndex]));
1445 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001446
Douglas Gregor37e26842009-04-21 23:56:24 +00001447 ++NumMacrosRead;
1448 break;
1449 }
Mike Stump1eb44332009-09-09 15:08:12 +00001450
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001451 case PP_TOKEN: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001452 // If we see a TOKEN before a PP_MACRO_*, then the file is
1453 // erroneous, just pretend we didn't see this.
1454 if (Macro == 0) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001455
Douglas Gregor37e26842009-04-21 23:56:24 +00001456 Token Tok;
1457 Tok.startToken();
Sebastian Redlc3632732010-10-05 15:59:54 +00001458 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001459 Tok.setLength(Record[1]);
1460 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1461 Tok.setIdentifierInfo(II);
1462 Tok.setKind((tok::TokenKind)Record[3]);
1463 Tok.setFlag((Token::TokenFlags)Record[4]);
1464 Macro->AddTokenToBody(Tok);
1465 break;
1466 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001467
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001468 case PP_MACRO_INSTANTIATION: {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001469 // If we already have a macro, that means that we've hit the end
1470 // of the definition of the macro we were looking for. We're
1471 // done.
1472 if (Macro)
1473 return;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001474
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001475 if (!PP->getPreprocessingRecord()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001476 Error("missing preprocessing record in AST file");
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001477 return;
1478 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001479
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001480 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1481 if (PPRec.getPreprocessedEntity(Record[0]))
1482 return;
1483
1484 MacroInstantiation *MI
1485 = new (PPRec) MacroInstantiation(DecodeIdentifierInfo(Record[3]),
Sebastian Redlc3632732010-10-05 15:59:54 +00001486 SourceRange(ReadSourceLocation(F, Record[1]),
1487 ReadSourceLocation(F, Record[2])),
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001488 getMacroDefinition(Record[4]));
1489 PPRec.SetPreallocatedEntity(Record[0], MI);
1490 return;
1491 }
1492
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001493 case PP_MACRO_DEFINITION: {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001494 // If we already have a macro, that means that we've hit the end
1495 // of the definition of the macro we were looking for. We're
1496 // done.
1497 if (Macro)
1498 return;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001499
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001500 if (!PP->getPreprocessingRecord()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001501 Error("missing preprocessing record in AST file");
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001502 return;
1503 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001504
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001505 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1506 if (PPRec.getPreprocessedEntity(Record[0]))
1507 return;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001508
Douglas Gregor77424bc2010-10-02 19:29:26 +00001509 if (Record[1] > MacroDefinitionsLoaded.size()) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001510 Error("out-of-bounds macro definition record");
1511 return;
1512 }
1513
Douglas Gregor77424bc2010-10-02 19:29:26 +00001514 // Decode the identifier info and then check again; if the macro is
Michael J. Spencer20249a12010-10-21 03:16:25 +00001515 // still defined and associated with the identifier,
Douglas Gregor77424bc2010-10-02 19:29:26 +00001516 IdentifierInfo *II = DecodeIdentifierInfo(Record[4]);
1517 if (!MacroDefinitionsLoaded[Record[1] - 1]) {
1518 MacroDefinition *MD
1519 = new (PPRec) MacroDefinition(II,
Sebastian Redlc3632732010-10-05 15:59:54 +00001520 ReadSourceLocation(F, Record[5]),
Douglas Gregorb1a7d9a2010-10-01 20:33:34 +00001521 SourceRange(
Sebastian Redlc3632732010-10-05 15:59:54 +00001522 ReadSourceLocation(F, Record[2]),
1523 ReadSourceLocation(F, Record[3])));
Michael J. Spencer20249a12010-10-21 03:16:25 +00001524
Douglas Gregor77424bc2010-10-02 19:29:26 +00001525 PPRec.SetPreallocatedEntity(Record[0], MD);
1526 MacroDefinitionsLoaded[Record[1] - 1] = MD;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001527
Douglas Gregor77424bc2010-10-02 19:29:26 +00001528 if (DeserializationListener)
1529 DeserializationListener->MacroDefinitionRead(Record[1], MD);
1530 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001531
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001532 return;
1533 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001534
Douglas Gregorecdcb882010-10-20 22:00:55 +00001535 case PP_INCLUSION_DIRECTIVE: {
1536 // If we already have a macro, that means that we've hit the end
1537 // of the definition of the macro we were looking for. We're
1538 // done.
1539 if (Macro)
1540 return;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001541
Douglas Gregorecdcb882010-10-20 22:00:55 +00001542 if (!PP->getPreprocessingRecord()) {
1543 Error("missing preprocessing record in AST file");
1544 return;
1545 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001546
Douglas Gregorecdcb882010-10-20 22:00:55 +00001547 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1548 if (PPRec.getPreprocessedEntity(Record[0]))
1549 return;
1550
1551 const char *FullFileNameStart = BlobStart + Record[3];
Michael J. Spencer20249a12010-10-21 03:16:25 +00001552 const FileEntry *File
Douglas Gregorecdcb882010-10-20 22:00:55 +00001553 = PP->getFileManager().getFile(FullFileNameStart,
1554 FullFileNameStart + (BlobLen - Record[3]));
Michael J. Spencer20249a12010-10-21 03:16:25 +00001555
Douglas Gregorecdcb882010-10-20 22:00:55 +00001556 // FIXME: Stable encoding
1557 InclusionDirective::InclusionKind Kind
1558 = static_cast<InclusionDirective::InclusionKind>(Record[5]);
1559 InclusionDirective *ID
1560 = new (PPRec) InclusionDirective(Kind,
1561 llvm::StringRef(BlobStart, Record[3]),
1562 Record[4],
1563 File,
1564 SourceRange(ReadSourceLocation(F, Record[1]),
1565 ReadSourceLocation(F, Record[2])));
1566 PPRec.SetPreallocatedEntity(Record[0], ID);
1567 return;
1568 }
Sebastian Redlb57a6242010-09-27 22:18:47 +00001569 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001570 }
1571}
1572
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001573void ASTReader::ReadDefinedMacros() {
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001574 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redlc3632732010-10-05 15:59:54 +00001575 PerFileData &F = *Chain[N - I - 1];
1576 llvm::BitstreamCursor &MacroCursor = F.MacroCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001577
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001578 // If there was no preprocessor block, skip this file.
1579 if (!MacroCursor.getBitStreamReader())
1580 continue;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001581
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001582 llvm::BitstreamCursor Cursor = MacroCursor;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001583 Cursor.JumpToBit(F.MacroStartOffset);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001584
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001585 RecordData Record;
1586 while (true) {
Sebastian Redledadecc2010-09-28 02:55:49 +00001587 uint64_t Offset = Cursor.GetCurrentBitNo();
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001588 unsigned Code = Cursor.ReadCode();
Douglas Gregorecdcb882010-10-20 22:00:55 +00001589 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001590 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001591
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001592 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1593 // No known subblocks, always skip them.
1594 Cursor.ReadSubBlockID();
1595 if (Cursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001596 Error("malformed block record in AST file");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001597 return;
1598 }
1599 continue;
1600 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001601
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001602 if (Code == llvm::bitc::DEFINE_ABBREV) {
1603 Cursor.ReadAbbrevRecord();
1604 continue;
1605 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001606
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001607 // Read a record.
1608 const char *BlobStart;
1609 unsigned BlobLen;
1610 Record.clear();
1611 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1612 default: // Default behavior: ignore.
1613 break;
Douglas Gregor88a35862010-01-04 19:18:44 +00001614
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001615 case PP_MACRO_OBJECT_LIKE:
1616 case PP_MACRO_FUNCTION_LIKE:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001617 DecodeIdentifierInfo(Record[0]);
1618 break;
1619
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001620 case PP_TOKEN:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001621 // Ignore tokens.
1622 break;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001623
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001624 case PP_MACRO_INSTANTIATION:
1625 case PP_MACRO_DEFINITION:
Douglas Gregorecdcb882010-10-20 22:00:55 +00001626 case PP_INCLUSION_DIRECTIVE:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001627 // Read the macro record.
Sebastian Redledadecc2010-09-28 02:55:49 +00001628 // FIXME: That's a stupid way to do this. We should reuse this cursor.
Sebastian Redlc3632732010-10-05 15:59:54 +00001629 ReadMacroRecord(F, Offset);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001630 break;
1631 }
Douglas Gregor88a35862010-01-04 19:18:44 +00001632 }
1633 }
1634}
1635
Sebastian Redlf73c93f2010-09-15 19:54:06 +00001636MacroDefinition *ASTReader::getMacroDefinition(MacroID ID) {
Douglas Gregor77424bc2010-10-02 19:29:26 +00001637 if (ID == 0 || ID > MacroDefinitionsLoaded.size())
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001638 return 0;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001639
Douglas Gregor77424bc2010-10-02 19:29:26 +00001640 if (!MacroDefinitionsLoaded[ID - 1]) {
1641 unsigned Index = ID - 1;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001642 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1643 PerFileData &F = *Chain[N - I - 1];
1644 if (Index < F.LocalNumMacroDefinitions) {
Sebastian Redlc3632732010-10-05 15:59:54 +00001645 ReadMacroRecord(F, F.MacroDefinitionOffsets[Index]);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001646 break;
1647 }
1648 Index -= F.LocalNumMacroDefinitions;
1649 }
Douglas Gregor77424bc2010-10-02 19:29:26 +00001650 assert(MacroDefinitionsLoaded[ID - 1] && "Broken chain");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001651 }
1652
Douglas Gregor77424bc2010-10-02 19:29:26 +00001653 return MacroDefinitionsLoaded[ID - 1];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001654}
1655
Douglas Gregore650c8c2009-07-07 00:12:59 +00001656/// \brief If we are loading a relocatable PCH file, and the filename is
1657/// not an absolute path, add the system root to the beginning of the file
1658/// name.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001659void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001660 // If this is not a relocatable PCH file, there's nothing to do.
1661 if (!RelocatablePCH)
1662 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001663
Daniel Dunbard5b21972009-11-18 19:50:41 +00001664 if (Filename.empty() || llvm::sys::Path(Filename).isAbsolute())
Douglas Gregore650c8c2009-07-07 00:12:59 +00001665 return;
1666
Douglas Gregore650c8c2009-07-07 00:12:59 +00001667 if (isysroot == 0) {
1668 // If no system root was given, default to '/'
1669 Filename.insert(Filename.begin(), '/');
1670 return;
1671 }
Mike Stump1eb44332009-09-09 15:08:12 +00001672
Douglas Gregore650c8c2009-07-07 00:12:59 +00001673 unsigned Length = strlen(isysroot);
1674 if (isysroot[Length - 1] != '/')
1675 Filename.insert(Filename.begin(), '/');
Mike Stump1eb44332009-09-09 15:08:12 +00001676
Douglas Gregore650c8c2009-07-07 00:12:59 +00001677 Filename.insert(Filename.begin(), isysroot, isysroot + Length);
1678}
1679
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001680ASTReader::ASTReadResult
Sebastian Redl571db7f2010-08-18 23:56:56 +00001681ASTReader::ReadASTBlock(PerFileData &F) {
Sebastian Redl9137a522010-07-16 17:50:48 +00001682 llvm::BitstreamCursor &Stream = F.Stream;
1683
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001684 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001685 Error("malformed block record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001686 return Failure;
1687 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001688
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001689 // Read all of the records and blocks for the ASt file.
Douglas Gregor8038d512009-04-10 17:25:41 +00001690 RecordData Record;
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001691 bool First = true;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001692 while (!Stream.AtEndOfStream()) {
1693 unsigned Code = Stream.ReadCode();
1694 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001695 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001696 Error("error at end of module block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001697 return Failure;
1698 }
Chris Lattner7356a312009-04-11 21:15:38 +00001699
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001700 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001701 }
1702
1703 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1704 switch (Stream.ReadSubBlockID()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001705 case DECLTYPES_BLOCK_ID:
Chris Lattner6367f6d2009-04-27 01:05:14 +00001706 // We lazily load the decls block, but we want to set up the
1707 // DeclsCursor cursor to point into it. Clone our current bitcode
1708 // cursor to it, enter the block and read the abbrevs in that block.
1709 // With the main cursor, we just skip over it.
Sebastian Redl9137a522010-07-16 17:50:48 +00001710 F.DeclsCursor = Stream;
Chris Lattner6367f6d2009-04-27 01:05:14 +00001711 if (Stream.SkipBlock() || // Skip with the main cursor.
1712 // Read the abbrevs.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001713 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001714 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001715 return Failure;
1716 }
1717 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001718
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001719 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl9137a522010-07-16 17:50:48 +00001720 F.MacroCursor = Stream;
Douglas Gregor88a35862010-01-04 19:18:44 +00001721 if (PP)
1722 PP->setExternalSource(this);
1723
Douglas Gregorecdcb882010-10-20 22:00:55 +00001724 if (Stream.SkipBlock() ||
1725 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001726 Error("malformed block record in AST file");
Chris Lattner7356a312009-04-11 21:15:38 +00001727 return Failure;
1728 }
Douglas Gregorecdcb882010-10-20 22:00:55 +00001729 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattner7356a312009-04-11 21:15:38 +00001730 break;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001731
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001732 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001733 switch (ReadSourceManagerBlock(F)) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00001734 case Success:
1735 break;
1736
1737 case Failure:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001738 Error("malformed source manager block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001739 return Failure;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001740
1741 case IgnorePCH:
1742 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001743 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001744 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001745 }
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001746 First = false;
Douglas Gregor8038d512009-04-10 17:25:41 +00001747 continue;
1748 }
1749
1750 if (Code == llvm::bitc::DEFINE_ABBREV) {
1751 Stream.ReadAbbrevRecord();
1752 continue;
1753 }
1754
1755 // Read and process a record.
1756 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00001757 const char *BlobStart = 0;
1758 unsigned BlobLen = 0;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001759 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00001760 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001761 default: // Default behavior: ignore.
1762 break;
1763
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001764 case METADATA: {
1765 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1766 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001767 : diag::warn_pch_version_too_new);
1768 return IgnorePCH;
1769 }
1770
1771 RelocatablePCH = Record[4];
1772 if (Listener) {
1773 std::string TargetTriple(BlobStart, BlobLen);
1774 if (Listener->ReadTargetTriple(TargetTriple))
1775 return IgnorePCH;
1776 }
1777 break;
1778 }
1779
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001780 case CHAINED_METADATA: {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001781 if (!First) {
1782 Error("CHAINED_METADATA is not first record in block");
1783 return Failure;
1784 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001785 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1786 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001787 : diag::warn_pch_version_too_new);
1788 return IgnorePCH;
1789 }
1790
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00001791 // Load the chained file, which is always a PCH file.
1792 switch(ReadASTCore(llvm::StringRef(BlobStart, BlobLen), PCH)) {
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001793 case Failure: return Failure;
1794 // If we have to ignore the dependency, we'll have to ignore this too.
1795 case IgnorePCH: return IgnorePCH;
1796 case Success: break;
1797 }
1798 break;
1799 }
1800
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001801 case TYPE_OFFSET:
Sebastian Redl12d6da02010-07-19 22:06:55 +00001802 if (F.LocalNumTypes != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001803 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001804 return Failure;
1805 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00001806 F.TypeOffsets = (const uint32_t *)BlobStart;
1807 F.LocalNumTypes = Record[0];
Douglas Gregor8038d512009-04-10 17:25:41 +00001808 break;
1809
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001810 case DECL_OFFSET:
Sebastian Redl12d6da02010-07-19 22:06:55 +00001811 if (F.LocalNumDecls != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001812 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001813 return Failure;
1814 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00001815 F.DeclOffsets = (const uint32_t *)BlobStart;
1816 F.LocalNumDecls = Record[0];
Douglas Gregor8038d512009-04-10 17:25:41 +00001817 break;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001818
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001819 case TU_UPDATE_LEXICAL: {
Sebastian Redld692af72010-07-27 18:24:41 +00001820 DeclContextInfo Info = {
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001821 /* No visible information */ 0,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001822 reinterpret_cast<const KindDeclIDPair *>(BlobStart),
1823 BlobLen / sizeof(KindDeclIDPair)
Sebastian Redld692af72010-07-27 18:24:41 +00001824 };
Douglas Gregor3747ee72010-10-01 01:18:02 +00001825 DeclContextOffsets[Context ? Context->getTranslationUnitDecl() : 0]
1826 .push_back(Info);
Sebastian Redld692af72010-07-27 18:24:41 +00001827 break;
1828 }
1829
Sebastian Redle1dde812010-08-24 00:50:04 +00001830 case UPDATE_VISIBLE: {
1831 serialization::DeclID ID = Record[0];
1832 void *Table = ASTDeclContextNameLookupTable::Create(
1833 (const unsigned char *)BlobStart + Record[1],
1834 (const unsigned char *)BlobStart,
1835 ASTDeclContextNameLookupTrait(*this));
Douglas Gregor3747ee72010-10-01 01:18:02 +00001836 if (ID == 1 && Context) { // Is it the TU?
Sebastian Redle1dde812010-08-24 00:50:04 +00001837 DeclContextInfo Info = {
1838 Table, /* No lexical inforamtion */ 0, 0
1839 };
1840 DeclContextOffsets[Context->getTranslationUnitDecl()].push_back(Info);
1841 } else
1842 PendingVisibleUpdates[ID].push_back(Table);
1843 break;
1844 }
1845
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001846 case REDECLS_UPDATE_LATEST: {
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00001847 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
1848 for (unsigned i = 0, e = Record.size(); i < e; i += 2) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001849 DeclID First = Record[i], Latest = Record[i+1];
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00001850 assert((FirstLatestDeclIDs.find(First) == FirstLatestDeclIDs.end() ||
1851 Latest > FirstLatestDeclIDs[First]) &&
1852 "The new latest is supposed to come after the previous latest");
1853 FirstLatestDeclIDs[First] = Latest;
1854 }
1855 break;
1856 }
1857
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001858 case LANGUAGE_OPTIONS:
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001859 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001860 return IgnorePCH;
1861 break;
Douglas Gregor2bec0412009-04-10 21:16:55 +00001862
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001863 case IDENTIFIER_TABLE:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001864 F.IdentifierTableData = BlobStart;
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001865 if (Record[0]) {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001866 F.IdentifierLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001867 = ASTIdentifierLookupTable::Create(
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001868 (const unsigned char *)F.IdentifierTableData + Record[0],
1869 (const unsigned char *)F.IdentifierTableData,
Sebastian Redlc3632732010-10-05 15:59:54 +00001870 ASTIdentifierLookupTrait(*this, F));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001871 if (PP)
1872 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001873 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001874 break;
1875
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001876 case IDENTIFIER_OFFSET:
Sebastian Redl2da08f92010-07-19 22:28:42 +00001877 if (F.LocalNumIdentifiers != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001878 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00001879 return Failure;
1880 }
Sebastian Redl2da08f92010-07-19 22:28:42 +00001881 F.IdentifierOffsets = (const uint32_t *)BlobStart;
1882 F.LocalNumIdentifiers = Record[0];
Douglas Gregorafaf3082009-04-11 00:14:32 +00001883 break;
Douglas Gregorfdd01722009-04-14 00:24:19 +00001884
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001885 case EXTERNAL_DEFINITIONS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001886 // Optimization for the first block.
1887 if (ExternalDefinitions.empty())
1888 ExternalDefinitions.swap(Record);
1889 else
1890 ExternalDefinitions.insert(ExternalDefinitions.end(),
1891 Record.begin(), Record.end());
Douglas Gregorfdd01722009-04-14 00:24:19 +00001892 break;
Douglas Gregor3e1af842009-04-17 22:13:46 +00001893
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001894 case SPECIAL_TYPES:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001895 // Optimization for the first block
1896 if (SpecialTypes.empty())
1897 SpecialTypes.swap(Record);
1898 else
1899 SpecialTypes.insert(SpecialTypes.end(), Record.begin(), Record.end());
Douglas Gregorad1de002009-04-18 05:55:16 +00001900 break;
1901
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001902 case STATISTICS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001903 TotalNumStatements += Record[0];
1904 TotalNumMacros += Record[1];
1905 TotalLexicalDeclContexts += Record[2];
1906 TotalVisibleDeclContexts += Record[3];
Douglas Gregor3e1af842009-04-17 22:13:46 +00001907 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001908
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001909 case TENTATIVE_DEFINITIONS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001910 // Optimization for the first block.
1911 if (TentativeDefinitions.empty())
1912 TentativeDefinitions.swap(Record);
1913 else
1914 TentativeDefinitions.insert(TentativeDefinitions.end(),
1915 Record.begin(), Record.end());
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00001916 break;
Douglas Gregor14c22f22009-04-22 22:18:58 +00001917
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001918 case UNUSED_FILESCOPED_DECLS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001919 // Optimization for the first block.
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00001920 if (UnusedFileScopedDecls.empty())
1921 UnusedFileScopedDecls.swap(Record);
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001922 else
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00001923 UnusedFileScopedDecls.insert(UnusedFileScopedDecls.end(),
1924 Record.begin(), Record.end());
Tanya Lattnere6bbc012010-02-12 00:07:30 +00001925 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001926
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001927 case WEAK_UNDECLARED_IDENTIFIERS:
Sebastian Redl40566802010-08-05 18:21:25 +00001928 // Later blocks overwrite earlier ones.
1929 WeakUndeclaredIdentifiers.swap(Record);
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00001930 break;
1931
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001932 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001933 // Optimization for the first block.
1934 if (LocallyScopedExternalDecls.empty())
1935 LocallyScopedExternalDecls.swap(Record);
1936 else
1937 LocallyScopedExternalDecls.insert(LocallyScopedExternalDecls.end(),
1938 Record.begin(), Record.end());
Douglas Gregor14c22f22009-04-22 22:18:58 +00001939 break;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001940
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001941 case SELECTOR_OFFSETS:
Sebastian Redl059612d2010-08-03 21:58:15 +00001942 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redl725cd962010-08-04 20:40:17 +00001943 F.LocalNumSelectors = Record[0];
Douglas Gregor83941df2009-04-25 17:48:32 +00001944 break;
1945
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001946 case METHOD_POOL:
Sebastian Redl725cd962010-08-04 20:40:17 +00001947 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor83941df2009-04-25 17:48:32 +00001948 if (Record[0])
Sebastian Redl725cd962010-08-04 20:40:17 +00001949 F.SelectorLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001950 = ASTSelectorLookupTable::Create(
Sebastian Redl725cd962010-08-04 20:40:17 +00001951 F.SelectorLookupTableData + Record[0],
1952 F.SelectorLookupTableData,
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001953 ASTSelectorLookupTrait(*this));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00001954 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001955 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001956
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00001957 case REFERENCED_SELECTOR_POOL:
Sebastian Redlc3632732010-10-05 15:59:54 +00001958 F.ReferencedSelectorsData.swap(Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00001959 break;
1960
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001961 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001962 if (!Record.empty() && Listener)
1963 Listener->ReadCounter(Record[0]);
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001964 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001965
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001966 case SOURCE_LOCATION_OFFSETS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001967 F.SLocOffsets = (const uint32_t *)BlobStart;
1968 F.LocalNumSLocEntries = Record[0];
Sebastian Redl8db9fae2010-09-22 20:19:08 +00001969 F.LocalSLocSize = Record[1];
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001970 break;
1971
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001972 case SOURCE_LOCATION_PRELOADS:
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00001973 if (PreloadSLocEntries.empty())
1974 PreloadSLocEntries.swap(Record);
1975 else
1976 PreloadSLocEntries.insert(PreloadSLocEntries.end(),
1977 Record.begin(), Record.end());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001978 break;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001979
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001980 case STAT_CACHE: {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001981 ASTStatCache *MyStatCache =
1982 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
Douglas Gregor52e71082009-10-16 18:18:30 +00001983 (const unsigned char *)BlobStart,
1984 NumStatHits, NumStatMisses);
1985 FileMgr.addStatCache(MyStatCache);
Sebastian Redl9137a522010-07-16 17:50:48 +00001986 F.StatCache = MyStatCache;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001987 break;
Douglas Gregor52e71082009-10-16 18:18:30 +00001988 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001989
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001990 case EXT_VECTOR_DECLS:
Sebastian Redla9f23682010-07-28 21:38:49 +00001991 // Optimization for the first block.
1992 if (ExtVectorDecls.empty())
1993 ExtVectorDecls.swap(Record);
1994 else
1995 ExtVectorDecls.insert(ExtVectorDecls.end(),
1996 Record.begin(), Record.end());
Douglas Gregorb81c1702009-04-27 20:06:05 +00001997 break;
1998
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001999 case VTABLE_USES:
Sebastian Redl40566802010-08-05 18:21:25 +00002000 // Later tables overwrite earlier ones.
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002001 VTableUses.swap(Record);
2002 break;
2003
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002004 case DYNAMIC_CLASSES:
Sebastian Redl40566802010-08-05 18:21:25 +00002005 // Optimization for the first block.
2006 if (DynamicClasses.empty())
2007 DynamicClasses.swap(Record);
2008 else
2009 DynamicClasses.insert(DynamicClasses.end(),
2010 Record.begin(), Record.end());
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002011 break;
2012
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002013 case PENDING_IMPLICIT_INSTANTIATIONS:
Sebastian Redlc3632732010-10-05 15:59:54 +00002014 F.PendingInstantiations.swap(Record);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002015 break;
2016
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002017 case SEMA_DECL_REFS:
Sebastian Redl40566802010-08-05 18:21:25 +00002018 // Later tables overwrite earlier ones.
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002019 SemaDeclRefs.swap(Record);
2020 break;
2021
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002022 case ORIGINAL_FILE_NAME:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002023 // The primary AST will be the last to get here, so it will be the one
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002024 // that's used.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +00002025 ActualOriginalFileName.assign(BlobStart, BlobLen);
2026 OriginalFileName = ActualOriginalFileName;
Douglas Gregore650c8c2009-07-07 00:12:59 +00002027 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregorb64c1932009-05-12 01:31:05 +00002028 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002029
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002030 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek974be4d2010-02-12 23:31:14 +00002031 const std::string &CurBranch = getClangFullRepositoryVersion();
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002032 llvm::StringRef ASTBranch(BlobStart, BlobLen);
2033 if (llvm::StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2034 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregor445e23e2009-10-05 21:07:28 +00002035 return IgnorePCH;
2036 }
2037 break;
2038 }
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002039
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002040 case MACRO_DEFINITION_OFFSETS:
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002041 F.MacroDefinitionOffsets = (const uint32_t *)BlobStart;
2042 F.NumPreallocatedPreprocessingEntities = Record[0];
2043 F.LocalNumMacroDefinitions = Record[1];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002044 break;
Sebastian Redl0b17c612010-08-13 00:28:03 +00002045
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002046 case DECL_REPLACEMENTS: {
Sebastian Redl0b17c612010-08-13 00:28:03 +00002047 if (Record.size() % 2 != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002048 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redl0b17c612010-08-13 00:28:03 +00002049 return Failure;
2050 }
2051 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002052 ReplacedDecls[static_cast<DeclID>(Record[I])] =
Sebastian Redl0b17c612010-08-13 00:28:03 +00002053 std::make_pair(&F, Record[I+1]);
2054 break;
2055 }
Sebastian Redl6e50e002010-08-24 22:50:19 +00002056
2057 case ADDITIONAL_TEMPLATE_SPECIALIZATIONS: {
2058 AdditionalTemplateSpecializations &ATS =
2059 AdditionalTemplateSpecializationsPending[Record[0]];
2060 ATS.insert(ATS.end(), Record.begin()+1, Record.end());
2061 break;
2062 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002063 }
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002064 First = false;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002065 }
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002066 Error("premature end of bitstream in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002067 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002068}
2069
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002070ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
2071 ASTFileType Type) {
2072 switch(ReadASTCore(FileName, Type)) {
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002073 case Failure: return Failure;
2074 case IgnorePCH: return IgnorePCH;
2075 case Success: break;
2076 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002077
2078 // Here comes stuff that we only do once the entire chain is loaded.
2079
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002080 // Allocate space for loaded slocentries, identifiers, decls and types.
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002081 unsigned TotalNumIdentifiers = 0, TotalNumTypes = 0, TotalNumDecls = 0,
Sebastian Redl725cd962010-08-04 20:40:17 +00002082 TotalNumPreallocatedPreprocessingEntities = 0, TotalNumMacroDefs = 0,
2083 TotalNumSelectors = 0;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002084 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002085 TotalNumSLocEntries += Chain[I]->LocalNumSLocEntries;
Sebastian Redl8db9fae2010-09-22 20:19:08 +00002086 NextSLocOffset += Chain[I]->LocalSLocSize;
Sebastian Redl2da08f92010-07-19 22:28:42 +00002087 TotalNumIdentifiers += Chain[I]->LocalNumIdentifiers;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002088 TotalNumTypes += Chain[I]->LocalNumTypes;
2089 TotalNumDecls += Chain[I]->LocalNumDecls;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002090 TotalNumPreallocatedPreprocessingEntities +=
2091 Chain[I]->NumPreallocatedPreprocessingEntities;
2092 TotalNumMacroDefs += Chain[I]->LocalNumMacroDefinitions;
Sebastian Redl725cd962010-08-04 20:40:17 +00002093 TotalNumSelectors += Chain[I]->LocalNumSelectors;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002094 }
Sebastian Redl8db9fae2010-09-22 20:19:08 +00002095 SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, NextSLocOffset);
Sebastian Redl2da08f92010-07-19 22:28:42 +00002096 IdentifiersLoaded.resize(TotalNumIdentifiers);
Sebastian Redl12d6da02010-07-19 22:06:55 +00002097 TypesLoaded.resize(TotalNumTypes);
2098 DeclsLoaded.resize(TotalNumDecls);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002099 MacroDefinitionsLoaded.resize(TotalNumMacroDefs);
2100 if (PP) {
2101 if (TotalNumIdentifiers > 0)
2102 PP->getHeaderSearchInfo().SetExternalLookup(this);
2103 if (TotalNumPreallocatedPreprocessingEntities > 0) {
2104 if (!PP->getPreprocessingRecord())
2105 PP->createPreprocessingRecord();
2106 PP->getPreprocessingRecord()->SetExternalSource(*this,
2107 TotalNumPreallocatedPreprocessingEntities);
2108 }
2109 }
Sebastian Redl725cd962010-08-04 20:40:17 +00002110 SelectorsLoaded.resize(TotalNumSelectors);
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002111 // Preload SLocEntries.
2112 for (unsigned I = 0, N = PreloadSLocEntries.size(); I != N; ++I) {
2113 ASTReadResult Result = ReadSLocEntryRecord(PreloadSLocEntries[I]);
2114 if (Result != Success)
2115 return Result;
2116 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00002117
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002118 // Check the predefines buffers.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00002119 if (!DisableValidation && CheckPredefinesBuffers())
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002120 return IgnorePCH;
2121
2122 if (PP) {
2123 // Initialization of keywords and pragmas occurs before the
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002124 // AST file is read, so there may be some identifiers that were
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002125 // loaded into the IdentifierTable before we intercepted the
2126 // creation of identifiers. Iterate through the list of known
2127 // identifiers and determine whether we have to establish
2128 // preprocessor definitions or top-level identifier declaration
2129 // chains for those identifiers.
2130 //
2131 // We copy the IdentifierInfo pointers to a small vector first,
2132 // since de-serializing declarations or macro definitions can add
2133 // new entries into the identifier table, invalidating the
2134 // iterators.
2135 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
2136 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
2137 IdEnd = PP->getIdentifierTable().end();
2138 Id != IdEnd; ++Id)
2139 Identifiers.push_back(Id->second);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002140 // We need to search the tables in all files.
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002141 for (unsigned J = 0, M = Chain.size(); J != M; ++J) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002142 ASTIdentifierLookupTable *IdTable
2143 = (ASTIdentifierLookupTable *)Chain[J]->IdentifierLookupTable;
2144 // Not all AST files necessarily have identifier tables, only the useful
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00002145 // ones.
2146 if (!IdTable)
2147 continue;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002148 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2149 IdentifierInfo *II = Identifiers[I];
2150 // Look in the on-disk hash tables for an entry for this identifier
Sebastian Redlc3632732010-10-05 15:59:54 +00002151 ASTIdentifierLookupTrait Info(*this, *Chain[J], II);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002152 std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength());
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002153 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002154 if (Pos == IdTable->end())
2155 continue;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002156
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002157 // Dereferencing the iterator has the effect of populating the
2158 // IdentifierInfo node with the various declarations it needs.
2159 (void)*Pos;
2160 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002161 }
2162 }
2163
2164 if (Context)
2165 InitializeContext(*Context);
2166
2167 return Success;
2168}
2169
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002170ASTReader::ASTReadResult ASTReader::ReadASTCore(llvm::StringRef FileName,
2171 ASTFileType Type) {
Sebastian Redla866e652010-10-01 19:59:12 +00002172 PerFileData *Prev = Chain.empty() ? 0 : Chain.back();
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002173 Chain.push_back(new PerFileData(Type));
Sebastian Redl9137a522010-07-16 17:50:48 +00002174 PerFileData &F = *Chain.back();
Sebastian Redla866e652010-10-01 19:59:12 +00002175 if (Prev)
2176 Prev->NextInSource = &F;
2177 else
2178 FirstInSource = &F;
2179 F.Loaders.push_back(Prev);
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002180
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002181 // Set the AST file name.
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002182 F.FileName = FileName;
2183
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002184 // Open the AST file.
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002185 //
2186 // FIXME: This shouldn't be here, we should just take a raw_ostream.
2187 std::string ErrStr;
2188 F.Buffer.reset(llvm::MemoryBuffer::getFileOrSTDIN(FileName, &ErrStr));
2189 if (!F.Buffer) {
2190 Error(ErrStr.c_str());
2191 return IgnorePCH;
2192 }
2193
2194 // Initialize the stream
2195 F.StreamFile.init((const unsigned char *)F.Buffer->getBufferStart(),
2196 (const unsigned char *)F.Buffer->getBufferEnd());
Sebastian Redl9137a522010-07-16 17:50:48 +00002197 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002198 Stream.init(F.StreamFile);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002199 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002200
2201 // Sniff for the signature.
2202 if (Stream.Read(8) != 'C' ||
2203 Stream.Read(8) != 'P' ||
2204 Stream.Read(8) != 'C' ||
2205 Stream.Read(8) != 'H') {
2206 Diag(diag::err_not_a_pch_file) << FileName;
2207 return Failure;
2208 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002209
Douglas Gregor2cf26342009-04-09 22:27:44 +00002210 while (!Stream.AtEndOfStream()) {
2211 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002212
Douglas Gregore1d918e2009-04-10 23:10:45 +00002213 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002214 Error("invalid record at top-level of AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002215 return Failure;
2216 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002217
2218 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregor668c1a42009-04-21 22:25:48 +00002219
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002220 // We only know the AST subblock ID.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002221 switch (BlockID) {
2222 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002223 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002224 Error("malformed BlockInfoBlock in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002225 return Failure;
2226 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002227 break;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002228 case AST_BLOCK_ID:
Sebastian Redl571db7f2010-08-18 23:56:56 +00002229 switch (ReadASTBlock(F)) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002230 case Success:
2231 break;
2232
2233 case Failure:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002234 return Failure;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002235
2236 case IgnorePCH:
Douglas Gregor2bec0412009-04-10 21:16:55 +00002237 // FIXME: We could consider reading through to the end of this
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002238 // AST block, skipping subblocks, to see if there are other
2239 // AST blocks elsewhere.
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002240
2241 // Clear out any preallocated source location entries, so that
2242 // the source manager does not try to resolve them later.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002243 SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002244
2245 // Remove the stat cache.
Sebastian Redl9137a522010-07-16 17:50:48 +00002246 if (F.StatCache)
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002247 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002248
Douglas Gregore1d918e2009-04-10 23:10:45 +00002249 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002250 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002251 break;
2252 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002253 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002254 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002255 return Failure;
2256 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002257 break;
2258 }
Mike Stump1eb44332009-09-09 15:08:12 +00002259 }
2260
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002261 return Success;
2262}
2263
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002264void ASTReader::setPreprocessor(Preprocessor &pp) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002265 PP = &pp;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002266
2267 unsigned TotalNum = 0;
2268 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
2269 TotalNum += Chain[I]->NumPreallocatedPreprocessingEntities;
2270 if (TotalNum) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002271 if (!PP->getPreprocessingRecord())
2272 PP->createPreprocessingRecord();
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002273 PP->getPreprocessingRecord()->SetExternalSource(*this, TotalNum);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002274 }
2275}
2276
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002277void ASTReader::InitializeContext(ASTContext &Ctx) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002278 Context = &Ctx;
2279 assert(Context && "Passed null context!");
2280
2281 assert(PP && "Forgot to set Preprocessor ?");
2282 PP->getIdentifierTable().setExternalIdentifierLookup(this);
2283 PP->getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor88a35862010-01-04 19:18:44 +00002284 PP->setExternalSource(this);
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002285
Douglas Gregor3747ee72010-10-01 01:18:02 +00002286 // If we have an update block for the TU waiting, we have to add it before
2287 // deserializing the decl.
2288 DeclContextOffsetsMap::iterator DCU = DeclContextOffsets.find(0);
2289 if (DCU != DeclContextOffsets.end()) {
2290 // Insertion could invalidate map, so grab vector.
2291 DeclContextInfos T;
2292 T.swap(DCU->second);
2293 DeclContextOffsets.erase(DCU);
2294 DeclContextOffsets[Ctx.getTranslationUnitDecl()].swap(T);
2295 }
2296
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002297 // Load the translation unit declaration
Argyrios Kyrtzidis8871a442010-07-08 17:13:02 +00002298 GetTranslationUnitDecl();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002299
2300 // Load the special types.
2301 Context->setBuiltinVaListType(
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002302 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
2303 if (unsigned Id = SpecialTypes[SPECIAL_TYPE_OBJC_ID])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002304 Context->setObjCIdType(GetType(Id));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002305 if (unsigned Sel = SpecialTypes[SPECIAL_TYPE_OBJC_SELECTOR])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002306 Context->setObjCSelType(GetType(Sel));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002307 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002308 Context->setObjCProtoType(GetType(Proto));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002309 if (unsigned Class = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002310 Context->setObjCClassType(GetType(Class));
Steve Naroff14108da2009-07-10 23:34:53 +00002311
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002312 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002313 Context->setCFConstantStringType(GetType(String));
Mike Stump1eb44332009-09-09 15:08:12 +00002314 if (unsigned FastEnum
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002315 = SpecialTypes[SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002316 Context->setObjCFastEnumerationStateType(GetType(FastEnum));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002317 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002318 QualType FileType = GetType(File);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002319 if (FileType.isNull()) {
2320 Error("FILE type is NULL");
2321 return;
2322 }
John McCall183700f2009-09-21 23:43:11 +00002323 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002324 Context->setFILEDecl(Typedef->getDecl());
2325 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002326 const TagType *Tag = FileType->getAs<TagType>();
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002327 if (!Tag) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002328 Error("Invalid FILE type in AST file");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002329 return;
2330 }
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002331 Context->setFILEDecl(Tag->getDecl());
2332 }
2333 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002334 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
Mike Stump782fa302009-07-28 02:25:19 +00002335 QualType Jmp_bufType = GetType(Jmp_buf);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002336 if (Jmp_bufType.isNull()) {
2337 Error("jmp_bug type is NULL");
2338 return;
2339 }
John McCall183700f2009-09-21 23:43:11 +00002340 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
Mike Stump782fa302009-07-28 02:25:19 +00002341 Context->setjmp_bufDecl(Typedef->getDecl());
2342 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002343 const TagType *Tag = Jmp_bufType->getAs<TagType>();
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002344 if (!Tag) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002345 Error("Invalid jmp_buf type in AST file");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002346 return;
2347 }
Mike Stump782fa302009-07-28 02:25:19 +00002348 Context->setjmp_bufDecl(Tag->getDecl());
2349 }
2350 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002351 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
Mike Stump782fa302009-07-28 02:25:19 +00002352 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002353 if (Sigjmp_bufType.isNull()) {
2354 Error("sigjmp_buf type is NULL");
2355 return;
2356 }
John McCall183700f2009-09-21 23:43:11 +00002357 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
Mike Stump782fa302009-07-28 02:25:19 +00002358 Context->setsigjmp_bufDecl(Typedef->getDecl());
2359 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002360 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002361 assert(Tag && "Invalid sigjmp_buf type in AST file");
Mike Stump782fa302009-07-28 02:25:19 +00002362 Context->setsigjmp_bufDecl(Tag->getDecl());
2363 }
2364 }
Mike Stump1eb44332009-09-09 15:08:12 +00002365 if (unsigned ObjCIdRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002366 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION])
Douglas Gregord1571ac2009-08-21 00:27:50 +00002367 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
Mike Stump1eb44332009-09-09 15:08:12 +00002368 if (unsigned ObjCClassRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002369 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION])
Douglas Gregord1571ac2009-08-21 00:27:50 +00002370 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002371 if (unsigned String = SpecialTypes[SPECIAL_TYPE_BLOCK_DESCRIPTOR])
Mike Stumpadaaad32009-10-20 02:12:22 +00002372 Context->setBlockDescriptorType(GetType(String));
Mike Stump083c25e2009-10-22 00:49:09 +00002373 if (unsigned String
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002374 = SpecialTypes[SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR])
Mike Stump083c25e2009-10-22 00:49:09 +00002375 Context->setBlockDescriptorExtendedType(GetType(String));
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002376 if (unsigned ObjCSelRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002377 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION])
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002378 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002379 if (unsigned String = SpecialTypes[SPECIAL_TYPE_NS_CONSTANT_STRING])
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002380 Context->setNSConstantStringType(GetType(String));
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002381
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002382 if (SpecialTypes[SPECIAL_TYPE_INT128_INSTALLED])
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002383 Context->setInt128Installed();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002384}
2385
Douglas Gregorb64c1932009-05-12 01:31:05 +00002386/// \brief Retrieve the name of the original source file name
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002387/// directly from the AST file, without actually loading the AST
Douglas Gregorb64c1932009-05-12 01:31:05 +00002388/// file.
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002389std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00002390 Diagnostic &Diags) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002391 // Open the AST file.
Douglas Gregorb64c1932009-05-12 01:31:05 +00002392 std::string ErrStr;
2393 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002394 Buffer.reset(llvm::MemoryBuffer::getFile(ASTFileName.c_str(), &ErrStr));
Douglas Gregorb64c1932009-05-12 01:31:05 +00002395 if (!Buffer) {
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00002396 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002397 return std::string();
2398 }
2399
2400 // Initialize the stream
2401 llvm::BitstreamReader StreamFile;
2402 llvm::BitstreamCursor Stream;
Mike Stump1eb44332009-09-09 15:08:12 +00002403 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregorb64c1932009-05-12 01:31:05 +00002404 (const unsigned char *)Buffer->getBufferEnd());
2405 Stream.init(StreamFile);
2406
2407 // Sniff for the signature.
2408 if (Stream.Read(8) != 'C' ||
2409 Stream.Read(8) != 'P' ||
2410 Stream.Read(8) != 'C' ||
2411 Stream.Read(8) != 'H') {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002412 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002413 return std::string();
2414 }
2415
2416 RecordData Record;
2417 while (!Stream.AtEndOfStream()) {
2418 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002419
Douglas Gregorb64c1932009-05-12 01:31:05 +00002420 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2421 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump1eb44332009-09-09 15:08:12 +00002422
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002423 // We only know the AST subblock ID.
Douglas Gregorb64c1932009-05-12 01:31:05 +00002424 switch (BlockID) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002425 case AST_BLOCK_ID:
2426 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002427 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002428 return std::string();
2429 }
2430 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002431
Douglas Gregorb64c1932009-05-12 01:31:05 +00002432 default:
2433 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002434 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002435 return std::string();
2436 }
2437 break;
2438 }
2439 continue;
2440 }
2441
2442 if (Code == llvm::bitc::END_BLOCK) {
2443 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002444 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002445 return std::string();
2446 }
2447 continue;
2448 }
2449
2450 if (Code == llvm::bitc::DEFINE_ABBREV) {
2451 Stream.ReadAbbrevRecord();
2452 continue;
2453 }
2454
2455 Record.clear();
2456 const char *BlobStart = 0;
2457 unsigned BlobLen = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002458 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002459 == ORIGINAL_FILE_NAME)
Douglas Gregorb64c1932009-05-12 01:31:05 +00002460 return std::string(BlobStart, BlobLen);
Mike Stump1eb44332009-09-09 15:08:12 +00002461 }
Douglas Gregorb64c1932009-05-12 01:31:05 +00002462
2463 return std::string();
2464}
2465
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002466/// \brief Parse the record that corresponds to a LangOptions data
2467/// structure.
2468///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002469/// This routine parses the language options from the AST file and then gives
2470/// them to the AST listener if one is set.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002471///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002472/// \returns true if the listener deems the file unacceptable, false otherwise.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002473bool ASTReader::ParseLanguageOptions(
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002474 const llvm::SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002475 if (Listener) {
2476 LangOptions LangOpts;
Mike Stump1eb44332009-09-09 15:08:12 +00002477
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002478 #define PARSE_LANGOPT(Option) \
2479 LangOpts.Option = Record[Idx]; \
2480 ++Idx
Mike Stump1eb44332009-09-09 15:08:12 +00002481
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002482 unsigned Idx = 0;
2483 PARSE_LANGOPT(Trigraphs);
2484 PARSE_LANGOPT(BCPLComment);
2485 PARSE_LANGOPT(DollarIdents);
2486 PARSE_LANGOPT(AsmPreprocessor);
2487 PARSE_LANGOPT(GNUMode);
Chandler Carrutheb5d7b72010-04-17 20:17:31 +00002488 PARSE_LANGOPT(GNUKeywords);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002489 PARSE_LANGOPT(ImplicitInt);
2490 PARSE_LANGOPT(Digraphs);
2491 PARSE_LANGOPT(HexFloats);
2492 PARSE_LANGOPT(C99);
2493 PARSE_LANGOPT(Microsoft);
2494 PARSE_LANGOPT(CPlusPlus);
2495 PARSE_LANGOPT(CPlusPlus0x);
2496 PARSE_LANGOPT(CXXOperatorNames);
2497 PARSE_LANGOPT(ObjC1);
2498 PARSE_LANGOPT(ObjC2);
2499 PARSE_LANGOPT(ObjCNonFragileABI);
Fariborz Jahanian412e7982010-02-09 19:31:38 +00002500 PARSE_LANGOPT(ObjCNonFragileABI2);
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +00002501 PARSE_LANGOPT(NoConstantCFStrings);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002502 PARSE_LANGOPT(PascalStrings);
2503 PARSE_LANGOPT(WritableStrings);
2504 PARSE_LANGOPT(LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +00002505 PARSE_LANGOPT(AltiVec);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002506 PARSE_LANGOPT(Exceptions);
Daniel Dunbar73482882010-02-10 18:48:44 +00002507 PARSE_LANGOPT(SjLjExceptions);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002508 PARSE_LANGOPT(NeXTRuntime);
2509 PARSE_LANGOPT(Freestanding);
2510 PARSE_LANGOPT(NoBuiltin);
2511 PARSE_LANGOPT(ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +00002512 PARSE_LANGOPT(POSIXThreads);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002513 PARSE_LANGOPT(Blocks);
2514 PARSE_LANGOPT(EmitAllDecls);
2515 PARSE_LANGOPT(MathErrno);
Chris Lattnera4d71452010-06-26 21:25:03 +00002516 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
2517 Record[Idx++]);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002518 PARSE_LANGOPT(HeinousExtensions);
2519 PARSE_LANGOPT(Optimize);
2520 PARSE_LANGOPT(OptimizeSize);
2521 PARSE_LANGOPT(Static);
2522 PARSE_LANGOPT(PICLevel);
2523 PARSE_LANGOPT(GNUInline);
2524 PARSE_LANGOPT(NoInline);
2525 PARSE_LANGOPT(AccessControl);
2526 PARSE_LANGOPT(CharIsSigned);
John Thompsona6fda122009-11-05 20:14:16 +00002527 PARSE_LANGOPT(ShortWChar);
Chris Lattnera4d71452010-06-26 21:25:03 +00002528 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
2529 LangOpts.setVisibilityMode((LangOptions::VisibilityMode)Record[Idx++]);
Daniel Dunbarab8e2812009-09-21 04:16:19 +00002530 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
Chris Lattnera4d71452010-06-26 21:25:03 +00002531 Record[Idx++]);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002532 PARSE_LANGOPT(InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +00002533 PARSE_LANGOPT(OpenCL);
Mike Stump9c276ae2009-12-12 01:27:46 +00002534 PARSE_LANGOPT(CatchUndefined);
2535 // FIXME: Missing ElideConstructors?!
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002536 #undef PARSE_LANGOPT
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002537
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002538 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002539 }
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002540
2541 return false;
2542}
2543
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002544void ASTReader::ReadPreprocessedEntities() {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002545 ReadDefinedMacros();
2546}
2547
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002548/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002549ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002550 PerFileData *F = 0;
2551 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2552 F = Chain[N - I - 1];
2553 if (Index < F->LocalNumTypes)
2554 break;
2555 Index -= F->LocalNumTypes;
2556 }
2557 assert(F && F->LocalNumTypes > Index && "Broken chain");
Sebastian Redlc3632732010-10-05 15:59:54 +00002558 return RecordLocation(F, F->TypeOffsets[Index]);
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002559}
2560
2561/// \brief Read and return the type with the given index..
Douglas Gregor2cf26342009-04-09 22:27:44 +00002562///
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002563/// The index is the type ID, shifted and minus the number of predefs. This
2564/// routine actually reads the record corresponding to the type at the given
2565/// location. It is a helper routine for GetType, which deals with reading type
2566/// IDs.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002567QualType ASTReader::ReadTypeRecord(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002568 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00002569 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00002570
Douglas Gregor0b748912009-04-14 21:18:50 +00002571 // Keep track of where we are in the stream, then jump back there
2572 // after reading this type.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002573 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregor0b748912009-04-14 21:18:50 +00002574
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00002575 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redl27372b42010-08-11 18:52:41 +00002576
Douglas Gregord89275b2009-07-06 18:54:52 +00002577 // Note that we are loading a type record.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00002578 Deserializing AType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00002579
Sebastian Redlc3632732010-10-05 15:59:54 +00002580 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002581 RecordData Record;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002582 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002583 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
2584 case TYPE_EXT_QUAL: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002585 if (Record.size() != 2) {
2586 Error("Incorrect encoding of extended qualifier type");
2587 return QualType();
2588 }
Douglas Gregor6d473962009-04-15 22:00:08 +00002589 QualType Base = GetType(Record[0]);
John McCall0953e762009-09-24 19:53:00 +00002590 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]);
2591 return Context->getQualifiedType(Base, Quals);
Douglas Gregor6d473962009-04-15 22:00:08 +00002592 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002593
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002594 case TYPE_COMPLEX: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002595 if (Record.size() != 1) {
2596 Error("Incorrect encoding of complex type");
2597 return QualType();
2598 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002599 QualType ElemType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002600 return Context->getComplexType(ElemType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002601 }
2602
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002603 case TYPE_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002604 if (Record.size() != 1) {
2605 Error("Incorrect encoding of pointer type");
2606 return QualType();
2607 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002608 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002609 return Context->getPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002610 }
2611
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002612 case TYPE_BLOCK_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002613 if (Record.size() != 1) {
2614 Error("Incorrect encoding of block pointer type");
2615 return QualType();
2616 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002617 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002618 return Context->getBlockPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002619 }
2620
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002621 case TYPE_LVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002622 if (Record.size() != 1) {
2623 Error("Incorrect encoding of lvalue reference type");
2624 return QualType();
2625 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002626 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002627 return Context->getLValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002628 }
2629
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002630 case TYPE_RVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002631 if (Record.size() != 1) {
2632 Error("Incorrect encoding of rvalue reference type");
2633 return QualType();
2634 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002635 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002636 return Context->getRValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002637 }
2638
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002639 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidis240437b2010-07-02 11:55:15 +00002640 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002641 Error("Incorrect encoding of member pointer type");
2642 return QualType();
2643 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002644 QualType PointeeType = GetType(Record[0]);
2645 QualType ClassType = GetType(Record[1]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002646 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002647 }
2648
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002649 case TYPE_CONSTANT_ARRAY: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002650 QualType ElementType = GetType(Record[0]);
2651 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2652 unsigned IndexTypeQuals = Record[2];
2653 unsigned Idx = 3;
2654 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002655 return Context->getConstantArrayType(ElementType, Size,
2656 ASM, IndexTypeQuals);
2657 }
2658
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002659 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002660 QualType ElementType = GetType(Record[0]);
2661 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2662 unsigned IndexTypeQuals = Record[2];
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002663 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002664 }
2665
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002666 case TYPE_VARIABLE_ARRAY: {
Douglas Gregor0b748912009-04-14 21:18:50 +00002667 QualType ElementType = GetType(Record[0]);
2668 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2669 unsigned IndexTypeQuals = Record[2];
Sebastian Redlc3632732010-10-05 15:59:54 +00002670 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
2671 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
2672 return Context->getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002673 ASM, IndexTypeQuals,
2674 SourceRange(LBLoc, RBLoc));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002675 }
2676
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002677 case TYPE_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00002678 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002679 Error("incorrect encoding of vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002680 return QualType();
2681 }
2682
2683 QualType ElementType = GetType(Record[0]);
2684 unsigned NumElements = Record[1];
Chris Lattner788b0fd2010-06-23 06:00:24 +00002685 unsigned AltiVecSpec = Record[2];
2686 return Context->getVectorType(ElementType, NumElements,
2687 (VectorType::AltiVecSpecific)AltiVecSpec);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002688 }
2689
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002690 case TYPE_EXT_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00002691 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002692 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002693 return QualType();
2694 }
2695
2696 QualType ElementType = GetType(Record[0]);
2697 unsigned NumElements = Record[1];
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002698 return Context->getExtVectorType(ElementType, NumElements);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002699 }
2700
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002701 case TYPE_FUNCTION_NO_PROTO: {
Rafael Espindola425ef722010-03-30 22:15:11 +00002702 if (Record.size() != 4) {
Douglas Gregora02b1472009-04-28 21:53:25 +00002703 Error("incorrect encoding of no-proto function type");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002704 return QualType();
2705 }
2706 QualType ResultType = GetType(Record[0]);
Rafael Espindola425ef722010-03-30 22:15:11 +00002707 FunctionType::ExtInfo Info(Record[1], Record[2], (CallingConv)Record[3]);
Rafael Espindola264ba482010-03-30 20:24:48 +00002708 return Context->getFunctionNoProtoType(ResultType, Info);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002709 }
2710
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002711 case TYPE_FUNCTION_PROTO: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002712 QualType ResultType = GetType(Record[0]);
Douglas Gregor91236662009-12-22 18:11:50 +00002713 bool NoReturn = Record[1];
Rafael Espindola425ef722010-03-30 22:15:11 +00002714 unsigned RegParm = Record[2];
2715 CallingConv CallConv = (CallingConv)Record[3];
2716 unsigned Idx = 4;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002717 unsigned NumParams = Record[Idx++];
2718 llvm::SmallVector<QualType, 16> ParamTypes;
2719 for (unsigned I = 0; I != NumParams; ++I)
2720 ParamTypes.push_back(GetType(Record[Idx++]));
2721 bool isVariadic = Record[Idx++];
2722 unsigned Quals = Record[Idx++];
Sebastian Redl465226e2009-05-27 22:11:52 +00002723 bool hasExceptionSpec = Record[Idx++];
2724 bool hasAnyExceptionSpec = Record[Idx++];
2725 unsigned NumExceptions = Record[Idx++];
2726 llvm::SmallVector<QualType, 2> Exceptions;
2727 for (unsigned I = 0; I != NumExceptions; ++I)
2728 Exceptions.push_back(GetType(Record[Idx++]));
Jay Foadbeaaccd2009-05-21 09:52:38 +00002729 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
Sebastian Redl465226e2009-05-27 22:11:52 +00002730 isVariadic, Quals, hasExceptionSpec,
2731 hasAnyExceptionSpec, NumExceptions,
Rafael Espindola264ba482010-03-30 20:24:48 +00002732 Exceptions.data(),
Rafael Espindola425ef722010-03-30 22:15:11 +00002733 FunctionType::ExtInfo(NoReturn, RegParm,
2734 CallConv));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002735 }
2736
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002737 case TYPE_UNRESOLVED_USING:
John McCalled976492009-12-04 22:46:56 +00002738 return Context->getTypeDeclType(
2739 cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0])));
2740
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002741 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002742 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002743 Error("incorrect encoding of typedef type");
2744 return QualType();
2745 }
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002746 TypedefDecl *Decl = cast<TypedefDecl>(GetDecl(Record[0]));
2747 QualType Canonical = GetType(Record[1]);
2748 return Context->getTypedefType(Decl, Canonical);
2749 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002750
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002751 case TYPE_TYPEOF_EXPR:
Sebastian Redlc3632732010-10-05 15:59:54 +00002752 return Context->getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002753
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002754 case TYPE_TYPEOF: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002755 if (Record.size() != 1) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002756 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002757 return QualType();
2758 }
2759 QualType UnderlyingType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002760 return Context->getTypeOfType(UnderlyingType);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002761 }
Mike Stump1eb44332009-09-09 15:08:12 +00002762
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002763 case TYPE_DECLTYPE:
Sebastian Redlc3632732010-10-05 15:59:54 +00002764 return Context->getDecltypeType(ReadExpr(*Loc.F));
Anders Carlsson395b4752009-06-24 19:06:50 +00002765
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002766 case TYPE_RECORD: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002767 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002768 Error("incorrect encoding of record type");
2769 return QualType();
2770 }
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002771 bool IsDependent = Record[0];
2772 QualType T = Context->getRecordType(cast<RecordDecl>(GetDecl(Record[1])));
John McCallb870b882010-10-14 21:48:26 +00002773 T->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002774 return T;
2775 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002776
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002777 case TYPE_ENUM: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002778 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002779 Error("incorrect encoding of enum type");
2780 return QualType();
2781 }
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002782 bool IsDependent = Record[0];
2783 QualType T = Context->getEnumType(cast<EnumDecl>(GetDecl(Record[1])));
John McCallb870b882010-10-14 21:48:26 +00002784 T->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002785 return T;
2786 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002787
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002788 case TYPE_ELABORATED: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00002789 unsigned Idx = 0;
2790 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2791 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2792 QualType NamedType = GetType(Record[Idx++]);
2793 return Context->getElaboratedType(Keyword, NNS, NamedType);
John McCall7da24312009-09-05 00:15:47 +00002794 }
2795
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002796 case TYPE_OBJC_INTERFACE: {
Chris Lattnerc6fa4452009-04-22 06:45:28 +00002797 unsigned Idx = 0;
2798 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
John McCallc12c5bb2010-05-15 11:32:37 +00002799 return Context->getObjCInterfaceType(ItfD);
2800 }
2801
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002802 case TYPE_OBJC_OBJECT: {
John McCallc12c5bb2010-05-15 11:32:37 +00002803 unsigned Idx = 0;
2804 QualType Base = GetType(Record[Idx++]);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00002805 unsigned NumProtos = Record[Idx++];
2806 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2807 for (unsigned I = 0; I != NumProtos; ++I)
2808 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer20249a12010-10-21 03:16:25 +00002809 return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00002810 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002811
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002812 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00002813 unsigned Idx = 0;
John McCallc12c5bb2010-05-15 11:32:37 +00002814 QualType Pointee = GetType(Record[Idx++]);
2815 return Context->getObjCObjectPointerType(Pointee);
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00002816 }
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +00002817
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002818 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCall49a832b2009-10-18 09:09:24 +00002819 unsigned Idx = 0;
2820 QualType Parm = GetType(Record[Idx++]);
2821 QualType Replacement = GetType(Record[Idx++]);
2822 return
2823 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
2824 Replacement);
2825 }
John McCall3cb0ebd2010-03-10 03:28:59 +00002826
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002827 case TYPE_INJECTED_CLASS_NAME: {
John McCall3cb0ebd2010-03-10 03:28:59 +00002828 CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0]));
2829 QualType TST = GetType(Record[1]); // probably derivable
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00002830 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002831 // for AST reading, too much interdependencies.
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00002832 return
2833 QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCall3cb0ebd2010-03-10 03:28:59 +00002834 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00002835
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002836 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002837 unsigned Idx = 0;
2838 unsigned Depth = Record[Idx++];
2839 unsigned Index = Record[Idx++];
2840 bool Pack = Record[Idx++];
2841 IdentifierInfo *Name = GetIdentifierInfo(Record, Idx);
2842 return Context->getTemplateTypeParmType(Depth, Index, Pack, Name);
2843 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00002844
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002845 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00002846 unsigned Idx = 0;
2847 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2848 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2849 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +00002850 QualType Canon = GetType(Record[Idx++]);
2851 return Context->getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00002852 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00002853
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002854 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00002855 unsigned Idx = 0;
2856 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2857 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2858 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
2859 unsigned NumArgs = Record[Idx++];
2860 llvm::SmallVector<TemplateArgument, 8> Args;
2861 Args.reserve(NumArgs);
2862 while (NumArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00002863 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00002864 return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
2865 Args.size(), Args.data());
2866 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00002867
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002868 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00002869 unsigned Idx = 0;
2870
2871 // ArrayType
2872 QualType ElementType = GetType(Record[Idx++]);
2873 ArrayType::ArraySizeModifier ASM
2874 = (ArrayType::ArraySizeModifier)Record[Idx++];
2875 unsigned IndexTypeQuals = Record[Idx++];
2876
2877 // DependentSizedArrayType
Sebastian Redlc3632732010-10-05 15:59:54 +00002878 Expr *NumElts = ReadExpr(*Loc.F);
2879 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00002880
2881 return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
2882 IndexTypeQuals, Brackets);
2883 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00002884
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002885 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002886 unsigned Idx = 0;
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002887 bool IsDependent = Record[Idx++];
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002888 TemplateName Name = ReadTemplateName(Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002889 llvm::SmallVector<TemplateArgument, 8> Args;
Sebastian Redlc3632732010-10-05 15:59:54 +00002890 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00002891 QualType Canon = GetType(Record[Idx++]);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002892 QualType T;
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002893 if (Canon.isNull())
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002894 T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(),
2895 Args.size());
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002896 else
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002897 T = Context->getTemplateSpecializationType(Name, Args.data(),
2898 Args.size(), Canon);
John McCallb870b882010-10-14 21:48:26 +00002899 T->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002900 return T;
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002901 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002902 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002903 // Suppress a GCC warning
2904 return QualType();
2905}
2906
Sebastian Redlc3632732010-10-05 15:59:54 +00002907class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002908 ASTReader &Reader;
Sebastian Redlc3632732010-10-05 15:59:54 +00002909 ASTReader::PerFileData &F;
Sebastian Redl577d4792010-07-22 22:43:28 +00002910 llvm::BitstreamCursor &DeclsCursor;
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002911 const ASTReader::RecordData &Record;
John McCalla1ee0c52009-10-16 21:56:05 +00002912 unsigned &Idx;
2913
Sebastian Redlc3632732010-10-05 15:59:54 +00002914 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
2915 unsigned &I) {
2916 return Reader.ReadSourceLocation(F, R, I);
2917 }
2918
John McCalla1ee0c52009-10-16 21:56:05 +00002919public:
Sebastian Redlc3632732010-10-05 15:59:54 +00002920 TypeLocReader(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002921 const ASTReader::RecordData &Record, unsigned &Idx)
Sebastian Redlc3632732010-10-05 15:59:54 +00002922 : Reader(Reader), F(F), DeclsCursor(F.DeclsCursor), Record(Record), Idx(Idx)
2923 { }
John McCalla1ee0c52009-10-16 21:56:05 +00002924
John McCall51bd8032009-10-18 01:05:36 +00002925 // We want compile-time assurance that we've enumerated all of
2926 // these, so unfortunately we have to declare them first, then
2927 // define them out-of-line.
2928#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +00002929#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +00002930 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00002931#include "clang/AST/TypeLocNodes.def"
2932
John McCall51bd8032009-10-18 01:05:36 +00002933 void VisitFunctionTypeLoc(FunctionTypeLoc);
2934 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00002935};
2936
John McCall51bd8032009-10-18 01:05:36 +00002937void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCalla1ee0c52009-10-16 21:56:05 +00002938 // nothing to do
2939}
John McCall51bd8032009-10-18 01:05:36 +00002940void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00002941 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorddf889a2010-01-18 18:04:31 +00002942 if (TL.needsExtraLocalData()) {
2943 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
2944 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
2945 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
2946 TL.setModeAttr(Record[Idx++]);
2947 }
John McCalla1ee0c52009-10-16 21:56:05 +00002948}
John McCall51bd8032009-10-18 01:05:36 +00002949void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00002950 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00002951}
John McCall51bd8032009-10-18 01:05:36 +00002952void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00002953 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00002954}
John McCall51bd8032009-10-18 01:05:36 +00002955void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00002956 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00002957}
John McCall51bd8032009-10-18 01:05:36 +00002958void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00002959 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00002960}
John McCall51bd8032009-10-18 01:05:36 +00002961void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00002962 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00002963}
John McCall51bd8032009-10-18 01:05:36 +00002964void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00002965 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00002966}
John McCall51bd8032009-10-18 01:05:36 +00002967void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00002968 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
2969 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00002970 if (Record[Idx++])
Sebastian Redlc3632732010-10-05 15:59:54 +00002971 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002972 else
John McCall51bd8032009-10-18 01:05:36 +00002973 TL.setSizeExpr(0);
2974}
2975void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
2976 VisitArrayTypeLoc(TL);
2977}
2978void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
2979 VisitArrayTypeLoc(TL);
2980}
2981void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
2982 VisitArrayTypeLoc(TL);
2983}
2984void TypeLocReader::VisitDependentSizedArrayTypeLoc(
2985 DependentSizedArrayTypeLoc TL) {
2986 VisitArrayTypeLoc(TL);
2987}
2988void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
2989 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00002990 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00002991}
2992void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00002993 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00002994}
2995void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00002996 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00002997}
2998void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00002999 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3000 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
Douglas Gregordab60ad2010-10-01 18:44:50 +00003001 TL.setTrailingReturn(Record[Idx++]);
John McCall51bd8032009-10-18 01:05:36 +00003002 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
John McCall86acc2a2009-10-23 01:28:53 +00003003 TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
John McCall51bd8032009-10-18 01:05:36 +00003004 }
3005}
3006void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
3007 VisitFunctionTypeLoc(TL);
3008}
3009void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
3010 VisitFunctionTypeLoc(TL);
3011}
John McCalled976492009-12-04 22:46:56 +00003012void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003013 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalled976492009-12-04 22:46:56 +00003014}
John McCall51bd8032009-10-18 01:05:36 +00003015void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003016 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003017}
3018void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003019 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3020 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3021 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003022}
3023void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003024 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3025 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3026 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3027 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003028}
3029void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003030 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003031}
3032void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003033 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003034}
3035void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003036 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003037}
John McCall51bd8032009-10-18 01:05:36 +00003038void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003039 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003040}
John McCall49a832b2009-10-18 09:09:24 +00003041void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
3042 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003043 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall49a832b2009-10-18 09:09:24 +00003044}
John McCall51bd8032009-10-18 01:05:36 +00003045void TypeLocReader::VisitTemplateSpecializationTypeLoc(
3046 TemplateSpecializationTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003047 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
3048 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3049 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall833ca992009-10-29 08:12:44 +00003050 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
3051 TL.setArgLocInfo(i,
Sebastian Redlc3632732010-10-05 15:59:54 +00003052 Reader.GetTemplateArgumentLocInfo(F,
3053 TL.getTypePtr()->getArg(i).getKind(),
3054 Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003055}
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003056void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003057 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3058 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003059}
John McCall3cb0ebd2010-03-10 03:28:59 +00003060void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003061 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall3cb0ebd2010-03-10 03:28:59 +00003062}
Douglas Gregor4714c122010-03-31 17:34:00 +00003063void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003064 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3065 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
3066 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003067}
John McCall33500952010-06-11 00:33:02 +00003068void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
3069 DependentTemplateSpecializationTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003070 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3071 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
3072 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3073 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3074 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall33500952010-06-11 00:33:02 +00003075 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
3076 TL.setArgLocInfo(I,
Sebastian Redlc3632732010-10-05 15:59:54 +00003077 Reader.GetTemplateArgumentLocInfo(F,
3078 TL.getTypePtr()->getArg(I).getKind(),
3079 Record, Idx));
John McCall33500952010-06-11 00:33:02 +00003080}
John McCall51bd8032009-10-18 01:05:36 +00003081void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003082 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallc12c5bb2010-05-15 11:32:37 +00003083}
3084void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3085 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00003086 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3087 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003088 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redlc3632732010-10-05 15:59:54 +00003089 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003090}
John McCall54e14c42009-10-22 22:37:11 +00003091void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003092 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall54e14c42009-10-22 22:37:11 +00003093}
John McCalla1ee0c52009-10-16 21:56:05 +00003094
Sebastian Redlc3632732010-10-05 15:59:54 +00003095TypeSourceInfo *ASTReader::GetTypeSourceInfo(PerFileData &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00003096 const RecordData &Record,
John McCalla1ee0c52009-10-16 21:56:05 +00003097 unsigned &Idx) {
3098 QualType InfoTy = GetType(Record[Idx++]);
3099 if (InfoTy.isNull())
3100 return 0;
3101
John McCalla93c9342009-12-07 02:54:59 +00003102 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
Sebastian Redlc3632732010-10-05 15:59:54 +00003103 TypeLocReader TLR(*this, F, Record, Idx);
John McCalla93c9342009-12-07 02:54:59 +00003104 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCalla1ee0c52009-10-16 21:56:05 +00003105 TLR.Visit(TL);
John McCalla93c9342009-12-07 02:54:59 +00003106 return TInfo;
John McCalla1ee0c52009-10-16 21:56:05 +00003107}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003108
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003109QualType ASTReader::GetType(TypeID ID) {
John McCall0953e762009-09-24 19:53:00 +00003110 unsigned FastQuals = ID & Qualifiers::FastMask;
3111 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003112
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003113 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003114 QualType T;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003115 switch ((PredefinedTypeIDs)Index) {
3116 case PREDEF_TYPE_NULL_ID: return QualType();
3117 case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
3118 case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003119
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003120 case PREDEF_TYPE_CHAR_U_ID:
3121 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregor2cf26342009-04-09 22:27:44 +00003122 // FIXME: Check that the signedness of CharTy is correct!
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003123 T = Context->CharTy;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003124 break;
3125
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003126 case PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break;
3127 case PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break;
3128 case PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break;
3129 case PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break;
3130 case PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break;
3131 case PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break;
3132 case PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break;
3133 case PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break;
3134 case PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break;
3135 case PREDEF_TYPE_INT_ID: T = Context->IntTy; break;
3136 case PREDEF_TYPE_LONG_ID: T = Context->LongTy; break;
3137 case PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break;
3138 case PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break;
3139 case PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break;
3140 case PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break;
3141 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break;
3142 case PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break;
3143 case PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break;
3144 case PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break;
3145 case PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break;
3146 case PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break;
3147 case PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break;
3148 case PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break;
3149 case PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003150 }
3151
3152 assert(!T.isNull() && "Unknown predefined type");
John McCall0953e762009-09-24 19:53:00 +00003153 return T.withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003154 }
3155
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003156 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003157 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl07a353c2010-07-14 20:26:45 +00003158 if (TypesLoaded[Index].isNull()) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003159 TypesLoaded[Index] = ReadTypeRecord(Index);
Douglas Gregor97475832010-10-05 18:37:06 +00003160 if (TypesLoaded[Index].isNull())
3161 return QualType();
3162
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003163 TypesLoaded[Index]->setFromAST();
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003164 TypeIdxs[TypesLoaded[Index]] = TypeIdx::fromTypeID(ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003165 if (DeserializationListener)
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003166 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1476ed42010-07-16 16:36:56 +00003167 TypesLoaded[Index]);
Sebastian Redl07a353c2010-07-14 20:26:45 +00003168 }
Mike Stump1eb44332009-09-09 15:08:12 +00003169
John McCall0953e762009-09-24 19:53:00 +00003170 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003171}
3172
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003173TypeID ASTReader::GetTypeID(QualType T) const {
3174 return MakeTypeID(T,
3175 std::bind1st(std::mem_fun(&ASTReader::GetTypeIdx), this));
3176}
3177
3178TypeIdx ASTReader::GetTypeIdx(QualType T) const {
3179 if (T.isNull())
3180 return TypeIdx();
3181 assert(!T.getLocalFastQualifiers());
3182
3183 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3184 // GetTypeIdx is mostly used for computing the hash of DeclarationNames and
3185 // comparing keys of ASTDeclContextNameLookupTable.
3186 // If the type didn't come from the AST file use a specially marked index
3187 // so that any hash/key comparison fail since no such index is stored
3188 // in a AST file.
3189 if (I == TypeIdxs.end())
3190 return TypeIdx(-1);
3191 return I->second;
3192}
3193
John McCall833ca992009-10-29 08:12:44 +00003194TemplateArgumentLocInfo
Sebastian Redlc3632732010-10-05 15:59:54 +00003195ASTReader::GetTemplateArgumentLocInfo(PerFileData &F,
3196 TemplateArgument::ArgKind Kind,
John McCall833ca992009-10-29 08:12:44 +00003197 const RecordData &Record,
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003198 unsigned &Index) {
John McCall833ca992009-10-29 08:12:44 +00003199 switch (Kind) {
3200 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00003201 return ReadExpr(F);
John McCall833ca992009-10-29 08:12:44 +00003202 case TemplateArgument::Type:
Sebastian Redlc3632732010-10-05 15:59:54 +00003203 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor788cd062009-11-11 01:00:40 +00003204 case TemplateArgument::Template: {
Sebastian Redlc3632732010-10-05 15:59:54 +00003205 SourceRange QualifierRange = ReadSourceRange(F, Record, Index);
3206 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003207 return TemplateArgumentLocInfo(QualifierRange, TemplateNameLoc);
Douglas Gregor788cd062009-11-11 01:00:40 +00003208 }
John McCall833ca992009-10-29 08:12:44 +00003209 case TemplateArgument::Null:
3210 case TemplateArgument::Integral:
3211 case TemplateArgument::Declaration:
3212 case TemplateArgument::Pack:
3213 return TemplateArgumentLocInfo();
3214 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00003215 llvm_unreachable("unexpected template argument loc");
John McCall833ca992009-10-29 08:12:44 +00003216 return TemplateArgumentLocInfo();
3217}
3218
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003219TemplateArgumentLoc
Sebastian Redlc3632732010-10-05 15:59:54 +00003220ASTReader::ReadTemplateArgumentLoc(PerFileData &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00003221 const RecordData &Record, unsigned &Index) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003222 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003223
3224 if (Arg.getKind() == TemplateArgument::Expression) {
3225 if (Record[Index++]) // bool InfoHasSameExpr.
3226 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
3227 }
Sebastian Redlc3632732010-10-05 15:59:54 +00003228 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003229 Record, Index));
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003230}
3231
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003232Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall76bd1f32010-06-01 09:23:16 +00003233 return GetDecl(ID);
3234}
3235
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003236TranslationUnitDecl *ASTReader::GetTranslationUnitDecl() {
Sebastian Redl30c514c2010-07-14 23:45:08 +00003237 if (!DeclsLoaded[0]) {
Sebastian Redle1dde812010-08-24 00:50:04 +00003238 ReadDeclRecord(0, 1);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003239 if (DeserializationListener)
Sebastian Redl1476ed42010-07-16 16:36:56 +00003240 DeserializationListener->DeclRead(1, DeclsLoaded[0]);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003241 }
Argyrios Kyrtzidis8871a442010-07-08 17:13:02 +00003242
3243 return cast<TranslationUnitDecl>(DeclsLoaded[0]);
3244}
3245
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003246Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003247 if (ID == 0)
3248 return 0;
3249
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003250 if (ID > DeclsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003251 Error("declaration ID out-of-range for AST file");
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003252 return 0;
3253 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003254
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003255 unsigned Index = ID - 1;
Sebastian Redl30c514c2010-07-14 23:45:08 +00003256 if (!DeclsLoaded[Index]) {
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00003257 ReadDeclRecord(Index, ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003258 if (DeserializationListener)
3259 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
3260 }
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003261
3262 return DeclsLoaded[Index];
Douglas Gregor2cf26342009-04-09 22:27:44 +00003263}
3264
Chris Lattner887e2b32009-04-27 05:46:25 +00003265/// \brief Resolve the offset of a statement into a statement.
3266///
3267/// This operation will read a new statement from the external
3268/// source each time it is called, and is meant to be used via a
3269/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003270Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003271 // Offset here is a global offset across the entire chain.
3272 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3273 PerFileData &F = *Chain[N - I - 1];
3274 if (Offset < F.SizeInBits) {
3275 // Since we know that this statement is part of a decl, make sure to use
3276 // the decl cursor to read it.
3277 F.DeclsCursor.JumpToBit(Offset);
Sebastian Redlc3632732010-10-05 15:59:54 +00003278 return ReadStmtFromStream(F);
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003279 }
3280 Offset -= F.SizeInBits;
3281 }
3282 llvm_unreachable("Broken chain");
Douglas Gregor250fc9c2009-04-18 00:07:54 +00003283}
3284
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003285bool ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00003286 bool (*isKindWeWant)(Decl::Kind),
John McCall76bd1f32010-06-01 09:23:16 +00003287 llvm::SmallVectorImpl<Decl*> &Decls) {
Mike Stump1eb44332009-09-09 15:08:12 +00003288 assert(DC->hasExternalLexicalStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00003289 "DeclContext has no lexical decls in storage");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003290
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003291 // There might be lexical decls in multiple parts of the chain, for the TU
3292 // at least.
Sebastian Redl4a9eb262010-09-28 02:24:44 +00003293 // DeclContextOffsets might reallocate as we load additional decls below,
3294 // so make a copy of the vector.
3295 DeclContextInfos Infos = DeclContextOffsets[DC];
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003296 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3297 I != E; ++I) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003298 // IDs can be 0 if this context doesn't contain declarations.
3299 if (!I->LexicalDecls)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003300 continue;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003301
3302 // Load all of the declaration IDs
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00003303 for (const KindDeclIDPair *ID = I->LexicalDecls,
3304 *IDE = ID + I->NumLexicalDecls; ID != IDE; ++ID) {
3305 if (isKindWeWant && !isKindWeWant((Decl::Kind)ID->first))
3306 continue;
3307
3308 Decl *D = GetDecl(ID->second);
Sebastian Redl4a9eb262010-09-28 02:24:44 +00003309 assert(D && "Null decl in lexical decls");
3310 Decls.push_back(D);
3311 }
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003312 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003313
Douglas Gregor25123082009-04-22 22:34:57 +00003314 ++NumLexicalDeclContextsRead;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003315 return false;
3316}
3317
John McCall76bd1f32010-06-01 09:23:16 +00003318DeclContext::lookup_result
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003319ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +00003320 DeclarationName Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00003321 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00003322 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003323 if (!Name)
3324 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
3325 DeclContext::lookup_iterator(0));
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003326
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003327 llvm::SmallVector<NamedDecl *, 64> Decls;
Sebastian Redl8b122732010-08-24 00:49:55 +00003328 // There might be visible decls in multiple parts of the chain, for the TU
Sebastian Redl5967d622010-08-24 00:50:16 +00003329 // and namespaces. For any given name, the last available results replace
3330 // all earlier ones. For this reason, we walk in reverse.
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003331 DeclContextInfos &Infos = DeclContextOffsets[DC];
Sebastian Redl5967d622010-08-24 00:50:16 +00003332 for (DeclContextInfos::reverse_iterator I = Infos.rbegin(), E = Infos.rend();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003333 I != E; ++I) {
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003334 if (!I->NameLookupTableData)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003335 continue;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003336
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003337 ASTDeclContextNameLookupTable *LookupTable =
3338 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3339 ASTDeclContextNameLookupTable::iterator Pos = LookupTable->find(Name);
3340 if (Pos == LookupTable->end())
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003341 continue;
3342
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003343 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
3344 for (; Data.first != Data.second; ++Data.first)
3345 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
Sebastian Redl5967d622010-08-24 00:50:16 +00003346 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003347 }
3348
Douglas Gregor25123082009-04-22 22:34:57 +00003349 ++NumVisibleDeclContextsRead;
John McCall76bd1f32010-06-01 09:23:16 +00003350
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003351 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall76bd1f32010-06-01 09:23:16 +00003352 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003353}
3354
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00003355void ASTReader::MaterializeVisibleDecls(const DeclContext *DC) {
3356 assert(DC->hasExternalVisibleStorage() &&
3357 "DeclContext has no visible decls in storage");
3358
3359 llvm::SmallVector<NamedDecl *, 64> Decls;
3360 // There might be visible decls in multiple parts of the chain, for the TU
3361 // and namespaces.
3362 DeclContextInfos &Infos = DeclContextOffsets[DC];
3363 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3364 I != E; ++I) {
3365 if (!I->NameLookupTableData)
3366 continue;
3367
3368 ASTDeclContextNameLookupTable *LookupTable =
3369 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3370 for (ASTDeclContextNameLookupTable::item_iterator
3371 ItemI = LookupTable->item_begin(),
3372 ItemEnd = LookupTable->item_end() ; ItemI != ItemEnd; ++ItemI) {
3373 ASTDeclContextNameLookupTable::item_iterator::value_type Val
3374 = *ItemI;
3375 ASTDeclContextNameLookupTrait::data_type Data = Val.second;
3376 Decls.clear();
3377 for (; Data.first != Data.second; ++Data.first)
3378 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
3379 MaterializeVisibleDeclsForName(DC, Val.first, Decls);
3380 }
3381 }
3382}
3383
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003384void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003385 assert(Consumer);
3386 while (!InterestingDecls.empty()) {
3387 DeclGroupRef DG(InterestingDecls.front());
3388 InterestingDecls.pop_front();
Sebastian Redl27372b42010-08-11 18:52:41 +00003389 Consumer->HandleInterestingDecl(DG);
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003390 }
3391}
3392
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003393void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor0af2ca42009-04-22 19:09:20 +00003394 this->Consumer = Consumer;
3395
Douglas Gregorfdd01722009-04-14 00:24:19 +00003396 if (!Consumer)
3397 return;
3398
3399 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003400 // Force deserialization of this decl, which will cause it to be queued for
3401 // passing to the consumer.
Daniel Dunbar04a0b502009-09-17 03:06:44 +00003402 GetDecl(ExternalDefinitions[I]);
Douglas Gregorfdd01722009-04-14 00:24:19 +00003403 }
Douglas Gregorc62a2fe2009-04-25 00:41:30 +00003404
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003405 PassInterestingDeclsToConsumer();
Douglas Gregorfdd01722009-04-14 00:24:19 +00003406}
3407
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003408void ASTReader::PrintStats() {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003409 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregor2cf26342009-04-09 22:27:44 +00003410
Mike Stump1eb44332009-09-09 15:08:12 +00003411 unsigned NumTypesLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003412 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall0953e762009-09-24 19:53:00 +00003413 QualType());
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003414 unsigned NumDeclsLoaded
3415 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
3416 (Decl *)0);
3417 unsigned NumIdentifiersLoaded
3418 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
3419 IdentifiersLoaded.end(),
3420 (IdentifierInfo *)0);
Mike Stump1eb44332009-09-09 15:08:12 +00003421 unsigned NumSelectorsLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003422 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
3423 SelectorsLoaded.end(),
3424 Selector());
Douglas Gregor2d41cc12009-04-13 20:50:16 +00003425
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003426 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
3427 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00003428 if (TotalNumSLocEntries)
3429 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
3430 NumSLocEntriesRead, TotalNumSLocEntries,
3431 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003432 if (!TypesLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003433 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003434 NumTypesLoaded, (unsigned)TypesLoaded.size(),
3435 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
3436 if (!DeclsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003437 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003438 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
3439 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003440 if (!IdentifiersLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003441 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003442 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
3443 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redl725cd962010-08-04 20:40:17 +00003444 if (!SelectorsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003445 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redl725cd962010-08-04 20:40:17 +00003446 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
3447 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor83941df2009-04-25 17:48:32 +00003448 if (TotalNumStatements)
3449 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
3450 NumStatementsRead, TotalNumStatements,
3451 ((float)NumStatementsRead/TotalNumStatements * 100));
3452 if (TotalNumMacros)
3453 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
3454 NumMacrosRead, TotalNumMacros,
3455 ((float)NumMacrosRead/TotalNumMacros * 100));
3456 if (TotalLexicalDeclContexts)
3457 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
3458 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
3459 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
3460 * 100));
3461 if (TotalVisibleDeclContexts)
3462 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
3463 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
3464 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
3465 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003466 if (TotalNumMethodPoolEntries) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003467 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003468 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
3469 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor83941df2009-04-25 17:48:32 +00003470 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003471 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor83941df2009-04-25 17:48:32 +00003472 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003473 std::fprintf(stderr, "\n");
3474}
3475
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003476void ASTReader::InitializeSema(Sema &S) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00003477 SemaObj = &S;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003478 S.ExternalSource = this;
3479
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00003480 // Makes sure any declarations that were deserialized "too early"
3481 // still get added to the identifier's declaration chains.
Douglas Gregor76dc8892010-09-24 23:29:12 +00003482 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
3483 if (SemaObj->TUScope)
John McCalld226f652010-08-21 09:40:31 +00003484 SemaObj->TUScope->AddDecl(PreloadedDecls[I]);
Douglas Gregor76dc8892010-09-24 23:29:12 +00003485
3486 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregor668c1a42009-04-21 22:25:48 +00003487 }
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00003488 PreloadedDecls.clear();
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003489
3490 // If there were any tentative definitions, deserialize them and add
Sebastian Redle9d12b62010-01-31 22:27:38 +00003491 // them to Sema's list of tentative definitions.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003492 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
3493 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
Sebastian Redle9d12b62010-01-31 22:27:38 +00003494 SemaObj->TentativeDefinitions.push_back(Var);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003495 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00003496
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003497 // If there were any unused file scoped decls, deserialize them and add to
3498 // Sema's list of unused file scoped decls.
3499 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
3500 DeclaratorDecl *D = cast<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
3501 SemaObj->UnusedFileScopedDecls.push_back(D);
Tanya Lattnere6bbc012010-02-12 00:07:30 +00003502 }
Douglas Gregor14c22f22009-04-22 22:18:58 +00003503
3504 // If there were any locally-scoped external declarations,
3505 // deserialize them and add them to Sema's table of locally-scoped
3506 // external declarations.
3507 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
3508 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
3509 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
3510 }
Douglas Gregorb81c1702009-04-27 20:06:05 +00003511
3512 // If there were any ext_vector type declarations, deserialize them
3513 // and add them to Sema's vector of such declarations.
3514 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I)
3515 SemaObj->ExtVectorDecls.push_back(
3516 cast<TypedefDecl>(GetDecl(ExtVectorDecls[I])));
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003517
3518 // FIXME: Do VTable uses and dynamic classes deserialize too much ?
3519 // Can we cut them down before writing them ?
3520
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003521 // If there were any dynamic classes declarations, deserialize them
3522 // and add them to Sema's vector of such declarations.
3523 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I)
3524 SemaObj->DynamicClasses.push_back(
3525 cast<CXXRecordDecl>(GetDecl(DynamicClasses[I])));
Fariborz Jahanian32019832010-07-23 19:11:11 +00003526
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003527 // Load the offsets of the declarations that Sema references.
3528 // They will be lazily deserialized when needed.
3529 if (!SemaDeclRefs.empty()) {
3530 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
3531 SemaObj->StdNamespace = SemaDeclRefs[0];
3532 SemaObj->StdBadAlloc = SemaDeclRefs[1];
3533 }
3534
Sebastian Redlc3632732010-10-05 15:59:54 +00003535 for (PerFileData *F = FirstInSource; F; F = F->NextInSource) {
3536
3537 // If there are @selector references added them to its pool. This is for
3538 // implementation of -Wselector.
3539 if (!F->ReferencedSelectorsData.empty()) {
3540 unsigned int DataSize = F->ReferencedSelectorsData.size()-1;
3541 unsigned I = 0;
3542 while (I < DataSize) {
3543 Selector Sel = DecodeSelector(F->ReferencedSelectorsData[I++]);
3544 SourceLocation SelLoc = ReadSourceLocation(
3545 *F, F->ReferencedSelectorsData, I);
3546 SemaObj->ReferencedSelectors.insert(std::make_pair(Sel, SelLoc));
3547 }
3548 }
3549
3550 // If there were any pending implicit instantiations, deserialize them
3551 // and add them to Sema's queue of such instantiations.
3552 assert(F->PendingInstantiations.size() % 2 == 0 &&
3553 "Expected pairs of entries");
3554 for (unsigned Idx = 0, N = F->PendingInstantiations.size(); Idx < N;) {
3555 ValueDecl *D=cast<ValueDecl>(GetDecl(F->PendingInstantiations[Idx++]));
3556 SourceLocation Loc = ReadSourceLocation(*F, F->PendingInstantiations,Idx);
3557 SemaObj->PendingInstantiations.push_back(std::make_pair(D, Loc));
3558 }
3559 }
3560
3561 // The two special data sets below always come from the most recent PCH,
3562 // which is at the front of the chain.
3563 PerFileData &F = *Chain.front();
3564
3565 // If there were any weak undeclared identifiers, deserialize them and add to
3566 // Sema's list of weak undeclared identifiers.
3567 if (!WeakUndeclaredIdentifiers.empty()) {
3568 unsigned Idx = 0;
3569 for (unsigned I = 0, N = WeakUndeclaredIdentifiers[Idx++]; I != N; ++I) {
3570 IdentifierInfo *WeakId = GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3571 IdentifierInfo *AliasId= GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3572 SourceLocation Loc = ReadSourceLocation(F, WeakUndeclaredIdentifiers,Idx);
3573 bool Used = WeakUndeclaredIdentifiers[Idx++];
3574 Sema::WeakInfo WI(AliasId, Loc);
3575 WI.setUsed(Used);
3576 SemaObj->WeakUndeclaredIdentifiers.insert(std::make_pair(WeakId, WI));
3577 }
3578 }
3579
3580 // If there were any VTable uses, deserialize the information and add it
3581 // to Sema's vector and map of VTable uses.
3582 if (!VTableUses.empty()) {
3583 unsigned Idx = 0;
3584 for (unsigned I = 0, N = VTableUses[Idx++]; I != N; ++I) {
3585 CXXRecordDecl *Class = cast<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
3586 SourceLocation Loc = ReadSourceLocation(F, VTableUses, Idx);
3587 bool DefinitionRequired = VTableUses[Idx++];
3588 SemaObj->VTableUses.push_back(std::make_pair(Class, Loc));
3589 SemaObj->VTablesUsed[Class] = DefinitionRequired;
Fariborz Jahanian32019832010-07-23 19:11:11 +00003590 }
3591 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00003592}
3593
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003594IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Sebastian Redld8c5abb2010-08-02 18:30:12 +00003595 // Try to find this name within our on-disk hash tables. We start with the
3596 // most recent one, since that one contains the most up-to-date info.
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003597 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003598 ASTIdentifierLookupTable *IdTable
3599 = (ASTIdentifierLookupTable *)Chain[I]->IdentifierLookupTable;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003600 if (!IdTable)
3601 continue;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003602 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003603 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003604 if (Pos == IdTable->end())
3605 continue;
Douglas Gregor668c1a42009-04-21 22:25:48 +00003606
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003607 // Dereferencing the iterator has the effect of building the
3608 // IdentifierInfo node and populating it with the various
3609 // declarations it needs.
Sebastian Redld8c5abb2010-08-02 18:30:12 +00003610 return *Pos;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003611 }
Sebastian Redld8c5abb2010-08-02 18:30:12 +00003612 return 0;
Douglas Gregor668c1a42009-04-21 22:25:48 +00003613}
3614
Douglas Gregor95f42922010-10-14 22:11:03 +00003615namespace clang {
3616 /// \brief An identifier-lookup iterator that enumerates all of the
3617 /// identifiers stored within a set of AST files.
3618 class ASTIdentifierIterator : public IdentifierIterator {
3619 /// \brief The AST reader whose identifiers are being enumerated.
3620 const ASTReader &Reader;
3621
3622 /// \brief The current index into the chain of AST files stored in
3623 /// the AST reader.
3624 unsigned Index;
3625
3626 /// \brief The current position within the identifier lookup table
3627 /// of the current AST file.
3628 ASTIdentifierLookupTable::key_iterator Current;
3629
3630 /// \brief The end position within the identifier lookup table of
3631 /// the current AST file.
3632 ASTIdentifierLookupTable::key_iterator End;
3633
3634 public:
3635 explicit ASTIdentifierIterator(const ASTReader &Reader);
3636
3637 virtual llvm::StringRef Next();
3638 };
3639}
3640
3641ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
3642 : Reader(Reader), Index(Reader.Chain.size() - 1) {
3643 ASTIdentifierLookupTable *IdTable
3644 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
3645 Current = IdTable->key_begin();
3646 End = IdTable->key_end();
3647}
3648
3649llvm::StringRef ASTIdentifierIterator::Next() {
3650 while (Current == End) {
3651 // If we have exhausted all of our AST files, we're done.
3652 if (Index == 0)
3653 return llvm::StringRef();
3654
3655 --Index;
3656 ASTIdentifierLookupTable *IdTable
3657 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
3658 Current = IdTable->key_begin();
3659 End = IdTable->key_end();
3660 }
3661
3662 // We have any identifiers remaining in the current AST file; return
3663 // the next one.
3664 std::pair<const char*, unsigned> Key = *Current;
3665 ++Current;
3666 return llvm::StringRef(Key.first, Key.second);
3667}
3668
3669IdentifierIterator *ASTReader::getIdentifiers() const {
3670 return new ASTIdentifierIterator(*this);
3671}
3672
Mike Stump1eb44332009-09-09 15:08:12 +00003673std::pair<ObjCMethodList, ObjCMethodList>
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003674ASTReader::ReadMethodPool(Selector Sel) {
Sebastian Redl725cd962010-08-04 20:40:17 +00003675 // Find this selector in a hash table. We want to find the most recent entry.
3676 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3677 PerFileData &F = *Chain[I];
3678 if (!F.SelectorLookupTable)
3679 continue;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003680
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003681 ASTSelectorLookupTable *PoolTable
3682 = (ASTSelectorLookupTable*)F.SelectorLookupTable;
3683 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
Sebastian Redl725cd962010-08-04 20:40:17 +00003684 if (Pos != PoolTable->end()) {
3685 ++NumSelectorsRead;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003686 // FIXME: Not quite happy with the statistics here. We probably should
3687 // disable this tracking when called via LoadSelector.
3688 // Also, should entries without methods count as misses?
3689 ++NumMethodPoolEntriesRead;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003690 ASTSelectorLookupTrait::data_type Data = *Pos;
Sebastian Redl725cd962010-08-04 20:40:17 +00003691 if (DeserializationListener)
3692 DeserializationListener->SelectorRead(Data.ID, Sel);
3693 return std::make_pair(Data.Instance, Data.Factory);
3694 }
Douglas Gregor83941df2009-04-25 17:48:32 +00003695 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003696
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003697 ++NumMethodPoolMisses;
Sebastian Redl725cd962010-08-04 20:40:17 +00003698 return std::pair<ObjCMethodList, ObjCMethodList>();
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003699}
3700
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003701void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redle58aa892010-08-04 18:21:41 +00003702 // It would be complicated to avoid reading the methods anyway. So don't.
3703 ReadMethodPool(Sel);
3704}
3705
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003706void ASTReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00003707 assert(ID && "Non-zero identifier ID required");
Douglas Gregora02b1472009-04-28 21:53:25 +00003708 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003709 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003710 if (DeserializationListener)
3711 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregor668c1a42009-04-21 22:25:48 +00003712}
3713
Douglas Gregord89275b2009-07-06 18:54:52 +00003714/// \brief Set the globally-visible declarations associated with the given
3715/// identifier.
3716///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003717/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump1eb44332009-09-09 15:08:12 +00003718/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregord89275b2009-07-06 18:54:52 +00003719/// them.
3720///
3721/// \param II an IdentifierInfo that refers to one or more globally-visible
3722/// declarations.
3723///
3724/// \param DeclIDs the set of declaration IDs with the name @p II that are
3725/// visible at global scope.
3726///
3727/// \param Nonrecursive should be true to indicate that the caller knows that
3728/// this call is non-recursive, and therefore the globally-visible declarations
3729/// will not be placed onto the pending queue.
Mike Stump1eb44332009-09-09 15:08:12 +00003730void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003731ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Douglas Gregord89275b2009-07-06 18:54:52 +00003732 const llvm::SmallVectorImpl<uint32_t> &DeclIDs,
3733 bool Nonrecursive) {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00003734 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregord89275b2009-07-06 18:54:52 +00003735 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
3736 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
3737 PII.II = II;
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00003738 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregord89275b2009-07-06 18:54:52 +00003739 return;
3740 }
Mike Stump1eb44332009-09-09 15:08:12 +00003741
Douglas Gregord89275b2009-07-06 18:54:52 +00003742 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
3743 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
3744 if (SemaObj) {
Douglas Gregor914ed9d2010-08-13 03:15:25 +00003745 if (SemaObj->TUScope) {
3746 // Introduce this declaration into the translation-unit scope
3747 // and add it to the declaration chain for this identifier, so
3748 // that (unqualified) name lookup will find it.
John McCalld226f652010-08-21 09:40:31 +00003749 SemaObj->TUScope->AddDecl(D);
Douglas Gregor914ed9d2010-08-13 03:15:25 +00003750 }
Douglas Gregor76dc8892010-09-24 23:29:12 +00003751 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
Douglas Gregord89275b2009-07-06 18:54:52 +00003752 } else {
3753 // Queue this declaration so that it will be added to the
3754 // translation unit scope and identifier's declaration chain
3755 // once a Sema object is known.
3756 PreloadedDecls.push_back(D);
3757 }
3758 }
3759}
3760
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003761IdentifierInfo *ASTReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00003762 if (ID == 0)
3763 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003764
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00003765 if (IdentifiersLoaded.empty()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003766 Error("no identifier table in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00003767 return 0;
3768 }
Mike Stump1eb44332009-09-09 15:08:12 +00003769
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003770 assert(PP && "Forgot to set Preprocessor ?");
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00003771 ID -= 1;
3772 if (!IdentifiersLoaded[ID]) {
3773 unsigned Index = ID;
3774 const char *Str = 0;
3775 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3776 PerFileData *F = Chain[N - I - 1];
3777 if (Index < F->LocalNumIdentifiers) {
3778 uint32_t Offset = F->IdentifierOffsets[Index];
3779 Str = F->IdentifierTableData + Offset;
3780 break;
3781 }
3782 Index -= F->LocalNumIdentifiers;
3783 }
3784 assert(Str && "Broken Chain");
Douglas Gregord6595a42009-04-25 21:04:17 +00003785
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003786 // All of the strings in the AST file are preceded by a 16-bit length.
3787 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenek231bc0b2009-10-23 04:45:31 +00003788 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
3789 // unsigned integers. This is important to avoid integer overflow when
3790 // we cast them to 'unsigned'.
Ted Kremenekff1ea462009-10-23 03:57:22 +00003791 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregor02fc7512009-04-28 20:01:51 +00003792 unsigned StrLen = (((unsigned) StrLenPtr[0])
3793 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00003794 IdentifiersLoaded[ID]
Kovarththanan Rajaratnam811f4262010-03-12 10:32:27 +00003795 = &PP->getIdentifierTable().get(Str, StrLen);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003796 if (DeserializationListener)
3797 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregorafaf3082009-04-11 00:14:32 +00003798 }
Mike Stump1eb44332009-09-09 15:08:12 +00003799
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00003800 return IdentifiersLoaded[ID];
Douglas Gregor2cf26342009-04-09 22:27:44 +00003801}
3802
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003803void ASTReader::ReadSLocEntry(unsigned ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00003804 ReadSLocEntryRecord(ID);
3805}
3806
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003807Selector ASTReader::DecodeSelector(unsigned ID) {
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003808 if (ID == 0)
3809 return Selector();
Mike Stump1eb44332009-09-09 15:08:12 +00003810
Sebastian Redl725cd962010-08-04 20:40:17 +00003811 if (ID > SelectorsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003812 Error("selector ID out of range in AST file");
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003813 return Selector();
3814 }
Douglas Gregor83941df2009-04-25 17:48:32 +00003815
Sebastian Redl725cd962010-08-04 20:40:17 +00003816 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003817 // Load this selector from the selector table.
Sebastian Redl725cd962010-08-04 20:40:17 +00003818 unsigned Idx = ID - 1;
3819 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3820 PerFileData &F = *Chain[N - I - 1];
3821 if (Idx < F.LocalNumSelectors) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003822 ASTSelectorLookupTrait Trait(*this);
Sebastian Redl725cd962010-08-04 20:40:17 +00003823 SelectorsLoaded[ID - 1] =
3824 Trait.ReadKey(F.SelectorLookupTableData + F.SelectorOffsets[Idx], 0);
3825 if (DeserializationListener)
3826 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
3827 break;
3828 }
3829 Idx -= F.LocalNumSelectors;
3830 }
Douglas Gregor83941df2009-04-25 17:48:32 +00003831 }
3832
Sebastian Redl725cd962010-08-04 20:40:17 +00003833 return SelectorsLoaded[ID - 1];
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003834}
3835
Michael J. Spencer20249a12010-10-21 03:16:25 +00003836Selector ASTReader::GetExternalSelector(uint32_t ID) {
Douglas Gregor719770d2010-04-06 17:30:22 +00003837 return DecodeSelector(ID);
3838}
3839
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003840uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redl725cd962010-08-04 20:40:17 +00003841 // ID 0 (the null selector) is considered an external selector.
3842 return getTotalNumSelectors() + 1;
Douglas Gregor719770d2010-04-06 17:30:22 +00003843}
3844
Mike Stump1eb44332009-09-09 15:08:12 +00003845DeclarationName
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003846ASTReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003847 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
3848 switch (Kind) {
3849 case DeclarationName::Identifier:
3850 return DeclarationName(GetIdentifierInfo(Record, Idx));
3851
3852 case DeclarationName::ObjCZeroArgSelector:
3853 case DeclarationName::ObjCOneArgSelector:
3854 case DeclarationName::ObjCMultiArgSelector:
Steve Naroffa7503a72009-04-23 15:15:40 +00003855 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00003856
3857 case DeclarationName::CXXConstructorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003858 return Context->DeclarationNames.getCXXConstructorName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00003859 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00003860
3861 case DeclarationName::CXXDestructorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003862 return Context->DeclarationNames.getCXXDestructorName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00003863 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00003864
3865 case DeclarationName::CXXConversionFunctionName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003866 return Context->DeclarationNames.getCXXConversionFunctionName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00003867 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00003868
3869 case DeclarationName::CXXOperatorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003870 return Context->DeclarationNames.getCXXOperatorName(
Douglas Gregor2cf26342009-04-09 22:27:44 +00003871 (OverloadedOperatorKind)Record[Idx++]);
3872
Sean Hunt3e518bd2009-11-29 07:34:05 +00003873 case DeclarationName::CXXLiteralOperatorName:
3874 return Context->DeclarationNames.getCXXLiteralOperatorName(
3875 GetIdentifierInfo(Record, Idx));
3876
Douglas Gregor2cf26342009-04-09 22:27:44 +00003877 case DeclarationName::CXXUsingDirective:
3878 return DeclarationName::getUsingDirectiveName();
3879 }
3880
3881 // Required to silence GCC warning
3882 return DeclarationName();
3883}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003884
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003885void ASTReader::ReadDeclarationNameLoc(PerFileData &F,
3886 DeclarationNameLoc &DNLoc,
3887 DeclarationName Name,
3888 const RecordData &Record, unsigned &Idx) {
3889 switch (Name.getNameKind()) {
3890 case DeclarationName::CXXConstructorName:
3891 case DeclarationName::CXXDestructorName:
3892 case DeclarationName::CXXConversionFunctionName:
3893 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
3894 break;
3895
3896 case DeclarationName::CXXOperatorName:
3897 DNLoc.CXXOperatorName.BeginOpNameLoc
3898 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
3899 DNLoc.CXXOperatorName.EndOpNameLoc
3900 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
3901 break;
3902
3903 case DeclarationName::CXXLiteralOperatorName:
3904 DNLoc.CXXLiteralOperatorName.OpNameLoc
3905 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
3906 break;
3907
3908 case DeclarationName::Identifier:
3909 case DeclarationName::ObjCZeroArgSelector:
3910 case DeclarationName::ObjCOneArgSelector:
3911 case DeclarationName::ObjCMultiArgSelector:
3912 case DeclarationName::CXXUsingDirective:
3913 break;
3914 }
3915}
3916
3917void ASTReader::ReadDeclarationNameInfo(PerFileData &F,
3918 DeclarationNameInfo &NameInfo,
3919 const RecordData &Record, unsigned &Idx) {
3920 NameInfo.setName(ReadDeclarationName(Record, Idx));
3921 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
3922 DeclarationNameLoc DNLoc;
3923 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
3924 NameInfo.setInfo(DNLoc);
3925}
3926
3927void ASTReader::ReadQualifierInfo(PerFileData &F, QualifierInfo &Info,
3928 const RecordData &Record, unsigned &Idx) {
3929 Info.NNS = ReadNestedNameSpecifier(Record, Idx);
3930 Info.NNSRange = ReadSourceRange(F, Record, Idx);
3931 unsigned NumTPLists = Record[Idx++];
3932 Info.NumTemplParamLists = NumTPLists;
3933 if (NumTPLists) {
3934 Info.TemplParamLists = new (*Context) TemplateParameterList*[NumTPLists];
3935 for (unsigned i=0; i != NumTPLists; ++i)
3936 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
3937 }
3938}
3939
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003940TemplateName
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003941ASTReader::ReadTemplateName(const RecordData &Record, unsigned &Idx) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00003942 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003943 switch (Kind) {
3944 case TemplateName::Template:
3945 return TemplateName(cast_or_null<TemplateDecl>(GetDecl(Record[Idx++])));
3946
3947 case TemplateName::OverloadedTemplate: {
3948 unsigned size = Record[Idx++];
3949 UnresolvedSet<8> Decls;
3950 while (size--)
3951 Decls.addDecl(cast<NamedDecl>(GetDecl(Record[Idx++])));
3952
3953 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
3954 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003955
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003956 case TemplateName::QualifiedTemplate: {
3957 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3958 bool hasTemplKeyword = Record[Idx++];
3959 TemplateDecl *Template = cast<TemplateDecl>(GetDecl(Record[Idx++]));
3960 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
3961 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003962
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003963 case TemplateName::DependentTemplate: {
3964 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3965 if (Record[Idx++]) // isIdentifier
3966 return Context->getDependentTemplateName(NNS,
3967 GetIdentifierInfo(Record, Idx));
3968 return Context->getDependentTemplateName(NNS,
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003969 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003970 }
3971 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003972
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003973 assert(0 && "Unhandled template name kind!");
3974 return TemplateName();
3975}
3976
3977TemplateArgument
Sebastian Redlc3632732010-10-05 15:59:54 +00003978ASTReader::ReadTemplateArgument(PerFileData &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00003979 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003980 switch ((TemplateArgument::ArgKind)Record[Idx++]) {
3981 case TemplateArgument::Null:
3982 return TemplateArgument();
3983 case TemplateArgument::Type:
3984 return TemplateArgument(GetType(Record[Idx++]));
3985 case TemplateArgument::Declaration:
3986 return TemplateArgument(GetDecl(Record[Idx++]));
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00003987 case TemplateArgument::Integral: {
3988 llvm::APSInt Value = ReadAPSInt(Record, Idx);
3989 QualType T = GetType(Record[Idx++]);
3990 return TemplateArgument(Value, T);
3991 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003992 case TemplateArgument::Template:
3993 return TemplateArgument(ReadTemplateName(Record, Idx));
3994 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00003995 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003996 case TemplateArgument::Pack: {
3997 unsigned NumArgs = Record[Idx++];
3998 llvm::SmallVector<TemplateArgument, 8> Args;
3999 Args.reserve(NumArgs);
4000 while (NumArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00004001 Args.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004002 TemplateArgument TemplArg;
4003 TemplArg.setArgumentPack(Args.data(), Args.size(), /*CopyArgs=*/true);
4004 return TemplArg;
4005 }
4006 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004007
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004008 assert(0 && "Unhandled template argument kind!");
4009 return TemplateArgument();
4010}
4011
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004012TemplateParameterList *
Sebastian Redlc3632732010-10-05 15:59:54 +00004013ASTReader::ReadTemplateParameterList(PerFileData &F,
4014 const RecordData &Record, unsigned &Idx) {
4015 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
4016 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
4017 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004018
4019 unsigned NumParams = Record[Idx++];
4020 llvm::SmallVector<NamedDecl *, 16> Params;
4021 Params.reserve(NumParams);
4022 while (NumParams--)
4023 Params.push_back(cast<NamedDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer20249a12010-10-21 03:16:25 +00004024
4025 TemplateParameterList* TemplateParams =
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004026 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
4027 Params.data(), Params.size(), RAngleLoc);
4028 return TemplateParams;
4029}
4030
4031void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004032ASTReader::
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004033ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs,
Sebastian Redlc3632732010-10-05 15:59:54 +00004034 PerFileData &F, const RecordData &Record,
4035 unsigned &Idx) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004036 unsigned NumTemplateArgs = Record[Idx++];
4037 TemplArgs.reserve(NumTemplateArgs);
4038 while (NumTemplateArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00004039 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004040}
4041
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004042/// \brief Read a UnresolvedSet structure.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004043void ASTReader::ReadUnresolvedSet(UnresolvedSetImpl &Set,
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004044 const RecordData &Record, unsigned &Idx) {
4045 unsigned NumDecls = Record[Idx++];
4046 while (NumDecls--) {
4047 NamedDecl *D = cast<NamedDecl>(GetDecl(Record[Idx++]));
4048 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
4049 Set.addDecl(D, AS);
4050 }
4051}
4052
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004053CXXBaseSpecifier
Sebastian Redlc3632732010-10-05 15:59:54 +00004054ASTReader::ReadCXXBaseSpecifier(PerFileData &F,
Nick Lewycky56062202010-07-26 16:56:01 +00004055 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004056 bool isVirtual = static_cast<bool>(Record[Idx++]);
4057 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
4058 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00004059 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
4060 SourceRange Range = ReadSourceRange(F, Record, Idx);
Nick Lewycky56062202010-07-26 16:56:01 +00004061 return CXXBaseSpecifier(Range, isVirtual, isBaseOfClass, AS, TInfo);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004062}
4063
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004064std::pair<CXXBaseOrMemberInitializer **, unsigned>
Sebastian Redlc3632732010-10-05 15:59:54 +00004065ASTReader::ReadCXXBaseOrMemberInitializers(PerFileData &F,
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004066 const RecordData &Record,
4067 unsigned &Idx) {
4068 CXXBaseOrMemberInitializer **BaseOrMemberInitializers = 0;
4069 unsigned NumInitializers = Record[Idx++];
4070 if (NumInitializers) {
4071 ASTContext &C = *getContext();
4072
4073 BaseOrMemberInitializers
4074 = new (C) CXXBaseOrMemberInitializer*[NumInitializers];
4075 for (unsigned i=0; i != NumInitializers; ++i) {
4076 TypeSourceInfo *BaseClassInfo = 0;
4077 bool IsBaseVirtual = false;
4078 FieldDecl *Member = 0;
Michael J. Spencer20249a12010-10-21 03:16:25 +00004079
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004080 bool IsBaseInitializer = Record[Idx++];
4081 if (IsBaseInitializer) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004082 BaseClassInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004083 IsBaseVirtual = Record[Idx++];
4084 } else {
4085 Member = cast<FieldDecl>(GetDecl(Record[Idx++]));
4086 }
Sebastian Redlc3632732010-10-05 15:59:54 +00004087 SourceLocation MemberLoc = ReadSourceLocation(F, Record, Idx);
4088 Expr *Init = ReadExpr(F);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004089 FieldDecl *AnonUnionMember
4090 = cast_or_null<FieldDecl>(GetDecl(Record[Idx++]));
Sebastian Redlc3632732010-10-05 15:59:54 +00004091 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
4092 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004093 bool IsWritten = Record[Idx++];
4094 unsigned SourceOrderOrNumArrayIndices;
4095 llvm::SmallVector<VarDecl *, 8> Indices;
4096 if (IsWritten) {
4097 SourceOrderOrNumArrayIndices = Record[Idx++];
4098 } else {
4099 SourceOrderOrNumArrayIndices = Record[Idx++];
4100 Indices.reserve(SourceOrderOrNumArrayIndices);
4101 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
4102 Indices.push_back(cast<VarDecl>(GetDecl(Record[Idx++])));
4103 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004104
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004105 CXXBaseOrMemberInitializer *BOMInit;
4106 if (IsBaseInitializer) {
4107 BOMInit = new (C) CXXBaseOrMemberInitializer(C, BaseClassInfo,
4108 IsBaseVirtual, LParenLoc,
4109 Init, RParenLoc);
4110 } else if (IsWritten) {
4111 BOMInit = new (C) CXXBaseOrMemberInitializer(C, Member, MemberLoc,
4112 LParenLoc, Init, RParenLoc);
4113 } else {
4114 BOMInit = CXXBaseOrMemberInitializer::Create(C, Member, MemberLoc,
4115 LParenLoc, Init, RParenLoc,
4116 Indices.data(),
4117 Indices.size());
4118 }
4119
Argyrios Kyrtzidisf84cde12010-09-06 19:04:27 +00004120 if (IsWritten)
4121 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004122 BOMInit->setAnonUnionMember(AnonUnionMember);
4123 BaseOrMemberInitializers[i] = BOMInit;
4124 }
4125 }
4126
4127 return std::make_pair(BaseOrMemberInitializers, NumInitializers);
4128}
4129
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004130NestedNameSpecifier *
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004131ASTReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004132 unsigned N = Record[Idx++];
4133 NestedNameSpecifier *NNS = 0, *Prev = 0;
4134 for (unsigned I = 0; I != N; ++I) {
4135 NestedNameSpecifier::SpecifierKind Kind
4136 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
4137 switch (Kind) {
4138 case NestedNameSpecifier::Identifier: {
4139 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
4140 NNS = NestedNameSpecifier::Create(*Context, Prev, II);
4141 break;
4142 }
4143
4144 case NestedNameSpecifier::Namespace: {
4145 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
4146 NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
4147 break;
4148 }
4149
4150 case NestedNameSpecifier::TypeSpec:
4151 case NestedNameSpecifier::TypeSpecWithTemplate: {
4152 Type *T = GetType(Record[Idx++]).getTypePtr();
4153 bool Template = Record[Idx++];
4154 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
4155 break;
4156 }
4157
4158 case NestedNameSpecifier::Global: {
4159 NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
4160 // No associated value, and there can't be a prefix.
4161 break;
4162 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004163 }
Argyrios Kyrtzidisd2bb2c02010-07-07 15:46:30 +00004164 Prev = NNS;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004165 }
4166 return NNS;
4167}
4168
4169SourceRange
Sebastian Redlc3632732010-10-05 15:59:54 +00004170ASTReader::ReadSourceRange(PerFileData &F, const RecordData &Record,
4171 unsigned &Idx) {
4172 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
4173 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar8ee59392010-06-02 15:47:10 +00004174 return SourceRange(beg, end);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004175}
4176
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004177/// \brief Read an integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004178llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004179 unsigned BitWidth = Record[Idx++];
4180 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
4181 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
4182 Idx += NumWords;
4183 return Result;
4184}
4185
4186/// \brief Read a signed integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004187llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004188 bool isUnsigned = Record[Idx++];
4189 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
4190}
4191
Douglas Gregor17fc2232009-04-14 21:55:33 +00004192/// \brief Read a floating-point value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004193llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00004194 return llvm::APFloat(ReadAPInt(Record, Idx));
4195}
4196
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004197// \brief Read a string
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004198std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004199 unsigned Len = Record[Idx++];
Jay Foadbeaaccd2009-05-21 09:52:38 +00004200 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004201 Idx += Len;
4202 return Result;
4203}
4204
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004205CXXTemporary *ASTReader::ReadCXXTemporary(const RecordData &Record,
Chris Lattnerd2598362010-05-10 00:25:06 +00004206 unsigned &Idx) {
4207 CXXDestructorDecl *Decl = cast<CXXDestructorDecl>(GetDecl(Record[Idx++]));
4208 return CXXTemporary::Create(*Context, Decl);
4209}
4210
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004211DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00004212 return Diag(SourceLocation(), DiagID);
4213}
4214
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004215DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00004216 return Diags.Report(FullSourceLoc(Loc, SourceMgr), DiagID);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00004217}
Douglas Gregor025452f2009-04-17 00:04:06 +00004218
Douglas Gregor668c1a42009-04-21 22:25:48 +00004219/// \brief Retrieve the identifier table associated with the
4220/// preprocessor.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004221IdentifierTable &ASTReader::getIdentifierTable() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00004222 assert(PP && "Forgot to set Preprocessor ?");
4223 return PP->getIdentifierTable();
Douglas Gregor668c1a42009-04-21 22:25:48 +00004224}
4225
Douglas Gregor025452f2009-04-17 00:04:06 +00004226/// \brief Record that the given ID maps to the given switch-case
4227/// statement.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004228void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Douglas Gregor025452f2009-04-17 00:04:06 +00004229 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
4230 SwitchCaseStmts[ID] = SC;
4231}
4232
4233/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004234SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Douglas Gregor025452f2009-04-17 00:04:06 +00004235 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
4236 return SwitchCaseStmts[ID];
4237}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00004238
4239/// \brief Record that the given label statement has been
4240/// deserialized and has the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004241void ASTReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
Mike Stump1eb44332009-09-09 15:08:12 +00004242 assert(LabelStmts.find(ID) == LabelStmts.end() &&
Douglas Gregor1de05fe2009-04-17 18:18:49 +00004243 "Deserialized label twice");
4244 LabelStmts[ID] = S;
4245
4246 // If we've already seen any goto statements that point to this
4247 // label, resolve them now.
4248 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
4249 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
4250 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
4251 Goto->second->setLabel(S);
4252 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00004253
4254 // If we've already seen any address-label statements that point to
4255 // this label, resolve them now.
4256 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
Mike Stump1eb44332009-09-09 15:08:12 +00004257 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00004258 = UnresolvedAddrLabelExprs.equal_range(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00004259 for (AddrLabelIter AddrLabel = AddrLabels.first;
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00004260 AddrLabel != AddrLabels.second; ++AddrLabel)
4261 AddrLabel->second->setLabel(S);
4262 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor1de05fe2009-04-17 18:18:49 +00004263}
4264
4265/// \brief Set the label of the given statement to the label
4266/// identified by ID.
4267///
4268/// Depending on the order in which the label and other statements
4269/// referencing that label occur, this operation may complete
4270/// immediately (updating the statement) or it may queue the
4271/// statement to be back-patched later.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004272void ASTReader::SetLabelOf(GotoStmt *S, unsigned ID) {
Douglas Gregor1de05fe2009-04-17 18:18:49 +00004273 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
4274 if (Label != LabelStmts.end()) {
4275 // We've already seen this label, so set the label of the goto and
4276 // we're done.
4277 S->setLabel(Label->second);
4278 } else {
4279 // We haven't seen this label yet, so add this goto to the set of
4280 // unresolved goto statements.
4281 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
4282 }
4283}
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00004284
4285/// \brief Set the label of the given expression to the label
4286/// identified by ID.
4287///
4288/// Depending on the order in which the label and other statements
4289/// referencing that label occur, this operation may complete
4290/// immediately (updating the statement) or it may queue the
4291/// statement to be back-patched later.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004292void ASTReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00004293 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
4294 if (Label != LabelStmts.end()) {
4295 // We've already seen this label, so set the label of the
4296 // label-address expression and we're done.
4297 S->setLabel(Label->second);
4298 } else {
4299 // We haven't seen this label yet, so add this label-address
4300 // expression to the set of unresolved label-address expressions.
4301 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
4302 }
4303}
Douglas Gregord89275b2009-07-06 18:54:52 +00004304
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004305void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004306 assert(NumCurrentElementsDeserializing &&
4307 "FinishedDeserializing not paired with StartedDeserializing");
4308 if (NumCurrentElementsDeserializing == 1) {
Douglas Gregord89275b2009-07-06 18:54:52 +00004309 // If any identifiers with corresponding top-level declarations have
4310 // been loaded, load those declarations now.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004311 while (!PendingIdentifierInfos.empty()) {
4312 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
4313 PendingIdentifierInfos.front().DeclIDs, true);
4314 PendingIdentifierInfos.pop_front();
Douglas Gregord89275b2009-07-06 18:54:52 +00004315 }
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00004316
4317 // We are not in recursive loading, so it's safe to pass the "interesting"
4318 // decls to the consumer.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004319 if (Consumer)
4320 PassInterestingDeclsToConsumer();
Douglas Gregord89275b2009-07-06 18:54:52 +00004321 }
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004322 --NumCurrentElementsDeserializing;
Douglas Gregord89275b2009-07-06 18:54:52 +00004323}
Douglas Gregor501c1032010-08-19 00:28:17 +00004324
Sebastian Redle1dde812010-08-24 00:50:04 +00004325ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
4326 const char *isysroot, bool DisableValidation)
4327 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
4328 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
4329 Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
4330 Consumer(0), isysroot(isysroot), DisableValidation(DisableValidation),
4331 NumStatHits(0), NumStatMisses(0), NumSLocEntriesRead(0),
Sebastian Redl8db9fae2010-09-22 20:19:08 +00004332 TotalNumSLocEntries(0), NextSLocOffset(0), NumStatementsRead(0),
4333 TotalNumStatements(0), NumMacrosRead(0), TotalNumMacros(0),
4334 NumSelectorsRead(0), NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
Sebastian Redle1dde812010-08-24 00:50:04 +00004335 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
4336 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
4337 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
4338 RelocatablePCH = false;
4339}
4340
4341ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
4342 Diagnostic &Diags, const char *isysroot,
4343 bool DisableValidation)
4344 : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
4345 Diags(Diags), SemaObj(0), PP(0), Context(0), Consumer(0),
4346 isysroot(isysroot), DisableValidation(DisableValidation), NumStatHits(0),
4347 NumStatMisses(0), NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Sebastian Redl8db9fae2010-09-22 20:19:08 +00004348 NextSLocOffset(0), NumStatementsRead(0), TotalNumStatements(0),
4349 NumMacrosRead(0), TotalNumMacros(0), NumSelectorsRead(0),
4350 NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
4351 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
4352 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
4353 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
Sebastian Redle1dde812010-08-24 00:50:04 +00004354 RelocatablePCH = false;
4355}
4356
4357ASTReader::~ASTReader() {
4358 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
4359 delete Chain[e - i - 1];
4360 // Delete all visible decl lookup tables
4361 for (DeclContextOffsetsMap::iterator I = DeclContextOffsets.begin(),
4362 E = DeclContextOffsets.end();
4363 I != E; ++I) {
4364 for (DeclContextInfos::iterator J = I->second.begin(), F = I->second.end();
4365 J != F; ++J) {
4366 if (J->NameLookupTableData)
4367 delete static_cast<ASTDeclContextNameLookupTable*>(
4368 J->NameLookupTableData);
4369 }
4370 }
4371 for (DeclContextVisibleUpdatesPending::iterator
4372 I = PendingVisibleUpdates.begin(),
4373 E = PendingVisibleUpdates.end();
4374 I != E; ++I) {
4375 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
4376 F = I->second.end();
4377 J != F; ++J)
4378 delete static_cast<ASTDeclContextNameLookupTable*>(*J);
4379 }
4380}
4381
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00004382ASTReader::PerFileData::PerFileData(ASTFileType Ty)
4383 : Type(Ty), SizeInBits(0), LocalNumSLocEntries(0), SLocOffsets(0), LocalSLocSize(0),
Sebastian Redl301c9b02010-09-22 00:42:27 +00004384 LocalNumIdentifiers(0), IdentifierOffsets(0), IdentifierTableData(0),
4385 IdentifierLookupTable(0), LocalNumMacroDefinitions(0),
4386 MacroDefinitionOffsets(0), LocalNumSelectors(0), SelectorOffsets(0),
4387 SelectorLookupTableData(0), SelectorLookupTable(0), LocalNumDecls(0),
4388 DeclOffsets(0), LocalNumTypes(0), TypeOffsets(0), StatCache(0),
Sebastian Redla866e652010-10-01 19:59:12 +00004389 NumPreallocatedPreprocessingEntities(0), NextInSource(0)
Douglas Gregor501c1032010-08-19 00:28:17 +00004390{}
4391
4392ASTReader::PerFileData::~PerFileData() {
4393 delete static_cast<ASTIdentifierLookupTable *>(IdentifierLookupTable);
4394 delete static_cast<ASTSelectorLookupTable *>(SelectorLookupTable);
4395}
4396