blob: bc898f167657c44f8d012ba1ab7e8966c787ad93 [file] [log] [blame]
Sebastian Redl904c9c82010-08-18 23:57:11 +00001//===--- ASTReader.cpp - AST File Reader ------------------------*- C++ -*-===//
Douglas Gregor2cf26342009-04-09 22:27:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Sebastian Redlc43b54c2010-08-18 23:56:43 +000010// This file defines the ASTReader class, which reads AST files.
Douglas Gregor2cf26342009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
Chris Lattner4c6f9522009-04-27 05:14:47 +000013
Sebastian Redl6ab7cd82010-08-18 23:57:17 +000014#include "clang/Serialization/ASTReader.h"
15#include "clang/Serialization/ASTDeserializationListener.h"
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +000016#include "ASTCommon.h"
Douglas Gregor0a0428e2009-04-10 20:39:37 +000017#include "clang/Frontend/FrontendDiagnostic.h"
Daniel Dunbarc7162932009-11-11 23:58:53 +000018#include "clang/Frontend/Utils.h"
Douglas Gregore737f502010-08-12 20:07:10 +000019#include "clang/Sema/Sema.h"
John McCall5f1e0942010-08-24 08:50:51 +000020#include "clang/Sema/Scope.h"
Douglas Gregorfdd01722009-04-14 00:24:19 +000021#include "clang/AST/ASTConsumer.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000022#include "clang/AST/ASTContext.h"
John McCall2a7fb272010-08-25 05:32:35 +000023#include "clang/AST/DeclTemplate.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000024#include "clang/AST/Expr.h"
John McCall7a1fad32010-08-24 07:32:53 +000025#include "clang/AST/ExprCXX.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000026#include "clang/AST/Type.h"
John McCalla1ee0c52009-10-16 21:56:05 +000027#include "clang/AST/TypeLocVisitor.h"
Chris Lattner42d42b52009-04-10 21:41:48 +000028#include "clang/Lex/MacroInfo.h"
Douglas Gregor6a5a23f2010-03-19 21:51:54 +000029#include "clang/Lex/PreprocessingRecord.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000030#include "clang/Lex/Preprocessor.h"
Steve Naroff83d63c72009-04-24 20:03:17 +000031#include "clang/Lex/HeaderSearch.h"
Douglas Gregor668c1a42009-04-21 22:25:48 +000032#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000033#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000034#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000035#include "clang/Basic/FileManager.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000036#include "clang/Basic/TargetInfo.h"
Douglas Gregor445e23e2009-10-05 21:07:28 +000037#include "clang/Basic/Version.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000038#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000039#include "llvm/Bitcode/BitstreamReader.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000040#include "llvm/Support/MemoryBuffer.h"
John McCall833ca992009-10-29 08:12:44 +000041#include "llvm/Support/ErrorHandling.h"
Daniel Dunbard5b21972009-11-18 19:50:41 +000042#include "llvm/System/Path.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000043#include <algorithm>
Douglas Gregore721f952009-04-28 18:58:38 +000044#include <iterator>
Douglas Gregor2cf26342009-04-09 22:27:44 +000045#include <cstdio>
Douglas Gregor4fed3f42009-04-27 18:38:38 +000046#include <sys/stat.h>
Douglas Gregor2cf26342009-04-09 22:27:44 +000047using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000048using namespace clang::serialization;
Douglas Gregor2cf26342009-04-09 22:27:44 +000049
50//===----------------------------------------------------------------------===//
Sebastian Redl3c7f4132010-08-18 23:57:06 +000051// PCH validator implementation
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000052//===----------------------------------------------------------------------===//
53
Sebastian Redl571db7f2010-08-18 23:56:56 +000054ASTReaderListener::~ASTReaderListener() {}
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000055
56bool
57PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts) {
58 const LangOptions &PPLangOpts = PP.getLangOptions();
59#define PARSE_LANGOPT_BENIGN(Option)
60#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
61 if (PPLangOpts.Option != LangOpts.Option) { \
62 Reader.Diag(DiagID) << LangOpts.Option << PPLangOpts.Option; \
63 return true; \
64 }
65
66 PARSE_LANGOPT_BENIGN(Trigraphs);
67 PARSE_LANGOPT_BENIGN(BCPLComment);
68 PARSE_LANGOPT_BENIGN(DollarIdents);
69 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
70 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
Chandler Carrutheb5d7b72010-04-17 20:17:31 +000071 PARSE_LANGOPT_IMPORTANT(GNUKeywords, diag::warn_pch_gnu_keywords);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000072 PARSE_LANGOPT_BENIGN(ImplicitInt);
73 PARSE_LANGOPT_BENIGN(Digraphs);
74 PARSE_LANGOPT_BENIGN(HexFloats);
75 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
76 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
77 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
78 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
79 PARSE_LANGOPT_BENIGN(CXXOperatorName);
80 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
81 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
82 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
Fariborz Jahanian412e7982010-02-09 19:31:38 +000083 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI2, diag::warn_pch_nonfragile_abi2);
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +000084 PARSE_LANGOPT_IMPORTANT(NoConstantCFStrings,
85 diag::warn_pch_no_constant_cfstrings);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000086 PARSE_LANGOPT_BENIGN(PascalStrings);
87 PARSE_LANGOPT_BENIGN(WritableStrings);
Mike Stump1eb44332009-09-09 15:08:12 +000088 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000089 diag::warn_pch_lax_vector_conversions);
Nate Begeman69cfb9b2009-06-25 22:57:40 +000090 PARSE_LANGOPT_IMPORTANT(AltiVec, diag::warn_pch_altivec);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000091 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
Daniel Dunbar73482882010-02-10 18:48:44 +000092 PARSE_LANGOPT_IMPORTANT(SjLjExceptions, diag::warn_pch_sjlj_exceptions);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000093 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
94 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
95 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
Mike Stump1eb44332009-09-09 15:08:12 +000096 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000097 diag::warn_pch_thread_safe_statics);
Daniel Dunbar5345c392009-09-03 04:54:28 +000098 PARSE_LANGOPT_IMPORTANT(POSIXThreads, diag::warn_pch_posix_threads);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000099 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
100 PARSE_LANGOPT_BENIGN(EmitAllDecls);
101 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
Chris Lattnera4d71452010-06-26 21:25:03 +0000102 PARSE_LANGOPT_BENIGN(getSignedOverflowBehavior());
Mike Stump1eb44332009-09-09 15:08:12 +0000103 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000104 diag::warn_pch_heinous_extensions);
105 // FIXME: Most of the options below are benign if the macro wasn't
106 // used. Unfortunately, this means that a PCH compiled without
107 // optimization can't be used with optimization turned on, even
108 // though the only thing that changes is whether __OPTIMIZE__ was
109 // defined... but if __OPTIMIZE__ never showed up in the header, it
110 // doesn't matter. We could consider making this some special kind
111 // of check.
112 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
113 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
114 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
115 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
116 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
117 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
118 PARSE_LANGOPT_IMPORTANT(AccessControl, diag::warn_pch_access_control);
119 PARSE_LANGOPT_IMPORTANT(CharIsSigned, diag::warn_pch_char_signed);
John Thompsona6fda122009-11-05 20:14:16 +0000120 PARSE_LANGOPT_IMPORTANT(ShortWChar, diag::warn_pch_short_wchar);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000121 if ((PPLangOpts.getGCMode() != 0) != (LangOpts.getGCMode() != 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000122 Reader.Diag(diag::warn_pch_gc_mode)
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000123 << LangOpts.getGCMode() << PPLangOpts.getGCMode();
124 return true;
125 }
126 PARSE_LANGOPT_BENIGN(getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000127 PARSE_LANGOPT_IMPORTANT(getStackProtectorMode(),
128 diag::warn_pch_stack_protector);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000129 PARSE_LANGOPT_BENIGN(InstantiationDepth);
Nate Begeman69cfb9b2009-06-25 22:57:40 +0000130 PARSE_LANGOPT_IMPORTANT(OpenCL, diag::warn_pch_opencl);
Mike Stump9c276ae2009-12-12 01:27:46 +0000131 PARSE_LANGOPT_BENIGN(CatchUndefined);
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000132 PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors);
Douglas Gregora0068fc2010-07-09 17:35:33 +0000133 PARSE_LANGOPT_BENIGN(SpellChecking);
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +0000134#undef PARSE_LANGOPT_IMPORTANT
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000135#undef PARSE_LANGOPT_BENIGN
136
137 return false;
138}
139
Daniel Dunbardc3c0d22009-11-11 00:52:11 +0000140bool PCHValidator::ReadTargetTriple(llvm::StringRef Triple) {
141 if (Triple == PP.getTargetInfo().getTriple().str())
142 return false;
143
144 Reader.Diag(diag::warn_pch_target_triple)
145 << Triple << PP.getTargetInfo().getTriple().str();
146 return true;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000147}
148
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000149struct EmptyStringRef {
Benjamin Kramerec1b1cc2010-07-14 23:19:41 +0000150 bool operator ()(llvm::StringRef r) const { return r.empty(); }
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000151};
152struct EmptyBlock {
153 bool operator ()(const PCHPredefinesBlock &r) const { return r.Data.empty(); }
154};
155
156static bool EqualConcatenations(llvm::SmallVector<llvm::StringRef, 2> L,
157 PCHPredefinesBlocks R) {
158 // First, sum up the lengths.
159 unsigned LL = 0, RL = 0;
160 for (unsigned I = 0, N = L.size(); I != N; ++I) {
161 LL += L[I].size();
162 }
163 for (unsigned I = 0, N = R.size(); I != N; ++I) {
164 RL += R[I].Data.size();
165 }
166 if (LL != RL)
167 return false;
168 if (LL == 0 && RL == 0)
169 return true;
170
171 // Kick out empty parts, they confuse the algorithm below.
172 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
173 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
174
175 // Do it the hard way. At this point, both vectors must be non-empty.
176 llvm::StringRef LR = L[0], RR = R[0].Data;
177 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
Daniel Dunbarc76c9e02010-07-16 00:00:11 +0000178 (void) RN;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000179 for (;;) {
180 // Compare the current pieces.
181 if (LR.size() == RR.size()) {
182 // If they're the same length, it's pretty easy.
183 if (LR != RR)
184 return false;
185 // Both pieces are done, advance.
186 ++LI;
187 ++RI;
188 // If either string is done, they're both done, since they're the same
189 // length.
190 if (LI == LN) {
191 assert(RI == RN && "Strings not the same length after all?");
192 return true;
193 }
194 LR = L[LI];
195 RR = R[RI].Data;
196 } else if (LR.size() < RR.size()) {
197 // Right piece is longer.
198 if (!RR.startswith(LR))
199 return false;
200 ++LI;
201 assert(LI != LN && "Strings not the same length after all?");
202 RR = RR.substr(LR.size());
203 LR = L[LI];
204 } else {
205 // Left piece is longer.
206 if (!LR.startswith(RR))
207 return false;
208 ++RI;
209 assert(RI != RN && "Strings not the same length after all?");
210 LR = LR.substr(RR.size());
211 RR = R[RI].Data;
212 }
213 }
214}
215
216static std::pair<FileID, llvm::StringRef::size_type>
217FindMacro(const PCHPredefinesBlocks &Buffers, llvm::StringRef MacroDef) {
218 std::pair<FileID, llvm::StringRef::size_type> Res;
219 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
220 Res.second = Buffers[I].Data.find(MacroDef);
221 if (Res.second != llvm::StringRef::npos) {
222 Res.first = Buffers[I].BufferID;
223 break;
224 }
225 }
226 return Res;
227}
228
229bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000230 llvm::StringRef OriginalFileName,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000231 std::string &SuggestedPredefines) {
Daniel Dunbarc7162932009-11-11 23:58:53 +0000232 // We are in the context of an implicit include, so the predefines buffer will
233 // have a #include entry for the PCH file itself (as normalized by the
234 // preprocessor initialization). Find it and skip over it in the checking
235 // below.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000236 llvm::SmallString<256> PCHInclude;
237 PCHInclude += "#include \"";
Daniel Dunbarc7162932009-11-11 23:58:53 +0000238 PCHInclude += NormalizeDashIncludePath(OriginalFileName);
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000239 PCHInclude += "\"\n";
240 std::pair<llvm::StringRef,llvm::StringRef> Split =
241 llvm::StringRef(PP.getPredefines()).split(PCHInclude.str());
242 llvm::StringRef Left = Split.first, Right = Split.second;
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000243 if (Left == PP.getPredefines()) {
244 Error("Missing PCH include entry!");
245 return true;
246 }
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000247
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000248 // If the concatenation of all the PCH buffers is equal to the adjusted
249 // command line, we're done.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000250 llvm::SmallVector<llvm::StringRef, 2> CommandLine;
251 CommandLine.push_back(Left);
252 CommandLine.push_back(Right);
253 if (EqualConcatenations(CommandLine, Buffers))
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000254 return false;
255
256 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000257
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000258 // The predefines buffers are different. Determine what the differences are,
259 // and whether they require us to reject the PCH file.
Daniel Dunbare6750492009-11-13 16:46:11 +0000260 llvm::SmallVector<llvm::StringRef, 8> PCHLines;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000261 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
262 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Daniel Dunbare6750492009-11-13 16:46:11 +0000263
264 llvm::SmallVector<llvm::StringRef, 8> CmdLineLines;
265 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000266
267 // Pick out implicit #includes after the PCH and don't consider them for
268 // validation; we will insert them into SuggestedPredefines so that the
269 // preprocessor includes them.
270 std::string IncludesAfterPCH;
271 llvm::SmallVector<llvm::StringRef, 8> AfterPCHLines;
272 Right.split(AfterPCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
273 for (unsigned i = 0, e = AfterPCHLines.size(); i != e; ++i) {
274 if (AfterPCHLines[i].startswith("#include ")) {
275 IncludesAfterPCH += AfterPCHLines[i];
276 IncludesAfterPCH += '\n';
277 } else {
278 CmdLineLines.push_back(AfterPCHLines[i]);
279 }
280 }
281
282 // Make sure we add the includes last into SuggestedPredefines before we
283 // exit this function.
284 struct AddIncludesRAII {
285 std::string &SuggestedPredefines;
286 std::string &IncludesAfterPCH;
287
288 AddIncludesRAII(std::string &SuggestedPredefines,
289 std::string &IncludesAfterPCH)
290 : SuggestedPredefines(SuggestedPredefines),
291 IncludesAfterPCH(IncludesAfterPCH) { }
292 ~AddIncludesRAII() {
293 SuggestedPredefines += IncludesAfterPCH;
294 }
295 } AddIncludes(SuggestedPredefines, IncludesAfterPCH);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000296
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000297 // Sort both sets of predefined buffer lines, since we allow some extra
298 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000299 std::sort(CmdLineLines.begin(), CmdLineLines.end());
300 std::sort(PCHLines.begin(), PCHLines.end());
301
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000302 // Determine which predefines that were used to build the PCH file are missing
303 // from the command line.
304 std::vector<llvm::StringRef> MissingPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000305 std::set_difference(PCHLines.begin(), PCHLines.end(),
306 CmdLineLines.begin(), CmdLineLines.end(),
307 std::back_inserter(MissingPredefines));
308
309 bool MissingDefines = false;
310 bool ConflictingDefines = false;
311 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000312 llvm::StringRef Missing = MissingPredefines[I];
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000313 if (Missing.startswith("#include ")) {
314 // An -include was specified when generating the PCH; it is included in
315 // the PCH, just ignore it.
316 continue;
317 }
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000318 if (!Missing.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000319 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
320 return true;
321 }
Mike Stump1eb44332009-09-09 15:08:12 +0000322
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000323 // This is a macro definition. Determine the name of the macro we're
324 // defining.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000325 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000326 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000327 = Missing.find_first_of("( \n\r", StartOfMacroName);
328 assert(EndOfMacroName != std::string::npos &&
329 "Couldn't find the end of the macro name");
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000330 llvm::StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000331
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000332 // Determine whether this macro was given a different definition on the
333 // command line.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000334 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000335 std::string::size_type MacroDefLen = MacroDefStart.size();
Daniel Dunbare6750492009-11-13 16:46:11 +0000336 llvm::SmallVector<llvm::StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000337 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
338 MacroDefStart);
339 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000340 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000341 // Different macro; we're done.
342 ConflictPos = CmdLineLines.end();
Mike Stump1eb44332009-09-09 15:08:12 +0000343 break;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000344 }
Mike Stump1eb44332009-09-09 15:08:12 +0000345
346 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000347 "Invalid #define in predefines buffer?");
Mike Stump1eb44332009-09-09 15:08:12 +0000348 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000349 (*ConflictPos)[MacroDefLen] != '(')
350 continue; // Longer macro name; keep trying.
Mike Stump1eb44332009-09-09 15:08:12 +0000351
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000352 // We found a conflicting macro definition.
353 break;
354 }
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000356 if (ConflictPos != CmdLineLines.end()) {
357 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
358 << MacroName;
359
360 // Show the definition of this macro within the PCH file.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000361 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
362 FindMacro(Buffers, Missing);
363 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
364 SourceLocation PCHMissingLoc =
365 SourceMgr.getLocForStartOfFile(MacroLoc.first)
366 .getFileLocWithOffset(MacroLoc.second);
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000367 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000368
369 ConflictingDefines = true;
370 continue;
371 }
Mike Stump1eb44332009-09-09 15:08:12 +0000372
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000373 // If the macro doesn't conflict, then we'll just pick up the macro
374 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000375 if (ConflictingDefines)
376 continue; // Don't complain if there are already conflicting defs
Mike Stump1eb44332009-09-09 15:08:12 +0000377
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000378 if (!MissingDefines) {
379 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
380 MissingDefines = true;
381 }
382
383 // Show the definition of this macro within the PCH file.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000384 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
385 FindMacro(Buffers, Missing);
386 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
387 SourceLocation PCHMissingLoc =
388 SourceMgr.getLocForStartOfFile(MacroLoc.first)
389 .getFileLocWithOffset(MacroLoc.second);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000390 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
391 }
Mike Stump1eb44332009-09-09 15:08:12 +0000392
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000393 if (ConflictingDefines)
394 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000395
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000396 // Determine what predefines were introduced based on command-line
397 // parameters that were not present when building the PCH
398 // file. Extra #defines are okay, so long as the identifiers being
399 // defined were not used within the precompiled header.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000400 std::vector<llvm::StringRef> ExtraPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000401 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
402 PCHLines.begin(), PCHLines.end(),
Mike Stump1eb44332009-09-09 15:08:12 +0000403 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000404 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000405 llvm::StringRef &Extra = ExtraPredefines[I];
406 if (!Extra.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000407 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
408 return true;
409 }
410
411 // This is an extra macro definition. Determine the name of the
412 // macro we're defining.
413 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000414 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000415 = Extra.find_first_of("( \n\r", StartOfMacroName);
416 assert(EndOfMacroName != std::string::npos &&
417 "Couldn't find the end of the macro name");
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000418 llvm::StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000419
420 // Check whether this name was used somewhere in the PCH file. If
421 // so, defining it as a macro could change behavior, so we reject
422 // the PCH file.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000423 if (IdentifierInfo *II = Reader.get(MacroName)) {
Daniel Dunbar4fda42e2009-11-11 00:52:00 +0000424 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000425 return true;
426 }
427
428 // Add this definition to the suggested predefines buffer.
429 SuggestedPredefines += Extra;
430 SuggestedPredefines += '\n';
431 }
432
433 // If we get here, it's because the predefines buffer had compatible
434 // contents. Accept the PCH file.
435 return false;
436}
437
Douglas Gregor12fab312010-03-16 16:35:32 +0000438void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
439 unsigned ID) {
440 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
441 ++NumHeaderInfos;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000442}
443
444void PCHValidator::ReadCounter(unsigned Value) {
445 PP.setCounterValue(Value);
446}
447
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000448//===----------------------------------------------------------------------===//
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000449// AST reader implementation
Douglas Gregor668c1a42009-04-21 22:25:48 +0000450//===----------------------------------------------------------------------===//
451
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000452void
Sebastian Redl571db7f2010-08-18 23:56:56 +0000453ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000454 DeserializationListener = Listener;
455 if (DeserializationListener)
456 DeserializationListener->SetReader(this);
457}
458
Chris Lattner4c6f9522009-04-27 05:14:47 +0000459
Douglas Gregor668c1a42009-04-21 22:25:48 +0000460namespace {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000461class ASTSelectorLookupTrait {
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000462 ASTReader &Reader;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000463
464public:
Sebastian Redl5d050072010-08-04 17:20:04 +0000465 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000466 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +0000467 ObjCMethodList Instance, Factory;
468 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000469
470 typedef Selector external_key_type;
471 typedef external_key_type internal_key_type;
472
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000473 explicit ASTSelectorLookupTrait(ASTReader &Reader) : Reader(Reader) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000474
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000475 static bool EqualKey(const internal_key_type& a,
476 const internal_key_type& b) {
477 return a == b;
478 }
Mike Stump1eb44332009-09-09 15:08:12 +0000479
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000480 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +0000481 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000482 }
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000484 // This hopefully will just get inlined and removed by the optimizer.
485 static const internal_key_type&
486 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000488 static std::pair<unsigned, unsigned>
489 ReadKeyDataLength(const unsigned char*& d) {
490 using namespace clang::io;
491 unsigned KeyLen = ReadUnalignedLE16(d);
492 unsigned DataLen = ReadUnalignedLE16(d);
493 return std::make_pair(KeyLen, DataLen);
494 }
Mike Stump1eb44332009-09-09 15:08:12 +0000495
Douglas Gregor83941df2009-04-25 17:48:32 +0000496 internal_key_type ReadKey(const unsigned char* d, unsigned) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000497 using namespace clang::io;
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000498 SelectorTable &SelTable = Reader.getContext()->Selectors;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000499 unsigned N = ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +0000500 IdentifierInfo *FirstII
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000501 = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
502 if (N == 0)
503 return SelTable.getNullarySelector(FirstII);
504 else if (N == 1)
505 return SelTable.getUnarySelector(FirstII);
506
507 llvm::SmallVector<IdentifierInfo *, 16> Args;
508 Args.push_back(FirstII);
509 for (unsigned I = 1; I != N; ++I)
510 Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)));
511
Douglas Gregor75fdb232009-05-22 22:45:36 +0000512 return SelTable.getSelector(N, Args.data());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000513 }
Mike Stump1eb44332009-09-09 15:08:12 +0000514
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000515 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
516 using namespace clang::io;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000517
518 data_type Result;
519
Sebastian Redl5d050072010-08-04 17:20:04 +0000520 Result.ID = ReadUnalignedLE32(d);
521 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
522 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
523
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000524 // Load instance methods
525 ObjCMethodList *Prev = 0;
526 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000527 ObjCMethodDecl *Method
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000528 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl5d050072010-08-04 17:20:04 +0000529 if (!Result.Instance.Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000530 // This is the first method, which is the easy case.
Sebastian Redl5d050072010-08-04 17:20:04 +0000531 Result.Instance.Method = Method;
532 Prev = &Result.Instance;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000533 continue;
534 }
535
Ted Kremenek298ed872010-02-11 00:53:01 +0000536 ObjCMethodList *Mem =
537 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
538 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000539 Prev = Prev->Next;
540 }
541
542 // Load factory methods
543 Prev = 0;
544 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000545 ObjCMethodDecl *Method
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000546 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl5d050072010-08-04 17:20:04 +0000547 if (!Result.Factory.Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000548 // This is the first method, which is the easy case.
Sebastian Redl5d050072010-08-04 17:20:04 +0000549 Result.Factory.Method = Method;
550 Prev = &Result.Factory;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000551 continue;
552 }
553
Ted Kremenek298ed872010-02-11 00:53:01 +0000554 ObjCMethodList *Mem =
555 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
556 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000557 Prev = Prev->Next;
558 }
559
560 return Result;
561 }
562};
Mike Stump1eb44332009-09-09 15:08:12 +0000563
564} // end anonymous namespace
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000565
566/// \brief The on-disk hash table used for the global method pool.
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000567typedef OnDiskChainedHashTable<ASTSelectorLookupTrait>
568 ASTSelectorLookupTable;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000569
Sebastian Redlc3632732010-10-05 15:59:54 +0000570namespace clang {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000571class ASTIdentifierLookupTrait {
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000572 ASTReader &Reader;
Sebastian Redlc3632732010-10-05 15:59:54 +0000573 ASTReader::PerFileData &F;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000574
575 // If we know the IdentifierInfo in advance, it is here and we will
576 // not build a new one. Used when deserializing information about an
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000577 // identifier that was constructed before the AST file was read.
Douglas Gregor668c1a42009-04-21 22:25:48 +0000578 IdentifierInfo *KnownII;
579
580public:
581 typedef IdentifierInfo * data_type;
582
583 typedef const std::pair<const char*, unsigned> external_key_type;
584
585 typedef external_key_type internal_key_type;
586
Sebastian Redlc3632732010-10-05 15:59:54 +0000587 ASTIdentifierLookupTrait(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redld27d3fc2010-07-21 22:31:37 +0000588 IdentifierInfo *II = 0)
Sebastian Redlc3632732010-10-05 15:59:54 +0000589 : Reader(Reader), F(F), KnownII(II) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000590
Douglas Gregor668c1a42009-04-21 22:25:48 +0000591 static bool EqualKey(const internal_key_type& a,
592 const internal_key_type& b) {
593 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
594 : false;
595 }
Mike Stump1eb44332009-09-09 15:08:12 +0000596
Douglas Gregor668c1a42009-04-21 22:25:48 +0000597 static unsigned ComputeHash(const internal_key_type& a) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000598 return llvm::HashString(llvm::StringRef(a.first, a.second));
Douglas Gregor668c1a42009-04-21 22:25:48 +0000599 }
Mike Stump1eb44332009-09-09 15:08:12 +0000600
Douglas Gregor668c1a42009-04-21 22:25:48 +0000601 // This hopefully will just get inlined and removed by the optimizer.
602 static const internal_key_type&
603 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump1eb44332009-09-09 15:08:12 +0000604
Douglas Gregor668c1a42009-04-21 22:25:48 +0000605 static std::pair<unsigned, unsigned>
606 ReadKeyDataLength(const unsigned char*& d) {
607 using namespace clang::io;
Douglas Gregor5f8e3302009-04-25 20:26:24 +0000608 unsigned DataLen = ReadUnalignedLE16(d);
Douglas Gregord6595a42009-04-25 21:04:17 +0000609 unsigned KeyLen = ReadUnalignedLE16(d);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000610 return std::make_pair(KeyLen, DataLen);
611 }
Mike Stump1eb44332009-09-09 15:08:12 +0000612
Douglas Gregor668c1a42009-04-21 22:25:48 +0000613 static std::pair<const char*, unsigned>
614 ReadKey(const unsigned char* d, unsigned n) {
615 assert(n >= 2 && d[n-1] == '\0');
616 return std::make_pair((const char*) d, n-1);
617 }
Mike Stump1eb44332009-09-09 15:08:12 +0000618
619 IdentifierInfo *ReadData(const internal_key_type& k,
Douglas Gregor668c1a42009-04-21 22:25:48 +0000620 const unsigned char* d,
621 unsigned DataLen) {
622 using namespace clang::io;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000623 IdentID ID = ReadUnalignedLE32(d);
Douglas Gregora92193e2009-04-28 21:18:29 +0000624 bool IsInteresting = ID & 0x01;
625
626 // Wipe out the "is interesting" bit.
627 ID = ID >> 1;
628
629 if (!IsInteresting) {
Sebastian Redl083abdf2010-07-27 23:01:28 +0000630 // For uninteresting identifiers, just build the IdentifierInfo
Douglas Gregora92193e2009-04-28 21:18:29 +0000631 // and associate it with the persistent ID.
632 IdentifierInfo *II = KnownII;
633 if (!II)
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000634 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregora92193e2009-04-28 21:18:29 +0000635 Reader.SetIdentifierInfo(ID, II);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000636 II->setIsFromAST();
Douglas Gregora92193e2009-04-28 21:18:29 +0000637 return II;
638 }
639
Douglas Gregor5998da52009-04-28 21:32:13 +0000640 unsigned Bits = ReadUnalignedLE16(d);
Douglas Gregor2deaea32009-04-22 18:49:13 +0000641 bool CPlusPlusOperatorKeyword = Bits & 0x01;
642 Bits >>= 1;
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000643 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
644 Bits >>= 1;
Douglas Gregor2deaea32009-04-22 18:49:13 +0000645 bool Poisoned = Bits & 0x01;
646 Bits >>= 1;
647 bool ExtensionToken = Bits & 0x01;
648 Bits >>= 1;
649 bool hasMacroDefinition = Bits & 0x01;
650 Bits >>= 1;
651 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
652 Bits >>= 10;
Mike Stump1eb44332009-09-09 15:08:12 +0000653
Douglas Gregor2deaea32009-04-22 18:49:13 +0000654 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregor5998da52009-04-28 21:32:13 +0000655 DataLen -= 6;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000656
657 // Build the IdentifierInfo itself and link the identifier ID with
658 // the new IdentifierInfo.
659 IdentifierInfo *II = KnownII;
660 if (!II)
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000661 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000662 Reader.SetIdentifierInfo(ID, II);
663
Douglas Gregor2deaea32009-04-22 18:49:13 +0000664 // Set or check the various bits in the IdentifierInfo structure.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000665 // Token IDs are read-only.
666 if (HasRevertedTokenIDToIdentifier)
667 II->RevertTokenIDToIdentifier();
Douglas Gregor2deaea32009-04-22 18:49:13 +0000668 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
Mike Stump1eb44332009-09-09 15:08:12 +0000669 assert(II->isExtensionToken() == ExtensionToken &&
Douglas Gregor2deaea32009-04-22 18:49:13 +0000670 "Incorrect extension token flag");
671 (void)ExtensionToken;
672 II->setIsPoisoned(Poisoned);
673 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
674 "Incorrect C++ operator keyword flag");
675 (void)CPlusPlusOperatorKeyword;
676
Douglas Gregor37e26842009-04-21 23:56:24 +0000677 // If this identifier is a macro, deserialize the macro
678 // definition.
679 if (hasMacroDefinition) {
Douglas Gregor5998da52009-04-28 21:32:13 +0000680 uint32_t Offset = ReadUnalignedLE32(d);
Sebastian Redlc3632732010-10-05 15:59:54 +0000681 Reader.ReadMacroRecord(F, Offset);
Douglas Gregor5998da52009-04-28 21:32:13 +0000682 DataLen -= 4;
Douglas Gregor37e26842009-04-21 23:56:24 +0000683 }
Douglas Gregor668c1a42009-04-21 22:25:48 +0000684
685 // Read all of the declarations visible at global scope with this
686 // name.
Chris Lattner6bf690f2009-04-27 22:17:41 +0000687 if (Reader.getContext() == 0) return II;
Douglas Gregord89275b2009-07-06 18:54:52 +0000688 if (DataLen > 0) {
689 llvm::SmallVector<uint32_t, 4> DeclIDs;
690 for (; DataLen > 0; DataLen -= 4)
691 DeclIDs.push_back(ReadUnalignedLE32(d));
692 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000693 }
Mike Stump1eb44332009-09-09 15:08:12 +0000694
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000695 II->setIsFromAST();
Douglas Gregor668c1a42009-04-21 22:25:48 +0000696 return II;
697 }
698};
Mike Stump1eb44332009-09-09 15:08:12 +0000699
700} // end anonymous namespace
Douglas Gregor668c1a42009-04-21 22:25:48 +0000701
702/// \brief The on-disk hash table used to contain information about
703/// all of the identifiers in the program.
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000704typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait>
705 ASTIdentifierLookupTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000706
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000707namespace {
708class ASTDeclContextNameLookupTrait {
709 ASTReader &Reader;
710
711public:
712 /// \brief Pair of begin/end iterators for DeclIDs.
713 typedef std::pair<DeclID *, DeclID *> data_type;
714
715 /// \brief Special internal key for declaration names.
716 /// The hash table creates keys for comparison; we do not create
717 /// a DeclarationName for the internal key to avoid deserializing types.
718 struct DeclNameKey {
719 DeclarationName::NameKind Kind;
720 uint64_t Data;
721 DeclNameKey() : Kind((DeclarationName::NameKind)0), Data(0) { }
722 };
723
724 typedef DeclarationName external_key_type;
725 typedef DeclNameKey internal_key_type;
726
727 explicit ASTDeclContextNameLookupTrait(ASTReader &Reader) : Reader(Reader) { }
728
729 static bool EqualKey(const internal_key_type& a,
730 const internal_key_type& b) {
731 return a.Kind == b.Kind && a.Data == b.Data;
732 }
733
734 unsigned ComputeHash(const DeclNameKey &Key) const {
735 llvm::FoldingSetNodeID ID;
736 ID.AddInteger(Key.Kind);
737
738 switch (Key.Kind) {
739 case DeclarationName::Identifier:
740 case DeclarationName::CXXLiteralOperatorName:
741 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
742 break;
743 case DeclarationName::ObjCZeroArgSelector:
744 case DeclarationName::ObjCOneArgSelector:
745 case DeclarationName::ObjCMultiArgSelector:
746 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
747 break;
748 case DeclarationName::CXXConstructorName:
749 case DeclarationName::CXXDestructorName:
750 case DeclarationName::CXXConversionFunctionName:
751 ID.AddInteger((TypeID)Key.Data);
752 break;
753 case DeclarationName::CXXOperatorName:
754 ID.AddInteger((OverloadedOperatorKind)Key.Data);
755 break;
756 case DeclarationName::CXXUsingDirective:
757 break;
758 }
759
760 return ID.ComputeHash();
761 }
762
763 internal_key_type GetInternalKey(const external_key_type& Name) const {
764 DeclNameKey Key;
765 Key.Kind = Name.getNameKind();
766 switch (Name.getNameKind()) {
767 case DeclarationName::Identifier:
768 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
769 break;
770 case DeclarationName::ObjCZeroArgSelector:
771 case DeclarationName::ObjCOneArgSelector:
772 case DeclarationName::ObjCMultiArgSelector:
773 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
774 break;
775 case DeclarationName::CXXConstructorName:
776 case DeclarationName::CXXDestructorName:
777 case DeclarationName::CXXConversionFunctionName:
778 Key.Data = Reader.GetTypeID(Name.getCXXNameType());
779 break;
780 case DeclarationName::CXXOperatorName:
781 Key.Data = Name.getCXXOverloadedOperator();
782 break;
783 case DeclarationName::CXXLiteralOperatorName:
784 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
785 break;
786 case DeclarationName::CXXUsingDirective:
787 break;
788 }
789
790 return Key;
791 }
792
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000793 external_key_type GetExternalKey(const internal_key_type& Key) const {
794 ASTContext *Context = Reader.getContext();
795 switch (Key.Kind) {
796 case DeclarationName::Identifier:
797 return DeclarationName((IdentifierInfo*)Key.Data);
798
799 case DeclarationName::ObjCZeroArgSelector:
800 case DeclarationName::ObjCOneArgSelector:
801 case DeclarationName::ObjCMultiArgSelector:
802 return DeclarationName(Selector(Key.Data));
803
804 case DeclarationName::CXXConstructorName:
805 return Context->DeclarationNames.getCXXConstructorName(
806 Context->getCanonicalType(Reader.GetType(Key.Data)));
807
808 case DeclarationName::CXXDestructorName:
809 return Context->DeclarationNames.getCXXDestructorName(
810 Context->getCanonicalType(Reader.GetType(Key.Data)));
811
812 case DeclarationName::CXXConversionFunctionName:
813 return Context->DeclarationNames.getCXXConversionFunctionName(
814 Context->getCanonicalType(Reader.GetType(Key.Data)));
815
816 case DeclarationName::CXXOperatorName:
817 return Context->DeclarationNames.getCXXOperatorName(
818 (OverloadedOperatorKind)Key.Data);
819
820 case DeclarationName::CXXLiteralOperatorName:
821 return Context->DeclarationNames.getCXXLiteralOperatorName(
822 (IdentifierInfo*)Key.Data);
823
824 case DeclarationName::CXXUsingDirective:
825 return DeclarationName::getUsingDirectiveName();
826 }
827
828 llvm_unreachable("Invalid Name Kind ?");
829 }
830
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000831 static std::pair<unsigned, unsigned>
832 ReadKeyDataLength(const unsigned char*& d) {
833 using namespace clang::io;
834 unsigned KeyLen = ReadUnalignedLE16(d);
835 unsigned DataLen = ReadUnalignedLE16(d);
836 return std::make_pair(KeyLen, DataLen);
837 }
838
839 internal_key_type ReadKey(const unsigned char* d, unsigned) {
840 using namespace clang::io;
841
842 DeclNameKey Key;
843 Key.Kind = (DeclarationName::NameKind)*d++;
844 switch (Key.Kind) {
845 case DeclarationName::Identifier:
846 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
847 break;
848 case DeclarationName::ObjCZeroArgSelector:
849 case DeclarationName::ObjCOneArgSelector:
850 case DeclarationName::ObjCMultiArgSelector:
851 Key.Data =
852 (uint64_t)Reader.DecodeSelector(ReadUnalignedLE32(d)).getAsOpaquePtr();
853 break;
854 case DeclarationName::CXXConstructorName:
855 case DeclarationName::CXXDestructorName:
856 case DeclarationName::CXXConversionFunctionName:
857 Key.Data = ReadUnalignedLE32(d); // TypeID
858 break;
859 case DeclarationName::CXXOperatorName:
860 Key.Data = *d++; // OverloadedOperatorKind
861 break;
862 case DeclarationName::CXXLiteralOperatorName:
863 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
864 break;
865 case DeclarationName::CXXUsingDirective:
866 break;
867 }
868
869 return Key;
870 }
871
872 data_type ReadData(internal_key_type, const unsigned char* d,
873 unsigned DataLen) {
874 using namespace clang::io;
875 unsigned NumDecls = ReadUnalignedLE16(d);
876 DeclID *Start = (DeclID *)d;
877 return std::make_pair(Start, Start + NumDecls);
878 }
879};
880
881} // end anonymous namespace
882
883/// \brief The on-disk hash table used for the DeclContext's Name lookup table.
884typedef OnDiskChainedHashTable<ASTDeclContextNameLookupTrait>
885 ASTDeclContextNameLookupTable;
886
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000887bool ASTReader::ReadDeclContextStorage(llvm::BitstreamCursor &Cursor,
888 const std::pair<uint64_t, uint64_t> &Offsets,
889 DeclContextInfo &Info) {
890 SavedStreamPosition SavedPosition(Cursor);
891 // First the lexical decls.
892 if (Offsets.first != 0) {
893 Cursor.JumpToBit(Offsets.first);
894
895 RecordData Record;
896 const char *Blob;
897 unsigned BlobLen;
898 unsigned Code = Cursor.ReadCode();
899 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
900 if (RecCode != DECL_CONTEXT_LEXICAL) {
901 Error("Expected lexical block");
902 return true;
903 }
904
905 Info.LexicalDecls = reinterpret_cast<const DeclID*>(Blob);
906 Info.NumLexicalDecls = BlobLen / sizeof(DeclID);
907 } else {
908 Info.LexicalDecls = 0;
909 Info.NumLexicalDecls = 0;
910 }
911
912 // Now the lookup table.
913 if (Offsets.second != 0) {
914 Cursor.JumpToBit(Offsets.second);
915
916 RecordData Record;
917 const char *Blob;
918 unsigned BlobLen;
919 unsigned Code = Cursor.ReadCode();
920 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
921 if (RecCode != DECL_CONTEXT_VISIBLE) {
922 Error("Expected visible lookup table block");
923 return true;
924 }
925 Info.NameLookupTableData
926 = ASTDeclContextNameLookupTable::Create(
927 (const unsigned char *)Blob + Record[0],
928 (const unsigned char *)Blob,
929 ASTDeclContextNameLookupTrait(*this));
Sebastian Redl0ea8f7f2010-08-24 00:50:00 +0000930 } else {
931 Info.NameLookupTableData = 0;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000932 }
933
934 return false;
935}
936
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000937void ASTReader::Error(const char *Msg) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000938 Diag(diag::err_fe_pch_malformed) << Msg;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000939}
940
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000941/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000942bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000943 if (Listener)
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000944 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000945 ActualOriginalFileName,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000946 SuggestedPredefines);
Douglas Gregore721f952009-04-28 18:58:38 +0000947 return false;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000948}
949
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000950//===----------------------------------------------------------------------===//
951// Source Manager Deserialization
952//===----------------------------------------------------------------------===//
953
Douglas Gregorbd945002009-04-13 16:31:14 +0000954/// \brief Read the line table in the source manager block.
Sebastian Redlc3632732010-10-05 15:59:54 +0000955/// \returns true if there was an error.
956bool ASTReader::ParseLineTable(PerFileData &F,
957 llvm::SmallVectorImpl<uint64_t> &Record) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000958 unsigned Idx = 0;
959 LineTableInfo &LineTable = SourceMgr.getLineTable();
960
961 // Parse the file names
Douglas Gregorff0a9872009-04-13 17:12:42 +0000962 std::map<int, int> FileIDs;
963 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000964 // Extract the file name
965 unsigned FilenameLen = Record[Idx++];
966 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
967 Idx += FilenameLen;
Douglas Gregore650c8c2009-07-07 00:12:59 +0000968 MaybeAddSystemRootToFilename(Filename);
Mike Stump1eb44332009-09-09 15:08:12 +0000969 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
Douglas Gregorff0a9872009-04-13 17:12:42 +0000970 Filename.size());
Douglas Gregorbd945002009-04-13 16:31:14 +0000971 }
972
973 // Parse the line entries
974 std::vector<LineEntry> Entries;
975 while (Idx < Record.size()) {
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000976 int FID = Record[Idx++];
Douglas Gregorbd945002009-04-13 16:31:14 +0000977
978 // Extract the line entries
979 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000980 assert(NumEntries && "Numentries is 00000");
Douglas Gregorbd945002009-04-13 16:31:14 +0000981 Entries.clear();
982 Entries.reserve(NumEntries);
983 for (unsigned I = 0; I != NumEntries; ++I) {
984 unsigned FileOffset = Record[Idx++];
985 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000986 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump1eb44332009-09-09 15:08:12 +0000987 SrcMgr::CharacteristicKind FileKind
Douglas Gregorbd945002009-04-13 16:31:14 +0000988 = (SrcMgr::CharacteristicKind)Record[Idx++];
989 unsigned IncludeOffset = Record[Idx++];
990 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
991 FileKind, IncludeOffset));
992 }
993 LineTable.AddEntry(FID, Entries);
994 }
995
996 return false;
997}
998
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000999namespace {
1000
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001001class ASTStatData {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001002public:
1003 const bool hasStat;
1004 const ino_t ino;
1005 const dev_t dev;
1006 const mode_t mode;
1007 const time_t mtime;
1008 const off_t size;
Mike Stump1eb44332009-09-09 15:08:12 +00001009
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001010 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Mike Stump1eb44332009-09-09 15:08:12 +00001011 : hasStat(true), ino(i), dev(d), mode(mo), mtime(m), size(s) {}
1012
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001013 ASTStatData()
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001014 : hasStat(false), ino(0), dev(0), mode(0), mtime(0), size(0) {}
1015};
1016
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001017class ASTStatLookupTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001018 public:
1019 typedef const char *external_key_type;
1020 typedef const char *internal_key_type;
1021
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001022 typedef ASTStatData data_type;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001023
1024 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001025 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001026 }
1027
1028 static internal_key_type GetInternalKey(const char *path) { return path; }
1029
1030 static bool EqualKey(internal_key_type a, internal_key_type b) {
1031 return strcmp(a, b) == 0;
1032 }
1033
1034 static std::pair<unsigned, unsigned>
1035 ReadKeyDataLength(const unsigned char*& d) {
1036 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1037 unsigned DataLen = (unsigned) *d++;
1038 return std::make_pair(KeyLen + 1, DataLen);
1039 }
1040
1041 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1042 return (const char *)d;
1043 }
1044
1045 static data_type ReadData(const internal_key_type, const unsigned char *d,
1046 unsigned /*DataLen*/) {
1047 using namespace clang::io;
1048
1049 if (*d++ == 1)
1050 return data_type();
1051
1052 ino_t ino = (ino_t) ReadUnalignedLE32(d);
1053 dev_t dev = (dev_t) ReadUnalignedLE32(d);
1054 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +00001055 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001056 off_t size = (off_t) ReadUnalignedLE64(d);
1057 return data_type(ino, dev, mode, mtime, size);
1058 }
1059};
1060
1061/// \brief stat() cache for precompiled headers.
1062///
1063/// This cache is very similar to the stat cache used by pretokenized
1064/// headers.
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001065class ASTStatCache : public StatSysCallCache {
1066 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001067 CacheTy *Cache;
1068
1069 unsigned &NumStatHits, &NumStatMisses;
Mike Stump1eb44332009-09-09 15:08:12 +00001070public:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001071 ASTStatCache(const unsigned char *Buckets,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001072 const unsigned char *Base,
1073 unsigned &NumStatHits,
Mike Stump1eb44332009-09-09 15:08:12 +00001074 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
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001081 int stat(const char *path, struct stat *buf) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001082 // Do the lookup for the file's data in the AST file.
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001083 CacheTy::iterator I = Cache->find(path);
1084
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001085 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001086 if (I == Cache->end()) {
1087 ++NumStatMisses;
Douglas Gregor52e71082009-10-16 18:18:30 +00001088 return StatSysCallCache::stat(path, buf);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001089 }
Mike Stump1eb44332009-09-09 15:08:12 +00001090
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001091 ++NumStatHits;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001092 ASTStatData Data = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001093
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001094 if (!Data.hasStat)
1095 return 1;
1096
1097 buf->st_ino = Data.ino;
1098 buf->st_dev = Data.dev;
1099 buf->st_mtime = Data.mtime;
1100 buf->st_mode = Data.mode;
1101 buf->st_size = Data.size;
1102 return 0;
1103 }
1104};
1105} // end anonymous namespace
1106
1107
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001108/// \brief Read a source manager block
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001109ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(PerFileData &F) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001110 using namespace SrcMgr;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001111
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001112 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001113
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001114 // Set the source-location entry cursor to the current position in
1115 // the stream. This cursor will be used to read the contents of the
1116 // source manager block initially, and then lazily read
1117 // source-location entries as needed.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001118 SLocEntryCursor = F.Stream;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001119
1120 // The stream itself is going to skip over the source manager block.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001121 if (F.Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001122 Error("malformed block record in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001123 return Failure;
1124 }
1125
1126 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001127 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001128 Error("malformed source manager block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001129 return Failure;
1130 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001131
Douglas Gregor14f79002009-04-10 03:52:48 +00001132 RecordData Record;
1133 while (true) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001134 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregor14f79002009-04-10 03:52:48 +00001135 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001136 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001137 Error("error at end of Source Manager block in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001138 return Failure;
1139 }
Douglas Gregore1d918e2009-04-10 23:10:45 +00001140 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +00001141 }
Mike Stump1eb44332009-09-09 15:08:12 +00001142
Douglas Gregor14f79002009-04-10 03:52:48 +00001143 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1144 // No known subblocks, always skip them.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001145 SLocEntryCursor.ReadSubBlockID();
1146 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001147 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001148 return Failure;
1149 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001150 continue;
1151 }
Mike Stump1eb44332009-09-09 15:08:12 +00001152
Douglas Gregor14f79002009-04-10 03:52:48 +00001153 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001154 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregor14f79002009-04-10 03:52:48 +00001155 continue;
1156 }
Mike Stump1eb44332009-09-09 15:08:12 +00001157
Douglas Gregor14f79002009-04-10 03:52:48 +00001158 // Read a record.
1159 const char *BlobStart;
1160 unsigned BlobLen;
1161 Record.clear();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001162 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001163 default: // Default behavior: ignore.
1164 break;
1165
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001166 case SM_LINE_TABLE:
Sebastian Redlc3632732010-10-05 15:59:54 +00001167 if (ParseLineTable(F, Record))
Douglas Gregorbd945002009-04-13 16:31:14 +00001168 return Failure;
Chris Lattner2c78b872009-04-14 23:22:57 +00001169 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001170
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001171 case SM_SLOC_FILE_ENTRY:
1172 case SM_SLOC_BUFFER_ENTRY:
1173 case SM_SLOC_INSTANTIATION_ENTRY:
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001174 // Once we hit one of the source location entries, we're done.
1175 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +00001176 }
1177 }
1178}
1179
Sebastian Redl190faf72010-07-20 21:50:20 +00001180/// \brief Get a cursor that's correctly positioned for reading the source
1181/// location entry with the given ID.
Sebastian Redlc3632732010-10-05 15:59:54 +00001182ASTReader::PerFileData *ASTReader::SLocCursorForID(unsigned ID) {
Sebastian Redl190faf72010-07-20 21:50:20 +00001183 assert(ID != 0 && ID <= TotalNumSLocEntries &&
1184 "SLocCursorForID should only be called for real IDs.");
1185
1186 ID -= 1;
1187 PerFileData *F = 0;
1188 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1189 F = Chain[N - I - 1];
1190 if (ID < F->LocalNumSLocEntries)
1191 break;
1192 ID -= F->LocalNumSLocEntries;
1193 }
1194 assert(F && F->LocalNumSLocEntries > ID && "Chain corrupted");
1195
1196 F->SLocEntryCursor.JumpToBit(F->SLocOffsets[ID]);
Sebastian Redlc3632732010-10-05 15:59:54 +00001197 return F;
Sebastian Redl190faf72010-07-20 21:50:20 +00001198}
1199
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001200/// \brief Read in the source location entry with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001201ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(unsigned ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001202 if (ID == 0)
1203 return Success;
1204
1205 if (ID > TotalNumSLocEntries) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001206 Error("source location entry ID out-of-range for AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001207 return Failure;
1208 }
1209
Sebastian Redlc3632732010-10-05 15:59:54 +00001210 PerFileData *F = SLocCursorForID(ID);
1211 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001212
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001213 ++NumSLocEntriesRead;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001214 unsigned Code = SLocEntryCursor.ReadCode();
1215 if (Code == llvm::bitc::END_BLOCK ||
1216 Code == llvm::bitc::ENTER_SUBBLOCK ||
1217 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001218 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001219 return Failure;
1220 }
1221
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001222 RecordData Record;
1223 const char *BlobStart;
1224 unsigned BlobLen;
1225 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1226 default:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001227 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001228 return Failure;
1229
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001230 case SM_SLOC_FILE_ENTRY: {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001231 std::string Filename(BlobStart, BlobStart + BlobLen);
1232 MaybeAddSystemRootToFilename(Filename);
1233 const FileEntry *File = FileMgr.getFile(Filename);
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001234 if (File == 0) {
1235 std::string ErrorStr = "could not find file '";
Douglas Gregore650c8c2009-07-07 00:12:59 +00001236 ErrorStr += Filename;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001237 ErrorStr += "' referenced by AST file";
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001238 Error(ErrorStr.c_str());
1239 return Failure;
1240 }
Mike Stump1eb44332009-09-09 15:08:12 +00001241
Douglas Gregor2d52be52010-03-21 22:49:54 +00001242 if (Record.size() < 10) {
Ted Kremenek1857f622010-03-18 21:23:05 +00001243 Error("source location entry is incorrect");
1244 return Failure;
1245 }
1246
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001247 if (!DisableValidation &&
1248 ((off_t)Record[4] != File->getSize()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001249#if !defined(LLVM_ON_WIN32)
1250 // In our regression testing, the Windows file system seems to
1251 // have inconsistent modification times that sometimes
1252 // erroneously trigger this error-handling path.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001253 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001254#endif
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001255 )) {
Douglas Gregor2d52be52010-03-21 22:49:54 +00001256 Diag(diag::err_fe_pch_file_modified)
1257 << Filename;
1258 return Failure;
1259 }
1260
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001261 FileID FID = SourceMgr.createFileID(File,
Sebastian Redlc3632732010-10-05 15:59:54 +00001262 ReadSourceLocation(*F, Record[1]),
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001263 (SrcMgr::CharacteristicKind)Record[2],
1264 ID, Record[0]);
1265 if (Record[3])
1266 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile())
1267 .setHasLineDirectives();
1268
Douglas Gregor12fab312010-03-16 16:35:32 +00001269 // Reconstruct header-search information for this file.
1270 HeaderFileInfo HFI;
Douglas Gregor2d52be52010-03-21 22:49:54 +00001271 HFI.isImport = Record[6];
1272 HFI.DirInfo = Record[7];
1273 HFI.NumIncludes = Record[8];
1274 HFI.ControllingMacroID = Record[9];
Douglas Gregor12fab312010-03-16 16:35:32 +00001275 if (Listener)
1276 Listener->ReadHeaderFileInfo(HFI, File->getUID());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001277 break;
1278 }
1279
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001280 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001281 const char *Name = BlobStart;
1282 unsigned Offset = Record[0];
1283 unsigned Code = SLocEntryCursor.ReadCode();
1284 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001285 unsigned RecCode
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001286 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001287
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001288 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001289 Error("AST record has invalid code");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001290 return Failure;
1291 }
1292
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001293 llvm::MemoryBuffer *Buffer
Chris Lattnera0a270c2010-04-05 22:42:27 +00001294 = llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(BlobStart, BlobLen - 1),
1295 Name);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001296 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset);
Mike Stump1eb44332009-09-09 15:08:12 +00001297
Douglas Gregor92b059e2009-04-28 20:33:11 +00001298 if (strcmp(Name, "<built-in>") == 0) {
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +00001299 PCHPredefinesBlock Block = {
1300 BufferID,
1301 llvm::StringRef(BlobStart, BlobLen - 1)
1302 };
1303 PCHPredefinesBuffers.push_back(Block);
Douglas Gregor92b059e2009-04-28 20:33:11 +00001304 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001305
1306 break;
1307 }
1308
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001309 case SM_SLOC_INSTANTIATION_ENTRY: {
Sebastian Redlc3632732010-10-05 15:59:54 +00001310 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001311 SourceMgr.createInstantiationLoc(SpellingLoc,
Sebastian Redlc3632732010-10-05 15:59:54 +00001312 ReadSourceLocation(*F, Record[2]),
1313 ReadSourceLocation(*F, Record[3]),
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001314 Record[4],
1315 ID,
1316 Record[0]);
1317 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001318 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001319 }
1320
1321 return Success;
1322}
1323
Chris Lattner6367f6d2009-04-27 01:05:14 +00001324/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1325/// specified cursor. Read the abbreviations that are at the top of the block
1326/// and then leave the cursor pointing into the block.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001327bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattner6367f6d2009-04-27 01:05:14 +00001328 unsigned BlockID) {
1329 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001330 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001331 return Failure;
1332 }
Mike Stump1eb44332009-09-09 15:08:12 +00001333
Chris Lattner6367f6d2009-04-27 01:05:14 +00001334 while (true) {
1335 unsigned Code = Cursor.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00001336
Chris Lattner6367f6d2009-04-27 01:05:14 +00001337 // We expect all abbrevs to be at the start of the block.
1338 if (Code != llvm::bitc::DEFINE_ABBREV)
1339 return false;
1340 Cursor.ReadAbbrevRecord();
1341 }
1342}
1343
Sebastian Redlc3632732010-10-05 15:59:54 +00001344void ASTReader::ReadMacroRecord(PerFileData &F, uint64_t Offset) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001345 assert(PP && "Forgot to set Preprocessor ?");
Sebastian Redlc3632732010-10-05 15:59:54 +00001346 llvm::BitstreamCursor &Stream = F.Stream;
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:
1361 return;
1362
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 Gregor37e26842009-04-21 23:56:24 +00001368 return;
1369 }
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.
1379 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001380 PreprocessorRecordTypes RecType =
1381 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
Douglas Gregor37e26842009-04-21 23:56:24 +00001382 switch (RecType) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001383 case PP_MACRO_OBJECT_LIKE:
1384 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001385 // If we already have a macro, that means that we've hit the end
1386 // of the definition of the macro we were looking for. We're
1387 // done.
1388 if (Macro)
1389 return;
1390
1391 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1392 if (II == 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001393 Error("macro must have a name in AST file");
Douglas Gregor37e26842009-04-21 23:56:24 +00001394 return;
1395 }
Sebastian Redlc3632732010-10-05 15:59:54 +00001396 SourceLocation Loc = ReadSourceLocation(F, Record[1]);
Douglas Gregor37e26842009-04-21 23:56:24 +00001397 bool isUsed = Record[2];
Mike Stump1eb44332009-09-09 15:08:12 +00001398
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001399 MacroInfo *MI = PP->AllocateMacroInfo(Loc);
Douglas Gregor37e26842009-04-21 23:56:24 +00001400 MI->setIsUsed(isUsed);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001401 MI->setIsFromAST();
Mike Stump1eb44332009-09-09 15:08:12 +00001402
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001403 unsigned NextIndex = 3;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001404 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregor37e26842009-04-21 23:56:24 +00001405 // Decode function-like macro info.
1406 bool isC99VarArgs = Record[3];
1407 bool isGNUVarArgs = Record[4];
1408 MacroArgs.clear();
1409 unsigned NumArgs = Record[5];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001410 NextIndex = 6 + NumArgs;
Douglas Gregor37e26842009-04-21 23:56:24 +00001411 for (unsigned i = 0; i != NumArgs; ++i)
1412 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1413
1414 // Install function-like macro info.
1415 MI->setIsFunctionLike();
1416 if (isC99VarArgs) MI->setIsC99Varargs();
1417 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor75fdb232009-05-22 22:45:36 +00001418 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001419 PP->getPreprocessorAllocator());
Douglas Gregor37e26842009-04-21 23:56:24 +00001420 }
1421
1422 // Finally, install the macro.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001423 PP->setMacroInfo(II, MI);
Douglas Gregor37e26842009-04-21 23:56:24 +00001424
1425 // Remember that we saw this macro last so that we add the tokens that
1426 // form its body to it.
1427 Macro = MI;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001428
1429 if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) {
1430 // We have a macro definition. Load it now.
1431 PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro,
1432 getMacroDefinition(Record[NextIndex]));
1433 }
1434
Douglas Gregor37e26842009-04-21 23:56:24 +00001435 ++NumMacrosRead;
1436 break;
1437 }
Mike Stump1eb44332009-09-09 15:08:12 +00001438
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001439 case PP_TOKEN: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001440 // If we see a TOKEN before a PP_MACRO_*, then the file is
1441 // erroneous, just pretend we didn't see this.
1442 if (Macro == 0) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001443
Douglas Gregor37e26842009-04-21 23:56:24 +00001444 Token Tok;
1445 Tok.startToken();
Sebastian Redlc3632732010-10-05 15:59:54 +00001446 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001447 Tok.setLength(Record[1]);
1448 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1449 Tok.setIdentifierInfo(II);
1450 Tok.setKind((tok::TokenKind)Record[3]);
1451 Tok.setFlag((Token::TokenFlags)Record[4]);
1452 Macro->AddTokenToBody(Tok);
1453 break;
1454 }
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001455
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001456 case PP_MACRO_INSTANTIATION: {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001457 // If we already have a macro, that means that we've hit the end
1458 // of the definition of the macro we were looking for. We're
1459 // done.
1460 if (Macro)
1461 return;
1462
1463 if (!PP->getPreprocessingRecord()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001464 Error("missing preprocessing record in AST file");
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001465 return;
1466 }
1467
1468 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1469 if (PPRec.getPreprocessedEntity(Record[0]))
1470 return;
1471
1472 MacroInstantiation *MI
1473 = new (PPRec) MacroInstantiation(DecodeIdentifierInfo(Record[3]),
Sebastian Redlc3632732010-10-05 15:59:54 +00001474 SourceRange(ReadSourceLocation(F, Record[1]),
1475 ReadSourceLocation(F, Record[2])),
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001476 getMacroDefinition(Record[4]));
1477 PPRec.SetPreallocatedEntity(Record[0], MI);
1478 return;
1479 }
1480
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001481 case PP_MACRO_DEFINITION: {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001482 // If we already have a macro, that means that we've hit the end
1483 // of the definition of the macro we were looking for. We're
1484 // done.
1485 if (Macro)
1486 return;
1487
1488 if (!PP->getPreprocessingRecord()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001489 Error("missing preprocessing record in AST file");
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001490 return;
1491 }
1492
1493 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1494 if (PPRec.getPreprocessedEntity(Record[0]))
1495 return;
1496
Douglas Gregor77424bc2010-10-02 19:29:26 +00001497 if (Record[1] > MacroDefinitionsLoaded.size()) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001498 Error("out-of-bounds macro definition record");
1499 return;
1500 }
1501
Douglas Gregor77424bc2010-10-02 19:29:26 +00001502 // Decode the identifier info and then check again; if the macro is
1503 // still defined and associated with the identifier,
1504 IdentifierInfo *II = DecodeIdentifierInfo(Record[4]);
1505 if (!MacroDefinitionsLoaded[Record[1] - 1]) {
1506 MacroDefinition *MD
1507 = new (PPRec) MacroDefinition(II,
Sebastian Redlc3632732010-10-05 15:59:54 +00001508 ReadSourceLocation(F, Record[5]),
Douglas Gregorb1a7d9a2010-10-01 20:33:34 +00001509 SourceRange(
Sebastian Redlc3632732010-10-05 15:59:54 +00001510 ReadSourceLocation(F, Record[2]),
1511 ReadSourceLocation(F, Record[3])));
Douglas Gregor77424bc2010-10-02 19:29:26 +00001512
1513 PPRec.SetPreallocatedEntity(Record[0], MD);
1514 MacroDefinitionsLoaded[Record[1] - 1] = MD;
1515
1516 if (DeserializationListener)
1517 DeserializationListener->MacroDefinitionRead(Record[1], MD);
1518 }
1519
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001520 return;
1521 }
Sebastian Redlb57a6242010-09-27 22:18:47 +00001522 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001523 }
1524}
1525
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001526void ASTReader::ReadDefinedMacros() {
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001527 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redlc3632732010-10-05 15:59:54 +00001528 PerFileData &F = *Chain[N - I - 1];
1529 llvm::BitstreamCursor &MacroCursor = F.MacroCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001530
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001531 // If there was no preprocessor block, skip this file.
1532 if (!MacroCursor.getBitStreamReader())
1533 continue;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001534
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001535 llvm::BitstreamCursor Cursor = MacroCursor;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001536 if (Cursor.EnterSubBlock(PREPROCESSOR_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001537 Error("malformed preprocessor block record in AST file");
Douglas Gregor88a35862010-01-04 19:18:44 +00001538 return;
1539 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001540
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001541 RecordData Record;
1542 while (true) {
Sebastian Redledadecc2010-09-28 02:55:49 +00001543 uint64_t Offset = Cursor.GetCurrentBitNo();
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001544 unsigned Code = Cursor.ReadCode();
1545 if (Code == llvm::bitc::END_BLOCK) {
1546 if (Cursor.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001547 Error("error at end of preprocessor block in AST file");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001548 return;
1549 }
1550 break;
Douglas Gregor88a35862010-01-04 19:18:44 +00001551 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001552
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001553 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1554 // No known subblocks, always skip them.
1555 Cursor.ReadSubBlockID();
1556 if (Cursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001557 Error("malformed block record in AST file");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001558 return;
1559 }
1560 continue;
1561 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001562
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001563 if (Code == llvm::bitc::DEFINE_ABBREV) {
1564 Cursor.ReadAbbrevRecord();
1565 continue;
1566 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001567
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001568 // Read a record.
1569 const char *BlobStart;
1570 unsigned BlobLen;
1571 Record.clear();
1572 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1573 default: // Default behavior: ignore.
1574 break;
Douglas Gregor88a35862010-01-04 19:18:44 +00001575
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001576 case PP_MACRO_OBJECT_LIKE:
1577 case PP_MACRO_FUNCTION_LIKE:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001578 DecodeIdentifierInfo(Record[0]);
1579 break;
1580
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001581 case PP_TOKEN:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001582 // Ignore tokens.
1583 break;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001584
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001585 case PP_MACRO_INSTANTIATION:
1586 case PP_MACRO_DEFINITION:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001587 // Read the macro record.
Sebastian Redledadecc2010-09-28 02:55:49 +00001588 // FIXME: That's a stupid way to do this. We should reuse this cursor.
Sebastian Redlc3632732010-10-05 15:59:54 +00001589 ReadMacroRecord(F, Offset);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001590 break;
1591 }
Douglas Gregor88a35862010-01-04 19:18:44 +00001592 }
1593 }
1594}
1595
Sebastian Redlf73c93f2010-09-15 19:54:06 +00001596MacroDefinition *ASTReader::getMacroDefinition(MacroID ID) {
Douglas Gregor77424bc2010-10-02 19:29:26 +00001597 if (ID == 0 || ID > MacroDefinitionsLoaded.size())
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001598 return 0;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001599
Douglas Gregor77424bc2010-10-02 19:29:26 +00001600 if (!MacroDefinitionsLoaded[ID - 1]) {
1601 unsigned Index = ID - 1;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001602 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1603 PerFileData &F = *Chain[N - I - 1];
1604 if (Index < F.LocalNumMacroDefinitions) {
Sebastian Redlc3632732010-10-05 15:59:54 +00001605 ReadMacroRecord(F, F.MacroDefinitionOffsets[Index]);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001606 break;
1607 }
1608 Index -= F.LocalNumMacroDefinitions;
1609 }
Douglas Gregor77424bc2010-10-02 19:29:26 +00001610 assert(MacroDefinitionsLoaded[ID - 1] && "Broken chain");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001611 }
1612
Douglas Gregor77424bc2010-10-02 19:29:26 +00001613 return MacroDefinitionsLoaded[ID - 1];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001614}
1615
Douglas Gregore650c8c2009-07-07 00:12:59 +00001616/// \brief If we are loading a relocatable PCH file, and the filename is
1617/// not an absolute path, add the system root to the beginning of the file
1618/// name.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001619void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001620 // If this is not a relocatable PCH file, there's nothing to do.
1621 if (!RelocatablePCH)
1622 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001623
Daniel Dunbard5b21972009-11-18 19:50:41 +00001624 if (Filename.empty() || llvm::sys::Path(Filename).isAbsolute())
Douglas Gregore650c8c2009-07-07 00:12:59 +00001625 return;
1626
Douglas Gregore650c8c2009-07-07 00:12:59 +00001627 if (isysroot == 0) {
1628 // If no system root was given, default to '/'
1629 Filename.insert(Filename.begin(), '/');
1630 return;
1631 }
Mike Stump1eb44332009-09-09 15:08:12 +00001632
Douglas Gregore650c8c2009-07-07 00:12:59 +00001633 unsigned Length = strlen(isysroot);
1634 if (isysroot[Length - 1] != '/')
1635 Filename.insert(Filename.begin(), '/');
Mike Stump1eb44332009-09-09 15:08:12 +00001636
Douglas Gregore650c8c2009-07-07 00:12:59 +00001637 Filename.insert(Filename.begin(), isysroot, isysroot + Length);
1638}
1639
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001640ASTReader::ASTReadResult
Sebastian Redl571db7f2010-08-18 23:56:56 +00001641ASTReader::ReadASTBlock(PerFileData &F) {
Sebastian Redl9137a522010-07-16 17:50:48 +00001642 llvm::BitstreamCursor &Stream = F.Stream;
1643
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001644 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001645 Error("malformed block record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001646 return Failure;
1647 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001648
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001649 // Read all of the records and blocks for the ASt file.
Douglas Gregor8038d512009-04-10 17:25:41 +00001650 RecordData Record;
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001651 bool First = true;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001652 while (!Stream.AtEndOfStream()) {
1653 unsigned Code = Stream.ReadCode();
1654 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001655 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001656 Error("error at end of module block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001657 return Failure;
1658 }
Chris Lattner7356a312009-04-11 21:15:38 +00001659
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001660 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001661 }
1662
1663 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1664 switch (Stream.ReadSubBlockID()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001665 case DECLTYPES_BLOCK_ID:
Chris Lattner6367f6d2009-04-27 01:05:14 +00001666 // We lazily load the decls block, but we want to set up the
1667 // DeclsCursor cursor to point into it. Clone our current bitcode
1668 // cursor to it, enter the block and read the abbrevs in that block.
1669 // With the main cursor, we just skip over it.
Sebastian Redl9137a522010-07-16 17:50:48 +00001670 F.DeclsCursor = Stream;
Chris Lattner6367f6d2009-04-27 01:05:14 +00001671 if (Stream.SkipBlock() || // Skip with the main cursor.
1672 // Read the abbrevs.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001673 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001674 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001675 return Failure;
1676 }
1677 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001678
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001679 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl9137a522010-07-16 17:50:48 +00001680 F.MacroCursor = Stream;
Douglas Gregor88a35862010-01-04 19:18:44 +00001681 if (PP)
1682 PP->setExternalSource(this);
1683
Chris Lattner7356a312009-04-11 21:15:38 +00001684 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001685 Error("malformed block record in AST file");
Chris Lattner7356a312009-04-11 21:15:38 +00001686 return Failure;
1687 }
1688 break;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001689
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001690 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001691 switch (ReadSourceManagerBlock(F)) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00001692 case Success:
1693 break;
1694
1695 case Failure:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001696 Error("malformed source manager block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001697 return Failure;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001698
1699 case IgnorePCH:
1700 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001701 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001702 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001703 }
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001704 First = false;
Douglas Gregor8038d512009-04-10 17:25:41 +00001705 continue;
1706 }
1707
1708 if (Code == llvm::bitc::DEFINE_ABBREV) {
1709 Stream.ReadAbbrevRecord();
1710 continue;
1711 }
1712
1713 // Read and process a record.
1714 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00001715 const char *BlobStart = 0;
1716 unsigned BlobLen = 0;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001717 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00001718 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001719 default: // Default behavior: ignore.
1720 break;
1721
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001722 case METADATA: {
1723 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1724 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001725 : diag::warn_pch_version_too_new);
1726 return IgnorePCH;
1727 }
1728
1729 RelocatablePCH = Record[4];
1730 if (Listener) {
1731 std::string TargetTriple(BlobStart, BlobLen);
1732 if (Listener->ReadTargetTriple(TargetTriple))
1733 return IgnorePCH;
1734 }
1735 break;
1736 }
1737
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001738 case CHAINED_METADATA: {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001739 if (!First) {
1740 Error("CHAINED_METADATA is not first record in block");
1741 return Failure;
1742 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001743 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1744 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001745 : diag::warn_pch_version_too_new);
1746 return IgnorePCH;
1747 }
1748
1749 // Load the chained file.
Sebastian Redl571db7f2010-08-18 23:56:56 +00001750 switch(ReadASTCore(llvm::StringRef(BlobStart, BlobLen))) {
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001751 case Failure: return Failure;
1752 // If we have to ignore the dependency, we'll have to ignore this too.
1753 case IgnorePCH: return IgnorePCH;
1754 case Success: break;
1755 }
1756 break;
1757 }
1758
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001759 case TYPE_OFFSET:
Sebastian Redl12d6da02010-07-19 22:06:55 +00001760 if (F.LocalNumTypes != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001761 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001762 return Failure;
1763 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00001764 F.TypeOffsets = (const uint32_t *)BlobStart;
1765 F.LocalNumTypes = Record[0];
Douglas Gregor8038d512009-04-10 17:25:41 +00001766 break;
1767
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001768 case DECL_OFFSET:
Sebastian Redl12d6da02010-07-19 22:06:55 +00001769 if (F.LocalNumDecls != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001770 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001771 return Failure;
1772 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00001773 F.DeclOffsets = (const uint32_t *)BlobStart;
1774 F.LocalNumDecls = Record[0];
Douglas Gregor8038d512009-04-10 17:25:41 +00001775 break;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001776
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001777 case TU_UPDATE_LEXICAL: {
Sebastian Redld692af72010-07-27 18:24:41 +00001778 DeclContextInfo Info = {
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001779 /* No visible information */ 0,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001780 reinterpret_cast<const DeclID *>(BlobStart),
1781 BlobLen / sizeof(DeclID)
Sebastian Redld692af72010-07-27 18:24:41 +00001782 };
Douglas Gregor3747ee72010-10-01 01:18:02 +00001783 DeclContextOffsets[Context ? Context->getTranslationUnitDecl() : 0]
1784 .push_back(Info);
Sebastian Redld692af72010-07-27 18:24:41 +00001785 break;
1786 }
1787
Sebastian Redle1dde812010-08-24 00:50:04 +00001788 case UPDATE_VISIBLE: {
1789 serialization::DeclID ID = Record[0];
1790 void *Table = ASTDeclContextNameLookupTable::Create(
1791 (const unsigned char *)BlobStart + Record[1],
1792 (const unsigned char *)BlobStart,
1793 ASTDeclContextNameLookupTrait(*this));
Douglas Gregor3747ee72010-10-01 01:18:02 +00001794 if (ID == 1 && Context) { // Is it the TU?
Sebastian Redle1dde812010-08-24 00:50:04 +00001795 DeclContextInfo Info = {
1796 Table, /* No lexical inforamtion */ 0, 0
1797 };
1798 DeclContextOffsets[Context->getTranslationUnitDecl()].push_back(Info);
1799 } else
1800 PendingVisibleUpdates[ID].push_back(Table);
1801 break;
1802 }
1803
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001804 case REDECLS_UPDATE_LATEST: {
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00001805 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
1806 for (unsigned i = 0, e = Record.size(); i < e; i += 2) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001807 DeclID First = Record[i], Latest = Record[i+1];
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00001808 assert((FirstLatestDeclIDs.find(First) == FirstLatestDeclIDs.end() ||
1809 Latest > FirstLatestDeclIDs[First]) &&
1810 "The new latest is supposed to come after the previous latest");
1811 FirstLatestDeclIDs[First] = Latest;
1812 }
1813 break;
1814 }
1815
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001816 case LANGUAGE_OPTIONS:
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001817 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001818 return IgnorePCH;
1819 break;
Douglas Gregor2bec0412009-04-10 21:16:55 +00001820
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001821 case IDENTIFIER_TABLE:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001822 F.IdentifierTableData = BlobStart;
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001823 if (Record[0]) {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001824 F.IdentifierLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001825 = ASTIdentifierLookupTable::Create(
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001826 (const unsigned char *)F.IdentifierTableData + Record[0],
1827 (const unsigned char *)F.IdentifierTableData,
Sebastian Redlc3632732010-10-05 15:59:54 +00001828 ASTIdentifierLookupTrait(*this, F));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001829 if (PP)
1830 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001831 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001832 break;
1833
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001834 case IDENTIFIER_OFFSET:
Sebastian Redl2da08f92010-07-19 22:28:42 +00001835 if (F.LocalNumIdentifiers != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001836 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00001837 return Failure;
1838 }
Sebastian Redl2da08f92010-07-19 22:28:42 +00001839 F.IdentifierOffsets = (const uint32_t *)BlobStart;
1840 F.LocalNumIdentifiers = Record[0];
Douglas Gregorafaf3082009-04-11 00:14:32 +00001841 break;
Douglas Gregorfdd01722009-04-14 00:24:19 +00001842
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001843 case EXTERNAL_DEFINITIONS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001844 // Optimization for the first block.
1845 if (ExternalDefinitions.empty())
1846 ExternalDefinitions.swap(Record);
1847 else
1848 ExternalDefinitions.insert(ExternalDefinitions.end(),
1849 Record.begin(), Record.end());
Douglas Gregorfdd01722009-04-14 00:24:19 +00001850 break;
Douglas Gregor3e1af842009-04-17 22:13:46 +00001851
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001852 case SPECIAL_TYPES:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001853 // Optimization for the first block
1854 if (SpecialTypes.empty())
1855 SpecialTypes.swap(Record);
1856 else
1857 SpecialTypes.insert(SpecialTypes.end(), Record.begin(), Record.end());
Douglas Gregorad1de002009-04-18 05:55:16 +00001858 break;
1859
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001860 case STATISTICS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001861 TotalNumStatements += Record[0];
1862 TotalNumMacros += Record[1];
1863 TotalLexicalDeclContexts += Record[2];
1864 TotalVisibleDeclContexts += Record[3];
Douglas Gregor3e1af842009-04-17 22:13:46 +00001865 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001866
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001867 case TENTATIVE_DEFINITIONS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001868 // Optimization for the first block.
1869 if (TentativeDefinitions.empty())
1870 TentativeDefinitions.swap(Record);
1871 else
1872 TentativeDefinitions.insert(TentativeDefinitions.end(),
1873 Record.begin(), Record.end());
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00001874 break;
Douglas Gregor14c22f22009-04-22 22:18:58 +00001875
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001876 case UNUSED_FILESCOPED_DECLS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001877 // Optimization for the first block.
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00001878 if (UnusedFileScopedDecls.empty())
1879 UnusedFileScopedDecls.swap(Record);
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001880 else
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00001881 UnusedFileScopedDecls.insert(UnusedFileScopedDecls.end(),
1882 Record.begin(), Record.end());
Tanya Lattnere6bbc012010-02-12 00:07:30 +00001883 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001884
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001885 case WEAK_UNDECLARED_IDENTIFIERS:
Sebastian Redl40566802010-08-05 18:21:25 +00001886 // Later blocks overwrite earlier ones.
1887 WeakUndeclaredIdentifiers.swap(Record);
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00001888 break;
1889
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001890 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001891 // Optimization for the first block.
1892 if (LocallyScopedExternalDecls.empty())
1893 LocallyScopedExternalDecls.swap(Record);
1894 else
1895 LocallyScopedExternalDecls.insert(LocallyScopedExternalDecls.end(),
1896 Record.begin(), Record.end());
Douglas Gregor14c22f22009-04-22 22:18:58 +00001897 break;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001898
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001899 case SELECTOR_OFFSETS:
Sebastian Redl059612d2010-08-03 21:58:15 +00001900 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redl725cd962010-08-04 20:40:17 +00001901 F.LocalNumSelectors = Record[0];
Douglas Gregor83941df2009-04-25 17:48:32 +00001902 break;
1903
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001904 case METHOD_POOL:
Sebastian Redl725cd962010-08-04 20:40:17 +00001905 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor83941df2009-04-25 17:48:32 +00001906 if (Record[0])
Sebastian Redl725cd962010-08-04 20:40:17 +00001907 F.SelectorLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001908 = ASTSelectorLookupTable::Create(
Sebastian Redl725cd962010-08-04 20:40:17 +00001909 F.SelectorLookupTableData + Record[0],
1910 F.SelectorLookupTableData,
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001911 ASTSelectorLookupTrait(*this));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00001912 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001913 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001914
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00001915 case REFERENCED_SELECTOR_POOL:
Sebastian Redlc3632732010-10-05 15:59:54 +00001916 F.ReferencedSelectorsData.swap(Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00001917 break;
1918
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001919 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001920 if (!Record.empty() && Listener)
1921 Listener->ReadCounter(Record[0]);
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001922 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001923
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001924 case SOURCE_LOCATION_OFFSETS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001925 F.SLocOffsets = (const uint32_t *)BlobStart;
1926 F.LocalNumSLocEntries = Record[0];
Sebastian Redl8db9fae2010-09-22 20:19:08 +00001927 F.LocalSLocSize = Record[1];
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001928 break;
1929
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001930 case SOURCE_LOCATION_PRELOADS:
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00001931 if (PreloadSLocEntries.empty())
1932 PreloadSLocEntries.swap(Record);
1933 else
1934 PreloadSLocEntries.insert(PreloadSLocEntries.end(),
1935 Record.begin(), Record.end());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001936 break;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001937
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001938 case STAT_CACHE: {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001939 ASTStatCache *MyStatCache =
1940 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
Douglas Gregor52e71082009-10-16 18:18:30 +00001941 (const unsigned char *)BlobStart,
1942 NumStatHits, NumStatMisses);
1943 FileMgr.addStatCache(MyStatCache);
Sebastian Redl9137a522010-07-16 17:50:48 +00001944 F.StatCache = MyStatCache;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001945 break;
Douglas Gregor52e71082009-10-16 18:18:30 +00001946 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001947
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001948 case EXT_VECTOR_DECLS:
Sebastian Redla9f23682010-07-28 21:38:49 +00001949 // Optimization for the first block.
1950 if (ExtVectorDecls.empty())
1951 ExtVectorDecls.swap(Record);
1952 else
1953 ExtVectorDecls.insert(ExtVectorDecls.end(),
1954 Record.begin(), Record.end());
Douglas Gregorb81c1702009-04-27 20:06:05 +00001955 break;
1956
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001957 case VTABLE_USES:
Sebastian Redl40566802010-08-05 18:21:25 +00001958 // Later tables overwrite earlier ones.
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00001959 VTableUses.swap(Record);
1960 break;
1961
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001962 case DYNAMIC_CLASSES:
Sebastian Redl40566802010-08-05 18:21:25 +00001963 // Optimization for the first block.
1964 if (DynamicClasses.empty())
1965 DynamicClasses.swap(Record);
1966 else
1967 DynamicClasses.insert(DynamicClasses.end(),
1968 Record.begin(), Record.end());
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00001969 break;
1970
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001971 case PENDING_IMPLICIT_INSTANTIATIONS:
Sebastian Redlc3632732010-10-05 15:59:54 +00001972 F.PendingInstantiations.swap(Record);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00001973 break;
1974
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001975 case SEMA_DECL_REFS:
Sebastian Redl40566802010-08-05 18:21:25 +00001976 // Later tables overwrite earlier ones.
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00001977 SemaDeclRefs.swap(Record);
1978 break;
1979
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001980 case ORIGINAL_FILE_NAME:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001981 // The primary AST will be the last to get here, so it will be the one
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001982 // that's used.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +00001983 ActualOriginalFileName.assign(BlobStart, BlobLen);
1984 OriginalFileName = ActualOriginalFileName;
Douglas Gregore650c8c2009-07-07 00:12:59 +00001985 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001986 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001987
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001988 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek974be4d2010-02-12 23:31:14 +00001989 const std::string &CurBranch = getClangFullRepositoryVersion();
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001990 llvm::StringRef ASTBranch(BlobStart, BlobLen);
1991 if (llvm::StringRef(CurBranch) != ASTBranch && !DisableValidation) {
1992 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregor445e23e2009-10-05 21:07:28 +00001993 return IgnorePCH;
1994 }
1995 break;
1996 }
Sebastian Redl04e6fd42010-07-21 20:07:32 +00001997
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001998 case MACRO_DEFINITION_OFFSETS:
Sebastian Redl04e6fd42010-07-21 20:07:32 +00001999 F.MacroDefinitionOffsets = (const uint32_t *)BlobStart;
2000 F.NumPreallocatedPreprocessingEntities = Record[0];
2001 F.LocalNumMacroDefinitions = Record[1];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002002 break;
Sebastian Redl0b17c612010-08-13 00:28:03 +00002003
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002004 case DECL_REPLACEMENTS: {
Sebastian Redl0b17c612010-08-13 00:28:03 +00002005 if (Record.size() % 2 != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002006 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redl0b17c612010-08-13 00:28:03 +00002007 return Failure;
2008 }
2009 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002010 ReplacedDecls[static_cast<DeclID>(Record[I])] =
Sebastian Redl0b17c612010-08-13 00:28:03 +00002011 std::make_pair(&F, Record[I+1]);
2012 break;
2013 }
Sebastian Redl6e50e002010-08-24 22:50:19 +00002014
2015 case ADDITIONAL_TEMPLATE_SPECIALIZATIONS: {
2016 AdditionalTemplateSpecializations &ATS =
2017 AdditionalTemplateSpecializationsPending[Record[0]];
2018 ATS.insert(ATS.end(), Record.begin()+1, Record.end());
2019 break;
2020 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002021 }
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002022 First = false;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002023 }
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002024 Error("premature end of bitstream in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002025 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002026}
2027
Sebastian Redl571db7f2010-08-18 23:56:56 +00002028ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName) {
2029 switch(ReadASTCore(FileName)) {
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002030 case Failure: return Failure;
2031 case IgnorePCH: return IgnorePCH;
2032 case Success: break;
2033 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002034
2035 // Here comes stuff that we only do once the entire chain is loaded.
2036
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002037 // Allocate space for loaded slocentries, identifiers, decls and types.
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002038 unsigned TotalNumIdentifiers = 0, TotalNumTypes = 0, TotalNumDecls = 0,
Sebastian Redl725cd962010-08-04 20:40:17 +00002039 TotalNumPreallocatedPreprocessingEntities = 0, TotalNumMacroDefs = 0,
2040 TotalNumSelectors = 0;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002041 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002042 TotalNumSLocEntries += Chain[I]->LocalNumSLocEntries;
Sebastian Redl8db9fae2010-09-22 20:19:08 +00002043 NextSLocOffset += Chain[I]->LocalSLocSize;
Sebastian Redl2da08f92010-07-19 22:28:42 +00002044 TotalNumIdentifiers += Chain[I]->LocalNumIdentifiers;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002045 TotalNumTypes += Chain[I]->LocalNumTypes;
2046 TotalNumDecls += Chain[I]->LocalNumDecls;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002047 TotalNumPreallocatedPreprocessingEntities +=
2048 Chain[I]->NumPreallocatedPreprocessingEntities;
2049 TotalNumMacroDefs += Chain[I]->LocalNumMacroDefinitions;
Sebastian Redl725cd962010-08-04 20:40:17 +00002050 TotalNumSelectors += Chain[I]->LocalNumSelectors;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002051 }
Sebastian Redl8db9fae2010-09-22 20:19:08 +00002052 SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, NextSLocOffset);
Sebastian Redl2da08f92010-07-19 22:28:42 +00002053 IdentifiersLoaded.resize(TotalNumIdentifiers);
Sebastian Redl12d6da02010-07-19 22:06:55 +00002054 TypesLoaded.resize(TotalNumTypes);
2055 DeclsLoaded.resize(TotalNumDecls);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002056 MacroDefinitionsLoaded.resize(TotalNumMacroDefs);
2057 if (PP) {
2058 if (TotalNumIdentifiers > 0)
2059 PP->getHeaderSearchInfo().SetExternalLookup(this);
2060 if (TotalNumPreallocatedPreprocessingEntities > 0) {
2061 if (!PP->getPreprocessingRecord())
2062 PP->createPreprocessingRecord();
2063 PP->getPreprocessingRecord()->SetExternalSource(*this,
2064 TotalNumPreallocatedPreprocessingEntities);
2065 }
2066 }
Sebastian Redl725cd962010-08-04 20:40:17 +00002067 SelectorsLoaded.resize(TotalNumSelectors);
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002068 // Preload SLocEntries.
2069 for (unsigned I = 0, N = PreloadSLocEntries.size(); I != N; ++I) {
2070 ASTReadResult Result = ReadSLocEntryRecord(PreloadSLocEntries[I]);
2071 if (Result != Success)
2072 return Result;
2073 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00002074
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002075 // Check the predefines buffers.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00002076 if (!DisableValidation && CheckPredefinesBuffers())
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002077 return IgnorePCH;
2078
2079 if (PP) {
2080 // Initialization of keywords and pragmas occurs before the
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002081 // AST file is read, so there may be some identifiers that were
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002082 // loaded into the IdentifierTable before we intercepted the
2083 // creation of identifiers. Iterate through the list of known
2084 // identifiers and determine whether we have to establish
2085 // preprocessor definitions or top-level identifier declaration
2086 // chains for those identifiers.
2087 //
2088 // We copy the IdentifierInfo pointers to a small vector first,
2089 // since de-serializing declarations or macro definitions can add
2090 // new entries into the identifier table, invalidating the
2091 // iterators.
2092 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
2093 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
2094 IdEnd = PP->getIdentifierTable().end();
2095 Id != IdEnd; ++Id)
2096 Identifiers.push_back(Id->second);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002097 // We need to search the tables in all files.
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002098 for (unsigned J = 0, M = Chain.size(); J != M; ++J) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002099 ASTIdentifierLookupTable *IdTable
2100 = (ASTIdentifierLookupTable *)Chain[J]->IdentifierLookupTable;
2101 // Not all AST files necessarily have identifier tables, only the useful
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00002102 // ones.
2103 if (!IdTable)
2104 continue;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002105 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2106 IdentifierInfo *II = Identifiers[I];
2107 // Look in the on-disk hash tables for an entry for this identifier
Sebastian Redlc3632732010-10-05 15:59:54 +00002108 ASTIdentifierLookupTrait Info(*this, *Chain[J], II);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002109 std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength());
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002110 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002111 if (Pos == IdTable->end())
2112 continue;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002113
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002114 // Dereferencing the iterator has the effect of populating the
2115 // IdentifierInfo node with the various declarations it needs.
2116 (void)*Pos;
2117 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002118 }
2119 }
2120
2121 if (Context)
2122 InitializeContext(*Context);
2123
2124 return Success;
2125}
2126
Sebastian Redl571db7f2010-08-18 23:56:56 +00002127ASTReader::ASTReadResult ASTReader::ReadASTCore(llvm::StringRef FileName) {
Sebastian Redla866e652010-10-01 19:59:12 +00002128 PerFileData *Prev = Chain.empty() ? 0 : Chain.back();
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002129 Chain.push_back(new PerFileData());
Sebastian Redl9137a522010-07-16 17:50:48 +00002130 PerFileData &F = *Chain.back();
Sebastian Redla866e652010-10-01 19:59:12 +00002131 if (Prev)
2132 Prev->NextInSource = &F;
2133 else
2134 FirstInSource = &F;
2135 F.Loaders.push_back(Prev);
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002136
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002137 // Set the AST file name.
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002138 F.FileName = FileName;
2139
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002140 // Open the AST file.
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002141 //
2142 // FIXME: This shouldn't be here, we should just take a raw_ostream.
2143 std::string ErrStr;
2144 F.Buffer.reset(llvm::MemoryBuffer::getFileOrSTDIN(FileName, &ErrStr));
2145 if (!F.Buffer) {
2146 Error(ErrStr.c_str());
2147 return IgnorePCH;
2148 }
2149
2150 // Initialize the stream
2151 F.StreamFile.init((const unsigned char *)F.Buffer->getBufferStart(),
2152 (const unsigned char *)F.Buffer->getBufferEnd());
Sebastian Redl9137a522010-07-16 17:50:48 +00002153 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002154 Stream.init(F.StreamFile);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002155 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002156
2157 // Sniff for the signature.
2158 if (Stream.Read(8) != 'C' ||
2159 Stream.Read(8) != 'P' ||
2160 Stream.Read(8) != 'C' ||
2161 Stream.Read(8) != 'H') {
2162 Diag(diag::err_not_a_pch_file) << FileName;
2163 return Failure;
2164 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002165
Douglas Gregor2cf26342009-04-09 22:27:44 +00002166 while (!Stream.AtEndOfStream()) {
2167 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002168
Douglas Gregore1d918e2009-04-10 23:10:45 +00002169 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002170 Error("invalid record at top-level of AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002171 return Failure;
2172 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002173
2174 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregor668c1a42009-04-21 22:25:48 +00002175
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002176 // We only know the AST subblock ID.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002177 switch (BlockID) {
2178 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002179 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002180 Error("malformed BlockInfoBlock in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002181 return Failure;
2182 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002183 break;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002184 case AST_BLOCK_ID:
Sebastian Redl571db7f2010-08-18 23:56:56 +00002185 switch (ReadASTBlock(F)) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002186 case Success:
2187 break;
2188
2189 case Failure:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002190 return Failure;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002191
2192 case IgnorePCH:
Douglas Gregor2bec0412009-04-10 21:16:55 +00002193 // FIXME: We could consider reading through to the end of this
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002194 // AST block, skipping subblocks, to see if there are other
2195 // AST blocks elsewhere.
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002196
2197 // Clear out any preallocated source location entries, so that
2198 // the source manager does not try to resolve them later.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002199 SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002200
2201 // Remove the stat cache.
Sebastian Redl9137a522010-07-16 17:50:48 +00002202 if (F.StatCache)
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002203 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002204
Douglas Gregore1d918e2009-04-10 23:10:45 +00002205 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002206 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002207 break;
2208 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002209 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002210 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002211 return Failure;
2212 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002213 break;
2214 }
Mike Stump1eb44332009-09-09 15:08:12 +00002215 }
2216
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002217 return Success;
2218}
2219
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002220void ASTReader::setPreprocessor(Preprocessor &pp) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002221 PP = &pp;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002222
2223 unsigned TotalNum = 0;
2224 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
2225 TotalNum += Chain[I]->NumPreallocatedPreprocessingEntities;
2226 if (TotalNum) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002227 if (!PP->getPreprocessingRecord())
2228 PP->createPreprocessingRecord();
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002229 PP->getPreprocessingRecord()->SetExternalSource(*this, TotalNum);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002230 }
2231}
2232
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002233void ASTReader::InitializeContext(ASTContext &Ctx) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002234 Context = &Ctx;
2235 assert(Context && "Passed null context!");
2236
2237 assert(PP && "Forgot to set Preprocessor ?");
2238 PP->getIdentifierTable().setExternalIdentifierLookup(this);
2239 PP->getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor88a35862010-01-04 19:18:44 +00002240 PP->setExternalSource(this);
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002241
Douglas Gregor3747ee72010-10-01 01:18:02 +00002242 // If we have an update block for the TU waiting, we have to add it before
2243 // deserializing the decl.
2244 DeclContextOffsetsMap::iterator DCU = DeclContextOffsets.find(0);
2245 if (DCU != DeclContextOffsets.end()) {
2246 // Insertion could invalidate map, so grab vector.
2247 DeclContextInfos T;
2248 T.swap(DCU->second);
2249 DeclContextOffsets.erase(DCU);
2250 DeclContextOffsets[Ctx.getTranslationUnitDecl()].swap(T);
2251 }
2252
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002253 // Load the translation unit declaration
Argyrios Kyrtzidis8871a442010-07-08 17:13:02 +00002254 GetTranslationUnitDecl();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002255
2256 // Load the special types.
2257 Context->setBuiltinVaListType(
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002258 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
2259 if (unsigned Id = SpecialTypes[SPECIAL_TYPE_OBJC_ID])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002260 Context->setObjCIdType(GetType(Id));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002261 if (unsigned Sel = SpecialTypes[SPECIAL_TYPE_OBJC_SELECTOR])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002262 Context->setObjCSelType(GetType(Sel));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002263 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002264 Context->setObjCProtoType(GetType(Proto));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002265 if (unsigned Class = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002266 Context->setObjCClassType(GetType(Class));
Steve Naroff14108da2009-07-10 23:34:53 +00002267
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002268 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002269 Context->setCFConstantStringType(GetType(String));
Mike Stump1eb44332009-09-09 15:08:12 +00002270 if (unsigned FastEnum
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002271 = SpecialTypes[SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002272 Context->setObjCFastEnumerationStateType(GetType(FastEnum));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002273 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002274 QualType FileType = GetType(File);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002275 if (FileType.isNull()) {
2276 Error("FILE type is NULL");
2277 return;
2278 }
John McCall183700f2009-09-21 23:43:11 +00002279 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002280 Context->setFILEDecl(Typedef->getDecl());
2281 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002282 const TagType *Tag = FileType->getAs<TagType>();
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002283 if (!Tag) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002284 Error("Invalid FILE type in AST file");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002285 return;
2286 }
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002287 Context->setFILEDecl(Tag->getDecl());
2288 }
2289 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002290 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
Mike Stump782fa302009-07-28 02:25:19 +00002291 QualType Jmp_bufType = GetType(Jmp_buf);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002292 if (Jmp_bufType.isNull()) {
2293 Error("jmp_bug type is NULL");
2294 return;
2295 }
John McCall183700f2009-09-21 23:43:11 +00002296 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
Mike Stump782fa302009-07-28 02:25:19 +00002297 Context->setjmp_bufDecl(Typedef->getDecl());
2298 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002299 const TagType *Tag = Jmp_bufType->getAs<TagType>();
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002300 if (!Tag) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002301 Error("Invalid jmp_buf type in AST file");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002302 return;
2303 }
Mike Stump782fa302009-07-28 02:25:19 +00002304 Context->setjmp_bufDecl(Tag->getDecl());
2305 }
2306 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002307 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
Mike Stump782fa302009-07-28 02:25:19 +00002308 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002309 if (Sigjmp_bufType.isNull()) {
2310 Error("sigjmp_buf type is NULL");
2311 return;
2312 }
John McCall183700f2009-09-21 23:43:11 +00002313 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
Mike Stump782fa302009-07-28 02:25:19 +00002314 Context->setsigjmp_bufDecl(Typedef->getDecl());
2315 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002316 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002317 assert(Tag && "Invalid sigjmp_buf type in AST file");
Mike Stump782fa302009-07-28 02:25:19 +00002318 Context->setsigjmp_bufDecl(Tag->getDecl());
2319 }
2320 }
Mike Stump1eb44332009-09-09 15:08:12 +00002321 if (unsigned ObjCIdRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002322 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION])
Douglas Gregord1571ac2009-08-21 00:27:50 +00002323 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
Mike Stump1eb44332009-09-09 15:08:12 +00002324 if (unsigned ObjCClassRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002325 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION])
Douglas Gregord1571ac2009-08-21 00:27:50 +00002326 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002327 if (unsigned String = SpecialTypes[SPECIAL_TYPE_BLOCK_DESCRIPTOR])
Mike Stumpadaaad32009-10-20 02:12:22 +00002328 Context->setBlockDescriptorType(GetType(String));
Mike Stump083c25e2009-10-22 00:49:09 +00002329 if (unsigned String
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002330 = SpecialTypes[SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR])
Mike Stump083c25e2009-10-22 00:49:09 +00002331 Context->setBlockDescriptorExtendedType(GetType(String));
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002332 if (unsigned ObjCSelRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002333 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION])
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002334 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002335 if (unsigned String = SpecialTypes[SPECIAL_TYPE_NS_CONSTANT_STRING])
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002336 Context->setNSConstantStringType(GetType(String));
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002337
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002338 if (SpecialTypes[SPECIAL_TYPE_INT128_INSTALLED])
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002339 Context->setInt128Installed();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002340}
2341
Douglas Gregorb64c1932009-05-12 01:31:05 +00002342/// \brief Retrieve the name of the original source file name
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002343/// directly from the AST file, without actually loading the AST
Douglas Gregorb64c1932009-05-12 01:31:05 +00002344/// file.
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002345std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00002346 Diagnostic &Diags) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002347 // Open the AST file.
Douglas Gregorb64c1932009-05-12 01:31:05 +00002348 std::string ErrStr;
2349 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002350 Buffer.reset(llvm::MemoryBuffer::getFile(ASTFileName.c_str(), &ErrStr));
Douglas Gregorb64c1932009-05-12 01:31:05 +00002351 if (!Buffer) {
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00002352 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002353 return std::string();
2354 }
2355
2356 // Initialize the stream
2357 llvm::BitstreamReader StreamFile;
2358 llvm::BitstreamCursor Stream;
Mike Stump1eb44332009-09-09 15:08:12 +00002359 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregorb64c1932009-05-12 01:31:05 +00002360 (const unsigned char *)Buffer->getBufferEnd());
2361 Stream.init(StreamFile);
2362
2363 // Sniff for the signature.
2364 if (Stream.Read(8) != 'C' ||
2365 Stream.Read(8) != 'P' ||
2366 Stream.Read(8) != 'C' ||
2367 Stream.Read(8) != 'H') {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002368 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002369 return std::string();
2370 }
2371
2372 RecordData Record;
2373 while (!Stream.AtEndOfStream()) {
2374 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002375
Douglas Gregorb64c1932009-05-12 01:31:05 +00002376 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2377 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump1eb44332009-09-09 15:08:12 +00002378
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002379 // We only know the AST subblock ID.
Douglas Gregorb64c1932009-05-12 01:31:05 +00002380 switch (BlockID) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002381 case AST_BLOCK_ID:
2382 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002383 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002384 return std::string();
2385 }
2386 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002387
Douglas Gregorb64c1932009-05-12 01:31:05 +00002388 default:
2389 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002390 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002391 return std::string();
2392 }
2393 break;
2394 }
2395 continue;
2396 }
2397
2398 if (Code == llvm::bitc::END_BLOCK) {
2399 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002400 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002401 return std::string();
2402 }
2403 continue;
2404 }
2405
2406 if (Code == llvm::bitc::DEFINE_ABBREV) {
2407 Stream.ReadAbbrevRecord();
2408 continue;
2409 }
2410
2411 Record.clear();
2412 const char *BlobStart = 0;
2413 unsigned BlobLen = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002414 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002415 == ORIGINAL_FILE_NAME)
Douglas Gregorb64c1932009-05-12 01:31:05 +00002416 return std::string(BlobStart, BlobLen);
Mike Stump1eb44332009-09-09 15:08:12 +00002417 }
Douglas Gregorb64c1932009-05-12 01:31:05 +00002418
2419 return std::string();
2420}
2421
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002422/// \brief Parse the record that corresponds to a LangOptions data
2423/// structure.
2424///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002425/// This routine parses the language options from the AST file and then gives
2426/// them to the AST listener if one is set.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002427///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002428/// \returns true if the listener deems the file unacceptable, false otherwise.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002429bool ASTReader::ParseLanguageOptions(
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002430 const llvm::SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002431 if (Listener) {
2432 LangOptions LangOpts;
Mike Stump1eb44332009-09-09 15:08:12 +00002433
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002434 #define PARSE_LANGOPT(Option) \
2435 LangOpts.Option = Record[Idx]; \
2436 ++Idx
Mike Stump1eb44332009-09-09 15:08:12 +00002437
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002438 unsigned Idx = 0;
2439 PARSE_LANGOPT(Trigraphs);
2440 PARSE_LANGOPT(BCPLComment);
2441 PARSE_LANGOPT(DollarIdents);
2442 PARSE_LANGOPT(AsmPreprocessor);
2443 PARSE_LANGOPT(GNUMode);
Chandler Carrutheb5d7b72010-04-17 20:17:31 +00002444 PARSE_LANGOPT(GNUKeywords);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002445 PARSE_LANGOPT(ImplicitInt);
2446 PARSE_LANGOPT(Digraphs);
2447 PARSE_LANGOPT(HexFloats);
2448 PARSE_LANGOPT(C99);
2449 PARSE_LANGOPT(Microsoft);
2450 PARSE_LANGOPT(CPlusPlus);
2451 PARSE_LANGOPT(CPlusPlus0x);
2452 PARSE_LANGOPT(CXXOperatorNames);
2453 PARSE_LANGOPT(ObjC1);
2454 PARSE_LANGOPT(ObjC2);
2455 PARSE_LANGOPT(ObjCNonFragileABI);
Fariborz Jahanian412e7982010-02-09 19:31:38 +00002456 PARSE_LANGOPT(ObjCNonFragileABI2);
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +00002457 PARSE_LANGOPT(NoConstantCFStrings);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002458 PARSE_LANGOPT(PascalStrings);
2459 PARSE_LANGOPT(WritableStrings);
2460 PARSE_LANGOPT(LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +00002461 PARSE_LANGOPT(AltiVec);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002462 PARSE_LANGOPT(Exceptions);
Daniel Dunbar73482882010-02-10 18:48:44 +00002463 PARSE_LANGOPT(SjLjExceptions);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002464 PARSE_LANGOPT(NeXTRuntime);
2465 PARSE_LANGOPT(Freestanding);
2466 PARSE_LANGOPT(NoBuiltin);
2467 PARSE_LANGOPT(ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +00002468 PARSE_LANGOPT(POSIXThreads);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002469 PARSE_LANGOPT(Blocks);
2470 PARSE_LANGOPT(EmitAllDecls);
2471 PARSE_LANGOPT(MathErrno);
Chris Lattnera4d71452010-06-26 21:25:03 +00002472 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
2473 Record[Idx++]);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002474 PARSE_LANGOPT(HeinousExtensions);
2475 PARSE_LANGOPT(Optimize);
2476 PARSE_LANGOPT(OptimizeSize);
2477 PARSE_LANGOPT(Static);
2478 PARSE_LANGOPT(PICLevel);
2479 PARSE_LANGOPT(GNUInline);
2480 PARSE_LANGOPT(NoInline);
2481 PARSE_LANGOPT(AccessControl);
2482 PARSE_LANGOPT(CharIsSigned);
John Thompsona6fda122009-11-05 20:14:16 +00002483 PARSE_LANGOPT(ShortWChar);
Chris Lattnera4d71452010-06-26 21:25:03 +00002484 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
2485 LangOpts.setVisibilityMode((LangOptions::VisibilityMode)Record[Idx++]);
Daniel Dunbarab8e2812009-09-21 04:16:19 +00002486 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
Chris Lattnera4d71452010-06-26 21:25:03 +00002487 Record[Idx++]);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002488 PARSE_LANGOPT(InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +00002489 PARSE_LANGOPT(OpenCL);
Mike Stump9c276ae2009-12-12 01:27:46 +00002490 PARSE_LANGOPT(CatchUndefined);
2491 // FIXME: Missing ElideConstructors?!
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002492 #undef PARSE_LANGOPT
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002493
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002494 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002495 }
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002496
2497 return false;
2498}
2499
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002500void ASTReader::ReadPreprocessedEntities() {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002501 ReadDefinedMacros();
2502}
2503
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002504/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002505ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002506 PerFileData *F = 0;
2507 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2508 F = Chain[N - I - 1];
2509 if (Index < F->LocalNumTypes)
2510 break;
2511 Index -= F->LocalNumTypes;
2512 }
2513 assert(F && F->LocalNumTypes > Index && "Broken chain");
Sebastian Redlc3632732010-10-05 15:59:54 +00002514 return RecordLocation(F, F->TypeOffsets[Index]);
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002515}
2516
2517/// \brief Read and return the type with the given index..
Douglas Gregor2cf26342009-04-09 22:27:44 +00002518///
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002519/// The index is the type ID, shifted and minus the number of predefs. This
2520/// routine actually reads the record corresponding to the type at the given
2521/// location. It is a helper routine for GetType, which deals with reading type
2522/// IDs.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002523QualType ASTReader::ReadTypeRecord(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002524 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00002525 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00002526
Douglas Gregor0b748912009-04-14 21:18:50 +00002527 // Keep track of where we are in the stream, then jump back there
2528 // after reading this type.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002529 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregor0b748912009-04-14 21:18:50 +00002530
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00002531 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redl27372b42010-08-11 18:52:41 +00002532
Douglas Gregord89275b2009-07-06 18:54:52 +00002533 // Note that we are loading a type record.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00002534 Deserializing AType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00002535
Sebastian Redlc3632732010-10-05 15:59:54 +00002536 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002537 RecordData Record;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002538 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002539 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
2540 case TYPE_EXT_QUAL: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002541 if (Record.size() != 2) {
2542 Error("Incorrect encoding of extended qualifier type");
2543 return QualType();
2544 }
Douglas Gregor6d473962009-04-15 22:00:08 +00002545 QualType Base = GetType(Record[0]);
John McCall0953e762009-09-24 19:53:00 +00002546 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]);
2547 return Context->getQualifiedType(Base, Quals);
Douglas Gregor6d473962009-04-15 22:00:08 +00002548 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002549
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002550 case TYPE_COMPLEX: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002551 if (Record.size() != 1) {
2552 Error("Incorrect encoding of complex type");
2553 return QualType();
2554 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002555 QualType ElemType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002556 return Context->getComplexType(ElemType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002557 }
2558
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002559 case TYPE_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002560 if (Record.size() != 1) {
2561 Error("Incorrect encoding of pointer type");
2562 return QualType();
2563 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002564 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002565 return Context->getPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002566 }
2567
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002568 case TYPE_BLOCK_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002569 if (Record.size() != 1) {
2570 Error("Incorrect encoding of block pointer type");
2571 return QualType();
2572 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002573 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002574 return Context->getBlockPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002575 }
2576
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002577 case TYPE_LVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002578 if (Record.size() != 1) {
2579 Error("Incorrect encoding of lvalue reference type");
2580 return QualType();
2581 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002582 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002583 return Context->getLValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002584 }
2585
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002586 case TYPE_RVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002587 if (Record.size() != 1) {
2588 Error("Incorrect encoding of rvalue reference type");
2589 return QualType();
2590 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002591 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002592 return Context->getRValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002593 }
2594
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002595 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidis240437b2010-07-02 11:55:15 +00002596 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002597 Error("Incorrect encoding of member pointer type");
2598 return QualType();
2599 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002600 QualType PointeeType = GetType(Record[0]);
2601 QualType ClassType = GetType(Record[1]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002602 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002603 }
2604
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002605 case TYPE_CONSTANT_ARRAY: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002606 QualType ElementType = GetType(Record[0]);
2607 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2608 unsigned IndexTypeQuals = Record[2];
2609 unsigned Idx = 3;
2610 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002611 return Context->getConstantArrayType(ElementType, Size,
2612 ASM, IndexTypeQuals);
2613 }
2614
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002615 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002616 QualType ElementType = GetType(Record[0]);
2617 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2618 unsigned IndexTypeQuals = Record[2];
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002619 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002620 }
2621
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002622 case TYPE_VARIABLE_ARRAY: {
Douglas Gregor0b748912009-04-14 21:18:50 +00002623 QualType ElementType = GetType(Record[0]);
2624 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2625 unsigned IndexTypeQuals = Record[2];
Sebastian Redlc3632732010-10-05 15:59:54 +00002626 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
2627 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
2628 return Context->getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002629 ASM, IndexTypeQuals,
2630 SourceRange(LBLoc, RBLoc));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002631 }
2632
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002633 case TYPE_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00002634 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002635 Error("incorrect encoding of vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002636 return QualType();
2637 }
2638
2639 QualType ElementType = GetType(Record[0]);
2640 unsigned NumElements = Record[1];
Chris Lattner788b0fd2010-06-23 06:00:24 +00002641 unsigned AltiVecSpec = Record[2];
2642 return Context->getVectorType(ElementType, NumElements,
2643 (VectorType::AltiVecSpecific)AltiVecSpec);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002644 }
2645
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002646 case TYPE_EXT_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00002647 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002648 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002649 return QualType();
2650 }
2651
2652 QualType ElementType = GetType(Record[0]);
2653 unsigned NumElements = Record[1];
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002654 return Context->getExtVectorType(ElementType, NumElements);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002655 }
2656
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002657 case TYPE_FUNCTION_NO_PROTO: {
Rafael Espindola425ef722010-03-30 22:15:11 +00002658 if (Record.size() != 4) {
Douglas Gregora02b1472009-04-28 21:53:25 +00002659 Error("incorrect encoding of no-proto function type");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002660 return QualType();
2661 }
2662 QualType ResultType = GetType(Record[0]);
Rafael Espindola425ef722010-03-30 22:15:11 +00002663 FunctionType::ExtInfo Info(Record[1], Record[2], (CallingConv)Record[3]);
Rafael Espindola264ba482010-03-30 20:24:48 +00002664 return Context->getFunctionNoProtoType(ResultType, Info);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002665 }
2666
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002667 case TYPE_FUNCTION_PROTO: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002668 QualType ResultType = GetType(Record[0]);
Douglas Gregor91236662009-12-22 18:11:50 +00002669 bool NoReturn = Record[1];
Rafael Espindola425ef722010-03-30 22:15:11 +00002670 unsigned RegParm = Record[2];
2671 CallingConv CallConv = (CallingConv)Record[3];
2672 unsigned Idx = 4;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002673 unsigned NumParams = Record[Idx++];
2674 llvm::SmallVector<QualType, 16> ParamTypes;
2675 for (unsigned I = 0; I != NumParams; ++I)
2676 ParamTypes.push_back(GetType(Record[Idx++]));
2677 bool isVariadic = Record[Idx++];
2678 unsigned Quals = Record[Idx++];
Sebastian Redl465226e2009-05-27 22:11:52 +00002679 bool hasExceptionSpec = Record[Idx++];
2680 bool hasAnyExceptionSpec = Record[Idx++];
2681 unsigned NumExceptions = Record[Idx++];
2682 llvm::SmallVector<QualType, 2> Exceptions;
2683 for (unsigned I = 0; I != NumExceptions; ++I)
2684 Exceptions.push_back(GetType(Record[Idx++]));
Jay Foadbeaaccd2009-05-21 09:52:38 +00002685 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
Sebastian Redl465226e2009-05-27 22:11:52 +00002686 isVariadic, Quals, hasExceptionSpec,
2687 hasAnyExceptionSpec, NumExceptions,
Rafael Espindola264ba482010-03-30 20:24:48 +00002688 Exceptions.data(),
Rafael Espindola425ef722010-03-30 22:15:11 +00002689 FunctionType::ExtInfo(NoReturn, RegParm,
2690 CallConv));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002691 }
2692
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002693 case TYPE_UNRESOLVED_USING:
John McCalled976492009-12-04 22:46:56 +00002694 return Context->getTypeDeclType(
2695 cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0])));
2696
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002697 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002698 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002699 Error("incorrect encoding of typedef type");
2700 return QualType();
2701 }
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002702 TypedefDecl *Decl = cast<TypedefDecl>(GetDecl(Record[0]));
2703 QualType Canonical = GetType(Record[1]);
2704 return Context->getTypedefType(Decl, Canonical);
2705 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002706
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002707 case TYPE_TYPEOF_EXPR:
Sebastian Redlc3632732010-10-05 15:59:54 +00002708 return Context->getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002709
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002710 case TYPE_TYPEOF: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002711 if (Record.size() != 1) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002712 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002713 return QualType();
2714 }
2715 QualType UnderlyingType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002716 return Context->getTypeOfType(UnderlyingType);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002717 }
Mike Stump1eb44332009-09-09 15:08:12 +00002718
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002719 case TYPE_DECLTYPE:
Sebastian Redlc3632732010-10-05 15:59:54 +00002720 return Context->getDecltypeType(ReadExpr(*Loc.F));
Anders Carlsson395b4752009-06-24 19:06:50 +00002721
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002722 case TYPE_RECORD: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002723 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002724 Error("incorrect encoding of record type");
2725 return QualType();
2726 }
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002727 bool IsDependent = Record[0];
2728 QualType T = Context->getRecordType(cast<RecordDecl>(GetDecl(Record[1])));
2729 T->Dependent = IsDependent;
2730 return T;
2731 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002732
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002733 case TYPE_ENUM: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002734 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002735 Error("incorrect encoding of enum type");
2736 return QualType();
2737 }
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002738 bool IsDependent = Record[0];
2739 QualType T = Context->getEnumType(cast<EnumDecl>(GetDecl(Record[1])));
2740 T->Dependent = IsDependent;
2741 return T;
2742 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002743
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002744 case TYPE_ELABORATED: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00002745 unsigned Idx = 0;
2746 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2747 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2748 QualType NamedType = GetType(Record[Idx++]);
2749 return Context->getElaboratedType(Keyword, NNS, NamedType);
John McCall7da24312009-09-05 00:15:47 +00002750 }
2751
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002752 case TYPE_OBJC_INTERFACE: {
Chris Lattnerc6fa4452009-04-22 06:45:28 +00002753 unsigned Idx = 0;
2754 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
John McCallc12c5bb2010-05-15 11:32:37 +00002755 return Context->getObjCInterfaceType(ItfD);
2756 }
2757
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002758 case TYPE_OBJC_OBJECT: {
John McCallc12c5bb2010-05-15 11:32:37 +00002759 unsigned Idx = 0;
2760 QualType Base = GetType(Record[Idx++]);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00002761 unsigned NumProtos = Record[Idx++];
2762 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2763 for (unsigned I = 0; I != NumProtos; ++I)
2764 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
John McCallc12c5bb2010-05-15 11:32:37 +00002765 return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00002766 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002767
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002768 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00002769 unsigned Idx = 0;
John McCallc12c5bb2010-05-15 11:32:37 +00002770 QualType Pointee = GetType(Record[Idx++]);
2771 return Context->getObjCObjectPointerType(Pointee);
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00002772 }
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +00002773
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002774 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCall49a832b2009-10-18 09:09:24 +00002775 unsigned Idx = 0;
2776 QualType Parm = GetType(Record[Idx++]);
2777 QualType Replacement = GetType(Record[Idx++]);
2778 return
2779 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
2780 Replacement);
2781 }
John McCall3cb0ebd2010-03-10 03:28:59 +00002782
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002783 case TYPE_INJECTED_CLASS_NAME: {
John McCall3cb0ebd2010-03-10 03:28:59 +00002784 CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0]));
2785 QualType TST = GetType(Record[1]); // probably derivable
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00002786 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002787 // for AST reading, too much interdependencies.
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00002788 return
2789 QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCall3cb0ebd2010-03-10 03:28:59 +00002790 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00002791
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002792 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002793 unsigned Idx = 0;
2794 unsigned Depth = Record[Idx++];
2795 unsigned Index = Record[Idx++];
2796 bool Pack = Record[Idx++];
2797 IdentifierInfo *Name = GetIdentifierInfo(Record, Idx);
2798 return Context->getTemplateTypeParmType(Depth, Index, Pack, Name);
2799 }
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00002800
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002801 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00002802 unsigned Idx = 0;
2803 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2804 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2805 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +00002806 QualType Canon = GetType(Record[Idx++]);
2807 return Context->getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00002808 }
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00002809
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002810 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00002811 unsigned Idx = 0;
2812 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2813 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2814 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
2815 unsigned NumArgs = Record[Idx++];
2816 llvm::SmallVector<TemplateArgument, 8> Args;
2817 Args.reserve(NumArgs);
2818 while (NumArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00002819 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00002820 return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
2821 Args.size(), Args.data());
2822 }
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00002823
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002824 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00002825 unsigned Idx = 0;
2826
2827 // ArrayType
2828 QualType ElementType = GetType(Record[Idx++]);
2829 ArrayType::ArraySizeModifier ASM
2830 = (ArrayType::ArraySizeModifier)Record[Idx++];
2831 unsigned IndexTypeQuals = Record[Idx++];
2832
2833 // DependentSizedArrayType
Sebastian Redlc3632732010-10-05 15:59:54 +00002834 Expr *NumElts = ReadExpr(*Loc.F);
2835 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00002836
2837 return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
2838 IndexTypeQuals, Brackets);
2839 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00002840
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002841 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002842 unsigned Idx = 0;
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002843 bool IsDependent = Record[Idx++];
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002844 TemplateName Name = ReadTemplateName(Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002845 llvm::SmallVector<TemplateArgument, 8> Args;
Sebastian Redlc3632732010-10-05 15:59:54 +00002846 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00002847 QualType Canon = GetType(Record[Idx++]);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002848 QualType T;
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002849 if (Canon.isNull())
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002850 T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(),
2851 Args.size());
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002852 else
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002853 T = Context->getTemplateSpecializationType(Name, Args.data(),
2854 Args.size(), Canon);
2855 T->Dependent = IsDependent;
2856 return T;
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002857 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002858 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002859 // Suppress a GCC warning
2860 return QualType();
2861}
2862
Sebastian Redlc3632732010-10-05 15:59:54 +00002863class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002864 ASTReader &Reader;
Sebastian Redlc3632732010-10-05 15:59:54 +00002865 ASTReader::PerFileData &F;
Sebastian Redl577d4792010-07-22 22:43:28 +00002866 llvm::BitstreamCursor &DeclsCursor;
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002867 const ASTReader::RecordData &Record;
John McCalla1ee0c52009-10-16 21:56:05 +00002868 unsigned &Idx;
2869
Sebastian Redlc3632732010-10-05 15:59:54 +00002870 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
2871 unsigned &I) {
2872 return Reader.ReadSourceLocation(F, R, I);
2873 }
2874
John McCalla1ee0c52009-10-16 21:56:05 +00002875public:
Sebastian Redlc3632732010-10-05 15:59:54 +00002876 TypeLocReader(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002877 const ASTReader::RecordData &Record, unsigned &Idx)
Sebastian Redlc3632732010-10-05 15:59:54 +00002878 : Reader(Reader), F(F), DeclsCursor(F.DeclsCursor), Record(Record), Idx(Idx)
2879 { }
John McCalla1ee0c52009-10-16 21:56:05 +00002880
John McCall51bd8032009-10-18 01:05:36 +00002881 // We want compile-time assurance that we've enumerated all of
2882 // these, so unfortunately we have to declare them first, then
2883 // define them out-of-line.
2884#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +00002885#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +00002886 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00002887#include "clang/AST/TypeLocNodes.def"
2888
John McCall51bd8032009-10-18 01:05:36 +00002889 void VisitFunctionTypeLoc(FunctionTypeLoc);
2890 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00002891};
2892
John McCall51bd8032009-10-18 01:05:36 +00002893void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCalla1ee0c52009-10-16 21:56:05 +00002894 // nothing to do
2895}
John McCall51bd8032009-10-18 01:05:36 +00002896void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00002897 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorddf889a2010-01-18 18:04:31 +00002898 if (TL.needsExtraLocalData()) {
2899 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
2900 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
2901 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
2902 TL.setModeAttr(Record[Idx++]);
2903 }
John McCalla1ee0c52009-10-16 21:56:05 +00002904}
John McCall51bd8032009-10-18 01:05:36 +00002905void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00002906 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00002907}
John McCall51bd8032009-10-18 01:05:36 +00002908void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00002909 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00002910}
John McCall51bd8032009-10-18 01:05:36 +00002911void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00002912 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00002913}
John McCall51bd8032009-10-18 01:05:36 +00002914void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00002915 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00002916}
John McCall51bd8032009-10-18 01:05:36 +00002917void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00002918 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00002919}
John McCall51bd8032009-10-18 01:05:36 +00002920void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00002921 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00002922}
John McCall51bd8032009-10-18 01:05:36 +00002923void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00002924 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
2925 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00002926 if (Record[Idx++])
Sebastian Redlc3632732010-10-05 15:59:54 +00002927 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002928 else
John McCall51bd8032009-10-18 01:05:36 +00002929 TL.setSizeExpr(0);
2930}
2931void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
2932 VisitArrayTypeLoc(TL);
2933}
2934void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
2935 VisitArrayTypeLoc(TL);
2936}
2937void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
2938 VisitArrayTypeLoc(TL);
2939}
2940void TypeLocReader::VisitDependentSizedArrayTypeLoc(
2941 DependentSizedArrayTypeLoc TL) {
2942 VisitArrayTypeLoc(TL);
2943}
2944void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
2945 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00002946 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00002947}
2948void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00002949 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00002950}
2951void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00002952 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00002953}
2954void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00002955 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
2956 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
Douglas Gregordab60ad2010-10-01 18:44:50 +00002957 TL.setTrailingReturn(Record[Idx++]);
John McCall51bd8032009-10-18 01:05:36 +00002958 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
John McCall86acc2a2009-10-23 01:28:53 +00002959 TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
John McCall51bd8032009-10-18 01:05:36 +00002960 }
2961}
2962void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
2963 VisitFunctionTypeLoc(TL);
2964}
2965void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
2966 VisitFunctionTypeLoc(TL);
2967}
John McCalled976492009-12-04 22:46:56 +00002968void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00002969 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalled976492009-12-04 22:46:56 +00002970}
John McCall51bd8032009-10-18 01:05:36 +00002971void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00002972 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00002973}
2974void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00002975 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
2976 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
2977 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00002978}
2979void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00002980 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
2981 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
2982 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
2983 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00002984}
2985void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00002986 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00002987}
2988void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00002989 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00002990}
2991void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00002992 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00002993}
John McCall51bd8032009-10-18 01:05:36 +00002994void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00002995 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00002996}
John McCall49a832b2009-10-18 09:09:24 +00002997void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
2998 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00002999 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall49a832b2009-10-18 09:09:24 +00003000}
John McCall51bd8032009-10-18 01:05:36 +00003001void TypeLocReader::VisitTemplateSpecializationTypeLoc(
3002 TemplateSpecializationTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003003 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
3004 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3005 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall833ca992009-10-29 08:12:44 +00003006 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
3007 TL.setArgLocInfo(i,
Sebastian Redlc3632732010-10-05 15:59:54 +00003008 Reader.GetTemplateArgumentLocInfo(F,
3009 TL.getTypePtr()->getArg(i).getKind(),
3010 Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003011}
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003012void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003013 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3014 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003015}
John McCall3cb0ebd2010-03-10 03:28:59 +00003016void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003017 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall3cb0ebd2010-03-10 03:28:59 +00003018}
Douglas Gregor4714c122010-03-31 17:34:00 +00003019void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003020 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3021 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
3022 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003023}
John McCall33500952010-06-11 00:33:02 +00003024void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
3025 DependentTemplateSpecializationTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003026 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3027 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
3028 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3029 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3030 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall33500952010-06-11 00:33:02 +00003031 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
3032 TL.setArgLocInfo(I,
Sebastian Redlc3632732010-10-05 15:59:54 +00003033 Reader.GetTemplateArgumentLocInfo(F,
3034 TL.getTypePtr()->getArg(I).getKind(),
3035 Record, Idx));
John McCall33500952010-06-11 00:33:02 +00003036}
John McCall51bd8032009-10-18 01:05:36 +00003037void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003038 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallc12c5bb2010-05-15 11:32:37 +00003039}
3040void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3041 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00003042 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3043 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003044 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redlc3632732010-10-05 15:59:54 +00003045 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003046}
John McCall54e14c42009-10-22 22:37:11 +00003047void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003048 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall54e14c42009-10-22 22:37:11 +00003049}
John McCalla1ee0c52009-10-16 21:56:05 +00003050
Sebastian Redlc3632732010-10-05 15:59:54 +00003051TypeSourceInfo *ASTReader::GetTypeSourceInfo(PerFileData &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00003052 const RecordData &Record,
John McCalla1ee0c52009-10-16 21:56:05 +00003053 unsigned &Idx) {
3054 QualType InfoTy = GetType(Record[Idx++]);
3055 if (InfoTy.isNull())
3056 return 0;
3057
John McCalla93c9342009-12-07 02:54:59 +00003058 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
Sebastian Redlc3632732010-10-05 15:59:54 +00003059 TypeLocReader TLR(*this, F, Record, Idx);
John McCalla93c9342009-12-07 02:54:59 +00003060 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCalla1ee0c52009-10-16 21:56:05 +00003061 TLR.Visit(TL);
John McCalla93c9342009-12-07 02:54:59 +00003062 return TInfo;
John McCalla1ee0c52009-10-16 21:56:05 +00003063}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003064
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003065QualType ASTReader::GetType(TypeID ID) {
John McCall0953e762009-09-24 19:53:00 +00003066 unsigned FastQuals = ID & Qualifiers::FastMask;
3067 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003068
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003069 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003070 QualType T;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003071 switch ((PredefinedTypeIDs)Index) {
3072 case PREDEF_TYPE_NULL_ID: return QualType();
3073 case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
3074 case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003075
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003076 case PREDEF_TYPE_CHAR_U_ID:
3077 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregor2cf26342009-04-09 22:27:44 +00003078 // FIXME: Check that the signedness of CharTy is correct!
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003079 T = Context->CharTy;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003080 break;
3081
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003082 case PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break;
3083 case PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break;
3084 case PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break;
3085 case PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break;
3086 case PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break;
3087 case PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break;
3088 case PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break;
3089 case PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break;
3090 case PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break;
3091 case PREDEF_TYPE_INT_ID: T = Context->IntTy; break;
3092 case PREDEF_TYPE_LONG_ID: T = Context->LongTy; break;
3093 case PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break;
3094 case PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break;
3095 case PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break;
3096 case PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break;
3097 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break;
3098 case PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break;
3099 case PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break;
3100 case PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break;
3101 case PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break;
3102 case PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break;
3103 case PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break;
3104 case PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break;
3105 case PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003106 }
3107
3108 assert(!T.isNull() && "Unknown predefined type");
John McCall0953e762009-09-24 19:53:00 +00003109 return T.withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003110 }
3111
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003112 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003113 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl07a353c2010-07-14 20:26:45 +00003114 if (TypesLoaded[Index].isNull()) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003115 TypesLoaded[Index] = ReadTypeRecord(Index);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003116 TypesLoaded[Index]->setFromAST();
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003117 TypeIdxs[TypesLoaded[Index]] = TypeIdx::fromTypeID(ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003118 if (DeserializationListener)
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003119 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1476ed42010-07-16 16:36:56 +00003120 TypesLoaded[Index]);
Sebastian Redl07a353c2010-07-14 20:26:45 +00003121 }
Mike Stump1eb44332009-09-09 15:08:12 +00003122
John McCall0953e762009-09-24 19:53:00 +00003123 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003124}
3125
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003126TypeID ASTReader::GetTypeID(QualType T) const {
3127 return MakeTypeID(T,
3128 std::bind1st(std::mem_fun(&ASTReader::GetTypeIdx), this));
3129}
3130
3131TypeIdx ASTReader::GetTypeIdx(QualType T) const {
3132 if (T.isNull())
3133 return TypeIdx();
3134 assert(!T.getLocalFastQualifiers());
3135
3136 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3137 // GetTypeIdx is mostly used for computing the hash of DeclarationNames and
3138 // comparing keys of ASTDeclContextNameLookupTable.
3139 // If the type didn't come from the AST file use a specially marked index
3140 // so that any hash/key comparison fail since no such index is stored
3141 // in a AST file.
3142 if (I == TypeIdxs.end())
3143 return TypeIdx(-1);
3144 return I->second;
3145}
3146
John McCall833ca992009-10-29 08:12:44 +00003147TemplateArgumentLocInfo
Sebastian Redlc3632732010-10-05 15:59:54 +00003148ASTReader::GetTemplateArgumentLocInfo(PerFileData &F,
3149 TemplateArgument::ArgKind Kind,
John McCall833ca992009-10-29 08:12:44 +00003150 const RecordData &Record,
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003151 unsigned &Index) {
John McCall833ca992009-10-29 08:12:44 +00003152 switch (Kind) {
3153 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00003154 return ReadExpr(F);
John McCall833ca992009-10-29 08:12:44 +00003155 case TemplateArgument::Type:
Sebastian Redlc3632732010-10-05 15:59:54 +00003156 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor788cd062009-11-11 01:00:40 +00003157 case TemplateArgument::Template: {
Sebastian Redlc3632732010-10-05 15:59:54 +00003158 SourceRange QualifierRange = ReadSourceRange(F, Record, Index);
3159 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003160 return TemplateArgumentLocInfo(QualifierRange, TemplateNameLoc);
Douglas Gregor788cd062009-11-11 01:00:40 +00003161 }
John McCall833ca992009-10-29 08:12:44 +00003162 case TemplateArgument::Null:
3163 case TemplateArgument::Integral:
3164 case TemplateArgument::Declaration:
3165 case TemplateArgument::Pack:
3166 return TemplateArgumentLocInfo();
3167 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00003168 llvm_unreachable("unexpected template argument loc");
John McCall833ca992009-10-29 08:12:44 +00003169 return TemplateArgumentLocInfo();
3170}
3171
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003172TemplateArgumentLoc
Sebastian Redlc3632732010-10-05 15:59:54 +00003173ASTReader::ReadTemplateArgumentLoc(PerFileData &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00003174 const RecordData &Record, unsigned &Index) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003175 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003176
3177 if (Arg.getKind() == TemplateArgument::Expression) {
3178 if (Record[Index++]) // bool InfoHasSameExpr.
3179 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
3180 }
Sebastian Redlc3632732010-10-05 15:59:54 +00003181 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003182 Record, Index));
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003183}
3184
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003185Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall76bd1f32010-06-01 09:23:16 +00003186 return GetDecl(ID);
3187}
3188
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003189TranslationUnitDecl *ASTReader::GetTranslationUnitDecl() {
Sebastian Redl30c514c2010-07-14 23:45:08 +00003190 if (!DeclsLoaded[0]) {
Sebastian Redle1dde812010-08-24 00:50:04 +00003191 ReadDeclRecord(0, 1);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003192 if (DeserializationListener)
Sebastian Redl1476ed42010-07-16 16:36:56 +00003193 DeserializationListener->DeclRead(1, DeclsLoaded[0]);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003194 }
Argyrios Kyrtzidis8871a442010-07-08 17:13:02 +00003195
3196 return cast<TranslationUnitDecl>(DeclsLoaded[0]);
3197}
3198
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003199Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003200 if (ID == 0)
3201 return 0;
3202
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003203 if (ID > DeclsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003204 Error("declaration ID out-of-range for AST file");
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003205 return 0;
3206 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003207
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003208 unsigned Index = ID - 1;
Sebastian Redl30c514c2010-07-14 23:45:08 +00003209 if (!DeclsLoaded[Index]) {
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00003210 ReadDeclRecord(Index, ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003211 if (DeserializationListener)
3212 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
3213 }
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003214
3215 return DeclsLoaded[Index];
Douglas Gregor2cf26342009-04-09 22:27:44 +00003216}
3217
Chris Lattner887e2b32009-04-27 05:46:25 +00003218/// \brief Resolve the offset of a statement into a statement.
3219///
3220/// This operation will read a new statement from the external
3221/// source each time it is called, and is meant to be used via a
3222/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003223Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003224 // Offset here is a global offset across the entire chain.
3225 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3226 PerFileData &F = *Chain[N - I - 1];
3227 if (Offset < F.SizeInBits) {
3228 // Since we know that this statement is part of a decl, make sure to use
3229 // the decl cursor to read it.
3230 F.DeclsCursor.JumpToBit(Offset);
Sebastian Redlc3632732010-10-05 15:59:54 +00003231 return ReadStmtFromStream(F);
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003232 }
3233 Offset -= F.SizeInBits;
3234 }
3235 llvm_unreachable("Broken chain");
Douglas Gregor250fc9c2009-04-18 00:07:54 +00003236}
3237
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003238bool ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +00003239 llvm::SmallVectorImpl<Decl*> &Decls) {
Mike Stump1eb44332009-09-09 15:08:12 +00003240 assert(DC->hasExternalLexicalStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00003241 "DeclContext has no lexical decls in storage");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003242
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003243 // There might be lexical decls in multiple parts of the chain, for the TU
3244 // at least.
Sebastian Redl4a9eb262010-09-28 02:24:44 +00003245 // DeclContextOffsets might reallocate as we load additional decls below,
3246 // so make a copy of the vector.
3247 DeclContextInfos Infos = DeclContextOffsets[DC];
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003248 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3249 I != E; ++I) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003250 // IDs can be 0 if this context doesn't contain declarations.
3251 if (!I->LexicalDecls)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003252 continue;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003253
3254 // Load all of the declaration IDs
Sebastian Redledadecc2010-09-28 02:55:49 +00003255 for (const DeclID *ID = I->LexicalDecls, *IDE = ID + I->NumLexicalDecls;
3256 ID != IDE; ++ID) {
Sebastian Redl4a9eb262010-09-28 02:24:44 +00003257 Decl *D = GetDecl(*ID);
3258 assert(D && "Null decl in lexical decls");
3259 Decls.push_back(D);
3260 }
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003261 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003262
Douglas Gregor25123082009-04-22 22:34:57 +00003263 ++NumLexicalDeclContextsRead;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003264 return false;
3265}
3266
John McCall76bd1f32010-06-01 09:23:16 +00003267DeclContext::lookup_result
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003268ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +00003269 DeclarationName Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00003270 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00003271 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003272 if (!Name)
3273 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
3274 DeclContext::lookup_iterator(0));
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003275
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003276 llvm::SmallVector<NamedDecl *, 64> Decls;
Sebastian Redl8b122732010-08-24 00:49:55 +00003277 // There might be visible decls in multiple parts of the chain, for the TU
Sebastian Redl5967d622010-08-24 00:50:16 +00003278 // and namespaces. For any given name, the last available results replace
3279 // all earlier ones. For this reason, we walk in reverse.
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003280 DeclContextInfos &Infos = DeclContextOffsets[DC];
Sebastian Redl5967d622010-08-24 00:50:16 +00003281 for (DeclContextInfos::reverse_iterator I = Infos.rbegin(), E = Infos.rend();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003282 I != E; ++I) {
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003283 if (!I->NameLookupTableData)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003284 continue;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003285
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003286 ASTDeclContextNameLookupTable *LookupTable =
3287 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3288 ASTDeclContextNameLookupTable::iterator Pos = LookupTable->find(Name);
3289 if (Pos == LookupTable->end())
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003290 continue;
3291
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003292 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
3293 for (; Data.first != Data.second; ++Data.first)
3294 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
Sebastian Redl5967d622010-08-24 00:50:16 +00003295 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003296 }
3297
Douglas Gregor25123082009-04-22 22:34:57 +00003298 ++NumVisibleDeclContextsRead;
John McCall76bd1f32010-06-01 09:23:16 +00003299
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003300 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall76bd1f32010-06-01 09:23:16 +00003301 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003302}
3303
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00003304void ASTReader::MaterializeVisibleDecls(const DeclContext *DC) {
3305 assert(DC->hasExternalVisibleStorage() &&
3306 "DeclContext has no visible decls in storage");
3307
3308 llvm::SmallVector<NamedDecl *, 64> Decls;
3309 // There might be visible decls in multiple parts of the chain, for the TU
3310 // and namespaces.
3311 DeclContextInfos &Infos = DeclContextOffsets[DC];
3312 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3313 I != E; ++I) {
3314 if (!I->NameLookupTableData)
3315 continue;
3316
3317 ASTDeclContextNameLookupTable *LookupTable =
3318 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3319 for (ASTDeclContextNameLookupTable::item_iterator
3320 ItemI = LookupTable->item_begin(),
3321 ItemEnd = LookupTable->item_end() ; ItemI != ItemEnd; ++ItemI) {
3322 ASTDeclContextNameLookupTable::item_iterator::value_type Val
3323 = *ItemI;
3324 ASTDeclContextNameLookupTrait::data_type Data = Val.second;
3325 Decls.clear();
3326 for (; Data.first != Data.second; ++Data.first)
3327 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
3328 MaterializeVisibleDeclsForName(DC, Val.first, Decls);
3329 }
3330 }
3331}
3332
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003333void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003334 assert(Consumer);
3335 while (!InterestingDecls.empty()) {
3336 DeclGroupRef DG(InterestingDecls.front());
3337 InterestingDecls.pop_front();
Sebastian Redl27372b42010-08-11 18:52:41 +00003338 Consumer->HandleInterestingDecl(DG);
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003339 }
3340}
3341
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003342void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor0af2ca42009-04-22 19:09:20 +00003343 this->Consumer = Consumer;
3344
Douglas Gregorfdd01722009-04-14 00:24:19 +00003345 if (!Consumer)
3346 return;
3347
3348 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003349 // Force deserialization of this decl, which will cause it to be queued for
3350 // passing to the consumer.
Daniel Dunbar04a0b502009-09-17 03:06:44 +00003351 GetDecl(ExternalDefinitions[I]);
Douglas Gregorfdd01722009-04-14 00:24:19 +00003352 }
Douglas Gregorc62a2fe2009-04-25 00:41:30 +00003353
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003354 PassInterestingDeclsToConsumer();
Douglas Gregorfdd01722009-04-14 00:24:19 +00003355}
3356
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003357void ASTReader::PrintStats() {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003358 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregor2cf26342009-04-09 22:27:44 +00003359
Mike Stump1eb44332009-09-09 15:08:12 +00003360 unsigned NumTypesLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003361 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall0953e762009-09-24 19:53:00 +00003362 QualType());
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003363 unsigned NumDeclsLoaded
3364 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
3365 (Decl *)0);
3366 unsigned NumIdentifiersLoaded
3367 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
3368 IdentifiersLoaded.end(),
3369 (IdentifierInfo *)0);
Mike Stump1eb44332009-09-09 15:08:12 +00003370 unsigned NumSelectorsLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003371 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
3372 SelectorsLoaded.end(),
3373 Selector());
Douglas Gregor2d41cc12009-04-13 20:50:16 +00003374
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003375 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
3376 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00003377 if (TotalNumSLocEntries)
3378 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
3379 NumSLocEntriesRead, TotalNumSLocEntries,
3380 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003381 if (!TypesLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003382 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003383 NumTypesLoaded, (unsigned)TypesLoaded.size(),
3384 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
3385 if (!DeclsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003386 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003387 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
3388 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003389 if (!IdentifiersLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003390 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003391 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
3392 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redl725cd962010-08-04 20:40:17 +00003393 if (!SelectorsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003394 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redl725cd962010-08-04 20:40:17 +00003395 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
3396 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor83941df2009-04-25 17:48:32 +00003397 if (TotalNumStatements)
3398 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
3399 NumStatementsRead, TotalNumStatements,
3400 ((float)NumStatementsRead/TotalNumStatements * 100));
3401 if (TotalNumMacros)
3402 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
3403 NumMacrosRead, TotalNumMacros,
3404 ((float)NumMacrosRead/TotalNumMacros * 100));
3405 if (TotalLexicalDeclContexts)
3406 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
3407 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
3408 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
3409 * 100));
3410 if (TotalVisibleDeclContexts)
3411 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
3412 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
3413 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
3414 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003415 if (TotalNumMethodPoolEntries) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003416 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003417 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
3418 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor83941df2009-04-25 17:48:32 +00003419 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003420 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor83941df2009-04-25 17:48:32 +00003421 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003422 std::fprintf(stderr, "\n");
3423}
3424
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003425void ASTReader::InitializeSema(Sema &S) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00003426 SemaObj = &S;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003427 S.ExternalSource = this;
3428
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00003429 // Makes sure any declarations that were deserialized "too early"
3430 // still get added to the identifier's declaration chains.
Douglas Gregor76dc8892010-09-24 23:29:12 +00003431 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
3432 if (SemaObj->TUScope)
John McCalld226f652010-08-21 09:40:31 +00003433 SemaObj->TUScope->AddDecl(PreloadedDecls[I]);
Douglas Gregor76dc8892010-09-24 23:29:12 +00003434
3435 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregor668c1a42009-04-21 22:25:48 +00003436 }
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00003437 PreloadedDecls.clear();
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003438
3439 // If there were any tentative definitions, deserialize them and add
Sebastian Redle9d12b62010-01-31 22:27:38 +00003440 // them to Sema's list of tentative definitions.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003441 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
3442 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
Sebastian Redle9d12b62010-01-31 22:27:38 +00003443 SemaObj->TentativeDefinitions.push_back(Var);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003444 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00003445
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003446 // If there were any unused file scoped decls, deserialize them and add to
3447 // Sema's list of unused file scoped decls.
3448 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
3449 DeclaratorDecl *D = cast<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
3450 SemaObj->UnusedFileScopedDecls.push_back(D);
Tanya Lattnere6bbc012010-02-12 00:07:30 +00003451 }
Douglas Gregor14c22f22009-04-22 22:18:58 +00003452
3453 // If there were any locally-scoped external declarations,
3454 // deserialize them and add them to Sema's table of locally-scoped
3455 // external declarations.
3456 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
3457 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
3458 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
3459 }
Douglas Gregorb81c1702009-04-27 20:06:05 +00003460
3461 // If there were any ext_vector type declarations, deserialize them
3462 // and add them to Sema's vector of such declarations.
3463 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I)
3464 SemaObj->ExtVectorDecls.push_back(
3465 cast<TypedefDecl>(GetDecl(ExtVectorDecls[I])));
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003466
3467 // FIXME: Do VTable uses and dynamic classes deserialize too much ?
3468 // Can we cut them down before writing them ?
3469
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003470 // If there were any dynamic classes declarations, deserialize them
3471 // and add them to Sema's vector of such declarations.
3472 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I)
3473 SemaObj->DynamicClasses.push_back(
3474 cast<CXXRecordDecl>(GetDecl(DynamicClasses[I])));
Fariborz Jahanian32019832010-07-23 19:11:11 +00003475
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003476 // Load the offsets of the declarations that Sema references.
3477 // They will be lazily deserialized when needed.
3478 if (!SemaDeclRefs.empty()) {
3479 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
3480 SemaObj->StdNamespace = SemaDeclRefs[0];
3481 SemaObj->StdBadAlloc = SemaDeclRefs[1];
3482 }
3483
Sebastian Redlc3632732010-10-05 15:59:54 +00003484 for (PerFileData *F = FirstInSource; F; F = F->NextInSource) {
3485
3486 // If there are @selector references added them to its pool. This is for
3487 // implementation of -Wselector.
3488 if (!F->ReferencedSelectorsData.empty()) {
3489 unsigned int DataSize = F->ReferencedSelectorsData.size()-1;
3490 unsigned I = 0;
3491 while (I < DataSize) {
3492 Selector Sel = DecodeSelector(F->ReferencedSelectorsData[I++]);
3493 SourceLocation SelLoc = ReadSourceLocation(
3494 *F, F->ReferencedSelectorsData, I);
3495 SemaObj->ReferencedSelectors.insert(std::make_pair(Sel, SelLoc));
3496 }
3497 }
3498
3499 // If there were any pending implicit instantiations, deserialize them
3500 // and add them to Sema's queue of such instantiations.
3501 assert(F->PendingInstantiations.size() % 2 == 0 &&
3502 "Expected pairs of entries");
3503 for (unsigned Idx = 0, N = F->PendingInstantiations.size(); Idx < N;) {
3504 ValueDecl *D=cast<ValueDecl>(GetDecl(F->PendingInstantiations[Idx++]));
3505 SourceLocation Loc = ReadSourceLocation(*F, F->PendingInstantiations,Idx);
3506 SemaObj->PendingInstantiations.push_back(std::make_pair(D, Loc));
3507 }
3508 }
3509
3510 // The two special data sets below always come from the most recent PCH,
3511 // which is at the front of the chain.
3512 PerFileData &F = *Chain.front();
3513
3514 // If there were any weak undeclared identifiers, deserialize them and add to
3515 // Sema's list of weak undeclared identifiers.
3516 if (!WeakUndeclaredIdentifiers.empty()) {
3517 unsigned Idx = 0;
3518 for (unsigned I = 0, N = WeakUndeclaredIdentifiers[Idx++]; I != N; ++I) {
3519 IdentifierInfo *WeakId = GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3520 IdentifierInfo *AliasId= GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3521 SourceLocation Loc = ReadSourceLocation(F, WeakUndeclaredIdentifiers,Idx);
3522 bool Used = WeakUndeclaredIdentifiers[Idx++];
3523 Sema::WeakInfo WI(AliasId, Loc);
3524 WI.setUsed(Used);
3525 SemaObj->WeakUndeclaredIdentifiers.insert(std::make_pair(WeakId, WI));
3526 }
3527 }
3528
3529 // If there were any VTable uses, deserialize the information and add it
3530 // to Sema's vector and map of VTable uses.
3531 if (!VTableUses.empty()) {
3532 unsigned Idx = 0;
3533 for (unsigned I = 0, N = VTableUses[Idx++]; I != N; ++I) {
3534 CXXRecordDecl *Class = cast<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
3535 SourceLocation Loc = ReadSourceLocation(F, VTableUses, Idx);
3536 bool DefinitionRequired = VTableUses[Idx++];
3537 SemaObj->VTableUses.push_back(std::make_pair(Class, Loc));
3538 SemaObj->VTablesUsed[Class] = DefinitionRequired;
Fariborz Jahanian32019832010-07-23 19:11:11 +00003539 }
3540 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00003541}
3542
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003543IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Sebastian Redld8c5abb2010-08-02 18:30:12 +00003544 // Try to find this name within our on-disk hash tables. We start with the
3545 // most recent one, since that one contains the most up-to-date info.
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003546 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003547 ASTIdentifierLookupTable *IdTable
3548 = (ASTIdentifierLookupTable *)Chain[I]->IdentifierLookupTable;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003549 if (!IdTable)
3550 continue;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003551 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003552 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003553 if (Pos == IdTable->end())
3554 continue;
Douglas Gregor668c1a42009-04-21 22:25:48 +00003555
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003556 // Dereferencing the iterator has the effect of building the
3557 // IdentifierInfo node and populating it with the various
3558 // declarations it needs.
Sebastian Redld8c5abb2010-08-02 18:30:12 +00003559 return *Pos;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003560 }
Sebastian Redld8c5abb2010-08-02 18:30:12 +00003561 return 0;
Douglas Gregor668c1a42009-04-21 22:25:48 +00003562}
3563
Mike Stump1eb44332009-09-09 15:08:12 +00003564std::pair<ObjCMethodList, ObjCMethodList>
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003565ASTReader::ReadMethodPool(Selector Sel) {
Sebastian Redl725cd962010-08-04 20:40:17 +00003566 // Find this selector in a hash table. We want to find the most recent entry.
3567 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3568 PerFileData &F = *Chain[I];
3569 if (!F.SelectorLookupTable)
3570 continue;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003571
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003572 ASTSelectorLookupTable *PoolTable
3573 = (ASTSelectorLookupTable*)F.SelectorLookupTable;
3574 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
Sebastian Redl725cd962010-08-04 20:40:17 +00003575 if (Pos != PoolTable->end()) {
3576 ++NumSelectorsRead;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003577 // FIXME: Not quite happy with the statistics here. We probably should
3578 // disable this tracking when called via LoadSelector.
3579 // Also, should entries without methods count as misses?
3580 ++NumMethodPoolEntriesRead;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003581 ASTSelectorLookupTrait::data_type Data = *Pos;
Sebastian Redl725cd962010-08-04 20:40:17 +00003582 if (DeserializationListener)
3583 DeserializationListener->SelectorRead(Data.ID, Sel);
3584 return std::make_pair(Data.Instance, Data.Factory);
3585 }
Douglas Gregor83941df2009-04-25 17:48:32 +00003586 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003587
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003588 ++NumMethodPoolMisses;
Sebastian Redl725cd962010-08-04 20:40:17 +00003589 return std::pair<ObjCMethodList, ObjCMethodList>();
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003590}
3591
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003592void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redle58aa892010-08-04 18:21:41 +00003593 // It would be complicated to avoid reading the methods anyway. So don't.
3594 ReadMethodPool(Sel);
3595}
3596
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003597void ASTReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00003598 assert(ID && "Non-zero identifier ID required");
Douglas Gregora02b1472009-04-28 21:53:25 +00003599 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003600 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003601 if (DeserializationListener)
3602 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregor668c1a42009-04-21 22:25:48 +00003603}
3604
Douglas Gregord89275b2009-07-06 18:54:52 +00003605/// \brief Set the globally-visible declarations associated with the given
3606/// identifier.
3607///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003608/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump1eb44332009-09-09 15:08:12 +00003609/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregord89275b2009-07-06 18:54:52 +00003610/// them.
3611///
3612/// \param II an IdentifierInfo that refers to one or more globally-visible
3613/// declarations.
3614///
3615/// \param DeclIDs the set of declaration IDs with the name @p II that are
3616/// visible at global scope.
3617///
3618/// \param Nonrecursive should be true to indicate that the caller knows that
3619/// this call is non-recursive, and therefore the globally-visible declarations
3620/// will not be placed onto the pending queue.
Mike Stump1eb44332009-09-09 15:08:12 +00003621void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003622ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Douglas Gregord89275b2009-07-06 18:54:52 +00003623 const llvm::SmallVectorImpl<uint32_t> &DeclIDs,
3624 bool Nonrecursive) {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00003625 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregord89275b2009-07-06 18:54:52 +00003626 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
3627 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
3628 PII.II = II;
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00003629 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregord89275b2009-07-06 18:54:52 +00003630 return;
3631 }
Mike Stump1eb44332009-09-09 15:08:12 +00003632
Douglas Gregord89275b2009-07-06 18:54:52 +00003633 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
3634 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
3635 if (SemaObj) {
Douglas Gregor914ed9d2010-08-13 03:15:25 +00003636 if (SemaObj->TUScope) {
3637 // Introduce this declaration into the translation-unit scope
3638 // and add it to the declaration chain for this identifier, so
3639 // that (unqualified) name lookup will find it.
John McCalld226f652010-08-21 09:40:31 +00003640 SemaObj->TUScope->AddDecl(D);
Douglas Gregor914ed9d2010-08-13 03:15:25 +00003641 }
Douglas Gregor76dc8892010-09-24 23:29:12 +00003642 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
Douglas Gregord89275b2009-07-06 18:54:52 +00003643 } else {
3644 // Queue this declaration so that it will be added to the
3645 // translation unit scope and identifier's declaration chain
3646 // once a Sema object is known.
3647 PreloadedDecls.push_back(D);
3648 }
3649 }
3650}
3651
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003652IdentifierInfo *ASTReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00003653 if (ID == 0)
3654 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003655
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00003656 if (IdentifiersLoaded.empty()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003657 Error("no identifier table in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00003658 return 0;
3659 }
Mike Stump1eb44332009-09-09 15:08:12 +00003660
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003661 assert(PP && "Forgot to set Preprocessor ?");
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00003662 ID -= 1;
3663 if (!IdentifiersLoaded[ID]) {
3664 unsigned Index = ID;
3665 const char *Str = 0;
3666 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3667 PerFileData *F = Chain[N - I - 1];
3668 if (Index < F->LocalNumIdentifiers) {
3669 uint32_t Offset = F->IdentifierOffsets[Index];
3670 Str = F->IdentifierTableData + Offset;
3671 break;
3672 }
3673 Index -= F->LocalNumIdentifiers;
3674 }
3675 assert(Str && "Broken Chain");
Douglas Gregord6595a42009-04-25 21:04:17 +00003676
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003677 // All of the strings in the AST file are preceded by a 16-bit length.
3678 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenek231bc0b2009-10-23 04:45:31 +00003679 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
3680 // unsigned integers. This is important to avoid integer overflow when
3681 // we cast them to 'unsigned'.
Ted Kremenekff1ea462009-10-23 03:57:22 +00003682 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregor02fc7512009-04-28 20:01:51 +00003683 unsigned StrLen = (((unsigned) StrLenPtr[0])
3684 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00003685 IdentifiersLoaded[ID]
Kovarththanan Rajaratnam811f4262010-03-12 10:32:27 +00003686 = &PP->getIdentifierTable().get(Str, StrLen);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003687 if (DeserializationListener)
3688 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregorafaf3082009-04-11 00:14:32 +00003689 }
Mike Stump1eb44332009-09-09 15:08:12 +00003690
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00003691 return IdentifiersLoaded[ID];
Douglas Gregor2cf26342009-04-09 22:27:44 +00003692}
3693
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003694void ASTReader::ReadSLocEntry(unsigned ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00003695 ReadSLocEntryRecord(ID);
3696}
3697
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003698Selector ASTReader::DecodeSelector(unsigned ID) {
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003699 if (ID == 0)
3700 return Selector();
Mike Stump1eb44332009-09-09 15:08:12 +00003701
Sebastian Redl725cd962010-08-04 20:40:17 +00003702 if (ID > SelectorsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003703 Error("selector ID out of range in AST file");
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003704 return Selector();
3705 }
Douglas Gregor83941df2009-04-25 17:48:32 +00003706
Sebastian Redl725cd962010-08-04 20:40:17 +00003707 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003708 // Load this selector from the selector table.
Sebastian Redl725cd962010-08-04 20:40:17 +00003709 unsigned Idx = ID - 1;
3710 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3711 PerFileData &F = *Chain[N - I - 1];
3712 if (Idx < F.LocalNumSelectors) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003713 ASTSelectorLookupTrait Trait(*this);
Sebastian Redl725cd962010-08-04 20:40:17 +00003714 SelectorsLoaded[ID - 1] =
3715 Trait.ReadKey(F.SelectorLookupTableData + F.SelectorOffsets[Idx], 0);
3716 if (DeserializationListener)
3717 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
3718 break;
3719 }
3720 Idx -= F.LocalNumSelectors;
3721 }
Douglas Gregor83941df2009-04-25 17:48:32 +00003722 }
3723
Sebastian Redl725cd962010-08-04 20:40:17 +00003724 return SelectorsLoaded[ID - 1];
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003725}
3726
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003727Selector ASTReader::GetExternalSelector(uint32_t ID) {
Douglas Gregor719770d2010-04-06 17:30:22 +00003728 return DecodeSelector(ID);
3729}
3730
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003731uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redl725cd962010-08-04 20:40:17 +00003732 // ID 0 (the null selector) is considered an external selector.
3733 return getTotalNumSelectors() + 1;
Douglas Gregor719770d2010-04-06 17:30:22 +00003734}
3735
Mike Stump1eb44332009-09-09 15:08:12 +00003736DeclarationName
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003737ASTReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003738 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
3739 switch (Kind) {
3740 case DeclarationName::Identifier:
3741 return DeclarationName(GetIdentifierInfo(Record, Idx));
3742
3743 case DeclarationName::ObjCZeroArgSelector:
3744 case DeclarationName::ObjCOneArgSelector:
3745 case DeclarationName::ObjCMultiArgSelector:
Steve Naroffa7503a72009-04-23 15:15:40 +00003746 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00003747
3748 case DeclarationName::CXXConstructorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003749 return Context->DeclarationNames.getCXXConstructorName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00003750 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00003751
3752 case DeclarationName::CXXDestructorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003753 return Context->DeclarationNames.getCXXDestructorName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00003754 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00003755
3756 case DeclarationName::CXXConversionFunctionName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003757 return Context->DeclarationNames.getCXXConversionFunctionName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00003758 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00003759
3760 case DeclarationName::CXXOperatorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003761 return Context->DeclarationNames.getCXXOperatorName(
Douglas Gregor2cf26342009-04-09 22:27:44 +00003762 (OverloadedOperatorKind)Record[Idx++]);
3763
Sean Hunt3e518bd2009-11-29 07:34:05 +00003764 case DeclarationName::CXXLiteralOperatorName:
3765 return Context->DeclarationNames.getCXXLiteralOperatorName(
3766 GetIdentifierInfo(Record, Idx));
3767
Douglas Gregor2cf26342009-04-09 22:27:44 +00003768 case DeclarationName::CXXUsingDirective:
3769 return DeclarationName::getUsingDirectiveName();
3770 }
3771
3772 // Required to silence GCC warning
3773 return DeclarationName();
3774}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003775
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003776TemplateName
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003777ASTReader::ReadTemplateName(const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003778 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
3779 switch (Kind) {
3780 case TemplateName::Template:
3781 return TemplateName(cast_or_null<TemplateDecl>(GetDecl(Record[Idx++])));
3782
3783 case TemplateName::OverloadedTemplate: {
3784 unsigned size = Record[Idx++];
3785 UnresolvedSet<8> Decls;
3786 while (size--)
3787 Decls.addDecl(cast<NamedDecl>(GetDecl(Record[Idx++])));
3788
3789 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
3790 }
3791
3792 case TemplateName::QualifiedTemplate: {
3793 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3794 bool hasTemplKeyword = Record[Idx++];
3795 TemplateDecl *Template = cast<TemplateDecl>(GetDecl(Record[Idx++]));
3796 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
3797 }
3798
3799 case TemplateName::DependentTemplate: {
3800 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3801 if (Record[Idx++]) // isIdentifier
3802 return Context->getDependentTemplateName(NNS,
3803 GetIdentifierInfo(Record, Idx));
3804 return Context->getDependentTemplateName(NNS,
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003805 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003806 }
3807 }
3808
3809 assert(0 && "Unhandled template name kind!");
3810 return TemplateName();
3811}
3812
3813TemplateArgument
Sebastian Redlc3632732010-10-05 15:59:54 +00003814ASTReader::ReadTemplateArgument(PerFileData &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00003815 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003816 switch ((TemplateArgument::ArgKind)Record[Idx++]) {
3817 case TemplateArgument::Null:
3818 return TemplateArgument();
3819 case TemplateArgument::Type:
3820 return TemplateArgument(GetType(Record[Idx++]));
3821 case TemplateArgument::Declaration:
3822 return TemplateArgument(GetDecl(Record[Idx++]));
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00003823 case TemplateArgument::Integral: {
3824 llvm::APSInt Value = ReadAPSInt(Record, Idx);
3825 QualType T = GetType(Record[Idx++]);
3826 return TemplateArgument(Value, T);
3827 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003828 case TemplateArgument::Template:
3829 return TemplateArgument(ReadTemplateName(Record, Idx));
3830 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00003831 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003832 case TemplateArgument::Pack: {
3833 unsigned NumArgs = Record[Idx++];
3834 llvm::SmallVector<TemplateArgument, 8> Args;
3835 Args.reserve(NumArgs);
3836 while (NumArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00003837 Args.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003838 TemplateArgument TemplArg;
3839 TemplArg.setArgumentPack(Args.data(), Args.size(), /*CopyArgs=*/true);
3840 return TemplArg;
3841 }
3842 }
3843
3844 assert(0 && "Unhandled template argument kind!");
3845 return TemplateArgument();
3846}
3847
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003848TemplateParameterList *
Sebastian Redlc3632732010-10-05 15:59:54 +00003849ASTReader::ReadTemplateParameterList(PerFileData &F,
3850 const RecordData &Record, unsigned &Idx) {
3851 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
3852 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
3853 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003854
3855 unsigned NumParams = Record[Idx++];
3856 llvm::SmallVector<NamedDecl *, 16> Params;
3857 Params.reserve(NumParams);
3858 while (NumParams--)
3859 Params.push_back(cast<NamedDecl>(GetDecl(Record[Idx++])));
3860
3861 TemplateParameterList* TemplateParams =
3862 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
3863 Params.data(), Params.size(), RAngleLoc);
3864 return TemplateParams;
3865}
3866
3867void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003868ASTReader::
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003869ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs,
Sebastian Redlc3632732010-10-05 15:59:54 +00003870 PerFileData &F, const RecordData &Record,
3871 unsigned &Idx) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003872 unsigned NumTemplateArgs = Record[Idx++];
3873 TemplArgs.reserve(NumTemplateArgs);
3874 while (NumTemplateArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00003875 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003876}
3877
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00003878/// \brief Read a UnresolvedSet structure.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003879void ASTReader::ReadUnresolvedSet(UnresolvedSetImpl &Set,
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00003880 const RecordData &Record, unsigned &Idx) {
3881 unsigned NumDecls = Record[Idx++];
3882 while (NumDecls--) {
3883 NamedDecl *D = cast<NamedDecl>(GetDecl(Record[Idx++]));
3884 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
3885 Set.addDecl(D, AS);
3886 }
3887}
3888
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003889CXXBaseSpecifier
Sebastian Redlc3632732010-10-05 15:59:54 +00003890ASTReader::ReadCXXBaseSpecifier(PerFileData &F,
Nick Lewycky56062202010-07-26 16:56:01 +00003891 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003892 bool isVirtual = static_cast<bool>(Record[Idx++]);
3893 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
3894 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00003895 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
3896 SourceRange Range = ReadSourceRange(F, Record, Idx);
Nick Lewycky56062202010-07-26 16:56:01 +00003897 return CXXBaseSpecifier(Range, isVirtual, isBaseOfClass, AS, TInfo);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003898}
3899
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003900std::pair<CXXBaseOrMemberInitializer **, unsigned>
Sebastian Redlc3632732010-10-05 15:59:54 +00003901ASTReader::ReadCXXBaseOrMemberInitializers(PerFileData &F,
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003902 const RecordData &Record,
3903 unsigned &Idx) {
3904 CXXBaseOrMemberInitializer **BaseOrMemberInitializers = 0;
3905 unsigned NumInitializers = Record[Idx++];
3906 if (NumInitializers) {
3907 ASTContext &C = *getContext();
3908
3909 BaseOrMemberInitializers
3910 = new (C) CXXBaseOrMemberInitializer*[NumInitializers];
3911 for (unsigned i=0; i != NumInitializers; ++i) {
3912 TypeSourceInfo *BaseClassInfo = 0;
3913 bool IsBaseVirtual = false;
3914 FieldDecl *Member = 0;
3915
3916 bool IsBaseInitializer = Record[Idx++];
3917 if (IsBaseInitializer) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003918 BaseClassInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003919 IsBaseVirtual = Record[Idx++];
3920 } else {
3921 Member = cast<FieldDecl>(GetDecl(Record[Idx++]));
3922 }
Sebastian Redlc3632732010-10-05 15:59:54 +00003923 SourceLocation MemberLoc = ReadSourceLocation(F, Record, Idx);
3924 Expr *Init = ReadExpr(F);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003925 FieldDecl *AnonUnionMember
3926 = cast_or_null<FieldDecl>(GetDecl(Record[Idx++]));
Sebastian Redlc3632732010-10-05 15:59:54 +00003927 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
3928 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003929 bool IsWritten = Record[Idx++];
3930 unsigned SourceOrderOrNumArrayIndices;
3931 llvm::SmallVector<VarDecl *, 8> Indices;
3932 if (IsWritten) {
3933 SourceOrderOrNumArrayIndices = Record[Idx++];
3934 } else {
3935 SourceOrderOrNumArrayIndices = Record[Idx++];
3936 Indices.reserve(SourceOrderOrNumArrayIndices);
3937 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
3938 Indices.push_back(cast<VarDecl>(GetDecl(Record[Idx++])));
3939 }
3940
3941 CXXBaseOrMemberInitializer *BOMInit;
3942 if (IsBaseInitializer) {
3943 BOMInit = new (C) CXXBaseOrMemberInitializer(C, BaseClassInfo,
3944 IsBaseVirtual, LParenLoc,
3945 Init, RParenLoc);
3946 } else if (IsWritten) {
3947 BOMInit = new (C) CXXBaseOrMemberInitializer(C, Member, MemberLoc,
3948 LParenLoc, Init, RParenLoc);
3949 } else {
3950 BOMInit = CXXBaseOrMemberInitializer::Create(C, Member, MemberLoc,
3951 LParenLoc, Init, RParenLoc,
3952 Indices.data(),
3953 Indices.size());
3954 }
3955
Argyrios Kyrtzidisf84cde12010-09-06 19:04:27 +00003956 if (IsWritten)
3957 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003958 BOMInit->setAnonUnionMember(AnonUnionMember);
3959 BaseOrMemberInitializers[i] = BOMInit;
3960 }
3961 }
3962
3963 return std::make_pair(BaseOrMemberInitializers, NumInitializers);
3964}
3965
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003966NestedNameSpecifier *
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003967ASTReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003968 unsigned N = Record[Idx++];
3969 NestedNameSpecifier *NNS = 0, *Prev = 0;
3970 for (unsigned I = 0; I != N; ++I) {
3971 NestedNameSpecifier::SpecifierKind Kind
3972 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
3973 switch (Kind) {
3974 case NestedNameSpecifier::Identifier: {
3975 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
3976 NNS = NestedNameSpecifier::Create(*Context, Prev, II);
3977 break;
3978 }
3979
3980 case NestedNameSpecifier::Namespace: {
3981 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
3982 NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
3983 break;
3984 }
3985
3986 case NestedNameSpecifier::TypeSpec:
3987 case NestedNameSpecifier::TypeSpecWithTemplate: {
3988 Type *T = GetType(Record[Idx++]).getTypePtr();
3989 bool Template = Record[Idx++];
3990 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
3991 break;
3992 }
3993
3994 case NestedNameSpecifier::Global: {
3995 NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
3996 // No associated value, and there can't be a prefix.
3997 break;
3998 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003999 }
Argyrios Kyrtzidisd2bb2c02010-07-07 15:46:30 +00004000 Prev = NNS;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004001 }
4002 return NNS;
4003}
4004
4005SourceRange
Sebastian Redlc3632732010-10-05 15:59:54 +00004006ASTReader::ReadSourceRange(PerFileData &F, const RecordData &Record,
4007 unsigned &Idx) {
4008 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
4009 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar8ee59392010-06-02 15:47:10 +00004010 return SourceRange(beg, end);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004011}
4012
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004013/// \brief Read an integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004014llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004015 unsigned BitWidth = Record[Idx++];
4016 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
4017 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
4018 Idx += NumWords;
4019 return Result;
4020}
4021
4022/// \brief Read a signed integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004023llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004024 bool isUnsigned = Record[Idx++];
4025 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
4026}
4027
Douglas Gregor17fc2232009-04-14 21:55:33 +00004028/// \brief Read a floating-point value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004029llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00004030 return llvm::APFloat(ReadAPInt(Record, Idx));
4031}
4032
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004033// \brief Read a string
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004034std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004035 unsigned Len = Record[Idx++];
Jay Foadbeaaccd2009-05-21 09:52:38 +00004036 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004037 Idx += Len;
4038 return Result;
4039}
4040
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004041CXXTemporary *ASTReader::ReadCXXTemporary(const RecordData &Record,
Chris Lattnerd2598362010-05-10 00:25:06 +00004042 unsigned &Idx) {
4043 CXXDestructorDecl *Decl = cast<CXXDestructorDecl>(GetDecl(Record[Idx++]));
4044 return CXXTemporary::Create(*Context, Decl);
4045}
4046
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004047DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00004048 return Diag(SourceLocation(), DiagID);
4049}
4050
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004051DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00004052 return Diags.Report(FullSourceLoc(Loc, SourceMgr), DiagID);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00004053}
Douglas Gregor025452f2009-04-17 00:04:06 +00004054
Douglas Gregor668c1a42009-04-21 22:25:48 +00004055/// \brief Retrieve the identifier table associated with the
4056/// preprocessor.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004057IdentifierTable &ASTReader::getIdentifierTable() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00004058 assert(PP && "Forgot to set Preprocessor ?");
4059 return PP->getIdentifierTable();
Douglas Gregor668c1a42009-04-21 22:25:48 +00004060}
4061
Douglas Gregor025452f2009-04-17 00:04:06 +00004062/// \brief Record that the given ID maps to the given switch-case
4063/// statement.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004064void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Douglas Gregor025452f2009-04-17 00:04:06 +00004065 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
4066 SwitchCaseStmts[ID] = SC;
4067}
4068
4069/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004070SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Douglas Gregor025452f2009-04-17 00:04:06 +00004071 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
4072 return SwitchCaseStmts[ID];
4073}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00004074
4075/// \brief Record that the given label statement has been
4076/// deserialized and has the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004077void ASTReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
Mike Stump1eb44332009-09-09 15:08:12 +00004078 assert(LabelStmts.find(ID) == LabelStmts.end() &&
Douglas Gregor1de05fe2009-04-17 18:18:49 +00004079 "Deserialized label twice");
4080 LabelStmts[ID] = S;
4081
4082 // If we've already seen any goto statements that point to this
4083 // label, resolve them now.
4084 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
4085 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
4086 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
4087 Goto->second->setLabel(S);
4088 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00004089
4090 // If we've already seen any address-label statements that point to
4091 // this label, resolve them now.
4092 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
Mike Stump1eb44332009-09-09 15:08:12 +00004093 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00004094 = UnresolvedAddrLabelExprs.equal_range(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00004095 for (AddrLabelIter AddrLabel = AddrLabels.first;
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00004096 AddrLabel != AddrLabels.second; ++AddrLabel)
4097 AddrLabel->second->setLabel(S);
4098 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor1de05fe2009-04-17 18:18:49 +00004099}
4100
4101/// \brief Set the label of the given statement to the label
4102/// identified by ID.
4103///
4104/// Depending on the order in which the label and other statements
4105/// referencing that label occur, this operation may complete
4106/// immediately (updating the statement) or it may queue the
4107/// statement to be back-patched later.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004108void ASTReader::SetLabelOf(GotoStmt *S, unsigned ID) {
Douglas Gregor1de05fe2009-04-17 18:18:49 +00004109 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
4110 if (Label != LabelStmts.end()) {
4111 // We've already seen this label, so set the label of the goto and
4112 // we're done.
4113 S->setLabel(Label->second);
4114 } else {
4115 // We haven't seen this label yet, so add this goto to the set of
4116 // unresolved goto statements.
4117 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
4118 }
4119}
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00004120
4121/// \brief Set the label of the given expression to the label
4122/// identified by ID.
4123///
4124/// Depending on the order in which the label and other statements
4125/// referencing that label occur, this operation may complete
4126/// immediately (updating the statement) or it may queue the
4127/// statement to be back-patched later.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004128void ASTReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00004129 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
4130 if (Label != LabelStmts.end()) {
4131 // We've already seen this label, so set the label of the
4132 // label-address expression and we're done.
4133 S->setLabel(Label->second);
4134 } else {
4135 // We haven't seen this label yet, so add this label-address
4136 // expression to the set of unresolved label-address expressions.
4137 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
4138 }
4139}
Douglas Gregord89275b2009-07-06 18:54:52 +00004140
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004141void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004142 assert(NumCurrentElementsDeserializing &&
4143 "FinishedDeserializing not paired with StartedDeserializing");
4144 if (NumCurrentElementsDeserializing == 1) {
Douglas Gregord89275b2009-07-06 18:54:52 +00004145 // If any identifiers with corresponding top-level declarations have
4146 // been loaded, load those declarations now.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004147 while (!PendingIdentifierInfos.empty()) {
4148 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
4149 PendingIdentifierInfos.front().DeclIDs, true);
4150 PendingIdentifierInfos.pop_front();
Douglas Gregord89275b2009-07-06 18:54:52 +00004151 }
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00004152
4153 // We are not in recursive loading, so it's safe to pass the "interesting"
4154 // decls to the consumer.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004155 if (Consumer)
4156 PassInterestingDeclsToConsumer();
Douglas Gregord89275b2009-07-06 18:54:52 +00004157 }
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004158 --NumCurrentElementsDeserializing;
Douglas Gregord89275b2009-07-06 18:54:52 +00004159}
Douglas Gregor501c1032010-08-19 00:28:17 +00004160
Sebastian Redle1dde812010-08-24 00:50:04 +00004161ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
4162 const char *isysroot, bool DisableValidation)
4163 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
4164 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
4165 Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
4166 Consumer(0), isysroot(isysroot), DisableValidation(DisableValidation),
4167 NumStatHits(0), NumStatMisses(0), NumSLocEntriesRead(0),
Sebastian Redl8db9fae2010-09-22 20:19:08 +00004168 TotalNumSLocEntries(0), NextSLocOffset(0), NumStatementsRead(0),
4169 TotalNumStatements(0), NumMacrosRead(0), TotalNumMacros(0),
4170 NumSelectorsRead(0), NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
Sebastian Redle1dde812010-08-24 00:50:04 +00004171 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
4172 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
4173 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
4174 RelocatablePCH = false;
4175}
4176
4177ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
4178 Diagnostic &Diags, const char *isysroot,
4179 bool DisableValidation)
4180 : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
4181 Diags(Diags), SemaObj(0), PP(0), Context(0), Consumer(0),
4182 isysroot(isysroot), DisableValidation(DisableValidation), NumStatHits(0),
4183 NumStatMisses(0), NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Sebastian Redl8db9fae2010-09-22 20:19:08 +00004184 NextSLocOffset(0), NumStatementsRead(0), TotalNumStatements(0),
4185 NumMacrosRead(0), TotalNumMacros(0), NumSelectorsRead(0),
4186 NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
4187 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
4188 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
4189 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
Sebastian Redle1dde812010-08-24 00:50:04 +00004190 RelocatablePCH = false;
4191}
4192
4193ASTReader::~ASTReader() {
4194 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
4195 delete Chain[e - i - 1];
4196 // Delete all visible decl lookup tables
4197 for (DeclContextOffsetsMap::iterator I = DeclContextOffsets.begin(),
4198 E = DeclContextOffsets.end();
4199 I != E; ++I) {
4200 for (DeclContextInfos::iterator J = I->second.begin(), F = I->second.end();
4201 J != F; ++J) {
4202 if (J->NameLookupTableData)
4203 delete static_cast<ASTDeclContextNameLookupTable*>(
4204 J->NameLookupTableData);
4205 }
4206 }
4207 for (DeclContextVisibleUpdatesPending::iterator
4208 I = PendingVisibleUpdates.begin(),
4209 E = PendingVisibleUpdates.end();
4210 I != E; ++I) {
4211 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
4212 F = I->second.end();
4213 J != F; ++J)
4214 delete static_cast<ASTDeclContextNameLookupTable*>(*J);
4215 }
4216}
4217
Douglas Gregor501c1032010-08-19 00:28:17 +00004218ASTReader::PerFileData::PerFileData()
Sebastian Redl8db9fae2010-09-22 20:19:08 +00004219 : SizeInBits(0), LocalNumSLocEntries(0), SLocOffsets(0), LocalSLocSize(0),
Sebastian Redl301c9b02010-09-22 00:42:27 +00004220 LocalNumIdentifiers(0), IdentifierOffsets(0), IdentifierTableData(0),
4221 IdentifierLookupTable(0), LocalNumMacroDefinitions(0),
4222 MacroDefinitionOffsets(0), LocalNumSelectors(0), SelectorOffsets(0),
4223 SelectorLookupTableData(0), SelectorLookupTable(0), LocalNumDecls(0),
4224 DeclOffsets(0), LocalNumTypes(0), TypeOffsets(0), StatCache(0),
Sebastian Redla866e652010-10-01 19:59:12 +00004225 NumPreallocatedPreprocessingEntities(0), NextInSource(0)
Douglas Gregor501c1032010-08-19 00:28:17 +00004226{}
4227
4228ASTReader::PerFileData::~PerFileData() {
4229 delete static_cast<ASTIdentifierLookupTable *>(IdentifierLookupTable);
4230 delete static_cast<ASTSelectorLookupTable *>(SelectorLookupTable);
4231}
4232