blob: fb26851e3a3e59f2e716bbd023a5e7c51f9d6a7a [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"
Douglas Gregor0a0428e2009-04-10 20:39:37 +000016#include "clang/Frontend/FrontendDiagnostic.h"
Daniel Dunbarc7162932009-11-11 23:58:53 +000017#include "clang/Frontend/Utils.h"
Douglas Gregore737f502010-08-12 20:07:10 +000018#include "clang/Sema/Sema.h"
Douglas Gregorfdd01722009-04-14 00:24:19 +000019#include "clang/AST/ASTConsumer.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000020#include "clang/AST/ASTContext.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000021#include "clang/AST/Expr.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000022#include "clang/AST/Type.h"
John McCalla1ee0c52009-10-16 21:56:05 +000023#include "clang/AST/TypeLocVisitor.h"
Chris Lattner42d42b52009-04-10 21:41:48 +000024#include "clang/Lex/MacroInfo.h"
Douglas Gregor6a5a23f2010-03-19 21:51:54 +000025#include "clang/Lex/PreprocessingRecord.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000026#include "clang/Lex/Preprocessor.h"
Steve Naroff83d63c72009-04-24 20:03:17 +000027#include "clang/Lex/HeaderSearch.h"
Douglas Gregor668c1a42009-04-21 22:25:48 +000028#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000029#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000030#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000031#include "clang/Basic/FileManager.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000032#include "clang/Basic/TargetInfo.h"
Douglas Gregor445e23e2009-10-05 21:07:28 +000033#include "clang/Basic/Version.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000034#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000035#include "llvm/Bitcode/BitstreamReader.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000036#include "llvm/Support/MemoryBuffer.h"
John McCall833ca992009-10-29 08:12:44 +000037#include "llvm/Support/ErrorHandling.h"
Daniel Dunbard5b21972009-11-18 19:50:41 +000038#include "llvm/System/Path.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000039#include <algorithm>
Douglas Gregore721f952009-04-28 18:58:38 +000040#include <iterator>
Douglas Gregor2cf26342009-04-09 22:27:44 +000041#include <cstdio>
Douglas Gregor4fed3f42009-04-27 18:38:38 +000042#include <sys/stat.h>
Douglas Gregor2cf26342009-04-09 22:27:44 +000043using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000044using namespace clang::serialization;
Douglas Gregor2cf26342009-04-09 22:27:44 +000045
46//===----------------------------------------------------------------------===//
Sebastian Redl3c7f4132010-08-18 23:57:06 +000047// PCH validator implementation
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000048//===----------------------------------------------------------------------===//
49
Sebastian Redl571db7f2010-08-18 23:56:56 +000050ASTReaderListener::~ASTReaderListener() {}
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000051
52bool
53PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts) {
54 const LangOptions &PPLangOpts = PP.getLangOptions();
55#define PARSE_LANGOPT_BENIGN(Option)
56#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
57 if (PPLangOpts.Option != LangOpts.Option) { \
58 Reader.Diag(DiagID) << LangOpts.Option << PPLangOpts.Option; \
59 return true; \
60 }
61
62 PARSE_LANGOPT_BENIGN(Trigraphs);
63 PARSE_LANGOPT_BENIGN(BCPLComment);
64 PARSE_LANGOPT_BENIGN(DollarIdents);
65 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
66 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
Chandler Carrutheb5d7b72010-04-17 20:17:31 +000067 PARSE_LANGOPT_IMPORTANT(GNUKeywords, diag::warn_pch_gnu_keywords);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000068 PARSE_LANGOPT_BENIGN(ImplicitInt);
69 PARSE_LANGOPT_BENIGN(Digraphs);
70 PARSE_LANGOPT_BENIGN(HexFloats);
71 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
72 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
73 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
74 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
75 PARSE_LANGOPT_BENIGN(CXXOperatorName);
76 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
77 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
78 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
Fariborz Jahanian412e7982010-02-09 19:31:38 +000079 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI2, diag::warn_pch_nonfragile_abi2);
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +000080 PARSE_LANGOPT_IMPORTANT(NoConstantCFStrings,
81 diag::warn_pch_no_constant_cfstrings);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000082 PARSE_LANGOPT_BENIGN(PascalStrings);
83 PARSE_LANGOPT_BENIGN(WritableStrings);
Mike Stump1eb44332009-09-09 15:08:12 +000084 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000085 diag::warn_pch_lax_vector_conversions);
Nate Begeman69cfb9b2009-06-25 22:57:40 +000086 PARSE_LANGOPT_IMPORTANT(AltiVec, diag::warn_pch_altivec);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000087 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
Daniel Dunbar73482882010-02-10 18:48:44 +000088 PARSE_LANGOPT_IMPORTANT(SjLjExceptions, diag::warn_pch_sjlj_exceptions);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000089 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
90 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
91 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
Mike Stump1eb44332009-09-09 15:08:12 +000092 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000093 diag::warn_pch_thread_safe_statics);
Daniel Dunbar5345c392009-09-03 04:54:28 +000094 PARSE_LANGOPT_IMPORTANT(POSIXThreads, diag::warn_pch_posix_threads);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000095 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
96 PARSE_LANGOPT_BENIGN(EmitAllDecls);
97 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
Chris Lattnera4d71452010-06-26 21:25:03 +000098 PARSE_LANGOPT_BENIGN(getSignedOverflowBehavior());
Mike Stump1eb44332009-09-09 15:08:12 +000099 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000100 diag::warn_pch_heinous_extensions);
101 // FIXME: Most of the options below are benign if the macro wasn't
102 // used. Unfortunately, this means that a PCH compiled without
103 // optimization can't be used with optimization turned on, even
104 // though the only thing that changes is whether __OPTIMIZE__ was
105 // defined... but if __OPTIMIZE__ never showed up in the header, it
106 // doesn't matter. We could consider making this some special kind
107 // of check.
108 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
109 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
110 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
111 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
112 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
113 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
114 PARSE_LANGOPT_IMPORTANT(AccessControl, diag::warn_pch_access_control);
115 PARSE_LANGOPT_IMPORTANT(CharIsSigned, diag::warn_pch_char_signed);
John Thompsona6fda122009-11-05 20:14:16 +0000116 PARSE_LANGOPT_IMPORTANT(ShortWChar, diag::warn_pch_short_wchar);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000117 if ((PPLangOpts.getGCMode() != 0) != (LangOpts.getGCMode() != 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000118 Reader.Diag(diag::warn_pch_gc_mode)
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000119 << LangOpts.getGCMode() << PPLangOpts.getGCMode();
120 return true;
121 }
122 PARSE_LANGOPT_BENIGN(getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000123 PARSE_LANGOPT_IMPORTANT(getStackProtectorMode(),
124 diag::warn_pch_stack_protector);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000125 PARSE_LANGOPT_BENIGN(InstantiationDepth);
Nate Begeman69cfb9b2009-06-25 22:57:40 +0000126 PARSE_LANGOPT_IMPORTANT(OpenCL, diag::warn_pch_opencl);
Mike Stump9c276ae2009-12-12 01:27:46 +0000127 PARSE_LANGOPT_BENIGN(CatchUndefined);
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000128 PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors);
Douglas Gregora0068fc2010-07-09 17:35:33 +0000129 PARSE_LANGOPT_BENIGN(SpellChecking);
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +0000130#undef PARSE_LANGOPT_IMPORTANT
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000131#undef PARSE_LANGOPT_BENIGN
132
133 return false;
134}
135
Daniel Dunbardc3c0d22009-11-11 00:52:11 +0000136bool PCHValidator::ReadTargetTriple(llvm::StringRef Triple) {
137 if (Triple == PP.getTargetInfo().getTriple().str())
138 return false;
139
140 Reader.Diag(diag::warn_pch_target_triple)
141 << Triple << PP.getTargetInfo().getTriple().str();
142 return true;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000143}
144
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000145struct EmptyStringRef {
Benjamin Kramerec1b1cc2010-07-14 23:19:41 +0000146 bool operator ()(llvm::StringRef r) const { return r.empty(); }
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000147};
148struct EmptyBlock {
149 bool operator ()(const PCHPredefinesBlock &r) const { return r.Data.empty(); }
150};
151
152static bool EqualConcatenations(llvm::SmallVector<llvm::StringRef, 2> L,
153 PCHPredefinesBlocks R) {
154 // First, sum up the lengths.
155 unsigned LL = 0, RL = 0;
156 for (unsigned I = 0, N = L.size(); I != N; ++I) {
157 LL += L[I].size();
158 }
159 for (unsigned I = 0, N = R.size(); I != N; ++I) {
160 RL += R[I].Data.size();
161 }
162 if (LL != RL)
163 return false;
164 if (LL == 0 && RL == 0)
165 return true;
166
167 // Kick out empty parts, they confuse the algorithm below.
168 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
169 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
170
171 // Do it the hard way. At this point, both vectors must be non-empty.
172 llvm::StringRef LR = L[0], RR = R[0].Data;
173 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
Daniel Dunbarc76c9e02010-07-16 00:00:11 +0000174 (void) RN;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000175 for (;;) {
176 // Compare the current pieces.
177 if (LR.size() == RR.size()) {
178 // If they're the same length, it's pretty easy.
179 if (LR != RR)
180 return false;
181 // Both pieces are done, advance.
182 ++LI;
183 ++RI;
184 // If either string is done, they're both done, since they're the same
185 // length.
186 if (LI == LN) {
187 assert(RI == RN && "Strings not the same length after all?");
188 return true;
189 }
190 LR = L[LI];
191 RR = R[RI].Data;
192 } else if (LR.size() < RR.size()) {
193 // Right piece is longer.
194 if (!RR.startswith(LR))
195 return false;
196 ++LI;
197 assert(LI != LN && "Strings not the same length after all?");
198 RR = RR.substr(LR.size());
199 LR = L[LI];
200 } else {
201 // Left piece is longer.
202 if (!LR.startswith(RR))
203 return false;
204 ++RI;
205 assert(RI != RN && "Strings not the same length after all?");
206 LR = LR.substr(RR.size());
207 RR = R[RI].Data;
208 }
209 }
210}
211
212static std::pair<FileID, llvm::StringRef::size_type>
213FindMacro(const PCHPredefinesBlocks &Buffers, llvm::StringRef MacroDef) {
214 std::pair<FileID, llvm::StringRef::size_type> Res;
215 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
216 Res.second = Buffers[I].Data.find(MacroDef);
217 if (Res.second != llvm::StringRef::npos) {
218 Res.first = Buffers[I].BufferID;
219 break;
220 }
221 }
222 return Res;
223}
224
225bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000226 llvm::StringRef OriginalFileName,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000227 std::string &SuggestedPredefines) {
Daniel Dunbarc7162932009-11-11 23:58:53 +0000228 // We are in the context of an implicit include, so the predefines buffer will
229 // have a #include entry for the PCH file itself (as normalized by the
230 // preprocessor initialization). Find it and skip over it in the checking
231 // below.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000232 llvm::SmallString<256> PCHInclude;
233 PCHInclude += "#include \"";
Daniel Dunbarc7162932009-11-11 23:58:53 +0000234 PCHInclude += NormalizeDashIncludePath(OriginalFileName);
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000235 PCHInclude += "\"\n";
236 std::pair<llvm::StringRef,llvm::StringRef> Split =
237 llvm::StringRef(PP.getPredefines()).split(PCHInclude.str());
238 llvm::StringRef Left = Split.first, Right = Split.second;
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000239 if (Left == PP.getPredefines()) {
240 Error("Missing PCH include entry!");
241 return true;
242 }
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000243
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000244 // If the concatenation of all the PCH buffers is equal to the adjusted
245 // command line, we're done.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000246 llvm::SmallVector<llvm::StringRef, 2> CommandLine;
247 CommandLine.push_back(Left);
248 CommandLine.push_back(Right);
249 if (EqualConcatenations(CommandLine, Buffers))
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000250 return false;
251
252 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000253
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000254 // The predefines buffers are different. Determine what the differences are,
255 // and whether they require us to reject the PCH file.
Daniel Dunbare6750492009-11-13 16:46:11 +0000256 llvm::SmallVector<llvm::StringRef, 8> PCHLines;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000257 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
258 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Daniel Dunbare6750492009-11-13 16:46:11 +0000259
260 llvm::SmallVector<llvm::StringRef, 8> CmdLineLines;
261 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
262 Right.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000263
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000264 // Sort both sets of predefined buffer lines, since we allow some extra
265 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000266 std::sort(CmdLineLines.begin(), CmdLineLines.end());
267 std::sort(PCHLines.begin(), PCHLines.end());
268
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000269 // Determine which predefines that were used to build the PCH file are missing
270 // from the command line.
271 std::vector<llvm::StringRef> MissingPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000272 std::set_difference(PCHLines.begin(), PCHLines.end(),
273 CmdLineLines.begin(), CmdLineLines.end(),
274 std::back_inserter(MissingPredefines));
275
276 bool MissingDefines = false;
277 bool ConflictingDefines = false;
278 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000279 llvm::StringRef Missing = MissingPredefines[I];
280 if (!Missing.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000281 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
282 return true;
283 }
Mike Stump1eb44332009-09-09 15:08:12 +0000284
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000285 // This is a macro definition. Determine the name of the macro we're
286 // defining.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000287 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000288 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000289 = Missing.find_first_of("( \n\r", StartOfMacroName);
290 assert(EndOfMacroName != std::string::npos &&
291 "Couldn't find the end of the macro name");
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000292 llvm::StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000293
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000294 // Determine whether this macro was given a different definition on the
295 // command line.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000296 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000297 std::string::size_type MacroDefLen = MacroDefStart.size();
Daniel Dunbare6750492009-11-13 16:46:11 +0000298 llvm::SmallVector<llvm::StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000299 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
300 MacroDefStart);
301 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000302 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000303 // Different macro; we're done.
304 ConflictPos = CmdLineLines.end();
Mike Stump1eb44332009-09-09 15:08:12 +0000305 break;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000306 }
Mike Stump1eb44332009-09-09 15:08:12 +0000307
308 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000309 "Invalid #define in predefines buffer?");
Mike Stump1eb44332009-09-09 15:08:12 +0000310 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000311 (*ConflictPos)[MacroDefLen] != '(')
312 continue; // Longer macro name; keep trying.
Mike Stump1eb44332009-09-09 15:08:12 +0000313
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000314 // We found a conflicting macro definition.
315 break;
316 }
Mike Stump1eb44332009-09-09 15:08:12 +0000317
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000318 if (ConflictPos != CmdLineLines.end()) {
319 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
320 << MacroName;
321
322 // Show the definition of this macro within the PCH file.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000323 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
324 FindMacro(Buffers, Missing);
325 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
326 SourceLocation PCHMissingLoc =
327 SourceMgr.getLocForStartOfFile(MacroLoc.first)
328 .getFileLocWithOffset(MacroLoc.second);
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000329 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000330
331 ConflictingDefines = true;
332 continue;
333 }
Mike Stump1eb44332009-09-09 15:08:12 +0000334
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000335 // If the macro doesn't conflict, then we'll just pick up the macro
336 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000337 if (ConflictingDefines)
338 continue; // Don't complain if there are already conflicting defs
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000340 if (!MissingDefines) {
341 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
342 MissingDefines = true;
343 }
344
345 // Show the definition of this macro within the PCH file.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000346 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
347 FindMacro(Buffers, Missing);
348 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
349 SourceLocation PCHMissingLoc =
350 SourceMgr.getLocForStartOfFile(MacroLoc.first)
351 .getFileLocWithOffset(MacroLoc.second);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000352 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
353 }
Mike Stump1eb44332009-09-09 15:08:12 +0000354
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000355 if (ConflictingDefines)
356 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000357
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000358 // Determine what predefines were introduced based on command-line
359 // parameters that were not present when building the PCH
360 // file. Extra #defines are okay, so long as the identifiers being
361 // defined were not used within the precompiled header.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000362 std::vector<llvm::StringRef> ExtraPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000363 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
364 PCHLines.begin(), PCHLines.end(),
Mike Stump1eb44332009-09-09 15:08:12 +0000365 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000366 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000367 llvm::StringRef &Extra = ExtraPredefines[I];
368 if (!Extra.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000369 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
370 return true;
371 }
372
373 // This is an extra macro definition. Determine the name of the
374 // macro we're defining.
375 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000376 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000377 = Extra.find_first_of("( \n\r", StartOfMacroName);
378 assert(EndOfMacroName != std::string::npos &&
379 "Couldn't find the end of the macro name");
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000380 llvm::StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000381
382 // Check whether this name was used somewhere in the PCH file. If
383 // so, defining it as a macro could change behavior, so we reject
384 // the PCH file.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000385 if (IdentifierInfo *II = Reader.get(MacroName)) {
Daniel Dunbar4fda42e2009-11-11 00:52:00 +0000386 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000387 return true;
388 }
389
390 // Add this definition to the suggested predefines buffer.
391 SuggestedPredefines += Extra;
392 SuggestedPredefines += '\n';
393 }
394
395 // If we get here, it's because the predefines buffer had compatible
396 // contents. Accept the PCH file.
397 return false;
398}
399
Douglas Gregor12fab312010-03-16 16:35:32 +0000400void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
401 unsigned ID) {
402 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
403 ++NumHeaderInfos;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000404}
405
406void PCHValidator::ReadCounter(unsigned Value) {
407 PP.setCounterValue(Value);
408}
409
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000410//===----------------------------------------------------------------------===//
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000411// AST reader implementation
Douglas Gregor668c1a42009-04-21 22:25:48 +0000412//===----------------------------------------------------------------------===//
413
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000414ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
Douglas Gregorfae3b2f2010-07-27 00:27:13 +0000415 const char *isysroot, bool DisableValidation)
Sebastian Redl30c514c2010-07-14 23:45:08 +0000416 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
417 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
418 Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
Sebastian Redl725cd962010-08-04 20:40:17 +0000419 Consumer(0), isysroot(isysroot), DisableValidation(DisableValidation),
420 NumStatHits(0), NumStatMisses(0), NumSLocEntriesRead(0),
421 TotalNumSLocEntries(0), NumStatementsRead(0), TotalNumStatements(0),
Sebastian Redlfa78dec2010-08-04 21:22:45 +0000422 NumMacrosRead(0), TotalNumMacros(0), NumSelectorsRead(0),
423 NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
424 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
Sebastian Redl725cd962010-08-04 20:40:17 +0000425 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
426 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000427 RelocatablePCH = false;
428}
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000429
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000430ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
Douglas Gregorfae3b2f2010-07-27 00:27:13 +0000431 Diagnostic &Diags, const char *isysroot,
432 bool DisableValidation)
Sebastian Redl30c514c2010-07-14 23:45:08 +0000433 : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
Sebastian Redl9137a522010-07-16 17:50:48 +0000434 Diags(Diags), SemaObj(0), PP(0), Context(0), Consumer(0),
Sebastian Redl725cd962010-08-04 20:40:17 +0000435 isysroot(isysroot), DisableValidation(DisableValidation), NumStatHits(0),
436 NumStatMisses(0), NumSLocEntriesRead(0), TotalNumSLocEntries(0),
437 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
Sebastian Redlfa78dec2010-08-04 21:22:45 +0000438 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
439 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
Sebastian Redl725cd962010-08-04 20:40:17 +0000440 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
441 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
442 NumCurrentElementsDeserializing(0) {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000443 RelocatablePCH = false;
444}
Chris Lattner4c6f9522009-04-27 05:14:47 +0000445
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000446ASTReader::~ASTReader() {
Sebastian Redl9137a522010-07-16 17:50:48 +0000447 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
448 delete Chain[e - i - 1];
449}
450
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000451void
Sebastian Redl571db7f2010-08-18 23:56:56 +0000452ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000453 DeserializationListener = Listener;
454 if (DeserializationListener)
455 DeserializationListener->SetReader(this);
456}
457
Chris Lattner4c6f9522009-04-27 05:14:47 +0000458
Douglas Gregor668c1a42009-04-21 22:25:48 +0000459namespace {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000460class ASTSelectorLookupTrait {
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000461 ASTReader &Reader;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000462
463public:
Sebastian Redl5d050072010-08-04 17:20:04 +0000464 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000465 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +0000466 ObjCMethodList Instance, Factory;
467 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000468
469 typedef Selector external_key_type;
470 typedef external_key_type internal_key_type;
471
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000472 explicit ASTSelectorLookupTrait(ASTReader &Reader) : Reader(Reader) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000473
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000474 static bool EqualKey(const internal_key_type& a,
475 const internal_key_type& b) {
476 return a == b;
477 }
Mike Stump1eb44332009-09-09 15:08:12 +0000478
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000479 static unsigned ComputeHash(Selector Sel) {
480 unsigned N = Sel.getNumArgs();
481 if (N == 0)
482 ++N;
483 unsigned R = 5381;
484 for (unsigned I = 0; I != N; ++I)
485 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
Daniel Dunbar2596e422009-10-17 23:52:28 +0000486 R = llvm::HashString(II->getName(), R);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000487 return R;
488 }
Mike Stump1eb44332009-09-09 15:08:12 +0000489
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000490 // This hopefully will just get inlined and removed by the optimizer.
491 static const internal_key_type&
492 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000494 static std::pair<unsigned, unsigned>
495 ReadKeyDataLength(const unsigned char*& d) {
496 using namespace clang::io;
497 unsigned KeyLen = ReadUnalignedLE16(d);
498 unsigned DataLen = ReadUnalignedLE16(d);
499 return std::make_pair(KeyLen, DataLen);
500 }
Mike Stump1eb44332009-09-09 15:08:12 +0000501
Douglas Gregor83941df2009-04-25 17:48:32 +0000502 internal_key_type ReadKey(const unsigned char* d, unsigned) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000503 using namespace clang::io;
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000504 SelectorTable &SelTable = Reader.getContext()->Selectors;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000505 unsigned N = ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +0000506 IdentifierInfo *FirstII
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000507 = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
508 if (N == 0)
509 return SelTable.getNullarySelector(FirstII);
510 else if (N == 1)
511 return SelTable.getUnarySelector(FirstII);
512
513 llvm::SmallVector<IdentifierInfo *, 16> Args;
514 Args.push_back(FirstII);
515 for (unsigned I = 1; I != N; ++I)
516 Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)));
517
Douglas Gregor75fdb232009-05-22 22:45:36 +0000518 return SelTable.getSelector(N, Args.data());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000519 }
Mike Stump1eb44332009-09-09 15:08:12 +0000520
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000521 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
522 using namespace clang::io;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000523
524 data_type Result;
525
Sebastian Redl5d050072010-08-04 17:20:04 +0000526 Result.ID = ReadUnalignedLE32(d);
527 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
528 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
529
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000530 // Load instance methods
531 ObjCMethodList *Prev = 0;
532 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000533 ObjCMethodDecl *Method
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000534 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl5d050072010-08-04 17:20:04 +0000535 if (!Result.Instance.Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000536 // This is the first method, which is the easy case.
Sebastian Redl5d050072010-08-04 17:20:04 +0000537 Result.Instance.Method = Method;
538 Prev = &Result.Instance;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000539 continue;
540 }
541
Ted Kremenek298ed872010-02-11 00:53:01 +0000542 ObjCMethodList *Mem =
543 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
544 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000545 Prev = Prev->Next;
546 }
547
548 // Load factory methods
549 Prev = 0;
550 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000551 ObjCMethodDecl *Method
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000552 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl5d050072010-08-04 17:20:04 +0000553 if (!Result.Factory.Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000554 // This is the first method, which is the easy case.
Sebastian Redl5d050072010-08-04 17:20:04 +0000555 Result.Factory.Method = Method;
556 Prev = &Result.Factory;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000557 continue;
558 }
559
Ted Kremenek298ed872010-02-11 00:53:01 +0000560 ObjCMethodList *Mem =
561 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
562 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000563 Prev = Prev->Next;
564 }
565
566 return Result;
567 }
568};
Mike Stump1eb44332009-09-09 15:08:12 +0000569
570} // end anonymous namespace
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000571
572/// \brief The on-disk hash table used for the global method pool.
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000573typedef OnDiskChainedHashTable<ASTSelectorLookupTrait>
574 ASTSelectorLookupTable;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000575
576namespace {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000577class ASTIdentifierLookupTrait {
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000578 ASTReader &Reader;
Sebastian Redld27d3fc2010-07-21 22:31:37 +0000579 llvm::BitstreamCursor &Stream;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000580
581 // If we know the IdentifierInfo in advance, it is here and we will
582 // not build a new one. Used when deserializing information about an
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000583 // identifier that was constructed before the AST file was read.
Douglas Gregor668c1a42009-04-21 22:25:48 +0000584 IdentifierInfo *KnownII;
585
586public:
587 typedef IdentifierInfo * data_type;
588
589 typedef const std::pair<const char*, unsigned> external_key_type;
590
591 typedef external_key_type internal_key_type;
592
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000593 ASTIdentifierLookupTrait(ASTReader &Reader, llvm::BitstreamCursor &Stream,
Sebastian Redld27d3fc2010-07-21 22:31:37 +0000594 IdentifierInfo *II = 0)
595 : Reader(Reader), Stream(Stream), KnownII(II) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000596
Douglas Gregor668c1a42009-04-21 22:25:48 +0000597 static bool EqualKey(const internal_key_type& a,
598 const internal_key_type& b) {
599 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
600 : false;
601 }
Mike Stump1eb44332009-09-09 15:08:12 +0000602
Douglas Gregor668c1a42009-04-21 22:25:48 +0000603 static unsigned ComputeHash(const internal_key_type& a) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000604 return llvm::HashString(llvm::StringRef(a.first, a.second));
Douglas Gregor668c1a42009-04-21 22:25:48 +0000605 }
Mike Stump1eb44332009-09-09 15:08:12 +0000606
Douglas Gregor668c1a42009-04-21 22:25:48 +0000607 // This hopefully will just get inlined and removed by the optimizer.
608 static const internal_key_type&
609 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump1eb44332009-09-09 15:08:12 +0000610
Douglas Gregor668c1a42009-04-21 22:25:48 +0000611 static std::pair<unsigned, unsigned>
612 ReadKeyDataLength(const unsigned char*& d) {
613 using namespace clang::io;
Douglas Gregor5f8e3302009-04-25 20:26:24 +0000614 unsigned DataLen = ReadUnalignedLE16(d);
Douglas Gregord6595a42009-04-25 21:04:17 +0000615 unsigned KeyLen = ReadUnalignedLE16(d);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000616 return std::make_pair(KeyLen, DataLen);
617 }
Mike Stump1eb44332009-09-09 15:08:12 +0000618
Douglas Gregor668c1a42009-04-21 22:25:48 +0000619 static std::pair<const char*, unsigned>
620 ReadKey(const unsigned char* d, unsigned n) {
621 assert(n >= 2 && d[n-1] == '\0');
622 return std::make_pair((const char*) d, n-1);
623 }
Mike Stump1eb44332009-09-09 15:08:12 +0000624
625 IdentifierInfo *ReadData(const internal_key_type& k,
Douglas Gregor668c1a42009-04-21 22:25:48 +0000626 const unsigned char* d,
627 unsigned DataLen) {
628 using namespace clang::io;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000629 IdentID ID = ReadUnalignedLE32(d);
Douglas Gregora92193e2009-04-28 21:18:29 +0000630 bool IsInteresting = ID & 0x01;
631
632 // Wipe out the "is interesting" bit.
633 ID = ID >> 1;
634
635 if (!IsInteresting) {
Sebastian Redl083abdf2010-07-27 23:01:28 +0000636 // For uninteresting identifiers, just build the IdentifierInfo
Douglas Gregora92193e2009-04-28 21:18:29 +0000637 // and associate it with the persistent ID.
638 IdentifierInfo *II = KnownII;
639 if (!II)
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000640 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregora92193e2009-04-28 21:18:29 +0000641 Reader.SetIdentifierInfo(ID, II);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000642 II->setIsFromAST();
Douglas Gregora92193e2009-04-28 21:18:29 +0000643 return II;
644 }
645
Douglas Gregor5998da52009-04-28 21:32:13 +0000646 unsigned Bits = ReadUnalignedLE16(d);
Douglas Gregor2deaea32009-04-22 18:49:13 +0000647 bool CPlusPlusOperatorKeyword = Bits & 0x01;
648 Bits >>= 1;
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000649 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
650 Bits >>= 1;
Douglas Gregor2deaea32009-04-22 18:49:13 +0000651 bool Poisoned = Bits & 0x01;
652 Bits >>= 1;
653 bool ExtensionToken = Bits & 0x01;
654 Bits >>= 1;
655 bool hasMacroDefinition = Bits & 0x01;
656 Bits >>= 1;
657 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
658 Bits >>= 10;
Mike Stump1eb44332009-09-09 15:08:12 +0000659
Douglas Gregor2deaea32009-04-22 18:49:13 +0000660 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregor5998da52009-04-28 21:32:13 +0000661 DataLen -= 6;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000662
663 // Build the IdentifierInfo itself and link the identifier ID with
664 // the new IdentifierInfo.
665 IdentifierInfo *II = KnownII;
666 if (!II)
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000667 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000668 Reader.SetIdentifierInfo(ID, II);
669
Douglas Gregor2deaea32009-04-22 18:49:13 +0000670 // Set or check the various bits in the IdentifierInfo structure.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000671 // Token IDs are read-only.
672 if (HasRevertedTokenIDToIdentifier)
673 II->RevertTokenIDToIdentifier();
Douglas Gregor2deaea32009-04-22 18:49:13 +0000674 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
Mike Stump1eb44332009-09-09 15:08:12 +0000675 assert(II->isExtensionToken() == ExtensionToken &&
Douglas Gregor2deaea32009-04-22 18:49:13 +0000676 "Incorrect extension token flag");
677 (void)ExtensionToken;
678 II->setIsPoisoned(Poisoned);
679 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
680 "Incorrect C++ operator keyword flag");
681 (void)CPlusPlusOperatorKeyword;
682
Douglas Gregor37e26842009-04-21 23:56:24 +0000683 // If this identifier is a macro, deserialize the macro
684 // definition.
685 if (hasMacroDefinition) {
Douglas Gregor5998da52009-04-28 21:32:13 +0000686 uint32_t Offset = ReadUnalignedLE32(d);
Sebastian Redld27d3fc2010-07-21 22:31:37 +0000687 Reader.ReadMacroRecord(Stream, Offset);
Douglas Gregor5998da52009-04-28 21:32:13 +0000688 DataLen -= 4;
Douglas Gregor37e26842009-04-21 23:56:24 +0000689 }
Douglas Gregor668c1a42009-04-21 22:25:48 +0000690
691 // Read all of the declarations visible at global scope with this
692 // name.
Chris Lattner6bf690f2009-04-27 22:17:41 +0000693 if (Reader.getContext() == 0) return II;
Douglas Gregord89275b2009-07-06 18:54:52 +0000694 if (DataLen > 0) {
695 llvm::SmallVector<uint32_t, 4> DeclIDs;
696 for (; DataLen > 0; DataLen -= 4)
697 DeclIDs.push_back(ReadUnalignedLE32(d));
698 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000699 }
Mike Stump1eb44332009-09-09 15:08:12 +0000700
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000701 II->setIsFromAST();
Douglas Gregor668c1a42009-04-21 22:25:48 +0000702 return II;
703 }
704};
Mike Stump1eb44332009-09-09 15:08:12 +0000705
706} // end anonymous namespace
Douglas Gregor668c1a42009-04-21 22:25:48 +0000707
708/// \brief The on-disk hash table used to contain information about
709/// all of the identifiers in the program.
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000710typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait>
711 ASTIdentifierLookupTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000712
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000713void ASTReader::Error(const char *Msg) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000714 Diag(diag::err_fe_pch_malformed) << Msg;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000715}
716
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000717/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000718bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000719 if (Listener)
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000720 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000721 ActualOriginalFileName,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000722 SuggestedPredefines);
Douglas Gregore721f952009-04-28 18:58:38 +0000723 return false;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000724}
725
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000726//===----------------------------------------------------------------------===//
727// Source Manager Deserialization
728//===----------------------------------------------------------------------===//
729
Douglas Gregorbd945002009-04-13 16:31:14 +0000730/// \brief Read the line table in the source manager block.
731/// \returns true if ther was an error.
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000732bool ASTReader::ParseLineTable(llvm::SmallVectorImpl<uint64_t> &Record) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000733 unsigned Idx = 0;
734 LineTableInfo &LineTable = SourceMgr.getLineTable();
735
736 // Parse the file names
Douglas Gregorff0a9872009-04-13 17:12:42 +0000737 std::map<int, int> FileIDs;
738 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000739 // Extract the file name
740 unsigned FilenameLen = Record[Idx++];
741 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
742 Idx += FilenameLen;
Douglas Gregore650c8c2009-07-07 00:12:59 +0000743 MaybeAddSystemRootToFilename(Filename);
Mike Stump1eb44332009-09-09 15:08:12 +0000744 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
Douglas Gregorff0a9872009-04-13 17:12:42 +0000745 Filename.size());
Douglas Gregorbd945002009-04-13 16:31:14 +0000746 }
747
748 // Parse the line entries
749 std::vector<LineEntry> Entries;
750 while (Idx < Record.size()) {
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000751 int FID = Record[Idx++];
Douglas Gregorbd945002009-04-13 16:31:14 +0000752
753 // Extract the line entries
754 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000755 assert(NumEntries && "Numentries is 00000");
Douglas Gregorbd945002009-04-13 16:31:14 +0000756 Entries.clear();
757 Entries.reserve(NumEntries);
758 for (unsigned I = 0; I != NumEntries; ++I) {
759 unsigned FileOffset = Record[Idx++];
760 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000761 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump1eb44332009-09-09 15:08:12 +0000762 SrcMgr::CharacteristicKind FileKind
Douglas Gregorbd945002009-04-13 16:31:14 +0000763 = (SrcMgr::CharacteristicKind)Record[Idx++];
764 unsigned IncludeOffset = Record[Idx++];
765 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
766 FileKind, IncludeOffset));
767 }
768 LineTable.AddEntry(FID, Entries);
769 }
770
771 return false;
772}
773
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000774namespace {
775
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000776class ASTStatData {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000777public:
778 const bool hasStat;
779 const ino_t ino;
780 const dev_t dev;
781 const mode_t mode;
782 const time_t mtime;
783 const off_t size;
Mike Stump1eb44332009-09-09 15:08:12 +0000784
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000785 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Mike Stump1eb44332009-09-09 15:08:12 +0000786 : hasStat(true), ino(i), dev(d), mode(mo), mtime(m), size(s) {}
787
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000788 ASTStatData()
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000789 : hasStat(false), ino(0), dev(0), mode(0), mtime(0), size(0) {}
790};
791
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000792class ASTStatLookupTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000793 public:
794 typedef const char *external_key_type;
795 typedef const char *internal_key_type;
796
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000797 typedef ASTStatData data_type;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000798
799 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000800 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000801 }
802
803 static internal_key_type GetInternalKey(const char *path) { return path; }
804
805 static bool EqualKey(internal_key_type a, internal_key_type b) {
806 return strcmp(a, b) == 0;
807 }
808
809 static std::pair<unsigned, unsigned>
810 ReadKeyDataLength(const unsigned char*& d) {
811 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
812 unsigned DataLen = (unsigned) *d++;
813 return std::make_pair(KeyLen + 1, DataLen);
814 }
815
816 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
817 return (const char *)d;
818 }
819
820 static data_type ReadData(const internal_key_type, const unsigned char *d,
821 unsigned /*DataLen*/) {
822 using namespace clang::io;
823
824 if (*d++ == 1)
825 return data_type();
826
827 ino_t ino = (ino_t) ReadUnalignedLE32(d);
828 dev_t dev = (dev_t) ReadUnalignedLE32(d);
829 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +0000830 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000831 off_t size = (off_t) ReadUnalignedLE64(d);
832 return data_type(ino, dev, mode, mtime, size);
833 }
834};
835
836/// \brief stat() cache for precompiled headers.
837///
838/// This cache is very similar to the stat cache used by pretokenized
839/// headers.
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000840class ASTStatCache : public StatSysCallCache {
841 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000842 CacheTy *Cache;
843
844 unsigned &NumStatHits, &NumStatMisses;
Mike Stump1eb44332009-09-09 15:08:12 +0000845public:
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000846 ASTStatCache(const unsigned char *Buckets,
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000847 const unsigned char *Base,
848 unsigned &NumStatHits,
Mike Stump1eb44332009-09-09 15:08:12 +0000849 unsigned &NumStatMisses)
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000850 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
851 Cache = CacheTy::Create(Buckets, Base);
852 }
853
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000854 ~ASTStatCache() { delete Cache; }
Mike Stump1eb44332009-09-09 15:08:12 +0000855
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000856 int stat(const char *path, struct stat *buf) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000857 // Do the lookup for the file's data in the AST file.
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000858 CacheTy::iterator I = Cache->find(path);
859
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000860 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000861 if (I == Cache->end()) {
862 ++NumStatMisses;
Douglas Gregor52e71082009-10-16 18:18:30 +0000863 return StatSysCallCache::stat(path, buf);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000864 }
Mike Stump1eb44332009-09-09 15:08:12 +0000865
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000866 ++NumStatHits;
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000867 ASTStatData Data = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000868
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000869 if (!Data.hasStat)
870 return 1;
871
872 buf->st_ino = Data.ino;
873 buf->st_dev = Data.dev;
874 buf->st_mtime = Data.mtime;
875 buf->st_mode = Data.mode;
876 buf->st_size = Data.size;
877 return 0;
878 }
879};
880} // end anonymous namespace
881
882
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000883/// \brief Read a source manager block
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000884ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(PerFileData &F) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000885 using namespace SrcMgr;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000886
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000887 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +0000888
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000889 // Set the source-location entry cursor to the current position in
890 // the stream. This cursor will be used to read the contents of the
891 // source manager block initially, and then lazily read
892 // source-location entries as needed.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000893 SLocEntryCursor = F.Stream;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000894
895 // The stream itself is going to skip over the source manager block.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000896 if (F.Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000897 Error("malformed block record in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000898 return Failure;
899 }
900
901 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000902 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000903 Error("malformed source manager block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +0000904 return Failure;
905 }
Douglas Gregor14f79002009-04-10 03:52:48 +0000906
Douglas Gregor14f79002009-04-10 03:52:48 +0000907 RecordData Record;
908 while (true) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000909 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregor14f79002009-04-10 03:52:48 +0000910 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000911 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000912 Error("error at end of Source Manager block in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +0000913 return Failure;
914 }
Douglas Gregore1d918e2009-04-10 23:10:45 +0000915 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +0000916 }
Mike Stump1eb44332009-09-09 15:08:12 +0000917
Douglas Gregor14f79002009-04-10 03:52:48 +0000918 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
919 // No known subblocks, always skip them.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000920 SLocEntryCursor.ReadSubBlockID();
921 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000922 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +0000923 return Failure;
924 }
Douglas Gregor14f79002009-04-10 03:52:48 +0000925 continue;
926 }
Mike Stump1eb44332009-09-09 15:08:12 +0000927
Douglas Gregor14f79002009-04-10 03:52:48 +0000928 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000929 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregor14f79002009-04-10 03:52:48 +0000930 continue;
931 }
Mike Stump1eb44332009-09-09 15:08:12 +0000932
Douglas Gregor14f79002009-04-10 03:52:48 +0000933 // Read a record.
934 const char *BlobStart;
935 unsigned BlobLen;
936 Record.clear();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000937 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000938 default: // Default behavior: ignore.
939 break;
940
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000941 case SM_LINE_TABLE:
Sebastian Redl518d8cb2010-07-20 21:20:32 +0000942 if (ParseLineTable(Record))
Douglas Gregorbd945002009-04-13 16:31:14 +0000943 return Failure;
Chris Lattner2c78b872009-04-14 23:22:57 +0000944 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +0000945
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000946 case SM_SLOC_FILE_ENTRY:
947 case SM_SLOC_BUFFER_ENTRY:
948 case SM_SLOC_INSTANTIATION_ENTRY:
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000949 // Once we hit one of the source location entries, we're done.
950 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +0000951 }
952 }
953}
954
Sebastian Redl190faf72010-07-20 21:50:20 +0000955/// \brief Get a cursor that's correctly positioned for reading the source
956/// location entry with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000957llvm::BitstreamCursor &ASTReader::SLocCursorForID(unsigned ID) {
Sebastian Redl190faf72010-07-20 21:50:20 +0000958 assert(ID != 0 && ID <= TotalNumSLocEntries &&
959 "SLocCursorForID should only be called for real IDs.");
960
961 ID -= 1;
962 PerFileData *F = 0;
963 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
964 F = Chain[N - I - 1];
965 if (ID < F->LocalNumSLocEntries)
966 break;
967 ID -= F->LocalNumSLocEntries;
968 }
969 assert(F && F->LocalNumSLocEntries > ID && "Chain corrupted");
970
971 F->SLocEntryCursor.JumpToBit(F->SLocOffsets[ID]);
972 return F->SLocEntryCursor;
973}
974
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000975/// \brief Read in the source location entry with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000976ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(unsigned ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000977 if (ID == 0)
978 return Success;
979
980 if (ID > TotalNumSLocEntries) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000981 Error("source location entry ID out-of-range for AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000982 return Failure;
983 }
984
Sebastian Redl190faf72010-07-20 21:50:20 +0000985 llvm::BitstreamCursor &SLocEntryCursor = SLocCursorForID(ID);
Sebastian Redl9137a522010-07-16 17:50:48 +0000986
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000987 ++NumSLocEntriesRead;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000988 unsigned Code = SLocEntryCursor.ReadCode();
989 if (Code == llvm::bitc::END_BLOCK ||
990 Code == llvm::bitc::ENTER_SUBBLOCK ||
991 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000992 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000993 return Failure;
994 }
995
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000996 RecordData Record;
997 const char *BlobStart;
998 unsigned BlobLen;
999 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1000 default:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001001 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001002 return Failure;
1003
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001004 case SM_SLOC_FILE_ENTRY: {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001005 std::string Filename(BlobStart, BlobStart + BlobLen);
1006 MaybeAddSystemRootToFilename(Filename);
1007 const FileEntry *File = FileMgr.getFile(Filename);
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001008 if (File == 0) {
1009 std::string ErrorStr = "could not find file '";
Douglas Gregore650c8c2009-07-07 00:12:59 +00001010 ErrorStr += Filename;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001011 ErrorStr += "' referenced by AST file";
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001012 Error(ErrorStr.c_str());
1013 return Failure;
1014 }
Mike Stump1eb44332009-09-09 15:08:12 +00001015
Douglas Gregor2d52be52010-03-21 22:49:54 +00001016 if (Record.size() < 10) {
Ted Kremenek1857f622010-03-18 21:23:05 +00001017 Error("source location entry is incorrect");
1018 return Failure;
1019 }
1020
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001021 if (!DisableValidation &&
1022 ((off_t)Record[4] != File->getSize()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001023#if !defined(LLVM_ON_WIN32)
1024 // In our regression testing, the Windows file system seems to
1025 // have inconsistent modification times that sometimes
1026 // erroneously trigger this error-handling path.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001027 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001028#endif
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001029 )) {
Douglas Gregor2d52be52010-03-21 22:49:54 +00001030 Diag(diag::err_fe_pch_file_modified)
1031 << Filename;
1032 return Failure;
1033 }
1034
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001035 FileID FID = SourceMgr.createFileID(File,
1036 SourceLocation::getFromRawEncoding(Record[1]),
1037 (SrcMgr::CharacteristicKind)Record[2],
1038 ID, Record[0]);
1039 if (Record[3])
1040 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile())
1041 .setHasLineDirectives();
1042
Douglas Gregor12fab312010-03-16 16:35:32 +00001043 // Reconstruct header-search information for this file.
1044 HeaderFileInfo HFI;
Douglas Gregor2d52be52010-03-21 22:49:54 +00001045 HFI.isImport = Record[6];
1046 HFI.DirInfo = Record[7];
1047 HFI.NumIncludes = Record[8];
1048 HFI.ControllingMacroID = Record[9];
Douglas Gregor12fab312010-03-16 16:35:32 +00001049 if (Listener)
1050 Listener->ReadHeaderFileInfo(HFI, File->getUID());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001051 break;
1052 }
1053
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001054 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001055 const char *Name = BlobStart;
1056 unsigned Offset = Record[0];
1057 unsigned Code = SLocEntryCursor.ReadCode();
1058 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001059 unsigned RecCode
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001060 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001061
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001062 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001063 Error("AST record has invalid code");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001064 return Failure;
1065 }
1066
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001067 llvm::MemoryBuffer *Buffer
Chris Lattnera0a270c2010-04-05 22:42:27 +00001068 = llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(BlobStart, BlobLen - 1),
1069 Name);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001070 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset);
Mike Stump1eb44332009-09-09 15:08:12 +00001071
Douglas Gregor92b059e2009-04-28 20:33:11 +00001072 if (strcmp(Name, "<built-in>") == 0) {
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +00001073 PCHPredefinesBlock Block = {
1074 BufferID,
1075 llvm::StringRef(BlobStart, BlobLen - 1)
1076 };
1077 PCHPredefinesBuffers.push_back(Block);
Douglas Gregor92b059e2009-04-28 20:33:11 +00001078 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001079
1080 break;
1081 }
1082
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001083 case SM_SLOC_INSTANTIATION_ENTRY: {
Mike Stump1eb44332009-09-09 15:08:12 +00001084 SourceLocation SpellingLoc
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001085 = SourceLocation::getFromRawEncoding(Record[1]);
1086 SourceMgr.createInstantiationLoc(SpellingLoc,
1087 SourceLocation::getFromRawEncoding(Record[2]),
1088 SourceLocation::getFromRawEncoding(Record[3]),
1089 Record[4],
1090 ID,
1091 Record[0]);
1092 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001093 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001094 }
1095
1096 return Success;
1097}
1098
Chris Lattner6367f6d2009-04-27 01:05:14 +00001099/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1100/// specified cursor. Read the abbreviations that are at the top of the block
1101/// and then leave the cursor pointing into the block.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001102bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattner6367f6d2009-04-27 01:05:14 +00001103 unsigned BlockID) {
1104 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001105 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001106 return Failure;
1107 }
Mike Stump1eb44332009-09-09 15:08:12 +00001108
Chris Lattner6367f6d2009-04-27 01:05:14 +00001109 while (true) {
1110 unsigned Code = Cursor.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00001111
Chris Lattner6367f6d2009-04-27 01:05:14 +00001112 // We expect all abbrevs to be at the start of the block.
1113 if (Code != llvm::bitc::DEFINE_ABBREV)
1114 return false;
1115 Cursor.ReadAbbrevRecord();
1116 }
1117}
1118
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001119void ASTReader::ReadMacroRecord(llvm::BitstreamCursor &Stream, uint64_t Offset){
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001120 assert(PP && "Forgot to set Preprocessor ?");
Mike Stump1eb44332009-09-09 15:08:12 +00001121
Douglas Gregor37e26842009-04-21 23:56:24 +00001122 // Keep track of where we are in the stream, then jump back there
1123 // after reading this macro.
1124 SavedStreamPosition SavedPosition(Stream);
1125
1126 Stream.JumpToBit(Offset);
1127 RecordData Record;
1128 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1129 MacroInfo *Macro = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001130
Douglas Gregor37e26842009-04-21 23:56:24 +00001131 while (true) {
1132 unsigned Code = Stream.ReadCode();
1133 switch (Code) {
1134 case llvm::bitc::END_BLOCK:
1135 return;
1136
1137 case llvm::bitc::ENTER_SUBBLOCK:
1138 // No known subblocks, always skip them.
1139 Stream.ReadSubBlockID();
1140 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001141 Error("malformed block record in AST file");
Douglas Gregor37e26842009-04-21 23:56:24 +00001142 return;
1143 }
1144 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001145
Douglas Gregor37e26842009-04-21 23:56:24 +00001146 case llvm::bitc::DEFINE_ABBREV:
1147 Stream.ReadAbbrevRecord();
1148 continue;
1149 default: break;
1150 }
1151
1152 // Read a record.
1153 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001154 PreprocessorRecordTypes RecType =
1155 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
Douglas Gregor37e26842009-04-21 23:56:24 +00001156 switch (RecType) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001157 case PP_MACRO_OBJECT_LIKE:
1158 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001159 // If we already have a macro, that means that we've hit the end
1160 // of the definition of the macro we were looking for. We're
1161 // done.
1162 if (Macro)
1163 return;
1164
1165 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1166 if (II == 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001167 Error("macro must have a name in AST file");
Douglas Gregor37e26842009-04-21 23:56:24 +00001168 return;
1169 }
1170 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
1171 bool isUsed = Record[2];
Mike Stump1eb44332009-09-09 15:08:12 +00001172
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001173 MacroInfo *MI = PP->AllocateMacroInfo(Loc);
Douglas Gregor37e26842009-04-21 23:56:24 +00001174 MI->setIsUsed(isUsed);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001175 MI->setIsFromAST();
Mike Stump1eb44332009-09-09 15:08:12 +00001176
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001177 unsigned NextIndex = 3;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001178 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregor37e26842009-04-21 23:56:24 +00001179 // Decode function-like macro info.
1180 bool isC99VarArgs = Record[3];
1181 bool isGNUVarArgs = Record[4];
1182 MacroArgs.clear();
1183 unsigned NumArgs = Record[5];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001184 NextIndex = 6 + NumArgs;
Douglas Gregor37e26842009-04-21 23:56:24 +00001185 for (unsigned i = 0; i != NumArgs; ++i)
1186 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1187
1188 // Install function-like macro info.
1189 MI->setIsFunctionLike();
1190 if (isC99VarArgs) MI->setIsC99Varargs();
1191 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor75fdb232009-05-22 22:45:36 +00001192 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001193 PP->getPreprocessorAllocator());
Douglas Gregor37e26842009-04-21 23:56:24 +00001194 }
1195
1196 // Finally, install the macro.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001197 PP->setMacroInfo(II, MI);
Douglas Gregor37e26842009-04-21 23:56:24 +00001198
1199 // Remember that we saw this macro last so that we add the tokens that
1200 // form its body to it.
1201 Macro = MI;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001202
1203 if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) {
1204 // We have a macro definition. Load it now.
1205 PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro,
1206 getMacroDefinition(Record[NextIndex]));
1207 }
1208
Douglas Gregor37e26842009-04-21 23:56:24 +00001209 ++NumMacrosRead;
1210 break;
1211 }
Mike Stump1eb44332009-09-09 15:08:12 +00001212
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001213 case PP_TOKEN: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001214 // If we see a TOKEN before a PP_MACRO_*, then the file is
1215 // erroneous, just pretend we didn't see this.
1216 if (Macro == 0) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001217
Douglas Gregor37e26842009-04-21 23:56:24 +00001218 Token Tok;
1219 Tok.startToken();
1220 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
1221 Tok.setLength(Record[1]);
1222 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1223 Tok.setIdentifierInfo(II);
1224 Tok.setKind((tok::TokenKind)Record[3]);
1225 Tok.setFlag((Token::TokenFlags)Record[4]);
1226 Macro->AddTokenToBody(Tok);
1227 break;
1228 }
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001229
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001230 case PP_MACRO_INSTANTIATION: {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001231 // If we already have a macro, that means that we've hit the end
1232 // of the definition of the macro we were looking for. We're
1233 // done.
1234 if (Macro)
1235 return;
1236
1237 if (!PP->getPreprocessingRecord()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001238 Error("missing preprocessing record in AST file");
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001239 return;
1240 }
1241
1242 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1243 if (PPRec.getPreprocessedEntity(Record[0]))
1244 return;
1245
1246 MacroInstantiation *MI
1247 = new (PPRec) MacroInstantiation(DecodeIdentifierInfo(Record[3]),
1248 SourceRange(
1249 SourceLocation::getFromRawEncoding(Record[1]),
1250 SourceLocation::getFromRawEncoding(Record[2])),
1251 getMacroDefinition(Record[4]));
1252 PPRec.SetPreallocatedEntity(Record[0], MI);
1253 return;
1254 }
1255
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001256 case PP_MACRO_DEFINITION: {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001257 // If we already have a macro, that means that we've hit the end
1258 // of the definition of the macro we were looking for. We're
1259 // done.
1260 if (Macro)
1261 return;
1262
1263 if (!PP->getPreprocessingRecord()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001264 Error("missing preprocessing record in AST file");
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001265 return;
1266 }
1267
1268 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1269 if (PPRec.getPreprocessedEntity(Record[0]))
1270 return;
1271
1272 if (Record[1] >= MacroDefinitionsLoaded.size()) {
1273 Error("out-of-bounds macro definition record");
1274 return;
1275 }
1276
1277 MacroDefinition *MD
1278 = new (PPRec) MacroDefinition(DecodeIdentifierInfo(Record[4]),
1279 SourceLocation::getFromRawEncoding(Record[5]),
1280 SourceRange(
1281 SourceLocation::getFromRawEncoding(Record[2]),
1282 SourceLocation::getFromRawEncoding(Record[3])));
1283 PPRec.SetPreallocatedEntity(Record[0], MD);
1284 MacroDefinitionsLoaded[Record[1]] = MD;
1285 return;
1286 }
Steve Naroff83d63c72009-04-24 20:03:17 +00001287 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001288 }
1289}
1290
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001291void ASTReader::ReadDefinedMacros() {
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001292 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1293 llvm::BitstreamCursor &MacroCursor = Chain[N - I - 1]->MacroCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001294
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001295 // If there was no preprocessor block, skip this file.
1296 if (!MacroCursor.getBitStreamReader())
1297 continue;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001298
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001299 llvm::BitstreamCursor Cursor = MacroCursor;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001300 if (Cursor.EnterSubBlock(PREPROCESSOR_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001301 Error("malformed preprocessor block record in AST file");
Douglas Gregor88a35862010-01-04 19:18:44 +00001302 return;
1303 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001304
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001305 RecordData Record;
1306 while (true) {
1307 unsigned Code = Cursor.ReadCode();
1308 if (Code == llvm::bitc::END_BLOCK) {
1309 if (Cursor.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001310 Error("error at end of preprocessor block in AST file");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001311 return;
1312 }
1313 break;
Douglas Gregor88a35862010-01-04 19:18:44 +00001314 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001315
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001316 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1317 // No known subblocks, always skip them.
1318 Cursor.ReadSubBlockID();
1319 if (Cursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001320 Error("malformed block record in AST file");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001321 return;
1322 }
1323 continue;
1324 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001325
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001326 if (Code == llvm::bitc::DEFINE_ABBREV) {
1327 Cursor.ReadAbbrevRecord();
1328 continue;
1329 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001330
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001331 // Read a record.
1332 const char *BlobStart;
1333 unsigned BlobLen;
1334 Record.clear();
1335 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1336 default: // Default behavior: ignore.
1337 break;
Douglas Gregor88a35862010-01-04 19:18:44 +00001338
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001339 case PP_MACRO_OBJECT_LIKE:
1340 case PP_MACRO_FUNCTION_LIKE:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001341 DecodeIdentifierInfo(Record[0]);
1342 break;
1343
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001344 case PP_TOKEN:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001345 // Ignore tokens.
1346 break;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001347
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001348 case PP_MACRO_INSTANTIATION:
1349 case PP_MACRO_DEFINITION:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001350 // Read the macro record.
1351 ReadMacroRecord(Chain[N - I - 1]->Stream, Cursor.GetCurrentBitNo());
1352 break;
1353 }
Douglas Gregor88a35862010-01-04 19:18:44 +00001354 }
1355 }
1356}
1357
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001358MacroDefinition *ASTReader::getMacroDefinition(IdentID ID) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001359 if (ID == 0 || ID >= MacroDefinitionsLoaded.size())
1360 return 0;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001361
1362 if (!MacroDefinitionsLoaded[ID]) {
1363 unsigned Index = ID;
1364 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1365 PerFileData &F = *Chain[N - I - 1];
1366 if (Index < F.LocalNumMacroDefinitions) {
1367 ReadMacroRecord(F.Stream, F.MacroDefinitionOffsets[Index]);
1368 break;
1369 }
1370 Index -= F.LocalNumMacroDefinitions;
1371 }
1372 assert(MacroDefinitionsLoaded[ID] && "Broken chain");
1373 }
1374
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001375 return MacroDefinitionsLoaded[ID];
1376}
1377
Douglas Gregore650c8c2009-07-07 00:12:59 +00001378/// \brief If we are loading a relocatable PCH file, and the filename is
1379/// not an absolute path, add the system root to the beginning of the file
1380/// name.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001381void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001382 // If this is not a relocatable PCH file, there's nothing to do.
1383 if (!RelocatablePCH)
1384 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001385
Daniel Dunbard5b21972009-11-18 19:50:41 +00001386 if (Filename.empty() || llvm::sys::Path(Filename).isAbsolute())
Douglas Gregore650c8c2009-07-07 00:12:59 +00001387 return;
1388
Douglas Gregore650c8c2009-07-07 00:12:59 +00001389 if (isysroot == 0) {
1390 // If no system root was given, default to '/'
1391 Filename.insert(Filename.begin(), '/');
1392 return;
1393 }
Mike Stump1eb44332009-09-09 15:08:12 +00001394
Douglas Gregore650c8c2009-07-07 00:12:59 +00001395 unsigned Length = strlen(isysroot);
1396 if (isysroot[Length - 1] != '/')
1397 Filename.insert(Filename.begin(), '/');
Mike Stump1eb44332009-09-09 15:08:12 +00001398
Douglas Gregore650c8c2009-07-07 00:12:59 +00001399 Filename.insert(Filename.begin(), isysroot, isysroot + Length);
1400}
1401
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001402ASTReader::ASTReadResult
Sebastian Redl571db7f2010-08-18 23:56:56 +00001403ASTReader::ReadASTBlock(PerFileData &F) {
Sebastian Redl9137a522010-07-16 17:50:48 +00001404 llvm::BitstreamCursor &Stream = F.Stream;
1405
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001406 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001407 Error("malformed block record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001408 return Failure;
1409 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001410
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001411 // Read all of the records and blocks for the ASt file.
Douglas Gregor8038d512009-04-10 17:25:41 +00001412 RecordData Record;
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001413 bool First = true;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001414 while (!Stream.AtEndOfStream()) {
1415 unsigned Code = Stream.ReadCode();
1416 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001417 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001418 Error("error at end of module block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001419 return Failure;
1420 }
Chris Lattner7356a312009-04-11 21:15:38 +00001421
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001422 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001423 }
1424
1425 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1426 switch (Stream.ReadSubBlockID()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001427 case DECLTYPES_BLOCK_ID:
Chris Lattner6367f6d2009-04-27 01:05:14 +00001428 // We lazily load the decls block, but we want to set up the
1429 // DeclsCursor cursor to point into it. Clone our current bitcode
1430 // cursor to it, enter the block and read the abbrevs in that block.
1431 // With the main cursor, we just skip over it.
Sebastian Redl9137a522010-07-16 17:50:48 +00001432 F.DeclsCursor = Stream;
Chris Lattner6367f6d2009-04-27 01:05:14 +00001433 if (Stream.SkipBlock() || // Skip with the main cursor.
1434 // Read the abbrevs.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001435 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001436 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001437 return Failure;
1438 }
1439 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001440
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001441 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl9137a522010-07-16 17:50:48 +00001442 F.MacroCursor = Stream;
Douglas Gregor88a35862010-01-04 19:18:44 +00001443 if (PP)
1444 PP->setExternalSource(this);
1445
Chris Lattner7356a312009-04-11 21:15:38 +00001446 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001447 Error("malformed block record in AST file");
Chris Lattner7356a312009-04-11 21:15:38 +00001448 return Failure;
1449 }
1450 break;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001451
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001452 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001453 switch (ReadSourceManagerBlock(F)) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00001454 case Success:
1455 break;
1456
1457 case Failure:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001458 Error("malformed source manager block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001459 return Failure;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001460
1461 case IgnorePCH:
1462 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001463 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001464 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001465 }
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001466 First = false;
Douglas Gregor8038d512009-04-10 17:25:41 +00001467 continue;
1468 }
1469
1470 if (Code == llvm::bitc::DEFINE_ABBREV) {
1471 Stream.ReadAbbrevRecord();
1472 continue;
1473 }
1474
1475 // Read and process a record.
1476 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00001477 const char *BlobStart = 0;
1478 unsigned BlobLen = 0;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001479 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Douglas Gregor2bec0412009-04-10 21:16:55 +00001480 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001481 default: // Default behavior: ignore.
1482 break;
1483
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001484 case METADATA: {
1485 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1486 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001487 : diag::warn_pch_version_too_new);
1488 return IgnorePCH;
1489 }
1490
1491 RelocatablePCH = Record[4];
1492 if (Listener) {
1493 std::string TargetTriple(BlobStart, BlobLen);
1494 if (Listener->ReadTargetTriple(TargetTriple))
1495 return IgnorePCH;
1496 }
1497 break;
1498 }
1499
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001500 case CHAINED_METADATA: {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001501 if (!First) {
1502 Error("CHAINED_METADATA is not first record in block");
1503 return Failure;
1504 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001505 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1506 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001507 : diag::warn_pch_version_too_new);
1508 return IgnorePCH;
1509 }
1510
1511 // Load the chained file.
Sebastian Redl571db7f2010-08-18 23:56:56 +00001512 switch(ReadASTCore(llvm::StringRef(BlobStart, BlobLen))) {
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001513 case Failure: return Failure;
1514 // If we have to ignore the dependency, we'll have to ignore this too.
1515 case IgnorePCH: return IgnorePCH;
1516 case Success: break;
1517 }
1518 break;
1519 }
1520
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001521 case TYPE_OFFSET:
Sebastian Redl12d6da02010-07-19 22:06:55 +00001522 if (F.LocalNumTypes != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001523 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001524 return Failure;
1525 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00001526 F.TypeOffsets = (const uint32_t *)BlobStart;
1527 F.LocalNumTypes = Record[0];
Douglas Gregor8038d512009-04-10 17:25:41 +00001528 break;
1529
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001530 case DECL_OFFSET:
Sebastian Redl12d6da02010-07-19 22:06:55 +00001531 if (F.LocalNumDecls != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001532 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001533 return Failure;
1534 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00001535 F.DeclOffsets = (const uint32_t *)BlobStart;
1536 F.LocalNumDecls = Record[0];
Douglas Gregor8038d512009-04-10 17:25:41 +00001537 break;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001538
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001539 case TU_UPDATE_LEXICAL: {
Sebastian Redld692af72010-07-27 18:24:41 +00001540 DeclContextInfo Info = {
1541 /* No visible information */ 0, 0,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001542 reinterpret_cast<const DeclID *>(BlobStart),
1543 BlobLen / sizeof(DeclID)
Sebastian Redld692af72010-07-27 18:24:41 +00001544 };
1545 DeclContextOffsets[Context->getTranslationUnitDecl()].push_back(Info);
1546 break;
1547 }
1548
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001549 case REDECLS_UPDATE_LATEST: {
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00001550 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
1551 for (unsigned i = 0, e = Record.size(); i < e; i += 2) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001552 DeclID First = Record[i], Latest = Record[i+1];
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00001553 assert((FirstLatestDeclIDs.find(First) == FirstLatestDeclIDs.end() ||
1554 Latest > FirstLatestDeclIDs[First]) &&
1555 "The new latest is supposed to come after the previous latest");
1556 FirstLatestDeclIDs[First] = Latest;
1557 }
1558 break;
1559 }
1560
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001561 case LANGUAGE_OPTIONS:
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001562 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001563 return IgnorePCH;
1564 break;
Douglas Gregor2bec0412009-04-10 21:16:55 +00001565
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001566 case IDENTIFIER_TABLE:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001567 F.IdentifierTableData = BlobStart;
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001568 if (Record[0]) {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001569 F.IdentifierLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001570 = ASTIdentifierLookupTable::Create(
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001571 (const unsigned char *)F.IdentifierTableData + Record[0],
1572 (const unsigned char *)F.IdentifierTableData,
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001573 ASTIdentifierLookupTrait(*this, F.Stream));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001574 if (PP)
1575 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001576 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001577 break;
1578
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001579 case IDENTIFIER_OFFSET:
Sebastian Redl2da08f92010-07-19 22:28:42 +00001580 if (F.LocalNumIdentifiers != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001581 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00001582 return Failure;
1583 }
Sebastian Redl2da08f92010-07-19 22:28:42 +00001584 F.IdentifierOffsets = (const uint32_t *)BlobStart;
1585 F.LocalNumIdentifiers = Record[0];
Douglas Gregorafaf3082009-04-11 00:14:32 +00001586 break;
Douglas Gregorfdd01722009-04-14 00:24:19 +00001587
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001588 case EXTERNAL_DEFINITIONS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001589 // Optimization for the first block.
1590 if (ExternalDefinitions.empty())
1591 ExternalDefinitions.swap(Record);
1592 else
1593 ExternalDefinitions.insert(ExternalDefinitions.end(),
1594 Record.begin(), Record.end());
Douglas Gregorfdd01722009-04-14 00:24:19 +00001595 break;
Douglas Gregor3e1af842009-04-17 22:13:46 +00001596
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001597 case SPECIAL_TYPES:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001598 // Optimization for the first block
1599 if (SpecialTypes.empty())
1600 SpecialTypes.swap(Record);
1601 else
1602 SpecialTypes.insert(SpecialTypes.end(), Record.begin(), Record.end());
Douglas Gregorad1de002009-04-18 05:55:16 +00001603 break;
1604
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001605 case STATISTICS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001606 TotalNumStatements += Record[0];
1607 TotalNumMacros += Record[1];
1608 TotalLexicalDeclContexts += Record[2];
1609 TotalVisibleDeclContexts += Record[3];
Douglas Gregor3e1af842009-04-17 22:13:46 +00001610 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001611
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001612 case TENTATIVE_DEFINITIONS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001613 // Optimization for the first block.
1614 if (TentativeDefinitions.empty())
1615 TentativeDefinitions.swap(Record);
1616 else
1617 TentativeDefinitions.insert(TentativeDefinitions.end(),
1618 Record.begin(), Record.end());
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00001619 break;
Douglas Gregor14c22f22009-04-22 22:18:58 +00001620
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001621 case UNUSED_FILESCOPED_DECLS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001622 // Optimization for the first block.
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00001623 if (UnusedFileScopedDecls.empty())
1624 UnusedFileScopedDecls.swap(Record);
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001625 else
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00001626 UnusedFileScopedDecls.insert(UnusedFileScopedDecls.end(),
1627 Record.begin(), Record.end());
Tanya Lattnere6bbc012010-02-12 00:07:30 +00001628 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001629
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001630 case WEAK_UNDECLARED_IDENTIFIERS:
Sebastian Redl40566802010-08-05 18:21:25 +00001631 // Later blocks overwrite earlier ones.
1632 WeakUndeclaredIdentifiers.swap(Record);
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00001633 break;
1634
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001635 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001636 // Optimization for the first block.
1637 if (LocallyScopedExternalDecls.empty())
1638 LocallyScopedExternalDecls.swap(Record);
1639 else
1640 LocallyScopedExternalDecls.insert(LocallyScopedExternalDecls.end(),
1641 Record.begin(), Record.end());
Douglas Gregor14c22f22009-04-22 22:18:58 +00001642 break;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001643
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001644 case SELECTOR_OFFSETS:
Sebastian Redl059612d2010-08-03 21:58:15 +00001645 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redl725cd962010-08-04 20:40:17 +00001646 F.LocalNumSelectors = Record[0];
Douglas Gregor83941df2009-04-25 17:48:32 +00001647 break;
1648
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001649 case METHOD_POOL:
Sebastian Redl725cd962010-08-04 20:40:17 +00001650 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor83941df2009-04-25 17:48:32 +00001651 if (Record[0])
Sebastian Redl725cd962010-08-04 20:40:17 +00001652 F.SelectorLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001653 = ASTSelectorLookupTable::Create(
Sebastian Redl725cd962010-08-04 20:40:17 +00001654 F.SelectorLookupTableData + Record[0],
1655 F.SelectorLookupTableData,
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001656 ASTSelectorLookupTrait(*this));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00001657 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001658 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001659
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001660 case REFERENCED_SELECTOR_POOL: {
Sebastian Redl725cd962010-08-04 20:40:17 +00001661 ReferencedSelectorsData.insert(ReferencedSelectorsData.end(),
1662 Record.begin(), Record.end());
Fariborz Jahanian32019832010-07-23 19:11:11 +00001663 break;
Sebastian Redl681d7232010-07-27 00:17:23 +00001664 }
Fariborz Jahanian32019832010-07-23 19:11:11 +00001665
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001666 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001667 if (!Record.empty() && Listener)
1668 Listener->ReadCounter(Record[0]);
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001669 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001670
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001671 case SOURCE_LOCATION_OFFSETS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001672 F.SLocOffsets = (const uint32_t *)BlobStart;
1673 F.LocalNumSLocEntries = Record[0];
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001674 // We cannot delay this until the entire chain is loaded, because then
1675 // source location preloads would also have to be delayed.
1676 // FIXME: Is there a reason not to do that?
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001677 TotalNumSLocEntries += F.LocalNumSLocEntries;
Douglas Gregor445e23e2009-10-05 21:07:28 +00001678 SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, Record[1]);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001679 break;
1680
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001681 case SOURCE_LOCATION_PRELOADS:
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001682 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001683 ASTReadResult Result = ReadSLocEntryRecord(Record[I]);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001684 if (Result != Success)
1685 return Result;
1686 }
1687 break;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001688
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001689 case STAT_CACHE: {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001690 ASTStatCache *MyStatCache =
1691 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
Douglas Gregor52e71082009-10-16 18:18:30 +00001692 (const unsigned char *)BlobStart,
1693 NumStatHits, NumStatMisses);
1694 FileMgr.addStatCache(MyStatCache);
Sebastian Redl9137a522010-07-16 17:50:48 +00001695 F.StatCache = MyStatCache;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001696 break;
Douglas Gregor52e71082009-10-16 18:18:30 +00001697 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001698
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001699 case EXT_VECTOR_DECLS:
Sebastian Redla9f23682010-07-28 21:38:49 +00001700 // Optimization for the first block.
1701 if (ExtVectorDecls.empty())
1702 ExtVectorDecls.swap(Record);
1703 else
1704 ExtVectorDecls.insert(ExtVectorDecls.end(),
1705 Record.begin(), Record.end());
Douglas Gregorb81c1702009-04-27 20:06:05 +00001706 break;
1707
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001708 case VTABLE_USES:
Sebastian Redl40566802010-08-05 18:21:25 +00001709 // Later tables overwrite earlier ones.
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00001710 VTableUses.swap(Record);
1711 break;
1712
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001713 case DYNAMIC_CLASSES:
Sebastian Redl40566802010-08-05 18:21:25 +00001714 // Optimization for the first block.
1715 if (DynamicClasses.empty())
1716 DynamicClasses.swap(Record);
1717 else
1718 DynamicClasses.insert(DynamicClasses.end(),
1719 Record.begin(), Record.end());
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00001720 break;
1721
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001722 case PENDING_IMPLICIT_INSTANTIATIONS:
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00001723 // Optimization for the first block.
1724 if (PendingImplicitInstantiations.empty())
1725 PendingImplicitInstantiations.swap(Record);
1726 else
1727 PendingImplicitInstantiations.insert(
1728 PendingImplicitInstantiations.end(), Record.begin(), Record.end());
1729 break;
1730
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001731 case SEMA_DECL_REFS:
Sebastian Redl40566802010-08-05 18:21:25 +00001732 // Later tables overwrite earlier ones.
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00001733 SemaDeclRefs.swap(Record);
1734 break;
1735
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001736 case ORIGINAL_FILE_NAME:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001737 // The primary AST will be the last to get here, so it will be the one
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001738 // that's used.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +00001739 ActualOriginalFileName.assign(BlobStart, BlobLen);
1740 OriginalFileName = ActualOriginalFileName;
Douglas Gregore650c8c2009-07-07 00:12:59 +00001741 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001742 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001743
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001744 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek974be4d2010-02-12 23:31:14 +00001745 const std::string &CurBranch = getClangFullRepositoryVersion();
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001746 llvm::StringRef ASTBranch(BlobStart, BlobLen);
1747 if (llvm::StringRef(CurBranch) != ASTBranch && !DisableValidation) {
1748 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregor445e23e2009-10-05 21:07:28 +00001749 return IgnorePCH;
1750 }
1751 break;
1752 }
Sebastian Redl04e6fd42010-07-21 20:07:32 +00001753
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001754 case MACRO_DEFINITION_OFFSETS:
Sebastian Redl04e6fd42010-07-21 20:07:32 +00001755 F.MacroDefinitionOffsets = (const uint32_t *)BlobStart;
1756 F.NumPreallocatedPreprocessingEntities = Record[0];
1757 F.LocalNumMacroDefinitions = Record[1];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001758 break;
Sebastian Redl0b17c612010-08-13 00:28:03 +00001759
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001760 case DECL_REPLACEMENTS: {
Sebastian Redl0b17c612010-08-13 00:28:03 +00001761 if (Record.size() % 2 != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001762 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redl0b17c612010-08-13 00:28:03 +00001763 return Failure;
1764 }
1765 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001766 ReplacedDecls[static_cast<DeclID>(Record[I])] =
Sebastian Redl0b17c612010-08-13 00:28:03 +00001767 std::make_pair(&F, Record[I+1]);
1768 break;
1769 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001770 }
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001771 First = false;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001772 }
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001773 Error("premature end of bitstream in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001774 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001775}
1776
Sebastian Redl571db7f2010-08-18 23:56:56 +00001777ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName) {
1778 switch(ReadASTCore(FileName)) {
Sebastian Redlcdf3b832010-07-16 20:41:52 +00001779 case Failure: return Failure;
1780 case IgnorePCH: return IgnorePCH;
1781 case Success: break;
1782 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001783
1784 // Here comes stuff that we only do once the entire chain is loaded.
1785
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001786 // Allocate space for loaded identifiers, decls and types.
Sebastian Redl04e6fd42010-07-21 20:07:32 +00001787 unsigned TotalNumIdentifiers = 0, TotalNumTypes = 0, TotalNumDecls = 0,
Sebastian Redl725cd962010-08-04 20:40:17 +00001788 TotalNumPreallocatedPreprocessingEntities = 0, TotalNumMacroDefs = 0,
1789 TotalNumSelectors = 0;
Sebastian Redl12d6da02010-07-19 22:06:55 +00001790 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl2da08f92010-07-19 22:28:42 +00001791 TotalNumIdentifiers += Chain[I]->LocalNumIdentifiers;
Sebastian Redl12d6da02010-07-19 22:06:55 +00001792 TotalNumTypes += Chain[I]->LocalNumTypes;
1793 TotalNumDecls += Chain[I]->LocalNumDecls;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00001794 TotalNumPreallocatedPreprocessingEntities +=
1795 Chain[I]->NumPreallocatedPreprocessingEntities;
1796 TotalNumMacroDefs += Chain[I]->LocalNumMacroDefinitions;
Sebastian Redl725cd962010-08-04 20:40:17 +00001797 TotalNumSelectors += Chain[I]->LocalNumSelectors;
Sebastian Redl12d6da02010-07-19 22:06:55 +00001798 }
Sebastian Redl2da08f92010-07-19 22:28:42 +00001799 IdentifiersLoaded.resize(TotalNumIdentifiers);
Sebastian Redl12d6da02010-07-19 22:06:55 +00001800 TypesLoaded.resize(TotalNumTypes);
1801 DeclsLoaded.resize(TotalNumDecls);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00001802 MacroDefinitionsLoaded.resize(TotalNumMacroDefs);
1803 if (PP) {
1804 if (TotalNumIdentifiers > 0)
1805 PP->getHeaderSearchInfo().SetExternalLookup(this);
1806 if (TotalNumPreallocatedPreprocessingEntities > 0) {
1807 if (!PP->getPreprocessingRecord())
1808 PP->createPreprocessingRecord();
1809 PP->getPreprocessingRecord()->SetExternalSource(*this,
1810 TotalNumPreallocatedPreprocessingEntities);
1811 }
1812 }
Sebastian Redl725cd962010-08-04 20:40:17 +00001813 SelectorsLoaded.resize(TotalNumSelectors);
Sebastian Redl12d6da02010-07-19 22:06:55 +00001814
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001815 // Check the predefines buffers.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001816 if (!DisableValidation && CheckPredefinesBuffers())
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001817 return IgnorePCH;
1818
1819 if (PP) {
1820 // Initialization of keywords and pragmas occurs before the
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001821 // AST file is read, so there may be some identifiers that were
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001822 // loaded into the IdentifierTable before we intercepted the
1823 // creation of identifiers. Iterate through the list of known
1824 // identifiers and determine whether we have to establish
1825 // preprocessor definitions or top-level identifier declaration
1826 // chains for those identifiers.
1827 //
1828 // We copy the IdentifierInfo pointers to a small vector first,
1829 // since de-serializing declarations or macro definitions can add
1830 // new entries into the identifier table, invalidating the
1831 // iterators.
1832 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
1833 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
1834 IdEnd = PP->getIdentifierTable().end();
1835 Id != IdEnd; ++Id)
1836 Identifiers.push_back(Id->second);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00001837 // We need to search the tables in all files.
Sebastian Redl04e6fd42010-07-21 20:07:32 +00001838 for (unsigned J = 0, M = Chain.size(); J != M; ++J) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001839 ASTIdentifierLookupTable *IdTable
1840 = (ASTIdentifierLookupTable *)Chain[J]->IdentifierLookupTable;
1841 // Not all AST files necessarily have identifier tables, only the useful
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001842 // ones.
1843 if (!IdTable)
1844 continue;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00001845 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
1846 IdentifierInfo *II = Identifiers[I];
1847 // Look in the on-disk hash tables for an entry for this identifier
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001848 ASTIdentifierLookupTrait Info(*this, Chain[J]->Stream, II);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00001849 std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength());
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001850 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001851 if (Pos == IdTable->end())
1852 continue;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001853
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001854 // Dereferencing the iterator has the effect of populating the
1855 // IdentifierInfo node with the various declarations it needs.
1856 (void)*Pos;
1857 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001858 }
1859 }
1860
1861 if (Context)
1862 InitializeContext(*Context);
1863
1864 return Success;
1865}
1866
Sebastian Redl571db7f2010-08-18 23:56:56 +00001867ASTReader::ASTReadResult ASTReader::ReadASTCore(llvm::StringRef FileName) {
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001868 Chain.push_back(new PerFileData());
Sebastian Redl9137a522010-07-16 17:50:48 +00001869 PerFileData &F = *Chain.back();
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001870
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001871 // Set the AST file name.
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001872 F.FileName = FileName;
1873
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001874 // Open the AST file.
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001875 //
1876 // FIXME: This shouldn't be here, we should just take a raw_ostream.
1877 std::string ErrStr;
1878 F.Buffer.reset(llvm::MemoryBuffer::getFileOrSTDIN(FileName, &ErrStr));
1879 if (!F.Buffer) {
1880 Error(ErrStr.c_str());
1881 return IgnorePCH;
1882 }
1883
1884 // Initialize the stream
1885 F.StreamFile.init((const unsigned char *)F.Buffer->getBufferStart(),
1886 (const unsigned char *)F.Buffer->getBufferEnd());
Sebastian Redl9137a522010-07-16 17:50:48 +00001887 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001888 Stream.init(F.StreamFile);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00001889 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001890
1891 // Sniff for the signature.
1892 if (Stream.Read(8) != 'C' ||
1893 Stream.Read(8) != 'P' ||
1894 Stream.Read(8) != 'C' ||
1895 Stream.Read(8) != 'H') {
1896 Diag(diag::err_not_a_pch_file) << FileName;
1897 return Failure;
1898 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001899
Douglas Gregor2cf26342009-04-09 22:27:44 +00001900 while (!Stream.AtEndOfStream()) {
1901 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00001902
Douglas Gregore1d918e2009-04-10 23:10:45 +00001903 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001904 Error("invalid record at top-level of AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001905 return Failure;
1906 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001907
1908 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregor668c1a42009-04-21 22:25:48 +00001909
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001910 // We only know the AST subblock ID.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001911 switch (BlockID) {
1912 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001913 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001914 Error("malformed BlockInfoBlock in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001915 return Failure;
1916 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001917 break;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001918 case AST_BLOCK_ID:
Sebastian Redl571db7f2010-08-18 23:56:56 +00001919 switch (ReadASTBlock(F)) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001920 case Success:
1921 break;
1922
1923 case Failure:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001924 return Failure;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001925
1926 case IgnorePCH:
Douglas Gregor2bec0412009-04-10 21:16:55 +00001927 // FIXME: We could consider reading through to the end of this
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001928 // AST block, skipping subblocks, to see if there are other
1929 // AST blocks elsewhere.
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00001930
1931 // Clear out any preallocated source location entries, so that
1932 // the source manager does not try to resolve them later.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001933 SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00001934
1935 // Remove the stat cache.
Sebastian Redl9137a522010-07-16 17:50:48 +00001936 if (F.StatCache)
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001937 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00001938
Douglas Gregore1d918e2009-04-10 23:10:45 +00001939 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001940 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001941 break;
1942 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001943 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001944 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001945 return Failure;
1946 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001947 break;
1948 }
Mike Stump1eb44332009-09-09 15:08:12 +00001949 }
1950
Sebastian Redlcdf3b832010-07-16 20:41:52 +00001951 return Success;
1952}
1953
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001954void ASTReader::setPreprocessor(Preprocessor &pp) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001955 PP = &pp;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00001956
1957 unsigned TotalNum = 0;
1958 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
1959 TotalNum += Chain[I]->NumPreallocatedPreprocessingEntities;
1960 if (TotalNum) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001961 if (!PP->getPreprocessingRecord())
1962 PP->createPreprocessingRecord();
Sebastian Redl04e6fd42010-07-21 20:07:32 +00001963 PP->getPreprocessingRecord()->SetExternalSource(*this, TotalNum);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001964 }
1965}
1966
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001967void ASTReader::InitializeContext(ASTContext &Ctx) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001968 Context = &Ctx;
1969 assert(Context && "Passed null context!");
1970
1971 assert(PP && "Forgot to set Preprocessor ?");
1972 PP->getIdentifierTable().setExternalIdentifierLookup(this);
1973 PP->getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor88a35862010-01-04 19:18:44 +00001974 PP->setExternalSource(this);
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001975
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001976 // Load the translation unit declaration
Argyrios Kyrtzidis8871a442010-07-08 17:13:02 +00001977 GetTranslationUnitDecl();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001978
1979 // Load the special types.
1980 Context->setBuiltinVaListType(
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001981 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
1982 if (unsigned Id = SpecialTypes[SPECIAL_TYPE_OBJC_ID])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001983 Context->setObjCIdType(GetType(Id));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001984 if (unsigned Sel = SpecialTypes[SPECIAL_TYPE_OBJC_SELECTOR])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001985 Context->setObjCSelType(GetType(Sel));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001986 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001987 Context->setObjCProtoType(GetType(Proto));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001988 if (unsigned Class = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001989 Context->setObjCClassType(GetType(Class));
Steve Naroff14108da2009-07-10 23:34:53 +00001990
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001991 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001992 Context->setCFConstantStringType(GetType(String));
Mike Stump1eb44332009-09-09 15:08:12 +00001993 if (unsigned FastEnum
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001994 = SpecialTypes[SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001995 Context->setObjCFastEnumerationStateType(GetType(FastEnum));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001996 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
Douglas Gregorc29f77b2009-07-07 16:35:42 +00001997 QualType FileType = GetType(File);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001998 if (FileType.isNull()) {
1999 Error("FILE type is NULL");
2000 return;
2001 }
John McCall183700f2009-09-21 23:43:11 +00002002 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002003 Context->setFILEDecl(Typedef->getDecl());
2004 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002005 const TagType *Tag = FileType->getAs<TagType>();
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002006 if (!Tag) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002007 Error("Invalid FILE type in AST file");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002008 return;
2009 }
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002010 Context->setFILEDecl(Tag->getDecl());
2011 }
2012 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002013 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
Mike Stump782fa302009-07-28 02:25:19 +00002014 QualType Jmp_bufType = GetType(Jmp_buf);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002015 if (Jmp_bufType.isNull()) {
2016 Error("jmp_bug type is NULL");
2017 return;
2018 }
John McCall183700f2009-09-21 23:43:11 +00002019 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
Mike Stump782fa302009-07-28 02:25:19 +00002020 Context->setjmp_bufDecl(Typedef->getDecl());
2021 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002022 const TagType *Tag = Jmp_bufType->getAs<TagType>();
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002023 if (!Tag) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002024 Error("Invalid jmp_buf type in AST file");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002025 return;
2026 }
Mike Stump782fa302009-07-28 02:25:19 +00002027 Context->setjmp_bufDecl(Tag->getDecl());
2028 }
2029 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002030 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
Mike Stump782fa302009-07-28 02:25:19 +00002031 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002032 if (Sigjmp_bufType.isNull()) {
2033 Error("sigjmp_buf type is NULL");
2034 return;
2035 }
John McCall183700f2009-09-21 23:43:11 +00002036 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
Mike Stump782fa302009-07-28 02:25:19 +00002037 Context->setsigjmp_bufDecl(Typedef->getDecl());
2038 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002039 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002040 assert(Tag && "Invalid sigjmp_buf type in AST file");
Mike Stump782fa302009-07-28 02:25:19 +00002041 Context->setsigjmp_bufDecl(Tag->getDecl());
2042 }
2043 }
Mike Stump1eb44332009-09-09 15:08:12 +00002044 if (unsigned ObjCIdRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002045 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION])
Douglas Gregord1571ac2009-08-21 00:27:50 +00002046 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
Mike Stump1eb44332009-09-09 15:08:12 +00002047 if (unsigned ObjCClassRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002048 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION])
Douglas Gregord1571ac2009-08-21 00:27:50 +00002049 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002050 if (unsigned String = SpecialTypes[SPECIAL_TYPE_BLOCK_DESCRIPTOR])
Mike Stumpadaaad32009-10-20 02:12:22 +00002051 Context->setBlockDescriptorType(GetType(String));
Mike Stump083c25e2009-10-22 00:49:09 +00002052 if (unsigned String
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002053 = SpecialTypes[SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR])
Mike Stump083c25e2009-10-22 00:49:09 +00002054 Context->setBlockDescriptorExtendedType(GetType(String));
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002055 if (unsigned ObjCSelRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002056 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION])
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002057 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002058 if (unsigned String = SpecialTypes[SPECIAL_TYPE_NS_CONSTANT_STRING])
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002059 Context->setNSConstantStringType(GetType(String));
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002060
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002061 if (SpecialTypes[SPECIAL_TYPE_INT128_INSTALLED])
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002062 Context->setInt128Installed();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002063}
2064
Douglas Gregorb64c1932009-05-12 01:31:05 +00002065/// \brief Retrieve the name of the original source file name
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002066/// directly from the AST file, without actually loading the AST
Douglas Gregorb64c1932009-05-12 01:31:05 +00002067/// file.
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002068std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00002069 Diagnostic &Diags) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002070 // Open the AST file.
Douglas Gregorb64c1932009-05-12 01:31:05 +00002071 std::string ErrStr;
2072 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002073 Buffer.reset(llvm::MemoryBuffer::getFile(ASTFileName.c_str(), &ErrStr));
Douglas Gregorb64c1932009-05-12 01:31:05 +00002074 if (!Buffer) {
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00002075 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002076 return std::string();
2077 }
2078
2079 // Initialize the stream
2080 llvm::BitstreamReader StreamFile;
2081 llvm::BitstreamCursor Stream;
Mike Stump1eb44332009-09-09 15:08:12 +00002082 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregorb64c1932009-05-12 01:31:05 +00002083 (const unsigned char *)Buffer->getBufferEnd());
2084 Stream.init(StreamFile);
2085
2086 // Sniff for the signature.
2087 if (Stream.Read(8) != 'C' ||
2088 Stream.Read(8) != 'P' ||
2089 Stream.Read(8) != 'C' ||
2090 Stream.Read(8) != 'H') {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002091 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002092 return std::string();
2093 }
2094
2095 RecordData Record;
2096 while (!Stream.AtEndOfStream()) {
2097 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002098
Douglas Gregorb64c1932009-05-12 01:31:05 +00002099 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2100 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump1eb44332009-09-09 15:08:12 +00002101
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002102 // We only know the AST subblock ID.
Douglas Gregorb64c1932009-05-12 01:31:05 +00002103 switch (BlockID) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002104 case AST_BLOCK_ID:
2105 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002106 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002107 return std::string();
2108 }
2109 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002110
Douglas Gregorb64c1932009-05-12 01:31:05 +00002111 default:
2112 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002113 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002114 return std::string();
2115 }
2116 break;
2117 }
2118 continue;
2119 }
2120
2121 if (Code == llvm::bitc::END_BLOCK) {
2122 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002123 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002124 return std::string();
2125 }
2126 continue;
2127 }
2128
2129 if (Code == llvm::bitc::DEFINE_ABBREV) {
2130 Stream.ReadAbbrevRecord();
2131 continue;
2132 }
2133
2134 Record.clear();
2135 const char *BlobStart = 0;
2136 unsigned BlobLen = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002137 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002138 == ORIGINAL_FILE_NAME)
Douglas Gregorb64c1932009-05-12 01:31:05 +00002139 return std::string(BlobStart, BlobLen);
Mike Stump1eb44332009-09-09 15:08:12 +00002140 }
Douglas Gregorb64c1932009-05-12 01:31:05 +00002141
2142 return std::string();
2143}
2144
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002145/// \brief Parse the record that corresponds to a LangOptions data
2146/// structure.
2147///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002148/// This routine parses the language options from the AST file and then gives
2149/// them to the AST listener if one is set.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002150///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002151/// \returns true if the listener deems the file unacceptable, false otherwise.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002152bool ASTReader::ParseLanguageOptions(
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002153 const llvm::SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002154 if (Listener) {
2155 LangOptions LangOpts;
Mike Stump1eb44332009-09-09 15:08:12 +00002156
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002157 #define PARSE_LANGOPT(Option) \
2158 LangOpts.Option = Record[Idx]; \
2159 ++Idx
Mike Stump1eb44332009-09-09 15:08:12 +00002160
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002161 unsigned Idx = 0;
2162 PARSE_LANGOPT(Trigraphs);
2163 PARSE_LANGOPT(BCPLComment);
2164 PARSE_LANGOPT(DollarIdents);
2165 PARSE_LANGOPT(AsmPreprocessor);
2166 PARSE_LANGOPT(GNUMode);
Chandler Carrutheb5d7b72010-04-17 20:17:31 +00002167 PARSE_LANGOPT(GNUKeywords);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002168 PARSE_LANGOPT(ImplicitInt);
2169 PARSE_LANGOPT(Digraphs);
2170 PARSE_LANGOPT(HexFloats);
2171 PARSE_LANGOPT(C99);
2172 PARSE_LANGOPT(Microsoft);
2173 PARSE_LANGOPT(CPlusPlus);
2174 PARSE_LANGOPT(CPlusPlus0x);
2175 PARSE_LANGOPT(CXXOperatorNames);
2176 PARSE_LANGOPT(ObjC1);
2177 PARSE_LANGOPT(ObjC2);
2178 PARSE_LANGOPT(ObjCNonFragileABI);
Fariborz Jahanian412e7982010-02-09 19:31:38 +00002179 PARSE_LANGOPT(ObjCNonFragileABI2);
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +00002180 PARSE_LANGOPT(NoConstantCFStrings);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002181 PARSE_LANGOPT(PascalStrings);
2182 PARSE_LANGOPT(WritableStrings);
2183 PARSE_LANGOPT(LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +00002184 PARSE_LANGOPT(AltiVec);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002185 PARSE_LANGOPT(Exceptions);
Daniel Dunbar73482882010-02-10 18:48:44 +00002186 PARSE_LANGOPT(SjLjExceptions);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002187 PARSE_LANGOPT(NeXTRuntime);
2188 PARSE_LANGOPT(Freestanding);
2189 PARSE_LANGOPT(NoBuiltin);
2190 PARSE_LANGOPT(ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +00002191 PARSE_LANGOPT(POSIXThreads);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002192 PARSE_LANGOPT(Blocks);
2193 PARSE_LANGOPT(EmitAllDecls);
2194 PARSE_LANGOPT(MathErrno);
Chris Lattnera4d71452010-06-26 21:25:03 +00002195 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
2196 Record[Idx++]);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002197 PARSE_LANGOPT(HeinousExtensions);
2198 PARSE_LANGOPT(Optimize);
2199 PARSE_LANGOPT(OptimizeSize);
2200 PARSE_LANGOPT(Static);
2201 PARSE_LANGOPT(PICLevel);
2202 PARSE_LANGOPT(GNUInline);
2203 PARSE_LANGOPT(NoInline);
2204 PARSE_LANGOPT(AccessControl);
2205 PARSE_LANGOPT(CharIsSigned);
John Thompsona6fda122009-11-05 20:14:16 +00002206 PARSE_LANGOPT(ShortWChar);
Chris Lattnera4d71452010-06-26 21:25:03 +00002207 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
2208 LangOpts.setVisibilityMode((LangOptions::VisibilityMode)Record[Idx++]);
Daniel Dunbarab8e2812009-09-21 04:16:19 +00002209 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
Chris Lattnera4d71452010-06-26 21:25:03 +00002210 Record[Idx++]);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002211 PARSE_LANGOPT(InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +00002212 PARSE_LANGOPT(OpenCL);
Mike Stump9c276ae2009-12-12 01:27:46 +00002213 PARSE_LANGOPT(CatchUndefined);
2214 // FIXME: Missing ElideConstructors?!
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002215 #undef PARSE_LANGOPT
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002216
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002217 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002218 }
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002219
2220 return false;
2221}
2222
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002223void ASTReader::ReadPreprocessedEntities() {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002224 ReadDefinedMacros();
2225}
2226
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002227/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002228ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002229 PerFileData *F = 0;
2230 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2231 F = Chain[N - I - 1];
2232 if (Index < F->LocalNumTypes)
2233 break;
2234 Index -= F->LocalNumTypes;
2235 }
2236 assert(F && F->LocalNumTypes > Index && "Broken chain");
Sebastian Redl971dd442010-07-20 22:55:31 +00002237 return RecordLocation(&F->DeclsCursor, F->TypeOffsets[Index]);
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002238}
2239
2240/// \brief Read and return the type with the given index..
Douglas Gregor2cf26342009-04-09 22:27:44 +00002241///
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002242/// The index is the type ID, shifted and minus the number of predefs. This
2243/// routine actually reads the record corresponding to the type at the given
2244/// location. It is a helper routine for GetType, which deals with reading type
2245/// IDs.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002246QualType ASTReader::ReadTypeRecord(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002247 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redl971dd442010-07-20 22:55:31 +00002248 llvm::BitstreamCursor &DeclsCursor = *Loc.first;
Sebastian Redl9137a522010-07-16 17:50:48 +00002249
Douglas Gregor0b748912009-04-14 21:18:50 +00002250 // Keep track of where we are in the stream, then jump back there
2251 // after reading this type.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002252 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregor0b748912009-04-14 21:18:50 +00002253
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00002254 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redl27372b42010-08-11 18:52:41 +00002255
Douglas Gregord89275b2009-07-06 18:54:52 +00002256 // Note that we are loading a type record.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00002257 Deserializing AType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00002258
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002259 DeclsCursor.JumpToBit(Loc.second);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002260 RecordData Record;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002261 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002262 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
2263 case TYPE_EXT_QUAL: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002264 if (Record.size() != 2) {
2265 Error("Incorrect encoding of extended qualifier type");
2266 return QualType();
2267 }
Douglas Gregor6d473962009-04-15 22:00:08 +00002268 QualType Base = GetType(Record[0]);
John McCall0953e762009-09-24 19:53:00 +00002269 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]);
2270 return Context->getQualifiedType(Base, Quals);
Douglas Gregor6d473962009-04-15 22:00:08 +00002271 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002272
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002273 case TYPE_COMPLEX: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002274 if (Record.size() != 1) {
2275 Error("Incorrect encoding of complex type");
2276 return QualType();
2277 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002278 QualType ElemType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002279 return Context->getComplexType(ElemType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002280 }
2281
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002282 case TYPE_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002283 if (Record.size() != 1) {
2284 Error("Incorrect encoding of pointer type");
2285 return QualType();
2286 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002287 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002288 return Context->getPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002289 }
2290
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002291 case TYPE_BLOCK_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002292 if (Record.size() != 1) {
2293 Error("Incorrect encoding of block pointer type");
2294 return QualType();
2295 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002296 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002297 return Context->getBlockPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002298 }
2299
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002300 case TYPE_LVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002301 if (Record.size() != 1) {
2302 Error("Incorrect encoding of lvalue reference type");
2303 return QualType();
2304 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002305 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002306 return Context->getLValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002307 }
2308
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002309 case TYPE_RVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002310 if (Record.size() != 1) {
2311 Error("Incorrect encoding of rvalue reference type");
2312 return QualType();
2313 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002314 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002315 return Context->getRValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002316 }
2317
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002318 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidis240437b2010-07-02 11:55:15 +00002319 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002320 Error("Incorrect encoding of member pointer type");
2321 return QualType();
2322 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002323 QualType PointeeType = GetType(Record[0]);
2324 QualType ClassType = GetType(Record[1]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002325 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002326 }
2327
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002328 case TYPE_CONSTANT_ARRAY: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002329 QualType ElementType = GetType(Record[0]);
2330 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2331 unsigned IndexTypeQuals = Record[2];
2332 unsigned Idx = 3;
2333 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002334 return Context->getConstantArrayType(ElementType, Size,
2335 ASM, IndexTypeQuals);
2336 }
2337
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002338 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002339 QualType ElementType = GetType(Record[0]);
2340 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2341 unsigned IndexTypeQuals = Record[2];
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002342 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002343 }
2344
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002345 case TYPE_VARIABLE_ARRAY: {
Douglas Gregor0b748912009-04-14 21:18:50 +00002346 QualType ElementType = GetType(Record[0]);
2347 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2348 unsigned IndexTypeQuals = Record[2];
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002349 SourceLocation LBLoc = SourceLocation::getFromRawEncoding(Record[3]);
2350 SourceLocation RBLoc = SourceLocation::getFromRawEncoding(Record[4]);
Sebastian Redl577d4792010-07-22 22:43:28 +00002351 return Context->getVariableArrayType(ElementType, ReadExpr(DeclsCursor),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002352 ASM, IndexTypeQuals,
2353 SourceRange(LBLoc, RBLoc));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002354 }
2355
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002356 case TYPE_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00002357 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002358 Error("incorrect encoding of vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002359 return QualType();
2360 }
2361
2362 QualType ElementType = GetType(Record[0]);
2363 unsigned NumElements = Record[1];
Chris Lattner788b0fd2010-06-23 06:00:24 +00002364 unsigned AltiVecSpec = Record[2];
2365 return Context->getVectorType(ElementType, NumElements,
2366 (VectorType::AltiVecSpecific)AltiVecSpec);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002367 }
2368
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002369 case TYPE_EXT_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00002370 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002371 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002372 return QualType();
2373 }
2374
2375 QualType ElementType = GetType(Record[0]);
2376 unsigned NumElements = Record[1];
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002377 return Context->getExtVectorType(ElementType, NumElements);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002378 }
2379
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002380 case TYPE_FUNCTION_NO_PROTO: {
Rafael Espindola425ef722010-03-30 22:15:11 +00002381 if (Record.size() != 4) {
Douglas Gregora02b1472009-04-28 21:53:25 +00002382 Error("incorrect encoding of no-proto function type");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002383 return QualType();
2384 }
2385 QualType ResultType = GetType(Record[0]);
Rafael Espindola425ef722010-03-30 22:15:11 +00002386 FunctionType::ExtInfo Info(Record[1], Record[2], (CallingConv)Record[3]);
Rafael Espindola264ba482010-03-30 20:24:48 +00002387 return Context->getFunctionNoProtoType(ResultType, Info);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002388 }
2389
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002390 case TYPE_FUNCTION_PROTO: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002391 QualType ResultType = GetType(Record[0]);
Douglas Gregor91236662009-12-22 18:11:50 +00002392 bool NoReturn = Record[1];
Rafael Espindola425ef722010-03-30 22:15:11 +00002393 unsigned RegParm = Record[2];
2394 CallingConv CallConv = (CallingConv)Record[3];
2395 unsigned Idx = 4;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002396 unsigned NumParams = Record[Idx++];
2397 llvm::SmallVector<QualType, 16> ParamTypes;
2398 for (unsigned I = 0; I != NumParams; ++I)
2399 ParamTypes.push_back(GetType(Record[Idx++]));
2400 bool isVariadic = Record[Idx++];
2401 unsigned Quals = Record[Idx++];
Sebastian Redl465226e2009-05-27 22:11:52 +00002402 bool hasExceptionSpec = Record[Idx++];
2403 bool hasAnyExceptionSpec = Record[Idx++];
2404 unsigned NumExceptions = Record[Idx++];
2405 llvm::SmallVector<QualType, 2> Exceptions;
2406 for (unsigned I = 0; I != NumExceptions; ++I)
2407 Exceptions.push_back(GetType(Record[Idx++]));
Jay Foadbeaaccd2009-05-21 09:52:38 +00002408 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
Sebastian Redl465226e2009-05-27 22:11:52 +00002409 isVariadic, Quals, hasExceptionSpec,
2410 hasAnyExceptionSpec, NumExceptions,
Rafael Espindola264ba482010-03-30 20:24:48 +00002411 Exceptions.data(),
Rafael Espindola425ef722010-03-30 22:15:11 +00002412 FunctionType::ExtInfo(NoReturn, RegParm,
2413 CallConv));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002414 }
2415
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002416 case TYPE_UNRESOLVED_USING:
John McCalled976492009-12-04 22:46:56 +00002417 return Context->getTypeDeclType(
2418 cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0])));
2419
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002420 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002421 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002422 Error("incorrect encoding of typedef type");
2423 return QualType();
2424 }
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002425 TypedefDecl *Decl = cast<TypedefDecl>(GetDecl(Record[0]));
2426 QualType Canonical = GetType(Record[1]);
2427 return Context->getTypedefType(Decl, Canonical);
2428 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002429
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002430 case TYPE_TYPEOF_EXPR:
Sebastian Redl577d4792010-07-22 22:43:28 +00002431 return Context->getTypeOfExprType(ReadExpr(DeclsCursor));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002432
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002433 case TYPE_TYPEOF: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002434 if (Record.size() != 1) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002435 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002436 return QualType();
2437 }
2438 QualType UnderlyingType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002439 return Context->getTypeOfType(UnderlyingType);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002440 }
Mike Stump1eb44332009-09-09 15:08:12 +00002441
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002442 case TYPE_DECLTYPE:
Sebastian Redl577d4792010-07-22 22:43:28 +00002443 return Context->getDecltypeType(ReadExpr(DeclsCursor));
Anders Carlsson395b4752009-06-24 19:06:50 +00002444
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002445 case TYPE_RECORD: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002446 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002447 Error("incorrect encoding of record type");
2448 return QualType();
2449 }
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002450 bool IsDependent = Record[0];
2451 QualType T = Context->getRecordType(cast<RecordDecl>(GetDecl(Record[1])));
2452 T->Dependent = IsDependent;
2453 return T;
2454 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002455
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002456 case TYPE_ENUM: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002457 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002458 Error("incorrect encoding of enum type");
2459 return QualType();
2460 }
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002461 bool IsDependent = Record[0];
2462 QualType T = Context->getEnumType(cast<EnumDecl>(GetDecl(Record[1])));
2463 T->Dependent = IsDependent;
2464 return T;
2465 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002466
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002467 case TYPE_ELABORATED: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00002468 unsigned Idx = 0;
2469 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2470 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2471 QualType NamedType = GetType(Record[Idx++]);
2472 return Context->getElaboratedType(Keyword, NNS, NamedType);
John McCall7da24312009-09-05 00:15:47 +00002473 }
2474
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002475 case TYPE_OBJC_INTERFACE: {
Chris Lattnerc6fa4452009-04-22 06:45:28 +00002476 unsigned Idx = 0;
2477 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
John McCallc12c5bb2010-05-15 11:32:37 +00002478 return Context->getObjCInterfaceType(ItfD);
2479 }
2480
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002481 case TYPE_OBJC_OBJECT: {
John McCallc12c5bb2010-05-15 11:32:37 +00002482 unsigned Idx = 0;
2483 QualType Base = GetType(Record[Idx++]);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00002484 unsigned NumProtos = Record[Idx++];
2485 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2486 for (unsigned I = 0; I != NumProtos; ++I)
2487 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
John McCallc12c5bb2010-05-15 11:32:37 +00002488 return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00002489 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002490
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002491 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00002492 unsigned Idx = 0;
John McCallc12c5bb2010-05-15 11:32:37 +00002493 QualType Pointee = GetType(Record[Idx++]);
2494 return Context->getObjCObjectPointerType(Pointee);
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00002495 }
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +00002496
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002497 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCall49a832b2009-10-18 09:09:24 +00002498 unsigned Idx = 0;
2499 QualType Parm = GetType(Record[Idx++]);
2500 QualType Replacement = GetType(Record[Idx++]);
2501 return
2502 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
2503 Replacement);
2504 }
John McCall3cb0ebd2010-03-10 03:28:59 +00002505
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002506 case TYPE_INJECTED_CLASS_NAME: {
John McCall3cb0ebd2010-03-10 03:28:59 +00002507 CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0]));
2508 QualType TST = GetType(Record[1]); // probably derivable
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00002509 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002510 // for AST reading, too much interdependencies.
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00002511 return
2512 QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCall3cb0ebd2010-03-10 03:28:59 +00002513 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00002514
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002515 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002516 unsigned Idx = 0;
2517 unsigned Depth = Record[Idx++];
2518 unsigned Index = Record[Idx++];
2519 bool Pack = Record[Idx++];
2520 IdentifierInfo *Name = GetIdentifierInfo(Record, Idx);
2521 return Context->getTemplateTypeParmType(Depth, Index, Pack, Name);
2522 }
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00002523
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002524 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00002525 unsigned Idx = 0;
2526 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2527 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2528 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +00002529 QualType Canon = GetType(Record[Idx++]);
2530 return Context->getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00002531 }
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00002532
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002533 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00002534 unsigned Idx = 0;
2535 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2536 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2537 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
2538 unsigned NumArgs = Record[Idx++];
2539 llvm::SmallVector<TemplateArgument, 8> Args;
2540 Args.reserve(NumArgs);
2541 while (NumArgs--)
Sebastian Redl577d4792010-07-22 22:43:28 +00002542 Args.push_back(ReadTemplateArgument(DeclsCursor, Record, Idx));
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00002543 return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
2544 Args.size(), Args.data());
2545 }
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00002546
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002547 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00002548 unsigned Idx = 0;
2549
2550 // ArrayType
2551 QualType ElementType = GetType(Record[Idx++]);
2552 ArrayType::ArraySizeModifier ASM
2553 = (ArrayType::ArraySizeModifier)Record[Idx++];
2554 unsigned IndexTypeQuals = Record[Idx++];
2555
2556 // DependentSizedArrayType
Sebastian Redl577d4792010-07-22 22:43:28 +00002557 Expr *NumElts = ReadExpr(DeclsCursor);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00002558 SourceRange Brackets = ReadSourceRange(Record, Idx);
2559
2560 return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
2561 IndexTypeQuals, Brackets);
2562 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00002563
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002564 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002565 unsigned Idx = 0;
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002566 bool IsDependent = Record[Idx++];
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002567 TemplateName Name = ReadTemplateName(Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002568 llvm::SmallVector<TemplateArgument, 8> Args;
Sebastian Redl577d4792010-07-22 22:43:28 +00002569 ReadTemplateArgumentList(Args, DeclsCursor, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00002570 QualType Canon = GetType(Record[Idx++]);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002571 QualType T;
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002572 if (Canon.isNull())
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002573 T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(),
2574 Args.size());
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002575 else
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002576 T = Context->getTemplateSpecializationType(Name, Args.data(),
2577 Args.size(), Canon);
2578 T->Dependent = IsDependent;
2579 return T;
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002580 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002581 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002582 // Suppress a GCC warning
2583 return QualType();
2584}
2585
John McCalla1ee0c52009-10-16 21:56:05 +00002586namespace {
2587
2588class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002589 ASTReader &Reader;
Sebastian Redl577d4792010-07-22 22:43:28 +00002590 llvm::BitstreamCursor &DeclsCursor;
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002591 const ASTReader::RecordData &Record;
John McCalla1ee0c52009-10-16 21:56:05 +00002592 unsigned &Idx;
2593
2594public:
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002595 TypeLocReader(ASTReader &Reader, llvm::BitstreamCursor &Cursor,
2596 const ASTReader::RecordData &Record, unsigned &Idx)
Sebastian Redl577d4792010-07-22 22:43:28 +00002597 : Reader(Reader), DeclsCursor(Cursor), Record(Record), Idx(Idx) { }
John McCalla1ee0c52009-10-16 21:56:05 +00002598
John McCall51bd8032009-10-18 01:05:36 +00002599 // We want compile-time assurance that we've enumerated all of
2600 // these, so unfortunately we have to declare them first, then
2601 // define them out-of-line.
2602#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +00002603#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +00002604 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00002605#include "clang/AST/TypeLocNodes.def"
2606
John McCall51bd8032009-10-18 01:05:36 +00002607 void VisitFunctionTypeLoc(FunctionTypeLoc);
2608 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00002609};
2610
2611}
2612
John McCall51bd8032009-10-18 01:05:36 +00002613void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCalla1ee0c52009-10-16 21:56:05 +00002614 // nothing to do
2615}
John McCall51bd8032009-10-18 01:05:36 +00002616void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +00002617 TL.setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2618 if (TL.needsExtraLocalData()) {
2619 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
2620 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
2621 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
2622 TL.setModeAttr(Record[Idx++]);
2623 }
John McCalla1ee0c52009-10-16 21:56:05 +00002624}
John McCall51bd8032009-10-18 01:05:36 +00002625void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
2626 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002627}
John McCall51bd8032009-10-18 01:05:36 +00002628void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
2629 TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002630}
John McCall51bd8032009-10-18 01:05:36 +00002631void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
2632 TL.setCaretLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002633}
John McCall51bd8032009-10-18 01:05:36 +00002634void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
2635 TL.setAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002636}
John McCall51bd8032009-10-18 01:05:36 +00002637void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
2638 TL.setAmpAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002639}
John McCall51bd8032009-10-18 01:05:36 +00002640void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
2641 TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002642}
John McCall51bd8032009-10-18 01:05:36 +00002643void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
2644 TL.setLBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2645 TL.setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002646 if (Record[Idx++])
Sebastian Redl577d4792010-07-22 22:43:28 +00002647 TL.setSizeExpr(Reader.ReadExpr(DeclsCursor));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002648 else
John McCall51bd8032009-10-18 01:05:36 +00002649 TL.setSizeExpr(0);
2650}
2651void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
2652 VisitArrayTypeLoc(TL);
2653}
2654void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
2655 VisitArrayTypeLoc(TL);
2656}
2657void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
2658 VisitArrayTypeLoc(TL);
2659}
2660void TypeLocReader::VisitDependentSizedArrayTypeLoc(
2661 DependentSizedArrayTypeLoc TL) {
2662 VisitArrayTypeLoc(TL);
2663}
2664void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
2665 DependentSizedExtVectorTypeLoc TL) {
2666 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2667}
2668void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
2669 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2670}
2671void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
2672 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2673}
2674void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
2675 TL.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2676 TL.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2677 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
John McCall86acc2a2009-10-23 01:28:53 +00002678 TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
John McCall51bd8032009-10-18 01:05:36 +00002679 }
2680}
2681void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
2682 VisitFunctionTypeLoc(TL);
2683}
2684void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
2685 VisitFunctionTypeLoc(TL);
2686}
John McCalled976492009-12-04 22:46:56 +00002687void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
2688 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2689}
John McCall51bd8032009-10-18 01:05:36 +00002690void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
2691 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2692}
2693void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +00002694 TL.setTypeofLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2695 TL.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2696 TL.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall51bd8032009-10-18 01:05:36 +00002697}
2698void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +00002699 TL.setTypeofLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2700 TL.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2701 TL.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Sebastian Redl577d4792010-07-22 22:43:28 +00002702 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(DeclsCursor, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00002703}
2704void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
2705 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2706}
2707void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
2708 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2709}
2710void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
2711 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2712}
John McCall51bd8032009-10-18 01:05:36 +00002713void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
2714 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2715}
John McCall49a832b2009-10-18 09:09:24 +00002716void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
2717 SubstTemplateTypeParmTypeLoc TL) {
2718 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2719}
John McCall51bd8032009-10-18 01:05:36 +00002720void TypeLocReader::VisitTemplateSpecializationTypeLoc(
2721 TemplateSpecializationTypeLoc TL) {
John McCall833ca992009-10-29 08:12:44 +00002722 TL.setTemplateNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2723 TL.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2724 TL.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2725 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
2726 TL.setArgLocInfo(i,
2727 Reader.GetTemplateArgumentLocInfo(TL.getTypePtr()->getArg(i).getKind(),
Sebastian Redl577d4792010-07-22 22:43:28 +00002728 DeclsCursor, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00002729}
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002730void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002731 TL.setKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2732 TL.setQualifierRange(Reader.ReadSourceRange(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00002733}
John McCall3cb0ebd2010-03-10 03:28:59 +00002734void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
2735 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2736}
Douglas Gregor4714c122010-03-31 17:34:00 +00002737void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002738 TL.setKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2739 TL.setQualifierRange(Reader.ReadSourceRange(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00002740 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2741}
John McCall33500952010-06-11 00:33:02 +00002742void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
2743 DependentTemplateSpecializationTypeLoc TL) {
2744 TL.setKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2745 TL.setQualifierRange(Reader.ReadSourceRange(Record, Idx));
2746 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2747 TL.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2748 TL.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2749 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
2750 TL.setArgLocInfo(I,
2751 Reader.GetTemplateArgumentLocInfo(TL.getTypePtr()->getArg(I).getKind(),
Sebastian Redl577d4792010-07-22 22:43:28 +00002752 DeclsCursor, Record, Idx));
John McCall33500952010-06-11 00:33:02 +00002753}
John McCall51bd8032009-10-18 01:05:36 +00002754void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
2755 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCallc12c5bb2010-05-15 11:32:37 +00002756}
2757void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
2758 TL.setHasBaseTypeAsWritten(Record[Idx++]);
John McCall51bd8032009-10-18 01:05:36 +00002759 TL.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2760 TL.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2761 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
2762 TL.setProtocolLoc(i, SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002763}
John McCall54e14c42009-10-22 22:37:11 +00002764void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
2765 TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall54e14c42009-10-22 22:37:11 +00002766}
John McCalla1ee0c52009-10-16 21:56:05 +00002767
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002768TypeSourceInfo *ASTReader::GetTypeSourceInfo(llvm::BitstreamCursor &DeclsCursor,
Sebastian Redl577d4792010-07-22 22:43:28 +00002769 const RecordData &Record,
John McCalla1ee0c52009-10-16 21:56:05 +00002770 unsigned &Idx) {
2771 QualType InfoTy = GetType(Record[Idx++]);
2772 if (InfoTy.isNull())
2773 return 0;
2774
John McCalla93c9342009-12-07 02:54:59 +00002775 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
Sebastian Redl577d4792010-07-22 22:43:28 +00002776 TypeLocReader TLR(*this, DeclsCursor, Record, Idx);
John McCalla93c9342009-12-07 02:54:59 +00002777 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCalla1ee0c52009-10-16 21:56:05 +00002778 TLR.Visit(TL);
John McCalla93c9342009-12-07 02:54:59 +00002779 return TInfo;
John McCalla1ee0c52009-10-16 21:56:05 +00002780}
Douglas Gregor2cf26342009-04-09 22:27:44 +00002781
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002782QualType ASTReader::GetType(TypeID ID) {
John McCall0953e762009-09-24 19:53:00 +00002783 unsigned FastQuals = ID & Qualifiers::FastMask;
2784 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002785
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002786 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002787 QualType T;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002788 switch ((PredefinedTypeIDs)Index) {
2789 case PREDEF_TYPE_NULL_ID: return QualType();
2790 case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
2791 case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002792
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002793 case PREDEF_TYPE_CHAR_U_ID:
2794 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregor2cf26342009-04-09 22:27:44 +00002795 // FIXME: Check that the signedness of CharTy is correct!
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002796 T = Context->CharTy;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002797 break;
2798
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002799 case PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break;
2800 case PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break;
2801 case PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break;
2802 case PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break;
2803 case PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break;
2804 case PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break;
2805 case PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break;
2806 case PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break;
2807 case PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break;
2808 case PREDEF_TYPE_INT_ID: T = Context->IntTy; break;
2809 case PREDEF_TYPE_LONG_ID: T = Context->LongTy; break;
2810 case PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break;
2811 case PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break;
2812 case PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break;
2813 case PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break;
2814 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break;
2815 case PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break;
2816 case PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break;
2817 case PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break;
2818 case PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break;
2819 case PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break;
2820 case PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break;
2821 case PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break;
2822 case PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002823 }
2824
2825 assert(!T.isNull() && "Unknown predefined type");
John McCall0953e762009-09-24 19:53:00 +00002826 return T.withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002827 }
2828
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002829 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002830 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl07a353c2010-07-14 20:26:45 +00002831 if (TypesLoaded[Index].isNull()) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002832 TypesLoaded[Index] = ReadTypeRecord(Index);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002833 TypesLoaded[Index]->setFromAST();
Sebastian Redl30c514c2010-07-14 23:45:08 +00002834 if (DeserializationListener)
Sebastian Redl1476ed42010-07-16 16:36:56 +00002835 DeserializationListener->TypeRead(ID >> Qualifiers::FastWidth,
2836 TypesLoaded[Index]);
Sebastian Redl07a353c2010-07-14 20:26:45 +00002837 }
Mike Stump1eb44332009-09-09 15:08:12 +00002838
John McCall0953e762009-09-24 19:53:00 +00002839 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002840}
2841
John McCall833ca992009-10-29 08:12:44 +00002842TemplateArgumentLocInfo
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002843ASTReader::GetTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Sebastian Redl577d4792010-07-22 22:43:28 +00002844 llvm::BitstreamCursor &DeclsCursor,
John McCall833ca992009-10-29 08:12:44 +00002845 const RecordData &Record,
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00002846 unsigned &Index) {
John McCall833ca992009-10-29 08:12:44 +00002847 switch (Kind) {
2848 case TemplateArgument::Expression:
Sebastian Redl577d4792010-07-22 22:43:28 +00002849 return ReadExpr(DeclsCursor);
John McCall833ca992009-10-29 08:12:44 +00002850 case TemplateArgument::Type:
Sebastian Redl577d4792010-07-22 22:43:28 +00002851 return GetTypeSourceInfo(DeclsCursor, Record, Index);
Douglas Gregor788cd062009-11-11 01:00:40 +00002852 case TemplateArgument::Template: {
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00002853 SourceRange QualifierRange = ReadSourceRange(Record, Index);
2854 SourceLocation TemplateNameLoc = ReadSourceLocation(Record, Index);
2855 return TemplateArgumentLocInfo(QualifierRange, TemplateNameLoc);
Douglas Gregor788cd062009-11-11 01:00:40 +00002856 }
John McCall833ca992009-10-29 08:12:44 +00002857 case TemplateArgument::Null:
2858 case TemplateArgument::Integral:
2859 case TemplateArgument::Declaration:
2860 case TemplateArgument::Pack:
2861 return TemplateArgumentLocInfo();
2862 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002863 llvm_unreachable("unexpected template argument loc");
John McCall833ca992009-10-29 08:12:44 +00002864 return TemplateArgumentLocInfo();
2865}
2866
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00002867TemplateArgumentLoc
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002868ASTReader::ReadTemplateArgumentLoc(llvm::BitstreamCursor &DeclsCursor,
Sebastian Redl577d4792010-07-22 22:43:28 +00002869 const RecordData &Record, unsigned &Index) {
2870 TemplateArgument Arg = ReadTemplateArgument(DeclsCursor, Record, Index);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00002871
2872 if (Arg.getKind() == TemplateArgument::Expression) {
2873 if (Record[Index++]) // bool InfoHasSameExpr.
2874 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
2875 }
2876 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(Arg.getKind(),
Sebastian Redl577d4792010-07-22 22:43:28 +00002877 DeclsCursor,
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00002878 Record, Index));
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002879}
2880
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002881Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall76bd1f32010-06-01 09:23:16 +00002882 return GetDecl(ID);
2883}
2884
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002885TranslationUnitDecl *ASTReader::GetTranslationUnitDecl() {
Sebastian Redl30c514c2010-07-14 23:45:08 +00002886 if (!DeclsLoaded[0]) {
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00002887 ReadDeclRecord(0, 0);
Sebastian Redl30c514c2010-07-14 23:45:08 +00002888 if (DeserializationListener)
Sebastian Redl1476ed42010-07-16 16:36:56 +00002889 DeserializationListener->DeclRead(1, DeclsLoaded[0]);
Sebastian Redl30c514c2010-07-14 23:45:08 +00002890 }
Argyrios Kyrtzidis8871a442010-07-08 17:13:02 +00002891
2892 return cast<TranslationUnitDecl>(DeclsLoaded[0]);
2893}
2894
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002895Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002896 if (ID == 0)
2897 return 0;
2898
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002899 if (ID > DeclsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002900 Error("declaration ID out-of-range for AST file");
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002901 return 0;
2902 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002903
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002904 unsigned Index = ID - 1;
Sebastian Redl30c514c2010-07-14 23:45:08 +00002905 if (!DeclsLoaded[Index]) {
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00002906 ReadDeclRecord(Index, ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00002907 if (DeserializationListener)
2908 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
2909 }
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002910
2911 return DeclsLoaded[Index];
Douglas Gregor2cf26342009-04-09 22:27:44 +00002912}
2913
Chris Lattner887e2b32009-04-27 05:46:25 +00002914/// \brief Resolve the offset of a statement into a statement.
2915///
2916/// This operation will read a new statement from the external
2917/// source each time it is called, and is meant to be used via a
2918/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002919Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00002920 // Offset here is a global offset across the entire chain.
2921 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2922 PerFileData &F = *Chain[N - I - 1];
2923 if (Offset < F.SizeInBits) {
2924 // Since we know that this statement is part of a decl, make sure to use
2925 // the decl cursor to read it.
2926 F.DeclsCursor.JumpToBit(Offset);
2927 return ReadStmtFromStream(F.DeclsCursor);
2928 }
2929 Offset -= F.SizeInBits;
2930 }
2931 llvm_unreachable("Broken chain");
Douglas Gregor250fc9c2009-04-18 00:07:54 +00002932}
2933
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002934bool ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +00002935 llvm::SmallVectorImpl<Decl*> &Decls) {
Mike Stump1eb44332009-09-09 15:08:12 +00002936 assert(DC->hasExternalLexicalStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00002937 "DeclContext has no lexical decls in storage");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002938
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00002939 // There might be lexical decls in multiple parts of the chain, for the TU
2940 // at least.
2941 DeclContextInfos &Infos = DeclContextOffsets[DC];
2942 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
2943 I != E; ++I) {
Sebastian Redl681d7232010-07-27 00:17:23 +00002944 // IDs can be 0 if this context doesn't contain declarations.
2945 if (!I->LexicalDecls)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00002946 continue;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00002947
2948 // Load all of the declaration IDs
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002949 for (const DeclID *ID = I->LexicalDecls,
Sebastian Redl681d7232010-07-27 00:17:23 +00002950 *IDE = ID + I->NumLexicalDecls;
2951 ID != IDE; ++ID)
2952 Decls.push_back(GetDecl(*ID));
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002953 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002954
Douglas Gregor25123082009-04-22 22:34:57 +00002955 ++NumLexicalDeclContextsRead;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002956 return false;
2957}
2958
John McCall76bd1f32010-06-01 09:23:16 +00002959DeclContext::lookup_result
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002960ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +00002961 DeclarationName Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00002962 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00002963 "DeclContext has no visible decls in storage");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002964
John McCall76bd1f32010-06-01 09:23:16 +00002965 llvm::SmallVector<VisibleDeclaration, 64> Decls;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00002966 // There might be lexical decls in multiple parts of the chain, for the TU
2967 // and namespaces.
2968 DeclContextInfos &Infos = DeclContextOffsets[DC];
2969 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
2970 I != E; ++I) {
2971 uint64_t Offset = I->OffsetToVisibleDecls;
2972 if (Offset == 0)
2973 continue;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002974
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00002975 llvm::BitstreamCursor &DeclsCursor = *I->Stream;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002976
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00002977 // Keep track of where we are in the stream, then jump back there
2978 // after reading this context.
2979 SavedStreamPosition SavedPosition(DeclsCursor);
2980
2981 // Load the record containing all of the declarations visible in
2982 // this context.
2983 DeclsCursor.JumpToBit(Offset);
2984 RecordData Record;
2985 unsigned Code = DeclsCursor.ReadCode();
2986 unsigned RecCode = DeclsCursor.ReadRecord(Code, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002987 if (RecCode != DECL_CONTEXT_VISIBLE) {
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00002988 Error("Expected visible block");
2989 return DeclContext::lookup_result(DeclContext::lookup_iterator(),
2990 DeclContext::lookup_iterator());
2991 }
2992
2993 if (Record.empty())
2994 continue;
2995
2996 unsigned Idx = 0;
2997 while (Idx < Record.size()) {
2998 Decls.push_back(VisibleDeclaration());
2999 Decls.back().Name = ReadDeclarationName(Record, Idx);
3000
3001 unsigned Size = Record[Idx++];
3002 llvm::SmallVector<unsigned, 4> &LoadedDecls = Decls.back().Declarations;
3003 LoadedDecls.reserve(Size);
3004 for (unsigned J = 0; J < Size; ++J)
3005 LoadedDecls.push_back(Record[Idx++]);
3006 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003007 }
3008
Douglas Gregor25123082009-04-22 22:34:57 +00003009 ++NumVisibleDeclContextsRead;
John McCall76bd1f32010-06-01 09:23:16 +00003010
3011 SetExternalVisibleDecls(DC, Decls);
3012 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003013}
3014
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003015void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003016 assert(Consumer);
3017 while (!InterestingDecls.empty()) {
3018 DeclGroupRef DG(InterestingDecls.front());
3019 InterestingDecls.pop_front();
Sebastian Redl27372b42010-08-11 18:52:41 +00003020 Consumer->HandleInterestingDecl(DG);
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003021 }
3022}
3023
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003024void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor0af2ca42009-04-22 19:09:20 +00003025 this->Consumer = Consumer;
3026
Douglas Gregorfdd01722009-04-14 00:24:19 +00003027 if (!Consumer)
3028 return;
3029
3030 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003031 // Force deserialization of this decl, which will cause it to be queued for
3032 // passing to the consumer.
Daniel Dunbar04a0b502009-09-17 03:06:44 +00003033 GetDecl(ExternalDefinitions[I]);
Douglas Gregorfdd01722009-04-14 00:24:19 +00003034 }
Douglas Gregorc62a2fe2009-04-25 00:41:30 +00003035
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003036 PassInterestingDeclsToConsumer();
Douglas Gregorfdd01722009-04-14 00:24:19 +00003037}
3038
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003039void ASTReader::PrintStats() {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003040 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregor2cf26342009-04-09 22:27:44 +00003041
Mike Stump1eb44332009-09-09 15:08:12 +00003042 unsigned NumTypesLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003043 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall0953e762009-09-24 19:53:00 +00003044 QualType());
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003045 unsigned NumDeclsLoaded
3046 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
3047 (Decl *)0);
3048 unsigned NumIdentifiersLoaded
3049 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
3050 IdentifiersLoaded.end(),
3051 (IdentifierInfo *)0);
Mike Stump1eb44332009-09-09 15:08:12 +00003052 unsigned NumSelectorsLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003053 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
3054 SelectorsLoaded.end(),
3055 Selector());
Douglas Gregor2d41cc12009-04-13 20:50:16 +00003056
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003057 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
3058 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00003059 if (TotalNumSLocEntries)
3060 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
3061 NumSLocEntriesRead, TotalNumSLocEntries,
3062 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003063 if (!TypesLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003064 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003065 NumTypesLoaded, (unsigned)TypesLoaded.size(),
3066 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
3067 if (!DeclsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003068 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003069 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
3070 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003071 if (!IdentifiersLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003072 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003073 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
3074 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redl725cd962010-08-04 20:40:17 +00003075 if (!SelectorsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003076 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redl725cd962010-08-04 20:40:17 +00003077 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
3078 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor83941df2009-04-25 17:48:32 +00003079 if (TotalNumStatements)
3080 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
3081 NumStatementsRead, TotalNumStatements,
3082 ((float)NumStatementsRead/TotalNumStatements * 100));
3083 if (TotalNumMacros)
3084 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
3085 NumMacrosRead, TotalNumMacros,
3086 ((float)NumMacrosRead/TotalNumMacros * 100));
3087 if (TotalLexicalDeclContexts)
3088 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
3089 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
3090 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
3091 * 100));
3092 if (TotalVisibleDeclContexts)
3093 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
3094 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
3095 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
3096 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003097 if (TotalNumMethodPoolEntries) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003098 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003099 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
3100 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor83941df2009-04-25 17:48:32 +00003101 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003102 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor83941df2009-04-25 17:48:32 +00003103 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003104 std::fprintf(stderr, "\n");
3105}
3106
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003107void ASTReader::InitializeSema(Sema &S) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00003108 SemaObj = &S;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003109 S.ExternalSource = this;
3110
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00003111 // Makes sure any declarations that were deserialized "too early"
3112 // still get added to the identifier's declaration chains.
Douglas Gregor914ed9d2010-08-13 03:15:25 +00003113 if (SemaObj->TUScope) {
3114 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
3115 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(PreloadedDecls[I]));
3116 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
3117 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00003118 }
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00003119 PreloadedDecls.clear();
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003120
3121 // If there were any tentative definitions, deserialize them and add
Sebastian Redle9d12b62010-01-31 22:27:38 +00003122 // them to Sema's list of tentative definitions.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003123 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
3124 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
Sebastian Redle9d12b62010-01-31 22:27:38 +00003125 SemaObj->TentativeDefinitions.push_back(Var);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003126 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00003127
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003128 // If there were any unused file scoped decls, deserialize them and add to
3129 // Sema's list of unused file scoped decls.
3130 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
3131 DeclaratorDecl *D = cast<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
3132 SemaObj->UnusedFileScopedDecls.push_back(D);
Tanya Lattnere6bbc012010-02-12 00:07:30 +00003133 }
Douglas Gregor14c22f22009-04-22 22:18:58 +00003134
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003135 // If there were any weak undeclared identifiers, deserialize them and add to
3136 // Sema's list of weak undeclared identifiers.
3137 if (!WeakUndeclaredIdentifiers.empty()) {
3138 unsigned Idx = 0;
3139 for (unsigned I = 0, N = WeakUndeclaredIdentifiers[Idx++]; I != N; ++I) {
3140 IdentifierInfo *WeakId = GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3141 IdentifierInfo *AliasId=GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3142 SourceLocation Loc = ReadSourceLocation(WeakUndeclaredIdentifiers, Idx);
3143 bool Used = WeakUndeclaredIdentifiers[Idx++];
3144 Sema::WeakInfo WI(AliasId, Loc);
3145 WI.setUsed(Used);
3146 SemaObj->WeakUndeclaredIdentifiers.insert(std::make_pair(WeakId, WI));
3147 }
3148 }
3149
Douglas Gregor14c22f22009-04-22 22:18:58 +00003150 // If there were any locally-scoped external declarations,
3151 // deserialize them and add them to Sema's table of locally-scoped
3152 // external declarations.
3153 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
3154 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
3155 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
3156 }
Douglas Gregorb81c1702009-04-27 20:06:05 +00003157
3158 // If there were any ext_vector type declarations, deserialize them
3159 // and add them to Sema's vector of such declarations.
3160 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I)
3161 SemaObj->ExtVectorDecls.push_back(
3162 cast<TypedefDecl>(GetDecl(ExtVectorDecls[I])));
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003163
3164 // FIXME: Do VTable uses and dynamic classes deserialize too much ?
3165 // Can we cut them down before writing them ?
3166
3167 // If there were any VTable uses, deserialize the information and add it
3168 // to Sema's vector and map of VTable uses.
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003169 if (!VTableUses.empty()) {
3170 unsigned Idx = 0;
3171 for (unsigned I = 0, N = VTableUses[Idx++]; I != N; ++I) {
3172 CXXRecordDecl *Class = cast<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
3173 SourceLocation Loc = ReadSourceLocation(VTableUses, Idx);
3174 bool DefinitionRequired = VTableUses[Idx++];
3175 SemaObj->VTableUses.push_back(std::make_pair(Class, Loc));
3176 SemaObj->VTablesUsed[Class] = DefinitionRequired;
3177 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003178 }
3179
3180 // If there were any dynamic classes declarations, deserialize them
3181 // and add them to Sema's vector of such declarations.
3182 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I)
3183 SemaObj->DynamicClasses.push_back(
3184 cast<CXXRecordDecl>(GetDecl(DynamicClasses[I])));
Fariborz Jahanian32019832010-07-23 19:11:11 +00003185
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003186 // If there were any pending implicit instantiations, deserialize them
3187 // and add them to Sema's queue of such instantiations.
3188 assert(PendingImplicitInstantiations.size() % 2 == 0 &&
3189 "Expected pairs of entries");
3190 for (unsigned Idx = 0, N = PendingImplicitInstantiations.size(); Idx < N;) {
3191 ValueDecl *D=cast<ValueDecl>(GetDecl(PendingImplicitInstantiations[Idx++]));
3192 SourceLocation Loc = ReadSourceLocation(PendingImplicitInstantiations, Idx);
3193 SemaObj->PendingImplicitInstantiations.push_back(std::make_pair(D, Loc));
3194 }
3195
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003196 // Load the offsets of the declarations that Sema references.
3197 // They will be lazily deserialized when needed.
3198 if (!SemaDeclRefs.empty()) {
3199 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
3200 SemaObj->StdNamespace = SemaDeclRefs[0];
3201 SemaObj->StdBadAlloc = SemaDeclRefs[1];
3202 }
3203
Fariborz Jahanian32019832010-07-23 19:11:11 +00003204 // If there are @selector references added them to its pool. This is for
3205 // implementation of -Wselector.
Sebastian Redl725cd962010-08-04 20:40:17 +00003206 if (!ReferencedSelectorsData.empty()) {
3207 unsigned int DataSize = ReferencedSelectorsData.size()-1;
Fariborz Jahanian32019832010-07-23 19:11:11 +00003208 unsigned I = 0;
3209 while (I < DataSize) {
Sebastian Redl725cd962010-08-04 20:40:17 +00003210 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
Fariborz Jahanian32019832010-07-23 19:11:11 +00003211 SourceLocation SelLoc =
Sebastian Redl725cd962010-08-04 20:40:17 +00003212 SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
Fariborz Jahanian32019832010-07-23 19:11:11 +00003213 SemaObj->ReferencedSelectors.insert(std::make_pair(Sel, SelLoc));
3214 }
3215 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00003216}
3217
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003218IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Sebastian Redld8c5abb2010-08-02 18:30:12 +00003219 // Try to find this name within our on-disk hash tables. We start with the
3220 // most recent one, since that one contains the most up-to-date info.
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003221 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003222 ASTIdentifierLookupTable *IdTable
3223 = (ASTIdentifierLookupTable *)Chain[I]->IdentifierLookupTable;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003224 if (!IdTable)
3225 continue;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003226 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003227 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003228 if (Pos == IdTable->end())
3229 continue;
Douglas Gregor668c1a42009-04-21 22:25:48 +00003230
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003231 // Dereferencing the iterator has the effect of building the
3232 // IdentifierInfo node and populating it with the various
3233 // declarations it needs.
Sebastian Redld8c5abb2010-08-02 18:30:12 +00003234 return *Pos;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003235 }
Sebastian Redld8c5abb2010-08-02 18:30:12 +00003236 return 0;
Douglas Gregor668c1a42009-04-21 22:25:48 +00003237}
3238
Mike Stump1eb44332009-09-09 15:08:12 +00003239std::pair<ObjCMethodList, ObjCMethodList>
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003240ASTReader::ReadMethodPool(Selector Sel) {
Sebastian Redl725cd962010-08-04 20:40:17 +00003241 // Find this selector in a hash table. We want to find the most recent entry.
3242 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3243 PerFileData &F = *Chain[I];
3244 if (!F.SelectorLookupTable)
3245 continue;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003246
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003247 ASTSelectorLookupTable *PoolTable
3248 = (ASTSelectorLookupTable*)F.SelectorLookupTable;
3249 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
Sebastian Redl725cd962010-08-04 20:40:17 +00003250 if (Pos != PoolTable->end()) {
3251 ++NumSelectorsRead;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003252 // FIXME: Not quite happy with the statistics here. We probably should
3253 // disable this tracking when called via LoadSelector.
3254 // Also, should entries without methods count as misses?
3255 ++NumMethodPoolEntriesRead;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003256 ASTSelectorLookupTrait::data_type Data = *Pos;
Sebastian Redl725cd962010-08-04 20:40:17 +00003257 if (DeserializationListener)
3258 DeserializationListener->SelectorRead(Data.ID, Sel);
3259 return std::make_pair(Data.Instance, Data.Factory);
3260 }
Douglas Gregor83941df2009-04-25 17:48:32 +00003261 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003262
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003263 ++NumMethodPoolMisses;
Sebastian Redl725cd962010-08-04 20:40:17 +00003264 return std::pair<ObjCMethodList, ObjCMethodList>();
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003265}
3266
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003267void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redle58aa892010-08-04 18:21:41 +00003268 // It would be complicated to avoid reading the methods anyway. So don't.
3269 ReadMethodPool(Sel);
3270}
3271
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003272void ASTReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00003273 assert(ID && "Non-zero identifier ID required");
Douglas Gregora02b1472009-04-28 21:53:25 +00003274 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003275 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003276 if (DeserializationListener)
3277 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregor668c1a42009-04-21 22:25:48 +00003278}
3279
Douglas Gregord89275b2009-07-06 18:54:52 +00003280/// \brief Set the globally-visible declarations associated with the given
3281/// identifier.
3282///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003283/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump1eb44332009-09-09 15:08:12 +00003284/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregord89275b2009-07-06 18:54:52 +00003285/// them.
3286///
3287/// \param II an IdentifierInfo that refers to one or more globally-visible
3288/// declarations.
3289///
3290/// \param DeclIDs the set of declaration IDs with the name @p II that are
3291/// visible at global scope.
3292///
3293/// \param Nonrecursive should be true to indicate that the caller knows that
3294/// this call is non-recursive, and therefore the globally-visible declarations
3295/// will not be placed onto the pending queue.
Mike Stump1eb44332009-09-09 15:08:12 +00003296void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003297ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Douglas Gregord89275b2009-07-06 18:54:52 +00003298 const llvm::SmallVectorImpl<uint32_t> &DeclIDs,
3299 bool Nonrecursive) {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00003300 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregord89275b2009-07-06 18:54:52 +00003301 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
3302 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
3303 PII.II = II;
3304 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I)
3305 PII.DeclIDs.push_back(DeclIDs[I]);
3306 return;
3307 }
Mike Stump1eb44332009-09-09 15:08:12 +00003308
Douglas Gregord89275b2009-07-06 18:54:52 +00003309 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
3310 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
3311 if (SemaObj) {
Douglas Gregor914ed9d2010-08-13 03:15:25 +00003312 if (SemaObj->TUScope) {
3313 // Introduce this declaration into the translation-unit scope
3314 // and add it to the declaration chain for this identifier, so
3315 // that (unqualified) name lookup will find it.
3316 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(D));
3317 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
3318 }
Douglas Gregord89275b2009-07-06 18:54:52 +00003319 } else {
3320 // Queue this declaration so that it will be added to the
3321 // translation unit scope and identifier's declaration chain
3322 // once a Sema object is known.
3323 PreloadedDecls.push_back(D);
3324 }
3325 }
3326}
3327
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003328IdentifierInfo *ASTReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00003329 if (ID == 0)
3330 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003331
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00003332 if (IdentifiersLoaded.empty()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003333 Error("no identifier table in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00003334 return 0;
3335 }
Mike Stump1eb44332009-09-09 15:08:12 +00003336
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003337 assert(PP && "Forgot to set Preprocessor ?");
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00003338 ID -= 1;
3339 if (!IdentifiersLoaded[ID]) {
3340 unsigned Index = ID;
3341 const char *Str = 0;
3342 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3343 PerFileData *F = Chain[N - I - 1];
3344 if (Index < F->LocalNumIdentifiers) {
3345 uint32_t Offset = F->IdentifierOffsets[Index];
3346 Str = F->IdentifierTableData + Offset;
3347 break;
3348 }
3349 Index -= F->LocalNumIdentifiers;
3350 }
3351 assert(Str && "Broken Chain");
Douglas Gregord6595a42009-04-25 21:04:17 +00003352
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003353 // All of the strings in the AST file are preceded by a 16-bit length.
3354 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenek231bc0b2009-10-23 04:45:31 +00003355 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
3356 // unsigned integers. This is important to avoid integer overflow when
3357 // we cast them to 'unsigned'.
Ted Kremenekff1ea462009-10-23 03:57:22 +00003358 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregor02fc7512009-04-28 20:01:51 +00003359 unsigned StrLen = (((unsigned) StrLenPtr[0])
3360 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00003361 IdentifiersLoaded[ID]
Kovarththanan Rajaratnam811f4262010-03-12 10:32:27 +00003362 = &PP->getIdentifierTable().get(Str, StrLen);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003363 if (DeserializationListener)
3364 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregorafaf3082009-04-11 00:14:32 +00003365 }
Mike Stump1eb44332009-09-09 15:08:12 +00003366
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00003367 return IdentifiersLoaded[ID];
Douglas Gregor2cf26342009-04-09 22:27:44 +00003368}
3369
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003370void ASTReader::ReadSLocEntry(unsigned ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00003371 ReadSLocEntryRecord(ID);
3372}
3373
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003374Selector ASTReader::DecodeSelector(unsigned ID) {
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003375 if (ID == 0)
3376 return Selector();
Mike Stump1eb44332009-09-09 15:08:12 +00003377
Sebastian Redl725cd962010-08-04 20:40:17 +00003378 if (ID > SelectorsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003379 Error("selector ID out of range in AST file");
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003380 return Selector();
3381 }
Douglas Gregor83941df2009-04-25 17:48:32 +00003382
Sebastian Redl725cd962010-08-04 20:40:17 +00003383 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003384 // Load this selector from the selector table.
Sebastian Redl725cd962010-08-04 20:40:17 +00003385 unsigned Idx = ID - 1;
3386 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3387 PerFileData &F = *Chain[N - I - 1];
3388 if (Idx < F.LocalNumSelectors) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003389 ASTSelectorLookupTrait Trait(*this);
Sebastian Redl725cd962010-08-04 20:40:17 +00003390 SelectorsLoaded[ID - 1] =
3391 Trait.ReadKey(F.SelectorLookupTableData + F.SelectorOffsets[Idx], 0);
3392 if (DeserializationListener)
3393 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
3394 break;
3395 }
3396 Idx -= F.LocalNumSelectors;
3397 }
Douglas Gregor83941df2009-04-25 17:48:32 +00003398 }
3399
Sebastian Redl725cd962010-08-04 20:40:17 +00003400 return SelectorsLoaded[ID - 1];
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003401}
3402
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003403Selector ASTReader::GetExternalSelector(uint32_t ID) {
Douglas Gregor719770d2010-04-06 17:30:22 +00003404 return DecodeSelector(ID);
3405}
3406
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003407uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redl725cd962010-08-04 20:40:17 +00003408 // ID 0 (the null selector) is considered an external selector.
3409 return getTotalNumSelectors() + 1;
Douglas Gregor719770d2010-04-06 17:30:22 +00003410}
3411
Mike Stump1eb44332009-09-09 15:08:12 +00003412DeclarationName
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003413ASTReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003414 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
3415 switch (Kind) {
3416 case DeclarationName::Identifier:
3417 return DeclarationName(GetIdentifierInfo(Record, Idx));
3418
3419 case DeclarationName::ObjCZeroArgSelector:
3420 case DeclarationName::ObjCOneArgSelector:
3421 case DeclarationName::ObjCMultiArgSelector:
Steve Naroffa7503a72009-04-23 15:15:40 +00003422 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00003423
3424 case DeclarationName::CXXConstructorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003425 return Context->DeclarationNames.getCXXConstructorName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00003426 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00003427
3428 case DeclarationName::CXXDestructorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003429 return Context->DeclarationNames.getCXXDestructorName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00003430 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00003431
3432 case DeclarationName::CXXConversionFunctionName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003433 return Context->DeclarationNames.getCXXConversionFunctionName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00003434 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00003435
3436 case DeclarationName::CXXOperatorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003437 return Context->DeclarationNames.getCXXOperatorName(
Douglas Gregor2cf26342009-04-09 22:27:44 +00003438 (OverloadedOperatorKind)Record[Idx++]);
3439
Sean Hunt3e518bd2009-11-29 07:34:05 +00003440 case DeclarationName::CXXLiteralOperatorName:
3441 return Context->DeclarationNames.getCXXLiteralOperatorName(
3442 GetIdentifierInfo(Record, Idx));
3443
Douglas Gregor2cf26342009-04-09 22:27:44 +00003444 case DeclarationName::CXXUsingDirective:
3445 return DeclarationName::getUsingDirectiveName();
3446 }
3447
3448 // Required to silence GCC warning
3449 return DeclarationName();
3450}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003451
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003452TemplateName
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003453ASTReader::ReadTemplateName(const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003454 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
3455 switch (Kind) {
3456 case TemplateName::Template:
3457 return TemplateName(cast_or_null<TemplateDecl>(GetDecl(Record[Idx++])));
3458
3459 case TemplateName::OverloadedTemplate: {
3460 unsigned size = Record[Idx++];
3461 UnresolvedSet<8> Decls;
3462 while (size--)
3463 Decls.addDecl(cast<NamedDecl>(GetDecl(Record[Idx++])));
3464
3465 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
3466 }
3467
3468 case TemplateName::QualifiedTemplate: {
3469 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3470 bool hasTemplKeyword = Record[Idx++];
3471 TemplateDecl *Template = cast<TemplateDecl>(GetDecl(Record[Idx++]));
3472 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
3473 }
3474
3475 case TemplateName::DependentTemplate: {
3476 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3477 if (Record[Idx++]) // isIdentifier
3478 return Context->getDependentTemplateName(NNS,
3479 GetIdentifierInfo(Record, Idx));
3480 return Context->getDependentTemplateName(NNS,
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003481 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003482 }
3483 }
3484
3485 assert(0 && "Unhandled template name kind!");
3486 return TemplateName();
3487}
3488
3489TemplateArgument
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003490ASTReader::ReadTemplateArgument(llvm::BitstreamCursor &DeclsCursor,
Sebastian Redl577d4792010-07-22 22:43:28 +00003491 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003492 switch ((TemplateArgument::ArgKind)Record[Idx++]) {
3493 case TemplateArgument::Null:
3494 return TemplateArgument();
3495 case TemplateArgument::Type:
3496 return TemplateArgument(GetType(Record[Idx++]));
3497 case TemplateArgument::Declaration:
3498 return TemplateArgument(GetDecl(Record[Idx++]));
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00003499 case TemplateArgument::Integral: {
3500 llvm::APSInt Value = ReadAPSInt(Record, Idx);
3501 QualType T = GetType(Record[Idx++]);
3502 return TemplateArgument(Value, T);
3503 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003504 case TemplateArgument::Template:
3505 return TemplateArgument(ReadTemplateName(Record, Idx));
3506 case TemplateArgument::Expression:
Sebastian Redl577d4792010-07-22 22:43:28 +00003507 return TemplateArgument(ReadExpr(DeclsCursor));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003508 case TemplateArgument::Pack: {
3509 unsigned NumArgs = Record[Idx++];
3510 llvm::SmallVector<TemplateArgument, 8> Args;
3511 Args.reserve(NumArgs);
3512 while (NumArgs--)
Sebastian Redl577d4792010-07-22 22:43:28 +00003513 Args.push_back(ReadTemplateArgument(DeclsCursor, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003514 TemplateArgument TemplArg;
3515 TemplArg.setArgumentPack(Args.data(), Args.size(), /*CopyArgs=*/true);
3516 return TemplArg;
3517 }
3518 }
3519
3520 assert(0 && "Unhandled template argument kind!");
3521 return TemplateArgument();
3522}
3523
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003524TemplateParameterList *
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003525ASTReader::ReadTemplateParameterList(const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003526 SourceLocation TemplateLoc = ReadSourceLocation(Record, Idx);
3527 SourceLocation LAngleLoc = ReadSourceLocation(Record, Idx);
3528 SourceLocation RAngleLoc = ReadSourceLocation(Record, Idx);
3529
3530 unsigned NumParams = Record[Idx++];
3531 llvm::SmallVector<NamedDecl *, 16> Params;
3532 Params.reserve(NumParams);
3533 while (NumParams--)
3534 Params.push_back(cast<NamedDecl>(GetDecl(Record[Idx++])));
3535
3536 TemplateParameterList* TemplateParams =
3537 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
3538 Params.data(), Params.size(), RAngleLoc);
3539 return TemplateParams;
3540}
3541
3542void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003543ASTReader::
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003544ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs,
Sebastian Redl577d4792010-07-22 22:43:28 +00003545 llvm::BitstreamCursor &DeclsCursor,
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003546 const RecordData &Record, unsigned &Idx) {
3547 unsigned NumTemplateArgs = Record[Idx++];
3548 TemplArgs.reserve(NumTemplateArgs);
3549 while (NumTemplateArgs--)
Sebastian Redl577d4792010-07-22 22:43:28 +00003550 TemplArgs.push_back(ReadTemplateArgument(DeclsCursor, Record, Idx));
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003551}
3552
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00003553/// \brief Read a UnresolvedSet structure.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003554void ASTReader::ReadUnresolvedSet(UnresolvedSetImpl &Set,
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00003555 const RecordData &Record, unsigned &Idx) {
3556 unsigned NumDecls = Record[Idx++];
3557 while (NumDecls--) {
3558 NamedDecl *D = cast<NamedDecl>(GetDecl(Record[Idx++]));
3559 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
3560 Set.addDecl(D, AS);
3561 }
3562}
3563
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003564CXXBaseSpecifier
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003565ASTReader::ReadCXXBaseSpecifier(llvm::BitstreamCursor &DeclsCursor,
Nick Lewycky56062202010-07-26 16:56:01 +00003566 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003567 bool isVirtual = static_cast<bool>(Record[Idx++]);
3568 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
3569 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Nick Lewycky56062202010-07-26 16:56:01 +00003570 TypeSourceInfo *TInfo = GetTypeSourceInfo(DeclsCursor, Record, Idx);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003571 SourceRange Range = ReadSourceRange(Record, Idx);
Nick Lewycky56062202010-07-26 16:56:01 +00003572 return CXXBaseSpecifier(Range, isVirtual, isBaseOfClass, AS, TInfo);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003573}
3574
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003575std::pair<CXXBaseOrMemberInitializer **, unsigned>
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003576ASTReader::ReadCXXBaseOrMemberInitializers(llvm::BitstreamCursor &Cursor,
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003577 const RecordData &Record,
3578 unsigned &Idx) {
3579 CXXBaseOrMemberInitializer **BaseOrMemberInitializers = 0;
3580 unsigned NumInitializers = Record[Idx++];
3581 if (NumInitializers) {
3582 ASTContext &C = *getContext();
3583
3584 BaseOrMemberInitializers
3585 = new (C) CXXBaseOrMemberInitializer*[NumInitializers];
3586 for (unsigned i=0; i != NumInitializers; ++i) {
3587 TypeSourceInfo *BaseClassInfo = 0;
3588 bool IsBaseVirtual = false;
3589 FieldDecl *Member = 0;
3590
3591 bool IsBaseInitializer = Record[Idx++];
3592 if (IsBaseInitializer) {
3593 BaseClassInfo = GetTypeSourceInfo(Cursor, Record, Idx);
3594 IsBaseVirtual = Record[Idx++];
3595 } else {
3596 Member = cast<FieldDecl>(GetDecl(Record[Idx++]));
3597 }
3598 SourceLocation MemberLoc = ReadSourceLocation(Record, Idx);
3599 Expr *Init = ReadExpr(Cursor);
3600 FieldDecl *AnonUnionMember
3601 = cast_or_null<FieldDecl>(GetDecl(Record[Idx++]));
3602 SourceLocation LParenLoc = ReadSourceLocation(Record, Idx);
3603 SourceLocation RParenLoc = ReadSourceLocation(Record, Idx);
3604 bool IsWritten = Record[Idx++];
3605 unsigned SourceOrderOrNumArrayIndices;
3606 llvm::SmallVector<VarDecl *, 8> Indices;
3607 if (IsWritten) {
3608 SourceOrderOrNumArrayIndices = Record[Idx++];
3609 } else {
3610 SourceOrderOrNumArrayIndices = Record[Idx++];
3611 Indices.reserve(SourceOrderOrNumArrayIndices);
3612 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
3613 Indices.push_back(cast<VarDecl>(GetDecl(Record[Idx++])));
3614 }
3615
3616 CXXBaseOrMemberInitializer *BOMInit;
3617 if (IsBaseInitializer) {
3618 BOMInit = new (C) CXXBaseOrMemberInitializer(C, BaseClassInfo,
3619 IsBaseVirtual, LParenLoc,
3620 Init, RParenLoc);
3621 } else if (IsWritten) {
3622 BOMInit = new (C) CXXBaseOrMemberInitializer(C, Member, MemberLoc,
3623 LParenLoc, Init, RParenLoc);
3624 } else {
3625 BOMInit = CXXBaseOrMemberInitializer::Create(C, Member, MemberLoc,
3626 LParenLoc, Init, RParenLoc,
3627 Indices.data(),
3628 Indices.size());
3629 }
3630
3631 BOMInit->setAnonUnionMember(AnonUnionMember);
3632 BaseOrMemberInitializers[i] = BOMInit;
3633 }
3634 }
3635
3636 return std::make_pair(BaseOrMemberInitializers, NumInitializers);
3637}
3638
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003639NestedNameSpecifier *
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003640ASTReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003641 unsigned N = Record[Idx++];
3642 NestedNameSpecifier *NNS = 0, *Prev = 0;
3643 for (unsigned I = 0; I != N; ++I) {
3644 NestedNameSpecifier::SpecifierKind Kind
3645 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
3646 switch (Kind) {
3647 case NestedNameSpecifier::Identifier: {
3648 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
3649 NNS = NestedNameSpecifier::Create(*Context, Prev, II);
3650 break;
3651 }
3652
3653 case NestedNameSpecifier::Namespace: {
3654 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
3655 NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
3656 break;
3657 }
3658
3659 case NestedNameSpecifier::TypeSpec:
3660 case NestedNameSpecifier::TypeSpecWithTemplate: {
3661 Type *T = GetType(Record[Idx++]).getTypePtr();
3662 bool Template = Record[Idx++];
3663 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
3664 break;
3665 }
3666
3667 case NestedNameSpecifier::Global: {
3668 NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
3669 // No associated value, and there can't be a prefix.
3670 break;
3671 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003672 }
Argyrios Kyrtzidisd2bb2c02010-07-07 15:46:30 +00003673 Prev = NNS;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003674 }
3675 return NNS;
3676}
3677
3678SourceRange
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003679ASTReader::ReadSourceRange(const RecordData &Record, unsigned &Idx) {
Daniel Dunbar8ee59392010-06-02 15:47:10 +00003680 SourceLocation beg = SourceLocation::getFromRawEncoding(Record[Idx++]);
3681 SourceLocation end = SourceLocation::getFromRawEncoding(Record[Idx++]);
3682 return SourceRange(beg, end);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003683}
3684
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003685/// \brief Read an integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003686llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003687 unsigned BitWidth = Record[Idx++];
3688 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
3689 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
3690 Idx += NumWords;
3691 return Result;
3692}
3693
3694/// \brief Read a signed integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003695llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003696 bool isUnsigned = Record[Idx++];
3697 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
3698}
3699
Douglas Gregor17fc2232009-04-14 21:55:33 +00003700/// \brief Read a floating-point value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003701llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00003702 return llvm::APFloat(ReadAPInt(Record, Idx));
3703}
3704
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003705// \brief Read a string
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003706std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003707 unsigned Len = Record[Idx++];
Jay Foadbeaaccd2009-05-21 09:52:38 +00003708 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003709 Idx += Len;
3710 return Result;
3711}
3712
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003713CXXTemporary *ASTReader::ReadCXXTemporary(const RecordData &Record,
Chris Lattnerd2598362010-05-10 00:25:06 +00003714 unsigned &Idx) {
3715 CXXDestructorDecl *Decl = cast<CXXDestructorDecl>(GetDecl(Record[Idx++]));
3716 return CXXTemporary::Create(*Context, Decl);
3717}
3718
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003719DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00003720 return Diag(SourceLocation(), DiagID);
3721}
3722
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003723DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003724 return Diags.Report(FullSourceLoc(Loc, SourceMgr), DiagID);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003725}
Douglas Gregor025452f2009-04-17 00:04:06 +00003726
Douglas Gregor668c1a42009-04-21 22:25:48 +00003727/// \brief Retrieve the identifier table associated with the
3728/// preprocessor.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003729IdentifierTable &ASTReader::getIdentifierTable() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003730 assert(PP && "Forgot to set Preprocessor ?");
3731 return PP->getIdentifierTable();
Douglas Gregor668c1a42009-04-21 22:25:48 +00003732}
3733
Douglas Gregor025452f2009-04-17 00:04:06 +00003734/// \brief Record that the given ID maps to the given switch-case
3735/// statement.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003736void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Douglas Gregor025452f2009-04-17 00:04:06 +00003737 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
3738 SwitchCaseStmts[ID] = SC;
3739}
3740
3741/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003742SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Douglas Gregor025452f2009-04-17 00:04:06 +00003743 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
3744 return SwitchCaseStmts[ID];
3745}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00003746
3747/// \brief Record that the given label statement has been
3748/// deserialized and has the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003749void ASTReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
Mike Stump1eb44332009-09-09 15:08:12 +00003750 assert(LabelStmts.find(ID) == LabelStmts.end() &&
Douglas Gregor1de05fe2009-04-17 18:18:49 +00003751 "Deserialized label twice");
3752 LabelStmts[ID] = S;
3753
3754 // If we've already seen any goto statements that point to this
3755 // label, resolve them now.
3756 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
3757 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
3758 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
3759 Goto->second->setLabel(S);
3760 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00003761
3762 // If we've already seen any address-label statements that point to
3763 // this label, resolve them now.
3764 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
Mike Stump1eb44332009-09-09 15:08:12 +00003765 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00003766 = UnresolvedAddrLabelExprs.equal_range(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00003767 for (AddrLabelIter AddrLabel = AddrLabels.first;
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00003768 AddrLabel != AddrLabels.second; ++AddrLabel)
3769 AddrLabel->second->setLabel(S);
3770 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor1de05fe2009-04-17 18:18:49 +00003771}
3772
3773/// \brief Set the label of the given statement to the label
3774/// identified by ID.
3775///
3776/// Depending on the order in which the label and other statements
3777/// referencing that label occur, this operation may complete
3778/// immediately (updating the statement) or it may queue the
3779/// statement to be back-patched later.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003780void ASTReader::SetLabelOf(GotoStmt *S, unsigned ID) {
Douglas Gregor1de05fe2009-04-17 18:18:49 +00003781 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3782 if (Label != LabelStmts.end()) {
3783 // We've already seen this label, so set the label of the goto and
3784 // we're done.
3785 S->setLabel(Label->second);
3786 } else {
3787 // We haven't seen this label yet, so add this goto to the set of
3788 // unresolved goto statements.
3789 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
3790 }
3791}
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00003792
3793/// \brief Set the label of the given expression to the label
3794/// identified by ID.
3795///
3796/// Depending on the order in which the label and other statements
3797/// referencing that label occur, this operation may complete
3798/// immediately (updating the statement) or it may queue the
3799/// statement to be back-patched later.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003800void ASTReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00003801 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3802 if (Label != LabelStmts.end()) {
3803 // We've already seen this label, so set the label of the
3804 // label-address expression and we're done.
3805 S->setLabel(Label->second);
3806 } else {
3807 // We haven't seen this label yet, so add this label-address
3808 // expression to the set of unresolved label-address expressions.
3809 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
3810 }
3811}
Douglas Gregord89275b2009-07-06 18:54:52 +00003812
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003813void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00003814 assert(NumCurrentElementsDeserializing &&
3815 "FinishedDeserializing not paired with StartedDeserializing");
3816 if (NumCurrentElementsDeserializing == 1) {
Douglas Gregord89275b2009-07-06 18:54:52 +00003817 // If any identifiers with corresponding top-level declarations have
3818 // been loaded, load those declarations now.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00003819 while (!PendingIdentifierInfos.empty()) {
3820 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
3821 PendingIdentifierInfos.front().DeclIDs, true);
3822 PendingIdentifierInfos.pop_front();
Douglas Gregord89275b2009-07-06 18:54:52 +00003823 }
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003824
3825 // We are not in recursive loading, so it's safe to pass the "interesting"
3826 // decls to the consumer.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00003827 if (Consumer)
3828 PassInterestingDeclsToConsumer();
Douglas Gregord89275b2009-07-06 18:54:52 +00003829 }
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00003830 --NumCurrentElementsDeserializing;
Douglas Gregord89275b2009-07-06 18:54:52 +00003831}
Douglas Gregor501c1032010-08-19 00:28:17 +00003832
3833ASTReader::PerFileData::PerFileData()
3834 : StatCache(0), LocalNumSLocEntries(0), LocalNumTypes(0), TypeOffsets(0),
3835 LocalNumDecls(0), DeclOffsets(0), LocalNumIdentifiers(0),
3836 IdentifierOffsets(0), IdentifierTableData(0), IdentifierLookupTable(0),
3837 LocalNumMacroDefinitions(0), MacroDefinitionOffsets(0),
3838 NumPreallocatedPreprocessingEntities(0), SelectorLookupTable(0),
3839 SelectorLookupTableData(0), SelectorOffsets(0), LocalNumSelectors(0)
3840{}
3841
3842ASTReader::PerFileData::~PerFileData() {
3843 delete static_cast<ASTIdentifierLookupTable *>(IdentifierLookupTable);
3844 delete static_cast<ASTSelectorLookupTable *>(SelectorLookupTable);
3845}
3846