blob: bbabf80486ea0918214ac168e84a33aa479994c0 [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);
Michael J. Spencer20249a12010-10-21 03:16:25 +000087 PARSE_LANGOPT_IMPORTANT(NoConstantCFStrings,
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +000088 diag::warn_pch_no_constant_cfstrings);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000089 PARSE_LANGOPT_BENIGN(PascalStrings);
90 PARSE_LANGOPT_BENIGN(WritableStrings);
Mike Stump1eb44332009-09-09 15:08:12 +000091 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000092 diag::warn_pch_lax_vector_conversions);
Nate Begeman69cfb9b2009-06-25 22:57:40 +000093 PARSE_LANGOPT_IMPORTANT(AltiVec, diag::warn_pch_altivec);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000094 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
Daniel Dunbar73482882010-02-10 18:48:44 +000095 PARSE_LANGOPT_IMPORTANT(SjLjExceptions, diag::warn_pch_sjlj_exceptions);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000096 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
97 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
98 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
Mike Stump1eb44332009-09-09 15:08:12 +000099 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000100 diag::warn_pch_thread_safe_statics);
Daniel Dunbar5345c392009-09-03 04:54:28 +0000101 PARSE_LANGOPT_IMPORTANT(POSIXThreads, diag::warn_pch_posix_threads);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000102 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
103 PARSE_LANGOPT_BENIGN(EmitAllDecls);
104 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
Chris Lattnera4d71452010-06-26 21:25:03 +0000105 PARSE_LANGOPT_BENIGN(getSignedOverflowBehavior());
Mike Stump1eb44332009-09-09 15:08:12 +0000106 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000107 diag::warn_pch_heinous_extensions);
108 // FIXME: Most of the options below are benign if the macro wasn't
109 // used. Unfortunately, this means that a PCH compiled without
110 // optimization can't be used with optimization turned on, even
111 // though the only thing that changes is whether __OPTIMIZE__ was
112 // defined... but if __OPTIMIZE__ never showed up in the header, it
113 // doesn't matter. We could consider making this some special kind
114 // of check.
115 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
116 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
117 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
118 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
119 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
120 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
121 PARSE_LANGOPT_IMPORTANT(AccessControl, diag::warn_pch_access_control);
122 PARSE_LANGOPT_IMPORTANT(CharIsSigned, diag::warn_pch_char_signed);
John Thompsona6fda122009-11-05 20:14:16 +0000123 PARSE_LANGOPT_IMPORTANT(ShortWChar, diag::warn_pch_short_wchar);
Argyrios Kyrtzidis9a2b9d72010-10-08 00:25:19 +0000124 PARSE_LANGOPT_IMPORTANT(ShortEnums, diag::warn_pch_short_enums);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000125 if ((PPLangOpts.getGCMode() != 0) != (LangOpts.getGCMode() != 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000126 Reader.Diag(diag::warn_pch_gc_mode)
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000127 << LangOpts.getGCMode() << PPLangOpts.getGCMode();
128 return true;
129 }
130 PARSE_LANGOPT_BENIGN(getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000131 PARSE_LANGOPT_IMPORTANT(getStackProtectorMode(),
132 diag::warn_pch_stack_protector);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000133 PARSE_LANGOPT_BENIGN(InstantiationDepth);
Nate Begeman69cfb9b2009-06-25 22:57:40 +0000134 PARSE_LANGOPT_IMPORTANT(OpenCL, diag::warn_pch_opencl);
Peter Collingbourne08a53262010-12-01 19:14:57 +0000135 PARSE_LANGOPT_IMPORTANT(CUDA, diag::warn_pch_cuda);
Mike Stump9c276ae2009-12-12 01:27:46 +0000136 PARSE_LANGOPT_BENIGN(CatchUndefined);
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000137 PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors);
Douglas Gregora0068fc2010-07-09 17:35:33 +0000138 PARSE_LANGOPT_BENIGN(SpellChecking);
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +0000139#undef PARSE_LANGOPT_IMPORTANT
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000140#undef PARSE_LANGOPT_BENIGN
141
142 return false;
143}
144
Daniel Dunbardc3c0d22009-11-11 00:52:11 +0000145bool PCHValidator::ReadTargetTriple(llvm::StringRef Triple) {
146 if (Triple == PP.getTargetInfo().getTriple().str())
147 return false;
148
149 Reader.Diag(diag::warn_pch_target_triple)
150 << Triple << PP.getTargetInfo().getTriple().str();
151 return true;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000152}
153
Benjamin Kramer54353f42010-11-25 18:29:30 +0000154namespace {
155 struct EmptyStringRef {
156 bool operator ()(llvm::StringRef r) const { return r.empty(); }
157 };
158 struct EmptyBlock {
159 bool operator ()(const PCHPredefinesBlock &r) const {return r.Data.empty();}
160 };
161}
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000162
163static bool EqualConcatenations(llvm::SmallVector<llvm::StringRef, 2> L,
164 PCHPredefinesBlocks R) {
165 // First, sum up the lengths.
166 unsigned LL = 0, RL = 0;
167 for (unsigned I = 0, N = L.size(); I != N; ++I) {
168 LL += L[I].size();
169 }
170 for (unsigned I = 0, N = R.size(); I != N; ++I) {
171 RL += R[I].Data.size();
172 }
173 if (LL != RL)
174 return false;
175 if (LL == 0 && RL == 0)
176 return true;
177
178 // Kick out empty parts, they confuse the algorithm below.
179 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
180 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
181
182 // Do it the hard way. At this point, both vectors must be non-empty.
183 llvm::StringRef LR = L[0], RR = R[0].Data;
184 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
Daniel Dunbarc76c9e02010-07-16 00:00:11 +0000185 (void) RN;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000186 for (;;) {
187 // Compare the current pieces.
188 if (LR.size() == RR.size()) {
189 // If they're the same length, it's pretty easy.
190 if (LR != RR)
191 return false;
192 // Both pieces are done, advance.
193 ++LI;
194 ++RI;
195 // If either string is done, they're both done, since they're the same
196 // length.
197 if (LI == LN) {
198 assert(RI == RN && "Strings not the same length after all?");
199 return true;
200 }
201 LR = L[LI];
202 RR = R[RI].Data;
203 } else if (LR.size() < RR.size()) {
204 // Right piece is longer.
205 if (!RR.startswith(LR))
206 return false;
207 ++LI;
208 assert(LI != LN && "Strings not the same length after all?");
209 RR = RR.substr(LR.size());
210 LR = L[LI];
211 } else {
212 // Left piece is longer.
213 if (!LR.startswith(RR))
214 return false;
215 ++RI;
216 assert(RI != RN && "Strings not the same length after all?");
217 LR = LR.substr(RR.size());
218 RR = R[RI].Data;
219 }
220 }
221}
222
223static std::pair<FileID, llvm::StringRef::size_type>
224FindMacro(const PCHPredefinesBlocks &Buffers, llvm::StringRef MacroDef) {
225 std::pair<FileID, llvm::StringRef::size_type> Res;
226 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
227 Res.second = Buffers[I].Data.find(MacroDef);
228 if (Res.second != llvm::StringRef::npos) {
229 Res.first = Buffers[I].BufferID;
230 break;
231 }
232 }
233 return Res;
234}
235
236bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000237 llvm::StringRef OriginalFileName,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000238 std::string &SuggestedPredefines) {
Daniel Dunbarc7162932009-11-11 23:58:53 +0000239 // We are in the context of an implicit include, so the predefines buffer will
240 // have a #include entry for the PCH file itself (as normalized by the
241 // preprocessor initialization). Find it and skip over it in the checking
242 // below.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000243 llvm::SmallString<256> PCHInclude;
244 PCHInclude += "#include \"";
Daniel Dunbarc7162932009-11-11 23:58:53 +0000245 PCHInclude += NormalizeDashIncludePath(OriginalFileName);
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000246 PCHInclude += "\"\n";
247 std::pair<llvm::StringRef,llvm::StringRef> Split =
248 llvm::StringRef(PP.getPredefines()).split(PCHInclude.str());
249 llvm::StringRef Left = Split.first, Right = Split.second;
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000250 if (Left == PP.getPredefines()) {
251 Error("Missing PCH include entry!");
252 return true;
253 }
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000254
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000255 // If the concatenation of all the PCH buffers is equal to the adjusted
256 // command line, we're done.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000257 llvm::SmallVector<llvm::StringRef, 2> CommandLine;
258 CommandLine.push_back(Left);
259 CommandLine.push_back(Right);
260 if (EqualConcatenations(CommandLine, Buffers))
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000261 return false;
262
263 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000264
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000265 // The predefines buffers are different. Determine what the differences are,
266 // and whether they require us to reject the PCH file.
Daniel Dunbare6750492009-11-13 16:46:11 +0000267 llvm::SmallVector<llvm::StringRef, 8> PCHLines;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000268 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
269 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Daniel Dunbare6750492009-11-13 16:46:11 +0000270
271 llvm::SmallVector<llvm::StringRef, 8> CmdLineLines;
272 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000273
274 // Pick out implicit #includes after the PCH and don't consider them for
275 // validation; we will insert them into SuggestedPredefines so that the
276 // preprocessor includes them.
277 std::string IncludesAfterPCH;
278 llvm::SmallVector<llvm::StringRef, 8> AfterPCHLines;
279 Right.split(AfterPCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
280 for (unsigned i = 0, e = AfterPCHLines.size(); i != e; ++i) {
281 if (AfterPCHLines[i].startswith("#include ")) {
282 IncludesAfterPCH += AfterPCHLines[i];
283 IncludesAfterPCH += '\n';
284 } else {
285 CmdLineLines.push_back(AfterPCHLines[i]);
286 }
287 }
288
289 // Make sure we add the includes last into SuggestedPredefines before we
290 // exit this function.
291 struct AddIncludesRAII {
292 std::string &SuggestedPredefines;
293 std::string &IncludesAfterPCH;
294
295 AddIncludesRAII(std::string &SuggestedPredefines,
296 std::string &IncludesAfterPCH)
297 : SuggestedPredefines(SuggestedPredefines),
298 IncludesAfterPCH(IncludesAfterPCH) { }
299 ~AddIncludesRAII() {
300 SuggestedPredefines += IncludesAfterPCH;
301 }
302 } AddIncludes(SuggestedPredefines, IncludesAfterPCH);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000303
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000304 // Sort both sets of predefined buffer lines, since we allow some extra
305 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000306 std::sort(CmdLineLines.begin(), CmdLineLines.end());
307 std::sort(PCHLines.begin(), PCHLines.end());
308
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000309 // Determine which predefines that were used to build the PCH file are missing
310 // from the command line.
311 std::vector<llvm::StringRef> MissingPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000312 std::set_difference(PCHLines.begin(), PCHLines.end(),
313 CmdLineLines.begin(), CmdLineLines.end(),
314 std::back_inserter(MissingPredefines));
315
316 bool MissingDefines = false;
317 bool ConflictingDefines = false;
318 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000319 llvm::StringRef Missing = MissingPredefines[I];
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000320 if (Missing.startswith("#include ")) {
321 // An -include was specified when generating the PCH; it is included in
322 // the PCH, just ignore it.
323 continue;
324 }
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000325 if (!Missing.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000326 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
327 return true;
328 }
Mike Stump1eb44332009-09-09 15:08:12 +0000329
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000330 // This is a macro definition. Determine the name of the macro we're
331 // defining.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000332 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000333 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000334 = Missing.find_first_of("( \n\r", StartOfMacroName);
335 assert(EndOfMacroName != std::string::npos &&
336 "Couldn't find the end of the macro name");
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000337 llvm::StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000338
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000339 // Determine whether this macro was given a different definition on the
340 // command line.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000341 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000342 std::string::size_type MacroDefLen = MacroDefStart.size();
Daniel Dunbare6750492009-11-13 16:46:11 +0000343 llvm::SmallVector<llvm::StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000344 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
345 MacroDefStart);
346 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000347 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000348 // Different macro; we're done.
349 ConflictPos = CmdLineLines.end();
Mike Stump1eb44332009-09-09 15:08:12 +0000350 break;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000351 }
Mike Stump1eb44332009-09-09 15:08:12 +0000352
353 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000354 "Invalid #define in predefines buffer?");
Mike Stump1eb44332009-09-09 15:08:12 +0000355 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000356 (*ConflictPos)[MacroDefLen] != '(')
357 continue; // Longer macro name; keep trying.
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000359 // We found a conflicting macro definition.
360 break;
361 }
Mike Stump1eb44332009-09-09 15:08:12 +0000362
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000363 if (ConflictPos != CmdLineLines.end()) {
364 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
365 << MacroName;
366
367 // Show the definition of this macro within the PCH file.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000368 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
369 FindMacro(Buffers, Missing);
370 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
371 SourceLocation PCHMissingLoc =
372 SourceMgr.getLocForStartOfFile(MacroLoc.first)
373 .getFileLocWithOffset(MacroLoc.second);
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000374 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000375
376 ConflictingDefines = true;
377 continue;
378 }
Mike Stump1eb44332009-09-09 15:08:12 +0000379
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000380 // If the macro doesn't conflict, then we'll just pick up the macro
381 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000382 if (ConflictingDefines)
383 continue; // Don't complain if there are already conflicting defs
Mike Stump1eb44332009-09-09 15:08:12 +0000384
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000385 if (!MissingDefines) {
386 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
387 MissingDefines = true;
388 }
389
390 // Show the definition of this macro within the PCH file.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000391 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
392 FindMacro(Buffers, Missing);
393 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
394 SourceLocation PCHMissingLoc =
395 SourceMgr.getLocForStartOfFile(MacroLoc.first)
396 .getFileLocWithOffset(MacroLoc.second);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000397 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
398 }
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000400 if (ConflictingDefines)
401 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000402
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000403 // Determine what predefines were introduced based on command-line
404 // parameters that were not present when building the PCH
405 // file. Extra #defines are okay, so long as the identifiers being
406 // defined were not used within the precompiled header.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000407 std::vector<llvm::StringRef> ExtraPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000408 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
409 PCHLines.begin(), PCHLines.end(),
Mike Stump1eb44332009-09-09 15:08:12 +0000410 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000411 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000412 llvm::StringRef &Extra = ExtraPredefines[I];
413 if (!Extra.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000414 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
415 return true;
416 }
417
418 // This is an extra macro definition. Determine the name of the
419 // macro we're defining.
420 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000421 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000422 = Extra.find_first_of("( \n\r", StartOfMacroName);
423 assert(EndOfMacroName != std::string::npos &&
424 "Couldn't find the end of the macro name");
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000425 llvm::StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000426
427 // Check whether this name was used somewhere in the PCH file. If
428 // so, defining it as a macro could change behavior, so we reject
429 // the PCH file.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000430 if (IdentifierInfo *II = Reader.get(MacroName)) {
Daniel Dunbar4fda42e2009-11-11 00:52:00 +0000431 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000432 return true;
433 }
434
435 // Add this definition to the suggested predefines buffer.
436 SuggestedPredefines += Extra;
437 SuggestedPredefines += '\n';
438 }
439
440 // If we get here, it's because the predefines buffer had compatible
441 // contents. Accept the PCH file.
442 return false;
443}
444
Douglas Gregor12fab312010-03-16 16:35:32 +0000445void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
446 unsigned ID) {
447 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
448 ++NumHeaderInfos;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000449}
450
451void PCHValidator::ReadCounter(unsigned Value) {
452 PP.setCounterValue(Value);
453}
454
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000455//===----------------------------------------------------------------------===//
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000456// AST reader implementation
Douglas Gregor668c1a42009-04-21 22:25:48 +0000457//===----------------------------------------------------------------------===//
458
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000459void
Sebastian Redl571db7f2010-08-18 23:56:56 +0000460ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000461 DeserializationListener = Listener;
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000462}
463
Chris Lattner4c6f9522009-04-27 05:14:47 +0000464
Douglas Gregor668c1a42009-04-21 22:25:48 +0000465namespace {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000466class ASTSelectorLookupTrait {
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000467 ASTReader &Reader;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000468
469public:
Sebastian Redl5d050072010-08-04 17:20:04 +0000470 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000471 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +0000472 ObjCMethodList Instance, Factory;
473 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000474
475 typedef Selector external_key_type;
476 typedef external_key_type internal_key_type;
477
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000478 explicit ASTSelectorLookupTrait(ASTReader &Reader) : Reader(Reader) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000479
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000480 static bool EqualKey(const internal_key_type& a,
481 const internal_key_type& b) {
482 return a == b;
483 }
Mike Stump1eb44332009-09-09 15:08:12 +0000484
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000485 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +0000486 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000487 }
Mike Stump1eb44332009-09-09 15:08:12 +0000488
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000489 // This hopefully will just get inlined and removed by the optimizer.
490 static const internal_key_type&
491 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump1eb44332009-09-09 15:08:12 +0000492
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000493 static std::pair<unsigned, unsigned>
494 ReadKeyDataLength(const unsigned char*& d) {
495 using namespace clang::io;
496 unsigned KeyLen = ReadUnalignedLE16(d);
497 unsigned DataLen = ReadUnalignedLE16(d);
498 return std::make_pair(KeyLen, DataLen);
499 }
Mike Stump1eb44332009-09-09 15:08:12 +0000500
Douglas Gregor83941df2009-04-25 17:48:32 +0000501 internal_key_type ReadKey(const unsigned char* d, unsigned) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000502 using namespace clang::io;
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000503 SelectorTable &SelTable = Reader.getContext()->Selectors;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000504 unsigned N = ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +0000505 IdentifierInfo *FirstII
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000506 = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
507 if (N == 0)
508 return SelTable.getNullarySelector(FirstII);
509 else if (N == 1)
510 return SelTable.getUnarySelector(FirstII);
511
512 llvm::SmallVector<IdentifierInfo *, 16> Args;
513 Args.push_back(FirstII);
514 for (unsigned I = 1; I != N; ++I)
515 Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)));
516
Douglas Gregor75fdb232009-05-22 22:45:36 +0000517 return SelTable.getSelector(N, Args.data());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000518 }
Mike Stump1eb44332009-09-09 15:08:12 +0000519
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000520 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
521 using namespace clang::io;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000522
523 data_type Result;
524
Sebastian Redl5d050072010-08-04 17:20:04 +0000525 Result.ID = ReadUnalignedLE32(d);
526 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
527 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
528
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000529 // Load instance methods
530 ObjCMethodList *Prev = 0;
531 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000532 ObjCMethodDecl *Method
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000533 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl5d050072010-08-04 17:20:04 +0000534 if (!Result.Instance.Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000535 // This is the first method, which is the easy case.
Sebastian Redl5d050072010-08-04 17:20:04 +0000536 Result.Instance.Method = Method;
537 Prev = &Result.Instance;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000538 continue;
539 }
540
Ted Kremenek298ed872010-02-11 00:53:01 +0000541 ObjCMethodList *Mem =
542 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
543 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000544 Prev = Prev->Next;
545 }
546
547 // Load factory methods
548 Prev = 0;
549 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000550 ObjCMethodDecl *Method
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000551 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl5d050072010-08-04 17:20:04 +0000552 if (!Result.Factory.Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000553 // This is the first method, which is the easy case.
Sebastian Redl5d050072010-08-04 17:20:04 +0000554 Result.Factory.Method = Method;
555 Prev = &Result.Factory;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000556 continue;
557 }
558
Ted Kremenek298ed872010-02-11 00:53:01 +0000559 ObjCMethodList *Mem =
560 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
561 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000562 Prev = Prev->Next;
563 }
564
565 return Result;
566 }
567};
Mike Stump1eb44332009-09-09 15:08:12 +0000568
569} // end anonymous namespace
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000570
571/// \brief The on-disk hash table used for the global method pool.
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000572typedef OnDiskChainedHashTable<ASTSelectorLookupTrait>
573 ASTSelectorLookupTable;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000574
Sebastian Redlc3632732010-10-05 15:59:54 +0000575namespace clang {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000576class ASTIdentifierLookupTrait {
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000577 ASTReader &Reader;
Sebastian Redlc3632732010-10-05 15:59:54 +0000578 ASTReader::PerFileData &F;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000579
580 // If we know the IdentifierInfo in advance, it is here and we will
581 // not build a new one. Used when deserializing information about an
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000582 // identifier that was constructed before the AST file was read.
Douglas Gregor668c1a42009-04-21 22:25:48 +0000583 IdentifierInfo *KnownII;
584
585public:
586 typedef IdentifierInfo * data_type;
587
588 typedef const std::pair<const char*, unsigned> external_key_type;
589
590 typedef external_key_type internal_key_type;
591
Sebastian Redlc3632732010-10-05 15:59:54 +0000592 ASTIdentifierLookupTrait(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redld27d3fc2010-07-21 22:31:37 +0000593 IdentifierInfo *II = 0)
Sebastian Redlc3632732010-10-05 15:59:54 +0000594 : Reader(Reader), F(F), KnownII(II) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000595
Douglas Gregor668c1a42009-04-21 22:25:48 +0000596 static bool EqualKey(const internal_key_type& a,
597 const internal_key_type& b) {
598 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
599 : false;
600 }
Mike Stump1eb44332009-09-09 15:08:12 +0000601
Douglas Gregor668c1a42009-04-21 22:25:48 +0000602 static unsigned ComputeHash(const internal_key_type& a) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000603 return llvm::HashString(llvm::StringRef(a.first, a.second));
Douglas Gregor668c1a42009-04-21 22:25:48 +0000604 }
Mike Stump1eb44332009-09-09 15:08:12 +0000605
Douglas Gregor668c1a42009-04-21 22:25:48 +0000606 // This hopefully will just get inlined and removed by the optimizer.
607 static const internal_key_type&
608 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump1eb44332009-09-09 15:08:12 +0000609
Douglas Gregor95f42922010-10-14 22:11:03 +0000610 // This hopefully will just get inlined and removed by the optimizer.
611 static const external_key_type&
612 GetExternalKey(const internal_key_type& x) { return x; }
613
Douglas Gregor668c1a42009-04-21 22:25:48 +0000614 static std::pair<unsigned, unsigned>
615 ReadKeyDataLength(const unsigned char*& d) {
616 using namespace clang::io;
Douglas Gregor5f8e3302009-04-25 20:26:24 +0000617 unsigned DataLen = ReadUnalignedLE16(d);
Douglas Gregord6595a42009-04-25 21:04:17 +0000618 unsigned KeyLen = ReadUnalignedLE16(d);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000619 return std::make_pair(KeyLen, DataLen);
620 }
Mike Stump1eb44332009-09-09 15:08:12 +0000621
Douglas Gregor668c1a42009-04-21 22:25:48 +0000622 static std::pair<const char*, unsigned>
623 ReadKey(const unsigned char* d, unsigned n) {
624 assert(n >= 2 && d[n-1] == '\0');
625 return std::make_pair((const char*) d, n-1);
626 }
Mike Stump1eb44332009-09-09 15:08:12 +0000627
628 IdentifierInfo *ReadData(const internal_key_type& k,
Douglas Gregor668c1a42009-04-21 22:25:48 +0000629 const unsigned char* d,
630 unsigned DataLen) {
631 using namespace clang::io;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000632 IdentID ID = ReadUnalignedLE32(d);
Douglas Gregora92193e2009-04-28 21:18:29 +0000633 bool IsInteresting = ID & 0x01;
634
635 // Wipe out the "is interesting" bit.
636 ID = ID >> 1;
637
638 if (!IsInteresting) {
Sebastian Redl083abdf2010-07-27 23:01:28 +0000639 // For uninteresting identifiers, just build the IdentifierInfo
Douglas Gregora92193e2009-04-28 21:18:29 +0000640 // and associate it with the persistent ID.
641 IdentifierInfo *II = KnownII;
642 if (!II)
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000643 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregora92193e2009-04-28 21:18:29 +0000644 Reader.SetIdentifierInfo(ID, II);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000645 II->setIsFromAST();
Douglas Gregora92193e2009-04-28 21:18:29 +0000646 return II;
647 }
648
Douglas Gregor5998da52009-04-28 21:32:13 +0000649 unsigned Bits = ReadUnalignedLE16(d);
Douglas Gregor2deaea32009-04-22 18:49:13 +0000650 bool CPlusPlusOperatorKeyword = Bits & 0x01;
651 Bits >>= 1;
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000652 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
653 Bits >>= 1;
Douglas Gregor2deaea32009-04-22 18:49:13 +0000654 bool Poisoned = Bits & 0x01;
655 Bits >>= 1;
656 bool ExtensionToken = Bits & 0x01;
657 Bits >>= 1;
658 bool hasMacroDefinition = Bits & 0x01;
659 Bits >>= 1;
660 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
661 Bits >>= 10;
Mike Stump1eb44332009-09-09 15:08:12 +0000662
Douglas Gregor2deaea32009-04-22 18:49:13 +0000663 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregor5998da52009-04-28 21:32:13 +0000664 DataLen -= 6;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000665
666 // Build the IdentifierInfo itself and link the identifier ID with
667 // the new IdentifierInfo.
668 IdentifierInfo *II = KnownII;
669 if (!II)
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000670 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000671 Reader.SetIdentifierInfo(ID, II);
672
Douglas Gregor2deaea32009-04-22 18:49:13 +0000673 // Set or check the various bits in the IdentifierInfo structure.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000674 // Token IDs are read-only.
675 if (HasRevertedTokenIDToIdentifier)
676 II->RevertTokenIDToIdentifier();
Douglas Gregor2deaea32009-04-22 18:49:13 +0000677 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
Mike Stump1eb44332009-09-09 15:08:12 +0000678 assert(II->isExtensionToken() == ExtensionToken &&
Douglas Gregor2deaea32009-04-22 18:49:13 +0000679 "Incorrect extension token flag");
680 (void)ExtensionToken;
681 II->setIsPoisoned(Poisoned);
682 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
683 "Incorrect C++ operator keyword flag");
684 (void)CPlusPlusOperatorKeyword;
685
Douglas Gregor37e26842009-04-21 23:56:24 +0000686 // If this identifier is a macro, deserialize the macro
687 // definition.
688 if (hasMacroDefinition) {
Douglas Gregor5998da52009-04-28 21:32:13 +0000689 uint32_t Offset = ReadUnalignedLE32(d);
Douglas Gregor295a2a62010-10-30 00:23:06 +0000690 Reader.SetIdentifierIsMacro(II, F, Offset);
Douglas Gregor5998da52009-04-28 21:32:13 +0000691 DataLen -= 4;
Douglas Gregor37e26842009-04-21 23:56:24 +0000692 }
Douglas Gregor668c1a42009-04-21 22:25:48 +0000693
694 // Read all of the declarations visible at global scope with this
695 // name.
Chris Lattner6bf690f2009-04-27 22:17:41 +0000696 if (Reader.getContext() == 0) return II;
Douglas Gregord89275b2009-07-06 18:54:52 +0000697 if (DataLen > 0) {
698 llvm::SmallVector<uint32_t, 4> DeclIDs;
699 for (; DataLen > 0; DataLen -= 4)
700 DeclIDs.push_back(ReadUnalignedLE32(d));
701 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000702 }
Mike Stump1eb44332009-09-09 15:08:12 +0000703
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000704 II->setIsFromAST();
Douglas Gregor668c1a42009-04-21 22:25:48 +0000705 return II;
706 }
707};
Mike Stump1eb44332009-09-09 15:08:12 +0000708
709} // end anonymous namespace
Douglas Gregor668c1a42009-04-21 22:25:48 +0000710
711/// \brief The on-disk hash table used to contain information about
712/// all of the identifiers in the program.
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000713typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait>
714 ASTIdentifierLookupTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000715
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000716namespace {
717class ASTDeclContextNameLookupTrait {
718 ASTReader &Reader;
719
720public:
721 /// \brief Pair of begin/end iterators for DeclIDs.
722 typedef std::pair<DeclID *, DeclID *> data_type;
723
724 /// \brief Special internal key for declaration names.
725 /// The hash table creates keys for comparison; we do not create
726 /// a DeclarationName for the internal key to avoid deserializing types.
727 struct DeclNameKey {
728 DeclarationName::NameKind Kind;
729 uint64_t Data;
730 DeclNameKey() : Kind((DeclarationName::NameKind)0), Data(0) { }
731 };
732
733 typedef DeclarationName external_key_type;
734 typedef DeclNameKey internal_key_type;
735
736 explicit ASTDeclContextNameLookupTrait(ASTReader &Reader) : Reader(Reader) { }
737
738 static bool EqualKey(const internal_key_type& a,
739 const internal_key_type& b) {
740 return a.Kind == b.Kind && a.Data == b.Data;
741 }
742
743 unsigned ComputeHash(const DeclNameKey &Key) const {
744 llvm::FoldingSetNodeID ID;
745 ID.AddInteger(Key.Kind);
746
747 switch (Key.Kind) {
748 case DeclarationName::Identifier:
749 case DeclarationName::CXXLiteralOperatorName:
750 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
751 break;
752 case DeclarationName::ObjCZeroArgSelector:
753 case DeclarationName::ObjCOneArgSelector:
754 case DeclarationName::ObjCMultiArgSelector:
755 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
756 break;
757 case DeclarationName::CXXConstructorName:
758 case DeclarationName::CXXDestructorName:
759 case DeclarationName::CXXConversionFunctionName:
760 ID.AddInteger((TypeID)Key.Data);
761 break;
762 case DeclarationName::CXXOperatorName:
763 ID.AddInteger((OverloadedOperatorKind)Key.Data);
764 break;
765 case DeclarationName::CXXUsingDirective:
766 break;
767 }
768
769 return ID.ComputeHash();
770 }
771
772 internal_key_type GetInternalKey(const external_key_type& Name) const {
773 DeclNameKey Key;
774 Key.Kind = Name.getNameKind();
775 switch (Name.getNameKind()) {
776 case DeclarationName::Identifier:
777 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
778 break;
779 case DeclarationName::ObjCZeroArgSelector:
780 case DeclarationName::ObjCOneArgSelector:
781 case DeclarationName::ObjCMultiArgSelector:
782 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
783 break;
784 case DeclarationName::CXXConstructorName:
785 case DeclarationName::CXXDestructorName:
786 case DeclarationName::CXXConversionFunctionName:
787 Key.Data = Reader.GetTypeID(Name.getCXXNameType());
788 break;
789 case DeclarationName::CXXOperatorName:
790 Key.Data = Name.getCXXOverloadedOperator();
791 break;
792 case DeclarationName::CXXLiteralOperatorName:
793 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
794 break;
795 case DeclarationName::CXXUsingDirective:
796 break;
797 }
Michael J. Spencer20249a12010-10-21 03:16:25 +0000798
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000799 return Key;
800 }
801
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000802 external_key_type GetExternalKey(const internal_key_type& Key) const {
803 ASTContext *Context = Reader.getContext();
804 switch (Key.Kind) {
805 case DeclarationName::Identifier:
806 return DeclarationName((IdentifierInfo*)Key.Data);
807
808 case DeclarationName::ObjCZeroArgSelector:
809 case DeclarationName::ObjCOneArgSelector:
810 case DeclarationName::ObjCMultiArgSelector:
811 return DeclarationName(Selector(Key.Data));
812
813 case DeclarationName::CXXConstructorName:
814 return Context->DeclarationNames.getCXXConstructorName(
815 Context->getCanonicalType(Reader.GetType(Key.Data)));
816
817 case DeclarationName::CXXDestructorName:
818 return Context->DeclarationNames.getCXXDestructorName(
819 Context->getCanonicalType(Reader.GetType(Key.Data)));
820
821 case DeclarationName::CXXConversionFunctionName:
822 return Context->DeclarationNames.getCXXConversionFunctionName(
823 Context->getCanonicalType(Reader.GetType(Key.Data)));
824
825 case DeclarationName::CXXOperatorName:
826 return Context->DeclarationNames.getCXXOperatorName(
827 (OverloadedOperatorKind)Key.Data);
828
829 case DeclarationName::CXXLiteralOperatorName:
830 return Context->DeclarationNames.getCXXLiteralOperatorName(
831 (IdentifierInfo*)Key.Data);
832
833 case DeclarationName::CXXUsingDirective:
834 return DeclarationName::getUsingDirectiveName();
835 }
836
837 llvm_unreachable("Invalid Name Kind ?");
838 }
839
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000840 static std::pair<unsigned, unsigned>
841 ReadKeyDataLength(const unsigned char*& d) {
842 using namespace clang::io;
843 unsigned KeyLen = ReadUnalignedLE16(d);
844 unsigned DataLen = ReadUnalignedLE16(d);
845 return std::make_pair(KeyLen, DataLen);
846 }
847
848 internal_key_type ReadKey(const unsigned char* d, unsigned) {
849 using namespace clang::io;
850
851 DeclNameKey Key;
852 Key.Kind = (DeclarationName::NameKind)*d++;
853 switch (Key.Kind) {
854 case DeclarationName::Identifier:
855 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
856 break;
857 case DeclarationName::ObjCZeroArgSelector:
858 case DeclarationName::ObjCOneArgSelector:
859 case DeclarationName::ObjCMultiArgSelector:
Michael J. Spencer20249a12010-10-21 03:16:25 +0000860 Key.Data =
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000861 (uint64_t)Reader.DecodeSelector(ReadUnalignedLE32(d)).getAsOpaquePtr();
862 break;
863 case DeclarationName::CXXConstructorName:
864 case DeclarationName::CXXDestructorName:
865 case DeclarationName::CXXConversionFunctionName:
866 Key.Data = ReadUnalignedLE32(d); // TypeID
867 break;
868 case DeclarationName::CXXOperatorName:
869 Key.Data = *d++; // OverloadedOperatorKind
870 break;
871 case DeclarationName::CXXLiteralOperatorName:
872 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
873 break;
874 case DeclarationName::CXXUsingDirective:
875 break;
876 }
Michael J. Spencer20249a12010-10-21 03:16:25 +0000877
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000878 return Key;
879 }
880
881 data_type ReadData(internal_key_type, const unsigned char* d,
882 unsigned DataLen) {
883 using namespace clang::io;
884 unsigned NumDecls = ReadUnalignedLE16(d);
885 DeclID *Start = (DeclID *)d;
886 return std::make_pair(Start, Start + NumDecls);
887 }
888};
889
890} // end anonymous namespace
891
892/// \brief The on-disk hash table used for the DeclContext's Name lookup table.
893typedef OnDiskChainedHashTable<ASTDeclContextNameLookupTrait>
894 ASTDeclContextNameLookupTable;
895
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000896bool ASTReader::ReadDeclContextStorage(llvm::BitstreamCursor &Cursor,
897 const std::pair<uint64_t, uint64_t> &Offsets,
898 DeclContextInfo &Info) {
899 SavedStreamPosition SavedPosition(Cursor);
900 // First the lexical decls.
901 if (Offsets.first != 0) {
902 Cursor.JumpToBit(Offsets.first);
903
904 RecordData Record;
905 const char *Blob;
906 unsigned BlobLen;
907 unsigned Code = Cursor.ReadCode();
908 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
909 if (RecCode != DECL_CONTEXT_LEXICAL) {
910 Error("Expected lexical block");
911 return true;
912 }
913
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000914 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
915 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000916 } else {
917 Info.LexicalDecls = 0;
918 Info.NumLexicalDecls = 0;
919 }
920
921 // Now the lookup table.
922 if (Offsets.second != 0) {
923 Cursor.JumpToBit(Offsets.second);
924
925 RecordData Record;
926 const char *Blob;
927 unsigned BlobLen;
928 unsigned Code = Cursor.ReadCode();
929 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
930 if (RecCode != DECL_CONTEXT_VISIBLE) {
931 Error("Expected visible lookup table block");
932 return true;
933 }
934 Info.NameLookupTableData
935 = ASTDeclContextNameLookupTable::Create(
936 (const unsigned char *)Blob + Record[0],
937 (const unsigned char *)Blob,
938 ASTDeclContextNameLookupTrait(*this));
Sebastian Redl0ea8f7f2010-08-24 00:50:00 +0000939 } else {
940 Info.NameLookupTableData = 0;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000941 }
942
943 return false;
944}
945
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000946void ASTReader::Error(const char *Msg) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000947 Diag(diag::err_fe_pch_malformed) << Msg;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000948}
949
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000950/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000951bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000952 if (Listener)
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000953 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000954 ActualOriginalFileName,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000955 SuggestedPredefines);
Douglas Gregore721f952009-04-28 18:58:38 +0000956 return false;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000957}
958
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000959//===----------------------------------------------------------------------===//
960// Source Manager Deserialization
961//===----------------------------------------------------------------------===//
962
Douglas Gregorbd945002009-04-13 16:31:14 +0000963/// \brief Read the line table in the source manager block.
Sebastian Redlc3632732010-10-05 15:59:54 +0000964/// \returns true if there was an error.
965bool ASTReader::ParseLineTable(PerFileData &F,
966 llvm::SmallVectorImpl<uint64_t> &Record) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000967 unsigned Idx = 0;
968 LineTableInfo &LineTable = SourceMgr.getLineTable();
969
970 // Parse the file names
Douglas Gregorff0a9872009-04-13 17:12:42 +0000971 std::map<int, int> FileIDs;
972 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000973 // Extract the file name
974 unsigned FilenameLen = Record[Idx++];
975 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
976 Idx += FilenameLen;
Douglas Gregore650c8c2009-07-07 00:12:59 +0000977 MaybeAddSystemRootToFilename(Filename);
Mike Stump1eb44332009-09-09 15:08:12 +0000978 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
Douglas Gregorff0a9872009-04-13 17:12:42 +0000979 Filename.size());
Douglas Gregorbd945002009-04-13 16:31:14 +0000980 }
981
982 // Parse the line entries
983 std::vector<LineEntry> Entries;
984 while (Idx < Record.size()) {
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000985 int FID = Record[Idx++];
Douglas Gregorbd945002009-04-13 16:31:14 +0000986
987 // Extract the line entries
988 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000989 assert(NumEntries && "Numentries is 00000");
Douglas Gregorbd945002009-04-13 16:31:14 +0000990 Entries.clear();
991 Entries.reserve(NumEntries);
992 for (unsigned I = 0; I != NumEntries; ++I) {
993 unsigned FileOffset = Record[Idx++];
994 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000995 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump1eb44332009-09-09 15:08:12 +0000996 SrcMgr::CharacteristicKind FileKind
Douglas Gregorbd945002009-04-13 16:31:14 +0000997 = (SrcMgr::CharacteristicKind)Record[Idx++];
998 unsigned IncludeOffset = Record[Idx++];
999 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1000 FileKind, IncludeOffset));
1001 }
1002 LineTable.AddEntry(FID, Entries);
1003 }
1004
1005 return false;
1006}
1007
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001008namespace {
1009
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001010class ASTStatData {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001011public:
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001012 const ino_t ino;
1013 const dev_t dev;
1014 const mode_t mode;
1015 const time_t mtime;
1016 const off_t size;
Mike Stump1eb44332009-09-09 15:08:12 +00001017
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001018 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Chris Lattner74e976b2010-11-23 19:28:12 +00001019 : ino(i), dev(d), mode(mo), mtime(m), size(s) {}
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001020};
1021
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001022class ASTStatLookupTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001023 public:
1024 typedef const char *external_key_type;
1025 typedef const char *internal_key_type;
1026
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001027 typedef ASTStatData data_type;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001028
1029 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001030 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001031 }
1032
1033 static internal_key_type GetInternalKey(const char *path) { return path; }
1034
1035 static bool EqualKey(internal_key_type a, internal_key_type b) {
1036 return strcmp(a, b) == 0;
1037 }
1038
1039 static std::pair<unsigned, unsigned>
1040 ReadKeyDataLength(const unsigned char*& d) {
1041 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1042 unsigned DataLen = (unsigned) *d++;
1043 return std::make_pair(KeyLen + 1, DataLen);
1044 }
1045
1046 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1047 return (const char *)d;
1048 }
1049
1050 static data_type ReadData(const internal_key_type, const unsigned char *d,
1051 unsigned /*DataLen*/) {
1052 using namespace clang::io;
1053
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001054 ino_t ino = (ino_t) ReadUnalignedLE32(d);
1055 dev_t dev = (dev_t) ReadUnalignedLE32(d);
1056 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +00001057 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001058 off_t size = (off_t) ReadUnalignedLE64(d);
1059 return data_type(ino, dev, mode, mtime, size);
1060 }
1061};
1062
1063/// \brief stat() cache for precompiled headers.
1064///
1065/// This cache is very similar to the stat cache used by pretokenized
1066/// headers.
Chris Lattner10e286a2010-11-23 19:19:34 +00001067class ASTStatCache : public FileSystemStatCache {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001068 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001069 CacheTy *Cache;
1070
1071 unsigned &NumStatHits, &NumStatMisses;
Mike Stump1eb44332009-09-09 15:08:12 +00001072public:
Chris Lattner74e976b2010-11-23 19:28:12 +00001073 ASTStatCache(const unsigned char *Buckets, const unsigned char *Base,
1074 unsigned &NumStatHits, unsigned &NumStatMisses)
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001075 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
1076 Cache = CacheTy::Create(Buckets, Base);
1077 }
1078
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001079 ~ASTStatCache() { delete Cache; }
Mike Stump1eb44332009-09-09 15:08:12 +00001080
Chris Lattner898a0612010-11-23 21:17:56 +00001081 LookupResult getStat(const char *Path, struct stat &StatBuf,
1082 int *FileDescriptor) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001083 // Do the lookup for the file's data in the AST file.
Chris Lattner10e286a2010-11-23 19:19:34 +00001084 CacheTy::iterator I = Cache->find(Path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001085
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001086 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001087 if (I == Cache->end()) {
1088 ++NumStatMisses;
Chris Lattner898a0612010-11-23 21:17:56 +00001089 return statChained(Path, StatBuf, FileDescriptor);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001090 }
Mike Stump1eb44332009-09-09 15:08:12 +00001091
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001092 ++NumStatHits;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001093 ASTStatData Data = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001094
Chris Lattner10e286a2010-11-23 19:19:34 +00001095 StatBuf.st_ino = Data.ino;
1096 StatBuf.st_dev = Data.dev;
1097 StatBuf.st_mtime = Data.mtime;
1098 StatBuf.st_mode = Data.mode;
1099 StatBuf.st_size = Data.size;
Chris Lattnerd6f61112010-11-23 20:05:15 +00001100 return CacheExists;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001101 }
1102};
1103} // end anonymous namespace
1104
1105
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001106/// \brief Read a source manager block
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001107ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(PerFileData &F) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001108 using namespace SrcMgr;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001109
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001110 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001111
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001112 // Set the source-location entry cursor to the current position in
1113 // the stream. This cursor will be used to read the contents of the
1114 // source manager block initially, and then lazily read
1115 // source-location entries as needed.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001116 SLocEntryCursor = F.Stream;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001117
1118 // The stream itself is going to skip over the source manager block.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001119 if (F.Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001120 Error("malformed block record in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001121 return Failure;
1122 }
1123
1124 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001125 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001126 Error("malformed source manager block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001127 return Failure;
1128 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001129
Douglas Gregor14f79002009-04-10 03:52:48 +00001130 RecordData Record;
1131 while (true) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001132 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregor14f79002009-04-10 03:52:48 +00001133 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001134 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001135 Error("error at end of Source Manager block in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001136 return Failure;
1137 }
Douglas Gregore1d918e2009-04-10 23:10:45 +00001138 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +00001139 }
Mike Stump1eb44332009-09-09 15:08:12 +00001140
Douglas Gregor14f79002009-04-10 03:52:48 +00001141 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1142 // No known subblocks, always skip them.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001143 SLocEntryCursor.ReadSubBlockID();
1144 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001145 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001146 return Failure;
1147 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001148 continue;
1149 }
Mike Stump1eb44332009-09-09 15:08:12 +00001150
Douglas Gregor14f79002009-04-10 03:52:48 +00001151 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001152 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregor14f79002009-04-10 03:52:48 +00001153 continue;
1154 }
Mike Stump1eb44332009-09-09 15:08:12 +00001155
Douglas Gregor14f79002009-04-10 03:52:48 +00001156 // Read a record.
1157 const char *BlobStart;
1158 unsigned BlobLen;
1159 Record.clear();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001160 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001161 default: // Default behavior: ignore.
1162 break;
1163
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001164 case SM_LINE_TABLE:
Sebastian Redlc3632732010-10-05 15:59:54 +00001165 if (ParseLineTable(F, Record))
Douglas Gregorbd945002009-04-13 16:31:14 +00001166 return Failure;
Chris Lattner2c78b872009-04-14 23:22:57 +00001167 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001168
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001169 case SM_SLOC_FILE_ENTRY:
1170 case SM_SLOC_BUFFER_ENTRY:
1171 case SM_SLOC_INSTANTIATION_ENTRY:
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001172 // Once we hit one of the source location entries, we're done.
1173 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +00001174 }
1175 }
1176}
1177
Sebastian Redl190faf72010-07-20 21:50:20 +00001178/// \brief Get a cursor that's correctly positioned for reading the source
1179/// location entry with the given ID.
Sebastian Redlc3632732010-10-05 15:59:54 +00001180ASTReader::PerFileData *ASTReader::SLocCursorForID(unsigned ID) {
Sebastian Redl190faf72010-07-20 21:50:20 +00001181 assert(ID != 0 && ID <= TotalNumSLocEntries &&
1182 "SLocCursorForID should only be called for real IDs.");
1183
1184 ID -= 1;
1185 PerFileData *F = 0;
1186 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1187 F = Chain[N - I - 1];
1188 if (ID < F->LocalNumSLocEntries)
1189 break;
1190 ID -= F->LocalNumSLocEntries;
1191 }
1192 assert(F && F->LocalNumSLocEntries > ID && "Chain corrupted");
1193
1194 F->SLocEntryCursor.JumpToBit(F->SLocOffsets[ID]);
Sebastian Redlc3632732010-10-05 15:59:54 +00001195 return F;
Sebastian Redl190faf72010-07-20 21:50:20 +00001196}
1197
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001198/// \brief Read in the source location entry with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001199ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(unsigned ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001200 if (ID == 0)
1201 return Success;
1202
1203 if (ID > TotalNumSLocEntries) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001204 Error("source location entry ID out-of-range for AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001205 return Failure;
1206 }
1207
Sebastian Redlc3632732010-10-05 15:59:54 +00001208 PerFileData *F = SLocCursorForID(ID);
1209 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001210
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001211 ++NumSLocEntriesRead;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001212 unsigned Code = SLocEntryCursor.ReadCode();
1213 if (Code == llvm::bitc::END_BLOCK ||
1214 Code == llvm::bitc::ENTER_SUBBLOCK ||
1215 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001216 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001217 return Failure;
1218 }
1219
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001220 RecordData Record;
1221 const char *BlobStart;
1222 unsigned BlobLen;
1223 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1224 default:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001225 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001226 return Failure;
1227
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001228 case SM_SLOC_FILE_ENTRY: {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001229 std::string Filename(BlobStart, BlobStart + BlobLen);
1230 MaybeAddSystemRootToFilename(Filename);
Chris Lattner39b49bc2010-11-23 08:35:12 +00001231 const FileEntry *File = FileMgr.getFile(Filename);
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001232 if (File == 0) {
1233 std::string ErrorStr = "could not find file '";
Douglas Gregore650c8c2009-07-07 00:12:59 +00001234 ErrorStr += Filename;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001235 ErrorStr += "' referenced by AST file";
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001236 Error(ErrorStr.c_str());
1237 return Failure;
1238 }
Mike Stump1eb44332009-09-09 15:08:12 +00001239
Douglas Gregor2d52be52010-03-21 22:49:54 +00001240 if (Record.size() < 10) {
Ted Kremenek1857f622010-03-18 21:23:05 +00001241 Error("source location entry is incorrect");
1242 return Failure;
1243 }
1244
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001245 if (!DisableValidation &&
1246 ((off_t)Record[4] != File->getSize()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001247#if !defined(LLVM_ON_WIN32)
1248 // In our regression testing, the Windows file system seems to
1249 // have inconsistent modification times that sometimes
1250 // erroneously trigger this error-handling path.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001251 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001252#endif
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001253 )) {
Douglas Gregor2d52be52010-03-21 22:49:54 +00001254 Diag(diag::err_fe_pch_file_modified)
1255 << Filename;
1256 return Failure;
1257 }
1258
Chris Lattner75dfb652010-11-23 09:19:42 +00001259 FileID FID = SourceMgr.createFileID(File, ReadSourceLocation(*F, Record[1]),
1260 (SrcMgr::CharacteristicKind)Record[2],
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001261 ID, Record[0]);
1262 if (Record[3])
1263 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile())
1264 .setHasLineDirectives();
1265
Douglas Gregor12fab312010-03-16 16:35:32 +00001266 // Reconstruct header-search information for this file.
1267 HeaderFileInfo HFI;
Douglas Gregor2d52be52010-03-21 22:49:54 +00001268 HFI.isImport = Record[6];
1269 HFI.DirInfo = Record[7];
1270 HFI.NumIncludes = Record[8];
1271 HFI.ControllingMacroID = Record[9];
Douglas Gregor12fab312010-03-16 16:35:32 +00001272 if (Listener)
1273 Listener->ReadHeaderFileInfo(HFI, File->getUID());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001274 break;
1275 }
1276
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001277 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001278 const char *Name = BlobStart;
1279 unsigned Offset = Record[0];
1280 unsigned Code = SLocEntryCursor.ReadCode();
1281 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001282 unsigned RecCode
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001283 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001284
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001285 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001286 Error("AST record has invalid code");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001287 return Failure;
1288 }
1289
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001290 llvm::MemoryBuffer *Buffer
Chris Lattnera0a270c2010-04-05 22:42:27 +00001291 = llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(BlobStart, BlobLen - 1),
1292 Name);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001293 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset);
Mike Stump1eb44332009-09-09 15:08:12 +00001294
Douglas Gregor92b059e2009-04-28 20:33:11 +00001295 if (strcmp(Name, "<built-in>") == 0) {
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +00001296 PCHPredefinesBlock Block = {
1297 BufferID,
1298 llvm::StringRef(BlobStart, BlobLen - 1)
1299 };
1300 PCHPredefinesBuffers.push_back(Block);
Douglas Gregor92b059e2009-04-28 20:33:11 +00001301 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001302
1303 break;
1304 }
1305
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001306 case SM_SLOC_INSTANTIATION_ENTRY: {
Sebastian Redlc3632732010-10-05 15:59:54 +00001307 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001308 SourceMgr.createInstantiationLoc(SpellingLoc,
Sebastian Redlc3632732010-10-05 15:59:54 +00001309 ReadSourceLocation(*F, Record[2]),
1310 ReadSourceLocation(*F, Record[3]),
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001311 Record[4],
1312 ID,
1313 Record[0]);
1314 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001315 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001316 }
1317
1318 return Success;
1319}
1320
Chris Lattner6367f6d2009-04-27 01:05:14 +00001321/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1322/// specified cursor. Read the abbreviations that are at the top of the block
1323/// and then leave the cursor pointing into the block.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001324bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattner6367f6d2009-04-27 01:05:14 +00001325 unsigned BlockID) {
1326 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001327 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001328 return Failure;
1329 }
Mike Stump1eb44332009-09-09 15:08:12 +00001330
Chris Lattner6367f6d2009-04-27 01:05:14 +00001331 while (true) {
Douglas Gregorecdcb882010-10-20 22:00:55 +00001332 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattner6367f6d2009-04-27 01:05:14 +00001333 unsigned Code = Cursor.ReadCode();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001334
Chris Lattner6367f6d2009-04-27 01:05:14 +00001335 // We expect all abbrevs to be at the start of the block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001336 if (Code != llvm::bitc::DEFINE_ABBREV) {
1337 Cursor.JumpToBit(Offset);
Chris Lattner6367f6d2009-04-27 01:05:14 +00001338 return false;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001339 }
Chris Lattner6367f6d2009-04-27 01:05:14 +00001340 Cursor.ReadAbbrevRecord();
1341 }
1342}
1343
Douglas Gregor89d99802010-11-30 06:16:57 +00001344PreprocessedEntity *ASTReader::ReadMacroRecord(PerFileData &F, uint64_t Offset) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001345 assert(PP && "Forgot to set Preprocessor ?");
Douglas Gregorecdcb882010-10-20 22:00:55 +00001346 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump1eb44332009-09-09 15:08:12 +00001347
Douglas Gregor37e26842009-04-21 23:56:24 +00001348 // Keep track of where we are in the stream, then jump back there
1349 // after reading this macro.
1350 SavedStreamPosition SavedPosition(Stream);
1351
1352 Stream.JumpToBit(Offset);
1353 RecordData Record;
1354 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1355 MacroInfo *Macro = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001356
Douglas Gregor37e26842009-04-21 23:56:24 +00001357 while (true) {
1358 unsigned Code = Stream.ReadCode();
1359 switch (Code) {
1360 case llvm::bitc::END_BLOCK:
Douglas Gregor89d99802010-11-30 06:16:57 +00001361 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001362
1363 case llvm::bitc::ENTER_SUBBLOCK:
1364 // No known subblocks, always skip them.
1365 Stream.ReadSubBlockID();
1366 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001367 Error("malformed block record in AST file");
Douglas Gregor89d99802010-11-30 06:16:57 +00001368 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001369 }
1370 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001371
Douglas Gregor37e26842009-04-21 23:56:24 +00001372 case llvm::bitc::DEFINE_ABBREV:
1373 Stream.ReadAbbrevRecord();
1374 continue;
1375 default: break;
1376 }
1377
1378 // Read a record.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001379 const char *BlobStart = 0;
1380 unsigned BlobLen = 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001381 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001382 PreprocessorRecordTypes RecType =
Michael J. Spencer20249a12010-10-21 03:16:25 +00001383 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregorecdcb882010-10-20 22:00:55 +00001384 BlobLen);
Douglas Gregor37e26842009-04-21 23:56:24 +00001385 switch (RecType) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001386 case PP_MACRO_OBJECT_LIKE:
1387 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001388 // If we already have a macro, that means that we've hit the end
1389 // of the definition of the macro we were looking for. We're
1390 // done.
1391 if (Macro)
Douglas Gregor89d99802010-11-30 06:16:57 +00001392 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001393
1394 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1395 if (II == 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001396 Error("macro must have a name in AST file");
Douglas Gregor89d99802010-11-30 06:16:57 +00001397 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001398 }
Sebastian Redlc3632732010-10-05 15:59:54 +00001399 SourceLocation Loc = ReadSourceLocation(F, Record[1]);
Douglas Gregor37e26842009-04-21 23:56:24 +00001400 bool isUsed = Record[2];
Mike Stump1eb44332009-09-09 15:08:12 +00001401
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001402 MacroInfo *MI = PP->AllocateMacroInfo(Loc);
Douglas Gregor37e26842009-04-21 23:56:24 +00001403 MI->setIsUsed(isUsed);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001404 MI->setIsFromAST();
Mike Stump1eb44332009-09-09 15:08:12 +00001405
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001406 unsigned NextIndex = 3;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001407 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregor37e26842009-04-21 23:56:24 +00001408 // Decode function-like macro info.
1409 bool isC99VarArgs = Record[3];
1410 bool isGNUVarArgs = Record[4];
1411 MacroArgs.clear();
1412 unsigned NumArgs = Record[5];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001413 NextIndex = 6 + NumArgs;
Douglas Gregor37e26842009-04-21 23:56:24 +00001414 for (unsigned i = 0; i != NumArgs; ++i)
1415 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1416
1417 // Install function-like macro info.
1418 MI->setIsFunctionLike();
1419 if (isC99VarArgs) MI->setIsC99Varargs();
1420 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor75fdb232009-05-22 22:45:36 +00001421 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001422 PP->getPreprocessorAllocator());
Douglas Gregor37e26842009-04-21 23:56:24 +00001423 }
1424
1425 // Finally, install the macro.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001426 PP->setMacroInfo(II, MI);
Douglas Gregor37e26842009-04-21 23:56:24 +00001427
1428 // Remember that we saw this macro last so that we add the tokens that
1429 // form its body to it.
1430 Macro = MI;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001431
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001432 if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) {
1433 // We have a macro definition. Load it now.
1434 PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro,
1435 getMacroDefinition(Record[NextIndex]));
1436 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001437
Douglas Gregor37e26842009-04-21 23:56:24 +00001438 ++NumMacrosRead;
1439 break;
1440 }
Mike Stump1eb44332009-09-09 15:08:12 +00001441
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001442 case PP_TOKEN: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001443 // If we see a TOKEN before a PP_MACRO_*, then the file is
1444 // erroneous, just pretend we didn't see this.
1445 if (Macro == 0) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001446
Douglas Gregor37e26842009-04-21 23:56:24 +00001447 Token Tok;
1448 Tok.startToken();
Sebastian Redlc3632732010-10-05 15:59:54 +00001449 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001450 Tok.setLength(Record[1]);
1451 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1452 Tok.setIdentifierInfo(II);
1453 Tok.setKind((tok::TokenKind)Record[3]);
1454 Tok.setFlag((Token::TokenFlags)Record[4]);
1455 Macro->AddTokenToBody(Tok);
1456 break;
1457 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001458
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001459 case PP_MACRO_INSTANTIATION: {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001460 // If we already have a macro, that means that we've hit the end
1461 // of the definition of the macro we were looking for. We're
1462 // done.
1463 if (Macro)
Douglas Gregor89d99802010-11-30 06:16:57 +00001464 return 0;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001465
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001466 if (!PP->getPreprocessingRecord()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001467 Error("missing preprocessing record in AST file");
Douglas Gregor89d99802010-11-30 06:16:57 +00001468 return 0;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001469 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001470
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001471 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
Douglas Gregor89d99802010-11-30 06:16:57 +00001472 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1473 return PE;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001474
1475 MacroInstantiation *MI
1476 = new (PPRec) MacroInstantiation(DecodeIdentifierInfo(Record[3]),
Sebastian Redlc3632732010-10-05 15:59:54 +00001477 SourceRange(ReadSourceLocation(F, Record[1]),
1478 ReadSourceLocation(F, Record[2])),
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001479 getMacroDefinition(Record[4]));
1480 PPRec.SetPreallocatedEntity(Record[0], MI);
Douglas Gregor89d99802010-11-30 06:16:57 +00001481 return MI;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001482 }
1483
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001484 case PP_MACRO_DEFINITION: {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001485 // If we already have a macro, that means that we've hit the end
1486 // of the definition of the macro we were looking for. We're
1487 // done.
1488 if (Macro)
Douglas Gregor89d99802010-11-30 06:16:57 +00001489 return 0;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001490
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001491 if (!PP->getPreprocessingRecord()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001492 Error("missing preprocessing record in AST file");
Douglas Gregor89d99802010-11-30 06:16:57 +00001493 return 0;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001494 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001495
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001496 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
Douglas Gregor89d99802010-11-30 06:16:57 +00001497 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1498 return PE;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001499
Douglas Gregor77424bc2010-10-02 19:29:26 +00001500 if (Record[1] > MacroDefinitionsLoaded.size()) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001501 Error("out-of-bounds macro definition record");
Douglas Gregor89d99802010-11-30 06:16:57 +00001502 return 0;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001503 }
1504
Douglas Gregor77424bc2010-10-02 19:29:26 +00001505 // Decode the identifier info and then check again; if the macro is
Michael J. Spencer20249a12010-10-21 03:16:25 +00001506 // still defined and associated with the identifier,
Douglas Gregor77424bc2010-10-02 19:29:26 +00001507 IdentifierInfo *II = DecodeIdentifierInfo(Record[4]);
1508 if (!MacroDefinitionsLoaded[Record[1] - 1]) {
1509 MacroDefinition *MD
1510 = new (PPRec) MacroDefinition(II,
Sebastian Redlc3632732010-10-05 15:59:54 +00001511 ReadSourceLocation(F, Record[5]),
Douglas Gregorb1a7d9a2010-10-01 20:33:34 +00001512 SourceRange(
Sebastian Redlc3632732010-10-05 15:59:54 +00001513 ReadSourceLocation(F, Record[2]),
1514 ReadSourceLocation(F, Record[3])));
Michael J. Spencer20249a12010-10-21 03:16:25 +00001515
Douglas Gregor77424bc2010-10-02 19:29:26 +00001516 PPRec.SetPreallocatedEntity(Record[0], MD);
1517 MacroDefinitionsLoaded[Record[1] - 1] = MD;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001518
Douglas Gregor77424bc2010-10-02 19:29:26 +00001519 if (DeserializationListener)
1520 DeserializationListener->MacroDefinitionRead(Record[1], MD);
1521 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001522
Douglas Gregor89d99802010-11-30 06:16:57 +00001523 return MacroDefinitionsLoaded[Record[1] - 1];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001524 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001525
Douglas Gregorecdcb882010-10-20 22:00:55 +00001526 case PP_INCLUSION_DIRECTIVE: {
1527 // If we already have a macro, that means that we've hit the end
1528 // of the definition of the macro we were looking for. We're
1529 // done.
1530 if (Macro)
Douglas Gregor89d99802010-11-30 06:16:57 +00001531 return 0;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001532
Douglas Gregorecdcb882010-10-20 22:00:55 +00001533 if (!PP->getPreprocessingRecord()) {
1534 Error("missing preprocessing record in AST file");
Douglas Gregor89d99802010-11-30 06:16:57 +00001535 return 0;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001536 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001537
Douglas Gregorecdcb882010-10-20 22:00:55 +00001538 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
Douglas Gregor89d99802010-11-30 06:16:57 +00001539 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1540 return PE;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001541
1542 const char *FullFileNameStart = BlobStart + Record[3];
Michael J. Spencer20249a12010-10-21 03:16:25 +00001543 const FileEntry *File
Douglas Gregor89d99802010-11-30 06:16:57 +00001544 = PP->getFileManager().getFile(llvm::StringRef(FullFileNameStart,
1545 BlobLen - Record[3]));
Michael J. Spencer20249a12010-10-21 03:16:25 +00001546
Douglas Gregorecdcb882010-10-20 22:00:55 +00001547 // FIXME: Stable encoding
1548 InclusionDirective::InclusionKind Kind
1549 = static_cast<InclusionDirective::InclusionKind>(Record[5]);
1550 InclusionDirective *ID
Douglas Gregor4ab829c2010-11-01 15:03:47 +00001551 = new (PPRec) InclusionDirective(PPRec, Kind,
Douglas Gregorecdcb882010-10-20 22:00:55 +00001552 llvm::StringRef(BlobStart, Record[3]),
1553 Record[4],
1554 File,
1555 SourceRange(ReadSourceLocation(F, Record[1]),
1556 ReadSourceLocation(F, Record[2])));
1557 PPRec.SetPreallocatedEntity(Record[0], ID);
Douglas Gregor89d99802010-11-30 06:16:57 +00001558 return ID;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001559 }
Sebastian Redlb57a6242010-09-27 22:18:47 +00001560 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001561 }
Douglas Gregor89d99802010-11-30 06:16:57 +00001562
1563 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001564}
1565
Douglas Gregor295a2a62010-10-30 00:23:06 +00001566void ASTReader::SetIdentifierIsMacro(IdentifierInfo *II, PerFileData &F,
1567 uint64_t Offset) {
1568 // Note that this identifier has a macro definition.
1569 II->setHasMacroDefinition(true);
1570
1571 // Adjust the offset based on our position in the chain.
1572 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1573 if (Chain[I] == &F)
1574 break;
1575
1576 Offset += Chain[I]->SizeInBits;
1577 }
1578
1579 UnreadMacroRecordOffsets[II] = Offset;
1580}
1581
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001582void ASTReader::ReadDefinedMacros() {
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001583 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redlc3632732010-10-05 15:59:54 +00001584 PerFileData &F = *Chain[N - I - 1];
1585 llvm::BitstreamCursor &MacroCursor = F.MacroCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001586
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001587 // If there was no preprocessor block, skip this file.
1588 if (!MacroCursor.getBitStreamReader())
1589 continue;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001590
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001591 llvm::BitstreamCursor Cursor = MacroCursor;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001592 Cursor.JumpToBit(F.MacroStartOffset);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001593
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001594 RecordData Record;
1595 while (true) {
Sebastian Redledadecc2010-09-28 02:55:49 +00001596 uint64_t Offset = Cursor.GetCurrentBitNo();
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001597 unsigned Code = Cursor.ReadCode();
Douglas Gregorecdcb882010-10-20 22:00:55 +00001598 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001599 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001600
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001601 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1602 // No known subblocks, always skip them.
1603 Cursor.ReadSubBlockID();
1604 if (Cursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001605 Error("malformed block record in AST file");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001606 return;
1607 }
1608 continue;
1609 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001610
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001611 if (Code == llvm::bitc::DEFINE_ABBREV) {
1612 Cursor.ReadAbbrevRecord();
1613 continue;
1614 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001615
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001616 // Read a record.
1617 const char *BlobStart;
1618 unsigned BlobLen;
1619 Record.clear();
1620 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1621 default: // Default behavior: ignore.
1622 break;
Douglas Gregor88a35862010-01-04 19:18:44 +00001623
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001624 case PP_MACRO_OBJECT_LIKE:
1625 case PP_MACRO_FUNCTION_LIKE:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001626 DecodeIdentifierInfo(Record[0]);
1627 break;
1628
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001629 case PP_TOKEN:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001630 // Ignore tokens.
1631 break;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001632
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001633 case PP_MACRO_INSTANTIATION:
1634 case PP_MACRO_DEFINITION:
Douglas Gregorecdcb882010-10-20 22:00:55 +00001635 case PP_INCLUSION_DIRECTIVE:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001636 // Read the macro record.
Sebastian Redledadecc2010-09-28 02:55:49 +00001637 // FIXME: That's a stupid way to do this. We should reuse this cursor.
Sebastian Redlc3632732010-10-05 15:59:54 +00001638 ReadMacroRecord(F, Offset);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001639 break;
1640 }
Douglas Gregor88a35862010-01-04 19:18:44 +00001641 }
1642 }
Douglas Gregor295a2a62010-10-30 00:23:06 +00001643
1644 // Drain the unread macro-record offsets map.
1645 while (!UnreadMacroRecordOffsets.empty())
1646 LoadMacroDefinition(UnreadMacroRecordOffsets.begin());
1647}
1648
1649void ASTReader::LoadMacroDefinition(
1650 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos) {
1651 assert(Pos != UnreadMacroRecordOffsets.end() && "Unknown macro definition");
1652 PerFileData *F = 0;
1653 uint64_t Offset = Pos->second;
1654 UnreadMacroRecordOffsets.erase(Pos);
1655
1656 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1657 if (Offset < Chain[I]->SizeInBits) {
1658 F = Chain[I];
1659 break;
1660 }
1661
1662 Offset -= Chain[I]->SizeInBits;
1663 }
1664 if (!F) {
1665 Error("Malformed macro record offset");
1666 return;
1667 }
1668
1669 ReadMacroRecord(*F, Offset);
1670}
1671
1672void ASTReader::LoadMacroDefinition(IdentifierInfo *II) {
1673 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos
1674 = UnreadMacroRecordOffsets.find(II);
1675 LoadMacroDefinition(Pos);
Douglas Gregor88a35862010-01-04 19:18:44 +00001676}
1677
Sebastian Redlf73c93f2010-09-15 19:54:06 +00001678MacroDefinition *ASTReader::getMacroDefinition(MacroID ID) {
Douglas Gregor77424bc2010-10-02 19:29:26 +00001679 if (ID == 0 || ID > MacroDefinitionsLoaded.size())
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001680 return 0;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001681
Douglas Gregor77424bc2010-10-02 19:29:26 +00001682 if (!MacroDefinitionsLoaded[ID - 1]) {
1683 unsigned Index = ID - 1;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001684 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1685 PerFileData &F = *Chain[N - I - 1];
1686 if (Index < F.LocalNumMacroDefinitions) {
Sebastian Redlc3632732010-10-05 15:59:54 +00001687 ReadMacroRecord(F, F.MacroDefinitionOffsets[Index]);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001688 break;
1689 }
1690 Index -= F.LocalNumMacroDefinitions;
1691 }
Douglas Gregor77424bc2010-10-02 19:29:26 +00001692 assert(MacroDefinitionsLoaded[ID - 1] && "Broken chain");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001693 }
1694
Douglas Gregor77424bc2010-10-02 19:29:26 +00001695 return MacroDefinitionsLoaded[ID - 1];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001696}
1697
Douglas Gregore650c8c2009-07-07 00:12:59 +00001698/// \brief If we are loading a relocatable PCH file, and the filename is
1699/// not an absolute path, add the system root to the beginning of the file
1700/// name.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001701void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001702 // If this is not a relocatable PCH file, there's nothing to do.
1703 if (!RelocatablePCH)
1704 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001705
Daniel Dunbard5b21972009-11-18 19:50:41 +00001706 if (Filename.empty() || llvm::sys::Path(Filename).isAbsolute())
Douglas Gregore650c8c2009-07-07 00:12:59 +00001707 return;
1708
Douglas Gregore650c8c2009-07-07 00:12:59 +00001709 if (isysroot == 0) {
1710 // If no system root was given, default to '/'
1711 Filename.insert(Filename.begin(), '/');
1712 return;
1713 }
Mike Stump1eb44332009-09-09 15:08:12 +00001714
Douglas Gregore650c8c2009-07-07 00:12:59 +00001715 unsigned Length = strlen(isysroot);
1716 if (isysroot[Length - 1] != '/')
1717 Filename.insert(Filename.begin(), '/');
Mike Stump1eb44332009-09-09 15:08:12 +00001718
Douglas Gregore650c8c2009-07-07 00:12:59 +00001719 Filename.insert(Filename.begin(), isysroot, isysroot + Length);
1720}
1721
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001722ASTReader::ASTReadResult
Sebastian Redl571db7f2010-08-18 23:56:56 +00001723ASTReader::ReadASTBlock(PerFileData &F) {
Sebastian Redl9137a522010-07-16 17:50:48 +00001724 llvm::BitstreamCursor &Stream = F.Stream;
1725
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001726 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001727 Error("malformed block record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001728 return Failure;
1729 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001730
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001731 // Read all of the records and blocks for the ASt file.
Douglas Gregor8038d512009-04-10 17:25:41 +00001732 RecordData Record;
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001733 bool First = true;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001734 while (!Stream.AtEndOfStream()) {
1735 unsigned Code = Stream.ReadCode();
1736 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001737 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001738 Error("error at end of module block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001739 return Failure;
1740 }
Chris Lattner7356a312009-04-11 21:15:38 +00001741
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001742 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001743 }
1744
1745 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1746 switch (Stream.ReadSubBlockID()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001747 case DECLTYPES_BLOCK_ID:
Chris Lattner6367f6d2009-04-27 01:05:14 +00001748 // We lazily load the decls block, but we want to set up the
1749 // DeclsCursor cursor to point into it. Clone our current bitcode
1750 // cursor to it, enter the block and read the abbrevs in that block.
1751 // With the main cursor, we just skip over it.
Sebastian Redl9137a522010-07-16 17:50:48 +00001752 F.DeclsCursor = Stream;
Chris Lattner6367f6d2009-04-27 01:05:14 +00001753 if (Stream.SkipBlock() || // Skip with the main cursor.
1754 // Read the abbrevs.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001755 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001756 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001757 return Failure;
1758 }
1759 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001760
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00001761 case DECL_UPDATES_BLOCK_ID:
1762 if (Stream.SkipBlock()) {
1763 Error("malformed block record in AST file");
1764 return Failure;
1765 }
1766 break;
1767
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001768 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl9137a522010-07-16 17:50:48 +00001769 F.MacroCursor = Stream;
Douglas Gregor88a35862010-01-04 19:18:44 +00001770 if (PP)
1771 PP->setExternalSource(this);
1772
Douglas Gregorecdcb882010-10-20 22:00:55 +00001773 if (Stream.SkipBlock() ||
1774 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001775 Error("malformed block record in AST file");
Chris Lattner7356a312009-04-11 21:15:38 +00001776 return Failure;
1777 }
Douglas Gregorecdcb882010-10-20 22:00:55 +00001778 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattner7356a312009-04-11 21:15:38 +00001779 break;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001780
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001781 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001782 switch (ReadSourceManagerBlock(F)) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00001783 case Success:
1784 break;
1785
1786 case Failure:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001787 Error("malformed source manager block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001788 return Failure;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001789
1790 case IgnorePCH:
1791 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001792 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001793 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001794 }
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001795 First = false;
Douglas Gregor8038d512009-04-10 17:25:41 +00001796 continue;
1797 }
1798
1799 if (Code == llvm::bitc::DEFINE_ABBREV) {
1800 Stream.ReadAbbrevRecord();
1801 continue;
1802 }
1803
1804 // Read and process a record.
1805 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00001806 const char *BlobStart = 0;
1807 unsigned BlobLen = 0;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001808 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00001809 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001810 default: // Default behavior: ignore.
1811 break;
1812
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001813 case METADATA: {
1814 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1815 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001816 : diag::warn_pch_version_too_new);
1817 return IgnorePCH;
1818 }
1819
1820 RelocatablePCH = Record[4];
1821 if (Listener) {
1822 std::string TargetTriple(BlobStart, BlobLen);
1823 if (Listener->ReadTargetTriple(TargetTriple))
1824 return IgnorePCH;
1825 }
1826 break;
1827 }
1828
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001829 case CHAINED_METADATA: {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001830 if (!First) {
1831 Error("CHAINED_METADATA is not first record in block");
1832 return Failure;
1833 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001834 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1835 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001836 : diag::warn_pch_version_too_new);
1837 return IgnorePCH;
1838 }
1839
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00001840 // Load the chained file, which is always a PCH file.
1841 switch(ReadASTCore(llvm::StringRef(BlobStart, BlobLen), PCH)) {
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001842 case Failure: return Failure;
1843 // If we have to ignore the dependency, we'll have to ignore this too.
1844 case IgnorePCH: return IgnorePCH;
1845 case Success: break;
1846 }
1847 break;
1848 }
1849
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001850 case TYPE_OFFSET:
Sebastian Redl12d6da02010-07-19 22:06:55 +00001851 if (F.LocalNumTypes != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001852 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001853 return Failure;
1854 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00001855 F.TypeOffsets = (const uint32_t *)BlobStart;
1856 F.LocalNumTypes = Record[0];
Douglas Gregor8038d512009-04-10 17:25:41 +00001857 break;
1858
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001859 case DECL_OFFSET:
Sebastian Redl12d6da02010-07-19 22:06:55 +00001860 if (F.LocalNumDecls != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001861 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001862 return Failure;
1863 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00001864 F.DeclOffsets = (const uint32_t *)BlobStart;
1865 F.LocalNumDecls = Record[0];
Douglas Gregor8038d512009-04-10 17:25:41 +00001866 break;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001867
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001868 case TU_UPDATE_LEXICAL: {
Sebastian Redld692af72010-07-27 18:24:41 +00001869 DeclContextInfo Info = {
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001870 /* No visible information */ 0,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001871 reinterpret_cast<const KindDeclIDPair *>(BlobStart),
1872 BlobLen / sizeof(KindDeclIDPair)
Sebastian Redld692af72010-07-27 18:24:41 +00001873 };
Douglas Gregor3747ee72010-10-01 01:18:02 +00001874 DeclContextOffsets[Context ? Context->getTranslationUnitDecl() : 0]
1875 .push_back(Info);
Sebastian Redld692af72010-07-27 18:24:41 +00001876 break;
1877 }
1878
Sebastian Redle1dde812010-08-24 00:50:04 +00001879 case UPDATE_VISIBLE: {
1880 serialization::DeclID ID = Record[0];
1881 void *Table = ASTDeclContextNameLookupTable::Create(
1882 (const unsigned char *)BlobStart + Record[1],
1883 (const unsigned char *)BlobStart,
1884 ASTDeclContextNameLookupTrait(*this));
Douglas Gregor3747ee72010-10-01 01:18:02 +00001885 if (ID == 1 && Context) { // Is it the TU?
Sebastian Redle1dde812010-08-24 00:50:04 +00001886 DeclContextInfo Info = {
1887 Table, /* No lexical inforamtion */ 0, 0
1888 };
1889 DeclContextOffsets[Context->getTranslationUnitDecl()].push_back(Info);
1890 } else
1891 PendingVisibleUpdates[ID].push_back(Table);
1892 break;
1893 }
1894
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001895 case REDECLS_UPDATE_LATEST: {
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00001896 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
1897 for (unsigned i = 0, e = Record.size(); i < e; i += 2) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001898 DeclID First = Record[i], Latest = Record[i+1];
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00001899 assert((FirstLatestDeclIDs.find(First) == FirstLatestDeclIDs.end() ||
1900 Latest > FirstLatestDeclIDs[First]) &&
1901 "The new latest is supposed to come after the previous latest");
1902 FirstLatestDeclIDs[First] = Latest;
1903 }
1904 break;
1905 }
1906
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001907 case LANGUAGE_OPTIONS:
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001908 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001909 return IgnorePCH;
1910 break;
Douglas Gregor2bec0412009-04-10 21:16:55 +00001911
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001912 case IDENTIFIER_TABLE:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001913 F.IdentifierTableData = BlobStart;
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001914 if (Record[0]) {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001915 F.IdentifierLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001916 = ASTIdentifierLookupTable::Create(
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001917 (const unsigned char *)F.IdentifierTableData + Record[0],
1918 (const unsigned char *)F.IdentifierTableData,
Sebastian Redlc3632732010-10-05 15:59:54 +00001919 ASTIdentifierLookupTrait(*this, F));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001920 if (PP)
1921 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001922 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001923 break;
1924
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001925 case IDENTIFIER_OFFSET:
Sebastian Redl2da08f92010-07-19 22:28:42 +00001926 if (F.LocalNumIdentifiers != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001927 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00001928 return Failure;
1929 }
Sebastian Redl2da08f92010-07-19 22:28:42 +00001930 F.IdentifierOffsets = (const uint32_t *)BlobStart;
1931 F.LocalNumIdentifiers = Record[0];
Douglas Gregorafaf3082009-04-11 00:14:32 +00001932 break;
Douglas Gregorfdd01722009-04-14 00:24:19 +00001933
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001934 case EXTERNAL_DEFINITIONS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001935 // Optimization for the first block.
1936 if (ExternalDefinitions.empty())
1937 ExternalDefinitions.swap(Record);
1938 else
1939 ExternalDefinitions.insert(ExternalDefinitions.end(),
1940 Record.begin(), Record.end());
Douglas Gregorfdd01722009-04-14 00:24:19 +00001941 break;
Douglas Gregor3e1af842009-04-17 22:13:46 +00001942
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001943 case SPECIAL_TYPES:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001944 // Optimization for the first block
1945 if (SpecialTypes.empty())
1946 SpecialTypes.swap(Record);
1947 else
1948 SpecialTypes.insert(SpecialTypes.end(), Record.begin(), Record.end());
Douglas Gregorad1de002009-04-18 05:55:16 +00001949 break;
1950
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001951 case STATISTICS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001952 TotalNumStatements += Record[0];
1953 TotalNumMacros += Record[1];
1954 TotalLexicalDeclContexts += Record[2];
1955 TotalVisibleDeclContexts += Record[3];
Douglas Gregor3e1af842009-04-17 22:13:46 +00001956 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001957
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001958 case TENTATIVE_DEFINITIONS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001959 // Optimization for the first block.
1960 if (TentativeDefinitions.empty())
1961 TentativeDefinitions.swap(Record);
1962 else
1963 TentativeDefinitions.insert(TentativeDefinitions.end(),
1964 Record.begin(), Record.end());
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00001965 break;
Douglas Gregor14c22f22009-04-22 22:18:58 +00001966
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001967 case UNUSED_FILESCOPED_DECLS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001968 // Optimization for the first block.
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00001969 if (UnusedFileScopedDecls.empty())
1970 UnusedFileScopedDecls.swap(Record);
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001971 else
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00001972 UnusedFileScopedDecls.insert(UnusedFileScopedDecls.end(),
1973 Record.begin(), Record.end());
Tanya Lattnere6bbc012010-02-12 00:07:30 +00001974 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001975
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001976 case WEAK_UNDECLARED_IDENTIFIERS:
Sebastian Redl40566802010-08-05 18:21:25 +00001977 // Later blocks overwrite earlier ones.
1978 WeakUndeclaredIdentifiers.swap(Record);
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00001979 break;
1980
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001981 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001982 // Optimization for the first block.
1983 if (LocallyScopedExternalDecls.empty())
1984 LocallyScopedExternalDecls.swap(Record);
1985 else
1986 LocallyScopedExternalDecls.insert(LocallyScopedExternalDecls.end(),
1987 Record.begin(), Record.end());
Douglas Gregor14c22f22009-04-22 22:18:58 +00001988 break;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001989
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001990 case SELECTOR_OFFSETS:
Sebastian Redl059612d2010-08-03 21:58:15 +00001991 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redl725cd962010-08-04 20:40:17 +00001992 F.LocalNumSelectors = Record[0];
Douglas Gregor83941df2009-04-25 17:48:32 +00001993 break;
1994
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001995 case METHOD_POOL:
Sebastian Redl725cd962010-08-04 20:40:17 +00001996 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor83941df2009-04-25 17:48:32 +00001997 if (Record[0])
Sebastian Redl725cd962010-08-04 20:40:17 +00001998 F.SelectorLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001999 = ASTSelectorLookupTable::Create(
Sebastian Redl725cd962010-08-04 20:40:17 +00002000 F.SelectorLookupTableData + Record[0],
2001 F.SelectorLookupTableData,
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002002 ASTSelectorLookupTrait(*this));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002003 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002004 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002005
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002006 case REFERENCED_SELECTOR_POOL:
Sebastian Redlc3632732010-10-05 15:59:54 +00002007 F.ReferencedSelectorsData.swap(Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002008 break;
2009
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002010 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002011 if (!Record.empty() && Listener)
2012 Listener->ReadCounter(Record[0]);
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002013 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002014
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002015 case SOURCE_LOCATION_OFFSETS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002016 F.SLocOffsets = (const uint32_t *)BlobStart;
2017 F.LocalNumSLocEntries = Record[0];
Sebastian Redl8db9fae2010-09-22 20:19:08 +00002018 F.LocalSLocSize = Record[1];
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002019 break;
2020
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002021 case SOURCE_LOCATION_PRELOADS:
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002022 if (PreloadSLocEntries.empty())
2023 PreloadSLocEntries.swap(Record);
2024 else
2025 PreloadSLocEntries.insert(PreloadSLocEntries.end(),
2026 Record.begin(), Record.end());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002027 break;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002028
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002029 case STAT_CACHE: {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002030 ASTStatCache *MyStatCache =
2031 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
Douglas Gregor52e71082009-10-16 18:18:30 +00002032 (const unsigned char *)BlobStart,
2033 NumStatHits, NumStatMisses);
2034 FileMgr.addStatCache(MyStatCache);
Sebastian Redl9137a522010-07-16 17:50:48 +00002035 F.StatCache = MyStatCache;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002036 break;
Douglas Gregor52e71082009-10-16 18:18:30 +00002037 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002038
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002039 case EXT_VECTOR_DECLS:
Sebastian Redla9f23682010-07-28 21:38:49 +00002040 // Optimization for the first block.
2041 if (ExtVectorDecls.empty())
2042 ExtVectorDecls.swap(Record);
2043 else
2044 ExtVectorDecls.insert(ExtVectorDecls.end(),
2045 Record.begin(), Record.end());
Douglas Gregorb81c1702009-04-27 20:06:05 +00002046 break;
2047
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002048 case VTABLE_USES:
Sebastian Redl40566802010-08-05 18:21:25 +00002049 // Later tables overwrite earlier ones.
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002050 VTableUses.swap(Record);
2051 break;
2052
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002053 case DYNAMIC_CLASSES:
Sebastian Redl40566802010-08-05 18:21:25 +00002054 // Optimization for the first block.
2055 if (DynamicClasses.empty())
2056 DynamicClasses.swap(Record);
2057 else
2058 DynamicClasses.insert(DynamicClasses.end(),
2059 Record.begin(), Record.end());
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002060 break;
2061
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002062 case PENDING_IMPLICIT_INSTANTIATIONS:
Sebastian Redlc3632732010-10-05 15:59:54 +00002063 F.PendingInstantiations.swap(Record);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002064 break;
2065
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002066 case SEMA_DECL_REFS:
Sebastian Redl40566802010-08-05 18:21:25 +00002067 // Later tables overwrite earlier ones.
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002068 SemaDeclRefs.swap(Record);
2069 break;
2070
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002071 case ORIGINAL_FILE_NAME:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002072 // The primary AST will be the last to get here, so it will be the one
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002073 // that's used.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +00002074 ActualOriginalFileName.assign(BlobStart, BlobLen);
2075 OriginalFileName = ActualOriginalFileName;
Douglas Gregore650c8c2009-07-07 00:12:59 +00002076 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregorb64c1932009-05-12 01:31:05 +00002077 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002078
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002079 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek974be4d2010-02-12 23:31:14 +00002080 const std::string &CurBranch = getClangFullRepositoryVersion();
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002081 llvm::StringRef ASTBranch(BlobStart, BlobLen);
2082 if (llvm::StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2083 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregor445e23e2009-10-05 21:07:28 +00002084 return IgnorePCH;
2085 }
2086 break;
2087 }
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002088
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002089 case MACRO_DEFINITION_OFFSETS:
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002090 F.MacroDefinitionOffsets = (const uint32_t *)BlobStart;
2091 F.NumPreallocatedPreprocessingEntities = Record[0];
2092 F.LocalNumMacroDefinitions = Record[1];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002093 break;
Sebastian Redl0b17c612010-08-13 00:28:03 +00002094
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002095 case DECL_UPDATE_OFFSETS: {
2096 if (Record.size() % 2 != 0) {
2097 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2098 return Failure;
2099 }
2100 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
2101 DeclUpdateOffsets[static_cast<DeclID>(Record[I])]
2102 .push_back(std::make_pair(&F, Record[I+1]));
2103 break;
2104 }
2105
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002106 case DECL_REPLACEMENTS: {
Sebastian Redl0b17c612010-08-13 00:28:03 +00002107 if (Record.size() % 2 != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002108 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redl0b17c612010-08-13 00:28:03 +00002109 return Failure;
2110 }
2111 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002112 ReplacedDecls[static_cast<DeclID>(Record[I])] =
Sebastian Redl0b17c612010-08-13 00:28:03 +00002113 std::make_pair(&F, Record[I+1]);
2114 break;
2115 }
Douglas Gregor7c789c12010-10-29 22:39:52 +00002116
2117 case CXX_BASE_SPECIFIER_OFFSETS: {
2118 if (F.LocalNumCXXBaseSpecifiers != 0) {
2119 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2120 return Failure;
2121 }
2122
2123 F.LocalNumCXXBaseSpecifiers = Record[0];
2124 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
2125 break;
2126 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002127
2128 case DIAG_USER_MAPPINGS:
2129 if (Record.size() % 2 != 0) {
2130 Error("invalid DIAG_USER_MAPPINGS block in AST file");
2131 return Failure;
2132 }
2133 if (UserDiagMappings.empty())
2134 UserDiagMappings.swap(Record);
2135 else
2136 UserDiagMappings.insert(UserDiagMappings.end(),
2137 Record.begin(), Record.end());
2138 break;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002139 }
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002140 First = false;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002141 }
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002142 Error("premature end of bitstream in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002143 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002144}
2145
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002146ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
2147 ASTFileType Type) {
2148 switch(ReadASTCore(FileName, Type)) {
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002149 case Failure: return Failure;
2150 case IgnorePCH: return IgnorePCH;
2151 case Success: break;
2152 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002153
2154 // Here comes stuff that we only do once the entire chain is loaded.
2155
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002156 // Allocate space for loaded slocentries, identifiers, decls and types.
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002157 unsigned TotalNumIdentifiers = 0, TotalNumTypes = 0, TotalNumDecls = 0,
Sebastian Redl725cd962010-08-04 20:40:17 +00002158 TotalNumPreallocatedPreprocessingEntities = 0, TotalNumMacroDefs = 0,
2159 TotalNumSelectors = 0;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002160 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002161 TotalNumSLocEntries += Chain[I]->LocalNumSLocEntries;
Sebastian Redl8db9fae2010-09-22 20:19:08 +00002162 NextSLocOffset += Chain[I]->LocalSLocSize;
Sebastian Redl2da08f92010-07-19 22:28:42 +00002163 TotalNumIdentifiers += Chain[I]->LocalNumIdentifiers;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002164 TotalNumTypes += Chain[I]->LocalNumTypes;
2165 TotalNumDecls += Chain[I]->LocalNumDecls;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002166 TotalNumPreallocatedPreprocessingEntities +=
2167 Chain[I]->NumPreallocatedPreprocessingEntities;
2168 TotalNumMacroDefs += Chain[I]->LocalNumMacroDefinitions;
Sebastian Redl725cd962010-08-04 20:40:17 +00002169 TotalNumSelectors += Chain[I]->LocalNumSelectors;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002170 }
Sebastian Redl8db9fae2010-09-22 20:19:08 +00002171 SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, NextSLocOffset);
Sebastian Redl2da08f92010-07-19 22:28:42 +00002172 IdentifiersLoaded.resize(TotalNumIdentifiers);
Sebastian Redl12d6da02010-07-19 22:06:55 +00002173 TypesLoaded.resize(TotalNumTypes);
2174 DeclsLoaded.resize(TotalNumDecls);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002175 MacroDefinitionsLoaded.resize(TotalNumMacroDefs);
2176 if (PP) {
2177 if (TotalNumIdentifiers > 0)
2178 PP->getHeaderSearchInfo().SetExternalLookup(this);
2179 if (TotalNumPreallocatedPreprocessingEntities > 0) {
2180 if (!PP->getPreprocessingRecord())
2181 PP->createPreprocessingRecord();
2182 PP->getPreprocessingRecord()->SetExternalSource(*this,
2183 TotalNumPreallocatedPreprocessingEntities);
2184 }
2185 }
Sebastian Redl725cd962010-08-04 20:40:17 +00002186 SelectorsLoaded.resize(TotalNumSelectors);
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002187 // Preload SLocEntries.
2188 for (unsigned I = 0, N = PreloadSLocEntries.size(); I != N; ++I) {
2189 ASTReadResult Result = ReadSLocEntryRecord(PreloadSLocEntries[I]);
2190 if (Result != Success)
2191 return Result;
2192 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00002193
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002194 // Check the predefines buffers.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00002195 if (!DisableValidation && CheckPredefinesBuffers())
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002196 return IgnorePCH;
2197
2198 if (PP) {
2199 // Initialization of keywords and pragmas occurs before the
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002200 // AST file is read, so there may be some identifiers that were
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002201 // loaded into the IdentifierTable before we intercepted the
2202 // creation of identifiers. Iterate through the list of known
2203 // identifiers and determine whether we have to establish
2204 // preprocessor definitions or top-level identifier declaration
2205 // chains for those identifiers.
2206 //
2207 // We copy the IdentifierInfo pointers to a small vector first,
2208 // since de-serializing declarations or macro definitions can add
2209 // new entries into the identifier table, invalidating the
2210 // iterators.
2211 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
2212 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
2213 IdEnd = PP->getIdentifierTable().end();
2214 Id != IdEnd; ++Id)
2215 Identifiers.push_back(Id->second);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002216 // We need to search the tables in all files.
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002217 for (unsigned J = 0, M = Chain.size(); J != M; ++J) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002218 ASTIdentifierLookupTable *IdTable
2219 = (ASTIdentifierLookupTable *)Chain[J]->IdentifierLookupTable;
2220 // Not all AST files necessarily have identifier tables, only the useful
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00002221 // ones.
2222 if (!IdTable)
2223 continue;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002224 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2225 IdentifierInfo *II = Identifiers[I];
2226 // Look in the on-disk hash tables for an entry for this identifier
Sebastian Redlc3632732010-10-05 15:59:54 +00002227 ASTIdentifierLookupTrait Info(*this, *Chain[J], II);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002228 std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength());
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002229 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002230 if (Pos == IdTable->end())
2231 continue;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002232
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002233 // Dereferencing the iterator has the effect of populating the
2234 // IdentifierInfo node with the various declarations it needs.
2235 (void)*Pos;
2236 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002237 }
2238 }
2239
2240 if (Context)
2241 InitializeContext(*Context);
2242
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002243 if (DeserializationListener)
2244 DeserializationListener->ReaderInitialized(this);
2245
Douglas Gregor414cb642010-11-30 05:23:00 +00002246 // If this AST file is a precompiled preamble, then set the main file ID of
2247 // the source manager to the file source file from which the preamble was
2248 // built. This is the only valid way to use a precompiled preamble.
2249 if (Type == Preamble) {
2250 SourceLocation Loc
2251 = SourceMgr.getLocation(FileMgr.getFile(getOriginalSourceFile()), 1, 1);
2252 if (Loc.isValid()) {
2253 std::pair<FileID, unsigned> Decomposed = SourceMgr.getDecomposedLoc(Loc);
2254 SourceMgr.SetPreambleFileID(Decomposed.first);
2255 }
2256 }
2257
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002258 return Success;
2259}
2260
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002261ASTReader::ASTReadResult ASTReader::ReadASTCore(llvm::StringRef FileName,
2262 ASTFileType Type) {
Sebastian Redla866e652010-10-01 19:59:12 +00002263 PerFileData *Prev = Chain.empty() ? 0 : Chain.back();
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002264 Chain.push_back(new PerFileData(Type));
Sebastian Redl9137a522010-07-16 17:50:48 +00002265 PerFileData &F = *Chain.back();
Sebastian Redla866e652010-10-01 19:59:12 +00002266 if (Prev)
2267 Prev->NextInSource = &F;
2268 else
2269 FirstInSource = &F;
2270 F.Loaders.push_back(Prev);
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002271
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002272 // Set the AST file name.
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002273 F.FileName = FileName;
2274
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002275 // Open the AST file.
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002276 //
2277 // FIXME: This shouldn't be here, we should just take a raw_ostream.
2278 std::string ErrStr;
Michael J. Spencer3a321e22010-12-09 17:36:38 +00002279 llvm::error_code ec;
2280 if (FileName == "-") {
2281 F.Buffer.reset(llvm::MemoryBuffer::getSTDIN(ec));
2282 if (ec)
2283 ErrStr = ec.message();
2284 } else
Chris Lattner39b49bc2010-11-23 08:35:12 +00002285 F.Buffer.reset(FileMgr.getBufferForFile(FileName, &ErrStr));
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002286 if (!F.Buffer) {
2287 Error(ErrStr.c_str());
2288 return IgnorePCH;
2289 }
2290
2291 // Initialize the stream
2292 F.StreamFile.init((const unsigned char *)F.Buffer->getBufferStart(),
2293 (const unsigned char *)F.Buffer->getBufferEnd());
Sebastian Redl9137a522010-07-16 17:50:48 +00002294 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002295 Stream.init(F.StreamFile);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002296 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002297
2298 // Sniff for the signature.
2299 if (Stream.Read(8) != 'C' ||
2300 Stream.Read(8) != 'P' ||
2301 Stream.Read(8) != 'C' ||
2302 Stream.Read(8) != 'H') {
2303 Diag(diag::err_not_a_pch_file) << FileName;
2304 return Failure;
2305 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002306
Douglas Gregor2cf26342009-04-09 22:27:44 +00002307 while (!Stream.AtEndOfStream()) {
2308 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002309
Douglas Gregore1d918e2009-04-10 23:10:45 +00002310 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002311 Error("invalid record at top-level of AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002312 return Failure;
2313 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002314
2315 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregor668c1a42009-04-21 22:25:48 +00002316
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002317 // We only know the AST subblock ID.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002318 switch (BlockID) {
2319 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002320 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002321 Error("malformed BlockInfoBlock in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002322 return Failure;
2323 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002324 break;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002325 case AST_BLOCK_ID:
Sebastian Redl571db7f2010-08-18 23:56:56 +00002326 switch (ReadASTBlock(F)) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002327 case Success:
2328 break;
2329
2330 case Failure:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002331 return Failure;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002332
2333 case IgnorePCH:
Douglas Gregor2bec0412009-04-10 21:16:55 +00002334 // FIXME: We could consider reading through to the end of this
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002335 // AST block, skipping subblocks, to see if there are other
2336 // AST blocks elsewhere.
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002337
2338 // Clear out any preallocated source location entries, so that
2339 // the source manager does not try to resolve them later.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002340 SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002341
2342 // Remove the stat cache.
Sebastian Redl9137a522010-07-16 17:50:48 +00002343 if (F.StatCache)
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002344 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002345
Douglas Gregore1d918e2009-04-10 23:10:45 +00002346 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002347 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002348 break;
2349 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002350 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002351 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002352 return Failure;
2353 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002354 break;
2355 }
Mike Stump1eb44332009-09-09 15:08:12 +00002356 }
2357
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002358 return Success;
2359}
2360
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002361void ASTReader::setPreprocessor(Preprocessor &pp) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002362 PP = &pp;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002363
2364 unsigned TotalNum = 0;
2365 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
2366 TotalNum += Chain[I]->NumPreallocatedPreprocessingEntities;
2367 if (TotalNum) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002368 if (!PP->getPreprocessingRecord())
2369 PP->createPreprocessingRecord();
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002370 PP->getPreprocessingRecord()->SetExternalSource(*this, TotalNum);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002371 }
2372}
2373
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002374void ASTReader::InitializeContext(ASTContext &Ctx) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002375 Context = &Ctx;
2376 assert(Context && "Passed null context!");
2377
2378 assert(PP && "Forgot to set Preprocessor ?");
2379 PP->getIdentifierTable().setExternalIdentifierLookup(this);
2380 PP->getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor88a35862010-01-04 19:18:44 +00002381 PP->setExternalSource(this);
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002382
Douglas Gregor3747ee72010-10-01 01:18:02 +00002383 // If we have an update block for the TU waiting, we have to add it before
2384 // deserializing the decl.
2385 DeclContextOffsetsMap::iterator DCU = DeclContextOffsets.find(0);
2386 if (DCU != DeclContextOffsets.end()) {
2387 // Insertion could invalidate map, so grab vector.
2388 DeclContextInfos T;
2389 T.swap(DCU->second);
2390 DeclContextOffsets.erase(DCU);
2391 DeclContextOffsets[Ctx.getTranslationUnitDecl()].swap(T);
2392 }
2393
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002394 // Load the translation unit declaration
Argyrios Kyrtzidis8871a442010-07-08 17:13:02 +00002395 GetTranslationUnitDecl();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002396
2397 // Load the special types.
2398 Context->setBuiltinVaListType(
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002399 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
2400 if (unsigned Id = SpecialTypes[SPECIAL_TYPE_OBJC_ID])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002401 Context->setObjCIdType(GetType(Id));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002402 if (unsigned Sel = SpecialTypes[SPECIAL_TYPE_OBJC_SELECTOR])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002403 Context->setObjCSelType(GetType(Sel));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002404 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002405 Context->setObjCProtoType(GetType(Proto));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002406 if (unsigned Class = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002407 Context->setObjCClassType(GetType(Class));
Steve Naroff14108da2009-07-10 23:34:53 +00002408
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002409 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002410 Context->setCFConstantStringType(GetType(String));
Mike Stump1eb44332009-09-09 15:08:12 +00002411 if (unsigned FastEnum
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002412 = SpecialTypes[SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002413 Context->setObjCFastEnumerationStateType(GetType(FastEnum));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002414 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002415 QualType FileType = GetType(File);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002416 if (FileType.isNull()) {
2417 Error("FILE type is NULL");
2418 return;
2419 }
John McCall183700f2009-09-21 23:43:11 +00002420 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002421 Context->setFILEDecl(Typedef->getDecl());
2422 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002423 const TagType *Tag = FileType->getAs<TagType>();
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002424 if (!Tag) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002425 Error("Invalid FILE type in AST file");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002426 return;
2427 }
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002428 Context->setFILEDecl(Tag->getDecl());
2429 }
2430 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002431 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
Mike Stump782fa302009-07-28 02:25:19 +00002432 QualType Jmp_bufType = GetType(Jmp_buf);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002433 if (Jmp_bufType.isNull()) {
2434 Error("jmp_bug type is NULL");
2435 return;
2436 }
John McCall183700f2009-09-21 23:43:11 +00002437 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
Mike Stump782fa302009-07-28 02:25:19 +00002438 Context->setjmp_bufDecl(Typedef->getDecl());
2439 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002440 const TagType *Tag = Jmp_bufType->getAs<TagType>();
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002441 if (!Tag) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002442 Error("Invalid jmp_buf type in AST file");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002443 return;
2444 }
Mike Stump782fa302009-07-28 02:25:19 +00002445 Context->setjmp_bufDecl(Tag->getDecl());
2446 }
2447 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002448 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
Mike Stump782fa302009-07-28 02:25:19 +00002449 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002450 if (Sigjmp_bufType.isNull()) {
2451 Error("sigjmp_buf type is NULL");
2452 return;
2453 }
John McCall183700f2009-09-21 23:43:11 +00002454 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
Mike Stump782fa302009-07-28 02:25:19 +00002455 Context->setsigjmp_bufDecl(Typedef->getDecl());
2456 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002457 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002458 assert(Tag && "Invalid sigjmp_buf type in AST file");
Mike Stump782fa302009-07-28 02:25:19 +00002459 Context->setsigjmp_bufDecl(Tag->getDecl());
2460 }
2461 }
Mike Stump1eb44332009-09-09 15:08:12 +00002462 if (unsigned ObjCIdRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002463 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION])
Douglas Gregord1571ac2009-08-21 00:27:50 +00002464 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
Mike Stump1eb44332009-09-09 15:08:12 +00002465 if (unsigned ObjCClassRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002466 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION])
Douglas Gregord1571ac2009-08-21 00:27:50 +00002467 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002468 if (unsigned String = SpecialTypes[SPECIAL_TYPE_BLOCK_DESCRIPTOR])
Mike Stumpadaaad32009-10-20 02:12:22 +00002469 Context->setBlockDescriptorType(GetType(String));
Mike Stump083c25e2009-10-22 00:49:09 +00002470 if (unsigned String
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002471 = SpecialTypes[SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR])
Mike Stump083c25e2009-10-22 00:49:09 +00002472 Context->setBlockDescriptorExtendedType(GetType(String));
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002473 if (unsigned ObjCSelRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002474 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION])
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002475 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002476 if (unsigned String = SpecialTypes[SPECIAL_TYPE_NS_CONSTANT_STRING])
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002477 Context->setNSConstantStringType(GetType(String));
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002478
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002479 if (SpecialTypes[SPECIAL_TYPE_INT128_INSTALLED])
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002480 Context->setInt128Installed();
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002481
2482 ReadUserDiagnosticMappings(Context->getDiagnostics());
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002483}
2484
Douglas Gregorb64c1932009-05-12 01:31:05 +00002485/// \brief Retrieve the name of the original source file name
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002486/// directly from the AST file, without actually loading the AST
Douglas Gregorb64c1932009-05-12 01:31:05 +00002487/// file.
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002488std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00002489 FileManager &FileMgr,
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00002490 Diagnostic &Diags) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002491 // Open the AST file.
Douglas Gregorb64c1932009-05-12 01:31:05 +00002492 std::string ErrStr;
2493 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner39b49bc2010-11-23 08:35:12 +00002494 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregorb64c1932009-05-12 01:31:05 +00002495 if (!Buffer) {
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00002496 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002497 return std::string();
2498 }
2499
2500 // Initialize the stream
2501 llvm::BitstreamReader StreamFile;
2502 llvm::BitstreamCursor Stream;
Mike Stump1eb44332009-09-09 15:08:12 +00002503 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregorb64c1932009-05-12 01:31:05 +00002504 (const unsigned char *)Buffer->getBufferEnd());
2505 Stream.init(StreamFile);
2506
2507 // Sniff for the signature.
2508 if (Stream.Read(8) != 'C' ||
2509 Stream.Read(8) != 'P' ||
2510 Stream.Read(8) != 'C' ||
2511 Stream.Read(8) != 'H') {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002512 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002513 return std::string();
2514 }
2515
2516 RecordData Record;
2517 while (!Stream.AtEndOfStream()) {
2518 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002519
Douglas Gregorb64c1932009-05-12 01:31:05 +00002520 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2521 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump1eb44332009-09-09 15:08:12 +00002522
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002523 // We only know the AST subblock ID.
Douglas Gregorb64c1932009-05-12 01:31:05 +00002524 switch (BlockID) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002525 case AST_BLOCK_ID:
2526 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002527 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002528 return std::string();
2529 }
2530 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002531
Douglas Gregorb64c1932009-05-12 01:31:05 +00002532 default:
2533 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002534 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002535 return std::string();
2536 }
2537 break;
2538 }
2539 continue;
2540 }
2541
2542 if (Code == llvm::bitc::END_BLOCK) {
2543 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002544 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002545 return std::string();
2546 }
2547 continue;
2548 }
2549
2550 if (Code == llvm::bitc::DEFINE_ABBREV) {
2551 Stream.ReadAbbrevRecord();
2552 continue;
2553 }
2554
2555 Record.clear();
2556 const char *BlobStart = 0;
2557 unsigned BlobLen = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002558 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002559 == ORIGINAL_FILE_NAME)
Douglas Gregorb64c1932009-05-12 01:31:05 +00002560 return std::string(BlobStart, BlobLen);
Mike Stump1eb44332009-09-09 15:08:12 +00002561 }
Douglas Gregorb64c1932009-05-12 01:31:05 +00002562
2563 return std::string();
2564}
2565
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002566/// \brief Parse the record that corresponds to a LangOptions data
2567/// structure.
2568///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002569/// This routine parses the language options from the AST file and then gives
2570/// them to the AST listener if one is set.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002571///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002572/// \returns true if the listener deems the file unacceptable, false otherwise.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002573bool ASTReader::ParseLanguageOptions(
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002574 const llvm::SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002575 if (Listener) {
2576 LangOptions LangOpts;
Mike Stump1eb44332009-09-09 15:08:12 +00002577
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002578 #define PARSE_LANGOPT(Option) \
2579 LangOpts.Option = Record[Idx]; \
2580 ++Idx
Mike Stump1eb44332009-09-09 15:08:12 +00002581
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002582 unsigned Idx = 0;
2583 PARSE_LANGOPT(Trigraphs);
2584 PARSE_LANGOPT(BCPLComment);
2585 PARSE_LANGOPT(DollarIdents);
2586 PARSE_LANGOPT(AsmPreprocessor);
2587 PARSE_LANGOPT(GNUMode);
Chandler Carrutheb5d7b72010-04-17 20:17:31 +00002588 PARSE_LANGOPT(GNUKeywords);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002589 PARSE_LANGOPT(ImplicitInt);
2590 PARSE_LANGOPT(Digraphs);
2591 PARSE_LANGOPT(HexFloats);
2592 PARSE_LANGOPT(C99);
2593 PARSE_LANGOPT(Microsoft);
2594 PARSE_LANGOPT(CPlusPlus);
2595 PARSE_LANGOPT(CPlusPlus0x);
2596 PARSE_LANGOPT(CXXOperatorNames);
2597 PARSE_LANGOPT(ObjC1);
2598 PARSE_LANGOPT(ObjC2);
2599 PARSE_LANGOPT(ObjCNonFragileABI);
Fariborz Jahanian412e7982010-02-09 19:31:38 +00002600 PARSE_LANGOPT(ObjCNonFragileABI2);
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +00002601 PARSE_LANGOPT(NoConstantCFStrings);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002602 PARSE_LANGOPT(PascalStrings);
2603 PARSE_LANGOPT(WritableStrings);
2604 PARSE_LANGOPT(LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +00002605 PARSE_LANGOPT(AltiVec);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002606 PARSE_LANGOPT(Exceptions);
Daniel Dunbar73482882010-02-10 18:48:44 +00002607 PARSE_LANGOPT(SjLjExceptions);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002608 PARSE_LANGOPT(NeXTRuntime);
2609 PARSE_LANGOPT(Freestanding);
2610 PARSE_LANGOPT(NoBuiltin);
2611 PARSE_LANGOPT(ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +00002612 PARSE_LANGOPT(POSIXThreads);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002613 PARSE_LANGOPT(Blocks);
2614 PARSE_LANGOPT(EmitAllDecls);
2615 PARSE_LANGOPT(MathErrno);
Chris Lattnera4d71452010-06-26 21:25:03 +00002616 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
2617 Record[Idx++]);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002618 PARSE_LANGOPT(HeinousExtensions);
2619 PARSE_LANGOPT(Optimize);
2620 PARSE_LANGOPT(OptimizeSize);
2621 PARSE_LANGOPT(Static);
2622 PARSE_LANGOPT(PICLevel);
2623 PARSE_LANGOPT(GNUInline);
2624 PARSE_LANGOPT(NoInline);
2625 PARSE_LANGOPT(AccessControl);
2626 PARSE_LANGOPT(CharIsSigned);
John Thompsona6fda122009-11-05 20:14:16 +00002627 PARSE_LANGOPT(ShortWChar);
Chris Lattnera4d71452010-06-26 21:25:03 +00002628 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
John McCall1fb0caa2010-10-22 21:05:15 +00002629 LangOpts.setVisibilityMode((Visibility)Record[Idx++]);
Daniel Dunbarab8e2812009-09-21 04:16:19 +00002630 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
Chris Lattnera4d71452010-06-26 21:25:03 +00002631 Record[Idx++]);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002632 PARSE_LANGOPT(InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +00002633 PARSE_LANGOPT(OpenCL);
Peter Collingbourne08a53262010-12-01 19:14:57 +00002634 PARSE_LANGOPT(CUDA);
Mike Stump9c276ae2009-12-12 01:27:46 +00002635 PARSE_LANGOPT(CatchUndefined);
2636 // FIXME: Missing ElideConstructors?!
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002637 #undef PARSE_LANGOPT
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002638
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002639 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002640 }
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002641
2642 return false;
2643}
2644
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002645void ASTReader::ReadPreprocessedEntities() {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002646 ReadDefinedMacros();
2647}
2648
Douglas Gregor89d99802010-11-30 06:16:57 +00002649PreprocessedEntity *ASTReader::ReadPreprocessedEntity(uint64_t Offset) {
2650 PerFileData *F = 0;
2651 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2652 if (Offset < Chain[I]->SizeInBits) {
2653 F = Chain[I];
2654 break;
2655 }
2656
2657 Offset -= Chain[I]->SizeInBits;
2658 }
2659
2660 if (!F) {
2661 Error("Malformed preprocessed entity offset");
2662 return 0;
2663 }
2664
2665 return ReadMacroRecord(*F, Offset);
2666}
2667
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002668void ASTReader::ReadUserDiagnosticMappings(Diagnostic &Diag) {
2669 unsigned Idx = 0;
2670 while (Idx < UserDiagMappings.size()) {
2671 unsigned DiagID = UserDiagMappings[Idx++];
2672 unsigned Map = UserDiagMappings[Idx++];
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00002673 Diag.setDiagnosticMappingInternal(DiagID, Map, Diag.GetCurDiagState(),
2674 /*isUser=*/true);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002675 }
2676}
2677
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002678/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002679ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002680 PerFileData *F = 0;
2681 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2682 F = Chain[N - I - 1];
2683 if (Index < F->LocalNumTypes)
2684 break;
2685 Index -= F->LocalNumTypes;
2686 }
2687 assert(F && F->LocalNumTypes > Index && "Broken chain");
Sebastian Redlc3632732010-10-05 15:59:54 +00002688 return RecordLocation(F, F->TypeOffsets[Index]);
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002689}
2690
2691/// \brief Read and return the type with the given index..
Douglas Gregor2cf26342009-04-09 22:27:44 +00002692///
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002693/// The index is the type ID, shifted and minus the number of predefs. This
2694/// routine actually reads the record corresponding to the type at the given
2695/// location. It is a helper routine for GetType, which deals with reading type
2696/// IDs.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002697QualType ASTReader::ReadTypeRecord(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002698 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00002699 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00002700
Douglas Gregor0b748912009-04-14 21:18:50 +00002701 // Keep track of where we are in the stream, then jump back there
2702 // after reading this type.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002703 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregor0b748912009-04-14 21:18:50 +00002704
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00002705 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redl27372b42010-08-11 18:52:41 +00002706
Douglas Gregord89275b2009-07-06 18:54:52 +00002707 // Note that we are loading a type record.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00002708 Deserializing AType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00002709
Sebastian Redlc3632732010-10-05 15:59:54 +00002710 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002711 RecordData Record;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002712 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002713 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
2714 case TYPE_EXT_QUAL: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002715 if (Record.size() != 2) {
2716 Error("Incorrect encoding of extended qualifier type");
2717 return QualType();
2718 }
Douglas Gregor6d473962009-04-15 22:00:08 +00002719 QualType Base = GetType(Record[0]);
John McCall0953e762009-09-24 19:53:00 +00002720 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]);
2721 return Context->getQualifiedType(Base, Quals);
Douglas Gregor6d473962009-04-15 22:00:08 +00002722 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002723
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002724 case TYPE_COMPLEX: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002725 if (Record.size() != 1) {
2726 Error("Incorrect encoding of complex type");
2727 return QualType();
2728 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002729 QualType ElemType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002730 return Context->getComplexType(ElemType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002731 }
2732
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002733 case TYPE_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002734 if (Record.size() != 1) {
2735 Error("Incorrect encoding of pointer type");
2736 return QualType();
2737 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002738 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002739 return Context->getPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002740 }
2741
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002742 case TYPE_BLOCK_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002743 if (Record.size() != 1) {
2744 Error("Incorrect encoding of block pointer type");
2745 return QualType();
2746 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002747 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002748 return Context->getBlockPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002749 }
2750
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002751 case TYPE_LVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002752 if (Record.size() != 1) {
2753 Error("Incorrect encoding of lvalue reference type");
2754 return QualType();
2755 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002756 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002757 return Context->getLValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002758 }
2759
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002760 case TYPE_RVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002761 if (Record.size() != 1) {
2762 Error("Incorrect encoding of rvalue reference type");
2763 return QualType();
2764 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002765 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002766 return Context->getRValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002767 }
2768
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002769 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidis240437b2010-07-02 11:55:15 +00002770 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002771 Error("Incorrect encoding of member pointer type");
2772 return QualType();
2773 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002774 QualType PointeeType = GetType(Record[0]);
2775 QualType ClassType = GetType(Record[1]);
Douglas Gregor1ab55e92010-12-10 17:03:06 +00002776 if (PointeeType.isNull() || ClassType.isNull())
2777 return QualType();
2778
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002779 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002780 }
2781
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002782 case TYPE_CONSTANT_ARRAY: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002783 QualType ElementType = GetType(Record[0]);
2784 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2785 unsigned IndexTypeQuals = Record[2];
2786 unsigned Idx = 3;
2787 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002788 return Context->getConstantArrayType(ElementType, Size,
2789 ASM, IndexTypeQuals);
2790 }
2791
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002792 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002793 QualType ElementType = GetType(Record[0]);
2794 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2795 unsigned IndexTypeQuals = Record[2];
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002796 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002797 }
2798
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002799 case TYPE_VARIABLE_ARRAY: {
Douglas Gregor0b748912009-04-14 21:18:50 +00002800 QualType ElementType = GetType(Record[0]);
2801 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2802 unsigned IndexTypeQuals = Record[2];
Sebastian Redlc3632732010-10-05 15:59:54 +00002803 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
2804 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
2805 return Context->getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002806 ASM, IndexTypeQuals,
2807 SourceRange(LBLoc, RBLoc));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002808 }
2809
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002810 case TYPE_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00002811 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002812 Error("incorrect encoding of vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002813 return QualType();
2814 }
2815
2816 QualType ElementType = GetType(Record[0]);
2817 unsigned NumElements = Record[1];
Bob Wilsone86d78c2010-11-10 21:56:12 +00002818 unsigned VecKind = Record[2];
Chris Lattner788b0fd2010-06-23 06:00:24 +00002819 return Context->getVectorType(ElementType, NumElements,
Bob Wilsone86d78c2010-11-10 21:56:12 +00002820 (VectorType::VectorKind)VecKind);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002821 }
2822
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002823 case TYPE_EXT_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00002824 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002825 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002826 return QualType();
2827 }
2828
2829 QualType ElementType = GetType(Record[0]);
2830 unsigned NumElements = Record[1];
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002831 return Context->getExtVectorType(ElementType, NumElements);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002832 }
2833
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002834 case TYPE_FUNCTION_NO_PROTO: {
Rafael Espindola425ef722010-03-30 22:15:11 +00002835 if (Record.size() != 4) {
Douglas Gregora02b1472009-04-28 21:53:25 +00002836 Error("incorrect encoding of no-proto function type");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002837 return QualType();
2838 }
2839 QualType ResultType = GetType(Record[0]);
Rafael Espindola425ef722010-03-30 22:15:11 +00002840 FunctionType::ExtInfo Info(Record[1], Record[2], (CallingConv)Record[3]);
Rafael Espindola264ba482010-03-30 20:24:48 +00002841 return Context->getFunctionNoProtoType(ResultType, Info);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002842 }
2843
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002844 case TYPE_FUNCTION_PROTO: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002845 QualType ResultType = GetType(Record[0]);
John McCalle23cf432010-12-14 08:05:40 +00002846
2847 FunctionProtoType::ExtProtoInfo EPI;
2848 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
2849 /*regparm*/ Record[2],
2850 static_cast<CallingConv>(Record[3]));
2851
Rafael Espindola425ef722010-03-30 22:15:11 +00002852 unsigned Idx = 4;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002853 unsigned NumParams = Record[Idx++];
2854 llvm::SmallVector<QualType, 16> ParamTypes;
2855 for (unsigned I = 0; I != NumParams; ++I)
2856 ParamTypes.push_back(GetType(Record[Idx++]));
John McCalle23cf432010-12-14 08:05:40 +00002857
2858 EPI.Variadic = Record[Idx++];
2859 EPI.TypeQuals = Record[Idx++];
2860 EPI.HasExceptionSpec = Record[Idx++];
2861 EPI.HasAnyExceptionSpec = Record[Idx++];
2862 EPI.NumExceptions = Record[Idx++];
Sebastian Redl465226e2009-05-27 22:11:52 +00002863 llvm::SmallVector<QualType, 2> Exceptions;
John McCalle23cf432010-12-14 08:05:40 +00002864 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
Sebastian Redl465226e2009-05-27 22:11:52 +00002865 Exceptions.push_back(GetType(Record[Idx++]));
John McCalle23cf432010-12-14 08:05:40 +00002866 EPI.Exceptions = Exceptions.data();
Jay Foadbeaaccd2009-05-21 09:52:38 +00002867 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
John McCalle23cf432010-12-14 08:05:40 +00002868 EPI);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002869 }
2870
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002871 case TYPE_UNRESOLVED_USING:
John McCalled976492009-12-04 22:46:56 +00002872 return Context->getTypeDeclType(
2873 cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0])));
2874
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002875 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002876 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002877 Error("incorrect encoding of typedef type");
2878 return QualType();
2879 }
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002880 TypedefDecl *Decl = cast<TypedefDecl>(GetDecl(Record[0]));
2881 QualType Canonical = GetType(Record[1]);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00002882 if (!Canonical.isNull())
2883 Canonical = Context->getCanonicalType(Canonical);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002884 return Context->getTypedefType(Decl, Canonical);
2885 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002886
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002887 case TYPE_TYPEOF_EXPR:
Sebastian Redlc3632732010-10-05 15:59:54 +00002888 return Context->getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002889
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002890 case TYPE_TYPEOF: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002891 if (Record.size() != 1) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002892 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002893 return QualType();
2894 }
2895 QualType UnderlyingType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002896 return Context->getTypeOfType(UnderlyingType);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002897 }
Mike Stump1eb44332009-09-09 15:08:12 +00002898
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002899 case TYPE_DECLTYPE:
Sebastian Redlc3632732010-10-05 15:59:54 +00002900 return Context->getDecltypeType(ReadExpr(*Loc.F));
Anders Carlsson395b4752009-06-24 19:06:50 +00002901
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002902 case TYPE_RECORD: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002903 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002904 Error("incorrect encoding of record type");
2905 return QualType();
2906 }
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002907 bool IsDependent = Record[0];
2908 QualType T = Context->getRecordType(cast<RecordDecl>(GetDecl(Record[1])));
John McCallb870b882010-10-14 21:48:26 +00002909 T->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002910 return T;
2911 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002912
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002913 case TYPE_ENUM: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002914 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002915 Error("incorrect encoding of enum type");
2916 return QualType();
2917 }
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002918 bool IsDependent = Record[0];
2919 QualType T = Context->getEnumType(cast<EnumDecl>(GetDecl(Record[1])));
John McCallb870b882010-10-14 21:48:26 +00002920 T->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002921 return T;
2922 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002923
Abramo Bagnara075f8f12010-12-10 16:29:40 +00002924 case TYPE_PAREN: {
2925 if (Record.size() != 1) {
2926 Error("incorrect encoding of paren type");
2927 return QualType();
2928 }
2929 QualType InnerType = GetType(Record[0]);
2930 return Context->getParenType(InnerType);
2931 }
2932
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002933 case TYPE_ELABORATED: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00002934 unsigned Idx = 0;
2935 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2936 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2937 QualType NamedType = GetType(Record[Idx++]);
2938 return Context->getElaboratedType(Keyword, NNS, NamedType);
John McCall7da24312009-09-05 00:15:47 +00002939 }
2940
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002941 case TYPE_OBJC_INTERFACE: {
Chris Lattnerc6fa4452009-04-22 06:45:28 +00002942 unsigned Idx = 0;
2943 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
John McCallc12c5bb2010-05-15 11:32:37 +00002944 return Context->getObjCInterfaceType(ItfD);
2945 }
2946
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002947 case TYPE_OBJC_OBJECT: {
John McCallc12c5bb2010-05-15 11:32:37 +00002948 unsigned Idx = 0;
2949 QualType Base = GetType(Record[Idx++]);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00002950 unsigned NumProtos = Record[Idx++];
2951 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2952 for (unsigned I = 0; I != NumProtos; ++I)
2953 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer20249a12010-10-21 03:16:25 +00002954 return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00002955 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002956
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002957 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00002958 unsigned Idx = 0;
John McCallc12c5bb2010-05-15 11:32:37 +00002959 QualType Pointee = GetType(Record[Idx++]);
2960 return Context->getObjCObjectPointerType(Pointee);
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00002961 }
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +00002962
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002963 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCall49a832b2009-10-18 09:09:24 +00002964 unsigned Idx = 0;
2965 QualType Parm = GetType(Record[Idx++]);
2966 QualType Replacement = GetType(Record[Idx++]);
2967 return
2968 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
2969 Replacement);
2970 }
John McCall3cb0ebd2010-03-10 03:28:59 +00002971
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002972 case TYPE_INJECTED_CLASS_NAME: {
John McCall3cb0ebd2010-03-10 03:28:59 +00002973 CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0]));
2974 QualType TST = GetType(Record[1]); // probably derivable
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00002975 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002976 // for AST reading, too much interdependencies.
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00002977 return
2978 QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCall3cb0ebd2010-03-10 03:28:59 +00002979 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00002980
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002981 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002982 unsigned Idx = 0;
2983 unsigned Depth = Record[Idx++];
2984 unsigned Index = Record[Idx++];
2985 bool Pack = Record[Idx++];
2986 IdentifierInfo *Name = GetIdentifierInfo(Record, Idx);
2987 return Context->getTemplateTypeParmType(Depth, Index, Pack, Name);
2988 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00002989
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002990 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00002991 unsigned Idx = 0;
2992 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2993 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2994 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +00002995 QualType Canon = GetType(Record[Idx++]);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00002996 if (!Canon.isNull())
2997 Canon = Context->getCanonicalType(Canon);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +00002998 return Context->getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00002999 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003000
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003001 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003002 unsigned Idx = 0;
3003 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3004 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3005 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
3006 unsigned NumArgs = Record[Idx++];
3007 llvm::SmallVector<TemplateArgument, 8> Args;
3008 Args.reserve(NumArgs);
3009 while (NumArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00003010 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003011 return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
3012 Args.size(), Args.data());
3013 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003014
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003015 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00003016 unsigned Idx = 0;
3017
3018 // ArrayType
3019 QualType ElementType = GetType(Record[Idx++]);
3020 ArrayType::ArraySizeModifier ASM
3021 = (ArrayType::ArraySizeModifier)Record[Idx++];
3022 unsigned IndexTypeQuals = Record[Idx++];
3023
3024 // DependentSizedArrayType
Sebastian Redlc3632732010-10-05 15:59:54 +00003025 Expr *NumElts = ReadExpr(*Loc.F);
3026 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00003027
3028 return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
3029 IndexTypeQuals, Brackets);
3030 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003031
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003032 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003033 unsigned Idx = 0;
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003034 bool IsDependent = Record[Idx++];
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003035 TemplateName Name = ReadTemplateName(Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003036 llvm::SmallVector<TemplateArgument, 8> Args;
Sebastian Redlc3632732010-10-05 15:59:54 +00003037 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003038 QualType Canon = GetType(Record[Idx++]);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003039 QualType T;
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003040 if (Canon.isNull())
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003041 T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(),
3042 Args.size());
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003043 else
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003044 T = Context->getTemplateSpecializationType(Name, Args.data(),
3045 Args.size(), Canon);
John McCallb870b882010-10-14 21:48:26 +00003046 T->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003047 return T;
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003048 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003049 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003050 // Suppress a GCC warning
3051 return QualType();
3052}
3053
Sebastian Redlc3632732010-10-05 15:59:54 +00003054class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003055 ASTReader &Reader;
Sebastian Redlc3632732010-10-05 15:59:54 +00003056 ASTReader::PerFileData &F;
Sebastian Redl577d4792010-07-22 22:43:28 +00003057 llvm::BitstreamCursor &DeclsCursor;
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003058 const ASTReader::RecordData &Record;
John McCalla1ee0c52009-10-16 21:56:05 +00003059 unsigned &Idx;
3060
Sebastian Redlc3632732010-10-05 15:59:54 +00003061 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
3062 unsigned &I) {
3063 return Reader.ReadSourceLocation(F, R, I);
3064 }
3065
John McCalla1ee0c52009-10-16 21:56:05 +00003066public:
Sebastian Redlc3632732010-10-05 15:59:54 +00003067 TypeLocReader(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003068 const ASTReader::RecordData &Record, unsigned &Idx)
Sebastian Redlc3632732010-10-05 15:59:54 +00003069 : Reader(Reader), F(F), DeclsCursor(F.DeclsCursor), Record(Record), Idx(Idx)
3070 { }
John McCalla1ee0c52009-10-16 21:56:05 +00003071
John McCall51bd8032009-10-18 01:05:36 +00003072 // We want compile-time assurance that we've enumerated all of
3073 // these, so unfortunately we have to declare them first, then
3074 // define them out-of-line.
3075#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +00003076#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +00003077 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00003078#include "clang/AST/TypeLocNodes.def"
3079
John McCall51bd8032009-10-18 01:05:36 +00003080 void VisitFunctionTypeLoc(FunctionTypeLoc);
3081 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00003082};
3083
John McCall51bd8032009-10-18 01:05:36 +00003084void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCalla1ee0c52009-10-16 21:56:05 +00003085 // nothing to do
3086}
John McCall51bd8032009-10-18 01:05:36 +00003087void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003088 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorddf889a2010-01-18 18:04:31 +00003089 if (TL.needsExtraLocalData()) {
3090 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
3091 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
3092 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
3093 TL.setModeAttr(Record[Idx++]);
3094 }
John McCalla1ee0c52009-10-16 21:56:05 +00003095}
John McCall51bd8032009-10-18 01:05:36 +00003096void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003097 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003098}
John McCall51bd8032009-10-18 01:05:36 +00003099void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003100 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003101}
John McCall51bd8032009-10-18 01:05:36 +00003102void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003103 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003104}
John McCall51bd8032009-10-18 01:05:36 +00003105void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003106 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003107}
John McCall51bd8032009-10-18 01:05:36 +00003108void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003109 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003110}
John McCall51bd8032009-10-18 01:05:36 +00003111void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003112 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003113}
John McCall51bd8032009-10-18 01:05:36 +00003114void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003115 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
3116 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003117 if (Record[Idx++])
Sebastian Redlc3632732010-10-05 15:59:54 +00003118 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003119 else
John McCall51bd8032009-10-18 01:05:36 +00003120 TL.setSizeExpr(0);
3121}
3122void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
3123 VisitArrayTypeLoc(TL);
3124}
3125void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
3126 VisitArrayTypeLoc(TL);
3127}
3128void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
3129 VisitArrayTypeLoc(TL);
3130}
3131void TypeLocReader::VisitDependentSizedArrayTypeLoc(
3132 DependentSizedArrayTypeLoc TL) {
3133 VisitArrayTypeLoc(TL);
3134}
3135void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
3136 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003137 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003138}
3139void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003140 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003141}
3142void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003143 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003144}
3145void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003146 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3147 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
Douglas Gregordab60ad2010-10-01 18:44:50 +00003148 TL.setTrailingReturn(Record[Idx++]);
John McCall51bd8032009-10-18 01:05:36 +00003149 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
John McCall86acc2a2009-10-23 01:28:53 +00003150 TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
John McCall51bd8032009-10-18 01:05:36 +00003151 }
3152}
3153void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
3154 VisitFunctionTypeLoc(TL);
3155}
3156void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
3157 VisitFunctionTypeLoc(TL);
3158}
John McCalled976492009-12-04 22:46:56 +00003159void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003160 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalled976492009-12-04 22:46:56 +00003161}
John McCall51bd8032009-10-18 01:05:36 +00003162void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003163 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003164}
3165void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003166 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3167 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3168 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003169}
3170void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003171 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3172 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3173 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3174 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003175}
3176void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003177 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003178}
3179void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003180 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003181}
3182void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003183 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003184}
John McCall51bd8032009-10-18 01:05:36 +00003185void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003186 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003187}
John McCall49a832b2009-10-18 09:09:24 +00003188void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
3189 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003190 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall49a832b2009-10-18 09:09:24 +00003191}
John McCall51bd8032009-10-18 01:05:36 +00003192void TypeLocReader::VisitTemplateSpecializationTypeLoc(
3193 TemplateSpecializationTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003194 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
3195 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3196 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall833ca992009-10-29 08:12:44 +00003197 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
3198 TL.setArgLocInfo(i,
Sebastian Redlc3632732010-10-05 15:59:54 +00003199 Reader.GetTemplateArgumentLocInfo(F,
3200 TL.getTypePtr()->getArg(i).getKind(),
3201 Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003202}
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003203void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
3204 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3205 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3206}
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003207void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003208 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3209 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003210}
John McCall3cb0ebd2010-03-10 03:28:59 +00003211void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003212 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall3cb0ebd2010-03-10 03:28:59 +00003213}
Douglas Gregor4714c122010-03-31 17:34:00 +00003214void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003215 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3216 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
3217 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003218}
John McCall33500952010-06-11 00:33:02 +00003219void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
3220 DependentTemplateSpecializationTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003221 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3222 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
3223 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3224 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3225 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall33500952010-06-11 00:33:02 +00003226 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
3227 TL.setArgLocInfo(I,
Sebastian Redlc3632732010-10-05 15:59:54 +00003228 Reader.GetTemplateArgumentLocInfo(F,
3229 TL.getTypePtr()->getArg(I).getKind(),
3230 Record, Idx));
John McCall33500952010-06-11 00:33:02 +00003231}
John McCall51bd8032009-10-18 01:05:36 +00003232void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003233 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallc12c5bb2010-05-15 11:32:37 +00003234}
3235void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3236 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00003237 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3238 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003239 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redlc3632732010-10-05 15:59:54 +00003240 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003241}
John McCall54e14c42009-10-22 22:37:11 +00003242void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003243 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall54e14c42009-10-22 22:37:11 +00003244}
John McCalla1ee0c52009-10-16 21:56:05 +00003245
Sebastian Redlc3632732010-10-05 15:59:54 +00003246TypeSourceInfo *ASTReader::GetTypeSourceInfo(PerFileData &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00003247 const RecordData &Record,
John McCalla1ee0c52009-10-16 21:56:05 +00003248 unsigned &Idx) {
3249 QualType InfoTy = GetType(Record[Idx++]);
3250 if (InfoTy.isNull())
3251 return 0;
3252
John McCalla93c9342009-12-07 02:54:59 +00003253 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
Sebastian Redlc3632732010-10-05 15:59:54 +00003254 TypeLocReader TLR(*this, F, Record, Idx);
John McCalla93c9342009-12-07 02:54:59 +00003255 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCalla1ee0c52009-10-16 21:56:05 +00003256 TLR.Visit(TL);
John McCalla93c9342009-12-07 02:54:59 +00003257 return TInfo;
John McCalla1ee0c52009-10-16 21:56:05 +00003258}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003259
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003260QualType ASTReader::GetType(TypeID ID) {
John McCall0953e762009-09-24 19:53:00 +00003261 unsigned FastQuals = ID & Qualifiers::FastMask;
3262 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003263
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003264 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003265 QualType T;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003266 switch ((PredefinedTypeIDs)Index) {
3267 case PREDEF_TYPE_NULL_ID: return QualType();
3268 case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
3269 case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003270
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003271 case PREDEF_TYPE_CHAR_U_ID:
3272 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregor2cf26342009-04-09 22:27:44 +00003273 // FIXME: Check that the signedness of CharTy is correct!
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003274 T = Context->CharTy;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003275 break;
3276
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003277 case PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break;
3278 case PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break;
3279 case PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break;
3280 case PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break;
3281 case PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break;
3282 case PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break;
3283 case PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break;
3284 case PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break;
3285 case PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break;
3286 case PREDEF_TYPE_INT_ID: T = Context->IntTy; break;
3287 case PREDEF_TYPE_LONG_ID: T = Context->LongTy; break;
3288 case PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break;
3289 case PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break;
3290 case PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break;
3291 case PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break;
3292 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break;
3293 case PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break;
3294 case PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break;
3295 case PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break;
3296 case PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break;
3297 case PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break;
3298 case PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break;
3299 case PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break;
3300 case PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003301 }
3302
3303 assert(!T.isNull() && "Unknown predefined type");
John McCall0953e762009-09-24 19:53:00 +00003304 return T.withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003305 }
3306
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003307 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003308 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl07a353c2010-07-14 20:26:45 +00003309 if (TypesLoaded[Index].isNull()) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003310 TypesLoaded[Index] = ReadTypeRecord(Index);
Douglas Gregor97475832010-10-05 18:37:06 +00003311 if (TypesLoaded[Index].isNull())
3312 return QualType();
3313
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003314 TypesLoaded[Index]->setFromAST();
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003315 TypeIdxs[TypesLoaded[Index]] = TypeIdx::fromTypeID(ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003316 if (DeserializationListener)
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003317 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1476ed42010-07-16 16:36:56 +00003318 TypesLoaded[Index]);
Sebastian Redl07a353c2010-07-14 20:26:45 +00003319 }
Mike Stump1eb44332009-09-09 15:08:12 +00003320
John McCall0953e762009-09-24 19:53:00 +00003321 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003322}
3323
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003324TypeID ASTReader::GetTypeID(QualType T) const {
3325 return MakeTypeID(T,
3326 std::bind1st(std::mem_fun(&ASTReader::GetTypeIdx), this));
3327}
3328
3329TypeIdx ASTReader::GetTypeIdx(QualType T) const {
3330 if (T.isNull())
3331 return TypeIdx();
3332 assert(!T.getLocalFastQualifiers());
3333
3334 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3335 // GetTypeIdx is mostly used for computing the hash of DeclarationNames and
3336 // comparing keys of ASTDeclContextNameLookupTable.
3337 // If the type didn't come from the AST file use a specially marked index
3338 // so that any hash/key comparison fail since no such index is stored
3339 // in a AST file.
3340 if (I == TypeIdxs.end())
3341 return TypeIdx(-1);
3342 return I->second;
3343}
3344
Douglas Gregor7c789c12010-10-29 22:39:52 +00003345unsigned ASTReader::getTotalNumCXXBaseSpecifiers() const {
3346 unsigned Result = 0;
3347 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
3348 Result += Chain[I]->LocalNumCXXBaseSpecifiers;
3349
3350 return Result;
3351}
3352
John McCall833ca992009-10-29 08:12:44 +00003353TemplateArgumentLocInfo
Sebastian Redlc3632732010-10-05 15:59:54 +00003354ASTReader::GetTemplateArgumentLocInfo(PerFileData &F,
3355 TemplateArgument::ArgKind Kind,
John McCall833ca992009-10-29 08:12:44 +00003356 const RecordData &Record,
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003357 unsigned &Index) {
John McCall833ca992009-10-29 08:12:44 +00003358 switch (Kind) {
3359 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00003360 return ReadExpr(F);
John McCall833ca992009-10-29 08:12:44 +00003361 case TemplateArgument::Type:
Sebastian Redlc3632732010-10-05 15:59:54 +00003362 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor788cd062009-11-11 01:00:40 +00003363 case TemplateArgument::Template: {
Sebastian Redlc3632732010-10-05 15:59:54 +00003364 SourceRange QualifierRange = ReadSourceRange(F, Record, Index);
3365 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003366 return TemplateArgumentLocInfo(QualifierRange, TemplateNameLoc);
Douglas Gregor788cd062009-11-11 01:00:40 +00003367 }
John McCall833ca992009-10-29 08:12:44 +00003368 case TemplateArgument::Null:
3369 case TemplateArgument::Integral:
3370 case TemplateArgument::Declaration:
3371 case TemplateArgument::Pack:
3372 return TemplateArgumentLocInfo();
3373 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00003374 llvm_unreachable("unexpected template argument loc");
John McCall833ca992009-10-29 08:12:44 +00003375 return TemplateArgumentLocInfo();
3376}
3377
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003378TemplateArgumentLoc
Sebastian Redlc3632732010-10-05 15:59:54 +00003379ASTReader::ReadTemplateArgumentLoc(PerFileData &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00003380 const RecordData &Record, unsigned &Index) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003381 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003382
3383 if (Arg.getKind() == TemplateArgument::Expression) {
3384 if (Record[Index++]) // bool InfoHasSameExpr.
3385 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
3386 }
Sebastian Redlc3632732010-10-05 15:59:54 +00003387 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003388 Record, Index));
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003389}
3390
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003391Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall76bd1f32010-06-01 09:23:16 +00003392 return GetDecl(ID);
3393}
3394
Douglas Gregor7c789c12010-10-29 22:39:52 +00003395uint64_t
3396ASTReader::GetCXXBaseSpecifiersOffset(serialization::CXXBaseSpecifiersID ID) {
3397 if (ID == 0)
3398 return 0;
3399
3400 --ID;
3401 uint64_t Offset = 0;
3402 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3403 if (ID < Chain[I]->LocalNumCXXBaseSpecifiers)
3404 return Offset + Chain[I]->CXXBaseSpecifiersOffsets[ID];
3405
3406 ID -= Chain[I]->LocalNumCXXBaseSpecifiers;
3407 Offset += Chain[I]->SizeInBits;
3408 }
3409
3410 assert(false && "CXXBaseSpecifiers not found");
3411 return 0;
3412}
3413
3414CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
3415 // Figure out which AST file contains this offset.
3416 PerFileData *F = 0;
3417 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3418 if (Offset < Chain[I]->SizeInBits) {
3419 F = Chain[I];
3420 break;
3421 }
3422
3423 Offset -= Chain[I]->SizeInBits;
3424 }
3425
3426 if (!F) {
3427 Error("Malformed AST file: C++ base specifiers at impossible offset");
3428 return 0;
3429 }
3430
3431 llvm::BitstreamCursor &Cursor = F->DeclsCursor;
3432 SavedStreamPosition SavedPosition(Cursor);
3433 Cursor.JumpToBit(Offset);
3434 ReadingKindTracker ReadingKind(Read_Decl, *this);
3435 RecordData Record;
3436 unsigned Code = Cursor.ReadCode();
3437 unsigned RecCode = Cursor.ReadRecord(Code, Record);
3438 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
3439 Error("Malformed AST file: missing C++ base specifiers");
3440 return 0;
3441 }
3442
3443 unsigned Idx = 0;
3444 unsigned NumBases = Record[Idx++];
3445 void *Mem = Context->Allocate(sizeof(CXXBaseSpecifier) * NumBases);
3446 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
3447 for (unsigned I = 0; I != NumBases; ++I)
3448 Bases[I] = ReadCXXBaseSpecifier(*F, Record, Idx);
3449 return Bases;
3450}
3451
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003452TranslationUnitDecl *ASTReader::GetTranslationUnitDecl() {
Sebastian Redl30c514c2010-07-14 23:45:08 +00003453 if (!DeclsLoaded[0]) {
Sebastian Redle1dde812010-08-24 00:50:04 +00003454 ReadDeclRecord(0, 1);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003455 if (DeserializationListener)
Sebastian Redl1476ed42010-07-16 16:36:56 +00003456 DeserializationListener->DeclRead(1, DeclsLoaded[0]);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003457 }
Argyrios Kyrtzidis8871a442010-07-08 17:13:02 +00003458
3459 return cast<TranslationUnitDecl>(DeclsLoaded[0]);
3460}
3461
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003462Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003463 if (ID == 0)
3464 return 0;
3465
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003466 if (ID > DeclsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003467 Error("declaration ID out-of-range for AST file");
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003468 return 0;
3469 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003470
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003471 unsigned Index = ID - 1;
Sebastian Redl30c514c2010-07-14 23:45:08 +00003472 if (!DeclsLoaded[Index]) {
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00003473 ReadDeclRecord(Index, ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003474 if (DeserializationListener)
3475 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
3476 }
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003477
3478 return DeclsLoaded[Index];
Douglas Gregor2cf26342009-04-09 22:27:44 +00003479}
3480
Chris Lattner887e2b32009-04-27 05:46:25 +00003481/// \brief Resolve the offset of a statement into a statement.
3482///
3483/// This operation will read a new statement from the external
3484/// source each time it is called, and is meant to be used via a
3485/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003486Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00003487 // Switch case IDs are per Decl.
3488 ClearSwitchCaseIDs();
3489
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003490 // Offset here is a global offset across the entire chain.
3491 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3492 PerFileData &F = *Chain[N - I - 1];
3493 if (Offset < F.SizeInBits) {
3494 // Since we know that this statement is part of a decl, make sure to use
3495 // the decl cursor to read it.
3496 F.DeclsCursor.JumpToBit(Offset);
Sebastian Redlc3632732010-10-05 15:59:54 +00003497 return ReadStmtFromStream(F);
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003498 }
3499 Offset -= F.SizeInBits;
3500 }
3501 llvm_unreachable("Broken chain");
Douglas Gregor250fc9c2009-04-18 00:07:54 +00003502}
3503
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003504bool ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00003505 bool (*isKindWeWant)(Decl::Kind),
John McCall76bd1f32010-06-01 09:23:16 +00003506 llvm::SmallVectorImpl<Decl*> &Decls) {
Mike Stump1eb44332009-09-09 15:08:12 +00003507 assert(DC->hasExternalLexicalStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00003508 "DeclContext has no lexical decls in storage");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003509
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003510 // There might be lexical decls in multiple parts of the chain, for the TU
3511 // at least.
Sebastian Redl4a9eb262010-09-28 02:24:44 +00003512 // DeclContextOffsets might reallocate as we load additional decls below,
3513 // so make a copy of the vector.
3514 DeclContextInfos Infos = DeclContextOffsets[DC];
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003515 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3516 I != E; ++I) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003517 // IDs can be 0 if this context doesn't contain declarations.
3518 if (!I->LexicalDecls)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003519 continue;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003520
3521 // Load all of the declaration IDs
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00003522 for (const KindDeclIDPair *ID = I->LexicalDecls,
3523 *IDE = ID + I->NumLexicalDecls; ID != IDE; ++ID) {
3524 if (isKindWeWant && !isKindWeWant((Decl::Kind)ID->first))
3525 continue;
3526
3527 Decl *D = GetDecl(ID->second);
Sebastian Redl4a9eb262010-09-28 02:24:44 +00003528 assert(D && "Null decl in lexical decls");
3529 Decls.push_back(D);
3530 }
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003531 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003532
Douglas Gregor25123082009-04-22 22:34:57 +00003533 ++NumLexicalDeclContextsRead;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003534 return false;
3535}
3536
John McCall76bd1f32010-06-01 09:23:16 +00003537DeclContext::lookup_result
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003538ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +00003539 DeclarationName Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00003540 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00003541 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003542 if (!Name)
3543 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
3544 DeclContext::lookup_iterator(0));
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003545
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003546 llvm::SmallVector<NamedDecl *, 64> Decls;
Sebastian Redl8b122732010-08-24 00:49:55 +00003547 // There might be visible decls in multiple parts of the chain, for the TU
Sebastian Redl5967d622010-08-24 00:50:16 +00003548 // and namespaces. For any given name, the last available results replace
3549 // all earlier ones. For this reason, we walk in reverse.
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003550 DeclContextInfos &Infos = DeclContextOffsets[DC];
Sebastian Redl5967d622010-08-24 00:50:16 +00003551 for (DeclContextInfos::reverse_iterator I = Infos.rbegin(), E = Infos.rend();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003552 I != E; ++I) {
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003553 if (!I->NameLookupTableData)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003554 continue;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003555
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003556 ASTDeclContextNameLookupTable *LookupTable =
3557 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3558 ASTDeclContextNameLookupTable::iterator Pos = LookupTable->find(Name);
3559 if (Pos == LookupTable->end())
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003560 continue;
3561
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003562 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
3563 for (; Data.first != Data.second; ++Data.first)
3564 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
Sebastian Redl5967d622010-08-24 00:50:16 +00003565 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003566 }
3567
Douglas Gregor25123082009-04-22 22:34:57 +00003568 ++NumVisibleDeclContextsRead;
John McCall76bd1f32010-06-01 09:23:16 +00003569
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003570 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall76bd1f32010-06-01 09:23:16 +00003571 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003572}
3573
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00003574void ASTReader::MaterializeVisibleDecls(const DeclContext *DC) {
3575 assert(DC->hasExternalVisibleStorage() &&
3576 "DeclContext has no visible decls in storage");
3577
3578 llvm::SmallVector<NamedDecl *, 64> Decls;
3579 // There might be visible decls in multiple parts of the chain, for the TU
3580 // and namespaces.
3581 DeclContextInfos &Infos = DeclContextOffsets[DC];
3582 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3583 I != E; ++I) {
3584 if (!I->NameLookupTableData)
3585 continue;
3586
3587 ASTDeclContextNameLookupTable *LookupTable =
3588 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3589 for (ASTDeclContextNameLookupTable::item_iterator
3590 ItemI = LookupTable->item_begin(),
3591 ItemEnd = LookupTable->item_end() ; ItemI != ItemEnd; ++ItemI) {
3592 ASTDeclContextNameLookupTable::item_iterator::value_type Val
3593 = *ItemI;
3594 ASTDeclContextNameLookupTrait::data_type Data = Val.second;
3595 Decls.clear();
3596 for (; Data.first != Data.second; ++Data.first)
3597 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
3598 MaterializeVisibleDeclsForName(DC, Val.first, Decls);
3599 }
3600 }
3601}
3602
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003603void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003604 assert(Consumer);
3605 while (!InterestingDecls.empty()) {
3606 DeclGroupRef DG(InterestingDecls.front());
3607 InterestingDecls.pop_front();
Sebastian Redl27372b42010-08-11 18:52:41 +00003608 Consumer->HandleInterestingDecl(DG);
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003609 }
3610}
3611
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003612void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor0af2ca42009-04-22 19:09:20 +00003613 this->Consumer = Consumer;
3614
Douglas Gregorfdd01722009-04-14 00:24:19 +00003615 if (!Consumer)
3616 return;
3617
3618 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003619 // Force deserialization of this decl, which will cause it to be queued for
3620 // passing to the consumer.
Daniel Dunbar04a0b502009-09-17 03:06:44 +00003621 GetDecl(ExternalDefinitions[I]);
Douglas Gregorfdd01722009-04-14 00:24:19 +00003622 }
Douglas Gregorc62a2fe2009-04-25 00:41:30 +00003623
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003624 PassInterestingDeclsToConsumer();
Douglas Gregorfdd01722009-04-14 00:24:19 +00003625}
3626
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003627void ASTReader::PrintStats() {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003628 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregor2cf26342009-04-09 22:27:44 +00003629
Mike Stump1eb44332009-09-09 15:08:12 +00003630 unsigned NumTypesLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003631 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall0953e762009-09-24 19:53:00 +00003632 QualType());
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003633 unsigned NumDeclsLoaded
3634 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
3635 (Decl *)0);
3636 unsigned NumIdentifiersLoaded
3637 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
3638 IdentifiersLoaded.end(),
3639 (IdentifierInfo *)0);
Mike Stump1eb44332009-09-09 15:08:12 +00003640 unsigned NumSelectorsLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003641 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
3642 SelectorsLoaded.end(),
3643 Selector());
Douglas Gregor2d41cc12009-04-13 20:50:16 +00003644
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003645 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
3646 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00003647 if (TotalNumSLocEntries)
3648 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
3649 NumSLocEntriesRead, TotalNumSLocEntries,
3650 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003651 if (!TypesLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003652 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003653 NumTypesLoaded, (unsigned)TypesLoaded.size(),
3654 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
3655 if (!DeclsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003656 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003657 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
3658 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003659 if (!IdentifiersLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003660 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003661 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
3662 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redl725cd962010-08-04 20:40:17 +00003663 if (!SelectorsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003664 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redl725cd962010-08-04 20:40:17 +00003665 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
3666 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor83941df2009-04-25 17:48:32 +00003667 if (TotalNumStatements)
3668 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
3669 NumStatementsRead, TotalNumStatements,
3670 ((float)NumStatementsRead/TotalNumStatements * 100));
3671 if (TotalNumMacros)
3672 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
3673 NumMacrosRead, TotalNumMacros,
3674 ((float)NumMacrosRead/TotalNumMacros * 100));
3675 if (TotalLexicalDeclContexts)
3676 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
3677 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
3678 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
3679 * 100));
3680 if (TotalVisibleDeclContexts)
3681 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
3682 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
3683 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
3684 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003685 if (TotalNumMethodPoolEntries) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003686 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003687 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
3688 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor83941df2009-04-25 17:48:32 +00003689 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003690 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor83941df2009-04-25 17:48:32 +00003691 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003692 std::fprintf(stderr, "\n");
3693}
3694
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003695void ASTReader::InitializeSema(Sema &S) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00003696 SemaObj = &S;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003697 S.ExternalSource = this;
3698
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00003699 // Makes sure any declarations that were deserialized "too early"
3700 // still get added to the identifier's declaration chains.
Douglas Gregor76dc8892010-09-24 23:29:12 +00003701 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
3702 if (SemaObj->TUScope)
John McCalld226f652010-08-21 09:40:31 +00003703 SemaObj->TUScope->AddDecl(PreloadedDecls[I]);
Douglas Gregor76dc8892010-09-24 23:29:12 +00003704
3705 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregor668c1a42009-04-21 22:25:48 +00003706 }
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00003707 PreloadedDecls.clear();
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003708
3709 // If there were any tentative definitions, deserialize them and add
Sebastian Redle9d12b62010-01-31 22:27:38 +00003710 // them to Sema's list of tentative definitions.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003711 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
3712 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
Sebastian Redle9d12b62010-01-31 22:27:38 +00003713 SemaObj->TentativeDefinitions.push_back(Var);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003714 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00003715
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003716 // If there were any unused file scoped decls, deserialize them and add to
3717 // Sema's list of unused file scoped decls.
3718 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
3719 DeclaratorDecl *D = cast<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
3720 SemaObj->UnusedFileScopedDecls.push_back(D);
Tanya Lattnere6bbc012010-02-12 00:07:30 +00003721 }
Douglas Gregor14c22f22009-04-22 22:18:58 +00003722
3723 // If there were any locally-scoped external declarations,
3724 // deserialize them and add them to Sema's table of locally-scoped
3725 // external declarations.
3726 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
3727 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
3728 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
3729 }
Douglas Gregorb81c1702009-04-27 20:06:05 +00003730
3731 // If there were any ext_vector type declarations, deserialize them
3732 // and add them to Sema's vector of such declarations.
3733 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I)
3734 SemaObj->ExtVectorDecls.push_back(
3735 cast<TypedefDecl>(GetDecl(ExtVectorDecls[I])));
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003736
3737 // FIXME: Do VTable uses and dynamic classes deserialize too much ?
3738 // Can we cut them down before writing them ?
3739
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003740 // If there were any dynamic classes declarations, deserialize them
3741 // and add them to Sema's vector of such declarations.
3742 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I)
3743 SemaObj->DynamicClasses.push_back(
3744 cast<CXXRecordDecl>(GetDecl(DynamicClasses[I])));
Fariborz Jahanian32019832010-07-23 19:11:11 +00003745
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003746 // Load the offsets of the declarations that Sema references.
3747 // They will be lazily deserialized when needed.
3748 if (!SemaDeclRefs.empty()) {
3749 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
3750 SemaObj->StdNamespace = SemaDeclRefs[0];
3751 SemaObj->StdBadAlloc = SemaDeclRefs[1];
3752 }
3753
Sebastian Redlc3632732010-10-05 15:59:54 +00003754 for (PerFileData *F = FirstInSource; F; F = F->NextInSource) {
3755
3756 // If there are @selector references added them to its pool. This is for
3757 // implementation of -Wselector.
3758 if (!F->ReferencedSelectorsData.empty()) {
3759 unsigned int DataSize = F->ReferencedSelectorsData.size()-1;
3760 unsigned I = 0;
3761 while (I < DataSize) {
3762 Selector Sel = DecodeSelector(F->ReferencedSelectorsData[I++]);
3763 SourceLocation SelLoc = ReadSourceLocation(
3764 *F, F->ReferencedSelectorsData, I);
3765 SemaObj->ReferencedSelectors.insert(std::make_pair(Sel, SelLoc));
3766 }
3767 }
3768
3769 // If there were any pending implicit instantiations, deserialize them
3770 // and add them to Sema's queue of such instantiations.
3771 assert(F->PendingInstantiations.size() % 2 == 0 &&
3772 "Expected pairs of entries");
3773 for (unsigned Idx = 0, N = F->PendingInstantiations.size(); Idx < N;) {
3774 ValueDecl *D=cast<ValueDecl>(GetDecl(F->PendingInstantiations[Idx++]));
3775 SourceLocation Loc = ReadSourceLocation(*F, F->PendingInstantiations,Idx);
3776 SemaObj->PendingInstantiations.push_back(std::make_pair(D, Loc));
3777 }
3778 }
3779
3780 // The two special data sets below always come from the most recent PCH,
3781 // which is at the front of the chain.
3782 PerFileData &F = *Chain.front();
3783
3784 // If there were any weak undeclared identifiers, deserialize them and add to
3785 // Sema's list of weak undeclared identifiers.
3786 if (!WeakUndeclaredIdentifiers.empty()) {
3787 unsigned Idx = 0;
3788 for (unsigned I = 0, N = WeakUndeclaredIdentifiers[Idx++]; I != N; ++I) {
3789 IdentifierInfo *WeakId = GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3790 IdentifierInfo *AliasId= GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3791 SourceLocation Loc = ReadSourceLocation(F, WeakUndeclaredIdentifiers,Idx);
3792 bool Used = WeakUndeclaredIdentifiers[Idx++];
3793 Sema::WeakInfo WI(AliasId, Loc);
3794 WI.setUsed(Used);
3795 SemaObj->WeakUndeclaredIdentifiers.insert(std::make_pair(WeakId, WI));
3796 }
3797 }
3798
3799 // If there were any VTable uses, deserialize the information and add it
3800 // to Sema's vector and map of VTable uses.
3801 if (!VTableUses.empty()) {
3802 unsigned Idx = 0;
3803 for (unsigned I = 0, N = VTableUses[Idx++]; I != N; ++I) {
3804 CXXRecordDecl *Class = cast<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
3805 SourceLocation Loc = ReadSourceLocation(F, VTableUses, Idx);
3806 bool DefinitionRequired = VTableUses[Idx++];
3807 SemaObj->VTableUses.push_back(std::make_pair(Class, Loc));
3808 SemaObj->VTablesUsed[Class] = DefinitionRequired;
Fariborz Jahanian32019832010-07-23 19:11:11 +00003809 }
3810 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00003811}
3812
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003813IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Sebastian Redld8c5abb2010-08-02 18:30:12 +00003814 // Try to find this name within our on-disk hash tables. We start with the
3815 // most recent one, since that one contains the most up-to-date info.
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003816 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003817 ASTIdentifierLookupTable *IdTable
3818 = (ASTIdentifierLookupTable *)Chain[I]->IdentifierLookupTable;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003819 if (!IdTable)
3820 continue;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003821 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003822 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003823 if (Pos == IdTable->end())
3824 continue;
Douglas Gregor668c1a42009-04-21 22:25:48 +00003825
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003826 // Dereferencing the iterator has the effect of building the
3827 // IdentifierInfo node and populating it with the various
3828 // declarations it needs.
Sebastian Redld8c5abb2010-08-02 18:30:12 +00003829 return *Pos;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003830 }
Sebastian Redld8c5abb2010-08-02 18:30:12 +00003831 return 0;
Douglas Gregor668c1a42009-04-21 22:25:48 +00003832}
3833
Douglas Gregor95f42922010-10-14 22:11:03 +00003834namespace clang {
3835 /// \brief An identifier-lookup iterator that enumerates all of the
3836 /// identifiers stored within a set of AST files.
3837 class ASTIdentifierIterator : public IdentifierIterator {
3838 /// \brief The AST reader whose identifiers are being enumerated.
3839 const ASTReader &Reader;
3840
3841 /// \brief The current index into the chain of AST files stored in
3842 /// the AST reader.
3843 unsigned Index;
3844
3845 /// \brief The current position within the identifier lookup table
3846 /// of the current AST file.
3847 ASTIdentifierLookupTable::key_iterator Current;
3848
3849 /// \brief The end position within the identifier lookup table of
3850 /// the current AST file.
3851 ASTIdentifierLookupTable::key_iterator End;
3852
3853 public:
3854 explicit ASTIdentifierIterator(const ASTReader &Reader);
3855
3856 virtual llvm::StringRef Next();
3857 };
3858}
3859
3860ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
3861 : Reader(Reader), Index(Reader.Chain.size() - 1) {
3862 ASTIdentifierLookupTable *IdTable
3863 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
3864 Current = IdTable->key_begin();
3865 End = IdTable->key_end();
3866}
3867
3868llvm::StringRef ASTIdentifierIterator::Next() {
3869 while (Current == End) {
3870 // If we have exhausted all of our AST files, we're done.
3871 if (Index == 0)
3872 return llvm::StringRef();
3873
3874 --Index;
3875 ASTIdentifierLookupTable *IdTable
3876 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
3877 Current = IdTable->key_begin();
3878 End = IdTable->key_end();
3879 }
3880
3881 // We have any identifiers remaining in the current AST file; return
3882 // the next one.
3883 std::pair<const char*, unsigned> Key = *Current;
3884 ++Current;
3885 return llvm::StringRef(Key.first, Key.second);
3886}
3887
3888IdentifierIterator *ASTReader::getIdentifiers() const {
3889 return new ASTIdentifierIterator(*this);
3890}
3891
Mike Stump1eb44332009-09-09 15:08:12 +00003892std::pair<ObjCMethodList, ObjCMethodList>
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003893ASTReader::ReadMethodPool(Selector Sel) {
Sebastian Redl725cd962010-08-04 20:40:17 +00003894 // Find this selector in a hash table. We want to find the most recent entry.
3895 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3896 PerFileData &F = *Chain[I];
3897 if (!F.SelectorLookupTable)
3898 continue;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003899
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003900 ASTSelectorLookupTable *PoolTable
3901 = (ASTSelectorLookupTable*)F.SelectorLookupTable;
3902 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
Sebastian Redl725cd962010-08-04 20:40:17 +00003903 if (Pos != PoolTable->end()) {
3904 ++NumSelectorsRead;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003905 // FIXME: Not quite happy with the statistics here. We probably should
3906 // disable this tracking when called via LoadSelector.
3907 // Also, should entries without methods count as misses?
3908 ++NumMethodPoolEntriesRead;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003909 ASTSelectorLookupTrait::data_type Data = *Pos;
Sebastian Redl725cd962010-08-04 20:40:17 +00003910 if (DeserializationListener)
3911 DeserializationListener->SelectorRead(Data.ID, Sel);
3912 return std::make_pair(Data.Instance, Data.Factory);
3913 }
Douglas Gregor83941df2009-04-25 17:48:32 +00003914 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003915
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003916 ++NumMethodPoolMisses;
Sebastian Redl725cd962010-08-04 20:40:17 +00003917 return std::pair<ObjCMethodList, ObjCMethodList>();
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003918}
3919
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003920void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redle58aa892010-08-04 18:21:41 +00003921 // It would be complicated to avoid reading the methods anyway. So don't.
3922 ReadMethodPool(Sel);
3923}
3924
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003925void ASTReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00003926 assert(ID && "Non-zero identifier ID required");
Douglas Gregora02b1472009-04-28 21:53:25 +00003927 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003928 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003929 if (DeserializationListener)
3930 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregor668c1a42009-04-21 22:25:48 +00003931}
3932
Douglas Gregord89275b2009-07-06 18:54:52 +00003933/// \brief Set the globally-visible declarations associated with the given
3934/// identifier.
3935///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003936/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump1eb44332009-09-09 15:08:12 +00003937/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregord89275b2009-07-06 18:54:52 +00003938/// them.
3939///
3940/// \param II an IdentifierInfo that refers to one or more globally-visible
3941/// declarations.
3942///
3943/// \param DeclIDs the set of declaration IDs with the name @p II that are
3944/// visible at global scope.
3945///
3946/// \param Nonrecursive should be true to indicate that the caller knows that
3947/// this call is non-recursive, and therefore the globally-visible declarations
3948/// will not be placed onto the pending queue.
Mike Stump1eb44332009-09-09 15:08:12 +00003949void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003950ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Douglas Gregord89275b2009-07-06 18:54:52 +00003951 const llvm::SmallVectorImpl<uint32_t> &DeclIDs,
3952 bool Nonrecursive) {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00003953 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregord89275b2009-07-06 18:54:52 +00003954 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
3955 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
3956 PII.II = II;
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00003957 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregord89275b2009-07-06 18:54:52 +00003958 return;
3959 }
Mike Stump1eb44332009-09-09 15:08:12 +00003960
Douglas Gregord89275b2009-07-06 18:54:52 +00003961 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
3962 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
3963 if (SemaObj) {
Douglas Gregor914ed9d2010-08-13 03:15:25 +00003964 if (SemaObj->TUScope) {
3965 // Introduce this declaration into the translation-unit scope
3966 // and add it to the declaration chain for this identifier, so
3967 // that (unqualified) name lookup will find it.
John McCalld226f652010-08-21 09:40:31 +00003968 SemaObj->TUScope->AddDecl(D);
Douglas Gregor914ed9d2010-08-13 03:15:25 +00003969 }
Douglas Gregor76dc8892010-09-24 23:29:12 +00003970 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
Douglas Gregord89275b2009-07-06 18:54:52 +00003971 } else {
3972 // Queue this declaration so that it will be added to the
3973 // translation unit scope and identifier's declaration chain
3974 // once a Sema object is known.
3975 PreloadedDecls.push_back(D);
3976 }
3977 }
3978}
3979
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003980IdentifierInfo *ASTReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00003981 if (ID == 0)
3982 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003983
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00003984 if (IdentifiersLoaded.empty()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003985 Error("no identifier table in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00003986 return 0;
3987 }
Mike Stump1eb44332009-09-09 15:08:12 +00003988
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003989 assert(PP && "Forgot to set Preprocessor ?");
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00003990 ID -= 1;
3991 if (!IdentifiersLoaded[ID]) {
3992 unsigned Index = ID;
3993 const char *Str = 0;
3994 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3995 PerFileData *F = Chain[N - I - 1];
3996 if (Index < F->LocalNumIdentifiers) {
3997 uint32_t Offset = F->IdentifierOffsets[Index];
3998 Str = F->IdentifierTableData + Offset;
3999 break;
4000 }
4001 Index -= F->LocalNumIdentifiers;
4002 }
4003 assert(Str && "Broken Chain");
Douglas Gregord6595a42009-04-25 21:04:17 +00004004
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004005 // All of the strings in the AST file are preceded by a 16-bit length.
4006 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenek231bc0b2009-10-23 04:45:31 +00004007 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
4008 // unsigned integers. This is important to avoid integer overflow when
4009 // we cast them to 'unsigned'.
Ted Kremenekff1ea462009-10-23 03:57:22 +00004010 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregor02fc7512009-04-28 20:01:51 +00004011 unsigned StrLen = (((unsigned) StrLenPtr[0])
4012 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004013 IdentifiersLoaded[ID]
Kovarththanan Rajaratnam811f4262010-03-12 10:32:27 +00004014 = &PP->getIdentifierTable().get(Str, StrLen);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004015 if (DeserializationListener)
4016 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregorafaf3082009-04-11 00:14:32 +00004017 }
Mike Stump1eb44332009-09-09 15:08:12 +00004018
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004019 return IdentifiersLoaded[ID];
Douglas Gregor2cf26342009-04-09 22:27:44 +00004020}
4021
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004022void ASTReader::ReadSLocEntry(unsigned ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00004023 ReadSLocEntryRecord(ID);
4024}
4025
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004026Selector ASTReader::DecodeSelector(unsigned ID) {
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004027 if (ID == 0)
4028 return Selector();
Mike Stump1eb44332009-09-09 15:08:12 +00004029
Sebastian Redl725cd962010-08-04 20:40:17 +00004030 if (ID > SelectorsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004031 Error("selector ID out of range in AST file");
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004032 return Selector();
4033 }
Douglas Gregor83941df2009-04-25 17:48:32 +00004034
Sebastian Redl725cd962010-08-04 20:40:17 +00004035 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor83941df2009-04-25 17:48:32 +00004036 // Load this selector from the selector table.
Sebastian Redl725cd962010-08-04 20:40:17 +00004037 unsigned Idx = ID - 1;
4038 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4039 PerFileData &F = *Chain[N - I - 1];
4040 if (Idx < F.LocalNumSelectors) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004041 ASTSelectorLookupTrait Trait(*this);
Sebastian Redl725cd962010-08-04 20:40:17 +00004042 SelectorsLoaded[ID - 1] =
4043 Trait.ReadKey(F.SelectorLookupTableData + F.SelectorOffsets[Idx], 0);
4044 if (DeserializationListener)
4045 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
4046 break;
4047 }
4048 Idx -= F.LocalNumSelectors;
4049 }
Douglas Gregor83941df2009-04-25 17:48:32 +00004050 }
4051
Sebastian Redl725cd962010-08-04 20:40:17 +00004052 return SelectorsLoaded[ID - 1];
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004053}
4054
Michael J. Spencer20249a12010-10-21 03:16:25 +00004055Selector ASTReader::GetExternalSelector(uint32_t ID) {
Douglas Gregor719770d2010-04-06 17:30:22 +00004056 return DecodeSelector(ID);
4057}
4058
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004059uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redl725cd962010-08-04 20:40:17 +00004060 // ID 0 (the null selector) is considered an external selector.
4061 return getTotalNumSelectors() + 1;
Douglas Gregor719770d2010-04-06 17:30:22 +00004062}
4063
Mike Stump1eb44332009-09-09 15:08:12 +00004064DeclarationName
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004065ASTReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004066 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
4067 switch (Kind) {
4068 case DeclarationName::Identifier:
4069 return DeclarationName(GetIdentifierInfo(Record, Idx));
4070
4071 case DeclarationName::ObjCZeroArgSelector:
4072 case DeclarationName::ObjCOneArgSelector:
4073 case DeclarationName::ObjCMultiArgSelector:
Steve Naroffa7503a72009-04-23 15:15:40 +00004074 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004075
4076 case DeclarationName::CXXConstructorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004077 return Context->DeclarationNames.getCXXConstructorName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00004078 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004079
4080 case DeclarationName::CXXDestructorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004081 return Context->DeclarationNames.getCXXDestructorName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00004082 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004083
4084 case DeclarationName::CXXConversionFunctionName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004085 return Context->DeclarationNames.getCXXConversionFunctionName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00004086 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004087
4088 case DeclarationName::CXXOperatorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004089 return Context->DeclarationNames.getCXXOperatorName(
Douglas Gregor2cf26342009-04-09 22:27:44 +00004090 (OverloadedOperatorKind)Record[Idx++]);
4091
Sean Hunt3e518bd2009-11-29 07:34:05 +00004092 case DeclarationName::CXXLiteralOperatorName:
4093 return Context->DeclarationNames.getCXXLiteralOperatorName(
4094 GetIdentifierInfo(Record, Idx));
4095
Douglas Gregor2cf26342009-04-09 22:27:44 +00004096 case DeclarationName::CXXUsingDirective:
4097 return DeclarationName::getUsingDirectiveName();
4098 }
4099
4100 // Required to silence GCC warning
4101 return DeclarationName();
4102}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00004103
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004104void ASTReader::ReadDeclarationNameLoc(PerFileData &F,
4105 DeclarationNameLoc &DNLoc,
4106 DeclarationName Name,
4107 const RecordData &Record, unsigned &Idx) {
4108 switch (Name.getNameKind()) {
4109 case DeclarationName::CXXConstructorName:
4110 case DeclarationName::CXXDestructorName:
4111 case DeclarationName::CXXConversionFunctionName:
4112 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
4113 break;
4114
4115 case DeclarationName::CXXOperatorName:
4116 DNLoc.CXXOperatorName.BeginOpNameLoc
4117 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4118 DNLoc.CXXOperatorName.EndOpNameLoc
4119 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4120 break;
4121
4122 case DeclarationName::CXXLiteralOperatorName:
4123 DNLoc.CXXLiteralOperatorName.OpNameLoc
4124 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4125 break;
4126
4127 case DeclarationName::Identifier:
4128 case DeclarationName::ObjCZeroArgSelector:
4129 case DeclarationName::ObjCOneArgSelector:
4130 case DeclarationName::ObjCMultiArgSelector:
4131 case DeclarationName::CXXUsingDirective:
4132 break;
4133 }
4134}
4135
4136void ASTReader::ReadDeclarationNameInfo(PerFileData &F,
4137 DeclarationNameInfo &NameInfo,
4138 const RecordData &Record, unsigned &Idx) {
4139 NameInfo.setName(ReadDeclarationName(Record, Idx));
4140 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
4141 DeclarationNameLoc DNLoc;
4142 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
4143 NameInfo.setInfo(DNLoc);
4144}
4145
4146void ASTReader::ReadQualifierInfo(PerFileData &F, QualifierInfo &Info,
4147 const RecordData &Record, unsigned &Idx) {
4148 Info.NNS = ReadNestedNameSpecifier(Record, Idx);
4149 Info.NNSRange = ReadSourceRange(F, Record, Idx);
4150 unsigned NumTPLists = Record[Idx++];
4151 Info.NumTemplParamLists = NumTPLists;
4152 if (NumTPLists) {
4153 Info.TemplParamLists = new (*Context) TemplateParameterList*[NumTPLists];
4154 for (unsigned i=0; i != NumTPLists; ++i)
4155 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
4156 }
4157}
4158
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004159TemplateName
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004160ASTReader::ReadTemplateName(const RecordData &Record, unsigned &Idx) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00004161 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004162 switch (Kind) {
4163 case TemplateName::Template:
4164 return TemplateName(cast_or_null<TemplateDecl>(GetDecl(Record[Idx++])));
4165
4166 case TemplateName::OverloadedTemplate: {
4167 unsigned size = Record[Idx++];
4168 UnresolvedSet<8> Decls;
4169 while (size--)
4170 Decls.addDecl(cast<NamedDecl>(GetDecl(Record[Idx++])));
4171
4172 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
4173 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004174
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004175 case TemplateName::QualifiedTemplate: {
4176 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4177 bool hasTemplKeyword = Record[Idx++];
4178 TemplateDecl *Template = cast<TemplateDecl>(GetDecl(Record[Idx++]));
4179 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
4180 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004181
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004182 case TemplateName::DependentTemplate: {
4183 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4184 if (Record[Idx++]) // isIdentifier
4185 return Context->getDependentTemplateName(NNS,
4186 GetIdentifierInfo(Record, Idx));
4187 return Context->getDependentTemplateName(NNS,
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004188 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004189 }
4190 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004191
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004192 assert(0 && "Unhandled template name kind!");
4193 return TemplateName();
4194}
4195
4196TemplateArgument
Sebastian Redlc3632732010-10-05 15:59:54 +00004197ASTReader::ReadTemplateArgument(PerFileData &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00004198 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004199 switch ((TemplateArgument::ArgKind)Record[Idx++]) {
4200 case TemplateArgument::Null:
4201 return TemplateArgument();
4202 case TemplateArgument::Type:
4203 return TemplateArgument(GetType(Record[Idx++]));
4204 case TemplateArgument::Declaration:
4205 return TemplateArgument(GetDecl(Record[Idx++]));
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00004206 case TemplateArgument::Integral: {
4207 llvm::APSInt Value = ReadAPSInt(Record, Idx);
4208 QualType T = GetType(Record[Idx++]);
4209 return TemplateArgument(Value, T);
4210 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004211 case TemplateArgument::Template:
4212 return TemplateArgument(ReadTemplateName(Record, Idx));
4213 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00004214 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004215 case TemplateArgument::Pack: {
4216 unsigned NumArgs = Record[Idx++];
Douglas Gregor910f8002010-11-07 23:05:16 +00004217 TemplateArgument *Args = new (*Context) TemplateArgument[NumArgs];
4218 for (unsigned I = 0; I != NumArgs; ++I)
4219 Args[I] = ReadTemplateArgument(F, Record, Idx);
4220 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004221 }
4222 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004223
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004224 assert(0 && "Unhandled template argument kind!");
4225 return TemplateArgument();
4226}
4227
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004228TemplateParameterList *
Sebastian Redlc3632732010-10-05 15:59:54 +00004229ASTReader::ReadTemplateParameterList(PerFileData &F,
4230 const RecordData &Record, unsigned &Idx) {
4231 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
4232 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
4233 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004234
4235 unsigned NumParams = Record[Idx++];
4236 llvm::SmallVector<NamedDecl *, 16> Params;
4237 Params.reserve(NumParams);
4238 while (NumParams--)
4239 Params.push_back(cast<NamedDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer20249a12010-10-21 03:16:25 +00004240
4241 TemplateParameterList* TemplateParams =
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004242 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
4243 Params.data(), Params.size(), RAngleLoc);
4244 return TemplateParams;
4245}
4246
4247void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004248ASTReader::
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004249ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs,
Sebastian Redlc3632732010-10-05 15:59:54 +00004250 PerFileData &F, const RecordData &Record,
4251 unsigned &Idx) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004252 unsigned NumTemplateArgs = Record[Idx++];
4253 TemplArgs.reserve(NumTemplateArgs);
4254 while (NumTemplateArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00004255 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004256}
4257
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004258/// \brief Read a UnresolvedSet structure.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004259void ASTReader::ReadUnresolvedSet(UnresolvedSetImpl &Set,
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004260 const RecordData &Record, unsigned &Idx) {
4261 unsigned NumDecls = Record[Idx++];
4262 while (NumDecls--) {
4263 NamedDecl *D = cast<NamedDecl>(GetDecl(Record[Idx++]));
4264 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
4265 Set.addDecl(D, AS);
4266 }
4267}
4268
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004269CXXBaseSpecifier
Sebastian Redlc3632732010-10-05 15:59:54 +00004270ASTReader::ReadCXXBaseSpecifier(PerFileData &F,
Nick Lewycky56062202010-07-26 16:56:01 +00004271 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004272 bool isVirtual = static_cast<bool>(Record[Idx++]);
4273 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
4274 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00004275 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
4276 SourceRange Range = ReadSourceRange(F, Record, Idx);
Nick Lewycky56062202010-07-26 16:56:01 +00004277 return CXXBaseSpecifier(Range, isVirtual, isBaseOfClass, AS, TInfo);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004278}
4279
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004280std::pair<CXXBaseOrMemberInitializer **, unsigned>
Sebastian Redlc3632732010-10-05 15:59:54 +00004281ASTReader::ReadCXXBaseOrMemberInitializers(PerFileData &F,
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004282 const RecordData &Record,
4283 unsigned &Idx) {
4284 CXXBaseOrMemberInitializer **BaseOrMemberInitializers = 0;
4285 unsigned NumInitializers = Record[Idx++];
4286 if (NumInitializers) {
4287 ASTContext &C = *getContext();
4288
4289 BaseOrMemberInitializers
4290 = new (C) CXXBaseOrMemberInitializer*[NumInitializers];
4291 for (unsigned i=0; i != NumInitializers; ++i) {
4292 TypeSourceInfo *BaseClassInfo = 0;
4293 bool IsBaseVirtual = false;
4294 FieldDecl *Member = 0;
Francois Pichet00eb3f92010-12-04 09:14:42 +00004295 IndirectFieldDecl *IndirectMember = 0;
Michael J. Spencer20249a12010-10-21 03:16:25 +00004296
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004297 bool IsBaseInitializer = Record[Idx++];
4298 if (IsBaseInitializer) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004299 BaseClassInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004300 IsBaseVirtual = Record[Idx++];
4301 } else {
Francois Pichet00eb3f92010-12-04 09:14:42 +00004302 bool IsIndirectMemberInitializer = Record[Idx++];
4303 if (IsIndirectMemberInitializer)
4304 IndirectMember = cast<IndirectFieldDecl>(GetDecl(Record[Idx++]));
4305 else
4306 Member = cast<FieldDecl>(GetDecl(Record[Idx++]));
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004307 }
Sebastian Redlc3632732010-10-05 15:59:54 +00004308 SourceLocation MemberLoc = ReadSourceLocation(F, Record, Idx);
4309 Expr *Init = ReadExpr(F);
Sebastian Redlc3632732010-10-05 15:59:54 +00004310 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
4311 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004312 bool IsWritten = Record[Idx++];
4313 unsigned SourceOrderOrNumArrayIndices;
4314 llvm::SmallVector<VarDecl *, 8> Indices;
4315 if (IsWritten) {
4316 SourceOrderOrNumArrayIndices = Record[Idx++];
4317 } else {
4318 SourceOrderOrNumArrayIndices = Record[Idx++];
4319 Indices.reserve(SourceOrderOrNumArrayIndices);
4320 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
4321 Indices.push_back(cast<VarDecl>(GetDecl(Record[Idx++])));
4322 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004323
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004324 CXXBaseOrMemberInitializer *BOMInit;
4325 if (IsBaseInitializer) {
4326 BOMInit = new (C) CXXBaseOrMemberInitializer(C, BaseClassInfo,
4327 IsBaseVirtual, LParenLoc,
4328 Init, RParenLoc);
4329 } else if (IsWritten) {
Francois Pichet00eb3f92010-12-04 09:14:42 +00004330 if (Member)
4331 BOMInit = new (C) CXXBaseOrMemberInitializer(C, Member, MemberLoc,
4332 LParenLoc, Init,
4333 RParenLoc);
4334 else
4335 BOMInit = new (C) CXXBaseOrMemberInitializer(C, IndirectMember,
4336 MemberLoc, LParenLoc,
4337 Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004338 } else {
4339 BOMInit = CXXBaseOrMemberInitializer::Create(C, Member, MemberLoc,
4340 LParenLoc, Init, RParenLoc,
4341 Indices.data(),
4342 Indices.size());
4343 }
4344
Argyrios Kyrtzidisf84cde12010-09-06 19:04:27 +00004345 if (IsWritten)
4346 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004347 BaseOrMemberInitializers[i] = BOMInit;
4348 }
4349 }
4350
4351 return std::make_pair(BaseOrMemberInitializers, NumInitializers);
4352}
4353
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004354NestedNameSpecifier *
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004355ASTReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004356 unsigned N = Record[Idx++];
4357 NestedNameSpecifier *NNS = 0, *Prev = 0;
4358 for (unsigned I = 0; I != N; ++I) {
4359 NestedNameSpecifier::SpecifierKind Kind
4360 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
4361 switch (Kind) {
4362 case NestedNameSpecifier::Identifier: {
4363 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
4364 NNS = NestedNameSpecifier::Create(*Context, Prev, II);
4365 break;
4366 }
4367
4368 case NestedNameSpecifier::Namespace: {
4369 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
4370 NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
4371 break;
4372 }
4373
4374 case NestedNameSpecifier::TypeSpec:
4375 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregor1ab55e92010-12-10 17:03:06 +00004376 Type *T = GetType(Record[Idx++]).getTypePtrOrNull();
4377 if (!T)
4378 return 0;
4379
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004380 bool Template = Record[Idx++];
4381 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
4382 break;
4383 }
4384
4385 case NestedNameSpecifier::Global: {
4386 NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
4387 // No associated value, and there can't be a prefix.
4388 break;
4389 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004390 }
Argyrios Kyrtzidisd2bb2c02010-07-07 15:46:30 +00004391 Prev = NNS;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004392 }
4393 return NNS;
4394}
4395
4396SourceRange
Sebastian Redlc3632732010-10-05 15:59:54 +00004397ASTReader::ReadSourceRange(PerFileData &F, const RecordData &Record,
4398 unsigned &Idx) {
4399 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
4400 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar8ee59392010-06-02 15:47:10 +00004401 return SourceRange(beg, end);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004402}
4403
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004404/// \brief Read an integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004405llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004406 unsigned BitWidth = Record[Idx++];
4407 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
4408 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
4409 Idx += NumWords;
4410 return Result;
4411}
4412
4413/// \brief Read a signed integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004414llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004415 bool isUnsigned = Record[Idx++];
4416 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
4417}
4418
Douglas Gregor17fc2232009-04-14 21:55:33 +00004419/// \brief Read a floating-point value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004420llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00004421 return llvm::APFloat(ReadAPInt(Record, Idx));
4422}
4423
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004424// \brief Read a string
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004425std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004426 unsigned Len = Record[Idx++];
Jay Foadbeaaccd2009-05-21 09:52:38 +00004427 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004428 Idx += Len;
4429 return Result;
4430}
4431
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004432CXXTemporary *ASTReader::ReadCXXTemporary(const RecordData &Record,
Chris Lattnerd2598362010-05-10 00:25:06 +00004433 unsigned &Idx) {
4434 CXXDestructorDecl *Decl = cast<CXXDestructorDecl>(GetDecl(Record[Idx++]));
4435 return CXXTemporary::Create(*Context, Decl);
4436}
4437
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004438DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00004439 return Diag(SourceLocation(), DiagID);
4440}
4441
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004442DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004443 return Diags.Report(Loc, DiagID);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00004444}
Douglas Gregor025452f2009-04-17 00:04:06 +00004445
Douglas Gregor668c1a42009-04-21 22:25:48 +00004446/// \brief Retrieve the identifier table associated with the
4447/// preprocessor.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004448IdentifierTable &ASTReader::getIdentifierTable() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00004449 assert(PP && "Forgot to set Preprocessor ?");
4450 return PP->getIdentifierTable();
Douglas Gregor668c1a42009-04-21 22:25:48 +00004451}
4452
Douglas Gregor025452f2009-04-17 00:04:06 +00004453/// \brief Record that the given ID maps to the given switch-case
4454/// statement.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004455void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Douglas Gregor025452f2009-04-17 00:04:06 +00004456 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
4457 SwitchCaseStmts[ID] = SC;
4458}
4459
4460/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004461SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Douglas Gregor025452f2009-04-17 00:04:06 +00004462 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
4463 return SwitchCaseStmts[ID];
4464}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00004465
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00004466void ASTReader::ClearSwitchCaseIDs() {
4467 SwitchCaseStmts.clear();
4468}
4469
Douglas Gregor1de05fe2009-04-17 18:18:49 +00004470/// \brief Record that the given label statement has been
4471/// deserialized and has the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004472void ASTReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
Mike Stump1eb44332009-09-09 15:08:12 +00004473 assert(LabelStmts.find(ID) == LabelStmts.end() &&
Douglas Gregor1de05fe2009-04-17 18:18:49 +00004474 "Deserialized label twice");
4475 LabelStmts[ID] = S;
4476
4477 // If we've already seen any goto statements that point to this
4478 // label, resolve them now.
4479 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
4480 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
4481 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
4482 Goto->second->setLabel(S);
4483 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00004484
4485 // If we've already seen any address-label statements that point to
4486 // this label, resolve them now.
4487 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
Mike Stump1eb44332009-09-09 15:08:12 +00004488 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00004489 = UnresolvedAddrLabelExprs.equal_range(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00004490 for (AddrLabelIter AddrLabel = AddrLabels.first;
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00004491 AddrLabel != AddrLabels.second; ++AddrLabel)
4492 AddrLabel->second->setLabel(S);
4493 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor1de05fe2009-04-17 18:18:49 +00004494}
4495
4496/// \brief Set the label of the given statement to the label
4497/// identified by ID.
4498///
4499/// Depending on the order in which the label and other statements
4500/// referencing that label occur, this operation may complete
4501/// immediately (updating the statement) or it may queue the
4502/// statement to be back-patched later.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004503void ASTReader::SetLabelOf(GotoStmt *S, unsigned ID) {
Douglas Gregor1de05fe2009-04-17 18:18:49 +00004504 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
4505 if (Label != LabelStmts.end()) {
4506 // We've already seen this label, so set the label of the goto and
4507 // we're done.
4508 S->setLabel(Label->second);
4509 } else {
4510 // We haven't seen this label yet, so add this goto to the set of
4511 // unresolved goto statements.
4512 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
4513 }
4514}
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00004515
4516/// \brief Set the label of the given expression to the label
4517/// identified by ID.
4518///
4519/// Depending on the order in which the label and other statements
4520/// referencing that label occur, this operation may complete
4521/// immediately (updating the statement) or it may queue the
4522/// statement to be back-patched later.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004523void ASTReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00004524 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
4525 if (Label != LabelStmts.end()) {
4526 // We've already seen this label, so set the label of the
4527 // label-address expression and we're done.
4528 S->setLabel(Label->second);
4529 } else {
4530 // We haven't seen this label yet, so add this label-address
4531 // expression to the set of unresolved label-address expressions.
4532 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
4533 }
4534}
Douglas Gregord89275b2009-07-06 18:54:52 +00004535
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004536void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004537 assert(NumCurrentElementsDeserializing &&
4538 "FinishedDeserializing not paired with StartedDeserializing");
4539 if (NumCurrentElementsDeserializing == 1) {
Douglas Gregord89275b2009-07-06 18:54:52 +00004540 // If any identifiers with corresponding top-level declarations have
4541 // been loaded, load those declarations now.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004542 while (!PendingIdentifierInfos.empty()) {
4543 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
4544 PendingIdentifierInfos.front().DeclIDs, true);
4545 PendingIdentifierInfos.pop_front();
Douglas Gregord89275b2009-07-06 18:54:52 +00004546 }
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00004547
4548 // We are not in recursive loading, so it's safe to pass the "interesting"
4549 // decls to the consumer.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004550 if (Consumer)
4551 PassInterestingDeclsToConsumer();
Argyrios Kyrtzidis134db1f2010-10-24 17:26:31 +00004552
4553 assert(PendingForwardRefs.size() == 0 &&
4554 "Some forward refs did not get linked to the definition!");
Douglas Gregord89275b2009-07-06 18:54:52 +00004555 }
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004556 --NumCurrentElementsDeserializing;
Douglas Gregord89275b2009-07-06 18:54:52 +00004557}
Douglas Gregor501c1032010-08-19 00:28:17 +00004558
Sebastian Redle1dde812010-08-24 00:50:04 +00004559ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
4560 const char *isysroot, bool DisableValidation)
4561 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
4562 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
4563 Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
4564 Consumer(0), isysroot(isysroot), DisableValidation(DisableValidation),
4565 NumStatHits(0), NumStatMisses(0), NumSLocEntriesRead(0),
Sebastian Redl8db9fae2010-09-22 20:19:08 +00004566 TotalNumSLocEntries(0), NextSLocOffset(0), NumStatementsRead(0),
4567 TotalNumStatements(0), NumMacrosRead(0), TotalNumMacros(0),
4568 NumSelectorsRead(0), NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
Sebastian Redle1dde812010-08-24 00:50:04 +00004569 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
4570 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
4571 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
4572 RelocatablePCH = false;
4573}
4574
4575ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
4576 Diagnostic &Diags, const char *isysroot,
4577 bool DisableValidation)
4578 : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
4579 Diags(Diags), SemaObj(0), PP(0), Context(0), Consumer(0),
4580 isysroot(isysroot), DisableValidation(DisableValidation), NumStatHits(0),
4581 NumStatMisses(0), NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Sebastian Redl8db9fae2010-09-22 20:19:08 +00004582 NextSLocOffset(0), NumStatementsRead(0), TotalNumStatements(0),
4583 NumMacrosRead(0), TotalNumMacros(0), NumSelectorsRead(0),
4584 NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
4585 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
4586 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
4587 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
Sebastian Redle1dde812010-08-24 00:50:04 +00004588 RelocatablePCH = false;
4589}
4590
4591ASTReader::~ASTReader() {
4592 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
4593 delete Chain[e - i - 1];
4594 // Delete all visible decl lookup tables
4595 for (DeclContextOffsetsMap::iterator I = DeclContextOffsets.begin(),
4596 E = DeclContextOffsets.end();
4597 I != E; ++I) {
4598 for (DeclContextInfos::iterator J = I->second.begin(), F = I->second.end();
4599 J != F; ++J) {
4600 if (J->NameLookupTableData)
4601 delete static_cast<ASTDeclContextNameLookupTable*>(
4602 J->NameLookupTableData);
4603 }
4604 }
4605 for (DeclContextVisibleUpdatesPending::iterator
4606 I = PendingVisibleUpdates.begin(),
4607 E = PendingVisibleUpdates.end();
4608 I != E; ++I) {
4609 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
4610 F = I->second.end();
4611 J != F; ++J)
4612 delete static_cast<ASTDeclContextNameLookupTable*>(*J);
4613 }
4614}
4615
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00004616ASTReader::PerFileData::PerFileData(ASTFileType Ty)
4617 : Type(Ty), SizeInBits(0), LocalNumSLocEntries(0), SLocOffsets(0), LocalSLocSize(0),
Sebastian Redl301c9b02010-09-22 00:42:27 +00004618 LocalNumIdentifiers(0), IdentifierOffsets(0), IdentifierTableData(0),
4619 IdentifierLookupTable(0), LocalNumMacroDefinitions(0),
4620 MacroDefinitionOffsets(0), LocalNumSelectors(0), SelectorOffsets(0),
4621 SelectorLookupTableData(0), SelectorLookupTable(0), LocalNumDecls(0),
Douglas Gregor7c789c12010-10-29 22:39:52 +00004622 DeclOffsets(0), LocalNumCXXBaseSpecifiers(0), CXXBaseSpecifiersOffsets(0),
4623 LocalNumTypes(0), TypeOffsets(0), StatCache(0),
Sebastian Redla866e652010-10-01 19:59:12 +00004624 NumPreallocatedPreprocessingEntities(0), NextInSource(0)
Douglas Gregor501c1032010-08-19 00:28:17 +00004625{}
4626
4627ASTReader::PerFileData::~PerFileData() {
4628 delete static_cast<ASTIdentifierLookupTable *>(IdentifierLookupTable);
4629 delete static_cast<ASTSelectorLookupTable *>(SelectorLookupTable);
4630}
4631