blob: 040277074c221ef371c6f5f57cf88bbd43b4a775 [file] [log] [blame]
Sebastian Redl904c9c82010-08-18 23:57:11 +00001//===--- ASTReader.cpp - AST File Reader ------------------------*- C++ -*-===//
Douglas Gregor2cf26342009-04-09 22:27:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Sebastian Redlc43b54c2010-08-18 23:56:43 +000010// This file defines the ASTReader class, which reads AST files.
Douglas Gregor2cf26342009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
Chris Lattner4c6f9522009-04-27 05:14:47 +000013
Sebastian Redl6ab7cd82010-08-18 23:57:17 +000014#include "clang/Serialization/ASTReader.h"
15#include "clang/Serialization/ASTDeserializationListener.h"
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +000016#include "ASTCommon.h"
Douglas Gregor0a0428e2009-04-10 20:39:37 +000017#include "clang/Frontend/FrontendDiagnostic.h"
Daniel Dunbarc7162932009-11-11 23:58:53 +000018#include "clang/Frontend/Utils.h"
Douglas Gregore737f502010-08-12 20:07:10 +000019#include "clang/Sema/Sema.h"
Douglas Gregorfdd01722009-04-14 00:24:19 +000020#include "clang/AST/ASTConsumer.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000021#include "clang/AST/ASTContext.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000022#include "clang/AST/Expr.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000023#include "clang/AST/Type.h"
John McCalla1ee0c52009-10-16 21:56:05 +000024#include "clang/AST/TypeLocVisitor.h"
Chris Lattner42d42b52009-04-10 21:41:48 +000025#include "clang/Lex/MacroInfo.h"
Douglas Gregor6a5a23f2010-03-19 21:51:54 +000026#include "clang/Lex/PreprocessingRecord.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000027#include "clang/Lex/Preprocessor.h"
Steve Naroff83d63c72009-04-24 20:03:17 +000028#include "clang/Lex/HeaderSearch.h"
Douglas Gregor668c1a42009-04-21 22:25:48 +000029#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000030#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000031#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000032#include "clang/Basic/FileManager.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000033#include "clang/Basic/TargetInfo.h"
Douglas Gregor445e23e2009-10-05 21:07:28 +000034#include "clang/Basic/Version.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000035#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000036#include "llvm/Bitcode/BitstreamReader.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000037#include "llvm/Support/MemoryBuffer.h"
John McCall833ca992009-10-29 08:12:44 +000038#include "llvm/Support/ErrorHandling.h"
Daniel Dunbard5b21972009-11-18 19:50:41 +000039#include "llvm/System/Path.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000040#include <algorithm>
Douglas Gregore721f952009-04-28 18:58:38 +000041#include <iterator>
Douglas Gregor2cf26342009-04-09 22:27:44 +000042#include <cstdio>
Douglas Gregor4fed3f42009-04-27 18:38:38 +000043#include <sys/stat.h>
Douglas Gregor2cf26342009-04-09 22:27:44 +000044using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000045using namespace clang::serialization;
Douglas Gregor2cf26342009-04-09 22:27:44 +000046
47//===----------------------------------------------------------------------===//
Sebastian Redl3c7f4132010-08-18 23:57:06 +000048// PCH validator implementation
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000049//===----------------------------------------------------------------------===//
50
Sebastian Redl571db7f2010-08-18 23:56:56 +000051ASTReaderListener::~ASTReaderListener() {}
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000052
53bool
54PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts) {
55 const LangOptions &PPLangOpts = PP.getLangOptions();
56#define PARSE_LANGOPT_BENIGN(Option)
57#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
58 if (PPLangOpts.Option != LangOpts.Option) { \
59 Reader.Diag(DiagID) << LangOpts.Option << PPLangOpts.Option; \
60 return true; \
61 }
62
63 PARSE_LANGOPT_BENIGN(Trigraphs);
64 PARSE_LANGOPT_BENIGN(BCPLComment);
65 PARSE_LANGOPT_BENIGN(DollarIdents);
66 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
67 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
Chandler Carrutheb5d7b72010-04-17 20:17:31 +000068 PARSE_LANGOPT_IMPORTANT(GNUKeywords, diag::warn_pch_gnu_keywords);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000069 PARSE_LANGOPT_BENIGN(ImplicitInt);
70 PARSE_LANGOPT_BENIGN(Digraphs);
71 PARSE_LANGOPT_BENIGN(HexFloats);
72 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
73 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
74 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
75 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
76 PARSE_LANGOPT_BENIGN(CXXOperatorName);
77 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
78 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
79 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
Fariborz Jahanian412e7982010-02-09 19:31:38 +000080 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI2, diag::warn_pch_nonfragile_abi2);
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +000081 PARSE_LANGOPT_IMPORTANT(NoConstantCFStrings,
82 diag::warn_pch_no_constant_cfstrings);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000083 PARSE_LANGOPT_BENIGN(PascalStrings);
84 PARSE_LANGOPT_BENIGN(WritableStrings);
Mike Stump1eb44332009-09-09 15:08:12 +000085 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000086 diag::warn_pch_lax_vector_conversions);
Nate Begeman69cfb9b2009-06-25 22:57:40 +000087 PARSE_LANGOPT_IMPORTANT(AltiVec, diag::warn_pch_altivec);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000088 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
Daniel Dunbar73482882010-02-10 18:48:44 +000089 PARSE_LANGOPT_IMPORTANT(SjLjExceptions, diag::warn_pch_sjlj_exceptions);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000090 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
91 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
92 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
Mike Stump1eb44332009-09-09 15:08:12 +000093 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000094 diag::warn_pch_thread_safe_statics);
Daniel Dunbar5345c392009-09-03 04:54:28 +000095 PARSE_LANGOPT_IMPORTANT(POSIXThreads, diag::warn_pch_posix_threads);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000096 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
97 PARSE_LANGOPT_BENIGN(EmitAllDecls);
98 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
Chris Lattnera4d71452010-06-26 21:25:03 +000099 PARSE_LANGOPT_BENIGN(getSignedOverflowBehavior());
Mike Stump1eb44332009-09-09 15:08:12 +0000100 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000101 diag::warn_pch_heinous_extensions);
102 // FIXME: Most of the options below are benign if the macro wasn't
103 // used. Unfortunately, this means that a PCH compiled without
104 // optimization can't be used with optimization turned on, even
105 // though the only thing that changes is whether __OPTIMIZE__ was
106 // defined... but if __OPTIMIZE__ never showed up in the header, it
107 // doesn't matter. We could consider making this some special kind
108 // of check.
109 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
110 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
111 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
112 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
113 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
114 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
115 PARSE_LANGOPT_IMPORTANT(AccessControl, diag::warn_pch_access_control);
116 PARSE_LANGOPT_IMPORTANT(CharIsSigned, diag::warn_pch_char_signed);
John Thompsona6fda122009-11-05 20:14:16 +0000117 PARSE_LANGOPT_IMPORTANT(ShortWChar, diag::warn_pch_short_wchar);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000118 if ((PPLangOpts.getGCMode() != 0) != (LangOpts.getGCMode() != 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000119 Reader.Diag(diag::warn_pch_gc_mode)
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000120 << LangOpts.getGCMode() << PPLangOpts.getGCMode();
121 return true;
122 }
123 PARSE_LANGOPT_BENIGN(getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000124 PARSE_LANGOPT_IMPORTANT(getStackProtectorMode(),
125 diag::warn_pch_stack_protector);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000126 PARSE_LANGOPT_BENIGN(InstantiationDepth);
Nate Begeman69cfb9b2009-06-25 22:57:40 +0000127 PARSE_LANGOPT_IMPORTANT(OpenCL, diag::warn_pch_opencl);
Mike Stump9c276ae2009-12-12 01:27:46 +0000128 PARSE_LANGOPT_BENIGN(CatchUndefined);
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000129 PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors);
Douglas Gregora0068fc2010-07-09 17:35:33 +0000130 PARSE_LANGOPT_BENIGN(SpellChecking);
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +0000131#undef PARSE_LANGOPT_IMPORTANT
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000132#undef PARSE_LANGOPT_BENIGN
133
134 return false;
135}
136
Daniel Dunbardc3c0d22009-11-11 00:52:11 +0000137bool PCHValidator::ReadTargetTriple(llvm::StringRef Triple) {
138 if (Triple == PP.getTargetInfo().getTriple().str())
139 return false;
140
141 Reader.Diag(diag::warn_pch_target_triple)
142 << Triple << PP.getTargetInfo().getTriple().str();
143 return true;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000144}
145
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000146struct EmptyStringRef {
Benjamin Kramerec1b1cc2010-07-14 23:19:41 +0000147 bool operator ()(llvm::StringRef r) const { return r.empty(); }
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000148};
149struct EmptyBlock {
150 bool operator ()(const PCHPredefinesBlock &r) const { return r.Data.empty(); }
151};
152
153static bool EqualConcatenations(llvm::SmallVector<llvm::StringRef, 2> L,
154 PCHPredefinesBlocks R) {
155 // First, sum up the lengths.
156 unsigned LL = 0, RL = 0;
157 for (unsigned I = 0, N = L.size(); I != N; ++I) {
158 LL += L[I].size();
159 }
160 for (unsigned I = 0, N = R.size(); I != N; ++I) {
161 RL += R[I].Data.size();
162 }
163 if (LL != RL)
164 return false;
165 if (LL == 0 && RL == 0)
166 return true;
167
168 // Kick out empty parts, they confuse the algorithm below.
169 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
170 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
171
172 // Do it the hard way. At this point, both vectors must be non-empty.
173 llvm::StringRef LR = L[0], RR = R[0].Data;
174 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
Daniel Dunbarc76c9e02010-07-16 00:00:11 +0000175 (void) RN;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000176 for (;;) {
177 // Compare the current pieces.
178 if (LR.size() == RR.size()) {
179 // If they're the same length, it's pretty easy.
180 if (LR != RR)
181 return false;
182 // Both pieces are done, advance.
183 ++LI;
184 ++RI;
185 // If either string is done, they're both done, since they're the same
186 // length.
187 if (LI == LN) {
188 assert(RI == RN && "Strings not the same length after all?");
189 return true;
190 }
191 LR = L[LI];
192 RR = R[RI].Data;
193 } else if (LR.size() < RR.size()) {
194 // Right piece is longer.
195 if (!RR.startswith(LR))
196 return false;
197 ++LI;
198 assert(LI != LN && "Strings not the same length after all?");
199 RR = RR.substr(LR.size());
200 LR = L[LI];
201 } else {
202 // Left piece is longer.
203 if (!LR.startswith(RR))
204 return false;
205 ++RI;
206 assert(RI != RN && "Strings not the same length after all?");
207 LR = LR.substr(RR.size());
208 RR = R[RI].Data;
209 }
210 }
211}
212
213static std::pair<FileID, llvm::StringRef::size_type>
214FindMacro(const PCHPredefinesBlocks &Buffers, llvm::StringRef MacroDef) {
215 std::pair<FileID, llvm::StringRef::size_type> Res;
216 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
217 Res.second = Buffers[I].Data.find(MacroDef);
218 if (Res.second != llvm::StringRef::npos) {
219 Res.first = Buffers[I].BufferID;
220 break;
221 }
222 }
223 return Res;
224}
225
226bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000227 llvm::StringRef OriginalFileName,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000228 std::string &SuggestedPredefines) {
Daniel Dunbarc7162932009-11-11 23:58:53 +0000229 // We are in the context of an implicit include, so the predefines buffer will
230 // have a #include entry for the PCH file itself (as normalized by the
231 // preprocessor initialization). Find it and skip over it in the checking
232 // below.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000233 llvm::SmallString<256> PCHInclude;
234 PCHInclude += "#include \"";
Daniel Dunbarc7162932009-11-11 23:58:53 +0000235 PCHInclude += NormalizeDashIncludePath(OriginalFileName);
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000236 PCHInclude += "\"\n";
237 std::pair<llvm::StringRef,llvm::StringRef> Split =
238 llvm::StringRef(PP.getPredefines()).split(PCHInclude.str());
239 llvm::StringRef Left = Split.first, Right = Split.second;
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000240 if (Left == PP.getPredefines()) {
241 Error("Missing PCH include entry!");
242 return true;
243 }
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000244
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000245 // If the concatenation of all the PCH buffers is equal to the adjusted
246 // command line, we're done.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000247 llvm::SmallVector<llvm::StringRef, 2> CommandLine;
248 CommandLine.push_back(Left);
249 CommandLine.push_back(Right);
250 if (EqualConcatenations(CommandLine, Buffers))
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000251 return false;
252
253 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000254
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000255 // The predefines buffers are different. Determine what the differences are,
256 // and whether they require us to reject the PCH file.
Daniel Dunbare6750492009-11-13 16:46:11 +0000257 llvm::SmallVector<llvm::StringRef, 8> PCHLines;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000258 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
259 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Daniel Dunbare6750492009-11-13 16:46:11 +0000260
261 llvm::SmallVector<llvm::StringRef, 8> CmdLineLines;
262 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
263 Right.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000264
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000265 // Sort both sets of predefined buffer lines, since we allow some extra
266 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000267 std::sort(CmdLineLines.begin(), CmdLineLines.end());
268 std::sort(PCHLines.begin(), PCHLines.end());
269
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000270 // Determine which predefines that were used to build the PCH file are missing
271 // from the command line.
272 std::vector<llvm::StringRef> MissingPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000273 std::set_difference(PCHLines.begin(), PCHLines.end(),
274 CmdLineLines.begin(), CmdLineLines.end(),
275 std::back_inserter(MissingPredefines));
276
277 bool MissingDefines = false;
278 bool ConflictingDefines = false;
279 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000280 llvm::StringRef Missing = MissingPredefines[I];
281 if (!Missing.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000282 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
283 return true;
284 }
Mike Stump1eb44332009-09-09 15:08:12 +0000285
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000286 // This is a macro definition. Determine the name of the macro we're
287 // defining.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000288 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000289 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000290 = Missing.find_first_of("( \n\r", StartOfMacroName);
291 assert(EndOfMacroName != std::string::npos &&
292 "Couldn't find the end of the macro name");
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000293 llvm::StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000294
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000295 // Determine whether this macro was given a different definition on the
296 // command line.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000297 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000298 std::string::size_type MacroDefLen = MacroDefStart.size();
Daniel Dunbare6750492009-11-13 16:46:11 +0000299 llvm::SmallVector<llvm::StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000300 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
301 MacroDefStart);
302 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000303 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000304 // Different macro; we're done.
305 ConflictPos = CmdLineLines.end();
Mike Stump1eb44332009-09-09 15:08:12 +0000306 break;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000307 }
Mike Stump1eb44332009-09-09 15:08:12 +0000308
309 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000310 "Invalid #define in predefines buffer?");
Mike Stump1eb44332009-09-09 15:08:12 +0000311 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000312 (*ConflictPos)[MacroDefLen] != '(')
313 continue; // Longer macro name; keep trying.
Mike Stump1eb44332009-09-09 15:08:12 +0000314
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000315 // We found a conflicting macro definition.
316 break;
317 }
Mike Stump1eb44332009-09-09 15:08:12 +0000318
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000319 if (ConflictPos != CmdLineLines.end()) {
320 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
321 << MacroName;
322
323 // Show the definition of this macro within the PCH file.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000324 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
325 FindMacro(Buffers, Missing);
326 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
327 SourceLocation PCHMissingLoc =
328 SourceMgr.getLocForStartOfFile(MacroLoc.first)
329 .getFileLocWithOffset(MacroLoc.second);
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000330 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000331
332 ConflictingDefines = true;
333 continue;
334 }
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000336 // If the macro doesn't conflict, then we'll just pick up the macro
337 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000338 if (ConflictingDefines)
339 continue; // Don't complain if there are already conflicting defs
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000341 if (!MissingDefines) {
342 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
343 MissingDefines = true;
344 }
345
346 // Show the definition of this macro within the PCH file.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000347 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
348 FindMacro(Buffers, Missing);
349 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
350 SourceLocation PCHMissingLoc =
351 SourceMgr.getLocForStartOfFile(MacroLoc.first)
352 .getFileLocWithOffset(MacroLoc.second);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000353 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
354 }
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000356 if (ConflictingDefines)
357 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000359 // Determine what predefines were introduced based on command-line
360 // parameters that were not present when building the PCH
361 // file. Extra #defines are okay, so long as the identifiers being
362 // defined were not used within the precompiled header.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000363 std::vector<llvm::StringRef> ExtraPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000364 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
365 PCHLines.begin(), PCHLines.end(),
Mike Stump1eb44332009-09-09 15:08:12 +0000366 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000367 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000368 llvm::StringRef &Extra = ExtraPredefines[I];
369 if (!Extra.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000370 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
371 return true;
372 }
373
374 // This is an extra macro definition. Determine the name of the
375 // macro we're defining.
376 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000377 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000378 = Extra.find_first_of("( \n\r", StartOfMacroName);
379 assert(EndOfMacroName != std::string::npos &&
380 "Couldn't find the end of the macro name");
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000381 llvm::StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000382
383 // Check whether this name was used somewhere in the PCH file. If
384 // so, defining it as a macro could change behavior, so we reject
385 // the PCH file.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000386 if (IdentifierInfo *II = Reader.get(MacroName)) {
Daniel Dunbar4fda42e2009-11-11 00:52:00 +0000387 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000388 return true;
389 }
390
391 // Add this definition to the suggested predefines buffer.
392 SuggestedPredefines += Extra;
393 SuggestedPredefines += '\n';
394 }
395
396 // If we get here, it's because the predefines buffer had compatible
397 // contents. Accept the PCH file.
398 return false;
399}
400
Douglas Gregor12fab312010-03-16 16:35:32 +0000401void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
402 unsigned ID) {
403 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
404 ++NumHeaderInfos;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000405}
406
407void PCHValidator::ReadCounter(unsigned Value) {
408 PP.setCounterValue(Value);
409}
410
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000411//===----------------------------------------------------------------------===//
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000412// AST reader implementation
Douglas Gregor668c1a42009-04-21 22:25:48 +0000413//===----------------------------------------------------------------------===//
414
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000415ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
Douglas Gregorfae3b2f2010-07-27 00:27:13 +0000416 const char *isysroot, bool DisableValidation)
Sebastian Redl30c514c2010-07-14 23:45:08 +0000417 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
418 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
419 Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
Sebastian Redl725cd962010-08-04 20:40:17 +0000420 Consumer(0), isysroot(isysroot), DisableValidation(DisableValidation),
421 NumStatHits(0), NumStatMisses(0), NumSLocEntriesRead(0),
422 TotalNumSLocEntries(0), NumStatementsRead(0), TotalNumStatements(0),
Sebastian Redlfa78dec2010-08-04 21:22:45 +0000423 NumMacrosRead(0), TotalNumMacros(0), NumSelectorsRead(0),
424 NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
425 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
Sebastian Redl725cd962010-08-04 20:40:17 +0000426 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
427 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000428 RelocatablePCH = false;
429}
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000430
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000431ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
Douglas Gregorfae3b2f2010-07-27 00:27:13 +0000432 Diagnostic &Diags, const char *isysroot,
433 bool DisableValidation)
Sebastian Redl30c514c2010-07-14 23:45:08 +0000434 : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
Sebastian Redl9137a522010-07-16 17:50:48 +0000435 Diags(Diags), SemaObj(0), PP(0), Context(0), Consumer(0),
Sebastian Redl725cd962010-08-04 20:40:17 +0000436 isysroot(isysroot), DisableValidation(DisableValidation), NumStatHits(0),
437 NumStatMisses(0), NumSLocEntriesRead(0), TotalNumSLocEntries(0),
438 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
Sebastian Redlfa78dec2010-08-04 21:22:45 +0000439 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
440 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
Sebastian Redl725cd962010-08-04 20:40:17 +0000441 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
442 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
443 NumCurrentElementsDeserializing(0) {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000444 RelocatablePCH = false;
445}
Chris Lattner4c6f9522009-04-27 05:14:47 +0000446
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000447ASTReader::~ASTReader() {
Sebastian Redl9137a522010-07-16 17:50:48 +0000448 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
449 delete Chain[e - i - 1];
450}
451
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000452void
Sebastian Redl571db7f2010-08-18 23:56:56 +0000453ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000454 DeserializationListener = Listener;
455 if (DeserializationListener)
456 DeserializationListener->SetReader(this);
457}
458
Chris Lattner4c6f9522009-04-27 05:14:47 +0000459
Douglas Gregor668c1a42009-04-21 22:25:48 +0000460namespace {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000461class ASTSelectorLookupTrait {
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000462 ASTReader &Reader;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000463
464public:
Sebastian Redl5d050072010-08-04 17:20:04 +0000465 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000466 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +0000467 ObjCMethodList Instance, Factory;
468 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000469
470 typedef Selector external_key_type;
471 typedef external_key_type internal_key_type;
472
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000473 explicit ASTSelectorLookupTrait(ASTReader &Reader) : Reader(Reader) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000474
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000475 static bool EqualKey(const internal_key_type& a,
476 const internal_key_type& b) {
477 return a == b;
478 }
Mike Stump1eb44332009-09-09 15:08:12 +0000479
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000480 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +0000481 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000482 }
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000484 // This hopefully will just get inlined and removed by the optimizer.
485 static const internal_key_type&
486 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000488 static std::pair<unsigned, unsigned>
489 ReadKeyDataLength(const unsigned char*& d) {
490 using namespace clang::io;
491 unsigned KeyLen = ReadUnalignedLE16(d);
492 unsigned DataLen = ReadUnalignedLE16(d);
493 return std::make_pair(KeyLen, DataLen);
494 }
Mike Stump1eb44332009-09-09 15:08:12 +0000495
Douglas Gregor83941df2009-04-25 17:48:32 +0000496 internal_key_type ReadKey(const unsigned char* d, unsigned) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000497 using namespace clang::io;
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000498 SelectorTable &SelTable = Reader.getContext()->Selectors;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000499 unsigned N = ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +0000500 IdentifierInfo *FirstII
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000501 = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
502 if (N == 0)
503 return SelTable.getNullarySelector(FirstII);
504 else if (N == 1)
505 return SelTable.getUnarySelector(FirstII);
506
507 llvm::SmallVector<IdentifierInfo *, 16> Args;
508 Args.push_back(FirstII);
509 for (unsigned I = 1; I != N; ++I)
510 Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)));
511
Douglas Gregor75fdb232009-05-22 22:45:36 +0000512 return SelTable.getSelector(N, Args.data());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000513 }
Mike Stump1eb44332009-09-09 15:08:12 +0000514
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000515 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
516 using namespace clang::io;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000517
518 data_type Result;
519
Sebastian Redl5d050072010-08-04 17:20:04 +0000520 Result.ID = ReadUnalignedLE32(d);
521 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
522 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
523
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000524 // Load instance methods
525 ObjCMethodList *Prev = 0;
526 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000527 ObjCMethodDecl *Method
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000528 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl5d050072010-08-04 17:20:04 +0000529 if (!Result.Instance.Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000530 // This is the first method, which is the easy case.
Sebastian Redl5d050072010-08-04 17:20:04 +0000531 Result.Instance.Method = Method;
532 Prev = &Result.Instance;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000533 continue;
534 }
535
Ted Kremenek298ed872010-02-11 00:53:01 +0000536 ObjCMethodList *Mem =
537 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
538 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000539 Prev = Prev->Next;
540 }
541
542 // Load factory methods
543 Prev = 0;
544 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000545 ObjCMethodDecl *Method
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000546 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl5d050072010-08-04 17:20:04 +0000547 if (!Result.Factory.Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000548 // This is the first method, which is the easy case.
Sebastian Redl5d050072010-08-04 17:20:04 +0000549 Result.Factory.Method = Method;
550 Prev = &Result.Factory;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000551 continue;
552 }
553
Ted Kremenek298ed872010-02-11 00:53:01 +0000554 ObjCMethodList *Mem =
555 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
556 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000557 Prev = Prev->Next;
558 }
559
560 return Result;
561 }
562};
Mike Stump1eb44332009-09-09 15:08:12 +0000563
564} // end anonymous namespace
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000565
566/// \brief The on-disk hash table used for the global method pool.
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000567typedef OnDiskChainedHashTable<ASTSelectorLookupTrait>
568 ASTSelectorLookupTable;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000569
570namespace {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000571class ASTIdentifierLookupTrait {
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000572 ASTReader &Reader;
Sebastian Redld27d3fc2010-07-21 22:31:37 +0000573 llvm::BitstreamCursor &Stream;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000574
575 // If we know the IdentifierInfo in advance, it is here and we will
576 // not build a new one. Used when deserializing information about an
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000577 // identifier that was constructed before the AST file was read.
Douglas Gregor668c1a42009-04-21 22:25:48 +0000578 IdentifierInfo *KnownII;
579
580public:
581 typedef IdentifierInfo * data_type;
582
583 typedef const std::pair<const char*, unsigned> external_key_type;
584
585 typedef external_key_type internal_key_type;
586
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000587 ASTIdentifierLookupTrait(ASTReader &Reader, llvm::BitstreamCursor &Stream,
Sebastian Redld27d3fc2010-07-21 22:31:37 +0000588 IdentifierInfo *II = 0)
589 : Reader(Reader), Stream(Stream), KnownII(II) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000590
Douglas Gregor668c1a42009-04-21 22:25:48 +0000591 static bool EqualKey(const internal_key_type& a,
592 const internal_key_type& b) {
593 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
594 : false;
595 }
Mike Stump1eb44332009-09-09 15:08:12 +0000596
Douglas Gregor668c1a42009-04-21 22:25:48 +0000597 static unsigned ComputeHash(const internal_key_type& a) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000598 return llvm::HashString(llvm::StringRef(a.first, a.second));
Douglas Gregor668c1a42009-04-21 22:25:48 +0000599 }
Mike Stump1eb44332009-09-09 15:08:12 +0000600
Douglas Gregor668c1a42009-04-21 22:25:48 +0000601 // This hopefully will just get inlined and removed by the optimizer.
602 static const internal_key_type&
603 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump1eb44332009-09-09 15:08:12 +0000604
Douglas Gregor668c1a42009-04-21 22:25:48 +0000605 static std::pair<unsigned, unsigned>
606 ReadKeyDataLength(const unsigned char*& d) {
607 using namespace clang::io;
Douglas Gregor5f8e3302009-04-25 20:26:24 +0000608 unsigned DataLen = ReadUnalignedLE16(d);
Douglas Gregord6595a42009-04-25 21:04:17 +0000609 unsigned KeyLen = ReadUnalignedLE16(d);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000610 return std::make_pair(KeyLen, DataLen);
611 }
Mike Stump1eb44332009-09-09 15:08:12 +0000612
Douglas Gregor668c1a42009-04-21 22:25:48 +0000613 static std::pair<const char*, unsigned>
614 ReadKey(const unsigned char* d, unsigned n) {
615 assert(n >= 2 && d[n-1] == '\0');
616 return std::make_pair((const char*) d, n-1);
617 }
Mike Stump1eb44332009-09-09 15:08:12 +0000618
619 IdentifierInfo *ReadData(const internal_key_type& k,
Douglas Gregor668c1a42009-04-21 22:25:48 +0000620 const unsigned char* d,
621 unsigned DataLen) {
622 using namespace clang::io;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000623 IdentID ID = ReadUnalignedLE32(d);
Douglas Gregora92193e2009-04-28 21:18:29 +0000624 bool IsInteresting = ID & 0x01;
625
626 // Wipe out the "is interesting" bit.
627 ID = ID >> 1;
628
629 if (!IsInteresting) {
Sebastian Redl083abdf2010-07-27 23:01:28 +0000630 // For uninteresting identifiers, just build the IdentifierInfo
Douglas Gregora92193e2009-04-28 21:18:29 +0000631 // and associate it with the persistent ID.
632 IdentifierInfo *II = KnownII;
633 if (!II)
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000634 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregora92193e2009-04-28 21:18:29 +0000635 Reader.SetIdentifierInfo(ID, II);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000636 II->setIsFromAST();
Douglas Gregora92193e2009-04-28 21:18:29 +0000637 return II;
638 }
639
Douglas Gregor5998da52009-04-28 21:32:13 +0000640 unsigned Bits = ReadUnalignedLE16(d);
Douglas Gregor2deaea32009-04-22 18:49:13 +0000641 bool CPlusPlusOperatorKeyword = Bits & 0x01;
642 Bits >>= 1;
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000643 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
644 Bits >>= 1;
Douglas Gregor2deaea32009-04-22 18:49:13 +0000645 bool Poisoned = Bits & 0x01;
646 Bits >>= 1;
647 bool ExtensionToken = Bits & 0x01;
648 Bits >>= 1;
649 bool hasMacroDefinition = Bits & 0x01;
650 Bits >>= 1;
651 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
652 Bits >>= 10;
Mike Stump1eb44332009-09-09 15:08:12 +0000653
Douglas Gregor2deaea32009-04-22 18:49:13 +0000654 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregor5998da52009-04-28 21:32:13 +0000655 DataLen -= 6;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000656
657 // Build the IdentifierInfo itself and link the identifier ID with
658 // the new IdentifierInfo.
659 IdentifierInfo *II = KnownII;
660 if (!II)
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000661 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000662 Reader.SetIdentifierInfo(ID, II);
663
Douglas Gregor2deaea32009-04-22 18:49:13 +0000664 // Set or check the various bits in the IdentifierInfo structure.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000665 // Token IDs are read-only.
666 if (HasRevertedTokenIDToIdentifier)
667 II->RevertTokenIDToIdentifier();
Douglas Gregor2deaea32009-04-22 18:49:13 +0000668 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
Mike Stump1eb44332009-09-09 15:08:12 +0000669 assert(II->isExtensionToken() == ExtensionToken &&
Douglas Gregor2deaea32009-04-22 18:49:13 +0000670 "Incorrect extension token flag");
671 (void)ExtensionToken;
672 II->setIsPoisoned(Poisoned);
673 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
674 "Incorrect C++ operator keyword flag");
675 (void)CPlusPlusOperatorKeyword;
676
Douglas Gregor37e26842009-04-21 23:56:24 +0000677 // If this identifier is a macro, deserialize the macro
678 // definition.
679 if (hasMacroDefinition) {
Douglas Gregor5998da52009-04-28 21:32:13 +0000680 uint32_t Offset = ReadUnalignedLE32(d);
Sebastian Redld27d3fc2010-07-21 22:31:37 +0000681 Reader.ReadMacroRecord(Stream, Offset);
Douglas Gregor5998da52009-04-28 21:32:13 +0000682 DataLen -= 4;
Douglas Gregor37e26842009-04-21 23:56:24 +0000683 }
Douglas Gregor668c1a42009-04-21 22:25:48 +0000684
685 // Read all of the declarations visible at global scope with this
686 // name.
Chris Lattner6bf690f2009-04-27 22:17:41 +0000687 if (Reader.getContext() == 0) return II;
Douglas Gregord89275b2009-07-06 18:54:52 +0000688 if (DataLen > 0) {
689 llvm::SmallVector<uint32_t, 4> DeclIDs;
690 for (; DataLen > 0; DataLen -= 4)
691 DeclIDs.push_back(ReadUnalignedLE32(d));
692 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000693 }
Mike Stump1eb44332009-09-09 15:08:12 +0000694
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000695 II->setIsFromAST();
Douglas Gregor668c1a42009-04-21 22:25:48 +0000696 return II;
697 }
698};
Mike Stump1eb44332009-09-09 15:08:12 +0000699
700} // end anonymous namespace
Douglas Gregor668c1a42009-04-21 22:25:48 +0000701
702/// \brief The on-disk hash table used to contain information about
703/// all of the identifiers in the program.
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000704typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait>
705 ASTIdentifierLookupTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000706
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000707void ASTReader::Error(const char *Msg) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000708 Diag(diag::err_fe_pch_malformed) << Msg;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000709}
710
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000711/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000712bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000713 if (Listener)
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000714 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000715 ActualOriginalFileName,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000716 SuggestedPredefines);
Douglas Gregore721f952009-04-28 18:58:38 +0000717 return false;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000718}
719
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000720//===----------------------------------------------------------------------===//
721// Source Manager Deserialization
722//===----------------------------------------------------------------------===//
723
Douglas Gregorbd945002009-04-13 16:31:14 +0000724/// \brief Read the line table in the source manager block.
725/// \returns true if ther was an error.
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000726bool ASTReader::ParseLineTable(llvm::SmallVectorImpl<uint64_t> &Record) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000727 unsigned Idx = 0;
728 LineTableInfo &LineTable = SourceMgr.getLineTable();
729
730 // Parse the file names
Douglas Gregorff0a9872009-04-13 17:12:42 +0000731 std::map<int, int> FileIDs;
732 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000733 // Extract the file name
734 unsigned FilenameLen = Record[Idx++];
735 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
736 Idx += FilenameLen;
Douglas Gregore650c8c2009-07-07 00:12:59 +0000737 MaybeAddSystemRootToFilename(Filename);
Mike Stump1eb44332009-09-09 15:08:12 +0000738 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
Douglas Gregorff0a9872009-04-13 17:12:42 +0000739 Filename.size());
Douglas Gregorbd945002009-04-13 16:31:14 +0000740 }
741
742 // Parse the line entries
743 std::vector<LineEntry> Entries;
744 while (Idx < Record.size()) {
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000745 int FID = Record[Idx++];
Douglas Gregorbd945002009-04-13 16:31:14 +0000746
747 // Extract the line entries
748 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000749 assert(NumEntries && "Numentries is 00000");
Douglas Gregorbd945002009-04-13 16:31:14 +0000750 Entries.clear();
751 Entries.reserve(NumEntries);
752 for (unsigned I = 0; I != NumEntries; ++I) {
753 unsigned FileOffset = Record[Idx++];
754 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000755 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump1eb44332009-09-09 15:08:12 +0000756 SrcMgr::CharacteristicKind FileKind
Douglas Gregorbd945002009-04-13 16:31:14 +0000757 = (SrcMgr::CharacteristicKind)Record[Idx++];
758 unsigned IncludeOffset = Record[Idx++];
759 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
760 FileKind, IncludeOffset));
761 }
762 LineTable.AddEntry(FID, Entries);
763 }
764
765 return false;
766}
767
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000768namespace {
769
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000770class ASTStatData {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000771public:
772 const bool hasStat;
773 const ino_t ino;
774 const dev_t dev;
775 const mode_t mode;
776 const time_t mtime;
777 const off_t size;
Mike Stump1eb44332009-09-09 15:08:12 +0000778
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000779 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Mike Stump1eb44332009-09-09 15:08:12 +0000780 : hasStat(true), ino(i), dev(d), mode(mo), mtime(m), size(s) {}
781
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000782 ASTStatData()
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000783 : hasStat(false), ino(0), dev(0), mode(0), mtime(0), size(0) {}
784};
785
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000786class ASTStatLookupTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000787 public:
788 typedef const char *external_key_type;
789 typedef const char *internal_key_type;
790
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000791 typedef ASTStatData data_type;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000792
793 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000794 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000795 }
796
797 static internal_key_type GetInternalKey(const char *path) { return path; }
798
799 static bool EqualKey(internal_key_type a, internal_key_type b) {
800 return strcmp(a, b) == 0;
801 }
802
803 static std::pair<unsigned, unsigned>
804 ReadKeyDataLength(const unsigned char*& d) {
805 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
806 unsigned DataLen = (unsigned) *d++;
807 return std::make_pair(KeyLen + 1, DataLen);
808 }
809
810 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
811 return (const char *)d;
812 }
813
814 static data_type ReadData(const internal_key_type, const unsigned char *d,
815 unsigned /*DataLen*/) {
816 using namespace clang::io;
817
818 if (*d++ == 1)
819 return data_type();
820
821 ino_t ino = (ino_t) ReadUnalignedLE32(d);
822 dev_t dev = (dev_t) ReadUnalignedLE32(d);
823 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +0000824 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000825 off_t size = (off_t) ReadUnalignedLE64(d);
826 return data_type(ino, dev, mode, mtime, size);
827 }
828};
829
830/// \brief stat() cache for precompiled headers.
831///
832/// This cache is very similar to the stat cache used by pretokenized
833/// headers.
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000834class ASTStatCache : public StatSysCallCache {
835 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000836 CacheTy *Cache;
837
838 unsigned &NumStatHits, &NumStatMisses;
Mike Stump1eb44332009-09-09 15:08:12 +0000839public:
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000840 ASTStatCache(const unsigned char *Buckets,
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000841 const unsigned char *Base,
842 unsigned &NumStatHits,
Mike Stump1eb44332009-09-09 15:08:12 +0000843 unsigned &NumStatMisses)
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000844 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
845 Cache = CacheTy::Create(Buckets, Base);
846 }
847
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000848 ~ASTStatCache() { delete Cache; }
Mike Stump1eb44332009-09-09 15:08:12 +0000849
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000850 int stat(const char *path, struct stat *buf) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000851 // Do the lookup for the file's data in the AST file.
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000852 CacheTy::iterator I = Cache->find(path);
853
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000854 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000855 if (I == Cache->end()) {
856 ++NumStatMisses;
Douglas Gregor52e71082009-10-16 18:18:30 +0000857 return StatSysCallCache::stat(path, buf);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000858 }
Mike Stump1eb44332009-09-09 15:08:12 +0000859
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000860 ++NumStatHits;
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000861 ASTStatData Data = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000862
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000863 if (!Data.hasStat)
864 return 1;
865
866 buf->st_ino = Data.ino;
867 buf->st_dev = Data.dev;
868 buf->st_mtime = Data.mtime;
869 buf->st_mode = Data.mode;
870 buf->st_size = Data.size;
871 return 0;
872 }
873};
874} // end anonymous namespace
875
876
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000877/// \brief Read a source manager block
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000878ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(PerFileData &F) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000879 using namespace SrcMgr;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000880
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000881 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +0000882
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000883 // Set the source-location entry cursor to the current position in
884 // the stream. This cursor will be used to read the contents of the
885 // source manager block initially, and then lazily read
886 // source-location entries as needed.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000887 SLocEntryCursor = F.Stream;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000888
889 // The stream itself is going to skip over the source manager block.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000890 if (F.Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000891 Error("malformed block record in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000892 return Failure;
893 }
894
895 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000896 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000897 Error("malformed source manager block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +0000898 return Failure;
899 }
Douglas Gregor14f79002009-04-10 03:52:48 +0000900
Douglas Gregor14f79002009-04-10 03:52:48 +0000901 RecordData Record;
902 while (true) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000903 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregor14f79002009-04-10 03:52:48 +0000904 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000905 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000906 Error("error at end of Source Manager block in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +0000907 return Failure;
908 }
Douglas Gregore1d918e2009-04-10 23:10:45 +0000909 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +0000910 }
Mike Stump1eb44332009-09-09 15:08:12 +0000911
Douglas Gregor14f79002009-04-10 03:52:48 +0000912 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
913 // No known subblocks, always skip them.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000914 SLocEntryCursor.ReadSubBlockID();
915 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000916 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +0000917 return Failure;
918 }
Douglas Gregor14f79002009-04-10 03:52:48 +0000919 continue;
920 }
Mike Stump1eb44332009-09-09 15:08:12 +0000921
Douglas Gregor14f79002009-04-10 03:52:48 +0000922 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000923 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregor14f79002009-04-10 03:52:48 +0000924 continue;
925 }
Mike Stump1eb44332009-09-09 15:08:12 +0000926
Douglas Gregor14f79002009-04-10 03:52:48 +0000927 // Read a record.
928 const char *BlobStart;
929 unsigned BlobLen;
930 Record.clear();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000931 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000932 default: // Default behavior: ignore.
933 break;
934
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000935 case SM_LINE_TABLE:
Sebastian Redl518d8cb2010-07-20 21:20:32 +0000936 if (ParseLineTable(Record))
Douglas Gregorbd945002009-04-13 16:31:14 +0000937 return Failure;
Chris Lattner2c78b872009-04-14 23:22:57 +0000938 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +0000939
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000940 case SM_SLOC_FILE_ENTRY:
941 case SM_SLOC_BUFFER_ENTRY:
942 case SM_SLOC_INSTANTIATION_ENTRY:
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000943 // Once we hit one of the source location entries, we're done.
944 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +0000945 }
946 }
947}
948
Sebastian Redl190faf72010-07-20 21:50:20 +0000949/// \brief Get a cursor that's correctly positioned for reading the source
950/// location entry with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000951llvm::BitstreamCursor &ASTReader::SLocCursorForID(unsigned ID) {
Sebastian Redl190faf72010-07-20 21:50:20 +0000952 assert(ID != 0 && ID <= TotalNumSLocEntries &&
953 "SLocCursorForID should only be called for real IDs.");
954
955 ID -= 1;
956 PerFileData *F = 0;
957 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
958 F = Chain[N - I - 1];
959 if (ID < F->LocalNumSLocEntries)
960 break;
961 ID -= F->LocalNumSLocEntries;
962 }
963 assert(F && F->LocalNumSLocEntries > ID && "Chain corrupted");
964
965 F->SLocEntryCursor.JumpToBit(F->SLocOffsets[ID]);
966 return F->SLocEntryCursor;
967}
968
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000969/// \brief Read in the source location entry with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000970ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(unsigned ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000971 if (ID == 0)
972 return Success;
973
974 if (ID > TotalNumSLocEntries) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000975 Error("source location entry ID out-of-range for AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000976 return Failure;
977 }
978
Sebastian Redl190faf72010-07-20 21:50:20 +0000979 llvm::BitstreamCursor &SLocEntryCursor = SLocCursorForID(ID);
Sebastian Redl9137a522010-07-16 17:50:48 +0000980
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000981 ++NumSLocEntriesRead;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000982 unsigned Code = SLocEntryCursor.ReadCode();
983 if (Code == llvm::bitc::END_BLOCK ||
984 Code == llvm::bitc::ENTER_SUBBLOCK ||
985 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000986 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000987 return Failure;
988 }
989
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000990 RecordData Record;
991 const char *BlobStart;
992 unsigned BlobLen;
993 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
994 default:
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000995 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000996 return Failure;
997
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000998 case SM_SLOC_FILE_ENTRY: {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000999 std::string Filename(BlobStart, BlobStart + BlobLen);
1000 MaybeAddSystemRootToFilename(Filename);
1001 const FileEntry *File = FileMgr.getFile(Filename);
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001002 if (File == 0) {
1003 std::string ErrorStr = "could not find file '";
Douglas Gregore650c8c2009-07-07 00:12:59 +00001004 ErrorStr += Filename;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001005 ErrorStr += "' referenced by AST file";
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001006 Error(ErrorStr.c_str());
1007 return Failure;
1008 }
Mike Stump1eb44332009-09-09 15:08:12 +00001009
Douglas Gregor2d52be52010-03-21 22:49:54 +00001010 if (Record.size() < 10) {
Ted Kremenek1857f622010-03-18 21:23:05 +00001011 Error("source location entry is incorrect");
1012 return Failure;
1013 }
1014
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001015 if (!DisableValidation &&
1016 ((off_t)Record[4] != File->getSize()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001017#if !defined(LLVM_ON_WIN32)
1018 // In our regression testing, the Windows file system seems to
1019 // have inconsistent modification times that sometimes
1020 // erroneously trigger this error-handling path.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001021 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001022#endif
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001023 )) {
Douglas Gregor2d52be52010-03-21 22:49:54 +00001024 Diag(diag::err_fe_pch_file_modified)
1025 << Filename;
1026 return Failure;
1027 }
1028
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001029 FileID FID = SourceMgr.createFileID(File,
1030 SourceLocation::getFromRawEncoding(Record[1]),
1031 (SrcMgr::CharacteristicKind)Record[2],
1032 ID, Record[0]);
1033 if (Record[3])
1034 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile())
1035 .setHasLineDirectives();
1036
Douglas Gregor12fab312010-03-16 16:35:32 +00001037 // Reconstruct header-search information for this file.
1038 HeaderFileInfo HFI;
Douglas Gregor2d52be52010-03-21 22:49:54 +00001039 HFI.isImport = Record[6];
1040 HFI.DirInfo = Record[7];
1041 HFI.NumIncludes = Record[8];
1042 HFI.ControllingMacroID = Record[9];
Douglas Gregor12fab312010-03-16 16:35:32 +00001043 if (Listener)
1044 Listener->ReadHeaderFileInfo(HFI, File->getUID());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001045 break;
1046 }
1047
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001048 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001049 const char *Name = BlobStart;
1050 unsigned Offset = Record[0];
1051 unsigned Code = SLocEntryCursor.ReadCode();
1052 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001053 unsigned RecCode
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001054 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001055
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001056 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001057 Error("AST record has invalid code");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001058 return Failure;
1059 }
1060
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001061 llvm::MemoryBuffer *Buffer
Chris Lattnera0a270c2010-04-05 22:42:27 +00001062 = llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(BlobStart, BlobLen - 1),
1063 Name);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001064 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset);
Mike Stump1eb44332009-09-09 15:08:12 +00001065
Douglas Gregor92b059e2009-04-28 20:33:11 +00001066 if (strcmp(Name, "<built-in>") == 0) {
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +00001067 PCHPredefinesBlock Block = {
1068 BufferID,
1069 llvm::StringRef(BlobStart, BlobLen - 1)
1070 };
1071 PCHPredefinesBuffers.push_back(Block);
Douglas Gregor92b059e2009-04-28 20:33:11 +00001072 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001073
1074 break;
1075 }
1076
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001077 case SM_SLOC_INSTANTIATION_ENTRY: {
Mike Stump1eb44332009-09-09 15:08:12 +00001078 SourceLocation SpellingLoc
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001079 = SourceLocation::getFromRawEncoding(Record[1]);
1080 SourceMgr.createInstantiationLoc(SpellingLoc,
1081 SourceLocation::getFromRawEncoding(Record[2]),
1082 SourceLocation::getFromRawEncoding(Record[3]),
1083 Record[4],
1084 ID,
1085 Record[0]);
1086 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001087 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001088 }
1089
1090 return Success;
1091}
1092
Chris Lattner6367f6d2009-04-27 01:05:14 +00001093/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1094/// specified cursor. Read the abbreviations that are at the top of the block
1095/// and then leave the cursor pointing into the block.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001096bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattner6367f6d2009-04-27 01:05:14 +00001097 unsigned BlockID) {
1098 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001099 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001100 return Failure;
1101 }
Mike Stump1eb44332009-09-09 15:08:12 +00001102
Chris Lattner6367f6d2009-04-27 01:05:14 +00001103 while (true) {
1104 unsigned Code = Cursor.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00001105
Chris Lattner6367f6d2009-04-27 01:05:14 +00001106 // We expect all abbrevs to be at the start of the block.
1107 if (Code != llvm::bitc::DEFINE_ABBREV)
1108 return false;
1109 Cursor.ReadAbbrevRecord();
1110 }
1111}
1112
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001113void ASTReader::ReadMacroRecord(llvm::BitstreamCursor &Stream, uint64_t Offset){
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001114 assert(PP && "Forgot to set Preprocessor ?");
Mike Stump1eb44332009-09-09 15:08:12 +00001115
Douglas Gregor37e26842009-04-21 23:56:24 +00001116 // Keep track of where we are in the stream, then jump back there
1117 // after reading this macro.
1118 SavedStreamPosition SavedPosition(Stream);
1119
1120 Stream.JumpToBit(Offset);
1121 RecordData Record;
1122 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1123 MacroInfo *Macro = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001124
Douglas Gregor37e26842009-04-21 23:56:24 +00001125 while (true) {
1126 unsigned Code = Stream.ReadCode();
1127 switch (Code) {
1128 case llvm::bitc::END_BLOCK:
1129 return;
1130
1131 case llvm::bitc::ENTER_SUBBLOCK:
1132 // No known subblocks, always skip them.
1133 Stream.ReadSubBlockID();
1134 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001135 Error("malformed block record in AST file");
Douglas Gregor37e26842009-04-21 23:56:24 +00001136 return;
1137 }
1138 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001139
Douglas Gregor37e26842009-04-21 23:56:24 +00001140 case llvm::bitc::DEFINE_ABBREV:
1141 Stream.ReadAbbrevRecord();
1142 continue;
1143 default: break;
1144 }
1145
1146 // Read a record.
1147 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001148 PreprocessorRecordTypes RecType =
1149 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
Douglas Gregor37e26842009-04-21 23:56:24 +00001150 switch (RecType) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001151 case PP_MACRO_OBJECT_LIKE:
1152 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001153 // If we already have a macro, that means that we've hit the end
1154 // of the definition of the macro we were looking for. We're
1155 // done.
1156 if (Macro)
1157 return;
1158
1159 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1160 if (II == 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001161 Error("macro must have a name in AST file");
Douglas Gregor37e26842009-04-21 23:56:24 +00001162 return;
1163 }
1164 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
1165 bool isUsed = Record[2];
Mike Stump1eb44332009-09-09 15:08:12 +00001166
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001167 MacroInfo *MI = PP->AllocateMacroInfo(Loc);
Douglas Gregor37e26842009-04-21 23:56:24 +00001168 MI->setIsUsed(isUsed);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001169 MI->setIsFromAST();
Mike Stump1eb44332009-09-09 15:08:12 +00001170
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001171 unsigned NextIndex = 3;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001172 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregor37e26842009-04-21 23:56:24 +00001173 // Decode function-like macro info.
1174 bool isC99VarArgs = Record[3];
1175 bool isGNUVarArgs = Record[4];
1176 MacroArgs.clear();
1177 unsigned NumArgs = Record[5];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001178 NextIndex = 6 + NumArgs;
Douglas Gregor37e26842009-04-21 23:56:24 +00001179 for (unsigned i = 0; i != NumArgs; ++i)
1180 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1181
1182 // Install function-like macro info.
1183 MI->setIsFunctionLike();
1184 if (isC99VarArgs) MI->setIsC99Varargs();
1185 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor75fdb232009-05-22 22:45:36 +00001186 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001187 PP->getPreprocessorAllocator());
Douglas Gregor37e26842009-04-21 23:56:24 +00001188 }
1189
1190 // Finally, install the macro.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001191 PP->setMacroInfo(II, MI);
Douglas Gregor37e26842009-04-21 23:56:24 +00001192
1193 // Remember that we saw this macro last so that we add the tokens that
1194 // form its body to it.
1195 Macro = MI;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001196
1197 if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) {
1198 // We have a macro definition. Load it now.
1199 PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro,
1200 getMacroDefinition(Record[NextIndex]));
1201 }
1202
Douglas Gregor37e26842009-04-21 23:56:24 +00001203 ++NumMacrosRead;
1204 break;
1205 }
Mike Stump1eb44332009-09-09 15:08:12 +00001206
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001207 case PP_TOKEN: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001208 // If we see a TOKEN before a PP_MACRO_*, then the file is
1209 // erroneous, just pretend we didn't see this.
1210 if (Macro == 0) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001211
Douglas Gregor37e26842009-04-21 23:56:24 +00001212 Token Tok;
1213 Tok.startToken();
1214 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
1215 Tok.setLength(Record[1]);
1216 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1217 Tok.setIdentifierInfo(II);
1218 Tok.setKind((tok::TokenKind)Record[3]);
1219 Tok.setFlag((Token::TokenFlags)Record[4]);
1220 Macro->AddTokenToBody(Tok);
1221 break;
1222 }
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001223
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001224 case PP_MACRO_INSTANTIATION: {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001225 // If we already have a macro, that means that we've hit the end
1226 // of the definition of the macro we were looking for. We're
1227 // done.
1228 if (Macro)
1229 return;
1230
1231 if (!PP->getPreprocessingRecord()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001232 Error("missing preprocessing record in AST file");
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001233 return;
1234 }
1235
1236 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1237 if (PPRec.getPreprocessedEntity(Record[0]))
1238 return;
1239
1240 MacroInstantiation *MI
1241 = new (PPRec) MacroInstantiation(DecodeIdentifierInfo(Record[3]),
1242 SourceRange(
1243 SourceLocation::getFromRawEncoding(Record[1]),
1244 SourceLocation::getFromRawEncoding(Record[2])),
1245 getMacroDefinition(Record[4]));
1246 PPRec.SetPreallocatedEntity(Record[0], MI);
1247 return;
1248 }
1249
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001250 case PP_MACRO_DEFINITION: {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001251 // If we already have a macro, that means that we've hit the end
1252 // of the definition of the macro we were looking for. We're
1253 // done.
1254 if (Macro)
1255 return;
1256
1257 if (!PP->getPreprocessingRecord()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001258 Error("missing preprocessing record in AST file");
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001259 return;
1260 }
1261
1262 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1263 if (PPRec.getPreprocessedEntity(Record[0]))
1264 return;
1265
1266 if (Record[1] >= MacroDefinitionsLoaded.size()) {
1267 Error("out-of-bounds macro definition record");
1268 return;
1269 }
1270
1271 MacroDefinition *MD
1272 = new (PPRec) MacroDefinition(DecodeIdentifierInfo(Record[4]),
1273 SourceLocation::getFromRawEncoding(Record[5]),
1274 SourceRange(
1275 SourceLocation::getFromRawEncoding(Record[2]),
1276 SourceLocation::getFromRawEncoding(Record[3])));
1277 PPRec.SetPreallocatedEntity(Record[0], MD);
1278 MacroDefinitionsLoaded[Record[1]] = MD;
1279 return;
1280 }
Steve Naroff83d63c72009-04-24 20:03:17 +00001281 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001282 }
1283}
1284
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001285void ASTReader::ReadDefinedMacros() {
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001286 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1287 llvm::BitstreamCursor &MacroCursor = Chain[N - I - 1]->MacroCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001288
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001289 // If there was no preprocessor block, skip this file.
1290 if (!MacroCursor.getBitStreamReader())
1291 continue;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001292
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001293 llvm::BitstreamCursor Cursor = MacroCursor;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001294 if (Cursor.EnterSubBlock(PREPROCESSOR_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001295 Error("malformed preprocessor block record in AST file");
Douglas Gregor88a35862010-01-04 19:18:44 +00001296 return;
1297 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001298
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001299 RecordData Record;
1300 while (true) {
1301 unsigned Code = Cursor.ReadCode();
1302 if (Code == llvm::bitc::END_BLOCK) {
1303 if (Cursor.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001304 Error("error at end of preprocessor block in AST file");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001305 return;
1306 }
1307 break;
Douglas Gregor88a35862010-01-04 19:18:44 +00001308 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001309
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001310 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1311 // No known subblocks, always skip them.
1312 Cursor.ReadSubBlockID();
1313 if (Cursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001314 Error("malformed block record in AST file");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001315 return;
1316 }
1317 continue;
1318 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001319
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001320 if (Code == llvm::bitc::DEFINE_ABBREV) {
1321 Cursor.ReadAbbrevRecord();
1322 continue;
1323 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001324
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001325 // Read a record.
1326 const char *BlobStart;
1327 unsigned BlobLen;
1328 Record.clear();
1329 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1330 default: // Default behavior: ignore.
1331 break;
Douglas Gregor88a35862010-01-04 19:18:44 +00001332
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001333 case PP_MACRO_OBJECT_LIKE:
1334 case PP_MACRO_FUNCTION_LIKE:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001335 DecodeIdentifierInfo(Record[0]);
1336 break;
1337
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001338 case PP_TOKEN:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001339 // Ignore tokens.
1340 break;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001341
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001342 case PP_MACRO_INSTANTIATION:
1343 case PP_MACRO_DEFINITION:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001344 // Read the macro record.
1345 ReadMacroRecord(Chain[N - I - 1]->Stream, Cursor.GetCurrentBitNo());
1346 break;
1347 }
Douglas Gregor88a35862010-01-04 19:18:44 +00001348 }
1349 }
1350}
1351
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001352MacroDefinition *ASTReader::getMacroDefinition(IdentID ID) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001353 if (ID == 0 || ID >= MacroDefinitionsLoaded.size())
1354 return 0;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001355
1356 if (!MacroDefinitionsLoaded[ID]) {
1357 unsigned Index = ID;
1358 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1359 PerFileData &F = *Chain[N - I - 1];
1360 if (Index < F.LocalNumMacroDefinitions) {
1361 ReadMacroRecord(F.Stream, F.MacroDefinitionOffsets[Index]);
1362 break;
1363 }
1364 Index -= F.LocalNumMacroDefinitions;
1365 }
1366 assert(MacroDefinitionsLoaded[ID] && "Broken chain");
1367 }
1368
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001369 return MacroDefinitionsLoaded[ID];
1370}
1371
Douglas Gregore650c8c2009-07-07 00:12:59 +00001372/// \brief If we are loading a relocatable PCH file, and the filename is
1373/// not an absolute path, add the system root to the beginning of the file
1374/// name.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001375void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001376 // If this is not a relocatable PCH file, there's nothing to do.
1377 if (!RelocatablePCH)
1378 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001379
Daniel Dunbard5b21972009-11-18 19:50:41 +00001380 if (Filename.empty() || llvm::sys::Path(Filename).isAbsolute())
Douglas Gregore650c8c2009-07-07 00:12:59 +00001381 return;
1382
Douglas Gregore650c8c2009-07-07 00:12:59 +00001383 if (isysroot == 0) {
1384 // If no system root was given, default to '/'
1385 Filename.insert(Filename.begin(), '/');
1386 return;
1387 }
Mike Stump1eb44332009-09-09 15:08:12 +00001388
Douglas Gregore650c8c2009-07-07 00:12:59 +00001389 unsigned Length = strlen(isysroot);
1390 if (isysroot[Length - 1] != '/')
1391 Filename.insert(Filename.begin(), '/');
Mike Stump1eb44332009-09-09 15:08:12 +00001392
Douglas Gregore650c8c2009-07-07 00:12:59 +00001393 Filename.insert(Filename.begin(), isysroot, isysroot + Length);
1394}
1395
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001396ASTReader::ASTReadResult
Sebastian Redl571db7f2010-08-18 23:56:56 +00001397ASTReader::ReadASTBlock(PerFileData &F) {
Sebastian Redl9137a522010-07-16 17:50:48 +00001398 llvm::BitstreamCursor &Stream = F.Stream;
1399
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001400 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001401 Error("malformed block record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001402 return Failure;
1403 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001404
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001405 // Read all of the records and blocks for the ASt file.
Douglas Gregor8038d512009-04-10 17:25:41 +00001406 RecordData Record;
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001407 bool First = true;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001408 while (!Stream.AtEndOfStream()) {
1409 unsigned Code = Stream.ReadCode();
1410 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001411 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001412 Error("error at end of module block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001413 return Failure;
1414 }
Chris Lattner7356a312009-04-11 21:15:38 +00001415
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001416 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001417 }
1418
1419 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1420 switch (Stream.ReadSubBlockID()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001421 case DECLTYPES_BLOCK_ID:
Chris Lattner6367f6d2009-04-27 01:05:14 +00001422 // We lazily load the decls block, but we want to set up the
1423 // DeclsCursor cursor to point into it. Clone our current bitcode
1424 // cursor to it, enter the block and read the abbrevs in that block.
1425 // With the main cursor, we just skip over it.
Sebastian Redl9137a522010-07-16 17:50:48 +00001426 F.DeclsCursor = Stream;
Chris Lattner6367f6d2009-04-27 01:05:14 +00001427 if (Stream.SkipBlock() || // Skip with the main cursor.
1428 // Read the abbrevs.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001429 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001430 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001431 return Failure;
1432 }
1433 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001434
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001435 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl9137a522010-07-16 17:50:48 +00001436 F.MacroCursor = Stream;
Douglas Gregor88a35862010-01-04 19:18:44 +00001437 if (PP)
1438 PP->setExternalSource(this);
1439
Chris Lattner7356a312009-04-11 21:15:38 +00001440 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001441 Error("malformed block record in AST file");
Chris Lattner7356a312009-04-11 21:15:38 +00001442 return Failure;
1443 }
1444 break;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001445
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001446 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001447 switch (ReadSourceManagerBlock(F)) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00001448 case Success:
1449 break;
1450
1451 case Failure:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001452 Error("malformed source manager block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001453 return Failure;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001454
1455 case IgnorePCH:
1456 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001457 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001458 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001459 }
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001460 First = false;
Douglas Gregor8038d512009-04-10 17:25:41 +00001461 continue;
1462 }
1463
1464 if (Code == llvm::bitc::DEFINE_ABBREV) {
1465 Stream.ReadAbbrevRecord();
1466 continue;
1467 }
1468
1469 // Read and process a record.
1470 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00001471 const char *BlobStart = 0;
1472 unsigned BlobLen = 0;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001473 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Douglas Gregor2bec0412009-04-10 21:16:55 +00001474 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001475 default: // Default behavior: ignore.
1476 break;
1477
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001478 case METADATA: {
1479 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1480 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001481 : diag::warn_pch_version_too_new);
1482 return IgnorePCH;
1483 }
1484
1485 RelocatablePCH = Record[4];
1486 if (Listener) {
1487 std::string TargetTriple(BlobStart, BlobLen);
1488 if (Listener->ReadTargetTriple(TargetTriple))
1489 return IgnorePCH;
1490 }
1491 break;
1492 }
1493
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001494 case CHAINED_METADATA: {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001495 if (!First) {
1496 Error("CHAINED_METADATA is not first record in block");
1497 return Failure;
1498 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001499 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1500 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001501 : diag::warn_pch_version_too_new);
1502 return IgnorePCH;
1503 }
1504
1505 // Load the chained file.
Sebastian Redl571db7f2010-08-18 23:56:56 +00001506 switch(ReadASTCore(llvm::StringRef(BlobStart, BlobLen))) {
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001507 case Failure: return Failure;
1508 // If we have to ignore the dependency, we'll have to ignore this too.
1509 case IgnorePCH: return IgnorePCH;
1510 case Success: break;
1511 }
1512 break;
1513 }
1514
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001515 case TYPE_OFFSET:
Sebastian Redl12d6da02010-07-19 22:06:55 +00001516 if (F.LocalNumTypes != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001517 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001518 return Failure;
1519 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00001520 F.TypeOffsets = (const uint32_t *)BlobStart;
1521 F.LocalNumTypes = Record[0];
Douglas Gregor8038d512009-04-10 17:25:41 +00001522 break;
1523
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001524 case DECL_OFFSET:
Sebastian Redl12d6da02010-07-19 22:06:55 +00001525 if (F.LocalNumDecls != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001526 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001527 return Failure;
1528 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00001529 F.DeclOffsets = (const uint32_t *)BlobStart;
1530 F.LocalNumDecls = Record[0];
Douglas Gregor8038d512009-04-10 17:25:41 +00001531 break;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001532
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001533 case TU_UPDATE_LEXICAL: {
Sebastian Redld692af72010-07-27 18:24:41 +00001534 DeclContextInfo Info = {
1535 /* No visible information */ 0, 0,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001536 reinterpret_cast<const DeclID *>(BlobStart),
1537 BlobLen / sizeof(DeclID)
Sebastian Redld692af72010-07-27 18:24:41 +00001538 };
1539 DeclContextOffsets[Context->getTranslationUnitDecl()].push_back(Info);
1540 break;
1541 }
1542
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001543 case REDECLS_UPDATE_LATEST: {
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00001544 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
1545 for (unsigned i = 0, e = Record.size(); i < e; i += 2) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001546 DeclID First = Record[i], Latest = Record[i+1];
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00001547 assert((FirstLatestDeclIDs.find(First) == FirstLatestDeclIDs.end() ||
1548 Latest > FirstLatestDeclIDs[First]) &&
1549 "The new latest is supposed to come after the previous latest");
1550 FirstLatestDeclIDs[First] = Latest;
1551 }
1552 break;
1553 }
1554
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001555 case LANGUAGE_OPTIONS:
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001556 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001557 return IgnorePCH;
1558 break;
Douglas Gregor2bec0412009-04-10 21:16:55 +00001559
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001560 case IDENTIFIER_TABLE:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001561 F.IdentifierTableData = BlobStart;
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001562 if (Record[0]) {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001563 F.IdentifierLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001564 = ASTIdentifierLookupTable::Create(
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001565 (const unsigned char *)F.IdentifierTableData + Record[0],
1566 (const unsigned char *)F.IdentifierTableData,
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001567 ASTIdentifierLookupTrait(*this, F.Stream));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001568 if (PP)
1569 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001570 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001571 break;
1572
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001573 case IDENTIFIER_OFFSET:
Sebastian Redl2da08f92010-07-19 22:28:42 +00001574 if (F.LocalNumIdentifiers != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001575 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00001576 return Failure;
1577 }
Sebastian Redl2da08f92010-07-19 22:28:42 +00001578 F.IdentifierOffsets = (const uint32_t *)BlobStart;
1579 F.LocalNumIdentifiers = Record[0];
Douglas Gregorafaf3082009-04-11 00:14:32 +00001580 break;
Douglas Gregorfdd01722009-04-14 00:24:19 +00001581
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001582 case EXTERNAL_DEFINITIONS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001583 // Optimization for the first block.
1584 if (ExternalDefinitions.empty())
1585 ExternalDefinitions.swap(Record);
1586 else
1587 ExternalDefinitions.insert(ExternalDefinitions.end(),
1588 Record.begin(), Record.end());
Douglas Gregorfdd01722009-04-14 00:24:19 +00001589 break;
Douglas Gregor3e1af842009-04-17 22:13:46 +00001590
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001591 case SPECIAL_TYPES:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001592 // Optimization for the first block
1593 if (SpecialTypes.empty())
1594 SpecialTypes.swap(Record);
1595 else
1596 SpecialTypes.insert(SpecialTypes.end(), Record.begin(), Record.end());
Douglas Gregorad1de002009-04-18 05:55:16 +00001597 break;
1598
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001599 case STATISTICS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001600 TotalNumStatements += Record[0];
1601 TotalNumMacros += Record[1];
1602 TotalLexicalDeclContexts += Record[2];
1603 TotalVisibleDeclContexts += Record[3];
Douglas Gregor3e1af842009-04-17 22:13:46 +00001604 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001605
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001606 case TENTATIVE_DEFINITIONS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001607 // Optimization for the first block.
1608 if (TentativeDefinitions.empty())
1609 TentativeDefinitions.swap(Record);
1610 else
1611 TentativeDefinitions.insert(TentativeDefinitions.end(),
1612 Record.begin(), Record.end());
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00001613 break;
Douglas Gregor14c22f22009-04-22 22:18:58 +00001614
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001615 case UNUSED_FILESCOPED_DECLS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001616 // Optimization for the first block.
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00001617 if (UnusedFileScopedDecls.empty())
1618 UnusedFileScopedDecls.swap(Record);
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001619 else
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00001620 UnusedFileScopedDecls.insert(UnusedFileScopedDecls.end(),
1621 Record.begin(), Record.end());
Tanya Lattnere6bbc012010-02-12 00:07:30 +00001622 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001623
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001624 case WEAK_UNDECLARED_IDENTIFIERS:
Sebastian Redl40566802010-08-05 18:21:25 +00001625 // Later blocks overwrite earlier ones.
1626 WeakUndeclaredIdentifiers.swap(Record);
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00001627 break;
1628
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001629 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001630 // Optimization for the first block.
1631 if (LocallyScopedExternalDecls.empty())
1632 LocallyScopedExternalDecls.swap(Record);
1633 else
1634 LocallyScopedExternalDecls.insert(LocallyScopedExternalDecls.end(),
1635 Record.begin(), Record.end());
Douglas Gregor14c22f22009-04-22 22:18:58 +00001636 break;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001637
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001638 case SELECTOR_OFFSETS:
Sebastian Redl059612d2010-08-03 21:58:15 +00001639 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redl725cd962010-08-04 20:40:17 +00001640 F.LocalNumSelectors = Record[0];
Douglas Gregor83941df2009-04-25 17:48:32 +00001641 break;
1642
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001643 case METHOD_POOL:
Sebastian Redl725cd962010-08-04 20:40:17 +00001644 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor83941df2009-04-25 17:48:32 +00001645 if (Record[0])
Sebastian Redl725cd962010-08-04 20:40:17 +00001646 F.SelectorLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001647 = ASTSelectorLookupTable::Create(
Sebastian Redl725cd962010-08-04 20:40:17 +00001648 F.SelectorLookupTableData + Record[0],
1649 F.SelectorLookupTableData,
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001650 ASTSelectorLookupTrait(*this));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00001651 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001652 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001653
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001654 case REFERENCED_SELECTOR_POOL: {
Sebastian Redl725cd962010-08-04 20:40:17 +00001655 ReferencedSelectorsData.insert(ReferencedSelectorsData.end(),
1656 Record.begin(), Record.end());
Fariborz Jahanian32019832010-07-23 19:11:11 +00001657 break;
Sebastian Redl681d7232010-07-27 00:17:23 +00001658 }
Fariborz Jahanian32019832010-07-23 19:11:11 +00001659
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001660 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001661 if (!Record.empty() && Listener)
1662 Listener->ReadCounter(Record[0]);
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001663 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001664
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001665 case SOURCE_LOCATION_OFFSETS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001666 F.SLocOffsets = (const uint32_t *)BlobStart;
1667 F.LocalNumSLocEntries = Record[0];
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001668 // We cannot delay this until the entire chain is loaded, because then
1669 // source location preloads would also have to be delayed.
1670 // FIXME: Is there a reason not to do that?
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001671 TotalNumSLocEntries += F.LocalNumSLocEntries;
Douglas Gregor445e23e2009-10-05 21:07:28 +00001672 SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, Record[1]);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001673 break;
1674
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001675 case SOURCE_LOCATION_PRELOADS:
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001676 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001677 ASTReadResult Result = ReadSLocEntryRecord(Record[I]);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001678 if (Result != Success)
1679 return Result;
1680 }
1681 break;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001682
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001683 case STAT_CACHE: {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001684 ASTStatCache *MyStatCache =
1685 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
Douglas Gregor52e71082009-10-16 18:18:30 +00001686 (const unsigned char *)BlobStart,
1687 NumStatHits, NumStatMisses);
1688 FileMgr.addStatCache(MyStatCache);
Sebastian Redl9137a522010-07-16 17:50:48 +00001689 F.StatCache = MyStatCache;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001690 break;
Douglas Gregor52e71082009-10-16 18:18:30 +00001691 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001692
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001693 case EXT_VECTOR_DECLS:
Sebastian Redla9f23682010-07-28 21:38:49 +00001694 // Optimization for the first block.
1695 if (ExtVectorDecls.empty())
1696 ExtVectorDecls.swap(Record);
1697 else
1698 ExtVectorDecls.insert(ExtVectorDecls.end(),
1699 Record.begin(), Record.end());
Douglas Gregorb81c1702009-04-27 20:06:05 +00001700 break;
1701
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001702 case VTABLE_USES:
Sebastian Redl40566802010-08-05 18:21:25 +00001703 // Later tables overwrite earlier ones.
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00001704 VTableUses.swap(Record);
1705 break;
1706
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001707 case DYNAMIC_CLASSES:
Sebastian Redl40566802010-08-05 18:21:25 +00001708 // Optimization for the first block.
1709 if (DynamicClasses.empty())
1710 DynamicClasses.swap(Record);
1711 else
1712 DynamicClasses.insert(DynamicClasses.end(),
1713 Record.begin(), Record.end());
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00001714 break;
1715
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001716 case PENDING_IMPLICIT_INSTANTIATIONS:
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00001717 // Optimization for the first block.
1718 if (PendingImplicitInstantiations.empty())
1719 PendingImplicitInstantiations.swap(Record);
1720 else
1721 PendingImplicitInstantiations.insert(
1722 PendingImplicitInstantiations.end(), Record.begin(), Record.end());
1723 break;
1724
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001725 case SEMA_DECL_REFS:
Sebastian Redl40566802010-08-05 18:21:25 +00001726 // Later tables overwrite earlier ones.
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00001727 SemaDeclRefs.swap(Record);
1728 break;
1729
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001730 case ORIGINAL_FILE_NAME:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001731 // The primary AST will be the last to get here, so it will be the one
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001732 // that's used.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +00001733 ActualOriginalFileName.assign(BlobStart, BlobLen);
1734 OriginalFileName = ActualOriginalFileName;
Douglas Gregore650c8c2009-07-07 00:12:59 +00001735 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001736 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001737
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001738 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek974be4d2010-02-12 23:31:14 +00001739 const std::string &CurBranch = getClangFullRepositoryVersion();
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001740 llvm::StringRef ASTBranch(BlobStart, BlobLen);
1741 if (llvm::StringRef(CurBranch) != ASTBranch && !DisableValidation) {
1742 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregor445e23e2009-10-05 21:07:28 +00001743 return IgnorePCH;
1744 }
1745 break;
1746 }
Sebastian Redl04e6fd42010-07-21 20:07:32 +00001747
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001748 case MACRO_DEFINITION_OFFSETS:
Sebastian Redl04e6fd42010-07-21 20:07:32 +00001749 F.MacroDefinitionOffsets = (const uint32_t *)BlobStart;
1750 F.NumPreallocatedPreprocessingEntities = Record[0];
1751 F.LocalNumMacroDefinitions = Record[1];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001752 break;
Sebastian Redl0b17c612010-08-13 00:28:03 +00001753
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001754 case DECL_REPLACEMENTS: {
Sebastian Redl0b17c612010-08-13 00:28:03 +00001755 if (Record.size() % 2 != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001756 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redl0b17c612010-08-13 00:28:03 +00001757 return Failure;
1758 }
1759 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001760 ReplacedDecls[static_cast<DeclID>(Record[I])] =
Sebastian Redl0b17c612010-08-13 00:28:03 +00001761 std::make_pair(&F, Record[I+1]);
1762 break;
1763 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001764 }
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001765 First = false;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001766 }
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001767 Error("premature end of bitstream in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001768 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001769}
1770
Sebastian Redl571db7f2010-08-18 23:56:56 +00001771ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName) {
1772 switch(ReadASTCore(FileName)) {
Sebastian Redlcdf3b832010-07-16 20:41:52 +00001773 case Failure: return Failure;
1774 case IgnorePCH: return IgnorePCH;
1775 case Success: break;
1776 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001777
1778 // Here comes stuff that we only do once the entire chain is loaded.
1779
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001780 // Allocate space for loaded identifiers, decls and types.
Sebastian Redl04e6fd42010-07-21 20:07:32 +00001781 unsigned TotalNumIdentifiers = 0, TotalNumTypes = 0, TotalNumDecls = 0,
Sebastian Redl725cd962010-08-04 20:40:17 +00001782 TotalNumPreallocatedPreprocessingEntities = 0, TotalNumMacroDefs = 0,
1783 TotalNumSelectors = 0;
Sebastian Redl12d6da02010-07-19 22:06:55 +00001784 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl2da08f92010-07-19 22:28:42 +00001785 TotalNumIdentifiers += Chain[I]->LocalNumIdentifiers;
Sebastian Redl12d6da02010-07-19 22:06:55 +00001786 TotalNumTypes += Chain[I]->LocalNumTypes;
1787 TotalNumDecls += Chain[I]->LocalNumDecls;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00001788 TotalNumPreallocatedPreprocessingEntities +=
1789 Chain[I]->NumPreallocatedPreprocessingEntities;
1790 TotalNumMacroDefs += Chain[I]->LocalNumMacroDefinitions;
Sebastian Redl725cd962010-08-04 20:40:17 +00001791 TotalNumSelectors += Chain[I]->LocalNumSelectors;
Sebastian Redl12d6da02010-07-19 22:06:55 +00001792 }
Sebastian Redl2da08f92010-07-19 22:28:42 +00001793 IdentifiersLoaded.resize(TotalNumIdentifiers);
Sebastian Redl12d6da02010-07-19 22:06:55 +00001794 TypesLoaded.resize(TotalNumTypes);
1795 DeclsLoaded.resize(TotalNumDecls);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00001796 MacroDefinitionsLoaded.resize(TotalNumMacroDefs);
1797 if (PP) {
1798 if (TotalNumIdentifiers > 0)
1799 PP->getHeaderSearchInfo().SetExternalLookup(this);
1800 if (TotalNumPreallocatedPreprocessingEntities > 0) {
1801 if (!PP->getPreprocessingRecord())
1802 PP->createPreprocessingRecord();
1803 PP->getPreprocessingRecord()->SetExternalSource(*this,
1804 TotalNumPreallocatedPreprocessingEntities);
1805 }
1806 }
Sebastian Redl725cd962010-08-04 20:40:17 +00001807 SelectorsLoaded.resize(TotalNumSelectors);
Sebastian Redl12d6da02010-07-19 22:06:55 +00001808
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001809 // Check the predefines buffers.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001810 if (!DisableValidation && CheckPredefinesBuffers())
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001811 return IgnorePCH;
1812
1813 if (PP) {
1814 // Initialization of keywords and pragmas occurs before the
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001815 // AST file is read, so there may be some identifiers that were
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001816 // loaded into the IdentifierTable before we intercepted the
1817 // creation of identifiers. Iterate through the list of known
1818 // identifiers and determine whether we have to establish
1819 // preprocessor definitions or top-level identifier declaration
1820 // chains for those identifiers.
1821 //
1822 // We copy the IdentifierInfo pointers to a small vector first,
1823 // since de-serializing declarations or macro definitions can add
1824 // new entries into the identifier table, invalidating the
1825 // iterators.
1826 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
1827 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
1828 IdEnd = PP->getIdentifierTable().end();
1829 Id != IdEnd; ++Id)
1830 Identifiers.push_back(Id->second);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00001831 // We need to search the tables in all files.
Sebastian Redl04e6fd42010-07-21 20:07:32 +00001832 for (unsigned J = 0, M = Chain.size(); J != M; ++J) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001833 ASTIdentifierLookupTable *IdTable
1834 = (ASTIdentifierLookupTable *)Chain[J]->IdentifierLookupTable;
1835 // Not all AST files necessarily have identifier tables, only the useful
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001836 // ones.
1837 if (!IdTable)
1838 continue;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00001839 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
1840 IdentifierInfo *II = Identifiers[I];
1841 // Look in the on-disk hash tables for an entry for this identifier
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001842 ASTIdentifierLookupTrait Info(*this, Chain[J]->Stream, II);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00001843 std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength());
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001844 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001845 if (Pos == IdTable->end())
1846 continue;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001847
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001848 // Dereferencing the iterator has the effect of populating the
1849 // IdentifierInfo node with the various declarations it needs.
1850 (void)*Pos;
1851 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001852 }
1853 }
1854
1855 if (Context)
1856 InitializeContext(*Context);
1857
1858 return Success;
1859}
1860
Sebastian Redl571db7f2010-08-18 23:56:56 +00001861ASTReader::ASTReadResult ASTReader::ReadASTCore(llvm::StringRef FileName) {
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001862 Chain.push_back(new PerFileData());
Sebastian Redl9137a522010-07-16 17:50:48 +00001863 PerFileData &F = *Chain.back();
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001864
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001865 // Set the AST file name.
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001866 F.FileName = FileName;
1867
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001868 // Open the AST file.
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001869 //
1870 // FIXME: This shouldn't be here, we should just take a raw_ostream.
1871 std::string ErrStr;
1872 F.Buffer.reset(llvm::MemoryBuffer::getFileOrSTDIN(FileName, &ErrStr));
1873 if (!F.Buffer) {
1874 Error(ErrStr.c_str());
1875 return IgnorePCH;
1876 }
1877
1878 // Initialize the stream
1879 F.StreamFile.init((const unsigned char *)F.Buffer->getBufferStart(),
1880 (const unsigned char *)F.Buffer->getBufferEnd());
Sebastian Redl9137a522010-07-16 17:50:48 +00001881 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001882 Stream.init(F.StreamFile);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00001883 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001884
1885 // Sniff for the signature.
1886 if (Stream.Read(8) != 'C' ||
1887 Stream.Read(8) != 'P' ||
1888 Stream.Read(8) != 'C' ||
1889 Stream.Read(8) != 'H') {
1890 Diag(diag::err_not_a_pch_file) << FileName;
1891 return Failure;
1892 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001893
Douglas Gregor2cf26342009-04-09 22:27:44 +00001894 while (!Stream.AtEndOfStream()) {
1895 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00001896
Douglas Gregore1d918e2009-04-10 23:10:45 +00001897 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001898 Error("invalid record at top-level of AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001899 return Failure;
1900 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001901
1902 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregor668c1a42009-04-21 22:25:48 +00001903
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001904 // We only know the AST subblock ID.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001905 switch (BlockID) {
1906 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001907 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001908 Error("malformed BlockInfoBlock in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001909 return Failure;
1910 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001911 break;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001912 case AST_BLOCK_ID:
Sebastian Redl571db7f2010-08-18 23:56:56 +00001913 switch (ReadASTBlock(F)) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001914 case Success:
1915 break;
1916
1917 case Failure:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001918 return Failure;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001919
1920 case IgnorePCH:
Douglas Gregor2bec0412009-04-10 21:16:55 +00001921 // FIXME: We could consider reading through to the end of this
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001922 // AST block, skipping subblocks, to see if there are other
1923 // AST blocks elsewhere.
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00001924
1925 // Clear out any preallocated source location entries, so that
1926 // the source manager does not try to resolve them later.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001927 SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00001928
1929 // Remove the stat cache.
Sebastian Redl9137a522010-07-16 17:50:48 +00001930 if (F.StatCache)
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001931 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00001932
Douglas Gregore1d918e2009-04-10 23:10:45 +00001933 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001934 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001935 break;
1936 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001937 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001938 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001939 return Failure;
1940 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001941 break;
1942 }
Mike Stump1eb44332009-09-09 15:08:12 +00001943 }
1944
Sebastian Redlcdf3b832010-07-16 20:41:52 +00001945 return Success;
1946}
1947
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001948void ASTReader::setPreprocessor(Preprocessor &pp) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001949 PP = &pp;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00001950
1951 unsigned TotalNum = 0;
1952 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
1953 TotalNum += Chain[I]->NumPreallocatedPreprocessingEntities;
1954 if (TotalNum) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001955 if (!PP->getPreprocessingRecord())
1956 PP->createPreprocessingRecord();
Sebastian Redl04e6fd42010-07-21 20:07:32 +00001957 PP->getPreprocessingRecord()->SetExternalSource(*this, TotalNum);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001958 }
1959}
1960
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001961void ASTReader::InitializeContext(ASTContext &Ctx) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001962 Context = &Ctx;
1963 assert(Context && "Passed null context!");
1964
1965 assert(PP && "Forgot to set Preprocessor ?");
1966 PP->getIdentifierTable().setExternalIdentifierLookup(this);
1967 PP->getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor88a35862010-01-04 19:18:44 +00001968 PP->setExternalSource(this);
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001969
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001970 // Load the translation unit declaration
Argyrios Kyrtzidis8871a442010-07-08 17:13:02 +00001971 GetTranslationUnitDecl();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001972
1973 // Load the special types.
1974 Context->setBuiltinVaListType(
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001975 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
1976 if (unsigned Id = SpecialTypes[SPECIAL_TYPE_OBJC_ID])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001977 Context->setObjCIdType(GetType(Id));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001978 if (unsigned Sel = SpecialTypes[SPECIAL_TYPE_OBJC_SELECTOR])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001979 Context->setObjCSelType(GetType(Sel));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001980 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001981 Context->setObjCProtoType(GetType(Proto));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001982 if (unsigned Class = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001983 Context->setObjCClassType(GetType(Class));
Steve Naroff14108da2009-07-10 23:34:53 +00001984
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001985 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001986 Context->setCFConstantStringType(GetType(String));
Mike Stump1eb44332009-09-09 15:08:12 +00001987 if (unsigned FastEnum
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001988 = SpecialTypes[SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001989 Context->setObjCFastEnumerationStateType(GetType(FastEnum));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001990 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
Douglas Gregorc29f77b2009-07-07 16:35:42 +00001991 QualType FileType = GetType(File);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001992 if (FileType.isNull()) {
1993 Error("FILE type is NULL");
1994 return;
1995 }
John McCall183700f2009-09-21 23:43:11 +00001996 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
Douglas Gregorc29f77b2009-07-07 16:35:42 +00001997 Context->setFILEDecl(Typedef->getDecl());
1998 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00001999 const TagType *Tag = FileType->getAs<TagType>();
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002000 if (!Tag) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002001 Error("Invalid FILE type in AST file");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002002 return;
2003 }
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002004 Context->setFILEDecl(Tag->getDecl());
2005 }
2006 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002007 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
Mike Stump782fa302009-07-28 02:25:19 +00002008 QualType Jmp_bufType = GetType(Jmp_buf);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002009 if (Jmp_bufType.isNull()) {
2010 Error("jmp_bug type is NULL");
2011 return;
2012 }
John McCall183700f2009-09-21 23:43:11 +00002013 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
Mike Stump782fa302009-07-28 02:25:19 +00002014 Context->setjmp_bufDecl(Typedef->getDecl());
2015 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002016 const TagType *Tag = Jmp_bufType->getAs<TagType>();
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002017 if (!Tag) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002018 Error("Invalid jmp_buf type in AST file");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002019 return;
2020 }
Mike Stump782fa302009-07-28 02:25:19 +00002021 Context->setjmp_bufDecl(Tag->getDecl());
2022 }
2023 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002024 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
Mike Stump782fa302009-07-28 02:25:19 +00002025 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002026 if (Sigjmp_bufType.isNull()) {
2027 Error("sigjmp_buf type is NULL");
2028 return;
2029 }
John McCall183700f2009-09-21 23:43:11 +00002030 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
Mike Stump782fa302009-07-28 02:25:19 +00002031 Context->setsigjmp_bufDecl(Typedef->getDecl());
2032 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002033 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002034 assert(Tag && "Invalid sigjmp_buf type in AST file");
Mike Stump782fa302009-07-28 02:25:19 +00002035 Context->setsigjmp_bufDecl(Tag->getDecl());
2036 }
2037 }
Mike Stump1eb44332009-09-09 15:08:12 +00002038 if (unsigned ObjCIdRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002039 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION])
Douglas Gregord1571ac2009-08-21 00:27:50 +00002040 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
Mike Stump1eb44332009-09-09 15:08:12 +00002041 if (unsigned ObjCClassRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002042 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION])
Douglas Gregord1571ac2009-08-21 00:27:50 +00002043 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002044 if (unsigned String = SpecialTypes[SPECIAL_TYPE_BLOCK_DESCRIPTOR])
Mike Stumpadaaad32009-10-20 02:12:22 +00002045 Context->setBlockDescriptorType(GetType(String));
Mike Stump083c25e2009-10-22 00:49:09 +00002046 if (unsigned String
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002047 = SpecialTypes[SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR])
Mike Stump083c25e2009-10-22 00:49:09 +00002048 Context->setBlockDescriptorExtendedType(GetType(String));
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002049 if (unsigned ObjCSelRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002050 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION])
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002051 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002052 if (unsigned String = SpecialTypes[SPECIAL_TYPE_NS_CONSTANT_STRING])
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002053 Context->setNSConstantStringType(GetType(String));
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002054
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002055 if (SpecialTypes[SPECIAL_TYPE_INT128_INSTALLED])
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002056 Context->setInt128Installed();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002057}
2058
Douglas Gregorb64c1932009-05-12 01:31:05 +00002059/// \brief Retrieve the name of the original source file name
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002060/// directly from the AST file, without actually loading the AST
Douglas Gregorb64c1932009-05-12 01:31:05 +00002061/// file.
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002062std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00002063 Diagnostic &Diags) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002064 // Open the AST file.
Douglas Gregorb64c1932009-05-12 01:31:05 +00002065 std::string ErrStr;
2066 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002067 Buffer.reset(llvm::MemoryBuffer::getFile(ASTFileName.c_str(), &ErrStr));
Douglas Gregorb64c1932009-05-12 01:31:05 +00002068 if (!Buffer) {
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00002069 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002070 return std::string();
2071 }
2072
2073 // Initialize the stream
2074 llvm::BitstreamReader StreamFile;
2075 llvm::BitstreamCursor Stream;
Mike Stump1eb44332009-09-09 15:08:12 +00002076 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregorb64c1932009-05-12 01:31:05 +00002077 (const unsigned char *)Buffer->getBufferEnd());
2078 Stream.init(StreamFile);
2079
2080 // Sniff for the signature.
2081 if (Stream.Read(8) != 'C' ||
2082 Stream.Read(8) != 'P' ||
2083 Stream.Read(8) != 'C' ||
2084 Stream.Read(8) != 'H') {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002085 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002086 return std::string();
2087 }
2088
2089 RecordData Record;
2090 while (!Stream.AtEndOfStream()) {
2091 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002092
Douglas Gregorb64c1932009-05-12 01:31:05 +00002093 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2094 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump1eb44332009-09-09 15:08:12 +00002095
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002096 // We only know the AST subblock ID.
Douglas Gregorb64c1932009-05-12 01:31:05 +00002097 switch (BlockID) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002098 case AST_BLOCK_ID:
2099 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002100 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002101 return std::string();
2102 }
2103 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002104
Douglas Gregorb64c1932009-05-12 01:31:05 +00002105 default:
2106 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002107 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002108 return std::string();
2109 }
2110 break;
2111 }
2112 continue;
2113 }
2114
2115 if (Code == llvm::bitc::END_BLOCK) {
2116 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002117 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002118 return std::string();
2119 }
2120 continue;
2121 }
2122
2123 if (Code == llvm::bitc::DEFINE_ABBREV) {
2124 Stream.ReadAbbrevRecord();
2125 continue;
2126 }
2127
2128 Record.clear();
2129 const char *BlobStart = 0;
2130 unsigned BlobLen = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002131 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002132 == ORIGINAL_FILE_NAME)
Douglas Gregorb64c1932009-05-12 01:31:05 +00002133 return std::string(BlobStart, BlobLen);
Mike Stump1eb44332009-09-09 15:08:12 +00002134 }
Douglas Gregorb64c1932009-05-12 01:31:05 +00002135
2136 return std::string();
2137}
2138
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002139/// \brief Parse the record that corresponds to a LangOptions data
2140/// structure.
2141///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002142/// This routine parses the language options from the AST file and then gives
2143/// them to the AST listener if one is set.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002144///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002145/// \returns true if the listener deems the file unacceptable, false otherwise.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002146bool ASTReader::ParseLanguageOptions(
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002147 const llvm::SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002148 if (Listener) {
2149 LangOptions LangOpts;
Mike Stump1eb44332009-09-09 15:08:12 +00002150
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002151 #define PARSE_LANGOPT(Option) \
2152 LangOpts.Option = Record[Idx]; \
2153 ++Idx
Mike Stump1eb44332009-09-09 15:08:12 +00002154
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002155 unsigned Idx = 0;
2156 PARSE_LANGOPT(Trigraphs);
2157 PARSE_LANGOPT(BCPLComment);
2158 PARSE_LANGOPT(DollarIdents);
2159 PARSE_LANGOPT(AsmPreprocessor);
2160 PARSE_LANGOPT(GNUMode);
Chandler Carrutheb5d7b72010-04-17 20:17:31 +00002161 PARSE_LANGOPT(GNUKeywords);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002162 PARSE_LANGOPT(ImplicitInt);
2163 PARSE_LANGOPT(Digraphs);
2164 PARSE_LANGOPT(HexFloats);
2165 PARSE_LANGOPT(C99);
2166 PARSE_LANGOPT(Microsoft);
2167 PARSE_LANGOPT(CPlusPlus);
2168 PARSE_LANGOPT(CPlusPlus0x);
2169 PARSE_LANGOPT(CXXOperatorNames);
2170 PARSE_LANGOPT(ObjC1);
2171 PARSE_LANGOPT(ObjC2);
2172 PARSE_LANGOPT(ObjCNonFragileABI);
Fariborz Jahanian412e7982010-02-09 19:31:38 +00002173 PARSE_LANGOPT(ObjCNonFragileABI2);
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +00002174 PARSE_LANGOPT(NoConstantCFStrings);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002175 PARSE_LANGOPT(PascalStrings);
2176 PARSE_LANGOPT(WritableStrings);
2177 PARSE_LANGOPT(LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +00002178 PARSE_LANGOPT(AltiVec);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002179 PARSE_LANGOPT(Exceptions);
Daniel Dunbar73482882010-02-10 18:48:44 +00002180 PARSE_LANGOPT(SjLjExceptions);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002181 PARSE_LANGOPT(NeXTRuntime);
2182 PARSE_LANGOPT(Freestanding);
2183 PARSE_LANGOPT(NoBuiltin);
2184 PARSE_LANGOPT(ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +00002185 PARSE_LANGOPT(POSIXThreads);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002186 PARSE_LANGOPT(Blocks);
2187 PARSE_LANGOPT(EmitAllDecls);
2188 PARSE_LANGOPT(MathErrno);
Chris Lattnera4d71452010-06-26 21:25:03 +00002189 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
2190 Record[Idx++]);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002191 PARSE_LANGOPT(HeinousExtensions);
2192 PARSE_LANGOPT(Optimize);
2193 PARSE_LANGOPT(OptimizeSize);
2194 PARSE_LANGOPT(Static);
2195 PARSE_LANGOPT(PICLevel);
2196 PARSE_LANGOPT(GNUInline);
2197 PARSE_LANGOPT(NoInline);
2198 PARSE_LANGOPT(AccessControl);
2199 PARSE_LANGOPT(CharIsSigned);
John Thompsona6fda122009-11-05 20:14:16 +00002200 PARSE_LANGOPT(ShortWChar);
Chris Lattnera4d71452010-06-26 21:25:03 +00002201 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
2202 LangOpts.setVisibilityMode((LangOptions::VisibilityMode)Record[Idx++]);
Daniel Dunbarab8e2812009-09-21 04:16:19 +00002203 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
Chris Lattnera4d71452010-06-26 21:25:03 +00002204 Record[Idx++]);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002205 PARSE_LANGOPT(InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +00002206 PARSE_LANGOPT(OpenCL);
Mike Stump9c276ae2009-12-12 01:27:46 +00002207 PARSE_LANGOPT(CatchUndefined);
2208 // FIXME: Missing ElideConstructors?!
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002209 #undef PARSE_LANGOPT
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002210
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002211 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002212 }
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002213
2214 return false;
2215}
2216
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002217void ASTReader::ReadPreprocessedEntities() {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002218 ReadDefinedMacros();
2219}
2220
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002221/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002222ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002223 PerFileData *F = 0;
2224 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2225 F = Chain[N - I - 1];
2226 if (Index < F->LocalNumTypes)
2227 break;
2228 Index -= F->LocalNumTypes;
2229 }
2230 assert(F && F->LocalNumTypes > Index && "Broken chain");
Sebastian Redl971dd442010-07-20 22:55:31 +00002231 return RecordLocation(&F->DeclsCursor, F->TypeOffsets[Index]);
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002232}
2233
2234/// \brief Read and return the type with the given index..
Douglas Gregor2cf26342009-04-09 22:27:44 +00002235///
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002236/// The index is the type ID, shifted and minus the number of predefs. This
2237/// routine actually reads the record corresponding to the type at the given
2238/// location. It is a helper routine for GetType, which deals with reading type
2239/// IDs.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002240QualType ASTReader::ReadTypeRecord(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002241 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redl971dd442010-07-20 22:55:31 +00002242 llvm::BitstreamCursor &DeclsCursor = *Loc.first;
Sebastian Redl9137a522010-07-16 17:50:48 +00002243
Douglas Gregor0b748912009-04-14 21:18:50 +00002244 // Keep track of where we are in the stream, then jump back there
2245 // after reading this type.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002246 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregor0b748912009-04-14 21:18:50 +00002247
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00002248 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redl27372b42010-08-11 18:52:41 +00002249
Douglas Gregord89275b2009-07-06 18:54:52 +00002250 // Note that we are loading a type record.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00002251 Deserializing AType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00002252
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002253 DeclsCursor.JumpToBit(Loc.second);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002254 RecordData Record;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002255 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002256 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
2257 case TYPE_EXT_QUAL: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002258 if (Record.size() != 2) {
2259 Error("Incorrect encoding of extended qualifier type");
2260 return QualType();
2261 }
Douglas Gregor6d473962009-04-15 22:00:08 +00002262 QualType Base = GetType(Record[0]);
John McCall0953e762009-09-24 19:53:00 +00002263 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]);
2264 return Context->getQualifiedType(Base, Quals);
Douglas Gregor6d473962009-04-15 22:00:08 +00002265 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002266
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002267 case TYPE_COMPLEX: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002268 if (Record.size() != 1) {
2269 Error("Incorrect encoding of complex type");
2270 return QualType();
2271 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002272 QualType ElemType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002273 return Context->getComplexType(ElemType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002274 }
2275
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002276 case TYPE_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002277 if (Record.size() != 1) {
2278 Error("Incorrect encoding of pointer type");
2279 return QualType();
2280 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002281 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002282 return Context->getPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002283 }
2284
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002285 case TYPE_BLOCK_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002286 if (Record.size() != 1) {
2287 Error("Incorrect encoding of block pointer type");
2288 return QualType();
2289 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002290 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002291 return Context->getBlockPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002292 }
2293
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002294 case TYPE_LVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002295 if (Record.size() != 1) {
2296 Error("Incorrect encoding of lvalue reference type");
2297 return QualType();
2298 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002299 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002300 return Context->getLValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002301 }
2302
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002303 case TYPE_RVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002304 if (Record.size() != 1) {
2305 Error("Incorrect encoding of rvalue reference type");
2306 return QualType();
2307 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002308 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002309 return Context->getRValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002310 }
2311
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002312 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidis240437b2010-07-02 11:55:15 +00002313 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002314 Error("Incorrect encoding of member pointer type");
2315 return QualType();
2316 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002317 QualType PointeeType = GetType(Record[0]);
2318 QualType ClassType = GetType(Record[1]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002319 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002320 }
2321
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002322 case TYPE_CONSTANT_ARRAY: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002323 QualType ElementType = GetType(Record[0]);
2324 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2325 unsigned IndexTypeQuals = Record[2];
2326 unsigned Idx = 3;
2327 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002328 return Context->getConstantArrayType(ElementType, Size,
2329 ASM, IndexTypeQuals);
2330 }
2331
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002332 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002333 QualType ElementType = GetType(Record[0]);
2334 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2335 unsigned IndexTypeQuals = Record[2];
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002336 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002337 }
2338
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002339 case TYPE_VARIABLE_ARRAY: {
Douglas Gregor0b748912009-04-14 21:18:50 +00002340 QualType ElementType = GetType(Record[0]);
2341 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2342 unsigned IndexTypeQuals = Record[2];
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002343 SourceLocation LBLoc = SourceLocation::getFromRawEncoding(Record[3]);
2344 SourceLocation RBLoc = SourceLocation::getFromRawEncoding(Record[4]);
Sebastian Redl577d4792010-07-22 22:43:28 +00002345 return Context->getVariableArrayType(ElementType, ReadExpr(DeclsCursor),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002346 ASM, IndexTypeQuals,
2347 SourceRange(LBLoc, RBLoc));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002348 }
2349
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002350 case TYPE_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00002351 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002352 Error("incorrect encoding of vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002353 return QualType();
2354 }
2355
2356 QualType ElementType = GetType(Record[0]);
2357 unsigned NumElements = Record[1];
Chris Lattner788b0fd2010-06-23 06:00:24 +00002358 unsigned AltiVecSpec = Record[2];
2359 return Context->getVectorType(ElementType, NumElements,
2360 (VectorType::AltiVecSpecific)AltiVecSpec);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002361 }
2362
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002363 case TYPE_EXT_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00002364 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002365 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002366 return QualType();
2367 }
2368
2369 QualType ElementType = GetType(Record[0]);
2370 unsigned NumElements = Record[1];
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002371 return Context->getExtVectorType(ElementType, NumElements);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002372 }
2373
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002374 case TYPE_FUNCTION_NO_PROTO: {
Rafael Espindola425ef722010-03-30 22:15:11 +00002375 if (Record.size() != 4) {
Douglas Gregora02b1472009-04-28 21:53:25 +00002376 Error("incorrect encoding of no-proto function type");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002377 return QualType();
2378 }
2379 QualType ResultType = GetType(Record[0]);
Rafael Espindola425ef722010-03-30 22:15:11 +00002380 FunctionType::ExtInfo Info(Record[1], Record[2], (CallingConv)Record[3]);
Rafael Espindola264ba482010-03-30 20:24:48 +00002381 return Context->getFunctionNoProtoType(ResultType, Info);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002382 }
2383
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002384 case TYPE_FUNCTION_PROTO: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002385 QualType ResultType = GetType(Record[0]);
Douglas Gregor91236662009-12-22 18:11:50 +00002386 bool NoReturn = Record[1];
Rafael Espindola425ef722010-03-30 22:15:11 +00002387 unsigned RegParm = Record[2];
2388 CallingConv CallConv = (CallingConv)Record[3];
2389 unsigned Idx = 4;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002390 unsigned NumParams = Record[Idx++];
2391 llvm::SmallVector<QualType, 16> ParamTypes;
2392 for (unsigned I = 0; I != NumParams; ++I)
2393 ParamTypes.push_back(GetType(Record[Idx++]));
2394 bool isVariadic = Record[Idx++];
2395 unsigned Quals = Record[Idx++];
Sebastian Redl465226e2009-05-27 22:11:52 +00002396 bool hasExceptionSpec = Record[Idx++];
2397 bool hasAnyExceptionSpec = Record[Idx++];
2398 unsigned NumExceptions = Record[Idx++];
2399 llvm::SmallVector<QualType, 2> Exceptions;
2400 for (unsigned I = 0; I != NumExceptions; ++I)
2401 Exceptions.push_back(GetType(Record[Idx++]));
Jay Foadbeaaccd2009-05-21 09:52:38 +00002402 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
Sebastian Redl465226e2009-05-27 22:11:52 +00002403 isVariadic, Quals, hasExceptionSpec,
2404 hasAnyExceptionSpec, NumExceptions,
Rafael Espindola264ba482010-03-30 20:24:48 +00002405 Exceptions.data(),
Rafael Espindola425ef722010-03-30 22:15:11 +00002406 FunctionType::ExtInfo(NoReturn, RegParm,
2407 CallConv));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002408 }
2409
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002410 case TYPE_UNRESOLVED_USING:
John McCalled976492009-12-04 22:46:56 +00002411 return Context->getTypeDeclType(
2412 cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0])));
2413
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002414 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002415 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002416 Error("incorrect encoding of typedef type");
2417 return QualType();
2418 }
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002419 TypedefDecl *Decl = cast<TypedefDecl>(GetDecl(Record[0]));
2420 QualType Canonical = GetType(Record[1]);
2421 return Context->getTypedefType(Decl, Canonical);
2422 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002423
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002424 case TYPE_TYPEOF_EXPR:
Sebastian Redl577d4792010-07-22 22:43:28 +00002425 return Context->getTypeOfExprType(ReadExpr(DeclsCursor));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002426
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002427 case TYPE_TYPEOF: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002428 if (Record.size() != 1) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002429 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002430 return QualType();
2431 }
2432 QualType UnderlyingType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002433 return Context->getTypeOfType(UnderlyingType);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002434 }
Mike Stump1eb44332009-09-09 15:08:12 +00002435
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002436 case TYPE_DECLTYPE:
Sebastian Redl577d4792010-07-22 22:43:28 +00002437 return Context->getDecltypeType(ReadExpr(DeclsCursor));
Anders Carlsson395b4752009-06-24 19:06:50 +00002438
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002439 case TYPE_RECORD: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002440 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002441 Error("incorrect encoding of record type");
2442 return QualType();
2443 }
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002444 bool IsDependent = Record[0];
2445 QualType T = Context->getRecordType(cast<RecordDecl>(GetDecl(Record[1])));
2446 T->Dependent = IsDependent;
2447 return T;
2448 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002449
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002450 case TYPE_ENUM: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002451 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002452 Error("incorrect encoding of enum type");
2453 return QualType();
2454 }
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002455 bool IsDependent = Record[0];
2456 QualType T = Context->getEnumType(cast<EnumDecl>(GetDecl(Record[1])));
2457 T->Dependent = IsDependent;
2458 return T;
2459 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002460
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002461 case TYPE_ELABORATED: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00002462 unsigned Idx = 0;
2463 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2464 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2465 QualType NamedType = GetType(Record[Idx++]);
2466 return Context->getElaboratedType(Keyword, NNS, NamedType);
John McCall7da24312009-09-05 00:15:47 +00002467 }
2468
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002469 case TYPE_OBJC_INTERFACE: {
Chris Lattnerc6fa4452009-04-22 06:45:28 +00002470 unsigned Idx = 0;
2471 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
John McCallc12c5bb2010-05-15 11:32:37 +00002472 return Context->getObjCInterfaceType(ItfD);
2473 }
2474
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002475 case TYPE_OBJC_OBJECT: {
John McCallc12c5bb2010-05-15 11:32:37 +00002476 unsigned Idx = 0;
2477 QualType Base = GetType(Record[Idx++]);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00002478 unsigned NumProtos = Record[Idx++];
2479 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2480 for (unsigned I = 0; I != NumProtos; ++I)
2481 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
John McCallc12c5bb2010-05-15 11:32:37 +00002482 return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00002483 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002484
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002485 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00002486 unsigned Idx = 0;
John McCallc12c5bb2010-05-15 11:32:37 +00002487 QualType Pointee = GetType(Record[Idx++]);
2488 return Context->getObjCObjectPointerType(Pointee);
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00002489 }
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +00002490
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002491 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCall49a832b2009-10-18 09:09:24 +00002492 unsigned Idx = 0;
2493 QualType Parm = GetType(Record[Idx++]);
2494 QualType Replacement = GetType(Record[Idx++]);
2495 return
2496 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
2497 Replacement);
2498 }
John McCall3cb0ebd2010-03-10 03:28:59 +00002499
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002500 case TYPE_INJECTED_CLASS_NAME: {
John McCall3cb0ebd2010-03-10 03:28:59 +00002501 CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0]));
2502 QualType TST = GetType(Record[1]); // probably derivable
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00002503 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002504 // for AST reading, too much interdependencies.
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00002505 return
2506 QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCall3cb0ebd2010-03-10 03:28:59 +00002507 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00002508
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002509 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002510 unsigned Idx = 0;
2511 unsigned Depth = Record[Idx++];
2512 unsigned Index = Record[Idx++];
2513 bool Pack = Record[Idx++];
2514 IdentifierInfo *Name = GetIdentifierInfo(Record, Idx);
2515 return Context->getTemplateTypeParmType(Depth, Index, Pack, Name);
2516 }
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00002517
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002518 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00002519 unsigned Idx = 0;
2520 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2521 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2522 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +00002523 QualType Canon = GetType(Record[Idx++]);
2524 return Context->getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00002525 }
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00002526
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002527 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00002528 unsigned Idx = 0;
2529 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2530 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2531 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
2532 unsigned NumArgs = Record[Idx++];
2533 llvm::SmallVector<TemplateArgument, 8> Args;
2534 Args.reserve(NumArgs);
2535 while (NumArgs--)
Sebastian Redl577d4792010-07-22 22:43:28 +00002536 Args.push_back(ReadTemplateArgument(DeclsCursor, Record, Idx));
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00002537 return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
2538 Args.size(), Args.data());
2539 }
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00002540
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002541 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00002542 unsigned Idx = 0;
2543
2544 // ArrayType
2545 QualType ElementType = GetType(Record[Idx++]);
2546 ArrayType::ArraySizeModifier ASM
2547 = (ArrayType::ArraySizeModifier)Record[Idx++];
2548 unsigned IndexTypeQuals = Record[Idx++];
2549
2550 // DependentSizedArrayType
Sebastian Redl577d4792010-07-22 22:43:28 +00002551 Expr *NumElts = ReadExpr(DeclsCursor);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00002552 SourceRange Brackets = ReadSourceRange(Record, Idx);
2553
2554 return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
2555 IndexTypeQuals, Brackets);
2556 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00002557
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002558 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002559 unsigned Idx = 0;
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002560 bool IsDependent = Record[Idx++];
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002561 TemplateName Name = ReadTemplateName(Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002562 llvm::SmallVector<TemplateArgument, 8> Args;
Sebastian Redl577d4792010-07-22 22:43:28 +00002563 ReadTemplateArgumentList(Args, DeclsCursor, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00002564 QualType Canon = GetType(Record[Idx++]);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002565 QualType T;
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002566 if (Canon.isNull())
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002567 T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(),
2568 Args.size());
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002569 else
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002570 T = Context->getTemplateSpecializationType(Name, Args.data(),
2571 Args.size(), Canon);
2572 T->Dependent = IsDependent;
2573 return T;
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002574 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002575 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002576 // Suppress a GCC warning
2577 return QualType();
2578}
2579
John McCalla1ee0c52009-10-16 21:56:05 +00002580namespace {
2581
2582class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002583 ASTReader &Reader;
Sebastian Redl577d4792010-07-22 22:43:28 +00002584 llvm::BitstreamCursor &DeclsCursor;
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002585 const ASTReader::RecordData &Record;
John McCalla1ee0c52009-10-16 21:56:05 +00002586 unsigned &Idx;
2587
2588public:
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002589 TypeLocReader(ASTReader &Reader, llvm::BitstreamCursor &Cursor,
2590 const ASTReader::RecordData &Record, unsigned &Idx)
Sebastian Redl577d4792010-07-22 22:43:28 +00002591 : Reader(Reader), DeclsCursor(Cursor), Record(Record), Idx(Idx) { }
John McCalla1ee0c52009-10-16 21:56:05 +00002592
John McCall51bd8032009-10-18 01:05:36 +00002593 // We want compile-time assurance that we've enumerated all of
2594 // these, so unfortunately we have to declare them first, then
2595 // define them out-of-line.
2596#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +00002597#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +00002598 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00002599#include "clang/AST/TypeLocNodes.def"
2600
John McCall51bd8032009-10-18 01:05:36 +00002601 void VisitFunctionTypeLoc(FunctionTypeLoc);
2602 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00002603};
2604
2605}
2606
John McCall51bd8032009-10-18 01:05:36 +00002607void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCalla1ee0c52009-10-16 21:56:05 +00002608 // nothing to do
2609}
John McCall51bd8032009-10-18 01:05:36 +00002610void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +00002611 TL.setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2612 if (TL.needsExtraLocalData()) {
2613 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
2614 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
2615 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
2616 TL.setModeAttr(Record[Idx++]);
2617 }
John McCalla1ee0c52009-10-16 21:56:05 +00002618}
John McCall51bd8032009-10-18 01:05:36 +00002619void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
2620 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002621}
John McCall51bd8032009-10-18 01:05:36 +00002622void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
2623 TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002624}
John McCall51bd8032009-10-18 01:05:36 +00002625void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
2626 TL.setCaretLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002627}
John McCall51bd8032009-10-18 01:05:36 +00002628void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
2629 TL.setAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002630}
John McCall51bd8032009-10-18 01:05:36 +00002631void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
2632 TL.setAmpAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002633}
John McCall51bd8032009-10-18 01:05:36 +00002634void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
2635 TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002636}
John McCall51bd8032009-10-18 01:05:36 +00002637void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
2638 TL.setLBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2639 TL.setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002640 if (Record[Idx++])
Sebastian Redl577d4792010-07-22 22:43:28 +00002641 TL.setSizeExpr(Reader.ReadExpr(DeclsCursor));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002642 else
John McCall51bd8032009-10-18 01:05:36 +00002643 TL.setSizeExpr(0);
2644}
2645void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
2646 VisitArrayTypeLoc(TL);
2647}
2648void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
2649 VisitArrayTypeLoc(TL);
2650}
2651void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
2652 VisitArrayTypeLoc(TL);
2653}
2654void TypeLocReader::VisitDependentSizedArrayTypeLoc(
2655 DependentSizedArrayTypeLoc TL) {
2656 VisitArrayTypeLoc(TL);
2657}
2658void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
2659 DependentSizedExtVectorTypeLoc TL) {
2660 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2661}
2662void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
2663 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2664}
2665void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
2666 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2667}
2668void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
2669 TL.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2670 TL.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2671 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
John McCall86acc2a2009-10-23 01:28:53 +00002672 TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
John McCall51bd8032009-10-18 01:05:36 +00002673 }
2674}
2675void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
2676 VisitFunctionTypeLoc(TL);
2677}
2678void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
2679 VisitFunctionTypeLoc(TL);
2680}
John McCalled976492009-12-04 22:46:56 +00002681void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
2682 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2683}
John McCall51bd8032009-10-18 01:05:36 +00002684void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
2685 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2686}
2687void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +00002688 TL.setTypeofLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2689 TL.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2690 TL.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall51bd8032009-10-18 01:05:36 +00002691}
2692void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +00002693 TL.setTypeofLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2694 TL.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2695 TL.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Sebastian Redl577d4792010-07-22 22:43:28 +00002696 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(DeclsCursor, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00002697}
2698void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
2699 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2700}
2701void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
2702 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2703}
2704void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
2705 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2706}
John McCall51bd8032009-10-18 01:05:36 +00002707void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
2708 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2709}
John McCall49a832b2009-10-18 09:09:24 +00002710void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
2711 SubstTemplateTypeParmTypeLoc TL) {
2712 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2713}
John McCall51bd8032009-10-18 01:05:36 +00002714void TypeLocReader::VisitTemplateSpecializationTypeLoc(
2715 TemplateSpecializationTypeLoc TL) {
John McCall833ca992009-10-29 08:12:44 +00002716 TL.setTemplateNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2717 TL.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2718 TL.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2719 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
2720 TL.setArgLocInfo(i,
2721 Reader.GetTemplateArgumentLocInfo(TL.getTypePtr()->getArg(i).getKind(),
Sebastian Redl577d4792010-07-22 22:43:28 +00002722 DeclsCursor, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00002723}
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002724void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002725 TL.setKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2726 TL.setQualifierRange(Reader.ReadSourceRange(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00002727}
John McCall3cb0ebd2010-03-10 03:28:59 +00002728void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
2729 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2730}
Douglas Gregor4714c122010-03-31 17:34:00 +00002731void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002732 TL.setKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2733 TL.setQualifierRange(Reader.ReadSourceRange(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00002734 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2735}
John McCall33500952010-06-11 00:33:02 +00002736void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
2737 DependentTemplateSpecializationTypeLoc TL) {
2738 TL.setKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2739 TL.setQualifierRange(Reader.ReadSourceRange(Record, Idx));
2740 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2741 TL.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2742 TL.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2743 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
2744 TL.setArgLocInfo(I,
2745 Reader.GetTemplateArgumentLocInfo(TL.getTypePtr()->getArg(I).getKind(),
Sebastian Redl577d4792010-07-22 22:43:28 +00002746 DeclsCursor, Record, Idx));
John McCall33500952010-06-11 00:33:02 +00002747}
John McCall51bd8032009-10-18 01:05:36 +00002748void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
2749 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCallc12c5bb2010-05-15 11:32:37 +00002750}
2751void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
2752 TL.setHasBaseTypeAsWritten(Record[Idx++]);
John McCall51bd8032009-10-18 01:05:36 +00002753 TL.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2754 TL.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2755 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
2756 TL.setProtocolLoc(i, SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002757}
John McCall54e14c42009-10-22 22:37:11 +00002758void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
2759 TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall54e14c42009-10-22 22:37:11 +00002760}
John McCalla1ee0c52009-10-16 21:56:05 +00002761
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002762TypeSourceInfo *ASTReader::GetTypeSourceInfo(llvm::BitstreamCursor &DeclsCursor,
Sebastian Redl577d4792010-07-22 22:43:28 +00002763 const RecordData &Record,
John McCalla1ee0c52009-10-16 21:56:05 +00002764 unsigned &Idx) {
2765 QualType InfoTy = GetType(Record[Idx++]);
2766 if (InfoTy.isNull())
2767 return 0;
2768
John McCalla93c9342009-12-07 02:54:59 +00002769 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
Sebastian Redl577d4792010-07-22 22:43:28 +00002770 TypeLocReader TLR(*this, DeclsCursor, Record, Idx);
John McCalla93c9342009-12-07 02:54:59 +00002771 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCalla1ee0c52009-10-16 21:56:05 +00002772 TLR.Visit(TL);
John McCalla93c9342009-12-07 02:54:59 +00002773 return TInfo;
John McCalla1ee0c52009-10-16 21:56:05 +00002774}
Douglas Gregor2cf26342009-04-09 22:27:44 +00002775
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002776QualType ASTReader::GetType(TypeID ID) {
John McCall0953e762009-09-24 19:53:00 +00002777 unsigned FastQuals = ID & Qualifiers::FastMask;
2778 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002779
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002780 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002781 QualType T;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002782 switch ((PredefinedTypeIDs)Index) {
2783 case PREDEF_TYPE_NULL_ID: return QualType();
2784 case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
2785 case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002786
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002787 case PREDEF_TYPE_CHAR_U_ID:
2788 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregor2cf26342009-04-09 22:27:44 +00002789 // FIXME: Check that the signedness of CharTy is correct!
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002790 T = Context->CharTy;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002791 break;
2792
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002793 case PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break;
2794 case PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break;
2795 case PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break;
2796 case PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break;
2797 case PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break;
2798 case PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break;
2799 case PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break;
2800 case PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break;
2801 case PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break;
2802 case PREDEF_TYPE_INT_ID: T = Context->IntTy; break;
2803 case PREDEF_TYPE_LONG_ID: T = Context->LongTy; break;
2804 case PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break;
2805 case PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break;
2806 case PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break;
2807 case PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break;
2808 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break;
2809 case PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break;
2810 case PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break;
2811 case PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break;
2812 case PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break;
2813 case PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break;
2814 case PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break;
2815 case PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break;
2816 case PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002817 }
2818
2819 assert(!T.isNull() && "Unknown predefined type");
John McCall0953e762009-09-24 19:53:00 +00002820 return T.withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002821 }
2822
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002823 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002824 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl07a353c2010-07-14 20:26:45 +00002825 if (TypesLoaded[Index].isNull()) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002826 TypesLoaded[Index] = ReadTypeRecord(Index);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002827 TypesLoaded[Index]->setFromAST();
Sebastian Redl30c514c2010-07-14 23:45:08 +00002828 if (DeserializationListener)
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002829 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1476ed42010-07-16 16:36:56 +00002830 TypesLoaded[Index]);
Sebastian Redl07a353c2010-07-14 20:26:45 +00002831 }
Mike Stump1eb44332009-09-09 15:08:12 +00002832
John McCall0953e762009-09-24 19:53:00 +00002833 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002834}
2835
John McCall833ca992009-10-29 08:12:44 +00002836TemplateArgumentLocInfo
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002837ASTReader::GetTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Sebastian Redl577d4792010-07-22 22:43:28 +00002838 llvm::BitstreamCursor &DeclsCursor,
John McCall833ca992009-10-29 08:12:44 +00002839 const RecordData &Record,
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00002840 unsigned &Index) {
John McCall833ca992009-10-29 08:12:44 +00002841 switch (Kind) {
2842 case TemplateArgument::Expression:
Sebastian Redl577d4792010-07-22 22:43:28 +00002843 return ReadExpr(DeclsCursor);
John McCall833ca992009-10-29 08:12:44 +00002844 case TemplateArgument::Type:
Sebastian Redl577d4792010-07-22 22:43:28 +00002845 return GetTypeSourceInfo(DeclsCursor, Record, Index);
Douglas Gregor788cd062009-11-11 01:00:40 +00002846 case TemplateArgument::Template: {
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00002847 SourceRange QualifierRange = ReadSourceRange(Record, Index);
2848 SourceLocation TemplateNameLoc = ReadSourceLocation(Record, Index);
2849 return TemplateArgumentLocInfo(QualifierRange, TemplateNameLoc);
Douglas Gregor788cd062009-11-11 01:00:40 +00002850 }
John McCall833ca992009-10-29 08:12:44 +00002851 case TemplateArgument::Null:
2852 case TemplateArgument::Integral:
2853 case TemplateArgument::Declaration:
2854 case TemplateArgument::Pack:
2855 return TemplateArgumentLocInfo();
2856 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002857 llvm_unreachable("unexpected template argument loc");
John McCall833ca992009-10-29 08:12:44 +00002858 return TemplateArgumentLocInfo();
2859}
2860
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00002861TemplateArgumentLoc
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002862ASTReader::ReadTemplateArgumentLoc(llvm::BitstreamCursor &DeclsCursor,
Sebastian Redl577d4792010-07-22 22:43:28 +00002863 const RecordData &Record, unsigned &Index) {
2864 TemplateArgument Arg = ReadTemplateArgument(DeclsCursor, Record, Index);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00002865
2866 if (Arg.getKind() == TemplateArgument::Expression) {
2867 if (Record[Index++]) // bool InfoHasSameExpr.
2868 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
2869 }
2870 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(Arg.getKind(),
Sebastian Redl577d4792010-07-22 22:43:28 +00002871 DeclsCursor,
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00002872 Record, Index));
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002873}
2874
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002875Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall76bd1f32010-06-01 09:23:16 +00002876 return GetDecl(ID);
2877}
2878
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002879TranslationUnitDecl *ASTReader::GetTranslationUnitDecl() {
Sebastian Redl30c514c2010-07-14 23:45:08 +00002880 if (!DeclsLoaded[0]) {
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00002881 ReadDeclRecord(0, 0);
Sebastian Redl30c514c2010-07-14 23:45:08 +00002882 if (DeserializationListener)
Sebastian Redl1476ed42010-07-16 16:36:56 +00002883 DeserializationListener->DeclRead(1, DeclsLoaded[0]);
Sebastian Redl30c514c2010-07-14 23:45:08 +00002884 }
Argyrios Kyrtzidis8871a442010-07-08 17:13:02 +00002885
2886 return cast<TranslationUnitDecl>(DeclsLoaded[0]);
2887}
2888
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002889Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002890 if (ID == 0)
2891 return 0;
2892
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002893 if (ID > DeclsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002894 Error("declaration ID out-of-range for AST file");
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002895 return 0;
2896 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002897
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002898 unsigned Index = ID - 1;
Sebastian Redl30c514c2010-07-14 23:45:08 +00002899 if (!DeclsLoaded[Index]) {
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00002900 ReadDeclRecord(Index, ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00002901 if (DeserializationListener)
2902 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
2903 }
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002904
2905 return DeclsLoaded[Index];
Douglas Gregor2cf26342009-04-09 22:27:44 +00002906}
2907
Chris Lattner887e2b32009-04-27 05:46:25 +00002908/// \brief Resolve the offset of a statement into a statement.
2909///
2910/// This operation will read a new statement from the external
2911/// source each time it is called, and is meant to be used via a
2912/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002913Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00002914 // Offset here is a global offset across the entire chain.
2915 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2916 PerFileData &F = *Chain[N - I - 1];
2917 if (Offset < F.SizeInBits) {
2918 // Since we know that this statement is part of a decl, make sure to use
2919 // the decl cursor to read it.
2920 F.DeclsCursor.JumpToBit(Offset);
2921 return ReadStmtFromStream(F.DeclsCursor);
2922 }
2923 Offset -= F.SizeInBits;
2924 }
2925 llvm_unreachable("Broken chain");
Douglas Gregor250fc9c2009-04-18 00:07:54 +00002926}
2927
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002928bool ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +00002929 llvm::SmallVectorImpl<Decl*> &Decls) {
Mike Stump1eb44332009-09-09 15:08:12 +00002930 assert(DC->hasExternalLexicalStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00002931 "DeclContext has no lexical decls in storage");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002932
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00002933 // There might be lexical decls in multiple parts of the chain, for the TU
2934 // at least.
2935 DeclContextInfos &Infos = DeclContextOffsets[DC];
2936 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
2937 I != E; ++I) {
Sebastian Redl681d7232010-07-27 00:17:23 +00002938 // IDs can be 0 if this context doesn't contain declarations.
2939 if (!I->LexicalDecls)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00002940 continue;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00002941
2942 // Load all of the declaration IDs
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002943 for (const DeclID *ID = I->LexicalDecls,
Sebastian Redl681d7232010-07-27 00:17:23 +00002944 *IDE = ID + I->NumLexicalDecls;
2945 ID != IDE; ++ID)
2946 Decls.push_back(GetDecl(*ID));
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002947 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002948
Douglas Gregor25123082009-04-22 22:34:57 +00002949 ++NumLexicalDeclContextsRead;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002950 return false;
2951}
2952
John McCall76bd1f32010-06-01 09:23:16 +00002953DeclContext::lookup_result
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002954ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +00002955 DeclarationName Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00002956 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00002957 "DeclContext has no visible decls in storage");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002958
John McCall76bd1f32010-06-01 09:23:16 +00002959 llvm::SmallVector<VisibleDeclaration, 64> Decls;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00002960 // There might be lexical decls in multiple parts of the chain, for the TU
2961 // and namespaces.
2962 DeclContextInfos &Infos = DeclContextOffsets[DC];
2963 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
2964 I != E; ++I) {
2965 uint64_t Offset = I->OffsetToVisibleDecls;
2966 if (Offset == 0)
2967 continue;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002968
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00002969 llvm::BitstreamCursor &DeclsCursor = *I->Stream;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002970
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00002971 // Keep track of where we are in the stream, then jump back there
2972 // after reading this context.
2973 SavedStreamPosition SavedPosition(DeclsCursor);
2974
2975 // Load the record containing all of the declarations visible in
2976 // this context.
2977 DeclsCursor.JumpToBit(Offset);
2978 RecordData Record;
2979 unsigned Code = DeclsCursor.ReadCode();
2980 unsigned RecCode = DeclsCursor.ReadRecord(Code, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002981 if (RecCode != DECL_CONTEXT_VISIBLE) {
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00002982 Error("Expected visible block");
2983 return DeclContext::lookup_result(DeclContext::lookup_iterator(),
2984 DeclContext::lookup_iterator());
2985 }
2986
2987 if (Record.empty())
2988 continue;
2989
2990 unsigned Idx = 0;
2991 while (Idx < Record.size()) {
2992 Decls.push_back(VisibleDeclaration());
2993 Decls.back().Name = ReadDeclarationName(Record, Idx);
2994
2995 unsigned Size = Record[Idx++];
2996 llvm::SmallVector<unsigned, 4> &LoadedDecls = Decls.back().Declarations;
2997 LoadedDecls.reserve(Size);
2998 for (unsigned J = 0; J < Size; ++J)
2999 LoadedDecls.push_back(Record[Idx++]);
3000 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003001 }
3002
Douglas Gregor25123082009-04-22 22:34:57 +00003003 ++NumVisibleDeclContextsRead;
John McCall76bd1f32010-06-01 09:23:16 +00003004
3005 SetExternalVisibleDecls(DC, Decls);
3006 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003007}
3008
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003009void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003010 assert(Consumer);
3011 while (!InterestingDecls.empty()) {
3012 DeclGroupRef DG(InterestingDecls.front());
3013 InterestingDecls.pop_front();
Sebastian Redl27372b42010-08-11 18:52:41 +00003014 Consumer->HandleInterestingDecl(DG);
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003015 }
3016}
3017
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003018void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor0af2ca42009-04-22 19:09:20 +00003019 this->Consumer = Consumer;
3020
Douglas Gregorfdd01722009-04-14 00:24:19 +00003021 if (!Consumer)
3022 return;
3023
3024 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003025 // Force deserialization of this decl, which will cause it to be queued for
3026 // passing to the consumer.
Daniel Dunbar04a0b502009-09-17 03:06:44 +00003027 GetDecl(ExternalDefinitions[I]);
Douglas Gregorfdd01722009-04-14 00:24:19 +00003028 }
Douglas Gregorc62a2fe2009-04-25 00:41:30 +00003029
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003030 PassInterestingDeclsToConsumer();
Douglas Gregorfdd01722009-04-14 00:24:19 +00003031}
3032
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003033void ASTReader::PrintStats() {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003034 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregor2cf26342009-04-09 22:27:44 +00003035
Mike Stump1eb44332009-09-09 15:08:12 +00003036 unsigned NumTypesLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003037 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall0953e762009-09-24 19:53:00 +00003038 QualType());
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003039 unsigned NumDeclsLoaded
3040 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
3041 (Decl *)0);
3042 unsigned NumIdentifiersLoaded
3043 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
3044 IdentifiersLoaded.end(),
3045 (IdentifierInfo *)0);
Mike Stump1eb44332009-09-09 15:08:12 +00003046 unsigned NumSelectorsLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003047 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
3048 SelectorsLoaded.end(),
3049 Selector());
Douglas Gregor2d41cc12009-04-13 20:50:16 +00003050
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003051 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
3052 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00003053 if (TotalNumSLocEntries)
3054 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
3055 NumSLocEntriesRead, TotalNumSLocEntries,
3056 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003057 if (!TypesLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003058 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003059 NumTypesLoaded, (unsigned)TypesLoaded.size(),
3060 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
3061 if (!DeclsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003062 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003063 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
3064 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003065 if (!IdentifiersLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003066 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003067 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
3068 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redl725cd962010-08-04 20:40:17 +00003069 if (!SelectorsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003070 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redl725cd962010-08-04 20:40:17 +00003071 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
3072 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor83941df2009-04-25 17:48:32 +00003073 if (TotalNumStatements)
3074 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
3075 NumStatementsRead, TotalNumStatements,
3076 ((float)NumStatementsRead/TotalNumStatements * 100));
3077 if (TotalNumMacros)
3078 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
3079 NumMacrosRead, TotalNumMacros,
3080 ((float)NumMacrosRead/TotalNumMacros * 100));
3081 if (TotalLexicalDeclContexts)
3082 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
3083 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
3084 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
3085 * 100));
3086 if (TotalVisibleDeclContexts)
3087 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
3088 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
3089 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
3090 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003091 if (TotalNumMethodPoolEntries) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003092 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003093 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
3094 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor83941df2009-04-25 17:48:32 +00003095 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003096 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor83941df2009-04-25 17:48:32 +00003097 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003098 std::fprintf(stderr, "\n");
3099}
3100
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003101void ASTReader::InitializeSema(Sema &S) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00003102 SemaObj = &S;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003103 S.ExternalSource = this;
3104
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00003105 // Makes sure any declarations that were deserialized "too early"
3106 // still get added to the identifier's declaration chains.
Douglas Gregor914ed9d2010-08-13 03:15:25 +00003107 if (SemaObj->TUScope) {
3108 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
3109 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(PreloadedDecls[I]));
3110 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
3111 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00003112 }
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00003113 PreloadedDecls.clear();
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003114
3115 // If there were any tentative definitions, deserialize them and add
Sebastian Redle9d12b62010-01-31 22:27:38 +00003116 // them to Sema's list of tentative definitions.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003117 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
3118 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
Sebastian Redle9d12b62010-01-31 22:27:38 +00003119 SemaObj->TentativeDefinitions.push_back(Var);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003120 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00003121
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003122 // If there were any unused file scoped decls, deserialize them and add to
3123 // Sema's list of unused file scoped decls.
3124 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
3125 DeclaratorDecl *D = cast<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
3126 SemaObj->UnusedFileScopedDecls.push_back(D);
Tanya Lattnere6bbc012010-02-12 00:07:30 +00003127 }
Douglas Gregor14c22f22009-04-22 22:18:58 +00003128
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003129 // If there were any weak undeclared identifiers, deserialize them and add to
3130 // Sema's list of weak undeclared identifiers.
3131 if (!WeakUndeclaredIdentifiers.empty()) {
3132 unsigned Idx = 0;
3133 for (unsigned I = 0, N = WeakUndeclaredIdentifiers[Idx++]; I != N; ++I) {
3134 IdentifierInfo *WeakId = GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3135 IdentifierInfo *AliasId=GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3136 SourceLocation Loc = ReadSourceLocation(WeakUndeclaredIdentifiers, Idx);
3137 bool Used = WeakUndeclaredIdentifiers[Idx++];
3138 Sema::WeakInfo WI(AliasId, Loc);
3139 WI.setUsed(Used);
3140 SemaObj->WeakUndeclaredIdentifiers.insert(std::make_pair(WeakId, WI));
3141 }
3142 }
3143
Douglas Gregor14c22f22009-04-22 22:18:58 +00003144 // If there were any locally-scoped external declarations,
3145 // deserialize them and add them to Sema's table of locally-scoped
3146 // external declarations.
3147 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
3148 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
3149 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
3150 }
Douglas Gregorb81c1702009-04-27 20:06:05 +00003151
3152 // If there were any ext_vector type declarations, deserialize them
3153 // and add them to Sema's vector of such declarations.
3154 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I)
3155 SemaObj->ExtVectorDecls.push_back(
3156 cast<TypedefDecl>(GetDecl(ExtVectorDecls[I])));
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003157
3158 // FIXME: Do VTable uses and dynamic classes deserialize too much ?
3159 // Can we cut them down before writing them ?
3160
3161 // If there were any VTable uses, deserialize the information and add it
3162 // to Sema's vector and map of VTable uses.
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003163 if (!VTableUses.empty()) {
3164 unsigned Idx = 0;
3165 for (unsigned I = 0, N = VTableUses[Idx++]; I != N; ++I) {
3166 CXXRecordDecl *Class = cast<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
3167 SourceLocation Loc = ReadSourceLocation(VTableUses, Idx);
3168 bool DefinitionRequired = VTableUses[Idx++];
3169 SemaObj->VTableUses.push_back(std::make_pair(Class, Loc));
3170 SemaObj->VTablesUsed[Class] = DefinitionRequired;
3171 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003172 }
3173
3174 // If there were any dynamic classes declarations, deserialize them
3175 // and add them to Sema's vector of such declarations.
3176 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I)
3177 SemaObj->DynamicClasses.push_back(
3178 cast<CXXRecordDecl>(GetDecl(DynamicClasses[I])));
Fariborz Jahanian32019832010-07-23 19:11:11 +00003179
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003180 // If there were any pending implicit instantiations, deserialize them
3181 // and add them to Sema's queue of such instantiations.
3182 assert(PendingImplicitInstantiations.size() % 2 == 0 &&
3183 "Expected pairs of entries");
3184 for (unsigned Idx = 0, N = PendingImplicitInstantiations.size(); Idx < N;) {
3185 ValueDecl *D=cast<ValueDecl>(GetDecl(PendingImplicitInstantiations[Idx++]));
3186 SourceLocation Loc = ReadSourceLocation(PendingImplicitInstantiations, Idx);
3187 SemaObj->PendingImplicitInstantiations.push_back(std::make_pair(D, Loc));
3188 }
3189
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003190 // Load the offsets of the declarations that Sema references.
3191 // They will be lazily deserialized when needed.
3192 if (!SemaDeclRefs.empty()) {
3193 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
3194 SemaObj->StdNamespace = SemaDeclRefs[0];
3195 SemaObj->StdBadAlloc = SemaDeclRefs[1];
3196 }
3197
Fariborz Jahanian32019832010-07-23 19:11:11 +00003198 // If there are @selector references added them to its pool. This is for
3199 // implementation of -Wselector.
Sebastian Redl725cd962010-08-04 20:40:17 +00003200 if (!ReferencedSelectorsData.empty()) {
3201 unsigned int DataSize = ReferencedSelectorsData.size()-1;
Fariborz Jahanian32019832010-07-23 19:11:11 +00003202 unsigned I = 0;
3203 while (I < DataSize) {
Sebastian Redl725cd962010-08-04 20:40:17 +00003204 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
Fariborz Jahanian32019832010-07-23 19:11:11 +00003205 SourceLocation SelLoc =
Sebastian Redl725cd962010-08-04 20:40:17 +00003206 SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
Fariborz Jahanian32019832010-07-23 19:11:11 +00003207 SemaObj->ReferencedSelectors.insert(std::make_pair(Sel, SelLoc));
3208 }
3209 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00003210}
3211
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003212IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Sebastian Redld8c5abb2010-08-02 18:30:12 +00003213 // Try to find this name within our on-disk hash tables. We start with the
3214 // most recent one, since that one contains the most up-to-date info.
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003215 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003216 ASTIdentifierLookupTable *IdTable
3217 = (ASTIdentifierLookupTable *)Chain[I]->IdentifierLookupTable;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003218 if (!IdTable)
3219 continue;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003220 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003221 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003222 if (Pos == IdTable->end())
3223 continue;
Douglas Gregor668c1a42009-04-21 22:25:48 +00003224
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003225 // Dereferencing the iterator has the effect of building the
3226 // IdentifierInfo node and populating it with the various
3227 // declarations it needs.
Sebastian Redld8c5abb2010-08-02 18:30:12 +00003228 return *Pos;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003229 }
Sebastian Redld8c5abb2010-08-02 18:30:12 +00003230 return 0;
Douglas Gregor668c1a42009-04-21 22:25:48 +00003231}
3232
Mike Stump1eb44332009-09-09 15:08:12 +00003233std::pair<ObjCMethodList, ObjCMethodList>
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003234ASTReader::ReadMethodPool(Selector Sel) {
Sebastian Redl725cd962010-08-04 20:40:17 +00003235 // Find this selector in a hash table. We want to find the most recent entry.
3236 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3237 PerFileData &F = *Chain[I];
3238 if (!F.SelectorLookupTable)
3239 continue;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003240
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003241 ASTSelectorLookupTable *PoolTable
3242 = (ASTSelectorLookupTable*)F.SelectorLookupTable;
3243 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
Sebastian Redl725cd962010-08-04 20:40:17 +00003244 if (Pos != PoolTable->end()) {
3245 ++NumSelectorsRead;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003246 // FIXME: Not quite happy with the statistics here. We probably should
3247 // disable this tracking when called via LoadSelector.
3248 // Also, should entries without methods count as misses?
3249 ++NumMethodPoolEntriesRead;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003250 ASTSelectorLookupTrait::data_type Data = *Pos;
Sebastian Redl725cd962010-08-04 20:40:17 +00003251 if (DeserializationListener)
3252 DeserializationListener->SelectorRead(Data.ID, Sel);
3253 return std::make_pair(Data.Instance, Data.Factory);
3254 }
Douglas Gregor83941df2009-04-25 17:48:32 +00003255 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003256
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003257 ++NumMethodPoolMisses;
Sebastian Redl725cd962010-08-04 20:40:17 +00003258 return std::pair<ObjCMethodList, ObjCMethodList>();
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003259}
3260
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003261void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redle58aa892010-08-04 18:21:41 +00003262 // It would be complicated to avoid reading the methods anyway. So don't.
3263 ReadMethodPool(Sel);
3264}
3265
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003266void ASTReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00003267 assert(ID && "Non-zero identifier ID required");
Douglas Gregora02b1472009-04-28 21:53:25 +00003268 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003269 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003270 if (DeserializationListener)
3271 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregor668c1a42009-04-21 22:25:48 +00003272}
3273
Douglas Gregord89275b2009-07-06 18:54:52 +00003274/// \brief Set the globally-visible declarations associated with the given
3275/// identifier.
3276///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003277/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump1eb44332009-09-09 15:08:12 +00003278/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregord89275b2009-07-06 18:54:52 +00003279/// them.
3280///
3281/// \param II an IdentifierInfo that refers to one or more globally-visible
3282/// declarations.
3283///
3284/// \param DeclIDs the set of declaration IDs with the name @p II that are
3285/// visible at global scope.
3286///
3287/// \param Nonrecursive should be true to indicate that the caller knows that
3288/// this call is non-recursive, and therefore the globally-visible declarations
3289/// will not be placed onto the pending queue.
Mike Stump1eb44332009-09-09 15:08:12 +00003290void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003291ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Douglas Gregord89275b2009-07-06 18:54:52 +00003292 const llvm::SmallVectorImpl<uint32_t> &DeclIDs,
3293 bool Nonrecursive) {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00003294 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregord89275b2009-07-06 18:54:52 +00003295 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
3296 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
3297 PII.II = II;
3298 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I)
3299 PII.DeclIDs.push_back(DeclIDs[I]);
3300 return;
3301 }
Mike Stump1eb44332009-09-09 15:08:12 +00003302
Douglas Gregord89275b2009-07-06 18:54:52 +00003303 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
3304 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
3305 if (SemaObj) {
Douglas Gregor914ed9d2010-08-13 03:15:25 +00003306 if (SemaObj->TUScope) {
3307 // Introduce this declaration into the translation-unit scope
3308 // and add it to the declaration chain for this identifier, so
3309 // that (unqualified) name lookup will find it.
3310 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(D));
3311 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
3312 }
Douglas Gregord89275b2009-07-06 18:54:52 +00003313 } else {
3314 // Queue this declaration so that it will be added to the
3315 // translation unit scope and identifier's declaration chain
3316 // once a Sema object is known.
3317 PreloadedDecls.push_back(D);
3318 }
3319 }
3320}
3321
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003322IdentifierInfo *ASTReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00003323 if (ID == 0)
3324 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003325
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00003326 if (IdentifiersLoaded.empty()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003327 Error("no identifier table in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00003328 return 0;
3329 }
Mike Stump1eb44332009-09-09 15:08:12 +00003330
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003331 assert(PP && "Forgot to set Preprocessor ?");
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00003332 ID -= 1;
3333 if (!IdentifiersLoaded[ID]) {
3334 unsigned Index = ID;
3335 const char *Str = 0;
3336 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3337 PerFileData *F = Chain[N - I - 1];
3338 if (Index < F->LocalNumIdentifiers) {
3339 uint32_t Offset = F->IdentifierOffsets[Index];
3340 Str = F->IdentifierTableData + Offset;
3341 break;
3342 }
3343 Index -= F->LocalNumIdentifiers;
3344 }
3345 assert(Str && "Broken Chain");
Douglas Gregord6595a42009-04-25 21:04:17 +00003346
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003347 // All of the strings in the AST file are preceded by a 16-bit length.
3348 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenek231bc0b2009-10-23 04:45:31 +00003349 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
3350 // unsigned integers. This is important to avoid integer overflow when
3351 // we cast them to 'unsigned'.
Ted Kremenekff1ea462009-10-23 03:57:22 +00003352 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregor02fc7512009-04-28 20:01:51 +00003353 unsigned StrLen = (((unsigned) StrLenPtr[0])
3354 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00003355 IdentifiersLoaded[ID]
Kovarththanan Rajaratnam811f4262010-03-12 10:32:27 +00003356 = &PP->getIdentifierTable().get(Str, StrLen);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003357 if (DeserializationListener)
3358 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregorafaf3082009-04-11 00:14:32 +00003359 }
Mike Stump1eb44332009-09-09 15:08:12 +00003360
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00003361 return IdentifiersLoaded[ID];
Douglas Gregor2cf26342009-04-09 22:27:44 +00003362}
3363
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003364void ASTReader::ReadSLocEntry(unsigned ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00003365 ReadSLocEntryRecord(ID);
3366}
3367
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003368Selector ASTReader::DecodeSelector(unsigned ID) {
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003369 if (ID == 0)
3370 return Selector();
Mike Stump1eb44332009-09-09 15:08:12 +00003371
Sebastian Redl725cd962010-08-04 20:40:17 +00003372 if (ID > SelectorsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003373 Error("selector ID out of range in AST file");
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003374 return Selector();
3375 }
Douglas Gregor83941df2009-04-25 17:48:32 +00003376
Sebastian Redl725cd962010-08-04 20:40:17 +00003377 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003378 // Load this selector from the selector table.
Sebastian Redl725cd962010-08-04 20:40:17 +00003379 unsigned Idx = ID - 1;
3380 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3381 PerFileData &F = *Chain[N - I - 1];
3382 if (Idx < F.LocalNumSelectors) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003383 ASTSelectorLookupTrait Trait(*this);
Sebastian Redl725cd962010-08-04 20:40:17 +00003384 SelectorsLoaded[ID - 1] =
3385 Trait.ReadKey(F.SelectorLookupTableData + F.SelectorOffsets[Idx], 0);
3386 if (DeserializationListener)
3387 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
3388 break;
3389 }
3390 Idx -= F.LocalNumSelectors;
3391 }
Douglas Gregor83941df2009-04-25 17:48:32 +00003392 }
3393
Sebastian Redl725cd962010-08-04 20:40:17 +00003394 return SelectorsLoaded[ID - 1];
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003395}
3396
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003397Selector ASTReader::GetExternalSelector(uint32_t ID) {
Douglas Gregor719770d2010-04-06 17:30:22 +00003398 return DecodeSelector(ID);
3399}
3400
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003401uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redl725cd962010-08-04 20:40:17 +00003402 // ID 0 (the null selector) is considered an external selector.
3403 return getTotalNumSelectors() + 1;
Douglas Gregor719770d2010-04-06 17:30:22 +00003404}
3405
Mike Stump1eb44332009-09-09 15:08:12 +00003406DeclarationName
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003407ASTReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003408 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
3409 switch (Kind) {
3410 case DeclarationName::Identifier:
3411 return DeclarationName(GetIdentifierInfo(Record, Idx));
3412
3413 case DeclarationName::ObjCZeroArgSelector:
3414 case DeclarationName::ObjCOneArgSelector:
3415 case DeclarationName::ObjCMultiArgSelector:
Steve Naroffa7503a72009-04-23 15:15:40 +00003416 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00003417
3418 case DeclarationName::CXXConstructorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003419 return Context->DeclarationNames.getCXXConstructorName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00003420 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00003421
3422 case DeclarationName::CXXDestructorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003423 return Context->DeclarationNames.getCXXDestructorName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00003424 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00003425
3426 case DeclarationName::CXXConversionFunctionName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003427 return Context->DeclarationNames.getCXXConversionFunctionName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00003428 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00003429
3430 case DeclarationName::CXXOperatorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003431 return Context->DeclarationNames.getCXXOperatorName(
Douglas Gregor2cf26342009-04-09 22:27:44 +00003432 (OverloadedOperatorKind)Record[Idx++]);
3433
Sean Hunt3e518bd2009-11-29 07:34:05 +00003434 case DeclarationName::CXXLiteralOperatorName:
3435 return Context->DeclarationNames.getCXXLiteralOperatorName(
3436 GetIdentifierInfo(Record, Idx));
3437
Douglas Gregor2cf26342009-04-09 22:27:44 +00003438 case DeclarationName::CXXUsingDirective:
3439 return DeclarationName::getUsingDirectiveName();
3440 }
3441
3442 // Required to silence GCC warning
3443 return DeclarationName();
3444}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003445
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003446TemplateName
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003447ASTReader::ReadTemplateName(const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003448 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
3449 switch (Kind) {
3450 case TemplateName::Template:
3451 return TemplateName(cast_or_null<TemplateDecl>(GetDecl(Record[Idx++])));
3452
3453 case TemplateName::OverloadedTemplate: {
3454 unsigned size = Record[Idx++];
3455 UnresolvedSet<8> Decls;
3456 while (size--)
3457 Decls.addDecl(cast<NamedDecl>(GetDecl(Record[Idx++])));
3458
3459 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
3460 }
3461
3462 case TemplateName::QualifiedTemplate: {
3463 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3464 bool hasTemplKeyword = Record[Idx++];
3465 TemplateDecl *Template = cast<TemplateDecl>(GetDecl(Record[Idx++]));
3466 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
3467 }
3468
3469 case TemplateName::DependentTemplate: {
3470 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3471 if (Record[Idx++]) // isIdentifier
3472 return Context->getDependentTemplateName(NNS,
3473 GetIdentifierInfo(Record, Idx));
3474 return Context->getDependentTemplateName(NNS,
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003475 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003476 }
3477 }
3478
3479 assert(0 && "Unhandled template name kind!");
3480 return TemplateName();
3481}
3482
3483TemplateArgument
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003484ASTReader::ReadTemplateArgument(llvm::BitstreamCursor &DeclsCursor,
Sebastian Redl577d4792010-07-22 22:43:28 +00003485 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003486 switch ((TemplateArgument::ArgKind)Record[Idx++]) {
3487 case TemplateArgument::Null:
3488 return TemplateArgument();
3489 case TemplateArgument::Type:
3490 return TemplateArgument(GetType(Record[Idx++]));
3491 case TemplateArgument::Declaration:
3492 return TemplateArgument(GetDecl(Record[Idx++]));
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00003493 case TemplateArgument::Integral: {
3494 llvm::APSInt Value = ReadAPSInt(Record, Idx);
3495 QualType T = GetType(Record[Idx++]);
3496 return TemplateArgument(Value, T);
3497 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003498 case TemplateArgument::Template:
3499 return TemplateArgument(ReadTemplateName(Record, Idx));
3500 case TemplateArgument::Expression:
Sebastian Redl577d4792010-07-22 22:43:28 +00003501 return TemplateArgument(ReadExpr(DeclsCursor));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003502 case TemplateArgument::Pack: {
3503 unsigned NumArgs = Record[Idx++];
3504 llvm::SmallVector<TemplateArgument, 8> Args;
3505 Args.reserve(NumArgs);
3506 while (NumArgs--)
Sebastian Redl577d4792010-07-22 22:43:28 +00003507 Args.push_back(ReadTemplateArgument(DeclsCursor, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003508 TemplateArgument TemplArg;
3509 TemplArg.setArgumentPack(Args.data(), Args.size(), /*CopyArgs=*/true);
3510 return TemplArg;
3511 }
3512 }
3513
3514 assert(0 && "Unhandled template argument kind!");
3515 return TemplateArgument();
3516}
3517
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003518TemplateParameterList *
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003519ASTReader::ReadTemplateParameterList(const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003520 SourceLocation TemplateLoc = ReadSourceLocation(Record, Idx);
3521 SourceLocation LAngleLoc = ReadSourceLocation(Record, Idx);
3522 SourceLocation RAngleLoc = ReadSourceLocation(Record, Idx);
3523
3524 unsigned NumParams = Record[Idx++];
3525 llvm::SmallVector<NamedDecl *, 16> Params;
3526 Params.reserve(NumParams);
3527 while (NumParams--)
3528 Params.push_back(cast<NamedDecl>(GetDecl(Record[Idx++])));
3529
3530 TemplateParameterList* TemplateParams =
3531 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
3532 Params.data(), Params.size(), RAngleLoc);
3533 return TemplateParams;
3534}
3535
3536void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003537ASTReader::
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003538ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs,
Sebastian Redl577d4792010-07-22 22:43:28 +00003539 llvm::BitstreamCursor &DeclsCursor,
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003540 const RecordData &Record, unsigned &Idx) {
3541 unsigned NumTemplateArgs = Record[Idx++];
3542 TemplArgs.reserve(NumTemplateArgs);
3543 while (NumTemplateArgs--)
Sebastian Redl577d4792010-07-22 22:43:28 +00003544 TemplArgs.push_back(ReadTemplateArgument(DeclsCursor, Record, Idx));
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003545}
3546
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00003547/// \brief Read a UnresolvedSet structure.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003548void ASTReader::ReadUnresolvedSet(UnresolvedSetImpl &Set,
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00003549 const RecordData &Record, unsigned &Idx) {
3550 unsigned NumDecls = Record[Idx++];
3551 while (NumDecls--) {
3552 NamedDecl *D = cast<NamedDecl>(GetDecl(Record[Idx++]));
3553 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
3554 Set.addDecl(D, AS);
3555 }
3556}
3557
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003558CXXBaseSpecifier
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003559ASTReader::ReadCXXBaseSpecifier(llvm::BitstreamCursor &DeclsCursor,
Nick Lewycky56062202010-07-26 16:56:01 +00003560 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003561 bool isVirtual = static_cast<bool>(Record[Idx++]);
3562 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
3563 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Nick Lewycky56062202010-07-26 16:56:01 +00003564 TypeSourceInfo *TInfo = GetTypeSourceInfo(DeclsCursor, Record, Idx);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003565 SourceRange Range = ReadSourceRange(Record, Idx);
Nick Lewycky56062202010-07-26 16:56:01 +00003566 return CXXBaseSpecifier(Range, isVirtual, isBaseOfClass, AS, TInfo);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003567}
3568
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003569std::pair<CXXBaseOrMemberInitializer **, unsigned>
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003570ASTReader::ReadCXXBaseOrMemberInitializers(llvm::BitstreamCursor &Cursor,
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003571 const RecordData &Record,
3572 unsigned &Idx) {
3573 CXXBaseOrMemberInitializer **BaseOrMemberInitializers = 0;
3574 unsigned NumInitializers = Record[Idx++];
3575 if (NumInitializers) {
3576 ASTContext &C = *getContext();
3577
3578 BaseOrMemberInitializers
3579 = new (C) CXXBaseOrMemberInitializer*[NumInitializers];
3580 for (unsigned i=0; i != NumInitializers; ++i) {
3581 TypeSourceInfo *BaseClassInfo = 0;
3582 bool IsBaseVirtual = false;
3583 FieldDecl *Member = 0;
3584
3585 bool IsBaseInitializer = Record[Idx++];
3586 if (IsBaseInitializer) {
3587 BaseClassInfo = GetTypeSourceInfo(Cursor, Record, Idx);
3588 IsBaseVirtual = Record[Idx++];
3589 } else {
3590 Member = cast<FieldDecl>(GetDecl(Record[Idx++]));
3591 }
3592 SourceLocation MemberLoc = ReadSourceLocation(Record, Idx);
3593 Expr *Init = ReadExpr(Cursor);
3594 FieldDecl *AnonUnionMember
3595 = cast_or_null<FieldDecl>(GetDecl(Record[Idx++]));
3596 SourceLocation LParenLoc = ReadSourceLocation(Record, Idx);
3597 SourceLocation RParenLoc = ReadSourceLocation(Record, Idx);
3598 bool IsWritten = Record[Idx++];
3599 unsigned SourceOrderOrNumArrayIndices;
3600 llvm::SmallVector<VarDecl *, 8> Indices;
3601 if (IsWritten) {
3602 SourceOrderOrNumArrayIndices = Record[Idx++];
3603 } else {
3604 SourceOrderOrNumArrayIndices = Record[Idx++];
3605 Indices.reserve(SourceOrderOrNumArrayIndices);
3606 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
3607 Indices.push_back(cast<VarDecl>(GetDecl(Record[Idx++])));
3608 }
3609
3610 CXXBaseOrMemberInitializer *BOMInit;
3611 if (IsBaseInitializer) {
3612 BOMInit = new (C) CXXBaseOrMemberInitializer(C, BaseClassInfo,
3613 IsBaseVirtual, LParenLoc,
3614 Init, RParenLoc);
3615 } else if (IsWritten) {
3616 BOMInit = new (C) CXXBaseOrMemberInitializer(C, Member, MemberLoc,
3617 LParenLoc, Init, RParenLoc);
3618 } else {
3619 BOMInit = CXXBaseOrMemberInitializer::Create(C, Member, MemberLoc,
3620 LParenLoc, Init, RParenLoc,
3621 Indices.data(),
3622 Indices.size());
3623 }
3624
3625 BOMInit->setAnonUnionMember(AnonUnionMember);
3626 BaseOrMemberInitializers[i] = BOMInit;
3627 }
3628 }
3629
3630 return std::make_pair(BaseOrMemberInitializers, NumInitializers);
3631}
3632
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003633NestedNameSpecifier *
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003634ASTReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003635 unsigned N = Record[Idx++];
3636 NestedNameSpecifier *NNS = 0, *Prev = 0;
3637 for (unsigned I = 0; I != N; ++I) {
3638 NestedNameSpecifier::SpecifierKind Kind
3639 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
3640 switch (Kind) {
3641 case NestedNameSpecifier::Identifier: {
3642 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
3643 NNS = NestedNameSpecifier::Create(*Context, Prev, II);
3644 break;
3645 }
3646
3647 case NestedNameSpecifier::Namespace: {
3648 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
3649 NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
3650 break;
3651 }
3652
3653 case NestedNameSpecifier::TypeSpec:
3654 case NestedNameSpecifier::TypeSpecWithTemplate: {
3655 Type *T = GetType(Record[Idx++]).getTypePtr();
3656 bool Template = Record[Idx++];
3657 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
3658 break;
3659 }
3660
3661 case NestedNameSpecifier::Global: {
3662 NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
3663 // No associated value, and there can't be a prefix.
3664 break;
3665 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003666 }
Argyrios Kyrtzidisd2bb2c02010-07-07 15:46:30 +00003667 Prev = NNS;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003668 }
3669 return NNS;
3670}
3671
3672SourceRange
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003673ASTReader::ReadSourceRange(const RecordData &Record, unsigned &Idx) {
Daniel Dunbar8ee59392010-06-02 15:47:10 +00003674 SourceLocation beg = SourceLocation::getFromRawEncoding(Record[Idx++]);
3675 SourceLocation end = SourceLocation::getFromRawEncoding(Record[Idx++]);
3676 return SourceRange(beg, end);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003677}
3678
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003679/// \brief Read an integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003680llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003681 unsigned BitWidth = Record[Idx++];
3682 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
3683 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
3684 Idx += NumWords;
3685 return Result;
3686}
3687
3688/// \brief Read a signed integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003689llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003690 bool isUnsigned = Record[Idx++];
3691 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
3692}
3693
Douglas Gregor17fc2232009-04-14 21:55:33 +00003694/// \brief Read a floating-point value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003695llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00003696 return llvm::APFloat(ReadAPInt(Record, Idx));
3697}
3698
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003699// \brief Read a string
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003700std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003701 unsigned Len = Record[Idx++];
Jay Foadbeaaccd2009-05-21 09:52:38 +00003702 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003703 Idx += Len;
3704 return Result;
3705}
3706
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003707CXXTemporary *ASTReader::ReadCXXTemporary(const RecordData &Record,
Chris Lattnerd2598362010-05-10 00:25:06 +00003708 unsigned &Idx) {
3709 CXXDestructorDecl *Decl = cast<CXXDestructorDecl>(GetDecl(Record[Idx++]));
3710 return CXXTemporary::Create(*Context, Decl);
3711}
3712
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003713DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00003714 return Diag(SourceLocation(), DiagID);
3715}
3716
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003717DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003718 return Diags.Report(FullSourceLoc(Loc, SourceMgr), DiagID);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003719}
Douglas Gregor025452f2009-04-17 00:04:06 +00003720
Douglas Gregor668c1a42009-04-21 22:25:48 +00003721/// \brief Retrieve the identifier table associated with the
3722/// preprocessor.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003723IdentifierTable &ASTReader::getIdentifierTable() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003724 assert(PP && "Forgot to set Preprocessor ?");
3725 return PP->getIdentifierTable();
Douglas Gregor668c1a42009-04-21 22:25:48 +00003726}
3727
Douglas Gregor025452f2009-04-17 00:04:06 +00003728/// \brief Record that the given ID maps to the given switch-case
3729/// statement.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003730void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Douglas Gregor025452f2009-04-17 00:04:06 +00003731 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
3732 SwitchCaseStmts[ID] = SC;
3733}
3734
3735/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003736SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Douglas Gregor025452f2009-04-17 00:04:06 +00003737 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
3738 return SwitchCaseStmts[ID];
3739}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00003740
3741/// \brief Record that the given label statement has been
3742/// deserialized and has the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003743void ASTReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
Mike Stump1eb44332009-09-09 15:08:12 +00003744 assert(LabelStmts.find(ID) == LabelStmts.end() &&
Douglas Gregor1de05fe2009-04-17 18:18:49 +00003745 "Deserialized label twice");
3746 LabelStmts[ID] = S;
3747
3748 // If we've already seen any goto statements that point to this
3749 // label, resolve them now.
3750 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
3751 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
3752 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
3753 Goto->second->setLabel(S);
3754 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00003755
3756 // If we've already seen any address-label statements that point to
3757 // this label, resolve them now.
3758 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
Mike Stump1eb44332009-09-09 15:08:12 +00003759 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00003760 = UnresolvedAddrLabelExprs.equal_range(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00003761 for (AddrLabelIter AddrLabel = AddrLabels.first;
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00003762 AddrLabel != AddrLabels.second; ++AddrLabel)
3763 AddrLabel->second->setLabel(S);
3764 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor1de05fe2009-04-17 18:18:49 +00003765}
3766
3767/// \brief Set the label of the given statement to the label
3768/// identified by ID.
3769///
3770/// Depending on the order in which the label and other statements
3771/// referencing that label occur, this operation may complete
3772/// immediately (updating the statement) or it may queue the
3773/// statement to be back-patched later.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003774void ASTReader::SetLabelOf(GotoStmt *S, unsigned ID) {
Douglas Gregor1de05fe2009-04-17 18:18:49 +00003775 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3776 if (Label != LabelStmts.end()) {
3777 // We've already seen this label, so set the label of the goto and
3778 // we're done.
3779 S->setLabel(Label->second);
3780 } else {
3781 // We haven't seen this label yet, so add this goto to the set of
3782 // unresolved goto statements.
3783 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
3784 }
3785}
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00003786
3787/// \brief Set the label of the given expression to the label
3788/// identified by ID.
3789///
3790/// Depending on the order in which the label and other statements
3791/// referencing that label occur, this operation may complete
3792/// immediately (updating the statement) or it may queue the
3793/// statement to be back-patched later.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003794void ASTReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00003795 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3796 if (Label != LabelStmts.end()) {
3797 // We've already seen this label, so set the label of the
3798 // label-address expression and we're done.
3799 S->setLabel(Label->second);
3800 } else {
3801 // We haven't seen this label yet, so add this label-address
3802 // expression to the set of unresolved label-address expressions.
3803 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
3804 }
3805}
Douglas Gregord89275b2009-07-06 18:54:52 +00003806
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003807void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00003808 assert(NumCurrentElementsDeserializing &&
3809 "FinishedDeserializing not paired with StartedDeserializing");
3810 if (NumCurrentElementsDeserializing == 1) {
Douglas Gregord89275b2009-07-06 18:54:52 +00003811 // If any identifiers with corresponding top-level declarations have
3812 // been loaded, load those declarations now.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00003813 while (!PendingIdentifierInfos.empty()) {
3814 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
3815 PendingIdentifierInfos.front().DeclIDs, true);
3816 PendingIdentifierInfos.pop_front();
Douglas Gregord89275b2009-07-06 18:54:52 +00003817 }
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003818
3819 // We are not in recursive loading, so it's safe to pass the "interesting"
3820 // decls to the consumer.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00003821 if (Consumer)
3822 PassInterestingDeclsToConsumer();
Douglas Gregord89275b2009-07-06 18:54:52 +00003823 }
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00003824 --NumCurrentElementsDeserializing;
Douglas Gregord89275b2009-07-06 18:54:52 +00003825}
Douglas Gregor501c1032010-08-19 00:28:17 +00003826
3827ASTReader::PerFileData::PerFileData()
3828 : StatCache(0), LocalNumSLocEntries(0), LocalNumTypes(0), TypeOffsets(0),
3829 LocalNumDecls(0), DeclOffsets(0), LocalNumIdentifiers(0),
3830 IdentifierOffsets(0), IdentifierTableData(0), IdentifierLookupTable(0),
3831 LocalNumMacroDefinitions(0), MacroDefinitionOffsets(0),
3832 NumPreallocatedPreprocessingEntities(0), SelectorLookupTable(0),
3833 SelectorLookupTableData(0), SelectorOffsets(0), LocalNumSelectors(0)
3834{}
3835
3836ASTReader::PerFileData::~PerFileData() {
3837 delete static_cast<ASTIdentifierLookupTable *>(IdentifierLookupTable);
3838 delete static_cast<ASTSelectorLookupTable *>(SelectorLookupTable);
3839}
3840