blob: d0edd71cd1e91400e95fea09142cd283f466167e [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"
Chris Lattner10e286a2010-11-23 19:19:34 +000036#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000037#include "clang/Basic/TargetInfo.h"
Douglas Gregor445e23e2009-10-05 21:07:28 +000038#include "clang/Basic/Version.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000039#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000040#include "llvm/Bitcode/BitstreamReader.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000041#include "llvm/Support/MemoryBuffer.h"
John McCall833ca992009-10-29 08:12:44 +000042#include "llvm/Support/ErrorHandling.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000043#include "llvm/Support/Path.h"
Michael J. Spencer3a321e22010-12-09 17:36:38 +000044#include "llvm/Support/system_error.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000045#include <algorithm>
Douglas Gregore721f952009-04-28 18:58:38 +000046#include <iterator>
Douglas Gregor2cf26342009-04-09 22:27:44 +000047#include <cstdio>
Douglas Gregor4fed3f42009-04-27 18:38:38 +000048#include <sys/stat.h>
Douglas Gregor2cf26342009-04-09 22:27:44 +000049using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000050using namespace clang::serialization;
Douglas Gregor2cf26342009-04-09 22:27:44 +000051
52//===----------------------------------------------------------------------===//
Sebastian Redl3c7f4132010-08-18 23:57:06 +000053// PCH validator implementation
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000054//===----------------------------------------------------------------------===//
55
Sebastian Redl571db7f2010-08-18 23:56:56 +000056ASTReaderListener::~ASTReaderListener() {}
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000057
58bool
59PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts) {
60 const LangOptions &PPLangOpts = PP.getLangOptions();
61#define PARSE_LANGOPT_BENIGN(Option)
62#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
63 if (PPLangOpts.Option != LangOpts.Option) { \
64 Reader.Diag(DiagID) << LangOpts.Option << PPLangOpts.Option; \
65 return true; \
66 }
67
68 PARSE_LANGOPT_BENIGN(Trigraphs);
69 PARSE_LANGOPT_BENIGN(BCPLComment);
70 PARSE_LANGOPT_BENIGN(DollarIdents);
71 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
72 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
Chandler Carrutheb5d7b72010-04-17 20:17:31 +000073 PARSE_LANGOPT_IMPORTANT(GNUKeywords, diag::warn_pch_gnu_keywords);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000074 PARSE_LANGOPT_BENIGN(ImplicitInt);
75 PARSE_LANGOPT_BENIGN(Digraphs);
76 PARSE_LANGOPT_BENIGN(HexFloats);
77 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
78 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
Michael J. Spencerdae4ac42010-10-21 05:21:48 +000079 PARSE_LANGOPT_BENIGN(MSCVersion);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000080 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
81 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
82 PARSE_LANGOPT_BENIGN(CXXOperatorName);
83 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
84 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
85 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
Fariborz Jahanian412e7982010-02-09 19:31:38 +000086 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI2, diag::warn_pch_nonfragile_abi2);
Fariborz Jahanianf84109e2011-01-07 18:59:25 +000087 PARSE_LANGOPT_IMPORTANT(AppleKext, diag::warn_pch_apple_kext);
Ted Kremenekc32647d2010-12-23 21:35:43 +000088 PARSE_LANGOPT_IMPORTANT(ObjCDefaultSynthProperties,
89 diag::warn_pch_objc_auto_properties);
Michael J. Spencer20249a12010-10-21 03:16:25 +000090 PARSE_LANGOPT_IMPORTANT(NoConstantCFStrings,
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +000091 diag::warn_pch_no_constant_cfstrings);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000092 PARSE_LANGOPT_BENIGN(PascalStrings);
93 PARSE_LANGOPT_BENIGN(WritableStrings);
Mike Stump1eb44332009-09-09 15:08:12 +000094 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000095 diag::warn_pch_lax_vector_conversions);
Nate Begeman69cfb9b2009-06-25 22:57:40 +000096 PARSE_LANGOPT_IMPORTANT(AltiVec, diag::warn_pch_altivec);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000097 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
Daniel Dunbar73482882010-02-10 18:48:44 +000098 PARSE_LANGOPT_IMPORTANT(SjLjExceptions, diag::warn_pch_sjlj_exceptions);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000099 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
100 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
101 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
Mike Stump1eb44332009-09-09 15:08:12 +0000102 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000103 diag::warn_pch_thread_safe_statics);
Daniel Dunbar5345c392009-09-03 04:54:28 +0000104 PARSE_LANGOPT_IMPORTANT(POSIXThreads, diag::warn_pch_posix_threads);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000105 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
106 PARSE_LANGOPT_BENIGN(EmitAllDecls);
107 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
Chris Lattnera4d71452010-06-26 21:25:03 +0000108 PARSE_LANGOPT_BENIGN(getSignedOverflowBehavior());
Mike Stump1eb44332009-09-09 15:08:12 +0000109 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000110 diag::warn_pch_heinous_extensions);
111 // FIXME: Most of the options below are benign if the macro wasn't
112 // used. Unfortunately, this means that a PCH compiled without
113 // optimization can't be used with optimization turned on, even
114 // though the only thing that changes is whether __OPTIMIZE__ was
115 // defined... but if __OPTIMIZE__ never showed up in the header, it
116 // doesn't matter. We could consider making this some special kind
117 // of check.
118 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
119 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
120 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
121 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
122 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
123 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
124 PARSE_LANGOPT_IMPORTANT(AccessControl, diag::warn_pch_access_control);
125 PARSE_LANGOPT_IMPORTANT(CharIsSigned, diag::warn_pch_char_signed);
John Thompsona6fda122009-11-05 20:14:16 +0000126 PARSE_LANGOPT_IMPORTANT(ShortWChar, diag::warn_pch_short_wchar);
Argyrios Kyrtzidis9a2b9d72010-10-08 00:25:19 +0000127 PARSE_LANGOPT_IMPORTANT(ShortEnums, diag::warn_pch_short_enums);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000128 if ((PPLangOpts.getGCMode() != 0) != (LangOpts.getGCMode() != 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000129 Reader.Diag(diag::warn_pch_gc_mode)
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000130 << LangOpts.getGCMode() << PPLangOpts.getGCMode();
131 return true;
132 }
133 PARSE_LANGOPT_BENIGN(getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000134 PARSE_LANGOPT_IMPORTANT(getStackProtectorMode(),
135 diag::warn_pch_stack_protector);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000136 PARSE_LANGOPT_BENIGN(InstantiationDepth);
Nate Begeman69cfb9b2009-06-25 22:57:40 +0000137 PARSE_LANGOPT_IMPORTANT(OpenCL, diag::warn_pch_opencl);
Peter Collingbourne08a53262010-12-01 19:14:57 +0000138 PARSE_LANGOPT_IMPORTANT(CUDA, diag::warn_pch_cuda);
Mike Stump9c276ae2009-12-12 01:27:46 +0000139 PARSE_LANGOPT_BENIGN(CatchUndefined);
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000140 PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors);
Douglas Gregora0068fc2010-07-09 17:35:33 +0000141 PARSE_LANGOPT_BENIGN(SpellChecking);
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +0000142#undef PARSE_LANGOPT_IMPORTANT
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000143#undef PARSE_LANGOPT_BENIGN
144
145 return false;
146}
147
Daniel Dunbardc3c0d22009-11-11 00:52:11 +0000148bool PCHValidator::ReadTargetTriple(llvm::StringRef Triple) {
149 if (Triple == PP.getTargetInfo().getTriple().str())
150 return false;
151
152 Reader.Diag(diag::warn_pch_target_triple)
153 << Triple << PP.getTargetInfo().getTriple().str();
154 return true;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000155}
156
Benjamin Kramer54353f42010-11-25 18:29:30 +0000157namespace {
158 struct EmptyStringRef {
159 bool operator ()(llvm::StringRef r) const { return r.empty(); }
160 };
161 struct EmptyBlock {
162 bool operator ()(const PCHPredefinesBlock &r) const {return r.Data.empty();}
163 };
164}
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000165
166static bool EqualConcatenations(llvm::SmallVector<llvm::StringRef, 2> L,
167 PCHPredefinesBlocks R) {
168 // First, sum up the lengths.
169 unsigned LL = 0, RL = 0;
170 for (unsigned I = 0, N = L.size(); I != N; ++I) {
171 LL += L[I].size();
172 }
173 for (unsigned I = 0, N = R.size(); I != N; ++I) {
174 RL += R[I].Data.size();
175 }
176 if (LL != RL)
177 return false;
178 if (LL == 0 && RL == 0)
179 return true;
180
181 // Kick out empty parts, they confuse the algorithm below.
182 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
183 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
184
185 // Do it the hard way. At this point, both vectors must be non-empty.
186 llvm::StringRef LR = L[0], RR = R[0].Data;
187 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
Daniel Dunbarc76c9e02010-07-16 00:00:11 +0000188 (void) RN;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000189 for (;;) {
190 // Compare the current pieces.
191 if (LR.size() == RR.size()) {
192 // If they're the same length, it's pretty easy.
193 if (LR != RR)
194 return false;
195 // Both pieces are done, advance.
196 ++LI;
197 ++RI;
198 // If either string is done, they're both done, since they're the same
199 // length.
200 if (LI == LN) {
201 assert(RI == RN && "Strings not the same length after all?");
202 return true;
203 }
204 LR = L[LI];
205 RR = R[RI].Data;
206 } else if (LR.size() < RR.size()) {
207 // Right piece is longer.
208 if (!RR.startswith(LR))
209 return false;
210 ++LI;
211 assert(LI != LN && "Strings not the same length after all?");
212 RR = RR.substr(LR.size());
213 LR = L[LI];
214 } else {
215 // Left piece is longer.
216 if (!LR.startswith(RR))
217 return false;
218 ++RI;
219 assert(RI != RN && "Strings not the same length after all?");
220 LR = LR.substr(RR.size());
221 RR = R[RI].Data;
222 }
223 }
224}
225
226static std::pair<FileID, llvm::StringRef::size_type>
227FindMacro(const PCHPredefinesBlocks &Buffers, llvm::StringRef MacroDef) {
228 std::pair<FileID, llvm::StringRef::size_type> Res;
229 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
230 Res.second = Buffers[I].Data.find(MacroDef);
231 if (Res.second != llvm::StringRef::npos) {
232 Res.first = Buffers[I].BufferID;
233 break;
234 }
235 }
236 return Res;
237}
238
239bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000240 llvm::StringRef OriginalFileName,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000241 std::string &SuggestedPredefines) {
Daniel Dunbarc7162932009-11-11 23:58:53 +0000242 // We are in the context of an implicit include, so the predefines buffer will
243 // have a #include entry for the PCH file itself (as normalized by the
244 // preprocessor initialization). Find it and skip over it in the checking
245 // below.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000246 llvm::SmallString<256> PCHInclude;
247 PCHInclude += "#include \"";
Daniel Dunbarc7162932009-11-11 23:58:53 +0000248 PCHInclude += NormalizeDashIncludePath(OriginalFileName);
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000249 PCHInclude += "\"\n";
250 std::pair<llvm::StringRef,llvm::StringRef> Split =
251 llvm::StringRef(PP.getPredefines()).split(PCHInclude.str());
252 llvm::StringRef Left = Split.first, Right = Split.second;
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000253 if (Left == PP.getPredefines()) {
254 Error("Missing PCH include entry!");
255 return true;
256 }
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000257
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000258 // If the concatenation of all the PCH buffers is equal to the adjusted
259 // command line, we're done.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000260 llvm::SmallVector<llvm::StringRef, 2> CommandLine;
261 CommandLine.push_back(Left);
262 CommandLine.push_back(Right);
263 if (EqualConcatenations(CommandLine, Buffers))
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000264 return false;
265
266 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000267
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000268 // The predefines buffers are different. Determine what the differences are,
269 // and whether they require us to reject the PCH file.
Daniel Dunbare6750492009-11-13 16:46:11 +0000270 llvm::SmallVector<llvm::StringRef, 8> PCHLines;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000271 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
272 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Daniel Dunbare6750492009-11-13 16:46:11 +0000273
274 llvm::SmallVector<llvm::StringRef, 8> CmdLineLines;
275 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000276
277 // Pick out implicit #includes after the PCH and don't consider them for
278 // validation; we will insert them into SuggestedPredefines so that the
279 // preprocessor includes them.
280 std::string IncludesAfterPCH;
281 llvm::SmallVector<llvm::StringRef, 8> AfterPCHLines;
282 Right.split(AfterPCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
283 for (unsigned i = 0, e = AfterPCHLines.size(); i != e; ++i) {
284 if (AfterPCHLines[i].startswith("#include ")) {
285 IncludesAfterPCH += AfterPCHLines[i];
286 IncludesAfterPCH += '\n';
287 } else {
288 CmdLineLines.push_back(AfterPCHLines[i]);
289 }
290 }
291
292 // Make sure we add the includes last into SuggestedPredefines before we
293 // exit this function.
294 struct AddIncludesRAII {
295 std::string &SuggestedPredefines;
296 std::string &IncludesAfterPCH;
297
298 AddIncludesRAII(std::string &SuggestedPredefines,
299 std::string &IncludesAfterPCH)
300 : SuggestedPredefines(SuggestedPredefines),
301 IncludesAfterPCH(IncludesAfterPCH) { }
302 ~AddIncludesRAII() {
303 SuggestedPredefines += IncludesAfterPCH;
304 }
305 } AddIncludes(SuggestedPredefines, IncludesAfterPCH);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000306
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000307 // Sort both sets of predefined buffer lines, since we allow some extra
308 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000309 std::sort(CmdLineLines.begin(), CmdLineLines.end());
310 std::sort(PCHLines.begin(), PCHLines.end());
311
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000312 // Determine which predefines that were used to build the PCH file are missing
313 // from the command line.
314 std::vector<llvm::StringRef> MissingPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000315 std::set_difference(PCHLines.begin(), PCHLines.end(),
316 CmdLineLines.begin(), CmdLineLines.end(),
317 std::back_inserter(MissingPredefines));
318
319 bool MissingDefines = false;
320 bool ConflictingDefines = false;
321 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000322 llvm::StringRef Missing = MissingPredefines[I];
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000323 if (Missing.startswith("#include ")) {
324 // An -include was specified when generating the PCH; it is included in
325 // the PCH, just ignore it.
326 continue;
327 }
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000328 if (!Missing.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000329 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
330 return true;
331 }
Mike Stump1eb44332009-09-09 15:08:12 +0000332
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000333 // This is a macro definition. Determine the name of the macro we're
334 // defining.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000335 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000336 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000337 = Missing.find_first_of("( \n\r", StartOfMacroName);
338 assert(EndOfMacroName != std::string::npos &&
339 "Couldn't find the end of the macro name");
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000340 llvm::StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000341
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000342 // Determine whether this macro was given a different definition on the
343 // command line.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000344 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000345 std::string::size_type MacroDefLen = MacroDefStart.size();
Daniel Dunbare6750492009-11-13 16:46:11 +0000346 llvm::SmallVector<llvm::StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000347 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
348 MacroDefStart);
349 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000350 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000351 // Different macro; we're done.
352 ConflictPos = CmdLineLines.end();
Mike Stump1eb44332009-09-09 15:08:12 +0000353 break;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000354 }
Mike Stump1eb44332009-09-09 15:08:12 +0000355
356 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000357 "Invalid #define in predefines buffer?");
Mike Stump1eb44332009-09-09 15:08:12 +0000358 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000359 (*ConflictPos)[MacroDefLen] != '(')
360 continue; // Longer macro name; keep trying.
Mike Stump1eb44332009-09-09 15:08:12 +0000361
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000362 // We found a conflicting macro definition.
363 break;
364 }
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000366 if (ConflictPos != CmdLineLines.end()) {
367 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
368 << MacroName;
369
370 // Show the definition of this macro within the PCH file.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000371 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
372 FindMacro(Buffers, Missing);
373 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
374 SourceLocation PCHMissingLoc =
375 SourceMgr.getLocForStartOfFile(MacroLoc.first)
376 .getFileLocWithOffset(MacroLoc.second);
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000377 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000378
379 ConflictingDefines = true;
380 continue;
381 }
Mike Stump1eb44332009-09-09 15:08:12 +0000382
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000383 // If the macro doesn't conflict, then we'll just pick up the macro
384 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000385 if (ConflictingDefines)
386 continue; // Don't complain if there are already conflicting defs
Mike Stump1eb44332009-09-09 15:08:12 +0000387
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000388 if (!MissingDefines) {
389 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
390 MissingDefines = true;
391 }
392
393 // Show the definition of this macro within the PCH file.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000394 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
395 FindMacro(Buffers, Missing);
396 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
397 SourceLocation PCHMissingLoc =
398 SourceMgr.getLocForStartOfFile(MacroLoc.first)
399 .getFileLocWithOffset(MacroLoc.second);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000400 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
401 }
Mike Stump1eb44332009-09-09 15:08:12 +0000402
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000403 if (ConflictingDefines)
404 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000405
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000406 // Determine what predefines were introduced based on command-line
407 // parameters that were not present when building the PCH
408 // file. Extra #defines are okay, so long as the identifiers being
409 // defined were not used within the precompiled header.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000410 std::vector<llvm::StringRef> ExtraPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000411 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
412 PCHLines.begin(), PCHLines.end(),
Mike Stump1eb44332009-09-09 15:08:12 +0000413 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000414 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000415 llvm::StringRef &Extra = ExtraPredefines[I];
416 if (!Extra.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000417 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
418 return true;
419 }
420
421 // This is an extra macro definition. Determine the name of the
422 // macro we're defining.
423 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000424 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000425 = Extra.find_first_of("( \n\r", StartOfMacroName);
426 assert(EndOfMacroName != std::string::npos &&
427 "Couldn't find the end of the macro name");
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000428 llvm::StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000429
430 // Check whether this name was used somewhere in the PCH file. If
431 // so, defining it as a macro could change behavior, so we reject
432 // the PCH file.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000433 if (IdentifierInfo *II = Reader.get(MacroName)) {
Daniel Dunbar4fda42e2009-11-11 00:52:00 +0000434 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000435 return true;
436 }
437
438 // Add this definition to the suggested predefines buffer.
439 SuggestedPredefines += Extra;
440 SuggestedPredefines += '\n';
441 }
442
443 // If we get here, it's because the predefines buffer had compatible
444 // contents. Accept the PCH file.
445 return false;
446}
447
Douglas Gregor12fab312010-03-16 16:35:32 +0000448void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
449 unsigned ID) {
450 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
451 ++NumHeaderInfos;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000452}
453
454void PCHValidator::ReadCounter(unsigned Value) {
455 PP.setCounterValue(Value);
456}
457
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000458//===----------------------------------------------------------------------===//
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000459// AST reader implementation
Douglas Gregor668c1a42009-04-21 22:25:48 +0000460//===----------------------------------------------------------------------===//
461
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000462void
Sebastian Redl571db7f2010-08-18 23:56:56 +0000463ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000464 DeserializationListener = Listener;
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000465}
466
Chris Lattner4c6f9522009-04-27 05:14:47 +0000467
Douglas Gregor668c1a42009-04-21 22:25:48 +0000468namespace {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000469class ASTSelectorLookupTrait {
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000470 ASTReader &Reader;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000471
472public:
Sebastian Redl5d050072010-08-04 17:20:04 +0000473 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000474 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +0000475 ObjCMethodList Instance, Factory;
476 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000477
478 typedef Selector external_key_type;
479 typedef external_key_type internal_key_type;
480
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000481 explicit ASTSelectorLookupTrait(ASTReader &Reader) : Reader(Reader) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000482
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000483 static bool EqualKey(const internal_key_type& a,
484 const internal_key_type& b) {
485 return a == b;
486 }
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000488 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +0000489 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000490 }
Mike Stump1eb44332009-09-09 15:08:12 +0000491
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000492 // This hopefully will just get inlined and removed by the optimizer.
493 static const internal_key_type&
494 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump1eb44332009-09-09 15:08:12 +0000495
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000496 static std::pair<unsigned, unsigned>
497 ReadKeyDataLength(const unsigned char*& d) {
498 using namespace clang::io;
499 unsigned KeyLen = ReadUnalignedLE16(d);
500 unsigned DataLen = ReadUnalignedLE16(d);
501 return std::make_pair(KeyLen, DataLen);
502 }
Mike Stump1eb44332009-09-09 15:08:12 +0000503
Douglas Gregor83941df2009-04-25 17:48:32 +0000504 internal_key_type ReadKey(const unsigned char* d, unsigned) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000505 using namespace clang::io;
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000506 SelectorTable &SelTable = Reader.getContext()->Selectors;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000507 unsigned N = ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +0000508 IdentifierInfo *FirstII
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000509 = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
510 if (N == 0)
511 return SelTable.getNullarySelector(FirstII);
512 else if (N == 1)
513 return SelTable.getUnarySelector(FirstII);
514
515 llvm::SmallVector<IdentifierInfo *, 16> Args;
516 Args.push_back(FirstII);
517 for (unsigned I = 1; I != N; ++I)
518 Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)));
519
Douglas Gregor75fdb232009-05-22 22:45:36 +0000520 return SelTable.getSelector(N, Args.data());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000521 }
Mike Stump1eb44332009-09-09 15:08:12 +0000522
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000523 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
524 using namespace clang::io;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000525
526 data_type Result;
527
Sebastian Redl5d050072010-08-04 17:20:04 +0000528 Result.ID = ReadUnalignedLE32(d);
529 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
530 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
531
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000532 // Load instance methods
533 ObjCMethodList *Prev = 0;
534 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000535 ObjCMethodDecl *Method
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000536 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl5d050072010-08-04 17:20:04 +0000537 if (!Result.Instance.Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000538 // This is the first method, which is the easy case.
Sebastian Redl5d050072010-08-04 17:20:04 +0000539 Result.Instance.Method = Method;
540 Prev = &Result.Instance;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000541 continue;
542 }
543
Ted Kremenek298ed872010-02-11 00:53:01 +0000544 ObjCMethodList *Mem =
545 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
546 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000547 Prev = Prev->Next;
548 }
549
550 // Load factory methods
551 Prev = 0;
552 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000553 ObjCMethodDecl *Method
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000554 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl5d050072010-08-04 17:20:04 +0000555 if (!Result.Factory.Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000556 // This is the first method, which is the easy case.
Sebastian Redl5d050072010-08-04 17:20:04 +0000557 Result.Factory.Method = Method;
558 Prev = &Result.Factory;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000559 continue;
560 }
561
Ted Kremenek298ed872010-02-11 00:53:01 +0000562 ObjCMethodList *Mem =
563 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
564 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000565 Prev = Prev->Next;
566 }
567
568 return Result;
569 }
570};
Mike Stump1eb44332009-09-09 15:08:12 +0000571
572} // end anonymous namespace
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000573
574/// \brief The on-disk hash table used for the global method pool.
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000575typedef OnDiskChainedHashTable<ASTSelectorLookupTrait>
576 ASTSelectorLookupTable;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000577
Sebastian Redlc3632732010-10-05 15:59:54 +0000578namespace clang {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000579class ASTIdentifierLookupTrait {
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000580 ASTReader &Reader;
Sebastian Redlc3632732010-10-05 15:59:54 +0000581 ASTReader::PerFileData &F;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000582
583 // If we know the IdentifierInfo in advance, it is here and we will
584 // not build a new one. Used when deserializing information about an
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000585 // identifier that was constructed before the AST file was read.
Douglas Gregor668c1a42009-04-21 22:25:48 +0000586 IdentifierInfo *KnownII;
587
588public:
589 typedef IdentifierInfo * data_type;
590
591 typedef const std::pair<const char*, unsigned> external_key_type;
592
593 typedef external_key_type internal_key_type;
594
Sebastian Redlc3632732010-10-05 15:59:54 +0000595 ASTIdentifierLookupTrait(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redld27d3fc2010-07-21 22:31:37 +0000596 IdentifierInfo *II = 0)
Sebastian Redlc3632732010-10-05 15:59:54 +0000597 : Reader(Reader), F(F), KnownII(II) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000598
Douglas Gregor668c1a42009-04-21 22:25:48 +0000599 static bool EqualKey(const internal_key_type& a,
600 const internal_key_type& b) {
601 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
602 : false;
603 }
Mike Stump1eb44332009-09-09 15:08:12 +0000604
Douglas Gregor668c1a42009-04-21 22:25:48 +0000605 static unsigned ComputeHash(const internal_key_type& a) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000606 return llvm::HashString(llvm::StringRef(a.first, a.second));
Douglas Gregor668c1a42009-04-21 22:25:48 +0000607 }
Mike Stump1eb44332009-09-09 15:08:12 +0000608
Douglas Gregor668c1a42009-04-21 22:25:48 +0000609 // This hopefully will just get inlined and removed by the optimizer.
610 static const internal_key_type&
611 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump1eb44332009-09-09 15:08:12 +0000612
Douglas Gregor95f42922010-10-14 22:11:03 +0000613 // This hopefully will just get inlined and removed by the optimizer.
614 static const external_key_type&
615 GetExternalKey(const internal_key_type& x) { return x; }
616
Douglas Gregor668c1a42009-04-21 22:25:48 +0000617 static std::pair<unsigned, unsigned>
618 ReadKeyDataLength(const unsigned char*& d) {
619 using namespace clang::io;
Douglas Gregor5f8e3302009-04-25 20:26:24 +0000620 unsigned DataLen = ReadUnalignedLE16(d);
Douglas Gregord6595a42009-04-25 21:04:17 +0000621 unsigned KeyLen = ReadUnalignedLE16(d);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000622 return std::make_pair(KeyLen, DataLen);
623 }
Mike Stump1eb44332009-09-09 15:08:12 +0000624
Douglas Gregor668c1a42009-04-21 22:25:48 +0000625 static std::pair<const char*, unsigned>
626 ReadKey(const unsigned char* d, unsigned n) {
627 assert(n >= 2 && d[n-1] == '\0');
628 return std::make_pair((const char*) d, n-1);
629 }
Mike Stump1eb44332009-09-09 15:08:12 +0000630
631 IdentifierInfo *ReadData(const internal_key_type& k,
Douglas Gregor668c1a42009-04-21 22:25:48 +0000632 const unsigned char* d,
633 unsigned DataLen) {
634 using namespace clang::io;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000635 IdentID ID = ReadUnalignedLE32(d);
Douglas Gregora92193e2009-04-28 21:18:29 +0000636 bool IsInteresting = ID & 0x01;
637
638 // Wipe out the "is interesting" bit.
639 ID = ID >> 1;
640
641 if (!IsInteresting) {
Sebastian Redl083abdf2010-07-27 23:01:28 +0000642 // For uninteresting identifiers, just build the IdentifierInfo
Douglas Gregora92193e2009-04-28 21:18:29 +0000643 // and associate it with the persistent ID.
644 IdentifierInfo *II = KnownII;
645 if (!II)
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000646 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregora92193e2009-04-28 21:18:29 +0000647 Reader.SetIdentifierInfo(ID, II);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000648 II->setIsFromAST();
Douglas Gregora92193e2009-04-28 21:18:29 +0000649 return II;
650 }
651
Douglas Gregor5998da52009-04-28 21:32:13 +0000652 unsigned Bits = ReadUnalignedLE16(d);
Douglas Gregor2deaea32009-04-22 18:49:13 +0000653 bool CPlusPlusOperatorKeyword = Bits & 0x01;
654 Bits >>= 1;
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000655 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
656 Bits >>= 1;
Douglas Gregor2deaea32009-04-22 18:49:13 +0000657 bool Poisoned = Bits & 0x01;
658 Bits >>= 1;
659 bool ExtensionToken = Bits & 0x01;
660 Bits >>= 1;
661 bool hasMacroDefinition = Bits & 0x01;
662 Bits >>= 1;
663 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
664 Bits >>= 10;
Mike Stump1eb44332009-09-09 15:08:12 +0000665
Douglas Gregor2deaea32009-04-22 18:49:13 +0000666 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregor5998da52009-04-28 21:32:13 +0000667 DataLen -= 6;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000668
669 // Build the IdentifierInfo itself and link the identifier ID with
670 // the new IdentifierInfo.
671 IdentifierInfo *II = KnownII;
672 if (!II)
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000673 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000674 Reader.SetIdentifierInfo(ID, II);
675
Douglas Gregor2deaea32009-04-22 18:49:13 +0000676 // Set or check the various bits in the IdentifierInfo structure.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000677 // Token IDs are read-only.
678 if (HasRevertedTokenIDToIdentifier)
679 II->RevertTokenIDToIdentifier();
Douglas Gregor2deaea32009-04-22 18:49:13 +0000680 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
Mike Stump1eb44332009-09-09 15:08:12 +0000681 assert(II->isExtensionToken() == ExtensionToken &&
Douglas Gregor2deaea32009-04-22 18:49:13 +0000682 "Incorrect extension token flag");
683 (void)ExtensionToken;
684 II->setIsPoisoned(Poisoned);
685 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
686 "Incorrect C++ operator keyword flag");
687 (void)CPlusPlusOperatorKeyword;
688
Douglas Gregor37e26842009-04-21 23:56:24 +0000689 // If this identifier is a macro, deserialize the macro
690 // definition.
691 if (hasMacroDefinition) {
Douglas Gregor5998da52009-04-28 21:32:13 +0000692 uint32_t Offset = ReadUnalignedLE32(d);
Douglas Gregor295a2a62010-10-30 00:23:06 +0000693 Reader.SetIdentifierIsMacro(II, F, Offset);
Douglas Gregor5998da52009-04-28 21:32:13 +0000694 DataLen -= 4;
Douglas Gregor37e26842009-04-21 23:56:24 +0000695 }
Douglas Gregor668c1a42009-04-21 22:25:48 +0000696
697 // Read all of the declarations visible at global scope with this
698 // name.
Chris Lattner6bf690f2009-04-27 22:17:41 +0000699 if (Reader.getContext() == 0) return II;
Douglas Gregord89275b2009-07-06 18:54:52 +0000700 if (DataLen > 0) {
701 llvm::SmallVector<uint32_t, 4> DeclIDs;
702 for (; DataLen > 0; DataLen -= 4)
703 DeclIDs.push_back(ReadUnalignedLE32(d));
704 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000705 }
Mike Stump1eb44332009-09-09 15:08:12 +0000706
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000707 II->setIsFromAST();
Douglas Gregor668c1a42009-04-21 22:25:48 +0000708 return II;
709 }
710};
Mike Stump1eb44332009-09-09 15:08:12 +0000711
712} // end anonymous namespace
Douglas Gregor668c1a42009-04-21 22:25:48 +0000713
714/// \brief The on-disk hash table used to contain information about
715/// all of the identifiers in the program.
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000716typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait>
717 ASTIdentifierLookupTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000718
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000719namespace {
720class ASTDeclContextNameLookupTrait {
721 ASTReader &Reader;
722
723public:
724 /// \brief Pair of begin/end iterators for DeclIDs.
725 typedef std::pair<DeclID *, DeclID *> data_type;
726
727 /// \brief Special internal key for declaration names.
728 /// The hash table creates keys for comparison; we do not create
729 /// a DeclarationName for the internal key to avoid deserializing types.
730 struct DeclNameKey {
731 DeclarationName::NameKind Kind;
732 uint64_t Data;
733 DeclNameKey() : Kind((DeclarationName::NameKind)0), Data(0) { }
734 };
735
736 typedef DeclarationName external_key_type;
737 typedef DeclNameKey internal_key_type;
738
739 explicit ASTDeclContextNameLookupTrait(ASTReader &Reader) : Reader(Reader) { }
740
741 static bool EqualKey(const internal_key_type& a,
742 const internal_key_type& b) {
743 return a.Kind == b.Kind && a.Data == b.Data;
744 }
745
746 unsigned ComputeHash(const DeclNameKey &Key) const {
747 llvm::FoldingSetNodeID ID;
748 ID.AddInteger(Key.Kind);
749
750 switch (Key.Kind) {
751 case DeclarationName::Identifier:
752 case DeclarationName::CXXLiteralOperatorName:
753 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
754 break;
755 case DeclarationName::ObjCZeroArgSelector:
756 case DeclarationName::ObjCOneArgSelector:
757 case DeclarationName::ObjCMultiArgSelector:
758 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
759 break;
760 case DeclarationName::CXXConstructorName:
761 case DeclarationName::CXXDestructorName:
762 case DeclarationName::CXXConversionFunctionName:
763 ID.AddInteger((TypeID)Key.Data);
764 break;
765 case DeclarationName::CXXOperatorName:
766 ID.AddInteger((OverloadedOperatorKind)Key.Data);
767 break;
768 case DeclarationName::CXXUsingDirective:
769 break;
770 }
771
772 return ID.ComputeHash();
773 }
774
775 internal_key_type GetInternalKey(const external_key_type& Name) const {
776 DeclNameKey Key;
777 Key.Kind = Name.getNameKind();
778 switch (Name.getNameKind()) {
779 case DeclarationName::Identifier:
780 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
781 break;
782 case DeclarationName::ObjCZeroArgSelector:
783 case DeclarationName::ObjCOneArgSelector:
784 case DeclarationName::ObjCMultiArgSelector:
785 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
786 break;
787 case DeclarationName::CXXConstructorName:
788 case DeclarationName::CXXDestructorName:
789 case DeclarationName::CXXConversionFunctionName:
790 Key.Data = Reader.GetTypeID(Name.getCXXNameType());
791 break;
792 case DeclarationName::CXXOperatorName:
793 Key.Data = Name.getCXXOverloadedOperator();
794 break;
795 case DeclarationName::CXXLiteralOperatorName:
796 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
797 break;
798 case DeclarationName::CXXUsingDirective:
799 break;
800 }
Michael J. Spencer20249a12010-10-21 03:16:25 +0000801
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000802 return Key;
803 }
804
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000805 external_key_type GetExternalKey(const internal_key_type& Key) const {
806 ASTContext *Context = Reader.getContext();
807 switch (Key.Kind) {
808 case DeclarationName::Identifier:
809 return DeclarationName((IdentifierInfo*)Key.Data);
810
811 case DeclarationName::ObjCZeroArgSelector:
812 case DeclarationName::ObjCOneArgSelector:
813 case DeclarationName::ObjCMultiArgSelector:
814 return DeclarationName(Selector(Key.Data));
815
816 case DeclarationName::CXXConstructorName:
817 return Context->DeclarationNames.getCXXConstructorName(
818 Context->getCanonicalType(Reader.GetType(Key.Data)));
819
820 case DeclarationName::CXXDestructorName:
821 return Context->DeclarationNames.getCXXDestructorName(
822 Context->getCanonicalType(Reader.GetType(Key.Data)));
823
824 case DeclarationName::CXXConversionFunctionName:
825 return Context->DeclarationNames.getCXXConversionFunctionName(
826 Context->getCanonicalType(Reader.GetType(Key.Data)));
827
828 case DeclarationName::CXXOperatorName:
829 return Context->DeclarationNames.getCXXOperatorName(
830 (OverloadedOperatorKind)Key.Data);
831
832 case DeclarationName::CXXLiteralOperatorName:
833 return Context->DeclarationNames.getCXXLiteralOperatorName(
834 (IdentifierInfo*)Key.Data);
835
836 case DeclarationName::CXXUsingDirective:
837 return DeclarationName::getUsingDirectiveName();
838 }
839
840 llvm_unreachable("Invalid Name Kind ?");
841 }
842
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000843 static std::pair<unsigned, unsigned>
844 ReadKeyDataLength(const unsigned char*& d) {
845 using namespace clang::io;
846 unsigned KeyLen = ReadUnalignedLE16(d);
847 unsigned DataLen = ReadUnalignedLE16(d);
848 return std::make_pair(KeyLen, DataLen);
849 }
850
851 internal_key_type ReadKey(const unsigned char* d, unsigned) {
852 using namespace clang::io;
853
854 DeclNameKey Key;
855 Key.Kind = (DeclarationName::NameKind)*d++;
856 switch (Key.Kind) {
857 case DeclarationName::Identifier:
858 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
859 break;
860 case DeclarationName::ObjCZeroArgSelector:
861 case DeclarationName::ObjCOneArgSelector:
862 case DeclarationName::ObjCMultiArgSelector:
Michael J. Spencer20249a12010-10-21 03:16:25 +0000863 Key.Data =
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000864 (uint64_t)Reader.DecodeSelector(ReadUnalignedLE32(d)).getAsOpaquePtr();
865 break;
866 case DeclarationName::CXXConstructorName:
867 case DeclarationName::CXXDestructorName:
868 case DeclarationName::CXXConversionFunctionName:
869 Key.Data = ReadUnalignedLE32(d); // TypeID
870 break;
871 case DeclarationName::CXXOperatorName:
872 Key.Data = *d++; // OverloadedOperatorKind
873 break;
874 case DeclarationName::CXXLiteralOperatorName:
875 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
876 break;
877 case DeclarationName::CXXUsingDirective:
878 break;
879 }
Michael J. Spencer20249a12010-10-21 03:16:25 +0000880
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000881 return Key;
882 }
883
884 data_type ReadData(internal_key_type, const unsigned char* d,
885 unsigned DataLen) {
886 using namespace clang::io;
887 unsigned NumDecls = ReadUnalignedLE16(d);
888 DeclID *Start = (DeclID *)d;
889 return std::make_pair(Start, Start + NumDecls);
890 }
891};
892
893} // end anonymous namespace
894
895/// \brief The on-disk hash table used for the DeclContext's Name lookup table.
896typedef OnDiskChainedHashTable<ASTDeclContextNameLookupTrait>
897 ASTDeclContextNameLookupTable;
898
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000899bool ASTReader::ReadDeclContextStorage(llvm::BitstreamCursor &Cursor,
900 const std::pair<uint64_t, uint64_t> &Offsets,
901 DeclContextInfo &Info) {
902 SavedStreamPosition SavedPosition(Cursor);
903 // First the lexical decls.
904 if (Offsets.first != 0) {
905 Cursor.JumpToBit(Offsets.first);
906
907 RecordData Record;
908 const char *Blob;
909 unsigned BlobLen;
910 unsigned Code = Cursor.ReadCode();
911 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
912 if (RecCode != DECL_CONTEXT_LEXICAL) {
913 Error("Expected lexical block");
914 return true;
915 }
916
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000917 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
918 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000919 } else {
920 Info.LexicalDecls = 0;
921 Info.NumLexicalDecls = 0;
922 }
923
924 // Now the lookup table.
925 if (Offsets.second != 0) {
926 Cursor.JumpToBit(Offsets.second);
927
928 RecordData Record;
929 const char *Blob;
930 unsigned BlobLen;
931 unsigned Code = Cursor.ReadCode();
932 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
933 if (RecCode != DECL_CONTEXT_VISIBLE) {
934 Error("Expected visible lookup table block");
935 return true;
936 }
937 Info.NameLookupTableData
938 = ASTDeclContextNameLookupTable::Create(
939 (const unsigned char *)Blob + Record[0],
940 (const unsigned char *)Blob,
941 ASTDeclContextNameLookupTrait(*this));
Sebastian Redl0ea8f7f2010-08-24 00:50:00 +0000942 } else {
943 Info.NameLookupTableData = 0;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000944 }
945
946 return false;
947}
948
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000949void ASTReader::Error(const char *Msg) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000950 Diag(diag::err_fe_pch_malformed) << Msg;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000951}
952
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000953/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000954bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000955 if (Listener)
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000956 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000957 ActualOriginalFileName,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000958 SuggestedPredefines);
Douglas Gregore721f952009-04-28 18:58:38 +0000959 return false;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000960}
961
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000962//===----------------------------------------------------------------------===//
963// Source Manager Deserialization
964//===----------------------------------------------------------------------===//
965
Douglas Gregorbd945002009-04-13 16:31:14 +0000966/// \brief Read the line table in the source manager block.
Sebastian Redlc3632732010-10-05 15:59:54 +0000967/// \returns true if there was an error.
968bool ASTReader::ParseLineTable(PerFileData &F,
969 llvm::SmallVectorImpl<uint64_t> &Record) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000970 unsigned Idx = 0;
971 LineTableInfo &LineTable = SourceMgr.getLineTable();
972
973 // Parse the file names
Douglas Gregorff0a9872009-04-13 17:12:42 +0000974 std::map<int, int> FileIDs;
975 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000976 // Extract the file name
977 unsigned FilenameLen = Record[Idx++];
978 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
979 Idx += FilenameLen;
Douglas Gregore650c8c2009-07-07 00:12:59 +0000980 MaybeAddSystemRootToFilename(Filename);
Mike Stump1eb44332009-09-09 15:08:12 +0000981 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
Douglas Gregorff0a9872009-04-13 17:12:42 +0000982 Filename.size());
Douglas Gregorbd945002009-04-13 16:31:14 +0000983 }
984
985 // Parse the line entries
986 std::vector<LineEntry> Entries;
987 while (Idx < Record.size()) {
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000988 int FID = Record[Idx++];
Douglas Gregorbd945002009-04-13 16:31:14 +0000989
990 // Extract the line entries
991 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000992 assert(NumEntries && "Numentries is 00000");
Douglas Gregorbd945002009-04-13 16:31:14 +0000993 Entries.clear();
994 Entries.reserve(NumEntries);
995 for (unsigned I = 0; I != NumEntries; ++I) {
996 unsigned FileOffset = Record[Idx++];
997 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000998 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump1eb44332009-09-09 15:08:12 +0000999 SrcMgr::CharacteristicKind FileKind
Douglas Gregorbd945002009-04-13 16:31:14 +00001000 = (SrcMgr::CharacteristicKind)Record[Idx++];
1001 unsigned IncludeOffset = Record[Idx++];
1002 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1003 FileKind, IncludeOffset));
1004 }
1005 LineTable.AddEntry(FID, Entries);
1006 }
1007
1008 return false;
1009}
1010
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001011namespace {
1012
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001013class ASTStatData {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001014public:
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001015 const ino_t ino;
1016 const dev_t dev;
1017 const mode_t mode;
1018 const time_t mtime;
1019 const off_t size;
Mike Stump1eb44332009-09-09 15:08:12 +00001020
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001021 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Chris Lattner74e976b2010-11-23 19:28:12 +00001022 : ino(i), dev(d), mode(mo), mtime(m), size(s) {}
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001023};
1024
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001025class ASTStatLookupTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001026 public:
1027 typedef const char *external_key_type;
1028 typedef const char *internal_key_type;
1029
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001030 typedef ASTStatData data_type;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001031
1032 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001033 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001034 }
1035
1036 static internal_key_type GetInternalKey(const char *path) { return path; }
1037
1038 static bool EqualKey(internal_key_type a, internal_key_type b) {
1039 return strcmp(a, b) == 0;
1040 }
1041
1042 static std::pair<unsigned, unsigned>
1043 ReadKeyDataLength(const unsigned char*& d) {
1044 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1045 unsigned DataLen = (unsigned) *d++;
1046 return std::make_pair(KeyLen + 1, DataLen);
1047 }
1048
1049 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1050 return (const char *)d;
1051 }
1052
1053 static data_type ReadData(const internal_key_type, const unsigned char *d,
1054 unsigned /*DataLen*/) {
1055 using namespace clang::io;
1056
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001057 ino_t ino = (ino_t) ReadUnalignedLE32(d);
1058 dev_t dev = (dev_t) ReadUnalignedLE32(d);
1059 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +00001060 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001061 off_t size = (off_t) ReadUnalignedLE64(d);
1062 return data_type(ino, dev, mode, mtime, size);
1063 }
1064};
1065
1066/// \brief stat() cache for precompiled headers.
1067///
1068/// This cache is very similar to the stat cache used by pretokenized
1069/// headers.
Chris Lattner10e286a2010-11-23 19:19:34 +00001070class ASTStatCache : public FileSystemStatCache {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001071 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001072 CacheTy *Cache;
1073
1074 unsigned &NumStatHits, &NumStatMisses;
Mike Stump1eb44332009-09-09 15:08:12 +00001075public:
Chris Lattner74e976b2010-11-23 19:28:12 +00001076 ASTStatCache(const unsigned char *Buckets, const unsigned char *Base,
1077 unsigned &NumStatHits, unsigned &NumStatMisses)
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001078 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
1079 Cache = CacheTy::Create(Buckets, Base);
1080 }
1081
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001082 ~ASTStatCache() { delete Cache; }
Mike Stump1eb44332009-09-09 15:08:12 +00001083
Chris Lattner898a0612010-11-23 21:17:56 +00001084 LookupResult getStat(const char *Path, struct stat &StatBuf,
1085 int *FileDescriptor) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001086 // Do the lookup for the file's data in the AST file.
Chris Lattner10e286a2010-11-23 19:19:34 +00001087 CacheTy::iterator I = Cache->find(Path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001088
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001089 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001090 if (I == Cache->end()) {
1091 ++NumStatMisses;
Chris Lattner898a0612010-11-23 21:17:56 +00001092 return statChained(Path, StatBuf, FileDescriptor);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001093 }
Mike Stump1eb44332009-09-09 15:08:12 +00001094
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001095 ++NumStatHits;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001096 ASTStatData Data = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001097
Chris Lattner10e286a2010-11-23 19:19:34 +00001098 StatBuf.st_ino = Data.ino;
1099 StatBuf.st_dev = Data.dev;
1100 StatBuf.st_mtime = Data.mtime;
1101 StatBuf.st_mode = Data.mode;
1102 StatBuf.st_size = Data.size;
Chris Lattnerd6f61112010-11-23 20:05:15 +00001103 return CacheExists;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001104 }
1105};
1106} // end anonymous namespace
1107
1108
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001109/// \brief Read a source manager block
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001110ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(PerFileData &F) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001111 using namespace SrcMgr;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001112
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001113 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001114
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001115 // Set the source-location entry cursor to the current position in
1116 // the stream. This cursor will be used to read the contents of the
1117 // source manager block initially, and then lazily read
1118 // source-location entries as needed.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001119 SLocEntryCursor = F.Stream;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001120
1121 // The stream itself is going to skip over the source manager block.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001122 if (F.Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001123 Error("malformed block record in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001124 return Failure;
1125 }
1126
1127 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001128 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001129 Error("malformed source manager block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001130 return Failure;
1131 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001132
Douglas Gregor14f79002009-04-10 03:52:48 +00001133 RecordData Record;
1134 while (true) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001135 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregor14f79002009-04-10 03:52:48 +00001136 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001137 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001138 Error("error at end of Source Manager block in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001139 return Failure;
1140 }
Douglas Gregore1d918e2009-04-10 23:10:45 +00001141 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +00001142 }
Mike Stump1eb44332009-09-09 15:08:12 +00001143
Douglas Gregor14f79002009-04-10 03:52:48 +00001144 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1145 // No known subblocks, always skip them.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001146 SLocEntryCursor.ReadSubBlockID();
1147 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001148 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001149 return Failure;
1150 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001151 continue;
1152 }
Mike Stump1eb44332009-09-09 15:08:12 +00001153
Douglas Gregor14f79002009-04-10 03:52:48 +00001154 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001155 SLocEntryCursor.ReadAbbrevRecord();
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 // Read a record.
1160 const char *BlobStart;
1161 unsigned BlobLen;
1162 Record.clear();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001163 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001164 default: // Default behavior: ignore.
1165 break;
1166
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001167 case SM_LINE_TABLE:
Sebastian Redlc3632732010-10-05 15:59:54 +00001168 if (ParseLineTable(F, Record))
Douglas Gregorbd945002009-04-13 16:31:14 +00001169 return Failure;
Chris Lattner2c78b872009-04-14 23:22:57 +00001170 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001171
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001172 case SM_SLOC_FILE_ENTRY:
1173 case SM_SLOC_BUFFER_ENTRY:
1174 case SM_SLOC_INSTANTIATION_ENTRY:
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001175 // Once we hit one of the source location entries, we're done.
1176 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +00001177 }
1178 }
1179}
1180
Sebastian Redl190faf72010-07-20 21:50:20 +00001181/// \brief Get a cursor that's correctly positioned for reading the source
1182/// location entry with the given ID.
Sebastian Redlc3632732010-10-05 15:59:54 +00001183ASTReader::PerFileData *ASTReader::SLocCursorForID(unsigned ID) {
Sebastian Redl190faf72010-07-20 21:50:20 +00001184 assert(ID != 0 && ID <= TotalNumSLocEntries &&
1185 "SLocCursorForID should only be called for real IDs.");
1186
1187 ID -= 1;
1188 PerFileData *F = 0;
1189 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1190 F = Chain[N - I - 1];
1191 if (ID < F->LocalNumSLocEntries)
1192 break;
1193 ID -= F->LocalNumSLocEntries;
1194 }
1195 assert(F && F->LocalNumSLocEntries > ID && "Chain corrupted");
1196
1197 F->SLocEntryCursor.JumpToBit(F->SLocOffsets[ID]);
Sebastian Redlc3632732010-10-05 15:59:54 +00001198 return F;
Sebastian Redl190faf72010-07-20 21:50:20 +00001199}
1200
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001201/// \brief Read in the source location entry with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001202ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(unsigned ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001203 if (ID == 0)
1204 return Success;
1205
1206 if (ID > TotalNumSLocEntries) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001207 Error("source location entry ID out-of-range for AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001208 return Failure;
1209 }
1210
Sebastian Redlc3632732010-10-05 15:59:54 +00001211 PerFileData *F = SLocCursorForID(ID);
1212 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001213
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001214 ++NumSLocEntriesRead;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001215 unsigned Code = SLocEntryCursor.ReadCode();
1216 if (Code == llvm::bitc::END_BLOCK ||
1217 Code == llvm::bitc::ENTER_SUBBLOCK ||
1218 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001219 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001220 return Failure;
1221 }
1222
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001223 RecordData Record;
1224 const char *BlobStart;
1225 unsigned BlobLen;
1226 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1227 default:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001228 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001229 return Failure;
1230
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001231 case SM_SLOC_FILE_ENTRY: {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001232 std::string Filename(BlobStart, BlobStart + BlobLen);
1233 MaybeAddSystemRootToFilename(Filename);
Chris Lattner39b49bc2010-11-23 08:35:12 +00001234 const FileEntry *File = FileMgr.getFile(Filename);
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001235 if (File == 0) {
1236 std::string ErrorStr = "could not find file '";
Douglas Gregore650c8c2009-07-07 00:12:59 +00001237 ErrorStr += Filename;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001238 ErrorStr += "' referenced by AST file";
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001239 Error(ErrorStr.c_str());
1240 return Failure;
1241 }
Mike Stump1eb44332009-09-09 15:08:12 +00001242
Douglas Gregor2d52be52010-03-21 22:49:54 +00001243 if (Record.size() < 10) {
Ted Kremenek1857f622010-03-18 21:23:05 +00001244 Error("source location entry is incorrect");
1245 return Failure;
1246 }
1247
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001248 if (!DisableValidation &&
1249 ((off_t)Record[4] != File->getSize()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001250#if !defined(LLVM_ON_WIN32)
1251 // In our regression testing, the Windows file system seems to
1252 // have inconsistent modification times that sometimes
1253 // erroneously trigger this error-handling path.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001254 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001255#endif
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001256 )) {
Douglas Gregor2d52be52010-03-21 22:49:54 +00001257 Diag(diag::err_fe_pch_file_modified)
1258 << Filename;
1259 return Failure;
1260 }
1261
Chris Lattner75dfb652010-11-23 09:19:42 +00001262 FileID FID = SourceMgr.createFileID(File, ReadSourceLocation(*F, Record[1]),
1263 (SrcMgr::CharacteristicKind)Record[2],
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001264 ID, Record[0]);
1265 if (Record[3])
1266 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile())
1267 .setHasLineDirectives();
1268
Douglas Gregor12fab312010-03-16 16:35:32 +00001269 // Reconstruct header-search information for this file.
1270 HeaderFileInfo HFI;
Douglas Gregor2d52be52010-03-21 22:49:54 +00001271 HFI.isImport = Record[6];
1272 HFI.DirInfo = Record[7];
1273 HFI.NumIncludes = Record[8];
1274 HFI.ControllingMacroID = Record[9];
Douglas Gregor12fab312010-03-16 16:35:32 +00001275 if (Listener)
1276 Listener->ReadHeaderFileInfo(HFI, File->getUID());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001277 break;
1278 }
1279
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001280 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001281 const char *Name = BlobStart;
1282 unsigned Offset = Record[0];
1283 unsigned Code = SLocEntryCursor.ReadCode();
1284 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001285 unsigned RecCode
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001286 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001287
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001288 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001289 Error("AST record has invalid code");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001290 return Failure;
1291 }
1292
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001293 llvm::MemoryBuffer *Buffer
Chris Lattnera0a270c2010-04-05 22:42:27 +00001294 = llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(BlobStart, BlobLen - 1),
1295 Name);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001296 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset);
Mike Stump1eb44332009-09-09 15:08:12 +00001297
Douglas Gregor92b059e2009-04-28 20:33:11 +00001298 if (strcmp(Name, "<built-in>") == 0) {
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +00001299 PCHPredefinesBlock Block = {
1300 BufferID,
1301 llvm::StringRef(BlobStart, BlobLen - 1)
1302 };
1303 PCHPredefinesBuffers.push_back(Block);
Douglas Gregor92b059e2009-04-28 20:33:11 +00001304 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001305
1306 break;
1307 }
1308
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001309 case SM_SLOC_INSTANTIATION_ENTRY: {
Sebastian Redlc3632732010-10-05 15:59:54 +00001310 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001311 SourceMgr.createInstantiationLoc(SpellingLoc,
Sebastian Redlc3632732010-10-05 15:59:54 +00001312 ReadSourceLocation(*F, Record[2]),
1313 ReadSourceLocation(*F, Record[3]),
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001314 Record[4],
1315 ID,
1316 Record[0]);
1317 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001318 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001319 }
1320
1321 return Success;
1322}
1323
Chris Lattner6367f6d2009-04-27 01:05:14 +00001324/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1325/// specified cursor. Read the abbreviations that are at the top of the block
1326/// and then leave the cursor pointing into the block.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001327bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattner6367f6d2009-04-27 01:05:14 +00001328 unsigned BlockID) {
1329 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001330 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001331 return Failure;
1332 }
Mike Stump1eb44332009-09-09 15:08:12 +00001333
Chris Lattner6367f6d2009-04-27 01:05:14 +00001334 while (true) {
Douglas Gregorecdcb882010-10-20 22:00:55 +00001335 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattner6367f6d2009-04-27 01:05:14 +00001336 unsigned Code = Cursor.ReadCode();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001337
Chris Lattner6367f6d2009-04-27 01:05:14 +00001338 // We expect all abbrevs to be at the start of the block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001339 if (Code != llvm::bitc::DEFINE_ABBREV) {
1340 Cursor.JumpToBit(Offset);
Chris Lattner6367f6d2009-04-27 01:05:14 +00001341 return false;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001342 }
Chris Lattner6367f6d2009-04-27 01:05:14 +00001343 Cursor.ReadAbbrevRecord();
1344 }
1345}
1346
Douglas Gregor89d99802010-11-30 06:16:57 +00001347PreprocessedEntity *ASTReader::ReadMacroRecord(PerFileData &F, uint64_t Offset) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001348 assert(PP && "Forgot to set Preprocessor ?");
Douglas Gregorecdcb882010-10-20 22:00:55 +00001349 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump1eb44332009-09-09 15:08:12 +00001350
Douglas Gregor37e26842009-04-21 23:56:24 +00001351 // Keep track of where we are in the stream, then jump back there
1352 // after reading this macro.
1353 SavedStreamPosition SavedPosition(Stream);
1354
1355 Stream.JumpToBit(Offset);
1356 RecordData Record;
1357 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1358 MacroInfo *Macro = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001359
Douglas Gregor37e26842009-04-21 23:56:24 +00001360 while (true) {
1361 unsigned Code = Stream.ReadCode();
1362 switch (Code) {
1363 case llvm::bitc::END_BLOCK:
Douglas Gregor89d99802010-11-30 06:16:57 +00001364 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001365
1366 case llvm::bitc::ENTER_SUBBLOCK:
1367 // No known subblocks, always skip them.
1368 Stream.ReadSubBlockID();
1369 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001370 Error("malformed block record in AST file");
Douglas Gregor89d99802010-11-30 06:16:57 +00001371 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001372 }
1373 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001374
Douglas Gregor37e26842009-04-21 23:56:24 +00001375 case llvm::bitc::DEFINE_ABBREV:
1376 Stream.ReadAbbrevRecord();
1377 continue;
1378 default: break;
1379 }
1380
1381 // Read a record.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001382 const char *BlobStart = 0;
1383 unsigned BlobLen = 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001384 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001385 PreprocessorRecordTypes RecType =
Michael J. Spencer20249a12010-10-21 03:16:25 +00001386 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregorecdcb882010-10-20 22:00:55 +00001387 BlobLen);
Douglas Gregor37e26842009-04-21 23:56:24 +00001388 switch (RecType) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001389 case PP_MACRO_OBJECT_LIKE:
1390 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001391 // If we already have a macro, that means that we've hit the end
1392 // of the definition of the macro we were looking for. We're
1393 // done.
1394 if (Macro)
Douglas Gregor89d99802010-11-30 06:16:57 +00001395 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001396
1397 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1398 if (II == 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001399 Error("macro must have a name in AST file");
Douglas Gregor89d99802010-11-30 06:16:57 +00001400 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001401 }
Sebastian Redlc3632732010-10-05 15:59:54 +00001402 SourceLocation Loc = ReadSourceLocation(F, Record[1]);
Douglas Gregor37e26842009-04-21 23:56:24 +00001403 bool isUsed = Record[2];
Mike Stump1eb44332009-09-09 15:08:12 +00001404
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001405 MacroInfo *MI = PP->AllocateMacroInfo(Loc);
Douglas Gregor37e26842009-04-21 23:56:24 +00001406 MI->setIsUsed(isUsed);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001407 MI->setIsFromAST();
Mike Stump1eb44332009-09-09 15:08:12 +00001408
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001409 unsigned NextIndex = 3;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001410 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregor37e26842009-04-21 23:56:24 +00001411 // Decode function-like macro info.
1412 bool isC99VarArgs = Record[3];
1413 bool isGNUVarArgs = Record[4];
1414 MacroArgs.clear();
1415 unsigned NumArgs = Record[5];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001416 NextIndex = 6 + NumArgs;
Douglas Gregor37e26842009-04-21 23:56:24 +00001417 for (unsigned i = 0; i != NumArgs; ++i)
1418 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1419
1420 // Install function-like macro info.
1421 MI->setIsFunctionLike();
1422 if (isC99VarArgs) MI->setIsC99Varargs();
1423 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor75fdb232009-05-22 22:45:36 +00001424 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001425 PP->getPreprocessorAllocator());
Douglas Gregor37e26842009-04-21 23:56:24 +00001426 }
1427
1428 // Finally, install the macro.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001429 PP->setMacroInfo(II, MI);
Douglas Gregor37e26842009-04-21 23:56:24 +00001430
1431 // Remember that we saw this macro last so that we add the tokens that
1432 // form its body to it.
1433 Macro = MI;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001434
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001435 if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) {
1436 // We have a macro definition. Load it now.
1437 PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro,
1438 getMacroDefinition(Record[NextIndex]));
1439 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001440
Douglas Gregor37e26842009-04-21 23:56:24 +00001441 ++NumMacrosRead;
1442 break;
1443 }
Mike Stump1eb44332009-09-09 15:08:12 +00001444
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001445 case PP_TOKEN: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001446 // If we see a TOKEN before a PP_MACRO_*, then the file is
1447 // erroneous, just pretend we didn't see this.
1448 if (Macro == 0) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001449
Douglas Gregor37e26842009-04-21 23:56:24 +00001450 Token Tok;
1451 Tok.startToken();
Sebastian Redlc3632732010-10-05 15:59:54 +00001452 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001453 Tok.setLength(Record[1]);
1454 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1455 Tok.setIdentifierInfo(II);
1456 Tok.setKind((tok::TokenKind)Record[3]);
1457 Tok.setFlag((Token::TokenFlags)Record[4]);
1458 Macro->AddTokenToBody(Tok);
1459 break;
1460 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001461
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001462 case PP_MACRO_INSTANTIATION: {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001463 // If we already have a macro, that means that we've hit the end
1464 // of the definition of the macro we were looking for. We're
1465 // done.
1466 if (Macro)
Douglas Gregor89d99802010-11-30 06:16:57 +00001467 return 0;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001468
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001469 if (!PP->getPreprocessingRecord()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001470 Error("missing preprocessing record in AST file");
Douglas Gregor89d99802010-11-30 06:16:57 +00001471 return 0;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001472 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001473
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001474 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
Douglas Gregor89d99802010-11-30 06:16:57 +00001475 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1476 return PE;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001477
1478 MacroInstantiation *MI
1479 = new (PPRec) MacroInstantiation(DecodeIdentifierInfo(Record[3]),
Sebastian Redlc3632732010-10-05 15:59:54 +00001480 SourceRange(ReadSourceLocation(F, Record[1]),
1481 ReadSourceLocation(F, Record[2])),
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001482 getMacroDefinition(Record[4]));
1483 PPRec.SetPreallocatedEntity(Record[0], MI);
Douglas Gregor89d99802010-11-30 06:16:57 +00001484 return MI;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001485 }
1486
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001487 case PP_MACRO_DEFINITION: {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001488 // If we already have a macro, that means that we've hit the end
1489 // of the definition of the macro we were looking for. We're
1490 // done.
1491 if (Macro)
Douglas Gregor89d99802010-11-30 06:16:57 +00001492 return 0;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001493
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001494 if (!PP->getPreprocessingRecord()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001495 Error("missing preprocessing record in AST file");
Douglas Gregor89d99802010-11-30 06:16:57 +00001496 return 0;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001497 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001498
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001499 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
Douglas Gregor89d99802010-11-30 06:16:57 +00001500 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1501 return PE;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001502
Douglas Gregor77424bc2010-10-02 19:29:26 +00001503 if (Record[1] > MacroDefinitionsLoaded.size()) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001504 Error("out-of-bounds macro definition record");
Douglas Gregor89d99802010-11-30 06:16:57 +00001505 return 0;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001506 }
1507
Douglas Gregor77424bc2010-10-02 19:29:26 +00001508 // Decode the identifier info and then check again; if the macro is
Michael J. Spencer20249a12010-10-21 03:16:25 +00001509 // still defined and associated with the identifier,
Douglas Gregor77424bc2010-10-02 19:29:26 +00001510 IdentifierInfo *II = DecodeIdentifierInfo(Record[4]);
1511 if (!MacroDefinitionsLoaded[Record[1] - 1]) {
1512 MacroDefinition *MD
1513 = new (PPRec) MacroDefinition(II,
Sebastian Redlc3632732010-10-05 15:59:54 +00001514 ReadSourceLocation(F, Record[5]),
Douglas Gregorb1a7d9a2010-10-01 20:33:34 +00001515 SourceRange(
Sebastian Redlc3632732010-10-05 15:59:54 +00001516 ReadSourceLocation(F, Record[2]),
1517 ReadSourceLocation(F, Record[3])));
Michael J. Spencer20249a12010-10-21 03:16:25 +00001518
Douglas Gregor77424bc2010-10-02 19:29:26 +00001519 PPRec.SetPreallocatedEntity(Record[0], MD);
1520 MacroDefinitionsLoaded[Record[1] - 1] = MD;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001521
Douglas Gregor77424bc2010-10-02 19:29:26 +00001522 if (DeserializationListener)
1523 DeserializationListener->MacroDefinitionRead(Record[1], MD);
1524 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001525
Douglas Gregor89d99802010-11-30 06:16:57 +00001526 return MacroDefinitionsLoaded[Record[1] - 1];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001527 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001528
Douglas Gregorecdcb882010-10-20 22:00:55 +00001529 case PP_INCLUSION_DIRECTIVE: {
1530 // If we already have a macro, that means that we've hit the end
1531 // of the definition of the macro we were looking for. We're
1532 // done.
1533 if (Macro)
Douglas Gregor89d99802010-11-30 06:16:57 +00001534 return 0;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001535
Douglas Gregorecdcb882010-10-20 22:00:55 +00001536 if (!PP->getPreprocessingRecord()) {
1537 Error("missing preprocessing record in AST file");
Douglas Gregor89d99802010-11-30 06:16:57 +00001538 return 0;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001539 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001540
Douglas Gregorecdcb882010-10-20 22:00:55 +00001541 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
Douglas Gregor89d99802010-11-30 06:16:57 +00001542 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1543 return PE;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001544
1545 const char *FullFileNameStart = BlobStart + Record[3];
Michael J. Spencer20249a12010-10-21 03:16:25 +00001546 const FileEntry *File
Douglas Gregor89d99802010-11-30 06:16:57 +00001547 = PP->getFileManager().getFile(llvm::StringRef(FullFileNameStart,
1548 BlobLen - Record[3]));
Michael J. Spencer20249a12010-10-21 03:16:25 +00001549
Douglas Gregorecdcb882010-10-20 22:00:55 +00001550 // FIXME: Stable encoding
1551 InclusionDirective::InclusionKind Kind
1552 = static_cast<InclusionDirective::InclusionKind>(Record[5]);
1553 InclusionDirective *ID
Douglas Gregor4ab829c2010-11-01 15:03:47 +00001554 = new (PPRec) InclusionDirective(PPRec, Kind,
Douglas Gregorecdcb882010-10-20 22:00:55 +00001555 llvm::StringRef(BlobStart, Record[3]),
1556 Record[4],
1557 File,
1558 SourceRange(ReadSourceLocation(F, Record[1]),
1559 ReadSourceLocation(F, Record[2])));
1560 PPRec.SetPreallocatedEntity(Record[0], ID);
Douglas Gregor89d99802010-11-30 06:16:57 +00001561 return ID;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001562 }
Sebastian Redlb57a6242010-09-27 22:18:47 +00001563 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001564 }
Douglas Gregor89d99802010-11-30 06:16:57 +00001565
1566 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001567}
1568
Douglas Gregor295a2a62010-10-30 00:23:06 +00001569void ASTReader::SetIdentifierIsMacro(IdentifierInfo *II, PerFileData &F,
1570 uint64_t Offset) {
1571 // Note that this identifier has a macro definition.
1572 II->setHasMacroDefinition(true);
1573
1574 // Adjust the offset based on our position in the chain.
1575 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1576 if (Chain[I] == &F)
1577 break;
1578
1579 Offset += Chain[I]->SizeInBits;
1580 }
1581
1582 UnreadMacroRecordOffsets[II] = Offset;
1583}
1584
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001585void ASTReader::ReadDefinedMacros() {
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001586 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redlc3632732010-10-05 15:59:54 +00001587 PerFileData &F = *Chain[N - I - 1];
1588 llvm::BitstreamCursor &MacroCursor = F.MacroCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001589
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001590 // If there was no preprocessor block, skip this file.
1591 if (!MacroCursor.getBitStreamReader())
1592 continue;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001593
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001594 llvm::BitstreamCursor Cursor = MacroCursor;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001595 Cursor.JumpToBit(F.MacroStartOffset);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001596
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001597 RecordData Record;
1598 while (true) {
Sebastian Redledadecc2010-09-28 02:55:49 +00001599 uint64_t Offset = Cursor.GetCurrentBitNo();
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001600 unsigned Code = Cursor.ReadCode();
Douglas Gregorecdcb882010-10-20 22:00:55 +00001601 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001602 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001603
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001604 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1605 // No known subblocks, always skip them.
1606 Cursor.ReadSubBlockID();
1607 if (Cursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001608 Error("malformed block record in AST file");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001609 return;
1610 }
1611 continue;
1612 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001613
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001614 if (Code == llvm::bitc::DEFINE_ABBREV) {
1615 Cursor.ReadAbbrevRecord();
1616 continue;
1617 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001618
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001619 // Read a record.
1620 const char *BlobStart;
1621 unsigned BlobLen;
1622 Record.clear();
1623 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1624 default: // Default behavior: ignore.
1625 break;
Douglas Gregor88a35862010-01-04 19:18:44 +00001626
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001627 case PP_MACRO_OBJECT_LIKE:
1628 case PP_MACRO_FUNCTION_LIKE:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001629 DecodeIdentifierInfo(Record[0]);
1630 break;
1631
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001632 case PP_TOKEN:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001633 // Ignore tokens.
1634 break;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001635
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001636 case PP_MACRO_INSTANTIATION:
1637 case PP_MACRO_DEFINITION:
Douglas Gregorecdcb882010-10-20 22:00:55 +00001638 case PP_INCLUSION_DIRECTIVE:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001639 // Read the macro record.
Sebastian Redledadecc2010-09-28 02:55:49 +00001640 // FIXME: That's a stupid way to do this. We should reuse this cursor.
Sebastian Redlc3632732010-10-05 15:59:54 +00001641 ReadMacroRecord(F, Offset);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001642 break;
1643 }
Douglas Gregor88a35862010-01-04 19:18:44 +00001644 }
1645 }
Douglas Gregor295a2a62010-10-30 00:23:06 +00001646
1647 // Drain the unread macro-record offsets map.
1648 while (!UnreadMacroRecordOffsets.empty())
1649 LoadMacroDefinition(UnreadMacroRecordOffsets.begin());
1650}
1651
1652void ASTReader::LoadMacroDefinition(
1653 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos) {
1654 assert(Pos != UnreadMacroRecordOffsets.end() && "Unknown macro definition");
1655 PerFileData *F = 0;
1656 uint64_t Offset = Pos->second;
1657 UnreadMacroRecordOffsets.erase(Pos);
1658
1659 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1660 if (Offset < Chain[I]->SizeInBits) {
1661 F = Chain[I];
1662 break;
1663 }
1664
1665 Offset -= Chain[I]->SizeInBits;
1666 }
1667 if (!F) {
1668 Error("Malformed macro record offset");
1669 return;
1670 }
1671
1672 ReadMacroRecord(*F, Offset);
1673}
1674
1675void ASTReader::LoadMacroDefinition(IdentifierInfo *II) {
1676 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos
1677 = UnreadMacroRecordOffsets.find(II);
1678 LoadMacroDefinition(Pos);
Douglas Gregor88a35862010-01-04 19:18:44 +00001679}
1680
Sebastian Redlf73c93f2010-09-15 19:54:06 +00001681MacroDefinition *ASTReader::getMacroDefinition(MacroID ID) {
Douglas Gregor77424bc2010-10-02 19:29:26 +00001682 if (ID == 0 || ID > MacroDefinitionsLoaded.size())
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001683 return 0;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001684
Douglas Gregor77424bc2010-10-02 19:29:26 +00001685 if (!MacroDefinitionsLoaded[ID - 1]) {
1686 unsigned Index = ID - 1;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001687 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1688 PerFileData &F = *Chain[N - I - 1];
1689 if (Index < F.LocalNumMacroDefinitions) {
Sebastian Redlc3632732010-10-05 15:59:54 +00001690 ReadMacroRecord(F, F.MacroDefinitionOffsets[Index]);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001691 break;
1692 }
1693 Index -= F.LocalNumMacroDefinitions;
1694 }
Douglas Gregor77424bc2010-10-02 19:29:26 +00001695 assert(MacroDefinitionsLoaded[ID - 1] && "Broken chain");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001696 }
1697
Douglas Gregor77424bc2010-10-02 19:29:26 +00001698 return MacroDefinitionsLoaded[ID - 1];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001699}
1700
Douglas Gregore650c8c2009-07-07 00:12:59 +00001701/// \brief If we are loading a relocatable PCH file, and the filename is
1702/// not an absolute path, add the system root to the beginning of the file
1703/// name.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001704void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001705 // If this is not a relocatable PCH file, there's nothing to do.
1706 if (!RelocatablePCH)
1707 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001708
Michael J. Spencer256053b2010-12-17 21:22:22 +00001709 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
Douglas Gregore650c8c2009-07-07 00:12:59 +00001710 return;
1711
Douglas Gregore650c8c2009-07-07 00:12:59 +00001712 if (isysroot == 0) {
1713 // If no system root was given, default to '/'
1714 Filename.insert(Filename.begin(), '/');
1715 return;
1716 }
Mike Stump1eb44332009-09-09 15:08:12 +00001717
Douglas Gregore650c8c2009-07-07 00:12:59 +00001718 unsigned Length = strlen(isysroot);
1719 if (isysroot[Length - 1] != '/')
1720 Filename.insert(Filename.begin(), '/');
Mike Stump1eb44332009-09-09 15:08:12 +00001721
Douglas Gregore650c8c2009-07-07 00:12:59 +00001722 Filename.insert(Filename.begin(), isysroot, isysroot + Length);
1723}
1724
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001725ASTReader::ASTReadResult
Sebastian Redl571db7f2010-08-18 23:56:56 +00001726ASTReader::ReadASTBlock(PerFileData &F) {
Sebastian Redl9137a522010-07-16 17:50:48 +00001727 llvm::BitstreamCursor &Stream = F.Stream;
1728
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001729 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001730 Error("malformed block record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001731 return Failure;
1732 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001733
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001734 // Read all of the records and blocks for the ASt file.
Douglas Gregor8038d512009-04-10 17:25:41 +00001735 RecordData Record;
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001736 bool First = true;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001737 while (!Stream.AtEndOfStream()) {
1738 unsigned Code = Stream.ReadCode();
1739 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001740 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001741 Error("error at end of module block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001742 return Failure;
1743 }
Chris Lattner7356a312009-04-11 21:15:38 +00001744
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001745 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001746 }
1747
1748 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1749 switch (Stream.ReadSubBlockID()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001750 case DECLTYPES_BLOCK_ID:
Chris Lattner6367f6d2009-04-27 01:05:14 +00001751 // We lazily load the decls block, but we want to set up the
1752 // DeclsCursor cursor to point into it. Clone our current bitcode
1753 // cursor to it, enter the block and read the abbrevs in that block.
1754 // With the main cursor, we just skip over it.
Sebastian Redl9137a522010-07-16 17:50:48 +00001755 F.DeclsCursor = Stream;
Chris Lattner6367f6d2009-04-27 01:05:14 +00001756 if (Stream.SkipBlock() || // Skip with the main cursor.
1757 // Read the abbrevs.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001758 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001759 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001760 return Failure;
1761 }
1762 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001763
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00001764 case DECL_UPDATES_BLOCK_ID:
1765 if (Stream.SkipBlock()) {
1766 Error("malformed block record in AST file");
1767 return Failure;
1768 }
1769 break;
1770
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001771 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl9137a522010-07-16 17:50:48 +00001772 F.MacroCursor = Stream;
Douglas Gregor88a35862010-01-04 19:18:44 +00001773 if (PP)
1774 PP->setExternalSource(this);
1775
Douglas Gregorecdcb882010-10-20 22:00:55 +00001776 if (Stream.SkipBlock() ||
1777 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001778 Error("malformed block record in AST file");
Chris Lattner7356a312009-04-11 21:15:38 +00001779 return Failure;
1780 }
Douglas Gregorecdcb882010-10-20 22:00:55 +00001781 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattner7356a312009-04-11 21:15:38 +00001782 break;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001783
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001784 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001785 switch (ReadSourceManagerBlock(F)) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00001786 case Success:
1787 break;
1788
1789 case Failure:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001790 Error("malformed source manager block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001791 return Failure;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001792
1793 case IgnorePCH:
1794 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001795 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001796 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001797 }
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001798 First = false;
Douglas Gregor8038d512009-04-10 17:25:41 +00001799 continue;
1800 }
1801
1802 if (Code == llvm::bitc::DEFINE_ABBREV) {
1803 Stream.ReadAbbrevRecord();
1804 continue;
1805 }
1806
1807 // Read and process a record.
1808 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00001809 const char *BlobStart = 0;
1810 unsigned BlobLen = 0;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001811 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00001812 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001813 default: // Default behavior: ignore.
1814 break;
1815
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001816 case METADATA: {
1817 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1818 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001819 : diag::warn_pch_version_too_new);
1820 return IgnorePCH;
1821 }
1822
1823 RelocatablePCH = Record[4];
1824 if (Listener) {
1825 std::string TargetTriple(BlobStart, BlobLen);
1826 if (Listener->ReadTargetTriple(TargetTriple))
1827 return IgnorePCH;
1828 }
1829 break;
1830 }
1831
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001832 case CHAINED_METADATA: {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001833 if (!First) {
1834 Error("CHAINED_METADATA is not first record in block");
1835 return Failure;
1836 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001837 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1838 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001839 : diag::warn_pch_version_too_new);
1840 return IgnorePCH;
1841 }
1842
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00001843 // Load the chained file, which is always a PCH file.
1844 switch(ReadASTCore(llvm::StringRef(BlobStart, BlobLen), PCH)) {
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001845 case Failure: return Failure;
1846 // If we have to ignore the dependency, we'll have to ignore this too.
1847 case IgnorePCH: return IgnorePCH;
1848 case Success: break;
1849 }
1850 break;
1851 }
1852
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001853 case TYPE_OFFSET:
Sebastian Redl12d6da02010-07-19 22:06:55 +00001854 if (F.LocalNumTypes != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001855 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001856 return Failure;
1857 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00001858 F.TypeOffsets = (const uint32_t *)BlobStart;
1859 F.LocalNumTypes = Record[0];
Douglas Gregor8038d512009-04-10 17:25:41 +00001860 break;
1861
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001862 case DECL_OFFSET:
Sebastian Redl12d6da02010-07-19 22:06:55 +00001863 if (F.LocalNumDecls != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001864 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001865 return Failure;
1866 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00001867 F.DeclOffsets = (const uint32_t *)BlobStart;
1868 F.LocalNumDecls = Record[0];
Douglas Gregor8038d512009-04-10 17:25:41 +00001869 break;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001870
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001871 case TU_UPDATE_LEXICAL: {
Sebastian Redld692af72010-07-27 18:24:41 +00001872 DeclContextInfo Info = {
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001873 /* No visible information */ 0,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001874 reinterpret_cast<const KindDeclIDPair *>(BlobStart),
1875 BlobLen / sizeof(KindDeclIDPair)
Sebastian Redld692af72010-07-27 18:24:41 +00001876 };
Douglas Gregor3747ee72010-10-01 01:18:02 +00001877 DeclContextOffsets[Context ? Context->getTranslationUnitDecl() : 0]
1878 .push_back(Info);
Sebastian Redld692af72010-07-27 18:24:41 +00001879 break;
1880 }
1881
Sebastian Redle1dde812010-08-24 00:50:04 +00001882 case UPDATE_VISIBLE: {
1883 serialization::DeclID ID = Record[0];
1884 void *Table = ASTDeclContextNameLookupTable::Create(
1885 (const unsigned char *)BlobStart + Record[1],
1886 (const unsigned char *)BlobStart,
1887 ASTDeclContextNameLookupTrait(*this));
Douglas Gregor3747ee72010-10-01 01:18:02 +00001888 if (ID == 1 && Context) { // Is it the TU?
Sebastian Redle1dde812010-08-24 00:50:04 +00001889 DeclContextInfo Info = {
1890 Table, /* No lexical inforamtion */ 0, 0
1891 };
1892 DeclContextOffsets[Context->getTranslationUnitDecl()].push_back(Info);
1893 } else
1894 PendingVisibleUpdates[ID].push_back(Table);
1895 break;
1896 }
1897
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001898 case REDECLS_UPDATE_LATEST: {
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00001899 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
1900 for (unsigned i = 0, e = Record.size(); i < e; i += 2) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001901 DeclID First = Record[i], Latest = Record[i+1];
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00001902 assert((FirstLatestDeclIDs.find(First) == FirstLatestDeclIDs.end() ||
1903 Latest > FirstLatestDeclIDs[First]) &&
1904 "The new latest is supposed to come after the previous latest");
1905 FirstLatestDeclIDs[First] = Latest;
1906 }
1907 break;
1908 }
1909
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001910 case LANGUAGE_OPTIONS:
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001911 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001912 return IgnorePCH;
1913 break;
Douglas Gregor2bec0412009-04-10 21:16:55 +00001914
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001915 case IDENTIFIER_TABLE:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001916 F.IdentifierTableData = BlobStart;
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001917 if (Record[0]) {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001918 F.IdentifierLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001919 = ASTIdentifierLookupTable::Create(
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001920 (const unsigned char *)F.IdentifierTableData + Record[0],
1921 (const unsigned char *)F.IdentifierTableData,
Sebastian Redlc3632732010-10-05 15:59:54 +00001922 ASTIdentifierLookupTrait(*this, F));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001923 if (PP)
1924 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001925 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001926 break;
1927
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001928 case IDENTIFIER_OFFSET:
Sebastian Redl2da08f92010-07-19 22:28:42 +00001929 if (F.LocalNumIdentifiers != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001930 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00001931 return Failure;
1932 }
Sebastian Redl2da08f92010-07-19 22:28:42 +00001933 F.IdentifierOffsets = (const uint32_t *)BlobStart;
1934 F.LocalNumIdentifiers = Record[0];
Douglas Gregorafaf3082009-04-11 00:14:32 +00001935 break;
Douglas Gregorfdd01722009-04-14 00:24:19 +00001936
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001937 case EXTERNAL_DEFINITIONS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001938 // Optimization for the first block.
1939 if (ExternalDefinitions.empty())
1940 ExternalDefinitions.swap(Record);
1941 else
1942 ExternalDefinitions.insert(ExternalDefinitions.end(),
1943 Record.begin(), Record.end());
Douglas Gregorfdd01722009-04-14 00:24:19 +00001944 break;
Douglas Gregor3e1af842009-04-17 22:13:46 +00001945
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001946 case SPECIAL_TYPES:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001947 // Optimization for the first block
1948 if (SpecialTypes.empty())
1949 SpecialTypes.swap(Record);
1950 else
1951 SpecialTypes.insert(SpecialTypes.end(), Record.begin(), Record.end());
Douglas Gregorad1de002009-04-18 05:55:16 +00001952 break;
1953
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001954 case STATISTICS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001955 TotalNumStatements += Record[0];
1956 TotalNumMacros += Record[1];
1957 TotalLexicalDeclContexts += Record[2];
1958 TotalVisibleDeclContexts += Record[3];
Douglas Gregor3e1af842009-04-17 22:13:46 +00001959 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001960
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001961 case TENTATIVE_DEFINITIONS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001962 // Optimization for the first block.
1963 if (TentativeDefinitions.empty())
1964 TentativeDefinitions.swap(Record);
1965 else
1966 TentativeDefinitions.insert(TentativeDefinitions.end(),
1967 Record.begin(), Record.end());
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00001968 break;
Douglas Gregor14c22f22009-04-22 22:18:58 +00001969
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001970 case UNUSED_FILESCOPED_DECLS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001971 // Optimization for the first block.
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00001972 if (UnusedFileScopedDecls.empty())
1973 UnusedFileScopedDecls.swap(Record);
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001974 else
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00001975 UnusedFileScopedDecls.insert(UnusedFileScopedDecls.end(),
1976 Record.begin(), Record.end());
Tanya Lattnere6bbc012010-02-12 00:07:30 +00001977 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001978
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001979 case WEAK_UNDECLARED_IDENTIFIERS:
Sebastian Redl40566802010-08-05 18:21:25 +00001980 // Later blocks overwrite earlier ones.
1981 WeakUndeclaredIdentifiers.swap(Record);
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00001982 break;
1983
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001984 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001985 // Optimization for the first block.
1986 if (LocallyScopedExternalDecls.empty())
1987 LocallyScopedExternalDecls.swap(Record);
1988 else
1989 LocallyScopedExternalDecls.insert(LocallyScopedExternalDecls.end(),
1990 Record.begin(), Record.end());
Douglas Gregor14c22f22009-04-22 22:18:58 +00001991 break;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001992
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001993 case SELECTOR_OFFSETS:
Sebastian Redl059612d2010-08-03 21:58:15 +00001994 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redl725cd962010-08-04 20:40:17 +00001995 F.LocalNumSelectors = Record[0];
Douglas Gregor83941df2009-04-25 17:48:32 +00001996 break;
1997
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001998 case METHOD_POOL:
Sebastian Redl725cd962010-08-04 20:40:17 +00001999 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor83941df2009-04-25 17:48:32 +00002000 if (Record[0])
Sebastian Redl725cd962010-08-04 20:40:17 +00002001 F.SelectorLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002002 = ASTSelectorLookupTable::Create(
Sebastian Redl725cd962010-08-04 20:40:17 +00002003 F.SelectorLookupTableData + Record[0],
2004 F.SelectorLookupTableData,
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002005 ASTSelectorLookupTrait(*this));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002006 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002007 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002008
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002009 case REFERENCED_SELECTOR_POOL:
Sebastian Redlc3632732010-10-05 15:59:54 +00002010 F.ReferencedSelectorsData.swap(Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002011 break;
2012
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002013 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002014 if (!Record.empty() && Listener)
2015 Listener->ReadCounter(Record[0]);
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002016 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002017
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002018 case SOURCE_LOCATION_OFFSETS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002019 F.SLocOffsets = (const uint32_t *)BlobStart;
2020 F.LocalNumSLocEntries = Record[0];
Sebastian Redl8db9fae2010-09-22 20:19:08 +00002021 F.LocalSLocSize = Record[1];
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002022 break;
2023
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002024 case SOURCE_LOCATION_PRELOADS:
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002025 if (PreloadSLocEntries.empty())
2026 PreloadSLocEntries.swap(Record);
2027 else
2028 PreloadSLocEntries.insert(PreloadSLocEntries.end(),
2029 Record.begin(), Record.end());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002030 break;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002031
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002032 case STAT_CACHE: {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002033 ASTStatCache *MyStatCache =
2034 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
Douglas Gregor52e71082009-10-16 18:18:30 +00002035 (const unsigned char *)BlobStart,
2036 NumStatHits, NumStatMisses);
2037 FileMgr.addStatCache(MyStatCache);
Sebastian Redl9137a522010-07-16 17:50:48 +00002038 F.StatCache = MyStatCache;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002039 break;
Douglas Gregor52e71082009-10-16 18:18:30 +00002040 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002041
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002042 case EXT_VECTOR_DECLS:
Sebastian Redla9f23682010-07-28 21:38:49 +00002043 // Optimization for the first block.
2044 if (ExtVectorDecls.empty())
2045 ExtVectorDecls.swap(Record);
2046 else
2047 ExtVectorDecls.insert(ExtVectorDecls.end(),
2048 Record.begin(), Record.end());
Douglas Gregorb81c1702009-04-27 20:06:05 +00002049 break;
2050
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002051 case VTABLE_USES:
Sebastian Redl40566802010-08-05 18:21:25 +00002052 // Later tables overwrite earlier ones.
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002053 VTableUses.swap(Record);
2054 break;
2055
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002056 case DYNAMIC_CLASSES:
Sebastian Redl40566802010-08-05 18:21:25 +00002057 // Optimization for the first block.
2058 if (DynamicClasses.empty())
2059 DynamicClasses.swap(Record);
2060 else
2061 DynamicClasses.insert(DynamicClasses.end(),
2062 Record.begin(), Record.end());
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002063 break;
2064
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002065 case PENDING_IMPLICIT_INSTANTIATIONS:
Sebastian Redlc3632732010-10-05 15:59:54 +00002066 F.PendingInstantiations.swap(Record);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002067 break;
2068
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002069 case SEMA_DECL_REFS:
Sebastian Redl40566802010-08-05 18:21:25 +00002070 // Later tables overwrite earlier ones.
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002071 SemaDeclRefs.swap(Record);
2072 break;
2073
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002074 case ORIGINAL_FILE_NAME:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002075 // The primary AST will be the last to get here, so it will be the one
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002076 // that's used.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +00002077 ActualOriginalFileName.assign(BlobStart, BlobLen);
2078 OriginalFileName = ActualOriginalFileName;
Douglas Gregore650c8c2009-07-07 00:12:59 +00002079 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregorb64c1932009-05-12 01:31:05 +00002080 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002081
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002082 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek974be4d2010-02-12 23:31:14 +00002083 const std::string &CurBranch = getClangFullRepositoryVersion();
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002084 llvm::StringRef ASTBranch(BlobStart, BlobLen);
2085 if (llvm::StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2086 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregor445e23e2009-10-05 21:07:28 +00002087 return IgnorePCH;
2088 }
2089 break;
2090 }
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002091
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002092 case MACRO_DEFINITION_OFFSETS:
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002093 F.MacroDefinitionOffsets = (const uint32_t *)BlobStart;
2094 F.NumPreallocatedPreprocessingEntities = Record[0];
2095 F.LocalNumMacroDefinitions = Record[1];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002096 break;
Sebastian Redl0b17c612010-08-13 00:28:03 +00002097
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002098 case DECL_UPDATE_OFFSETS: {
2099 if (Record.size() % 2 != 0) {
2100 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2101 return Failure;
2102 }
2103 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
2104 DeclUpdateOffsets[static_cast<DeclID>(Record[I])]
2105 .push_back(std::make_pair(&F, Record[I+1]));
2106 break;
2107 }
2108
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002109 case DECL_REPLACEMENTS: {
Sebastian Redl0b17c612010-08-13 00:28:03 +00002110 if (Record.size() % 2 != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002111 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redl0b17c612010-08-13 00:28:03 +00002112 return Failure;
2113 }
2114 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002115 ReplacedDecls[static_cast<DeclID>(Record[I])] =
Sebastian Redl0b17c612010-08-13 00:28:03 +00002116 std::make_pair(&F, Record[I+1]);
2117 break;
2118 }
Douglas Gregor7c789c12010-10-29 22:39:52 +00002119
2120 case CXX_BASE_SPECIFIER_OFFSETS: {
2121 if (F.LocalNumCXXBaseSpecifiers != 0) {
2122 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2123 return Failure;
2124 }
2125
2126 F.LocalNumCXXBaseSpecifiers = Record[0];
2127 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
2128 break;
2129 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002130
2131 case DIAG_USER_MAPPINGS:
2132 if (Record.size() % 2 != 0) {
2133 Error("invalid DIAG_USER_MAPPINGS block in AST file");
2134 return Failure;
2135 }
2136 if (UserDiagMappings.empty())
2137 UserDiagMappings.swap(Record);
2138 else
2139 UserDiagMappings.insert(UserDiagMappings.end(),
2140 Record.begin(), Record.end());
2141 break;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002142 }
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002143 First = false;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002144 }
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002145 Error("premature end of bitstream in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002146 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002147}
2148
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002149ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
2150 ASTFileType Type) {
2151 switch(ReadASTCore(FileName, Type)) {
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002152 case Failure: return Failure;
2153 case IgnorePCH: return IgnorePCH;
2154 case Success: break;
2155 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002156
2157 // Here comes stuff that we only do once the entire chain is loaded.
2158
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002159 // Allocate space for loaded slocentries, identifiers, decls and types.
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002160 unsigned TotalNumIdentifiers = 0, TotalNumTypes = 0, TotalNumDecls = 0,
Sebastian Redl725cd962010-08-04 20:40:17 +00002161 TotalNumPreallocatedPreprocessingEntities = 0, TotalNumMacroDefs = 0,
2162 TotalNumSelectors = 0;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002163 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002164 TotalNumSLocEntries += Chain[I]->LocalNumSLocEntries;
Sebastian Redl8db9fae2010-09-22 20:19:08 +00002165 NextSLocOffset += Chain[I]->LocalSLocSize;
Sebastian Redl2da08f92010-07-19 22:28:42 +00002166 TotalNumIdentifiers += Chain[I]->LocalNumIdentifiers;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002167 TotalNumTypes += Chain[I]->LocalNumTypes;
2168 TotalNumDecls += Chain[I]->LocalNumDecls;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002169 TotalNumPreallocatedPreprocessingEntities +=
2170 Chain[I]->NumPreallocatedPreprocessingEntities;
2171 TotalNumMacroDefs += Chain[I]->LocalNumMacroDefinitions;
Sebastian Redl725cd962010-08-04 20:40:17 +00002172 TotalNumSelectors += Chain[I]->LocalNumSelectors;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002173 }
Sebastian Redl8db9fae2010-09-22 20:19:08 +00002174 SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, NextSLocOffset);
Sebastian Redl2da08f92010-07-19 22:28:42 +00002175 IdentifiersLoaded.resize(TotalNumIdentifiers);
Sebastian Redl12d6da02010-07-19 22:06:55 +00002176 TypesLoaded.resize(TotalNumTypes);
2177 DeclsLoaded.resize(TotalNumDecls);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002178 MacroDefinitionsLoaded.resize(TotalNumMacroDefs);
2179 if (PP) {
2180 if (TotalNumIdentifiers > 0)
2181 PP->getHeaderSearchInfo().SetExternalLookup(this);
2182 if (TotalNumPreallocatedPreprocessingEntities > 0) {
2183 if (!PP->getPreprocessingRecord())
2184 PP->createPreprocessingRecord();
2185 PP->getPreprocessingRecord()->SetExternalSource(*this,
2186 TotalNumPreallocatedPreprocessingEntities);
2187 }
2188 }
Sebastian Redl725cd962010-08-04 20:40:17 +00002189 SelectorsLoaded.resize(TotalNumSelectors);
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002190 // Preload SLocEntries.
2191 for (unsigned I = 0, N = PreloadSLocEntries.size(); I != N; ++I) {
2192 ASTReadResult Result = ReadSLocEntryRecord(PreloadSLocEntries[I]);
2193 if (Result != Success)
2194 return Result;
2195 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00002196
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002197 // Check the predefines buffers.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00002198 if (!DisableValidation && CheckPredefinesBuffers())
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002199 return IgnorePCH;
2200
2201 if (PP) {
2202 // Initialization of keywords and pragmas occurs before the
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002203 // AST file is read, so there may be some identifiers that were
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002204 // loaded into the IdentifierTable before we intercepted the
2205 // creation of identifiers. Iterate through the list of known
2206 // identifiers and determine whether we have to establish
2207 // preprocessor definitions or top-level identifier declaration
2208 // chains for those identifiers.
2209 //
2210 // We copy the IdentifierInfo pointers to a small vector first,
2211 // since de-serializing declarations or macro definitions can add
2212 // new entries into the identifier table, invalidating the
2213 // iterators.
2214 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
2215 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
2216 IdEnd = PP->getIdentifierTable().end();
2217 Id != IdEnd; ++Id)
2218 Identifiers.push_back(Id->second);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002219 // We need to search the tables in all files.
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002220 for (unsigned J = 0, M = Chain.size(); J != M; ++J) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002221 ASTIdentifierLookupTable *IdTable
2222 = (ASTIdentifierLookupTable *)Chain[J]->IdentifierLookupTable;
2223 // Not all AST files necessarily have identifier tables, only the useful
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00002224 // ones.
2225 if (!IdTable)
2226 continue;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002227 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2228 IdentifierInfo *II = Identifiers[I];
2229 // Look in the on-disk hash tables for an entry for this identifier
Sebastian Redlc3632732010-10-05 15:59:54 +00002230 ASTIdentifierLookupTrait Info(*this, *Chain[J], II);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002231 std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength());
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002232 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002233 if (Pos == IdTable->end())
2234 continue;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002235
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002236 // Dereferencing the iterator has the effect of populating the
2237 // IdentifierInfo node with the various declarations it needs.
2238 (void)*Pos;
2239 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002240 }
2241 }
2242
2243 if (Context)
2244 InitializeContext(*Context);
2245
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002246 if (DeserializationListener)
2247 DeserializationListener->ReaderInitialized(this);
2248
Douglas Gregor414cb642010-11-30 05:23:00 +00002249 // If this AST file is a precompiled preamble, then set the main file ID of
2250 // the source manager to the file source file from which the preamble was
2251 // built. This is the only valid way to use a precompiled preamble.
2252 if (Type == Preamble) {
2253 SourceLocation Loc
2254 = SourceMgr.getLocation(FileMgr.getFile(getOriginalSourceFile()), 1, 1);
2255 if (Loc.isValid()) {
2256 std::pair<FileID, unsigned> Decomposed = SourceMgr.getDecomposedLoc(Loc);
2257 SourceMgr.SetPreambleFileID(Decomposed.first);
2258 }
2259 }
2260
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002261 return Success;
2262}
2263
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002264ASTReader::ASTReadResult ASTReader::ReadASTCore(llvm::StringRef FileName,
2265 ASTFileType Type) {
Sebastian Redla866e652010-10-01 19:59:12 +00002266 PerFileData *Prev = Chain.empty() ? 0 : Chain.back();
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002267 Chain.push_back(new PerFileData(Type));
Sebastian Redl9137a522010-07-16 17:50:48 +00002268 PerFileData &F = *Chain.back();
Sebastian Redla866e652010-10-01 19:59:12 +00002269 if (Prev)
2270 Prev->NextInSource = &F;
2271 else
2272 FirstInSource = &F;
2273 F.Loaders.push_back(Prev);
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002274
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002275 // Set the AST file name.
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002276 F.FileName = FileName;
2277
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002278 // Open the AST file.
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002279 //
2280 // FIXME: This shouldn't be here, we should just take a raw_ostream.
2281 std::string ErrStr;
Michael J. Spencer3a321e22010-12-09 17:36:38 +00002282 llvm::error_code ec;
2283 if (FileName == "-") {
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00002284 ec = llvm::MemoryBuffer::getSTDIN(F.Buffer);
Michael J. Spencer3a321e22010-12-09 17:36:38 +00002285 if (ec)
2286 ErrStr = ec.message();
2287 } else
Chris Lattner39b49bc2010-11-23 08:35:12 +00002288 F.Buffer.reset(FileMgr.getBufferForFile(FileName, &ErrStr));
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002289 if (!F.Buffer) {
2290 Error(ErrStr.c_str());
2291 return IgnorePCH;
2292 }
2293
2294 // Initialize the stream
2295 F.StreamFile.init((const unsigned char *)F.Buffer->getBufferStart(),
2296 (const unsigned char *)F.Buffer->getBufferEnd());
Sebastian Redl9137a522010-07-16 17:50:48 +00002297 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002298 Stream.init(F.StreamFile);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002299 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002300
2301 // Sniff for the signature.
2302 if (Stream.Read(8) != 'C' ||
2303 Stream.Read(8) != 'P' ||
2304 Stream.Read(8) != 'C' ||
2305 Stream.Read(8) != 'H') {
2306 Diag(diag::err_not_a_pch_file) << FileName;
2307 return Failure;
2308 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002309
Douglas Gregor2cf26342009-04-09 22:27:44 +00002310 while (!Stream.AtEndOfStream()) {
2311 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002312
Douglas Gregore1d918e2009-04-10 23:10:45 +00002313 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002314 Error("invalid record at top-level of AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002315 return Failure;
2316 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002317
2318 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregor668c1a42009-04-21 22:25:48 +00002319
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002320 // We only know the AST subblock ID.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002321 switch (BlockID) {
2322 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002323 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002324 Error("malformed BlockInfoBlock in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002325 return Failure;
2326 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002327 break;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002328 case AST_BLOCK_ID:
Sebastian Redl571db7f2010-08-18 23:56:56 +00002329 switch (ReadASTBlock(F)) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002330 case Success:
2331 break;
2332
2333 case Failure:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002334 return Failure;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002335
2336 case IgnorePCH:
Douglas Gregor2bec0412009-04-10 21:16:55 +00002337 // FIXME: We could consider reading through to the end of this
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002338 // AST block, skipping subblocks, to see if there are other
2339 // AST blocks elsewhere.
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002340
2341 // Clear out any preallocated source location entries, so that
2342 // the source manager does not try to resolve them later.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002343 SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002344
2345 // Remove the stat cache.
Sebastian Redl9137a522010-07-16 17:50:48 +00002346 if (F.StatCache)
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002347 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002348
Douglas Gregore1d918e2009-04-10 23:10:45 +00002349 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002350 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002351 break;
2352 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002353 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002354 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002355 return Failure;
2356 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002357 break;
2358 }
Mike Stump1eb44332009-09-09 15:08:12 +00002359 }
2360
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002361 return Success;
2362}
2363
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002364void ASTReader::setPreprocessor(Preprocessor &pp) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002365 PP = &pp;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002366
2367 unsigned TotalNum = 0;
2368 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
2369 TotalNum += Chain[I]->NumPreallocatedPreprocessingEntities;
2370 if (TotalNum) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002371 if (!PP->getPreprocessingRecord())
2372 PP->createPreprocessingRecord();
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002373 PP->getPreprocessingRecord()->SetExternalSource(*this, TotalNum);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002374 }
2375}
2376
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002377void ASTReader::InitializeContext(ASTContext &Ctx) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002378 Context = &Ctx;
2379 assert(Context && "Passed null context!");
2380
2381 assert(PP && "Forgot to set Preprocessor ?");
2382 PP->getIdentifierTable().setExternalIdentifierLookup(this);
2383 PP->getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor88a35862010-01-04 19:18:44 +00002384 PP->setExternalSource(this);
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002385
Douglas Gregor3747ee72010-10-01 01:18:02 +00002386 // If we have an update block for the TU waiting, we have to add it before
2387 // deserializing the decl.
2388 DeclContextOffsetsMap::iterator DCU = DeclContextOffsets.find(0);
2389 if (DCU != DeclContextOffsets.end()) {
2390 // Insertion could invalidate map, so grab vector.
2391 DeclContextInfos T;
2392 T.swap(DCU->second);
2393 DeclContextOffsets.erase(DCU);
2394 DeclContextOffsets[Ctx.getTranslationUnitDecl()].swap(T);
2395 }
2396
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002397 // Load the translation unit declaration
Argyrios Kyrtzidis8871a442010-07-08 17:13:02 +00002398 GetTranslationUnitDecl();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002399
2400 // Load the special types.
2401 Context->setBuiltinVaListType(
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002402 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
2403 if (unsigned Id = SpecialTypes[SPECIAL_TYPE_OBJC_ID])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002404 Context->setObjCIdType(GetType(Id));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002405 if (unsigned Sel = SpecialTypes[SPECIAL_TYPE_OBJC_SELECTOR])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002406 Context->setObjCSelType(GetType(Sel));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002407 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002408 Context->setObjCProtoType(GetType(Proto));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002409 if (unsigned Class = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002410 Context->setObjCClassType(GetType(Class));
Steve Naroff14108da2009-07-10 23:34:53 +00002411
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002412 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002413 Context->setCFConstantStringType(GetType(String));
Mike Stump1eb44332009-09-09 15:08:12 +00002414 if (unsigned FastEnum
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002415 = SpecialTypes[SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002416 Context->setObjCFastEnumerationStateType(GetType(FastEnum));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002417 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002418 QualType FileType = GetType(File);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002419 if (FileType.isNull()) {
2420 Error("FILE type is NULL");
2421 return;
2422 }
John McCall183700f2009-09-21 23:43:11 +00002423 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002424 Context->setFILEDecl(Typedef->getDecl());
2425 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002426 const TagType *Tag = FileType->getAs<TagType>();
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002427 if (!Tag) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002428 Error("Invalid FILE type in AST file");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002429 return;
2430 }
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002431 Context->setFILEDecl(Tag->getDecl());
2432 }
2433 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002434 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
Mike Stump782fa302009-07-28 02:25:19 +00002435 QualType Jmp_bufType = GetType(Jmp_buf);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002436 if (Jmp_bufType.isNull()) {
2437 Error("jmp_bug type is NULL");
2438 return;
2439 }
John McCall183700f2009-09-21 23:43:11 +00002440 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
Mike Stump782fa302009-07-28 02:25:19 +00002441 Context->setjmp_bufDecl(Typedef->getDecl());
2442 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002443 const TagType *Tag = Jmp_bufType->getAs<TagType>();
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002444 if (!Tag) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002445 Error("Invalid jmp_buf type in AST file");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002446 return;
2447 }
Mike Stump782fa302009-07-28 02:25:19 +00002448 Context->setjmp_bufDecl(Tag->getDecl());
2449 }
2450 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002451 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
Mike Stump782fa302009-07-28 02:25:19 +00002452 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002453 if (Sigjmp_bufType.isNull()) {
2454 Error("sigjmp_buf type is NULL");
2455 return;
2456 }
John McCall183700f2009-09-21 23:43:11 +00002457 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
Mike Stump782fa302009-07-28 02:25:19 +00002458 Context->setsigjmp_bufDecl(Typedef->getDecl());
2459 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002460 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002461 assert(Tag && "Invalid sigjmp_buf type in AST file");
Mike Stump782fa302009-07-28 02:25:19 +00002462 Context->setsigjmp_bufDecl(Tag->getDecl());
2463 }
2464 }
Mike Stump1eb44332009-09-09 15:08:12 +00002465 if (unsigned ObjCIdRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002466 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION])
Douglas Gregord1571ac2009-08-21 00:27:50 +00002467 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
Mike Stump1eb44332009-09-09 15:08:12 +00002468 if (unsigned ObjCClassRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002469 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION])
Douglas Gregord1571ac2009-08-21 00:27:50 +00002470 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002471 if (unsigned String = SpecialTypes[SPECIAL_TYPE_BLOCK_DESCRIPTOR])
Mike Stumpadaaad32009-10-20 02:12:22 +00002472 Context->setBlockDescriptorType(GetType(String));
Mike Stump083c25e2009-10-22 00:49:09 +00002473 if (unsigned String
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002474 = SpecialTypes[SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR])
Mike Stump083c25e2009-10-22 00:49:09 +00002475 Context->setBlockDescriptorExtendedType(GetType(String));
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002476 if (unsigned ObjCSelRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002477 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION])
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002478 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002479 if (unsigned String = SpecialTypes[SPECIAL_TYPE_NS_CONSTANT_STRING])
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002480 Context->setNSConstantStringType(GetType(String));
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002481
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002482 if (SpecialTypes[SPECIAL_TYPE_INT128_INSTALLED])
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002483 Context->setInt128Installed();
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002484
2485 ReadUserDiagnosticMappings(Context->getDiagnostics());
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002486}
2487
Douglas Gregorb64c1932009-05-12 01:31:05 +00002488/// \brief Retrieve the name of the original source file name
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002489/// directly from the AST file, without actually loading the AST
Douglas Gregorb64c1932009-05-12 01:31:05 +00002490/// file.
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002491std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00002492 FileManager &FileMgr,
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00002493 Diagnostic &Diags) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002494 // Open the AST file.
Douglas Gregorb64c1932009-05-12 01:31:05 +00002495 std::string ErrStr;
2496 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner39b49bc2010-11-23 08:35:12 +00002497 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregorb64c1932009-05-12 01:31:05 +00002498 if (!Buffer) {
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00002499 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002500 return std::string();
2501 }
2502
2503 // Initialize the stream
2504 llvm::BitstreamReader StreamFile;
2505 llvm::BitstreamCursor Stream;
Mike Stump1eb44332009-09-09 15:08:12 +00002506 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregorb64c1932009-05-12 01:31:05 +00002507 (const unsigned char *)Buffer->getBufferEnd());
2508 Stream.init(StreamFile);
2509
2510 // Sniff for the signature.
2511 if (Stream.Read(8) != 'C' ||
2512 Stream.Read(8) != 'P' ||
2513 Stream.Read(8) != 'C' ||
2514 Stream.Read(8) != 'H') {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002515 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002516 return std::string();
2517 }
2518
2519 RecordData Record;
2520 while (!Stream.AtEndOfStream()) {
2521 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002522
Douglas Gregorb64c1932009-05-12 01:31:05 +00002523 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2524 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump1eb44332009-09-09 15:08:12 +00002525
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002526 // We only know the AST subblock ID.
Douglas Gregorb64c1932009-05-12 01:31:05 +00002527 switch (BlockID) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002528 case AST_BLOCK_ID:
2529 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002530 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002531 return std::string();
2532 }
2533 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002534
Douglas Gregorb64c1932009-05-12 01:31:05 +00002535 default:
2536 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002537 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002538 return std::string();
2539 }
2540 break;
2541 }
2542 continue;
2543 }
2544
2545 if (Code == llvm::bitc::END_BLOCK) {
2546 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002547 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002548 return std::string();
2549 }
2550 continue;
2551 }
2552
2553 if (Code == llvm::bitc::DEFINE_ABBREV) {
2554 Stream.ReadAbbrevRecord();
2555 continue;
2556 }
2557
2558 Record.clear();
2559 const char *BlobStart = 0;
2560 unsigned BlobLen = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002561 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002562 == ORIGINAL_FILE_NAME)
Douglas Gregorb64c1932009-05-12 01:31:05 +00002563 return std::string(BlobStart, BlobLen);
Mike Stump1eb44332009-09-09 15:08:12 +00002564 }
Douglas Gregorb64c1932009-05-12 01:31:05 +00002565
2566 return std::string();
2567}
2568
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002569/// \brief Parse the record that corresponds to a LangOptions data
2570/// structure.
2571///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002572/// This routine parses the language options from the AST file and then gives
2573/// them to the AST listener if one is set.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002574///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002575/// \returns true if the listener deems the file unacceptable, false otherwise.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002576bool ASTReader::ParseLanguageOptions(
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002577 const llvm::SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002578 if (Listener) {
2579 LangOptions LangOpts;
Mike Stump1eb44332009-09-09 15:08:12 +00002580
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002581 #define PARSE_LANGOPT(Option) \
2582 LangOpts.Option = Record[Idx]; \
2583 ++Idx
Mike Stump1eb44332009-09-09 15:08:12 +00002584
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002585 unsigned Idx = 0;
2586 PARSE_LANGOPT(Trigraphs);
2587 PARSE_LANGOPT(BCPLComment);
2588 PARSE_LANGOPT(DollarIdents);
2589 PARSE_LANGOPT(AsmPreprocessor);
2590 PARSE_LANGOPT(GNUMode);
Chandler Carrutheb5d7b72010-04-17 20:17:31 +00002591 PARSE_LANGOPT(GNUKeywords);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002592 PARSE_LANGOPT(ImplicitInt);
2593 PARSE_LANGOPT(Digraphs);
2594 PARSE_LANGOPT(HexFloats);
2595 PARSE_LANGOPT(C99);
2596 PARSE_LANGOPT(Microsoft);
2597 PARSE_LANGOPT(CPlusPlus);
2598 PARSE_LANGOPT(CPlusPlus0x);
2599 PARSE_LANGOPT(CXXOperatorNames);
2600 PARSE_LANGOPT(ObjC1);
2601 PARSE_LANGOPT(ObjC2);
2602 PARSE_LANGOPT(ObjCNonFragileABI);
Fariborz Jahanian412e7982010-02-09 19:31:38 +00002603 PARSE_LANGOPT(ObjCNonFragileABI2);
Fariborz Jahanianf84109e2011-01-07 18:59:25 +00002604 PARSE_LANGOPT(AppleKext);
Ted Kremenekc32647d2010-12-23 21:35:43 +00002605 PARSE_LANGOPT(ObjCDefaultSynthProperties);
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +00002606 PARSE_LANGOPT(NoConstantCFStrings);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002607 PARSE_LANGOPT(PascalStrings);
2608 PARSE_LANGOPT(WritableStrings);
2609 PARSE_LANGOPT(LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +00002610 PARSE_LANGOPT(AltiVec);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002611 PARSE_LANGOPT(Exceptions);
Daniel Dunbar73482882010-02-10 18:48:44 +00002612 PARSE_LANGOPT(SjLjExceptions);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002613 PARSE_LANGOPT(NeXTRuntime);
2614 PARSE_LANGOPT(Freestanding);
2615 PARSE_LANGOPT(NoBuiltin);
2616 PARSE_LANGOPT(ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +00002617 PARSE_LANGOPT(POSIXThreads);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002618 PARSE_LANGOPT(Blocks);
2619 PARSE_LANGOPT(EmitAllDecls);
2620 PARSE_LANGOPT(MathErrno);
Chris Lattnera4d71452010-06-26 21:25:03 +00002621 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
2622 Record[Idx++]);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002623 PARSE_LANGOPT(HeinousExtensions);
2624 PARSE_LANGOPT(Optimize);
2625 PARSE_LANGOPT(OptimizeSize);
2626 PARSE_LANGOPT(Static);
2627 PARSE_LANGOPT(PICLevel);
2628 PARSE_LANGOPT(GNUInline);
2629 PARSE_LANGOPT(NoInline);
2630 PARSE_LANGOPT(AccessControl);
2631 PARSE_LANGOPT(CharIsSigned);
John Thompsona6fda122009-11-05 20:14:16 +00002632 PARSE_LANGOPT(ShortWChar);
Chris Lattnera4d71452010-06-26 21:25:03 +00002633 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
John McCall1fb0caa2010-10-22 21:05:15 +00002634 LangOpts.setVisibilityMode((Visibility)Record[Idx++]);
Daniel Dunbarab8e2812009-09-21 04:16:19 +00002635 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
Chris Lattnera4d71452010-06-26 21:25:03 +00002636 Record[Idx++]);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002637 PARSE_LANGOPT(InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +00002638 PARSE_LANGOPT(OpenCL);
Peter Collingbourne08a53262010-12-01 19:14:57 +00002639 PARSE_LANGOPT(CUDA);
Mike Stump9c276ae2009-12-12 01:27:46 +00002640 PARSE_LANGOPT(CatchUndefined);
2641 // FIXME: Missing ElideConstructors?!
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002642 #undef PARSE_LANGOPT
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002643
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002644 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002645 }
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002646
2647 return false;
2648}
2649
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002650void ASTReader::ReadPreprocessedEntities() {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002651 ReadDefinedMacros();
2652}
2653
Douglas Gregor89d99802010-11-30 06:16:57 +00002654PreprocessedEntity *ASTReader::ReadPreprocessedEntity(uint64_t Offset) {
2655 PerFileData *F = 0;
2656 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2657 if (Offset < Chain[I]->SizeInBits) {
2658 F = Chain[I];
2659 break;
2660 }
2661
2662 Offset -= Chain[I]->SizeInBits;
2663 }
2664
2665 if (!F) {
2666 Error("Malformed preprocessed entity offset");
2667 return 0;
2668 }
2669
2670 return ReadMacroRecord(*F, Offset);
2671}
2672
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002673void ASTReader::ReadUserDiagnosticMappings(Diagnostic &Diag) {
2674 unsigned Idx = 0;
2675 while (Idx < UserDiagMappings.size()) {
2676 unsigned DiagID = UserDiagMappings[Idx++];
2677 unsigned Map = UserDiagMappings[Idx++];
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00002678 Diag.setDiagnosticMappingInternal(DiagID, Map, Diag.GetCurDiagState(),
2679 /*isUser=*/true);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002680 }
2681}
2682
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002683/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002684ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002685 PerFileData *F = 0;
2686 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2687 F = Chain[N - I - 1];
2688 if (Index < F->LocalNumTypes)
2689 break;
2690 Index -= F->LocalNumTypes;
2691 }
2692 assert(F && F->LocalNumTypes > Index && "Broken chain");
Sebastian Redlc3632732010-10-05 15:59:54 +00002693 return RecordLocation(F, F->TypeOffsets[Index]);
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002694}
2695
2696/// \brief Read and return the type with the given index..
Douglas Gregor2cf26342009-04-09 22:27:44 +00002697///
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002698/// The index is the type ID, shifted and minus the number of predefs. This
2699/// routine actually reads the record corresponding to the type at the given
2700/// location. It is a helper routine for GetType, which deals with reading type
2701/// IDs.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002702QualType ASTReader::ReadTypeRecord(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002703 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00002704 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00002705
Douglas Gregor0b748912009-04-14 21:18:50 +00002706 // Keep track of where we are in the stream, then jump back there
2707 // after reading this type.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002708 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregor0b748912009-04-14 21:18:50 +00002709
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00002710 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redl27372b42010-08-11 18:52:41 +00002711
Douglas Gregord89275b2009-07-06 18:54:52 +00002712 // Note that we are loading a type record.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00002713 Deserializing AType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00002714
Sebastian Redlc3632732010-10-05 15:59:54 +00002715 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002716 RecordData Record;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002717 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002718 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
2719 case TYPE_EXT_QUAL: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002720 if (Record.size() != 2) {
2721 Error("Incorrect encoding of extended qualifier type");
2722 return QualType();
2723 }
Douglas Gregor6d473962009-04-15 22:00:08 +00002724 QualType Base = GetType(Record[0]);
John McCall0953e762009-09-24 19:53:00 +00002725 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]);
2726 return Context->getQualifiedType(Base, Quals);
Douglas Gregor6d473962009-04-15 22:00:08 +00002727 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002728
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002729 case TYPE_COMPLEX: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002730 if (Record.size() != 1) {
2731 Error("Incorrect encoding of complex type");
2732 return QualType();
2733 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002734 QualType ElemType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002735 return Context->getComplexType(ElemType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002736 }
2737
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002738 case TYPE_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002739 if (Record.size() != 1) {
2740 Error("Incorrect encoding of pointer type");
2741 return QualType();
2742 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002743 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002744 return Context->getPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002745 }
2746
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002747 case TYPE_BLOCK_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002748 if (Record.size() != 1) {
2749 Error("Incorrect encoding of block pointer type");
2750 return QualType();
2751 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002752 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002753 return Context->getBlockPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002754 }
2755
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002756 case TYPE_LVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002757 if (Record.size() != 1) {
2758 Error("Incorrect encoding of lvalue reference type");
2759 return QualType();
2760 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002761 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002762 return Context->getLValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002763 }
2764
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002765 case TYPE_RVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002766 if (Record.size() != 1) {
2767 Error("Incorrect encoding of rvalue reference type");
2768 return QualType();
2769 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002770 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002771 return Context->getRValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002772 }
2773
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002774 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidis240437b2010-07-02 11:55:15 +00002775 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002776 Error("Incorrect encoding of member pointer type");
2777 return QualType();
2778 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002779 QualType PointeeType = GetType(Record[0]);
2780 QualType ClassType = GetType(Record[1]);
Douglas Gregor1ab55e92010-12-10 17:03:06 +00002781 if (PointeeType.isNull() || ClassType.isNull())
2782 return QualType();
2783
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002784 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002785 }
2786
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002787 case TYPE_CONSTANT_ARRAY: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002788 QualType ElementType = GetType(Record[0]);
2789 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2790 unsigned IndexTypeQuals = Record[2];
2791 unsigned Idx = 3;
2792 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002793 return Context->getConstantArrayType(ElementType, Size,
2794 ASM, IndexTypeQuals);
2795 }
2796
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002797 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002798 QualType ElementType = GetType(Record[0]);
2799 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2800 unsigned IndexTypeQuals = Record[2];
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002801 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002802 }
2803
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002804 case TYPE_VARIABLE_ARRAY: {
Douglas Gregor0b748912009-04-14 21:18:50 +00002805 QualType ElementType = GetType(Record[0]);
2806 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2807 unsigned IndexTypeQuals = Record[2];
Sebastian Redlc3632732010-10-05 15:59:54 +00002808 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
2809 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
2810 return Context->getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002811 ASM, IndexTypeQuals,
2812 SourceRange(LBLoc, RBLoc));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002813 }
2814
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002815 case TYPE_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00002816 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002817 Error("incorrect encoding of vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002818 return QualType();
2819 }
2820
2821 QualType ElementType = GetType(Record[0]);
2822 unsigned NumElements = Record[1];
Bob Wilsone86d78c2010-11-10 21:56:12 +00002823 unsigned VecKind = Record[2];
Chris Lattner788b0fd2010-06-23 06:00:24 +00002824 return Context->getVectorType(ElementType, NumElements,
Bob Wilsone86d78c2010-11-10 21:56:12 +00002825 (VectorType::VectorKind)VecKind);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002826 }
2827
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002828 case TYPE_EXT_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00002829 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002830 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002831 return QualType();
2832 }
2833
2834 QualType ElementType = GetType(Record[0]);
2835 unsigned NumElements = Record[1];
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002836 return Context->getExtVectorType(ElementType, NumElements);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002837 }
2838
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002839 case TYPE_FUNCTION_NO_PROTO: {
Rafael Espindola425ef722010-03-30 22:15:11 +00002840 if (Record.size() != 4) {
Douglas Gregora02b1472009-04-28 21:53:25 +00002841 Error("incorrect encoding of no-proto function type");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002842 return QualType();
2843 }
2844 QualType ResultType = GetType(Record[0]);
Rafael Espindola425ef722010-03-30 22:15:11 +00002845 FunctionType::ExtInfo Info(Record[1], Record[2], (CallingConv)Record[3]);
Rafael Espindola264ba482010-03-30 20:24:48 +00002846 return Context->getFunctionNoProtoType(ResultType, Info);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002847 }
2848
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002849 case TYPE_FUNCTION_PROTO: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002850 QualType ResultType = GetType(Record[0]);
John McCalle23cf432010-12-14 08:05:40 +00002851
2852 FunctionProtoType::ExtProtoInfo EPI;
2853 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
2854 /*regparm*/ Record[2],
2855 static_cast<CallingConv>(Record[3]));
2856
Rafael Espindola425ef722010-03-30 22:15:11 +00002857 unsigned Idx = 4;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002858 unsigned NumParams = Record[Idx++];
2859 llvm::SmallVector<QualType, 16> ParamTypes;
2860 for (unsigned I = 0; I != NumParams; ++I)
2861 ParamTypes.push_back(GetType(Record[Idx++]));
John McCalle23cf432010-12-14 08:05:40 +00002862
2863 EPI.Variadic = Record[Idx++];
2864 EPI.TypeQuals = Record[Idx++];
2865 EPI.HasExceptionSpec = Record[Idx++];
2866 EPI.HasAnyExceptionSpec = Record[Idx++];
2867 EPI.NumExceptions = Record[Idx++];
Sebastian Redl465226e2009-05-27 22:11:52 +00002868 llvm::SmallVector<QualType, 2> Exceptions;
John McCalle23cf432010-12-14 08:05:40 +00002869 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
Sebastian Redl465226e2009-05-27 22:11:52 +00002870 Exceptions.push_back(GetType(Record[Idx++]));
John McCalle23cf432010-12-14 08:05:40 +00002871 EPI.Exceptions = Exceptions.data();
Jay Foadbeaaccd2009-05-21 09:52:38 +00002872 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
John McCalle23cf432010-12-14 08:05:40 +00002873 EPI);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002874 }
2875
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002876 case TYPE_UNRESOLVED_USING:
John McCalled976492009-12-04 22:46:56 +00002877 return Context->getTypeDeclType(
2878 cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0])));
2879
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002880 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002881 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002882 Error("incorrect encoding of typedef type");
2883 return QualType();
2884 }
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002885 TypedefDecl *Decl = cast<TypedefDecl>(GetDecl(Record[0]));
2886 QualType Canonical = GetType(Record[1]);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00002887 if (!Canonical.isNull())
2888 Canonical = Context->getCanonicalType(Canonical);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002889 return Context->getTypedefType(Decl, Canonical);
2890 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002891
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002892 case TYPE_TYPEOF_EXPR:
Sebastian Redlc3632732010-10-05 15:59:54 +00002893 return Context->getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002894
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002895 case TYPE_TYPEOF: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002896 if (Record.size() != 1) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002897 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002898 return QualType();
2899 }
2900 QualType UnderlyingType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002901 return Context->getTypeOfType(UnderlyingType);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002902 }
Mike Stump1eb44332009-09-09 15:08:12 +00002903
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002904 case TYPE_DECLTYPE:
Sebastian Redlc3632732010-10-05 15:59:54 +00002905 return Context->getDecltypeType(ReadExpr(*Loc.F));
Anders Carlsson395b4752009-06-24 19:06:50 +00002906
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002907 case TYPE_RECORD: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002908 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002909 Error("incorrect encoding of record type");
2910 return QualType();
2911 }
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002912 bool IsDependent = Record[0];
2913 QualType T = Context->getRecordType(cast<RecordDecl>(GetDecl(Record[1])));
John McCallb870b882010-10-14 21:48:26 +00002914 T->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002915 return T;
2916 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002917
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002918 case TYPE_ENUM: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002919 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002920 Error("incorrect encoding of enum type");
2921 return QualType();
2922 }
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002923 bool IsDependent = Record[0];
2924 QualType T = Context->getEnumType(cast<EnumDecl>(GetDecl(Record[1])));
John McCallb870b882010-10-14 21:48:26 +00002925 T->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002926 return T;
2927 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002928
John McCall9d156a72011-01-06 01:58:22 +00002929 case TYPE_ATTRIBUTED: {
2930 if (Record.size() != 3) {
2931 Error("incorrect encoding of attributed type");
2932 return QualType();
2933 }
2934 QualType modifiedType = GetType(Record[0]);
2935 QualType equivalentType = GetType(Record[1]);
2936 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
2937 return Context->getAttributedType(kind, modifiedType, equivalentType);
2938 }
2939
Abramo Bagnara075f8f12010-12-10 16:29:40 +00002940 case TYPE_PAREN: {
2941 if (Record.size() != 1) {
2942 Error("incorrect encoding of paren type");
2943 return QualType();
2944 }
2945 QualType InnerType = GetType(Record[0]);
2946 return Context->getParenType(InnerType);
2947 }
2948
Douglas Gregor7536dd52010-12-20 02:24:11 +00002949 case TYPE_PACK_EXPANSION: {
2950 if (Record.size() != 1) {
2951 Error("incorrect encoding of pack expansion type");
2952 return QualType();
2953 }
2954 QualType Pattern = GetType(Record[0]);
2955 if (Pattern.isNull())
2956 return QualType();
2957
2958 return Context->getPackExpansionType(Pattern);
2959 }
2960
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002961 case TYPE_ELABORATED: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00002962 unsigned Idx = 0;
2963 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2964 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2965 QualType NamedType = GetType(Record[Idx++]);
2966 return Context->getElaboratedType(Keyword, NNS, NamedType);
John McCall7da24312009-09-05 00:15:47 +00002967 }
2968
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002969 case TYPE_OBJC_INTERFACE: {
Chris Lattnerc6fa4452009-04-22 06:45:28 +00002970 unsigned Idx = 0;
2971 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
John McCallc12c5bb2010-05-15 11:32:37 +00002972 return Context->getObjCInterfaceType(ItfD);
2973 }
2974
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002975 case TYPE_OBJC_OBJECT: {
John McCallc12c5bb2010-05-15 11:32:37 +00002976 unsigned Idx = 0;
2977 QualType Base = GetType(Record[Idx++]);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00002978 unsigned NumProtos = Record[Idx++];
2979 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2980 for (unsigned I = 0; I != NumProtos; ++I)
2981 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer20249a12010-10-21 03:16:25 +00002982 return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00002983 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002984
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002985 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00002986 unsigned Idx = 0;
John McCallc12c5bb2010-05-15 11:32:37 +00002987 QualType Pointee = GetType(Record[Idx++]);
2988 return Context->getObjCObjectPointerType(Pointee);
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00002989 }
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +00002990
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002991 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCall49a832b2009-10-18 09:09:24 +00002992 unsigned Idx = 0;
2993 QualType Parm = GetType(Record[Idx++]);
2994 QualType Replacement = GetType(Record[Idx++]);
2995 return
2996 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
2997 Replacement);
2998 }
John McCall3cb0ebd2010-03-10 03:28:59 +00002999
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003000 case TYPE_INJECTED_CLASS_NAME: {
John McCall3cb0ebd2010-03-10 03:28:59 +00003001 CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0]));
3002 QualType TST = GetType(Record[1]); // probably derivable
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00003003 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003004 // for AST reading, too much interdependencies.
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00003005 return
3006 QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCall3cb0ebd2010-03-10 03:28:59 +00003007 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003008
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003009 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003010 unsigned Idx = 0;
3011 unsigned Depth = Record[Idx++];
3012 unsigned Index = Record[Idx++];
3013 bool Pack = Record[Idx++];
3014 IdentifierInfo *Name = GetIdentifierInfo(Record, Idx);
3015 return Context->getTemplateTypeParmType(Depth, Index, Pack, Name);
3016 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003017
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003018 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00003019 unsigned Idx = 0;
3020 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3021 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3022 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +00003023 QualType Canon = GetType(Record[Idx++]);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00003024 if (!Canon.isNull())
3025 Canon = Context->getCanonicalType(Canon);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +00003026 return Context->getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00003027 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003028
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003029 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003030 unsigned Idx = 0;
3031 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3032 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3033 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
3034 unsigned NumArgs = Record[Idx++];
3035 llvm::SmallVector<TemplateArgument, 8> Args;
3036 Args.reserve(NumArgs);
3037 while (NumArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00003038 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003039 return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
3040 Args.size(), Args.data());
3041 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003042
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003043 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00003044 unsigned Idx = 0;
3045
3046 // ArrayType
3047 QualType ElementType = GetType(Record[Idx++]);
3048 ArrayType::ArraySizeModifier ASM
3049 = (ArrayType::ArraySizeModifier)Record[Idx++];
3050 unsigned IndexTypeQuals = Record[Idx++];
3051
3052 // DependentSizedArrayType
Sebastian Redlc3632732010-10-05 15:59:54 +00003053 Expr *NumElts = ReadExpr(*Loc.F);
3054 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00003055
3056 return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
3057 IndexTypeQuals, Brackets);
3058 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003059
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003060 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003061 unsigned Idx = 0;
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003062 bool IsDependent = Record[Idx++];
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003063 TemplateName Name = ReadTemplateName(Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003064 llvm::SmallVector<TemplateArgument, 8> Args;
Sebastian Redlc3632732010-10-05 15:59:54 +00003065 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003066 QualType Canon = GetType(Record[Idx++]);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003067 QualType T;
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003068 if (Canon.isNull())
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003069 T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(),
3070 Args.size());
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003071 else
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003072 T = Context->getTemplateSpecializationType(Name, Args.data(),
3073 Args.size(), Canon);
John McCallb870b882010-10-14 21:48:26 +00003074 T->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003075 return T;
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003076 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003077 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003078 // Suppress a GCC warning
3079 return QualType();
3080}
3081
Sebastian Redlc3632732010-10-05 15:59:54 +00003082class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003083 ASTReader &Reader;
Sebastian Redlc3632732010-10-05 15:59:54 +00003084 ASTReader::PerFileData &F;
Sebastian Redl577d4792010-07-22 22:43:28 +00003085 llvm::BitstreamCursor &DeclsCursor;
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003086 const ASTReader::RecordData &Record;
John McCalla1ee0c52009-10-16 21:56:05 +00003087 unsigned &Idx;
3088
Sebastian Redlc3632732010-10-05 15:59:54 +00003089 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
3090 unsigned &I) {
3091 return Reader.ReadSourceLocation(F, R, I);
3092 }
3093
John McCalla1ee0c52009-10-16 21:56:05 +00003094public:
Sebastian Redlc3632732010-10-05 15:59:54 +00003095 TypeLocReader(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003096 const ASTReader::RecordData &Record, unsigned &Idx)
Sebastian Redlc3632732010-10-05 15:59:54 +00003097 : Reader(Reader), F(F), DeclsCursor(F.DeclsCursor), Record(Record), Idx(Idx)
3098 { }
John McCalla1ee0c52009-10-16 21:56:05 +00003099
John McCall51bd8032009-10-18 01:05:36 +00003100 // We want compile-time assurance that we've enumerated all of
3101 // these, so unfortunately we have to declare them first, then
3102 // define them out-of-line.
3103#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +00003104#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +00003105 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00003106#include "clang/AST/TypeLocNodes.def"
3107
John McCall51bd8032009-10-18 01:05:36 +00003108 void VisitFunctionTypeLoc(FunctionTypeLoc);
3109 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00003110};
3111
John McCall51bd8032009-10-18 01:05:36 +00003112void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCalla1ee0c52009-10-16 21:56:05 +00003113 // nothing to do
3114}
John McCall51bd8032009-10-18 01:05:36 +00003115void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003116 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorddf889a2010-01-18 18:04:31 +00003117 if (TL.needsExtraLocalData()) {
3118 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
3119 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
3120 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
3121 TL.setModeAttr(Record[Idx++]);
3122 }
John McCalla1ee0c52009-10-16 21:56:05 +00003123}
John McCall51bd8032009-10-18 01:05:36 +00003124void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003125 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003126}
John McCall51bd8032009-10-18 01:05:36 +00003127void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003128 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003129}
John McCall51bd8032009-10-18 01:05:36 +00003130void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003131 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003132}
John McCall51bd8032009-10-18 01:05:36 +00003133void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003134 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003135}
John McCall51bd8032009-10-18 01:05:36 +00003136void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003137 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003138}
John McCall51bd8032009-10-18 01:05:36 +00003139void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003140 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003141}
John McCall51bd8032009-10-18 01:05:36 +00003142void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003143 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
3144 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003145 if (Record[Idx++])
Sebastian Redlc3632732010-10-05 15:59:54 +00003146 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003147 else
John McCall51bd8032009-10-18 01:05:36 +00003148 TL.setSizeExpr(0);
3149}
3150void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
3151 VisitArrayTypeLoc(TL);
3152}
3153void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
3154 VisitArrayTypeLoc(TL);
3155}
3156void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
3157 VisitArrayTypeLoc(TL);
3158}
3159void TypeLocReader::VisitDependentSizedArrayTypeLoc(
3160 DependentSizedArrayTypeLoc TL) {
3161 VisitArrayTypeLoc(TL);
3162}
3163void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
3164 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003165 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003166}
3167void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003168 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003169}
3170void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003171 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003172}
3173void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003174 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3175 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
Douglas Gregordab60ad2010-10-01 18:44:50 +00003176 TL.setTrailingReturn(Record[Idx++]);
John McCall51bd8032009-10-18 01:05:36 +00003177 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
John McCall86acc2a2009-10-23 01:28:53 +00003178 TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
John McCall51bd8032009-10-18 01:05:36 +00003179 }
3180}
3181void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
3182 VisitFunctionTypeLoc(TL);
3183}
3184void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
3185 VisitFunctionTypeLoc(TL);
3186}
John McCalled976492009-12-04 22:46:56 +00003187void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003188 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalled976492009-12-04 22:46:56 +00003189}
John McCall51bd8032009-10-18 01:05:36 +00003190void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003191 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003192}
3193void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003194 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3195 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3196 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003197}
3198void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003199 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3200 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3201 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3202 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003203}
3204void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003205 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003206}
3207void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003208 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003209}
3210void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003211 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003212}
John McCall9d156a72011-01-06 01:58:22 +00003213void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
3214 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
3215 if (TL.hasAttrOperand()) {
3216 SourceRange range;
3217 range.setBegin(ReadSourceLocation(Record, Idx));
3218 range.setEnd(ReadSourceLocation(Record, Idx));
3219 TL.setAttrOperandParensRange(range);
3220 }
3221 if (TL.hasAttrExprOperand()) {
3222 if (Record[Idx++])
3223 TL.setAttrExprOperand(Reader.ReadExpr(F));
3224 else
3225 TL.setAttrExprOperand(0);
3226 } else if (TL.hasAttrEnumOperand())
3227 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
3228}
John McCall51bd8032009-10-18 01:05:36 +00003229void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003230 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003231}
John McCall49a832b2009-10-18 09:09:24 +00003232void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
3233 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003234 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall49a832b2009-10-18 09:09:24 +00003235}
John McCall51bd8032009-10-18 01:05:36 +00003236void TypeLocReader::VisitTemplateSpecializationTypeLoc(
3237 TemplateSpecializationTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003238 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
3239 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3240 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall833ca992009-10-29 08:12:44 +00003241 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
3242 TL.setArgLocInfo(i,
Sebastian Redlc3632732010-10-05 15:59:54 +00003243 Reader.GetTemplateArgumentLocInfo(F,
3244 TL.getTypePtr()->getArg(i).getKind(),
3245 Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003246}
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003247void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
3248 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3249 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3250}
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003251void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003252 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3253 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003254}
John McCall3cb0ebd2010-03-10 03:28:59 +00003255void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003256 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall3cb0ebd2010-03-10 03:28:59 +00003257}
Douglas Gregor4714c122010-03-31 17:34:00 +00003258void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003259 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3260 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
3261 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003262}
John McCall33500952010-06-11 00:33:02 +00003263void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
3264 DependentTemplateSpecializationTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003265 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3266 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
3267 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3268 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3269 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall33500952010-06-11 00:33:02 +00003270 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
3271 TL.setArgLocInfo(I,
Sebastian Redlc3632732010-10-05 15:59:54 +00003272 Reader.GetTemplateArgumentLocInfo(F,
3273 TL.getTypePtr()->getArg(I).getKind(),
3274 Record, Idx));
John McCall33500952010-06-11 00:33:02 +00003275}
Douglas Gregor7536dd52010-12-20 02:24:11 +00003276void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
3277 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
3278}
John McCall51bd8032009-10-18 01:05:36 +00003279void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003280 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallc12c5bb2010-05-15 11:32:37 +00003281}
3282void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3283 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00003284 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3285 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003286 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redlc3632732010-10-05 15:59:54 +00003287 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003288}
John McCall54e14c42009-10-22 22:37:11 +00003289void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003290 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall54e14c42009-10-22 22:37:11 +00003291}
John McCalla1ee0c52009-10-16 21:56:05 +00003292
Sebastian Redlc3632732010-10-05 15:59:54 +00003293TypeSourceInfo *ASTReader::GetTypeSourceInfo(PerFileData &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00003294 const RecordData &Record,
John McCalla1ee0c52009-10-16 21:56:05 +00003295 unsigned &Idx) {
3296 QualType InfoTy = GetType(Record[Idx++]);
3297 if (InfoTy.isNull())
3298 return 0;
3299
John McCalla93c9342009-12-07 02:54:59 +00003300 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
Sebastian Redlc3632732010-10-05 15:59:54 +00003301 TypeLocReader TLR(*this, F, Record, Idx);
John McCalla93c9342009-12-07 02:54:59 +00003302 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCalla1ee0c52009-10-16 21:56:05 +00003303 TLR.Visit(TL);
John McCalla93c9342009-12-07 02:54:59 +00003304 return TInfo;
John McCalla1ee0c52009-10-16 21:56:05 +00003305}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003306
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003307QualType ASTReader::GetType(TypeID ID) {
John McCall0953e762009-09-24 19:53:00 +00003308 unsigned FastQuals = ID & Qualifiers::FastMask;
3309 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003310
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003311 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003312 QualType T;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003313 switch ((PredefinedTypeIDs)Index) {
3314 case PREDEF_TYPE_NULL_ID: return QualType();
3315 case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
3316 case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003317
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003318 case PREDEF_TYPE_CHAR_U_ID:
3319 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregor2cf26342009-04-09 22:27:44 +00003320 // FIXME: Check that the signedness of CharTy is correct!
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003321 T = Context->CharTy;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003322 break;
3323
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003324 case PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break;
3325 case PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break;
3326 case PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break;
3327 case PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break;
3328 case PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break;
3329 case PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break;
3330 case PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break;
3331 case PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break;
3332 case PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break;
3333 case PREDEF_TYPE_INT_ID: T = Context->IntTy; break;
3334 case PREDEF_TYPE_LONG_ID: T = Context->LongTy; break;
3335 case PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break;
3336 case PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break;
3337 case PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break;
3338 case PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break;
3339 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break;
3340 case PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break;
3341 case PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break;
3342 case PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break;
3343 case PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break;
3344 case PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break;
3345 case PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break;
3346 case PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break;
3347 case PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003348 }
3349
3350 assert(!T.isNull() && "Unknown predefined type");
John McCall0953e762009-09-24 19:53:00 +00003351 return T.withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003352 }
3353
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003354 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003355 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl07a353c2010-07-14 20:26:45 +00003356 if (TypesLoaded[Index].isNull()) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003357 TypesLoaded[Index] = ReadTypeRecord(Index);
Douglas Gregor97475832010-10-05 18:37:06 +00003358 if (TypesLoaded[Index].isNull())
3359 return QualType();
3360
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003361 TypesLoaded[Index]->setFromAST();
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003362 TypeIdxs[TypesLoaded[Index]] = TypeIdx::fromTypeID(ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003363 if (DeserializationListener)
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003364 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1476ed42010-07-16 16:36:56 +00003365 TypesLoaded[Index]);
Sebastian Redl07a353c2010-07-14 20:26:45 +00003366 }
Mike Stump1eb44332009-09-09 15:08:12 +00003367
John McCall0953e762009-09-24 19:53:00 +00003368 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003369}
3370
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003371TypeID ASTReader::GetTypeID(QualType T) const {
3372 return MakeTypeID(T,
3373 std::bind1st(std::mem_fun(&ASTReader::GetTypeIdx), this));
3374}
3375
3376TypeIdx ASTReader::GetTypeIdx(QualType T) const {
3377 if (T.isNull())
3378 return TypeIdx();
3379 assert(!T.getLocalFastQualifiers());
3380
3381 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3382 // GetTypeIdx is mostly used for computing the hash of DeclarationNames and
3383 // comparing keys of ASTDeclContextNameLookupTable.
3384 // If the type didn't come from the AST file use a specially marked index
3385 // so that any hash/key comparison fail since no such index is stored
3386 // in a AST file.
3387 if (I == TypeIdxs.end())
3388 return TypeIdx(-1);
3389 return I->second;
3390}
3391
Douglas Gregor7c789c12010-10-29 22:39:52 +00003392unsigned ASTReader::getTotalNumCXXBaseSpecifiers() const {
3393 unsigned Result = 0;
3394 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
3395 Result += Chain[I]->LocalNumCXXBaseSpecifiers;
3396
3397 return Result;
3398}
3399
John McCall833ca992009-10-29 08:12:44 +00003400TemplateArgumentLocInfo
Sebastian Redlc3632732010-10-05 15:59:54 +00003401ASTReader::GetTemplateArgumentLocInfo(PerFileData &F,
3402 TemplateArgument::ArgKind Kind,
John McCall833ca992009-10-29 08:12:44 +00003403 const RecordData &Record,
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003404 unsigned &Index) {
John McCall833ca992009-10-29 08:12:44 +00003405 switch (Kind) {
3406 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00003407 return ReadExpr(F);
John McCall833ca992009-10-29 08:12:44 +00003408 case TemplateArgument::Type:
Sebastian Redlc3632732010-10-05 15:59:54 +00003409 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor788cd062009-11-11 01:00:40 +00003410 case TemplateArgument::Template: {
Sebastian Redlc3632732010-10-05 15:59:54 +00003411 SourceRange QualifierRange = ReadSourceRange(F, Record, Index);
3412 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003413 return TemplateArgumentLocInfo(QualifierRange, TemplateNameLoc,
3414 SourceLocation());
3415 }
3416 case TemplateArgument::TemplateExpansion: {
3417 SourceRange QualifierRange = ReadSourceRange(F, Record, Index);
3418 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorba68eca2011-01-05 17:40:24 +00003419 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
3420 return TemplateArgumentLocInfo(QualifierRange, TemplateNameLoc,
3421 EllipsisLoc);
Douglas Gregor788cd062009-11-11 01:00:40 +00003422 }
John McCall833ca992009-10-29 08:12:44 +00003423 case TemplateArgument::Null:
3424 case TemplateArgument::Integral:
3425 case TemplateArgument::Declaration:
3426 case TemplateArgument::Pack:
3427 return TemplateArgumentLocInfo();
3428 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00003429 llvm_unreachable("unexpected template argument loc");
John McCall833ca992009-10-29 08:12:44 +00003430 return TemplateArgumentLocInfo();
3431}
3432
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003433TemplateArgumentLoc
Sebastian Redlc3632732010-10-05 15:59:54 +00003434ASTReader::ReadTemplateArgumentLoc(PerFileData &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00003435 const RecordData &Record, unsigned &Index) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003436 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003437
3438 if (Arg.getKind() == TemplateArgument::Expression) {
3439 if (Record[Index++]) // bool InfoHasSameExpr.
3440 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
3441 }
Sebastian Redlc3632732010-10-05 15:59:54 +00003442 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003443 Record, Index));
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003444}
3445
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003446Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall76bd1f32010-06-01 09:23:16 +00003447 return GetDecl(ID);
3448}
3449
Douglas Gregor7c789c12010-10-29 22:39:52 +00003450uint64_t
3451ASTReader::GetCXXBaseSpecifiersOffset(serialization::CXXBaseSpecifiersID ID) {
3452 if (ID == 0)
3453 return 0;
3454
3455 --ID;
3456 uint64_t Offset = 0;
3457 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3458 if (ID < Chain[I]->LocalNumCXXBaseSpecifiers)
3459 return Offset + Chain[I]->CXXBaseSpecifiersOffsets[ID];
3460
3461 ID -= Chain[I]->LocalNumCXXBaseSpecifiers;
3462 Offset += Chain[I]->SizeInBits;
3463 }
3464
3465 assert(false && "CXXBaseSpecifiers not found");
3466 return 0;
3467}
3468
3469CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
3470 // Figure out which AST file contains this offset.
3471 PerFileData *F = 0;
3472 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3473 if (Offset < Chain[I]->SizeInBits) {
3474 F = Chain[I];
3475 break;
3476 }
3477
3478 Offset -= Chain[I]->SizeInBits;
3479 }
3480
3481 if (!F) {
3482 Error("Malformed AST file: C++ base specifiers at impossible offset");
3483 return 0;
3484 }
3485
3486 llvm::BitstreamCursor &Cursor = F->DeclsCursor;
3487 SavedStreamPosition SavedPosition(Cursor);
3488 Cursor.JumpToBit(Offset);
3489 ReadingKindTracker ReadingKind(Read_Decl, *this);
3490 RecordData Record;
3491 unsigned Code = Cursor.ReadCode();
3492 unsigned RecCode = Cursor.ReadRecord(Code, Record);
3493 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
3494 Error("Malformed AST file: missing C++ base specifiers");
3495 return 0;
3496 }
3497
3498 unsigned Idx = 0;
3499 unsigned NumBases = Record[Idx++];
3500 void *Mem = Context->Allocate(sizeof(CXXBaseSpecifier) * NumBases);
3501 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
3502 for (unsigned I = 0; I != NumBases; ++I)
3503 Bases[I] = ReadCXXBaseSpecifier(*F, Record, Idx);
3504 return Bases;
3505}
3506
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003507TranslationUnitDecl *ASTReader::GetTranslationUnitDecl() {
Sebastian Redl30c514c2010-07-14 23:45:08 +00003508 if (!DeclsLoaded[0]) {
Sebastian Redle1dde812010-08-24 00:50:04 +00003509 ReadDeclRecord(0, 1);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003510 if (DeserializationListener)
Sebastian Redl1476ed42010-07-16 16:36:56 +00003511 DeserializationListener->DeclRead(1, DeclsLoaded[0]);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003512 }
Argyrios Kyrtzidis8871a442010-07-08 17:13:02 +00003513
3514 return cast<TranslationUnitDecl>(DeclsLoaded[0]);
3515}
3516
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003517Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003518 if (ID == 0)
3519 return 0;
3520
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003521 if (ID > DeclsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003522 Error("declaration ID out-of-range for AST file");
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003523 return 0;
3524 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003525
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003526 unsigned Index = ID - 1;
Sebastian Redl30c514c2010-07-14 23:45:08 +00003527 if (!DeclsLoaded[Index]) {
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00003528 ReadDeclRecord(Index, ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003529 if (DeserializationListener)
3530 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
3531 }
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003532
3533 return DeclsLoaded[Index];
Douglas Gregor2cf26342009-04-09 22:27:44 +00003534}
3535
Chris Lattner887e2b32009-04-27 05:46:25 +00003536/// \brief Resolve the offset of a statement into a statement.
3537///
3538/// This operation will read a new statement from the external
3539/// source each time it is called, and is meant to be used via a
3540/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003541Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00003542 // Switch case IDs are per Decl.
3543 ClearSwitchCaseIDs();
3544
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003545 // Offset here is a global offset across the entire chain.
3546 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3547 PerFileData &F = *Chain[N - I - 1];
3548 if (Offset < F.SizeInBits) {
3549 // Since we know that this statement is part of a decl, make sure to use
3550 // the decl cursor to read it.
3551 F.DeclsCursor.JumpToBit(Offset);
Sebastian Redlc3632732010-10-05 15:59:54 +00003552 return ReadStmtFromStream(F);
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003553 }
3554 Offset -= F.SizeInBits;
3555 }
3556 llvm_unreachable("Broken chain");
Douglas Gregor250fc9c2009-04-18 00:07:54 +00003557}
3558
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003559bool ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00003560 bool (*isKindWeWant)(Decl::Kind),
John McCall76bd1f32010-06-01 09:23:16 +00003561 llvm::SmallVectorImpl<Decl*> &Decls) {
Mike Stump1eb44332009-09-09 15:08:12 +00003562 assert(DC->hasExternalLexicalStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00003563 "DeclContext has no lexical decls in storage");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003564
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003565 // There might be lexical decls in multiple parts of the chain, for the TU
3566 // at least.
Sebastian Redl4a9eb262010-09-28 02:24:44 +00003567 // DeclContextOffsets might reallocate as we load additional decls below,
3568 // so make a copy of the vector.
3569 DeclContextInfos Infos = DeclContextOffsets[DC];
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003570 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3571 I != E; ++I) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003572 // IDs can be 0 if this context doesn't contain declarations.
3573 if (!I->LexicalDecls)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003574 continue;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003575
3576 // Load all of the declaration IDs
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00003577 for (const KindDeclIDPair *ID = I->LexicalDecls,
3578 *IDE = ID + I->NumLexicalDecls; ID != IDE; ++ID) {
3579 if (isKindWeWant && !isKindWeWant((Decl::Kind)ID->first))
3580 continue;
3581
3582 Decl *D = GetDecl(ID->second);
Sebastian Redl4a9eb262010-09-28 02:24:44 +00003583 assert(D && "Null decl in lexical decls");
3584 Decls.push_back(D);
3585 }
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003586 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003587
Douglas Gregor25123082009-04-22 22:34:57 +00003588 ++NumLexicalDeclContextsRead;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003589 return false;
3590}
3591
John McCall76bd1f32010-06-01 09:23:16 +00003592DeclContext::lookup_result
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003593ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +00003594 DeclarationName Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00003595 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00003596 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003597 if (!Name)
3598 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
3599 DeclContext::lookup_iterator(0));
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003600
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003601 llvm::SmallVector<NamedDecl *, 64> Decls;
Sebastian Redl8b122732010-08-24 00:49:55 +00003602 // There might be visible decls in multiple parts of the chain, for the TU
Sebastian Redl5967d622010-08-24 00:50:16 +00003603 // and namespaces. For any given name, the last available results replace
3604 // all earlier ones. For this reason, we walk in reverse.
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003605 DeclContextInfos &Infos = DeclContextOffsets[DC];
Sebastian Redl5967d622010-08-24 00:50:16 +00003606 for (DeclContextInfos::reverse_iterator I = Infos.rbegin(), E = Infos.rend();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003607 I != E; ++I) {
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003608 if (!I->NameLookupTableData)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003609 continue;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003610
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003611 ASTDeclContextNameLookupTable *LookupTable =
3612 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3613 ASTDeclContextNameLookupTable::iterator Pos = LookupTable->find(Name);
3614 if (Pos == LookupTable->end())
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003615 continue;
3616
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003617 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
3618 for (; Data.first != Data.second; ++Data.first)
3619 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
Sebastian Redl5967d622010-08-24 00:50:16 +00003620 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003621 }
3622
Douglas Gregor25123082009-04-22 22:34:57 +00003623 ++NumVisibleDeclContextsRead;
John McCall76bd1f32010-06-01 09:23:16 +00003624
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003625 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall76bd1f32010-06-01 09:23:16 +00003626 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003627}
3628
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00003629void ASTReader::MaterializeVisibleDecls(const DeclContext *DC) {
3630 assert(DC->hasExternalVisibleStorage() &&
3631 "DeclContext has no visible decls in storage");
3632
3633 llvm::SmallVector<NamedDecl *, 64> Decls;
3634 // There might be visible decls in multiple parts of the chain, for the TU
3635 // and namespaces.
3636 DeclContextInfos &Infos = DeclContextOffsets[DC];
3637 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3638 I != E; ++I) {
3639 if (!I->NameLookupTableData)
3640 continue;
3641
3642 ASTDeclContextNameLookupTable *LookupTable =
3643 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3644 for (ASTDeclContextNameLookupTable::item_iterator
3645 ItemI = LookupTable->item_begin(),
3646 ItemEnd = LookupTable->item_end() ; ItemI != ItemEnd; ++ItemI) {
3647 ASTDeclContextNameLookupTable::item_iterator::value_type Val
3648 = *ItemI;
3649 ASTDeclContextNameLookupTrait::data_type Data = Val.second;
3650 Decls.clear();
3651 for (; Data.first != Data.second; ++Data.first)
3652 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
3653 MaterializeVisibleDeclsForName(DC, Val.first, Decls);
3654 }
3655 }
3656}
3657
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003658void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003659 assert(Consumer);
3660 while (!InterestingDecls.empty()) {
3661 DeclGroupRef DG(InterestingDecls.front());
3662 InterestingDecls.pop_front();
Sebastian Redl27372b42010-08-11 18:52:41 +00003663 Consumer->HandleInterestingDecl(DG);
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003664 }
3665}
3666
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003667void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor0af2ca42009-04-22 19:09:20 +00003668 this->Consumer = Consumer;
3669
Douglas Gregorfdd01722009-04-14 00:24:19 +00003670 if (!Consumer)
3671 return;
3672
3673 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003674 // Force deserialization of this decl, which will cause it to be queued for
3675 // passing to the consumer.
Daniel Dunbar04a0b502009-09-17 03:06:44 +00003676 GetDecl(ExternalDefinitions[I]);
Douglas Gregorfdd01722009-04-14 00:24:19 +00003677 }
Douglas Gregorc62a2fe2009-04-25 00:41:30 +00003678
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003679 PassInterestingDeclsToConsumer();
Douglas Gregorfdd01722009-04-14 00:24:19 +00003680}
3681
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003682void ASTReader::PrintStats() {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003683 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregor2cf26342009-04-09 22:27:44 +00003684
Mike Stump1eb44332009-09-09 15:08:12 +00003685 unsigned NumTypesLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003686 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall0953e762009-09-24 19:53:00 +00003687 QualType());
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003688 unsigned NumDeclsLoaded
3689 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
3690 (Decl *)0);
3691 unsigned NumIdentifiersLoaded
3692 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
3693 IdentifiersLoaded.end(),
3694 (IdentifierInfo *)0);
Mike Stump1eb44332009-09-09 15:08:12 +00003695 unsigned NumSelectorsLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003696 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
3697 SelectorsLoaded.end(),
3698 Selector());
Douglas Gregor2d41cc12009-04-13 20:50:16 +00003699
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003700 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
3701 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00003702 if (TotalNumSLocEntries)
3703 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
3704 NumSLocEntriesRead, TotalNumSLocEntries,
3705 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003706 if (!TypesLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003707 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003708 NumTypesLoaded, (unsigned)TypesLoaded.size(),
3709 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
3710 if (!DeclsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003711 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003712 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
3713 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003714 if (!IdentifiersLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003715 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003716 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
3717 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redl725cd962010-08-04 20:40:17 +00003718 if (!SelectorsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003719 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redl725cd962010-08-04 20:40:17 +00003720 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
3721 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor83941df2009-04-25 17:48:32 +00003722 if (TotalNumStatements)
3723 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
3724 NumStatementsRead, TotalNumStatements,
3725 ((float)NumStatementsRead/TotalNumStatements * 100));
3726 if (TotalNumMacros)
3727 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
3728 NumMacrosRead, TotalNumMacros,
3729 ((float)NumMacrosRead/TotalNumMacros * 100));
3730 if (TotalLexicalDeclContexts)
3731 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
3732 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
3733 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
3734 * 100));
3735 if (TotalVisibleDeclContexts)
3736 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
3737 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
3738 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
3739 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003740 if (TotalNumMethodPoolEntries) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003741 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003742 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
3743 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor83941df2009-04-25 17:48:32 +00003744 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003745 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor83941df2009-04-25 17:48:32 +00003746 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003747 std::fprintf(stderr, "\n");
3748}
3749
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003750void ASTReader::InitializeSema(Sema &S) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00003751 SemaObj = &S;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003752 S.ExternalSource = this;
3753
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00003754 // Makes sure any declarations that were deserialized "too early"
3755 // still get added to the identifier's declaration chains.
Douglas Gregor76dc8892010-09-24 23:29:12 +00003756 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
3757 if (SemaObj->TUScope)
John McCalld226f652010-08-21 09:40:31 +00003758 SemaObj->TUScope->AddDecl(PreloadedDecls[I]);
Douglas Gregor76dc8892010-09-24 23:29:12 +00003759
3760 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregor668c1a42009-04-21 22:25:48 +00003761 }
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00003762 PreloadedDecls.clear();
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003763
3764 // If there were any tentative definitions, deserialize them and add
Sebastian Redle9d12b62010-01-31 22:27:38 +00003765 // them to Sema's list of tentative definitions.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003766 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
3767 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
Sebastian Redle9d12b62010-01-31 22:27:38 +00003768 SemaObj->TentativeDefinitions.push_back(Var);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003769 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00003770
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003771 // If there were any unused file scoped decls, deserialize them and add to
3772 // Sema's list of unused file scoped decls.
3773 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
3774 DeclaratorDecl *D = cast<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
3775 SemaObj->UnusedFileScopedDecls.push_back(D);
Tanya Lattnere6bbc012010-02-12 00:07:30 +00003776 }
Douglas Gregor14c22f22009-04-22 22:18:58 +00003777
3778 // If there were any locally-scoped external declarations,
3779 // deserialize them and add them to Sema's table of locally-scoped
3780 // external declarations.
3781 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
3782 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
3783 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
3784 }
Douglas Gregorb81c1702009-04-27 20:06:05 +00003785
3786 // If there were any ext_vector type declarations, deserialize them
3787 // and add them to Sema's vector of such declarations.
3788 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I)
3789 SemaObj->ExtVectorDecls.push_back(
3790 cast<TypedefDecl>(GetDecl(ExtVectorDecls[I])));
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003791
3792 // FIXME: Do VTable uses and dynamic classes deserialize too much ?
3793 // Can we cut them down before writing them ?
3794
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003795 // If there were any dynamic classes declarations, deserialize them
3796 // and add them to Sema's vector of such declarations.
3797 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I)
3798 SemaObj->DynamicClasses.push_back(
3799 cast<CXXRecordDecl>(GetDecl(DynamicClasses[I])));
Fariborz Jahanian32019832010-07-23 19:11:11 +00003800
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003801 // Load the offsets of the declarations that Sema references.
3802 // They will be lazily deserialized when needed.
3803 if (!SemaDeclRefs.empty()) {
3804 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
3805 SemaObj->StdNamespace = SemaDeclRefs[0];
3806 SemaObj->StdBadAlloc = SemaDeclRefs[1];
3807 }
3808
Sebastian Redlc3632732010-10-05 15:59:54 +00003809 for (PerFileData *F = FirstInSource; F; F = F->NextInSource) {
3810
3811 // If there are @selector references added them to its pool. This is for
3812 // implementation of -Wselector.
3813 if (!F->ReferencedSelectorsData.empty()) {
3814 unsigned int DataSize = F->ReferencedSelectorsData.size()-1;
3815 unsigned I = 0;
3816 while (I < DataSize) {
3817 Selector Sel = DecodeSelector(F->ReferencedSelectorsData[I++]);
3818 SourceLocation SelLoc = ReadSourceLocation(
3819 *F, F->ReferencedSelectorsData, I);
3820 SemaObj->ReferencedSelectors.insert(std::make_pair(Sel, SelLoc));
3821 }
3822 }
3823
3824 // If there were any pending implicit instantiations, deserialize them
3825 // and add them to Sema's queue of such instantiations.
3826 assert(F->PendingInstantiations.size() % 2 == 0 &&
3827 "Expected pairs of entries");
3828 for (unsigned Idx = 0, N = F->PendingInstantiations.size(); Idx < N;) {
3829 ValueDecl *D=cast<ValueDecl>(GetDecl(F->PendingInstantiations[Idx++]));
3830 SourceLocation Loc = ReadSourceLocation(*F, F->PendingInstantiations,Idx);
3831 SemaObj->PendingInstantiations.push_back(std::make_pair(D, Loc));
3832 }
3833 }
3834
3835 // The two special data sets below always come from the most recent PCH,
3836 // which is at the front of the chain.
3837 PerFileData &F = *Chain.front();
3838
3839 // If there were any weak undeclared identifiers, deserialize them and add to
3840 // Sema's list of weak undeclared identifiers.
3841 if (!WeakUndeclaredIdentifiers.empty()) {
3842 unsigned Idx = 0;
3843 for (unsigned I = 0, N = WeakUndeclaredIdentifiers[Idx++]; I != N; ++I) {
3844 IdentifierInfo *WeakId = GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3845 IdentifierInfo *AliasId= GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3846 SourceLocation Loc = ReadSourceLocation(F, WeakUndeclaredIdentifiers,Idx);
3847 bool Used = WeakUndeclaredIdentifiers[Idx++];
3848 Sema::WeakInfo WI(AliasId, Loc);
3849 WI.setUsed(Used);
3850 SemaObj->WeakUndeclaredIdentifiers.insert(std::make_pair(WeakId, WI));
3851 }
3852 }
3853
3854 // If there were any VTable uses, deserialize the information and add it
3855 // to Sema's vector and map of VTable uses.
3856 if (!VTableUses.empty()) {
3857 unsigned Idx = 0;
3858 for (unsigned I = 0, N = VTableUses[Idx++]; I != N; ++I) {
3859 CXXRecordDecl *Class = cast<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
3860 SourceLocation Loc = ReadSourceLocation(F, VTableUses, Idx);
3861 bool DefinitionRequired = VTableUses[Idx++];
3862 SemaObj->VTableUses.push_back(std::make_pair(Class, Loc));
3863 SemaObj->VTablesUsed[Class] = DefinitionRequired;
Fariborz Jahanian32019832010-07-23 19:11:11 +00003864 }
3865 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00003866}
3867
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003868IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Sebastian Redld8c5abb2010-08-02 18:30:12 +00003869 // Try to find this name within our on-disk hash tables. We start with the
3870 // most recent one, since that one contains the most up-to-date info.
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003871 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003872 ASTIdentifierLookupTable *IdTable
3873 = (ASTIdentifierLookupTable *)Chain[I]->IdentifierLookupTable;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003874 if (!IdTable)
3875 continue;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003876 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003877 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003878 if (Pos == IdTable->end())
3879 continue;
Douglas Gregor668c1a42009-04-21 22:25:48 +00003880
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003881 // Dereferencing the iterator has the effect of building the
3882 // IdentifierInfo node and populating it with the various
3883 // declarations it needs.
Sebastian Redld8c5abb2010-08-02 18:30:12 +00003884 return *Pos;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003885 }
Sebastian Redld8c5abb2010-08-02 18:30:12 +00003886 return 0;
Douglas Gregor668c1a42009-04-21 22:25:48 +00003887}
3888
Douglas Gregor95f42922010-10-14 22:11:03 +00003889namespace clang {
3890 /// \brief An identifier-lookup iterator that enumerates all of the
3891 /// identifiers stored within a set of AST files.
3892 class ASTIdentifierIterator : public IdentifierIterator {
3893 /// \brief The AST reader whose identifiers are being enumerated.
3894 const ASTReader &Reader;
3895
3896 /// \brief The current index into the chain of AST files stored in
3897 /// the AST reader.
3898 unsigned Index;
3899
3900 /// \brief The current position within the identifier lookup table
3901 /// of the current AST file.
3902 ASTIdentifierLookupTable::key_iterator Current;
3903
3904 /// \brief The end position within the identifier lookup table of
3905 /// the current AST file.
3906 ASTIdentifierLookupTable::key_iterator End;
3907
3908 public:
3909 explicit ASTIdentifierIterator(const ASTReader &Reader);
3910
3911 virtual llvm::StringRef Next();
3912 };
3913}
3914
3915ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
3916 : Reader(Reader), Index(Reader.Chain.size() - 1) {
3917 ASTIdentifierLookupTable *IdTable
3918 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
3919 Current = IdTable->key_begin();
3920 End = IdTable->key_end();
3921}
3922
3923llvm::StringRef ASTIdentifierIterator::Next() {
3924 while (Current == End) {
3925 // If we have exhausted all of our AST files, we're done.
3926 if (Index == 0)
3927 return llvm::StringRef();
3928
3929 --Index;
3930 ASTIdentifierLookupTable *IdTable
3931 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
3932 Current = IdTable->key_begin();
3933 End = IdTable->key_end();
3934 }
3935
3936 // We have any identifiers remaining in the current AST file; return
3937 // the next one.
3938 std::pair<const char*, unsigned> Key = *Current;
3939 ++Current;
3940 return llvm::StringRef(Key.first, Key.second);
3941}
3942
3943IdentifierIterator *ASTReader::getIdentifiers() const {
3944 return new ASTIdentifierIterator(*this);
3945}
3946
Mike Stump1eb44332009-09-09 15:08:12 +00003947std::pair<ObjCMethodList, ObjCMethodList>
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003948ASTReader::ReadMethodPool(Selector Sel) {
Sebastian Redl725cd962010-08-04 20:40:17 +00003949 // Find this selector in a hash table. We want to find the most recent entry.
3950 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3951 PerFileData &F = *Chain[I];
3952 if (!F.SelectorLookupTable)
3953 continue;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003954
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003955 ASTSelectorLookupTable *PoolTable
3956 = (ASTSelectorLookupTable*)F.SelectorLookupTable;
3957 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
Sebastian Redl725cd962010-08-04 20:40:17 +00003958 if (Pos != PoolTable->end()) {
3959 ++NumSelectorsRead;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003960 // FIXME: Not quite happy with the statistics here. We probably should
3961 // disable this tracking when called via LoadSelector.
3962 // Also, should entries without methods count as misses?
3963 ++NumMethodPoolEntriesRead;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003964 ASTSelectorLookupTrait::data_type Data = *Pos;
Sebastian Redl725cd962010-08-04 20:40:17 +00003965 if (DeserializationListener)
3966 DeserializationListener->SelectorRead(Data.ID, Sel);
3967 return std::make_pair(Data.Instance, Data.Factory);
3968 }
Douglas Gregor83941df2009-04-25 17:48:32 +00003969 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003970
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003971 ++NumMethodPoolMisses;
Sebastian Redl725cd962010-08-04 20:40:17 +00003972 return std::pair<ObjCMethodList, ObjCMethodList>();
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003973}
3974
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003975void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redle58aa892010-08-04 18:21:41 +00003976 // It would be complicated to avoid reading the methods anyway. So don't.
3977 ReadMethodPool(Sel);
3978}
3979
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003980void ASTReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00003981 assert(ID && "Non-zero identifier ID required");
Douglas Gregora02b1472009-04-28 21:53:25 +00003982 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003983 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003984 if (DeserializationListener)
3985 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregor668c1a42009-04-21 22:25:48 +00003986}
3987
Douglas Gregord89275b2009-07-06 18:54:52 +00003988/// \brief Set the globally-visible declarations associated with the given
3989/// identifier.
3990///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003991/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump1eb44332009-09-09 15:08:12 +00003992/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregord89275b2009-07-06 18:54:52 +00003993/// them.
3994///
3995/// \param II an IdentifierInfo that refers to one or more globally-visible
3996/// declarations.
3997///
3998/// \param DeclIDs the set of declaration IDs with the name @p II that are
3999/// visible at global scope.
4000///
4001/// \param Nonrecursive should be true to indicate that the caller knows that
4002/// this call is non-recursive, and therefore the globally-visible declarations
4003/// will not be placed onto the pending queue.
Mike Stump1eb44332009-09-09 15:08:12 +00004004void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004005ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Douglas Gregord89275b2009-07-06 18:54:52 +00004006 const llvm::SmallVectorImpl<uint32_t> &DeclIDs,
4007 bool Nonrecursive) {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004008 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregord89275b2009-07-06 18:54:52 +00004009 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
4010 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
4011 PII.II = II;
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00004012 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregord89275b2009-07-06 18:54:52 +00004013 return;
4014 }
Mike Stump1eb44332009-09-09 15:08:12 +00004015
Douglas Gregord89275b2009-07-06 18:54:52 +00004016 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
4017 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
4018 if (SemaObj) {
Douglas Gregor914ed9d2010-08-13 03:15:25 +00004019 if (SemaObj->TUScope) {
4020 // Introduce this declaration into the translation-unit scope
4021 // and add it to the declaration chain for this identifier, so
4022 // that (unqualified) name lookup will find it.
John McCalld226f652010-08-21 09:40:31 +00004023 SemaObj->TUScope->AddDecl(D);
Douglas Gregor914ed9d2010-08-13 03:15:25 +00004024 }
Douglas Gregor76dc8892010-09-24 23:29:12 +00004025 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
Douglas Gregord89275b2009-07-06 18:54:52 +00004026 } else {
4027 // Queue this declaration so that it will be added to the
4028 // translation unit scope and identifier's declaration chain
4029 // once a Sema object is known.
4030 PreloadedDecls.push_back(D);
4031 }
4032 }
4033}
4034
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004035IdentifierInfo *ASTReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00004036 if (ID == 0)
4037 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00004038
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004039 if (IdentifiersLoaded.empty()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004040 Error("no identifier table in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00004041 return 0;
4042 }
Mike Stump1eb44332009-09-09 15:08:12 +00004043
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00004044 assert(PP && "Forgot to set Preprocessor ?");
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004045 ID -= 1;
4046 if (!IdentifiersLoaded[ID]) {
4047 unsigned Index = ID;
4048 const char *Str = 0;
4049 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4050 PerFileData *F = Chain[N - I - 1];
4051 if (Index < F->LocalNumIdentifiers) {
4052 uint32_t Offset = F->IdentifierOffsets[Index];
4053 Str = F->IdentifierTableData + Offset;
4054 break;
4055 }
4056 Index -= F->LocalNumIdentifiers;
4057 }
4058 assert(Str && "Broken Chain");
Douglas Gregord6595a42009-04-25 21:04:17 +00004059
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004060 // All of the strings in the AST file are preceded by a 16-bit length.
4061 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenek231bc0b2009-10-23 04:45:31 +00004062 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
4063 // unsigned integers. This is important to avoid integer overflow when
4064 // we cast them to 'unsigned'.
Ted Kremenekff1ea462009-10-23 03:57:22 +00004065 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregor02fc7512009-04-28 20:01:51 +00004066 unsigned StrLen = (((unsigned) StrLenPtr[0])
4067 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004068 IdentifiersLoaded[ID]
Kovarththanan Rajaratnam811f4262010-03-12 10:32:27 +00004069 = &PP->getIdentifierTable().get(Str, StrLen);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004070 if (DeserializationListener)
4071 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregorafaf3082009-04-11 00:14:32 +00004072 }
Mike Stump1eb44332009-09-09 15:08:12 +00004073
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004074 return IdentifiersLoaded[ID];
Douglas Gregor2cf26342009-04-09 22:27:44 +00004075}
4076
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004077void ASTReader::ReadSLocEntry(unsigned ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00004078 ReadSLocEntryRecord(ID);
4079}
4080
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004081Selector ASTReader::DecodeSelector(unsigned ID) {
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004082 if (ID == 0)
4083 return Selector();
Mike Stump1eb44332009-09-09 15:08:12 +00004084
Sebastian Redl725cd962010-08-04 20:40:17 +00004085 if (ID > SelectorsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004086 Error("selector ID out of range in AST file");
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004087 return Selector();
4088 }
Douglas Gregor83941df2009-04-25 17:48:32 +00004089
Sebastian Redl725cd962010-08-04 20:40:17 +00004090 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor83941df2009-04-25 17:48:32 +00004091 // Load this selector from the selector table.
Sebastian Redl725cd962010-08-04 20:40:17 +00004092 unsigned Idx = ID - 1;
4093 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4094 PerFileData &F = *Chain[N - I - 1];
4095 if (Idx < F.LocalNumSelectors) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004096 ASTSelectorLookupTrait Trait(*this);
Sebastian Redl725cd962010-08-04 20:40:17 +00004097 SelectorsLoaded[ID - 1] =
4098 Trait.ReadKey(F.SelectorLookupTableData + F.SelectorOffsets[Idx], 0);
4099 if (DeserializationListener)
4100 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
4101 break;
4102 }
4103 Idx -= F.LocalNumSelectors;
4104 }
Douglas Gregor83941df2009-04-25 17:48:32 +00004105 }
4106
Sebastian Redl725cd962010-08-04 20:40:17 +00004107 return SelectorsLoaded[ID - 1];
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004108}
4109
Michael J. Spencer20249a12010-10-21 03:16:25 +00004110Selector ASTReader::GetExternalSelector(uint32_t ID) {
Douglas Gregor719770d2010-04-06 17:30:22 +00004111 return DecodeSelector(ID);
4112}
4113
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004114uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redl725cd962010-08-04 20:40:17 +00004115 // ID 0 (the null selector) is considered an external selector.
4116 return getTotalNumSelectors() + 1;
Douglas Gregor719770d2010-04-06 17:30:22 +00004117}
4118
Mike Stump1eb44332009-09-09 15:08:12 +00004119DeclarationName
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004120ASTReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004121 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
4122 switch (Kind) {
4123 case DeclarationName::Identifier:
4124 return DeclarationName(GetIdentifierInfo(Record, Idx));
4125
4126 case DeclarationName::ObjCZeroArgSelector:
4127 case DeclarationName::ObjCOneArgSelector:
4128 case DeclarationName::ObjCMultiArgSelector:
Steve Naroffa7503a72009-04-23 15:15:40 +00004129 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004130
4131 case DeclarationName::CXXConstructorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004132 return Context->DeclarationNames.getCXXConstructorName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00004133 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004134
4135 case DeclarationName::CXXDestructorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004136 return Context->DeclarationNames.getCXXDestructorName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00004137 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004138
4139 case DeclarationName::CXXConversionFunctionName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004140 return Context->DeclarationNames.getCXXConversionFunctionName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00004141 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004142
4143 case DeclarationName::CXXOperatorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004144 return Context->DeclarationNames.getCXXOperatorName(
Douglas Gregor2cf26342009-04-09 22:27:44 +00004145 (OverloadedOperatorKind)Record[Idx++]);
4146
Sean Hunt3e518bd2009-11-29 07:34:05 +00004147 case DeclarationName::CXXLiteralOperatorName:
4148 return Context->DeclarationNames.getCXXLiteralOperatorName(
4149 GetIdentifierInfo(Record, Idx));
4150
Douglas Gregor2cf26342009-04-09 22:27:44 +00004151 case DeclarationName::CXXUsingDirective:
4152 return DeclarationName::getUsingDirectiveName();
4153 }
4154
4155 // Required to silence GCC warning
4156 return DeclarationName();
4157}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00004158
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004159void ASTReader::ReadDeclarationNameLoc(PerFileData &F,
4160 DeclarationNameLoc &DNLoc,
4161 DeclarationName Name,
4162 const RecordData &Record, unsigned &Idx) {
4163 switch (Name.getNameKind()) {
4164 case DeclarationName::CXXConstructorName:
4165 case DeclarationName::CXXDestructorName:
4166 case DeclarationName::CXXConversionFunctionName:
4167 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
4168 break;
4169
4170 case DeclarationName::CXXOperatorName:
4171 DNLoc.CXXOperatorName.BeginOpNameLoc
4172 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4173 DNLoc.CXXOperatorName.EndOpNameLoc
4174 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4175 break;
4176
4177 case DeclarationName::CXXLiteralOperatorName:
4178 DNLoc.CXXLiteralOperatorName.OpNameLoc
4179 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4180 break;
4181
4182 case DeclarationName::Identifier:
4183 case DeclarationName::ObjCZeroArgSelector:
4184 case DeclarationName::ObjCOneArgSelector:
4185 case DeclarationName::ObjCMultiArgSelector:
4186 case DeclarationName::CXXUsingDirective:
4187 break;
4188 }
4189}
4190
4191void ASTReader::ReadDeclarationNameInfo(PerFileData &F,
4192 DeclarationNameInfo &NameInfo,
4193 const RecordData &Record, unsigned &Idx) {
4194 NameInfo.setName(ReadDeclarationName(Record, Idx));
4195 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
4196 DeclarationNameLoc DNLoc;
4197 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
4198 NameInfo.setInfo(DNLoc);
4199}
4200
4201void ASTReader::ReadQualifierInfo(PerFileData &F, QualifierInfo &Info,
4202 const RecordData &Record, unsigned &Idx) {
4203 Info.NNS = ReadNestedNameSpecifier(Record, Idx);
4204 Info.NNSRange = ReadSourceRange(F, Record, Idx);
4205 unsigned NumTPLists = Record[Idx++];
4206 Info.NumTemplParamLists = NumTPLists;
4207 if (NumTPLists) {
4208 Info.TemplParamLists = new (*Context) TemplateParameterList*[NumTPLists];
4209 for (unsigned i=0; i != NumTPLists; ++i)
4210 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
4211 }
4212}
4213
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004214TemplateName
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004215ASTReader::ReadTemplateName(const RecordData &Record, unsigned &Idx) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00004216 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004217 switch (Kind) {
4218 case TemplateName::Template:
4219 return TemplateName(cast_or_null<TemplateDecl>(GetDecl(Record[Idx++])));
4220
4221 case TemplateName::OverloadedTemplate: {
4222 unsigned size = Record[Idx++];
4223 UnresolvedSet<8> Decls;
4224 while (size--)
4225 Decls.addDecl(cast<NamedDecl>(GetDecl(Record[Idx++])));
4226
4227 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
4228 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004229
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004230 case TemplateName::QualifiedTemplate: {
4231 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4232 bool hasTemplKeyword = Record[Idx++];
4233 TemplateDecl *Template = cast<TemplateDecl>(GetDecl(Record[Idx++]));
4234 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
4235 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004236
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004237 case TemplateName::DependentTemplate: {
4238 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4239 if (Record[Idx++]) // isIdentifier
4240 return Context->getDependentTemplateName(NNS,
4241 GetIdentifierInfo(Record, Idx));
4242 return Context->getDependentTemplateName(NNS,
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004243 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004244 }
4245 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004246
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004247 assert(0 && "Unhandled template name kind!");
4248 return TemplateName();
4249}
4250
4251TemplateArgument
Sebastian Redlc3632732010-10-05 15:59:54 +00004252ASTReader::ReadTemplateArgument(PerFileData &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00004253 const RecordData &Record, unsigned &Idx) {
Douglas Gregora7fc9012011-01-05 18:58:31 +00004254 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
4255 switch (Kind) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004256 case TemplateArgument::Null:
4257 return TemplateArgument();
4258 case TemplateArgument::Type:
4259 return TemplateArgument(GetType(Record[Idx++]));
4260 case TemplateArgument::Declaration:
4261 return TemplateArgument(GetDecl(Record[Idx++]));
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00004262 case TemplateArgument::Integral: {
4263 llvm::APSInt Value = ReadAPSInt(Record, Idx);
4264 QualType T = GetType(Record[Idx++]);
4265 return TemplateArgument(Value, T);
4266 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00004267 case TemplateArgument::Template:
4268 case TemplateArgument::TemplateExpansion: {
Douglas Gregorba68eca2011-01-05 17:40:24 +00004269 TemplateName Name = ReadTemplateName(Record, Idx);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004270 return TemplateArgument(Name, Kind == TemplateArgument::TemplateExpansion);
Douglas Gregorba68eca2011-01-05 17:40:24 +00004271 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004272 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00004273 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004274 case TemplateArgument::Pack: {
4275 unsigned NumArgs = Record[Idx++];
Douglas Gregor910f8002010-11-07 23:05:16 +00004276 TemplateArgument *Args = new (*Context) TemplateArgument[NumArgs];
4277 for (unsigned I = 0; I != NumArgs; ++I)
4278 Args[I] = ReadTemplateArgument(F, Record, Idx);
4279 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004280 }
4281 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004282
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004283 assert(0 && "Unhandled template argument kind!");
4284 return TemplateArgument();
4285}
4286
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004287TemplateParameterList *
Sebastian Redlc3632732010-10-05 15:59:54 +00004288ASTReader::ReadTemplateParameterList(PerFileData &F,
4289 const RecordData &Record, unsigned &Idx) {
4290 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
4291 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
4292 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004293
4294 unsigned NumParams = Record[Idx++];
4295 llvm::SmallVector<NamedDecl *, 16> Params;
4296 Params.reserve(NumParams);
4297 while (NumParams--)
4298 Params.push_back(cast<NamedDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer20249a12010-10-21 03:16:25 +00004299
4300 TemplateParameterList* TemplateParams =
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004301 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
4302 Params.data(), Params.size(), RAngleLoc);
4303 return TemplateParams;
4304}
4305
4306void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004307ASTReader::
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004308ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs,
Sebastian Redlc3632732010-10-05 15:59:54 +00004309 PerFileData &F, const RecordData &Record,
4310 unsigned &Idx) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004311 unsigned NumTemplateArgs = Record[Idx++];
4312 TemplArgs.reserve(NumTemplateArgs);
4313 while (NumTemplateArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00004314 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004315}
4316
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004317/// \brief Read a UnresolvedSet structure.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004318void ASTReader::ReadUnresolvedSet(UnresolvedSetImpl &Set,
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004319 const RecordData &Record, unsigned &Idx) {
4320 unsigned NumDecls = Record[Idx++];
4321 while (NumDecls--) {
4322 NamedDecl *D = cast<NamedDecl>(GetDecl(Record[Idx++]));
4323 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
4324 Set.addDecl(D, AS);
4325 }
4326}
4327
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004328CXXBaseSpecifier
Sebastian Redlc3632732010-10-05 15:59:54 +00004329ASTReader::ReadCXXBaseSpecifier(PerFileData &F,
Nick Lewycky56062202010-07-26 16:56:01 +00004330 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004331 bool isVirtual = static_cast<bool>(Record[Idx++]);
4332 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
4333 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00004334 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
4335 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00004336 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
4337 return CXXBaseSpecifier(Range, isVirtual, isBaseOfClass, AS, TInfo,
4338 EllipsisLoc);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004339}
4340
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004341std::pair<CXXBaseOrMemberInitializer **, unsigned>
Sebastian Redlc3632732010-10-05 15:59:54 +00004342ASTReader::ReadCXXBaseOrMemberInitializers(PerFileData &F,
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004343 const RecordData &Record,
4344 unsigned &Idx) {
4345 CXXBaseOrMemberInitializer **BaseOrMemberInitializers = 0;
4346 unsigned NumInitializers = Record[Idx++];
4347 if (NumInitializers) {
4348 ASTContext &C = *getContext();
4349
4350 BaseOrMemberInitializers
4351 = new (C) CXXBaseOrMemberInitializer*[NumInitializers];
4352 for (unsigned i=0; i != NumInitializers; ++i) {
4353 TypeSourceInfo *BaseClassInfo = 0;
4354 bool IsBaseVirtual = false;
4355 FieldDecl *Member = 0;
Francois Pichet00eb3f92010-12-04 09:14:42 +00004356 IndirectFieldDecl *IndirectMember = 0;
Michael J. Spencer20249a12010-10-21 03:16:25 +00004357
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004358 bool IsBaseInitializer = Record[Idx++];
4359 if (IsBaseInitializer) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004360 BaseClassInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004361 IsBaseVirtual = Record[Idx++];
4362 } else {
Francois Pichet00eb3f92010-12-04 09:14:42 +00004363 bool IsIndirectMemberInitializer = Record[Idx++];
4364 if (IsIndirectMemberInitializer)
4365 IndirectMember = cast<IndirectFieldDecl>(GetDecl(Record[Idx++]));
4366 else
4367 Member = cast<FieldDecl>(GetDecl(Record[Idx++]));
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004368 }
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00004369 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlc3632732010-10-05 15:59:54 +00004370 Expr *Init = ReadExpr(F);
Sebastian Redlc3632732010-10-05 15:59:54 +00004371 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
4372 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004373 bool IsWritten = Record[Idx++];
4374 unsigned SourceOrderOrNumArrayIndices;
4375 llvm::SmallVector<VarDecl *, 8> Indices;
4376 if (IsWritten) {
4377 SourceOrderOrNumArrayIndices = Record[Idx++];
4378 } else {
4379 SourceOrderOrNumArrayIndices = Record[Idx++];
4380 Indices.reserve(SourceOrderOrNumArrayIndices);
4381 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
4382 Indices.push_back(cast<VarDecl>(GetDecl(Record[Idx++])));
4383 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004384
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004385 CXXBaseOrMemberInitializer *BOMInit;
4386 if (IsBaseInitializer) {
4387 BOMInit = new (C) CXXBaseOrMemberInitializer(C, BaseClassInfo,
4388 IsBaseVirtual, LParenLoc,
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00004389 Init, RParenLoc,
4390 MemberOrEllipsisLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004391 } else if (IsWritten) {
Francois Pichet00eb3f92010-12-04 09:14:42 +00004392 if (Member)
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00004393 BOMInit = new (C) CXXBaseOrMemberInitializer(C, Member,
4394 MemberOrEllipsisLoc,
Francois Pichet00eb3f92010-12-04 09:14:42 +00004395 LParenLoc, Init,
4396 RParenLoc);
4397 else
4398 BOMInit = new (C) CXXBaseOrMemberInitializer(C, IndirectMember,
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00004399 MemberOrEllipsisLoc,
4400 LParenLoc,
Francois Pichet00eb3f92010-12-04 09:14:42 +00004401 Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004402 } else {
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00004403 BOMInit = CXXBaseOrMemberInitializer::Create(C, Member,
4404 MemberOrEllipsisLoc,
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004405 LParenLoc, Init, RParenLoc,
4406 Indices.data(),
4407 Indices.size());
4408 }
4409
Argyrios Kyrtzidisf84cde12010-09-06 19:04:27 +00004410 if (IsWritten)
4411 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004412 BaseOrMemberInitializers[i] = BOMInit;
4413 }
4414 }
4415
4416 return std::make_pair(BaseOrMemberInitializers, NumInitializers);
4417}
4418
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004419NestedNameSpecifier *
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004420ASTReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004421 unsigned N = Record[Idx++];
4422 NestedNameSpecifier *NNS = 0, *Prev = 0;
4423 for (unsigned I = 0; I != N; ++I) {
4424 NestedNameSpecifier::SpecifierKind Kind
4425 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
4426 switch (Kind) {
4427 case NestedNameSpecifier::Identifier: {
4428 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
4429 NNS = NestedNameSpecifier::Create(*Context, Prev, II);
4430 break;
4431 }
4432
4433 case NestedNameSpecifier::Namespace: {
4434 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
4435 NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
4436 break;
4437 }
4438
4439 case NestedNameSpecifier::TypeSpec:
4440 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregor1ab55e92010-12-10 17:03:06 +00004441 Type *T = GetType(Record[Idx++]).getTypePtrOrNull();
4442 if (!T)
4443 return 0;
4444
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004445 bool Template = Record[Idx++];
4446 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
4447 break;
4448 }
4449
4450 case NestedNameSpecifier::Global: {
4451 NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
4452 // No associated value, and there can't be a prefix.
4453 break;
4454 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004455 }
Argyrios Kyrtzidisd2bb2c02010-07-07 15:46:30 +00004456 Prev = NNS;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004457 }
4458 return NNS;
4459}
4460
4461SourceRange
Sebastian Redlc3632732010-10-05 15:59:54 +00004462ASTReader::ReadSourceRange(PerFileData &F, const RecordData &Record,
4463 unsigned &Idx) {
4464 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
4465 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar8ee59392010-06-02 15:47:10 +00004466 return SourceRange(beg, end);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004467}
4468
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004469/// \brief Read an integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004470llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004471 unsigned BitWidth = Record[Idx++];
4472 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
4473 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
4474 Idx += NumWords;
4475 return Result;
4476}
4477
4478/// \brief Read a signed integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004479llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004480 bool isUnsigned = Record[Idx++];
4481 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
4482}
4483
Douglas Gregor17fc2232009-04-14 21:55:33 +00004484/// \brief Read a floating-point value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004485llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00004486 return llvm::APFloat(ReadAPInt(Record, Idx));
4487}
4488
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004489// \brief Read a string
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004490std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004491 unsigned Len = Record[Idx++];
Jay Foadbeaaccd2009-05-21 09:52:38 +00004492 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004493 Idx += Len;
4494 return Result;
4495}
4496
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004497CXXTemporary *ASTReader::ReadCXXTemporary(const RecordData &Record,
Chris Lattnerd2598362010-05-10 00:25:06 +00004498 unsigned &Idx) {
4499 CXXDestructorDecl *Decl = cast<CXXDestructorDecl>(GetDecl(Record[Idx++]));
4500 return CXXTemporary::Create(*Context, Decl);
4501}
4502
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004503DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00004504 return Diag(SourceLocation(), DiagID);
4505}
4506
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004507DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004508 return Diags.Report(Loc, DiagID);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00004509}
Douglas Gregor025452f2009-04-17 00:04:06 +00004510
Douglas Gregor668c1a42009-04-21 22:25:48 +00004511/// \brief Retrieve the identifier table associated with the
4512/// preprocessor.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004513IdentifierTable &ASTReader::getIdentifierTable() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00004514 assert(PP && "Forgot to set Preprocessor ?");
4515 return PP->getIdentifierTable();
Douglas Gregor668c1a42009-04-21 22:25:48 +00004516}
4517
Douglas Gregor025452f2009-04-17 00:04:06 +00004518/// \brief Record that the given ID maps to the given switch-case
4519/// statement.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004520void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Douglas Gregor025452f2009-04-17 00:04:06 +00004521 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
4522 SwitchCaseStmts[ID] = SC;
4523}
4524
4525/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004526SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Douglas Gregor025452f2009-04-17 00:04:06 +00004527 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
4528 return SwitchCaseStmts[ID];
4529}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00004530
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00004531void ASTReader::ClearSwitchCaseIDs() {
4532 SwitchCaseStmts.clear();
4533}
4534
Douglas Gregor1de05fe2009-04-17 18:18:49 +00004535/// \brief Record that the given label statement has been
4536/// deserialized and has the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004537void ASTReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
Mike Stump1eb44332009-09-09 15:08:12 +00004538 assert(LabelStmts.find(ID) == LabelStmts.end() &&
Douglas Gregor1de05fe2009-04-17 18:18:49 +00004539 "Deserialized label twice");
4540 LabelStmts[ID] = S;
4541
4542 // If we've already seen any goto statements that point to this
4543 // label, resolve them now.
4544 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
4545 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
4546 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
4547 Goto->second->setLabel(S);
4548 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00004549
4550 // If we've already seen any address-label statements that point to
4551 // this label, resolve them now.
4552 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
Mike Stump1eb44332009-09-09 15:08:12 +00004553 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00004554 = UnresolvedAddrLabelExprs.equal_range(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00004555 for (AddrLabelIter AddrLabel = AddrLabels.first;
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00004556 AddrLabel != AddrLabels.second; ++AddrLabel)
4557 AddrLabel->second->setLabel(S);
4558 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor1de05fe2009-04-17 18:18:49 +00004559}
4560
4561/// \brief Set the label of the given statement to the label
4562/// identified by ID.
4563///
4564/// Depending on the order in which the label and other statements
4565/// referencing that label occur, this operation may complete
4566/// immediately (updating the statement) or it may queue the
4567/// statement to be back-patched later.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004568void ASTReader::SetLabelOf(GotoStmt *S, unsigned ID) {
Douglas Gregor1de05fe2009-04-17 18:18:49 +00004569 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
4570 if (Label != LabelStmts.end()) {
4571 // We've already seen this label, so set the label of the goto and
4572 // we're done.
4573 S->setLabel(Label->second);
4574 } else {
4575 // We haven't seen this label yet, so add this goto to the set of
4576 // unresolved goto statements.
4577 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
4578 }
4579}
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00004580
4581/// \brief Set the label of the given expression to the label
4582/// identified by ID.
4583///
4584/// Depending on the order in which the label and other statements
4585/// referencing that label occur, this operation may complete
4586/// immediately (updating the statement) or it may queue the
4587/// statement to be back-patched later.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004588void ASTReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00004589 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
4590 if (Label != LabelStmts.end()) {
4591 // We've already seen this label, so set the label of the
4592 // label-address expression and we're done.
4593 S->setLabel(Label->second);
4594 } else {
4595 // We haven't seen this label yet, so add this label-address
4596 // expression to the set of unresolved label-address expressions.
4597 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
4598 }
4599}
Douglas Gregord89275b2009-07-06 18:54:52 +00004600
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004601void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004602 assert(NumCurrentElementsDeserializing &&
4603 "FinishedDeserializing not paired with StartedDeserializing");
4604 if (NumCurrentElementsDeserializing == 1) {
Douglas Gregord89275b2009-07-06 18:54:52 +00004605 // If any identifiers with corresponding top-level declarations have
4606 // been loaded, load those declarations now.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004607 while (!PendingIdentifierInfos.empty()) {
4608 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
4609 PendingIdentifierInfos.front().DeclIDs, true);
4610 PendingIdentifierInfos.pop_front();
Douglas Gregord89275b2009-07-06 18:54:52 +00004611 }
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00004612
4613 // We are not in recursive loading, so it's safe to pass the "interesting"
4614 // decls to the consumer.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004615 if (Consumer)
4616 PassInterestingDeclsToConsumer();
Argyrios Kyrtzidis134db1f2010-10-24 17:26:31 +00004617
4618 assert(PendingForwardRefs.size() == 0 &&
4619 "Some forward refs did not get linked to the definition!");
Douglas Gregord89275b2009-07-06 18:54:52 +00004620 }
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004621 --NumCurrentElementsDeserializing;
Douglas Gregord89275b2009-07-06 18:54:52 +00004622}
Douglas Gregor501c1032010-08-19 00:28:17 +00004623
Sebastian Redle1dde812010-08-24 00:50:04 +00004624ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
4625 const char *isysroot, bool DisableValidation)
4626 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
4627 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
4628 Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
4629 Consumer(0), isysroot(isysroot), DisableValidation(DisableValidation),
4630 NumStatHits(0), NumStatMisses(0), NumSLocEntriesRead(0),
Sebastian Redl8db9fae2010-09-22 20:19:08 +00004631 TotalNumSLocEntries(0), NextSLocOffset(0), NumStatementsRead(0),
4632 TotalNumStatements(0), NumMacrosRead(0), TotalNumMacros(0),
4633 NumSelectorsRead(0), NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
Sebastian Redle1dde812010-08-24 00:50:04 +00004634 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
4635 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
4636 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
4637 RelocatablePCH = false;
4638}
4639
4640ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
4641 Diagnostic &Diags, const char *isysroot,
4642 bool DisableValidation)
4643 : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
4644 Diags(Diags), SemaObj(0), PP(0), Context(0), Consumer(0),
4645 isysroot(isysroot), DisableValidation(DisableValidation), NumStatHits(0),
4646 NumStatMisses(0), NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Sebastian Redl8db9fae2010-09-22 20:19:08 +00004647 NextSLocOffset(0), NumStatementsRead(0), TotalNumStatements(0),
4648 NumMacrosRead(0), TotalNumMacros(0), NumSelectorsRead(0),
4649 NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
4650 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
4651 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
4652 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
Sebastian Redle1dde812010-08-24 00:50:04 +00004653 RelocatablePCH = false;
4654}
4655
4656ASTReader::~ASTReader() {
4657 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
4658 delete Chain[e - i - 1];
4659 // Delete all visible decl lookup tables
4660 for (DeclContextOffsetsMap::iterator I = DeclContextOffsets.begin(),
4661 E = DeclContextOffsets.end();
4662 I != E; ++I) {
4663 for (DeclContextInfos::iterator J = I->second.begin(), F = I->second.end();
4664 J != F; ++J) {
4665 if (J->NameLookupTableData)
4666 delete static_cast<ASTDeclContextNameLookupTable*>(
4667 J->NameLookupTableData);
4668 }
4669 }
4670 for (DeclContextVisibleUpdatesPending::iterator
4671 I = PendingVisibleUpdates.begin(),
4672 E = PendingVisibleUpdates.end();
4673 I != E; ++I) {
4674 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
4675 F = I->second.end();
4676 J != F; ++J)
4677 delete static_cast<ASTDeclContextNameLookupTable*>(*J);
4678 }
4679}
4680
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00004681ASTReader::PerFileData::PerFileData(ASTFileType Ty)
4682 : Type(Ty), SizeInBits(0), LocalNumSLocEntries(0), SLocOffsets(0), LocalSLocSize(0),
Sebastian Redl301c9b02010-09-22 00:42:27 +00004683 LocalNumIdentifiers(0), IdentifierOffsets(0), IdentifierTableData(0),
4684 IdentifierLookupTable(0), LocalNumMacroDefinitions(0),
4685 MacroDefinitionOffsets(0), LocalNumSelectors(0), SelectorOffsets(0),
4686 SelectorLookupTableData(0), SelectorLookupTable(0), LocalNumDecls(0),
Douglas Gregor7c789c12010-10-29 22:39:52 +00004687 DeclOffsets(0), LocalNumCXXBaseSpecifiers(0), CXXBaseSpecifiersOffsets(0),
4688 LocalNumTypes(0), TypeOffsets(0), StatCache(0),
Sebastian Redla866e652010-10-01 19:59:12 +00004689 NumPreallocatedPreprocessingEntities(0), NextInSource(0)
Douglas Gregor501c1032010-08-19 00:28:17 +00004690{}
4691
4692ASTReader::PerFileData::~PerFileData() {
4693 delete static_cast<ASTIdentifierLookupTable *>(IdentifierLookupTable);
4694 delete static_cast<ASTSelectorLookupTable *>(SelectorLookupTable);
4695}
4696