blob: eade7650791fe437dee09eb2799b01984e4b48e9 [file] [log] [blame]
Sebastian Redl904c9c82010-08-18 23:57:11 +00001//===--- ASTReader.cpp - AST File Reader ------------------------*- C++ -*-===//
Douglas Gregor2cf26342009-04-09 22:27:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Sebastian Redlc43b54c2010-08-18 23:56:43 +000010// This file defines the ASTReader class, which reads AST files.
Douglas Gregor2cf26342009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
Chris Lattner4c6f9522009-04-27 05:14:47 +000013
Sebastian Redl6ab7cd82010-08-18 23:57:17 +000014#include "clang/Serialization/ASTReader.h"
15#include "clang/Serialization/ASTDeserializationListener.h"
Douglas Gregor0a0428e2009-04-10 20:39:37 +000016#include "clang/Frontend/FrontendDiagnostic.h"
Daniel Dunbarc7162932009-11-11 23:58:53 +000017#include "clang/Frontend/Utils.h"
Douglas Gregore737f502010-08-12 20:07:10 +000018#include "clang/Sema/Sema.h"
Douglas Gregorfdd01722009-04-14 00:24:19 +000019#include "clang/AST/ASTConsumer.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000020#include "clang/AST/ASTContext.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000021#include "clang/AST/Expr.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000022#include "clang/AST/Type.h"
John McCalla1ee0c52009-10-16 21:56:05 +000023#include "clang/AST/TypeLocVisitor.h"
Chris Lattner42d42b52009-04-10 21:41:48 +000024#include "clang/Lex/MacroInfo.h"
Douglas Gregor6a5a23f2010-03-19 21:51:54 +000025#include "clang/Lex/PreprocessingRecord.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000026#include "clang/Lex/Preprocessor.h"
Steve Naroff83d63c72009-04-24 20:03:17 +000027#include "clang/Lex/HeaderSearch.h"
Douglas Gregor668c1a42009-04-21 22:25:48 +000028#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000029#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000030#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000031#include "clang/Basic/FileManager.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000032#include "clang/Basic/TargetInfo.h"
Douglas Gregor445e23e2009-10-05 21:07:28 +000033#include "clang/Basic/Version.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000034#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000035#include "llvm/Bitcode/BitstreamReader.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000036#include "llvm/Support/MemoryBuffer.h"
John McCall833ca992009-10-29 08:12:44 +000037#include "llvm/Support/ErrorHandling.h"
Daniel Dunbard5b21972009-11-18 19:50:41 +000038#include "llvm/System/Path.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000039#include <algorithm>
Douglas Gregore721f952009-04-28 18:58:38 +000040#include <iterator>
Douglas Gregor2cf26342009-04-09 22:27:44 +000041#include <cstdio>
Douglas Gregor4fed3f42009-04-27 18:38:38 +000042#include <sys/stat.h>
Douglas Gregor2cf26342009-04-09 22:27:44 +000043using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000044using namespace clang::serialization;
Douglas Gregor2cf26342009-04-09 22:27:44 +000045
46//===----------------------------------------------------------------------===//
Sebastian Redl3c7f4132010-08-18 23:57:06 +000047// PCH validator implementation
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000048//===----------------------------------------------------------------------===//
49
Sebastian Redl571db7f2010-08-18 23:56:56 +000050ASTReaderListener::~ASTReaderListener() {}
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000051
52bool
53PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts) {
54 const LangOptions &PPLangOpts = PP.getLangOptions();
55#define PARSE_LANGOPT_BENIGN(Option)
56#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
57 if (PPLangOpts.Option != LangOpts.Option) { \
58 Reader.Diag(DiagID) << LangOpts.Option << PPLangOpts.Option; \
59 return true; \
60 }
61
62 PARSE_LANGOPT_BENIGN(Trigraphs);
63 PARSE_LANGOPT_BENIGN(BCPLComment);
64 PARSE_LANGOPT_BENIGN(DollarIdents);
65 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
66 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
Chandler Carrutheb5d7b72010-04-17 20:17:31 +000067 PARSE_LANGOPT_IMPORTANT(GNUKeywords, diag::warn_pch_gnu_keywords);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000068 PARSE_LANGOPT_BENIGN(ImplicitInt);
69 PARSE_LANGOPT_BENIGN(Digraphs);
70 PARSE_LANGOPT_BENIGN(HexFloats);
71 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
72 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
73 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
74 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
75 PARSE_LANGOPT_BENIGN(CXXOperatorName);
76 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
77 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
78 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
Fariborz Jahanian412e7982010-02-09 19:31:38 +000079 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI2, diag::warn_pch_nonfragile_abi2);
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +000080 PARSE_LANGOPT_IMPORTANT(NoConstantCFStrings,
81 diag::warn_pch_no_constant_cfstrings);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000082 PARSE_LANGOPT_BENIGN(PascalStrings);
83 PARSE_LANGOPT_BENIGN(WritableStrings);
Mike Stump1eb44332009-09-09 15:08:12 +000084 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000085 diag::warn_pch_lax_vector_conversions);
Nate Begeman69cfb9b2009-06-25 22:57:40 +000086 PARSE_LANGOPT_IMPORTANT(AltiVec, diag::warn_pch_altivec);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000087 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
Daniel Dunbar73482882010-02-10 18:48:44 +000088 PARSE_LANGOPT_IMPORTANT(SjLjExceptions, diag::warn_pch_sjlj_exceptions);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000089 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
90 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
91 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
Mike Stump1eb44332009-09-09 15:08:12 +000092 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000093 diag::warn_pch_thread_safe_statics);
Daniel Dunbar5345c392009-09-03 04:54:28 +000094 PARSE_LANGOPT_IMPORTANT(POSIXThreads, diag::warn_pch_posix_threads);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000095 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
96 PARSE_LANGOPT_BENIGN(EmitAllDecls);
97 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
Chris Lattnera4d71452010-06-26 21:25:03 +000098 PARSE_LANGOPT_BENIGN(getSignedOverflowBehavior());
Mike Stump1eb44332009-09-09 15:08:12 +000099 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000100 diag::warn_pch_heinous_extensions);
101 // FIXME: Most of the options below are benign if the macro wasn't
102 // used. Unfortunately, this means that a PCH compiled without
103 // optimization can't be used with optimization turned on, even
104 // though the only thing that changes is whether __OPTIMIZE__ was
105 // defined... but if __OPTIMIZE__ never showed up in the header, it
106 // doesn't matter. We could consider making this some special kind
107 // of check.
108 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
109 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
110 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
111 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
112 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
113 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
114 PARSE_LANGOPT_IMPORTANT(AccessControl, diag::warn_pch_access_control);
115 PARSE_LANGOPT_IMPORTANT(CharIsSigned, diag::warn_pch_char_signed);
John Thompsona6fda122009-11-05 20:14:16 +0000116 PARSE_LANGOPT_IMPORTANT(ShortWChar, diag::warn_pch_short_wchar);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000117 if ((PPLangOpts.getGCMode() != 0) != (LangOpts.getGCMode() != 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000118 Reader.Diag(diag::warn_pch_gc_mode)
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000119 << LangOpts.getGCMode() << PPLangOpts.getGCMode();
120 return true;
121 }
122 PARSE_LANGOPT_BENIGN(getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000123 PARSE_LANGOPT_IMPORTANT(getStackProtectorMode(),
124 diag::warn_pch_stack_protector);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000125 PARSE_LANGOPT_BENIGN(InstantiationDepth);
Nate Begeman69cfb9b2009-06-25 22:57:40 +0000126 PARSE_LANGOPT_IMPORTANT(OpenCL, diag::warn_pch_opencl);
Mike Stump9c276ae2009-12-12 01:27:46 +0000127 PARSE_LANGOPT_BENIGN(CatchUndefined);
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000128 PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors);
Douglas Gregora0068fc2010-07-09 17:35:33 +0000129 PARSE_LANGOPT_BENIGN(SpellChecking);
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +0000130#undef PARSE_LANGOPT_IMPORTANT
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000131#undef PARSE_LANGOPT_BENIGN
132
133 return false;
134}
135
Daniel Dunbardc3c0d22009-11-11 00:52:11 +0000136bool PCHValidator::ReadTargetTriple(llvm::StringRef Triple) {
137 if (Triple == PP.getTargetInfo().getTriple().str())
138 return false;
139
140 Reader.Diag(diag::warn_pch_target_triple)
141 << Triple << PP.getTargetInfo().getTriple().str();
142 return true;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000143}
144
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000145struct EmptyStringRef {
Benjamin Kramerec1b1cc2010-07-14 23:19:41 +0000146 bool operator ()(llvm::StringRef r) const { return r.empty(); }
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000147};
148struct EmptyBlock {
149 bool operator ()(const PCHPredefinesBlock &r) const { return r.Data.empty(); }
150};
151
152static bool EqualConcatenations(llvm::SmallVector<llvm::StringRef, 2> L,
153 PCHPredefinesBlocks R) {
154 // First, sum up the lengths.
155 unsigned LL = 0, RL = 0;
156 for (unsigned I = 0, N = L.size(); I != N; ++I) {
157 LL += L[I].size();
158 }
159 for (unsigned I = 0, N = R.size(); I != N; ++I) {
160 RL += R[I].Data.size();
161 }
162 if (LL != RL)
163 return false;
164 if (LL == 0 && RL == 0)
165 return true;
166
167 // Kick out empty parts, they confuse the algorithm below.
168 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
169 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
170
171 // Do it the hard way. At this point, both vectors must be non-empty.
172 llvm::StringRef LR = L[0], RR = R[0].Data;
173 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
Daniel Dunbarc76c9e02010-07-16 00:00:11 +0000174 (void) RN;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000175 for (;;) {
176 // Compare the current pieces.
177 if (LR.size() == RR.size()) {
178 // If they're the same length, it's pretty easy.
179 if (LR != RR)
180 return false;
181 // Both pieces are done, advance.
182 ++LI;
183 ++RI;
184 // If either string is done, they're both done, since they're the same
185 // length.
186 if (LI == LN) {
187 assert(RI == RN && "Strings not the same length after all?");
188 return true;
189 }
190 LR = L[LI];
191 RR = R[RI].Data;
192 } else if (LR.size() < RR.size()) {
193 // Right piece is longer.
194 if (!RR.startswith(LR))
195 return false;
196 ++LI;
197 assert(LI != LN && "Strings not the same length after all?");
198 RR = RR.substr(LR.size());
199 LR = L[LI];
200 } else {
201 // Left piece is longer.
202 if (!LR.startswith(RR))
203 return false;
204 ++RI;
205 assert(RI != RN && "Strings not the same length after all?");
206 LR = LR.substr(RR.size());
207 RR = R[RI].Data;
208 }
209 }
210}
211
212static std::pair<FileID, llvm::StringRef::size_type>
213FindMacro(const PCHPredefinesBlocks &Buffers, llvm::StringRef MacroDef) {
214 std::pair<FileID, llvm::StringRef::size_type> Res;
215 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
216 Res.second = Buffers[I].Data.find(MacroDef);
217 if (Res.second != llvm::StringRef::npos) {
218 Res.first = Buffers[I].BufferID;
219 break;
220 }
221 }
222 return Res;
223}
224
225bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000226 llvm::StringRef OriginalFileName,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000227 std::string &SuggestedPredefines) {
Daniel Dunbarc7162932009-11-11 23:58:53 +0000228 // We are in the context of an implicit include, so the predefines buffer will
229 // have a #include entry for the PCH file itself (as normalized by the
230 // preprocessor initialization). Find it and skip over it in the checking
231 // below.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000232 llvm::SmallString<256> PCHInclude;
233 PCHInclude += "#include \"";
Daniel Dunbarc7162932009-11-11 23:58:53 +0000234 PCHInclude += NormalizeDashIncludePath(OriginalFileName);
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000235 PCHInclude += "\"\n";
236 std::pair<llvm::StringRef,llvm::StringRef> Split =
237 llvm::StringRef(PP.getPredefines()).split(PCHInclude.str());
238 llvm::StringRef Left = Split.first, Right = Split.second;
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000239 if (Left == PP.getPredefines()) {
240 Error("Missing PCH include entry!");
241 return true;
242 }
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000243
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000244 // If the concatenation of all the PCH buffers is equal to the adjusted
245 // command line, we're done.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000246 llvm::SmallVector<llvm::StringRef, 2> CommandLine;
247 CommandLine.push_back(Left);
248 CommandLine.push_back(Right);
249 if (EqualConcatenations(CommandLine, Buffers))
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000250 return false;
251
252 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000253
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000254 // The predefines buffers are different. Determine what the differences are,
255 // and whether they require us to reject the PCH file.
Daniel Dunbare6750492009-11-13 16:46:11 +0000256 llvm::SmallVector<llvm::StringRef, 8> PCHLines;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000257 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
258 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Daniel Dunbare6750492009-11-13 16:46:11 +0000259
260 llvm::SmallVector<llvm::StringRef, 8> CmdLineLines;
261 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
262 Right.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000263
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000264 // Sort both sets of predefined buffer lines, since we allow some extra
265 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000266 std::sort(CmdLineLines.begin(), CmdLineLines.end());
267 std::sort(PCHLines.begin(), PCHLines.end());
268
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000269 // Determine which predefines that were used to build the PCH file are missing
270 // from the command line.
271 std::vector<llvm::StringRef> MissingPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000272 std::set_difference(PCHLines.begin(), PCHLines.end(),
273 CmdLineLines.begin(), CmdLineLines.end(),
274 std::back_inserter(MissingPredefines));
275
276 bool MissingDefines = false;
277 bool ConflictingDefines = false;
278 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000279 llvm::StringRef Missing = MissingPredefines[I];
280 if (!Missing.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000281 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
282 return true;
283 }
Mike Stump1eb44332009-09-09 15:08:12 +0000284
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000285 // This is a macro definition. Determine the name of the macro we're
286 // defining.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000287 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000288 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000289 = Missing.find_first_of("( \n\r", StartOfMacroName);
290 assert(EndOfMacroName != std::string::npos &&
291 "Couldn't find the end of the macro name");
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000292 llvm::StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000293
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000294 // Determine whether this macro was given a different definition on the
295 // command line.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000296 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000297 std::string::size_type MacroDefLen = MacroDefStart.size();
Daniel Dunbare6750492009-11-13 16:46:11 +0000298 llvm::SmallVector<llvm::StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000299 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
300 MacroDefStart);
301 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000302 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000303 // Different macro; we're done.
304 ConflictPos = CmdLineLines.end();
Mike Stump1eb44332009-09-09 15:08:12 +0000305 break;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000306 }
Mike Stump1eb44332009-09-09 15:08:12 +0000307
308 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000309 "Invalid #define in predefines buffer?");
Mike Stump1eb44332009-09-09 15:08:12 +0000310 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000311 (*ConflictPos)[MacroDefLen] != '(')
312 continue; // Longer macro name; keep trying.
Mike Stump1eb44332009-09-09 15:08:12 +0000313
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000314 // We found a conflicting macro definition.
315 break;
316 }
Mike Stump1eb44332009-09-09 15:08:12 +0000317
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000318 if (ConflictPos != CmdLineLines.end()) {
319 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
320 << MacroName;
321
322 // Show the definition of this macro within the PCH file.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000323 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
324 FindMacro(Buffers, Missing);
325 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
326 SourceLocation PCHMissingLoc =
327 SourceMgr.getLocForStartOfFile(MacroLoc.first)
328 .getFileLocWithOffset(MacroLoc.second);
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000329 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000330
331 ConflictingDefines = true;
332 continue;
333 }
Mike Stump1eb44332009-09-09 15:08:12 +0000334
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000335 // If the macro doesn't conflict, then we'll just pick up the macro
336 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000337 if (ConflictingDefines)
338 continue; // Don't complain if there are already conflicting defs
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000340 if (!MissingDefines) {
341 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
342 MissingDefines = true;
343 }
344
345 // Show the definition of this macro within the PCH file.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000346 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
347 FindMacro(Buffers, Missing);
348 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
349 SourceLocation PCHMissingLoc =
350 SourceMgr.getLocForStartOfFile(MacroLoc.first)
351 .getFileLocWithOffset(MacroLoc.second);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000352 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
353 }
Mike Stump1eb44332009-09-09 15:08:12 +0000354
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000355 if (ConflictingDefines)
356 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000357
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000358 // Determine what predefines were introduced based on command-line
359 // parameters that were not present when building the PCH
360 // file. Extra #defines are okay, so long as the identifiers being
361 // defined were not used within the precompiled header.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000362 std::vector<llvm::StringRef> ExtraPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000363 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
364 PCHLines.begin(), PCHLines.end(),
Mike Stump1eb44332009-09-09 15:08:12 +0000365 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000366 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000367 llvm::StringRef &Extra = ExtraPredefines[I];
368 if (!Extra.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000369 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
370 return true;
371 }
372
373 // This is an extra macro definition. Determine the name of the
374 // macro we're defining.
375 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000376 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000377 = Extra.find_first_of("( \n\r", StartOfMacroName);
378 assert(EndOfMacroName != std::string::npos &&
379 "Couldn't find the end of the macro name");
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000380 llvm::StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000381
382 // Check whether this name was used somewhere in the PCH file. If
383 // so, defining it as a macro could change behavior, so we reject
384 // the PCH file.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000385 if (IdentifierInfo *II = Reader.get(MacroName)) {
Daniel Dunbar4fda42e2009-11-11 00:52:00 +0000386 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000387 return true;
388 }
389
390 // Add this definition to the suggested predefines buffer.
391 SuggestedPredefines += Extra;
392 SuggestedPredefines += '\n';
393 }
394
395 // If we get here, it's because the predefines buffer had compatible
396 // contents. Accept the PCH file.
397 return false;
398}
399
Douglas Gregor12fab312010-03-16 16:35:32 +0000400void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
401 unsigned ID) {
402 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
403 ++NumHeaderInfos;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000404}
405
406void PCHValidator::ReadCounter(unsigned Value) {
407 PP.setCounterValue(Value);
408}
409
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000410//===----------------------------------------------------------------------===//
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000411// AST reader implementation
Douglas Gregor668c1a42009-04-21 22:25:48 +0000412//===----------------------------------------------------------------------===//
413
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000414ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
Douglas Gregorfae3b2f2010-07-27 00:27:13 +0000415 const char *isysroot, bool DisableValidation)
Sebastian Redl30c514c2010-07-14 23:45:08 +0000416 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
417 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
418 Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
Sebastian Redl725cd962010-08-04 20:40:17 +0000419 Consumer(0), isysroot(isysroot), DisableValidation(DisableValidation),
420 NumStatHits(0), NumStatMisses(0), NumSLocEntriesRead(0),
421 TotalNumSLocEntries(0), NumStatementsRead(0), TotalNumStatements(0),
Sebastian Redlfa78dec2010-08-04 21:22:45 +0000422 NumMacrosRead(0), TotalNumMacros(0), NumSelectorsRead(0),
423 NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
424 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
Sebastian Redl725cd962010-08-04 20:40:17 +0000425 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
426 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000427 RelocatablePCH = false;
428}
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000429
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000430ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
Douglas Gregorfae3b2f2010-07-27 00:27:13 +0000431 Diagnostic &Diags, const char *isysroot,
432 bool DisableValidation)
Sebastian Redl30c514c2010-07-14 23:45:08 +0000433 : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
Sebastian Redl9137a522010-07-16 17:50:48 +0000434 Diags(Diags), SemaObj(0), PP(0), Context(0), Consumer(0),
Sebastian Redl725cd962010-08-04 20:40:17 +0000435 isysroot(isysroot), DisableValidation(DisableValidation), NumStatHits(0),
436 NumStatMisses(0), NumSLocEntriesRead(0), TotalNumSLocEntries(0),
437 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
Sebastian Redlfa78dec2010-08-04 21:22:45 +0000438 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
439 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
Sebastian Redl725cd962010-08-04 20:40:17 +0000440 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
441 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
442 NumCurrentElementsDeserializing(0) {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000443 RelocatablePCH = false;
444}
Chris Lattner4c6f9522009-04-27 05:14:47 +0000445
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000446ASTReader::~ASTReader() {
Sebastian Redl9137a522010-07-16 17:50:48 +0000447 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
448 delete Chain[e - i - 1];
449}
450
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000451ASTReader::PerFileData::PerFileData()
Sebastian Redl12d6da02010-07-19 22:06:55 +0000452 : StatCache(0), LocalNumSLocEntries(0), LocalNumTypes(0), TypeOffsets(0),
Sebastian Redl2da08f92010-07-19 22:28:42 +0000453 LocalNumDecls(0), DeclOffsets(0), LocalNumIdentifiers(0),
Sebastian Redl04e6fd42010-07-21 20:07:32 +0000454 IdentifierOffsets(0), IdentifierTableData(0), IdentifierLookupTable(0),
455 LocalNumMacroDefinitions(0), MacroDefinitionOffsets(0),
Sebastian Redl725cd962010-08-04 20:40:17 +0000456 NumPreallocatedPreprocessingEntities(0), SelectorLookupTable(0),
457 SelectorLookupTableData(0), SelectorOffsets(0), LocalNumSelectors(0)
Sebastian Redl9137a522010-07-16 17:50:48 +0000458{}
Chris Lattner4c6f9522009-04-27 05:14:47 +0000459
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000460void
Sebastian Redl571db7f2010-08-18 23:56:56 +0000461ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000462 DeserializationListener = Listener;
463 if (DeserializationListener)
464 DeserializationListener->SetReader(this);
465}
466
Chris Lattner4c6f9522009-04-27 05:14:47 +0000467
Douglas Gregor668c1a42009-04-21 22:25:48 +0000468namespace {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000469class ASTSelectorLookupTrait {
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000470 ASTReader &Reader;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000471
472public:
Sebastian Redl5d050072010-08-04 17:20:04 +0000473 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000474 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +0000475 ObjCMethodList Instance, Factory;
476 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000477
478 typedef Selector external_key_type;
479 typedef external_key_type internal_key_type;
480
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000481 explicit ASTSelectorLookupTrait(ASTReader &Reader) : Reader(Reader) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000482
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000483 static bool EqualKey(const internal_key_type& a,
484 const internal_key_type& b) {
485 return a == b;
486 }
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000488 static unsigned ComputeHash(Selector Sel) {
489 unsigned N = Sel.getNumArgs();
490 if (N == 0)
491 ++N;
492 unsigned R = 5381;
493 for (unsigned I = 0; I != N; ++I)
494 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
Daniel Dunbar2596e422009-10-17 23:52:28 +0000495 R = llvm::HashString(II->getName(), R);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000496 return R;
497 }
Mike Stump1eb44332009-09-09 15:08:12 +0000498
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000499 // This hopefully will just get inlined and removed by the optimizer.
500 static const internal_key_type&
501 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump1eb44332009-09-09 15:08:12 +0000502
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000503 static std::pair<unsigned, unsigned>
504 ReadKeyDataLength(const unsigned char*& d) {
505 using namespace clang::io;
506 unsigned KeyLen = ReadUnalignedLE16(d);
507 unsigned DataLen = ReadUnalignedLE16(d);
508 return std::make_pair(KeyLen, DataLen);
509 }
Mike Stump1eb44332009-09-09 15:08:12 +0000510
Douglas Gregor83941df2009-04-25 17:48:32 +0000511 internal_key_type ReadKey(const unsigned char* d, unsigned) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000512 using namespace clang::io;
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000513 SelectorTable &SelTable = Reader.getContext()->Selectors;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000514 unsigned N = ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +0000515 IdentifierInfo *FirstII
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000516 = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
517 if (N == 0)
518 return SelTable.getNullarySelector(FirstII);
519 else if (N == 1)
520 return SelTable.getUnarySelector(FirstII);
521
522 llvm::SmallVector<IdentifierInfo *, 16> Args;
523 Args.push_back(FirstII);
524 for (unsigned I = 1; I != N; ++I)
525 Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)));
526
Douglas Gregor75fdb232009-05-22 22:45:36 +0000527 return SelTable.getSelector(N, Args.data());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000528 }
Mike Stump1eb44332009-09-09 15:08:12 +0000529
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000530 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
531 using namespace clang::io;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000532
533 data_type Result;
534
Sebastian Redl5d050072010-08-04 17:20:04 +0000535 Result.ID = ReadUnalignedLE32(d);
536 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
537 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
538
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000539 // Load instance methods
540 ObjCMethodList *Prev = 0;
541 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000542 ObjCMethodDecl *Method
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000543 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl5d050072010-08-04 17:20:04 +0000544 if (!Result.Instance.Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000545 // This is the first method, which is the easy case.
Sebastian Redl5d050072010-08-04 17:20:04 +0000546 Result.Instance.Method = Method;
547 Prev = &Result.Instance;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000548 continue;
549 }
550
Ted Kremenek298ed872010-02-11 00:53:01 +0000551 ObjCMethodList *Mem =
552 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
553 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000554 Prev = Prev->Next;
555 }
556
557 // Load factory methods
558 Prev = 0;
559 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000560 ObjCMethodDecl *Method
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000561 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl5d050072010-08-04 17:20:04 +0000562 if (!Result.Factory.Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000563 // This is the first method, which is the easy case.
Sebastian Redl5d050072010-08-04 17:20:04 +0000564 Result.Factory.Method = Method;
565 Prev = &Result.Factory;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000566 continue;
567 }
568
Ted Kremenek298ed872010-02-11 00:53:01 +0000569 ObjCMethodList *Mem =
570 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
571 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000572 Prev = Prev->Next;
573 }
574
575 return Result;
576 }
577};
Mike Stump1eb44332009-09-09 15:08:12 +0000578
579} // end anonymous namespace
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000580
581/// \brief The on-disk hash table used for the global method pool.
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000582typedef OnDiskChainedHashTable<ASTSelectorLookupTrait>
583 ASTSelectorLookupTable;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000584
585namespace {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000586class ASTIdentifierLookupTrait {
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000587 ASTReader &Reader;
Sebastian Redld27d3fc2010-07-21 22:31:37 +0000588 llvm::BitstreamCursor &Stream;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000589
590 // If we know the IdentifierInfo in advance, it is here and we will
591 // not build a new one. Used when deserializing information about an
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000592 // identifier that was constructed before the AST file was read.
Douglas Gregor668c1a42009-04-21 22:25:48 +0000593 IdentifierInfo *KnownII;
594
595public:
596 typedef IdentifierInfo * data_type;
597
598 typedef const std::pair<const char*, unsigned> external_key_type;
599
600 typedef external_key_type internal_key_type;
601
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000602 ASTIdentifierLookupTrait(ASTReader &Reader, llvm::BitstreamCursor &Stream,
Sebastian Redld27d3fc2010-07-21 22:31:37 +0000603 IdentifierInfo *II = 0)
604 : Reader(Reader), Stream(Stream), KnownII(II) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000605
Douglas Gregor668c1a42009-04-21 22:25:48 +0000606 static bool EqualKey(const internal_key_type& a,
607 const internal_key_type& b) {
608 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
609 : false;
610 }
Mike Stump1eb44332009-09-09 15:08:12 +0000611
Douglas Gregor668c1a42009-04-21 22:25:48 +0000612 static unsigned ComputeHash(const internal_key_type& a) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000613 return llvm::HashString(llvm::StringRef(a.first, a.second));
Douglas Gregor668c1a42009-04-21 22:25:48 +0000614 }
Mike Stump1eb44332009-09-09 15:08:12 +0000615
Douglas Gregor668c1a42009-04-21 22:25:48 +0000616 // This hopefully will just get inlined and removed by the optimizer.
617 static const internal_key_type&
618 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump1eb44332009-09-09 15:08:12 +0000619
Douglas Gregor668c1a42009-04-21 22:25:48 +0000620 static std::pair<unsigned, unsigned>
621 ReadKeyDataLength(const unsigned char*& d) {
622 using namespace clang::io;
Douglas Gregor5f8e3302009-04-25 20:26:24 +0000623 unsigned DataLen = ReadUnalignedLE16(d);
Douglas Gregord6595a42009-04-25 21:04:17 +0000624 unsigned KeyLen = ReadUnalignedLE16(d);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000625 return std::make_pair(KeyLen, DataLen);
626 }
Mike Stump1eb44332009-09-09 15:08:12 +0000627
Douglas Gregor668c1a42009-04-21 22:25:48 +0000628 static std::pair<const char*, unsigned>
629 ReadKey(const unsigned char* d, unsigned n) {
630 assert(n >= 2 && d[n-1] == '\0');
631 return std::make_pair((const char*) d, n-1);
632 }
Mike Stump1eb44332009-09-09 15:08:12 +0000633
634 IdentifierInfo *ReadData(const internal_key_type& k,
Douglas Gregor668c1a42009-04-21 22:25:48 +0000635 const unsigned char* d,
636 unsigned DataLen) {
637 using namespace clang::io;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000638 IdentID ID = ReadUnalignedLE32(d);
Douglas Gregora92193e2009-04-28 21:18:29 +0000639 bool IsInteresting = ID & 0x01;
640
641 // Wipe out the "is interesting" bit.
642 ID = ID >> 1;
643
644 if (!IsInteresting) {
Sebastian Redl083abdf2010-07-27 23:01:28 +0000645 // For uninteresting identifiers, just build the IdentifierInfo
Douglas Gregora92193e2009-04-28 21:18:29 +0000646 // and associate it with the persistent ID.
647 IdentifierInfo *II = KnownII;
648 if (!II)
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000649 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregora92193e2009-04-28 21:18:29 +0000650 Reader.SetIdentifierInfo(ID, II);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000651 II->setIsFromAST();
Douglas Gregora92193e2009-04-28 21:18:29 +0000652 return II;
653 }
654
Douglas Gregor5998da52009-04-28 21:32:13 +0000655 unsigned Bits = ReadUnalignedLE16(d);
Douglas Gregor2deaea32009-04-22 18:49:13 +0000656 bool CPlusPlusOperatorKeyword = Bits & 0x01;
657 Bits >>= 1;
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000658 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
659 Bits >>= 1;
Douglas Gregor2deaea32009-04-22 18:49:13 +0000660 bool Poisoned = Bits & 0x01;
661 Bits >>= 1;
662 bool ExtensionToken = Bits & 0x01;
663 Bits >>= 1;
664 bool hasMacroDefinition = Bits & 0x01;
665 Bits >>= 1;
666 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
667 Bits >>= 10;
Mike Stump1eb44332009-09-09 15:08:12 +0000668
Douglas Gregor2deaea32009-04-22 18:49:13 +0000669 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregor5998da52009-04-28 21:32:13 +0000670 DataLen -= 6;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000671
672 // Build the IdentifierInfo itself and link the identifier ID with
673 // the new IdentifierInfo.
674 IdentifierInfo *II = KnownII;
675 if (!II)
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000676 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000677 Reader.SetIdentifierInfo(ID, II);
678
Douglas Gregor2deaea32009-04-22 18:49:13 +0000679 // Set or check the various bits in the IdentifierInfo structure.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000680 // Token IDs are read-only.
681 if (HasRevertedTokenIDToIdentifier)
682 II->RevertTokenIDToIdentifier();
Douglas Gregor2deaea32009-04-22 18:49:13 +0000683 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
Mike Stump1eb44332009-09-09 15:08:12 +0000684 assert(II->isExtensionToken() == ExtensionToken &&
Douglas Gregor2deaea32009-04-22 18:49:13 +0000685 "Incorrect extension token flag");
686 (void)ExtensionToken;
687 II->setIsPoisoned(Poisoned);
688 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
689 "Incorrect C++ operator keyword flag");
690 (void)CPlusPlusOperatorKeyword;
691
Douglas Gregor37e26842009-04-21 23:56:24 +0000692 // If this identifier is a macro, deserialize the macro
693 // definition.
694 if (hasMacroDefinition) {
Douglas Gregor5998da52009-04-28 21:32:13 +0000695 uint32_t Offset = ReadUnalignedLE32(d);
Sebastian Redld27d3fc2010-07-21 22:31:37 +0000696 Reader.ReadMacroRecord(Stream, Offset);
Douglas Gregor5998da52009-04-28 21:32:13 +0000697 DataLen -= 4;
Douglas Gregor37e26842009-04-21 23:56:24 +0000698 }
Douglas Gregor668c1a42009-04-21 22:25:48 +0000699
700 // Read all of the declarations visible at global scope with this
701 // name.
Chris Lattner6bf690f2009-04-27 22:17:41 +0000702 if (Reader.getContext() == 0) return II;
Douglas Gregord89275b2009-07-06 18:54:52 +0000703 if (DataLen > 0) {
704 llvm::SmallVector<uint32_t, 4> DeclIDs;
705 for (; DataLen > 0; DataLen -= 4)
706 DeclIDs.push_back(ReadUnalignedLE32(d));
707 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000708 }
Mike Stump1eb44332009-09-09 15:08:12 +0000709
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000710 II->setIsFromAST();
Douglas Gregor668c1a42009-04-21 22:25:48 +0000711 return II;
712 }
713};
Mike Stump1eb44332009-09-09 15:08:12 +0000714
715} // end anonymous namespace
Douglas Gregor668c1a42009-04-21 22:25:48 +0000716
717/// \brief The on-disk hash table used to contain information about
718/// all of the identifiers in the program.
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000719typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait>
720 ASTIdentifierLookupTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000721
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000722void ASTReader::Error(const char *Msg) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000723 Diag(diag::err_fe_pch_malformed) << Msg;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000724}
725
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000726/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000727bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000728 if (Listener)
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000729 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000730 ActualOriginalFileName,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000731 SuggestedPredefines);
Douglas Gregore721f952009-04-28 18:58:38 +0000732 return false;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000733}
734
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000735//===----------------------------------------------------------------------===//
736// Source Manager Deserialization
737//===----------------------------------------------------------------------===//
738
Douglas Gregorbd945002009-04-13 16:31:14 +0000739/// \brief Read the line table in the source manager block.
740/// \returns true if ther was an error.
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000741bool ASTReader::ParseLineTable(llvm::SmallVectorImpl<uint64_t> &Record) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000742 unsigned Idx = 0;
743 LineTableInfo &LineTable = SourceMgr.getLineTable();
744
745 // Parse the file names
Douglas Gregorff0a9872009-04-13 17:12:42 +0000746 std::map<int, int> FileIDs;
747 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000748 // Extract the file name
749 unsigned FilenameLen = Record[Idx++];
750 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
751 Idx += FilenameLen;
Douglas Gregore650c8c2009-07-07 00:12:59 +0000752 MaybeAddSystemRootToFilename(Filename);
Mike Stump1eb44332009-09-09 15:08:12 +0000753 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
Douglas Gregorff0a9872009-04-13 17:12:42 +0000754 Filename.size());
Douglas Gregorbd945002009-04-13 16:31:14 +0000755 }
756
757 // Parse the line entries
758 std::vector<LineEntry> Entries;
759 while (Idx < Record.size()) {
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000760 int FID = Record[Idx++];
Douglas Gregorbd945002009-04-13 16:31:14 +0000761
762 // Extract the line entries
763 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000764 assert(NumEntries && "Numentries is 00000");
Douglas Gregorbd945002009-04-13 16:31:14 +0000765 Entries.clear();
766 Entries.reserve(NumEntries);
767 for (unsigned I = 0; I != NumEntries; ++I) {
768 unsigned FileOffset = Record[Idx++];
769 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000770 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump1eb44332009-09-09 15:08:12 +0000771 SrcMgr::CharacteristicKind FileKind
Douglas Gregorbd945002009-04-13 16:31:14 +0000772 = (SrcMgr::CharacteristicKind)Record[Idx++];
773 unsigned IncludeOffset = Record[Idx++];
774 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
775 FileKind, IncludeOffset));
776 }
777 LineTable.AddEntry(FID, Entries);
778 }
779
780 return false;
781}
782
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000783namespace {
784
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000785class ASTStatData {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000786public:
787 const bool hasStat;
788 const ino_t ino;
789 const dev_t dev;
790 const mode_t mode;
791 const time_t mtime;
792 const off_t size;
Mike Stump1eb44332009-09-09 15:08:12 +0000793
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000794 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Mike Stump1eb44332009-09-09 15:08:12 +0000795 : hasStat(true), ino(i), dev(d), mode(mo), mtime(m), size(s) {}
796
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000797 ASTStatData()
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000798 : hasStat(false), ino(0), dev(0), mode(0), mtime(0), size(0) {}
799};
800
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000801class ASTStatLookupTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000802 public:
803 typedef const char *external_key_type;
804 typedef const char *internal_key_type;
805
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000806 typedef ASTStatData data_type;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000807
808 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000809 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000810 }
811
812 static internal_key_type GetInternalKey(const char *path) { return path; }
813
814 static bool EqualKey(internal_key_type a, internal_key_type b) {
815 return strcmp(a, b) == 0;
816 }
817
818 static std::pair<unsigned, unsigned>
819 ReadKeyDataLength(const unsigned char*& d) {
820 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
821 unsigned DataLen = (unsigned) *d++;
822 return std::make_pair(KeyLen + 1, DataLen);
823 }
824
825 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
826 return (const char *)d;
827 }
828
829 static data_type ReadData(const internal_key_type, const unsigned char *d,
830 unsigned /*DataLen*/) {
831 using namespace clang::io;
832
833 if (*d++ == 1)
834 return data_type();
835
836 ino_t ino = (ino_t) ReadUnalignedLE32(d);
837 dev_t dev = (dev_t) ReadUnalignedLE32(d);
838 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +0000839 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000840 off_t size = (off_t) ReadUnalignedLE64(d);
841 return data_type(ino, dev, mode, mtime, size);
842 }
843};
844
845/// \brief stat() cache for precompiled headers.
846///
847/// This cache is very similar to the stat cache used by pretokenized
848/// headers.
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000849class ASTStatCache : public StatSysCallCache {
850 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000851 CacheTy *Cache;
852
853 unsigned &NumStatHits, &NumStatMisses;
Mike Stump1eb44332009-09-09 15:08:12 +0000854public:
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000855 ASTStatCache(const unsigned char *Buckets,
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000856 const unsigned char *Base,
857 unsigned &NumStatHits,
Mike Stump1eb44332009-09-09 15:08:12 +0000858 unsigned &NumStatMisses)
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000859 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
860 Cache = CacheTy::Create(Buckets, Base);
861 }
862
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000863 ~ASTStatCache() { delete Cache; }
Mike Stump1eb44332009-09-09 15:08:12 +0000864
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000865 int stat(const char *path, struct stat *buf) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000866 // Do the lookup for the file's data in the AST file.
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000867 CacheTy::iterator I = Cache->find(path);
868
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000869 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000870 if (I == Cache->end()) {
871 ++NumStatMisses;
Douglas Gregor52e71082009-10-16 18:18:30 +0000872 return StatSysCallCache::stat(path, buf);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000873 }
Mike Stump1eb44332009-09-09 15:08:12 +0000874
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000875 ++NumStatHits;
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000876 ASTStatData Data = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000877
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000878 if (!Data.hasStat)
879 return 1;
880
881 buf->st_ino = Data.ino;
882 buf->st_dev = Data.dev;
883 buf->st_mtime = Data.mtime;
884 buf->st_mode = Data.mode;
885 buf->st_size = Data.size;
886 return 0;
887 }
888};
889} // end anonymous namespace
890
891
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000892/// \brief Read a source manager block
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000893ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(PerFileData &F) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000894 using namespace SrcMgr;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000895
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000896 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +0000897
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000898 // Set the source-location entry cursor to the current position in
899 // the stream. This cursor will be used to read the contents of the
900 // source manager block initially, and then lazily read
901 // source-location entries as needed.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000902 SLocEntryCursor = F.Stream;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000903
904 // The stream itself is going to skip over the source manager block.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000905 if (F.Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000906 Error("malformed block record in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000907 return Failure;
908 }
909
910 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000911 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000912 Error("malformed source manager block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +0000913 return Failure;
914 }
Douglas Gregor14f79002009-04-10 03:52:48 +0000915
Douglas Gregor14f79002009-04-10 03:52:48 +0000916 RecordData Record;
917 while (true) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000918 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregor14f79002009-04-10 03:52:48 +0000919 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000920 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000921 Error("error at end of Source Manager block in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +0000922 return Failure;
923 }
Douglas Gregore1d918e2009-04-10 23:10:45 +0000924 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +0000925 }
Mike Stump1eb44332009-09-09 15:08:12 +0000926
Douglas Gregor14f79002009-04-10 03:52:48 +0000927 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
928 // No known subblocks, always skip them.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000929 SLocEntryCursor.ReadSubBlockID();
930 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000931 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +0000932 return Failure;
933 }
Douglas Gregor14f79002009-04-10 03:52:48 +0000934 continue;
935 }
Mike Stump1eb44332009-09-09 15:08:12 +0000936
Douglas Gregor14f79002009-04-10 03:52:48 +0000937 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000938 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregor14f79002009-04-10 03:52:48 +0000939 continue;
940 }
Mike Stump1eb44332009-09-09 15:08:12 +0000941
Douglas Gregor14f79002009-04-10 03:52:48 +0000942 // Read a record.
943 const char *BlobStart;
944 unsigned BlobLen;
945 Record.clear();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000946 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000947 default: // Default behavior: ignore.
948 break;
949
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000950 case SM_LINE_TABLE:
Sebastian Redl518d8cb2010-07-20 21:20:32 +0000951 if (ParseLineTable(Record))
Douglas Gregorbd945002009-04-13 16:31:14 +0000952 return Failure;
Chris Lattner2c78b872009-04-14 23:22:57 +0000953 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +0000954
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000955 case SM_SLOC_FILE_ENTRY:
956 case SM_SLOC_BUFFER_ENTRY:
957 case SM_SLOC_INSTANTIATION_ENTRY:
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000958 // Once we hit one of the source location entries, we're done.
959 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +0000960 }
961 }
962}
963
Sebastian Redl190faf72010-07-20 21:50:20 +0000964/// \brief Get a cursor that's correctly positioned for reading the source
965/// location entry with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000966llvm::BitstreamCursor &ASTReader::SLocCursorForID(unsigned ID) {
Sebastian Redl190faf72010-07-20 21:50:20 +0000967 assert(ID != 0 && ID <= TotalNumSLocEntries &&
968 "SLocCursorForID should only be called for real IDs.");
969
970 ID -= 1;
971 PerFileData *F = 0;
972 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
973 F = Chain[N - I - 1];
974 if (ID < F->LocalNumSLocEntries)
975 break;
976 ID -= F->LocalNumSLocEntries;
977 }
978 assert(F && F->LocalNumSLocEntries > ID && "Chain corrupted");
979
980 F->SLocEntryCursor.JumpToBit(F->SLocOffsets[ID]);
981 return F->SLocEntryCursor;
982}
983
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000984/// \brief Read in the source location entry with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000985ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(unsigned ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000986 if (ID == 0)
987 return Success;
988
989 if (ID > TotalNumSLocEntries) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000990 Error("source location entry ID out-of-range for AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000991 return Failure;
992 }
993
Sebastian Redl190faf72010-07-20 21:50:20 +0000994 llvm::BitstreamCursor &SLocEntryCursor = SLocCursorForID(ID);
Sebastian Redl9137a522010-07-16 17:50:48 +0000995
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000996 ++NumSLocEntriesRead;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000997 unsigned Code = SLocEntryCursor.ReadCode();
998 if (Code == llvm::bitc::END_BLOCK ||
999 Code == llvm::bitc::ENTER_SUBBLOCK ||
1000 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001001 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001002 return Failure;
1003 }
1004
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001005 RecordData Record;
1006 const char *BlobStart;
1007 unsigned BlobLen;
1008 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1009 default:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001010 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001011 return Failure;
1012
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001013 case SM_SLOC_FILE_ENTRY: {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001014 std::string Filename(BlobStart, BlobStart + BlobLen);
1015 MaybeAddSystemRootToFilename(Filename);
1016 const FileEntry *File = FileMgr.getFile(Filename);
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001017 if (File == 0) {
1018 std::string ErrorStr = "could not find file '";
Douglas Gregore650c8c2009-07-07 00:12:59 +00001019 ErrorStr += Filename;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001020 ErrorStr += "' referenced by AST file";
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001021 Error(ErrorStr.c_str());
1022 return Failure;
1023 }
Mike Stump1eb44332009-09-09 15:08:12 +00001024
Douglas Gregor2d52be52010-03-21 22:49:54 +00001025 if (Record.size() < 10) {
Ted Kremenek1857f622010-03-18 21:23:05 +00001026 Error("source location entry is incorrect");
1027 return Failure;
1028 }
1029
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001030 if (!DisableValidation &&
1031 ((off_t)Record[4] != File->getSize()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001032#if !defined(LLVM_ON_WIN32)
1033 // In our regression testing, the Windows file system seems to
1034 // have inconsistent modification times that sometimes
1035 // erroneously trigger this error-handling path.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001036 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001037#endif
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001038 )) {
Douglas Gregor2d52be52010-03-21 22:49:54 +00001039 Diag(diag::err_fe_pch_file_modified)
1040 << Filename;
1041 return Failure;
1042 }
1043
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001044 FileID FID = SourceMgr.createFileID(File,
1045 SourceLocation::getFromRawEncoding(Record[1]),
1046 (SrcMgr::CharacteristicKind)Record[2],
1047 ID, Record[0]);
1048 if (Record[3])
1049 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile())
1050 .setHasLineDirectives();
1051
Douglas Gregor12fab312010-03-16 16:35:32 +00001052 // Reconstruct header-search information for this file.
1053 HeaderFileInfo HFI;
Douglas Gregor2d52be52010-03-21 22:49:54 +00001054 HFI.isImport = Record[6];
1055 HFI.DirInfo = Record[7];
1056 HFI.NumIncludes = Record[8];
1057 HFI.ControllingMacroID = Record[9];
Douglas Gregor12fab312010-03-16 16:35:32 +00001058 if (Listener)
1059 Listener->ReadHeaderFileInfo(HFI, File->getUID());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001060 break;
1061 }
1062
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001063 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001064 const char *Name = BlobStart;
1065 unsigned Offset = Record[0];
1066 unsigned Code = SLocEntryCursor.ReadCode();
1067 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001068 unsigned RecCode
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001069 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001070
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001071 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001072 Error("AST record has invalid code");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001073 return Failure;
1074 }
1075
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001076 llvm::MemoryBuffer *Buffer
Chris Lattnera0a270c2010-04-05 22:42:27 +00001077 = llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(BlobStart, BlobLen - 1),
1078 Name);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001079 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset);
Mike Stump1eb44332009-09-09 15:08:12 +00001080
Douglas Gregor92b059e2009-04-28 20:33:11 +00001081 if (strcmp(Name, "<built-in>") == 0) {
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +00001082 PCHPredefinesBlock Block = {
1083 BufferID,
1084 llvm::StringRef(BlobStart, BlobLen - 1)
1085 };
1086 PCHPredefinesBuffers.push_back(Block);
Douglas Gregor92b059e2009-04-28 20:33:11 +00001087 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001088
1089 break;
1090 }
1091
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001092 case SM_SLOC_INSTANTIATION_ENTRY: {
Mike Stump1eb44332009-09-09 15:08:12 +00001093 SourceLocation SpellingLoc
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001094 = SourceLocation::getFromRawEncoding(Record[1]);
1095 SourceMgr.createInstantiationLoc(SpellingLoc,
1096 SourceLocation::getFromRawEncoding(Record[2]),
1097 SourceLocation::getFromRawEncoding(Record[3]),
1098 Record[4],
1099 ID,
1100 Record[0]);
1101 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001102 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001103 }
1104
1105 return Success;
1106}
1107
Chris Lattner6367f6d2009-04-27 01:05:14 +00001108/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1109/// specified cursor. Read the abbreviations that are at the top of the block
1110/// and then leave the cursor pointing into the block.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001111bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattner6367f6d2009-04-27 01:05:14 +00001112 unsigned BlockID) {
1113 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001114 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001115 return Failure;
1116 }
Mike Stump1eb44332009-09-09 15:08:12 +00001117
Chris Lattner6367f6d2009-04-27 01:05:14 +00001118 while (true) {
1119 unsigned Code = Cursor.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00001120
Chris Lattner6367f6d2009-04-27 01:05:14 +00001121 // We expect all abbrevs to be at the start of the block.
1122 if (Code != llvm::bitc::DEFINE_ABBREV)
1123 return false;
1124 Cursor.ReadAbbrevRecord();
1125 }
1126}
1127
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001128void ASTReader::ReadMacroRecord(llvm::BitstreamCursor &Stream, uint64_t Offset){
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001129 assert(PP && "Forgot to set Preprocessor ?");
Mike Stump1eb44332009-09-09 15:08:12 +00001130
Douglas Gregor37e26842009-04-21 23:56:24 +00001131 // Keep track of where we are in the stream, then jump back there
1132 // after reading this macro.
1133 SavedStreamPosition SavedPosition(Stream);
1134
1135 Stream.JumpToBit(Offset);
1136 RecordData Record;
1137 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1138 MacroInfo *Macro = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001139
Douglas Gregor37e26842009-04-21 23:56:24 +00001140 while (true) {
1141 unsigned Code = Stream.ReadCode();
1142 switch (Code) {
1143 case llvm::bitc::END_BLOCK:
1144 return;
1145
1146 case llvm::bitc::ENTER_SUBBLOCK:
1147 // No known subblocks, always skip them.
1148 Stream.ReadSubBlockID();
1149 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001150 Error("malformed block record in AST file");
Douglas Gregor37e26842009-04-21 23:56:24 +00001151 return;
1152 }
1153 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001154
Douglas Gregor37e26842009-04-21 23:56:24 +00001155 case llvm::bitc::DEFINE_ABBREV:
1156 Stream.ReadAbbrevRecord();
1157 continue;
1158 default: break;
1159 }
1160
1161 // Read a record.
1162 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001163 PreprocessorRecordTypes RecType =
1164 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
Douglas Gregor37e26842009-04-21 23:56:24 +00001165 switch (RecType) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001166 case PP_MACRO_OBJECT_LIKE:
1167 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001168 // If we already have a macro, that means that we've hit the end
1169 // of the definition of the macro we were looking for. We're
1170 // done.
1171 if (Macro)
1172 return;
1173
1174 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1175 if (II == 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001176 Error("macro must have a name in AST file");
Douglas Gregor37e26842009-04-21 23:56:24 +00001177 return;
1178 }
1179 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
1180 bool isUsed = Record[2];
Mike Stump1eb44332009-09-09 15:08:12 +00001181
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001182 MacroInfo *MI = PP->AllocateMacroInfo(Loc);
Douglas Gregor37e26842009-04-21 23:56:24 +00001183 MI->setIsUsed(isUsed);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001184 MI->setIsFromAST();
Mike Stump1eb44332009-09-09 15:08:12 +00001185
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001186 unsigned NextIndex = 3;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001187 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregor37e26842009-04-21 23:56:24 +00001188 // Decode function-like macro info.
1189 bool isC99VarArgs = Record[3];
1190 bool isGNUVarArgs = Record[4];
1191 MacroArgs.clear();
1192 unsigned NumArgs = Record[5];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001193 NextIndex = 6 + NumArgs;
Douglas Gregor37e26842009-04-21 23:56:24 +00001194 for (unsigned i = 0; i != NumArgs; ++i)
1195 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1196
1197 // Install function-like macro info.
1198 MI->setIsFunctionLike();
1199 if (isC99VarArgs) MI->setIsC99Varargs();
1200 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor75fdb232009-05-22 22:45:36 +00001201 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001202 PP->getPreprocessorAllocator());
Douglas Gregor37e26842009-04-21 23:56:24 +00001203 }
1204
1205 // Finally, install the macro.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001206 PP->setMacroInfo(II, MI);
Douglas Gregor37e26842009-04-21 23:56:24 +00001207
1208 // Remember that we saw this macro last so that we add the tokens that
1209 // form its body to it.
1210 Macro = MI;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001211
1212 if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) {
1213 // We have a macro definition. Load it now.
1214 PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro,
1215 getMacroDefinition(Record[NextIndex]));
1216 }
1217
Douglas Gregor37e26842009-04-21 23:56:24 +00001218 ++NumMacrosRead;
1219 break;
1220 }
Mike Stump1eb44332009-09-09 15:08:12 +00001221
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001222 case PP_TOKEN: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001223 // If we see a TOKEN before a PP_MACRO_*, then the file is
1224 // erroneous, just pretend we didn't see this.
1225 if (Macro == 0) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001226
Douglas Gregor37e26842009-04-21 23:56:24 +00001227 Token Tok;
1228 Tok.startToken();
1229 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
1230 Tok.setLength(Record[1]);
1231 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1232 Tok.setIdentifierInfo(II);
1233 Tok.setKind((tok::TokenKind)Record[3]);
1234 Tok.setFlag((Token::TokenFlags)Record[4]);
1235 Macro->AddTokenToBody(Tok);
1236 break;
1237 }
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001238
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001239 case PP_MACRO_INSTANTIATION: {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001240 // If we already have a macro, that means that we've hit the end
1241 // of the definition of the macro we were looking for. We're
1242 // done.
1243 if (Macro)
1244 return;
1245
1246 if (!PP->getPreprocessingRecord()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001247 Error("missing preprocessing record in AST file");
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001248 return;
1249 }
1250
1251 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1252 if (PPRec.getPreprocessedEntity(Record[0]))
1253 return;
1254
1255 MacroInstantiation *MI
1256 = new (PPRec) MacroInstantiation(DecodeIdentifierInfo(Record[3]),
1257 SourceRange(
1258 SourceLocation::getFromRawEncoding(Record[1]),
1259 SourceLocation::getFromRawEncoding(Record[2])),
1260 getMacroDefinition(Record[4]));
1261 PPRec.SetPreallocatedEntity(Record[0], MI);
1262 return;
1263 }
1264
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001265 case PP_MACRO_DEFINITION: {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001266 // If we already have a macro, that means that we've hit the end
1267 // of the definition of the macro we were looking for. We're
1268 // done.
1269 if (Macro)
1270 return;
1271
1272 if (!PP->getPreprocessingRecord()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001273 Error("missing preprocessing record in AST file");
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001274 return;
1275 }
1276
1277 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1278 if (PPRec.getPreprocessedEntity(Record[0]))
1279 return;
1280
1281 if (Record[1] >= MacroDefinitionsLoaded.size()) {
1282 Error("out-of-bounds macro definition record");
1283 return;
1284 }
1285
1286 MacroDefinition *MD
1287 = new (PPRec) MacroDefinition(DecodeIdentifierInfo(Record[4]),
1288 SourceLocation::getFromRawEncoding(Record[5]),
1289 SourceRange(
1290 SourceLocation::getFromRawEncoding(Record[2]),
1291 SourceLocation::getFromRawEncoding(Record[3])));
1292 PPRec.SetPreallocatedEntity(Record[0], MD);
1293 MacroDefinitionsLoaded[Record[1]] = MD;
1294 return;
1295 }
Steve Naroff83d63c72009-04-24 20:03:17 +00001296 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001297 }
1298}
1299
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001300void ASTReader::ReadDefinedMacros() {
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001301 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1302 llvm::BitstreamCursor &MacroCursor = Chain[N - I - 1]->MacroCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001303
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001304 // If there was no preprocessor block, skip this file.
1305 if (!MacroCursor.getBitStreamReader())
1306 continue;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001307
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001308 llvm::BitstreamCursor Cursor = MacroCursor;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001309 if (Cursor.EnterSubBlock(PREPROCESSOR_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001310 Error("malformed preprocessor block record in AST file");
Douglas Gregor88a35862010-01-04 19:18:44 +00001311 return;
1312 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001313
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001314 RecordData Record;
1315 while (true) {
1316 unsigned Code = Cursor.ReadCode();
1317 if (Code == llvm::bitc::END_BLOCK) {
1318 if (Cursor.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001319 Error("error at end of preprocessor block in AST file");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001320 return;
1321 }
1322 break;
Douglas Gregor88a35862010-01-04 19:18:44 +00001323 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001324
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001325 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1326 // No known subblocks, always skip them.
1327 Cursor.ReadSubBlockID();
1328 if (Cursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001329 Error("malformed block record in AST file");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001330 return;
1331 }
1332 continue;
1333 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001334
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001335 if (Code == llvm::bitc::DEFINE_ABBREV) {
1336 Cursor.ReadAbbrevRecord();
1337 continue;
1338 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001339
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001340 // Read a record.
1341 const char *BlobStart;
1342 unsigned BlobLen;
1343 Record.clear();
1344 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1345 default: // Default behavior: ignore.
1346 break;
Douglas Gregor88a35862010-01-04 19:18:44 +00001347
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001348 case PP_MACRO_OBJECT_LIKE:
1349 case PP_MACRO_FUNCTION_LIKE:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001350 DecodeIdentifierInfo(Record[0]);
1351 break;
1352
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001353 case PP_TOKEN:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001354 // Ignore tokens.
1355 break;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001356
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001357 case PP_MACRO_INSTANTIATION:
1358 case PP_MACRO_DEFINITION:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001359 // Read the macro record.
1360 ReadMacroRecord(Chain[N - I - 1]->Stream, Cursor.GetCurrentBitNo());
1361 break;
1362 }
Douglas Gregor88a35862010-01-04 19:18:44 +00001363 }
1364 }
1365}
1366
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001367MacroDefinition *ASTReader::getMacroDefinition(IdentID ID) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001368 if (ID == 0 || ID >= MacroDefinitionsLoaded.size())
1369 return 0;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001370
1371 if (!MacroDefinitionsLoaded[ID]) {
1372 unsigned Index = ID;
1373 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1374 PerFileData &F = *Chain[N - I - 1];
1375 if (Index < F.LocalNumMacroDefinitions) {
1376 ReadMacroRecord(F.Stream, F.MacroDefinitionOffsets[Index]);
1377 break;
1378 }
1379 Index -= F.LocalNumMacroDefinitions;
1380 }
1381 assert(MacroDefinitionsLoaded[ID] && "Broken chain");
1382 }
1383
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001384 return MacroDefinitionsLoaded[ID];
1385}
1386
Douglas Gregore650c8c2009-07-07 00:12:59 +00001387/// \brief If we are loading a relocatable PCH file, and the filename is
1388/// not an absolute path, add the system root to the beginning of the file
1389/// name.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001390void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001391 // If this is not a relocatable PCH file, there's nothing to do.
1392 if (!RelocatablePCH)
1393 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001394
Daniel Dunbard5b21972009-11-18 19:50:41 +00001395 if (Filename.empty() || llvm::sys::Path(Filename).isAbsolute())
Douglas Gregore650c8c2009-07-07 00:12:59 +00001396 return;
1397
Douglas Gregore650c8c2009-07-07 00:12:59 +00001398 if (isysroot == 0) {
1399 // If no system root was given, default to '/'
1400 Filename.insert(Filename.begin(), '/');
1401 return;
1402 }
Mike Stump1eb44332009-09-09 15:08:12 +00001403
Douglas Gregore650c8c2009-07-07 00:12:59 +00001404 unsigned Length = strlen(isysroot);
1405 if (isysroot[Length - 1] != '/')
1406 Filename.insert(Filename.begin(), '/');
Mike Stump1eb44332009-09-09 15:08:12 +00001407
Douglas Gregore650c8c2009-07-07 00:12:59 +00001408 Filename.insert(Filename.begin(), isysroot, isysroot + Length);
1409}
1410
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001411ASTReader::ASTReadResult
Sebastian Redl571db7f2010-08-18 23:56:56 +00001412ASTReader::ReadASTBlock(PerFileData &F) {
Sebastian Redl9137a522010-07-16 17:50:48 +00001413 llvm::BitstreamCursor &Stream = F.Stream;
1414
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001415 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001416 Error("malformed block record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001417 return Failure;
1418 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001419
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001420 // Read all of the records and blocks for the ASt file.
Douglas Gregor8038d512009-04-10 17:25:41 +00001421 RecordData Record;
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001422 bool First = true;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001423 while (!Stream.AtEndOfStream()) {
1424 unsigned Code = Stream.ReadCode();
1425 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001426 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001427 Error("error at end of module block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001428 return Failure;
1429 }
Chris Lattner7356a312009-04-11 21:15:38 +00001430
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001431 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001432 }
1433
1434 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1435 switch (Stream.ReadSubBlockID()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001436 case DECLTYPES_BLOCK_ID:
Chris Lattner6367f6d2009-04-27 01:05:14 +00001437 // We lazily load the decls block, but we want to set up the
1438 // DeclsCursor cursor to point into it. Clone our current bitcode
1439 // cursor to it, enter the block and read the abbrevs in that block.
1440 // With the main cursor, we just skip over it.
Sebastian Redl9137a522010-07-16 17:50:48 +00001441 F.DeclsCursor = Stream;
Chris Lattner6367f6d2009-04-27 01:05:14 +00001442 if (Stream.SkipBlock() || // Skip with the main cursor.
1443 // Read the abbrevs.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001444 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001445 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001446 return Failure;
1447 }
1448 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001449
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001450 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl9137a522010-07-16 17:50:48 +00001451 F.MacroCursor = Stream;
Douglas Gregor88a35862010-01-04 19:18:44 +00001452 if (PP)
1453 PP->setExternalSource(this);
1454
Chris Lattner7356a312009-04-11 21:15:38 +00001455 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001456 Error("malformed block record in AST file");
Chris Lattner7356a312009-04-11 21:15:38 +00001457 return Failure;
1458 }
1459 break;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001460
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001461 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001462 switch (ReadSourceManagerBlock(F)) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00001463 case Success:
1464 break;
1465
1466 case Failure:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001467 Error("malformed source manager block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001468 return Failure;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001469
1470 case IgnorePCH:
1471 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001472 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001473 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001474 }
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001475 First = false;
Douglas Gregor8038d512009-04-10 17:25:41 +00001476 continue;
1477 }
1478
1479 if (Code == llvm::bitc::DEFINE_ABBREV) {
1480 Stream.ReadAbbrevRecord();
1481 continue;
1482 }
1483
1484 // Read and process a record.
1485 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00001486 const char *BlobStart = 0;
1487 unsigned BlobLen = 0;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001488 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Douglas Gregor2bec0412009-04-10 21:16:55 +00001489 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001490 default: // Default behavior: ignore.
1491 break;
1492
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001493 case METADATA: {
1494 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1495 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001496 : diag::warn_pch_version_too_new);
1497 return IgnorePCH;
1498 }
1499
1500 RelocatablePCH = Record[4];
1501 if (Listener) {
1502 std::string TargetTriple(BlobStart, BlobLen);
1503 if (Listener->ReadTargetTriple(TargetTriple))
1504 return IgnorePCH;
1505 }
1506 break;
1507 }
1508
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001509 case CHAINED_METADATA: {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001510 if (!First) {
1511 Error("CHAINED_METADATA is not first record in block");
1512 return Failure;
1513 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001514 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1515 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001516 : diag::warn_pch_version_too_new);
1517 return IgnorePCH;
1518 }
1519
1520 // Load the chained file.
Sebastian Redl571db7f2010-08-18 23:56:56 +00001521 switch(ReadASTCore(llvm::StringRef(BlobStart, BlobLen))) {
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001522 case Failure: return Failure;
1523 // If we have to ignore the dependency, we'll have to ignore this too.
1524 case IgnorePCH: return IgnorePCH;
1525 case Success: break;
1526 }
1527 break;
1528 }
1529
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001530 case TYPE_OFFSET:
Sebastian Redl12d6da02010-07-19 22:06:55 +00001531 if (F.LocalNumTypes != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001532 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001533 return Failure;
1534 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00001535 F.TypeOffsets = (const uint32_t *)BlobStart;
1536 F.LocalNumTypes = Record[0];
Douglas Gregor8038d512009-04-10 17:25:41 +00001537 break;
1538
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001539 case DECL_OFFSET:
Sebastian Redl12d6da02010-07-19 22:06:55 +00001540 if (F.LocalNumDecls != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001541 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001542 return Failure;
1543 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00001544 F.DeclOffsets = (const uint32_t *)BlobStart;
1545 F.LocalNumDecls = Record[0];
Douglas Gregor8038d512009-04-10 17:25:41 +00001546 break;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001547
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001548 case TU_UPDATE_LEXICAL: {
Sebastian Redld692af72010-07-27 18:24:41 +00001549 DeclContextInfo Info = {
1550 /* No visible information */ 0, 0,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001551 reinterpret_cast<const DeclID *>(BlobStart),
1552 BlobLen / sizeof(DeclID)
Sebastian Redld692af72010-07-27 18:24:41 +00001553 };
1554 DeclContextOffsets[Context->getTranslationUnitDecl()].push_back(Info);
1555 break;
1556 }
1557
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001558 case REDECLS_UPDATE_LATEST: {
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00001559 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
1560 for (unsigned i = 0, e = Record.size(); i < e; i += 2) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001561 DeclID First = Record[i], Latest = Record[i+1];
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00001562 assert((FirstLatestDeclIDs.find(First) == FirstLatestDeclIDs.end() ||
1563 Latest > FirstLatestDeclIDs[First]) &&
1564 "The new latest is supposed to come after the previous latest");
1565 FirstLatestDeclIDs[First] = Latest;
1566 }
1567 break;
1568 }
1569
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001570 case LANGUAGE_OPTIONS:
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001571 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001572 return IgnorePCH;
1573 break;
Douglas Gregor2bec0412009-04-10 21:16:55 +00001574
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001575 case IDENTIFIER_TABLE:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001576 F.IdentifierTableData = BlobStart;
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001577 if (Record[0]) {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001578 F.IdentifierLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001579 = ASTIdentifierLookupTable::Create(
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001580 (const unsigned char *)F.IdentifierTableData + Record[0],
1581 (const unsigned char *)F.IdentifierTableData,
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001582 ASTIdentifierLookupTrait(*this, F.Stream));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001583 if (PP)
1584 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001585 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001586 break;
1587
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001588 case IDENTIFIER_OFFSET:
Sebastian Redl2da08f92010-07-19 22:28:42 +00001589 if (F.LocalNumIdentifiers != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001590 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00001591 return Failure;
1592 }
Sebastian Redl2da08f92010-07-19 22:28:42 +00001593 F.IdentifierOffsets = (const uint32_t *)BlobStart;
1594 F.LocalNumIdentifiers = Record[0];
Douglas Gregorafaf3082009-04-11 00:14:32 +00001595 break;
Douglas Gregorfdd01722009-04-14 00:24:19 +00001596
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001597 case EXTERNAL_DEFINITIONS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001598 // Optimization for the first block.
1599 if (ExternalDefinitions.empty())
1600 ExternalDefinitions.swap(Record);
1601 else
1602 ExternalDefinitions.insert(ExternalDefinitions.end(),
1603 Record.begin(), Record.end());
Douglas Gregorfdd01722009-04-14 00:24:19 +00001604 break;
Douglas Gregor3e1af842009-04-17 22:13:46 +00001605
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001606 case SPECIAL_TYPES:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001607 // Optimization for the first block
1608 if (SpecialTypes.empty())
1609 SpecialTypes.swap(Record);
1610 else
1611 SpecialTypes.insert(SpecialTypes.end(), Record.begin(), Record.end());
Douglas Gregorad1de002009-04-18 05:55:16 +00001612 break;
1613
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001614 case STATISTICS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001615 TotalNumStatements += Record[0];
1616 TotalNumMacros += Record[1];
1617 TotalLexicalDeclContexts += Record[2];
1618 TotalVisibleDeclContexts += Record[3];
Douglas Gregor3e1af842009-04-17 22:13:46 +00001619 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001620
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001621 case TENTATIVE_DEFINITIONS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001622 // Optimization for the first block.
1623 if (TentativeDefinitions.empty())
1624 TentativeDefinitions.swap(Record);
1625 else
1626 TentativeDefinitions.insert(TentativeDefinitions.end(),
1627 Record.begin(), Record.end());
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00001628 break;
Douglas Gregor14c22f22009-04-22 22:18:58 +00001629
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001630 case UNUSED_FILESCOPED_DECLS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001631 // Optimization for the first block.
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00001632 if (UnusedFileScopedDecls.empty())
1633 UnusedFileScopedDecls.swap(Record);
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001634 else
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00001635 UnusedFileScopedDecls.insert(UnusedFileScopedDecls.end(),
1636 Record.begin(), Record.end());
Tanya Lattnere6bbc012010-02-12 00:07:30 +00001637 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001638
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001639 case WEAK_UNDECLARED_IDENTIFIERS:
Sebastian Redl40566802010-08-05 18:21:25 +00001640 // Later blocks overwrite earlier ones.
1641 WeakUndeclaredIdentifiers.swap(Record);
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00001642 break;
1643
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001644 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001645 // Optimization for the first block.
1646 if (LocallyScopedExternalDecls.empty())
1647 LocallyScopedExternalDecls.swap(Record);
1648 else
1649 LocallyScopedExternalDecls.insert(LocallyScopedExternalDecls.end(),
1650 Record.begin(), Record.end());
Douglas Gregor14c22f22009-04-22 22:18:58 +00001651 break;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001652
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001653 case SELECTOR_OFFSETS:
Sebastian Redl059612d2010-08-03 21:58:15 +00001654 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redl725cd962010-08-04 20:40:17 +00001655 F.LocalNumSelectors = Record[0];
Douglas Gregor83941df2009-04-25 17:48:32 +00001656 break;
1657
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001658 case METHOD_POOL:
Sebastian Redl725cd962010-08-04 20:40:17 +00001659 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor83941df2009-04-25 17:48:32 +00001660 if (Record[0])
Sebastian Redl725cd962010-08-04 20:40:17 +00001661 F.SelectorLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001662 = ASTSelectorLookupTable::Create(
Sebastian Redl725cd962010-08-04 20:40:17 +00001663 F.SelectorLookupTableData + Record[0],
1664 F.SelectorLookupTableData,
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001665 ASTSelectorLookupTrait(*this));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00001666 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001667 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001668
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001669 case REFERENCED_SELECTOR_POOL: {
Sebastian Redl725cd962010-08-04 20:40:17 +00001670 ReferencedSelectorsData.insert(ReferencedSelectorsData.end(),
1671 Record.begin(), Record.end());
Fariborz Jahanian32019832010-07-23 19:11:11 +00001672 break;
Sebastian Redl681d7232010-07-27 00:17:23 +00001673 }
Fariborz Jahanian32019832010-07-23 19:11:11 +00001674
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001675 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001676 if (!Record.empty() && Listener)
1677 Listener->ReadCounter(Record[0]);
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001678 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001679
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001680 case SOURCE_LOCATION_OFFSETS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001681 F.SLocOffsets = (const uint32_t *)BlobStart;
1682 F.LocalNumSLocEntries = Record[0];
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001683 // We cannot delay this until the entire chain is loaded, because then
1684 // source location preloads would also have to be delayed.
1685 // FIXME: Is there a reason not to do that?
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001686 TotalNumSLocEntries += F.LocalNumSLocEntries;
Douglas Gregor445e23e2009-10-05 21:07:28 +00001687 SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, Record[1]);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001688 break;
1689
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001690 case SOURCE_LOCATION_PRELOADS:
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001691 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001692 ASTReadResult Result = ReadSLocEntryRecord(Record[I]);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001693 if (Result != Success)
1694 return Result;
1695 }
1696 break;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001697
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001698 case STAT_CACHE: {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001699 ASTStatCache *MyStatCache =
1700 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
Douglas Gregor52e71082009-10-16 18:18:30 +00001701 (const unsigned char *)BlobStart,
1702 NumStatHits, NumStatMisses);
1703 FileMgr.addStatCache(MyStatCache);
Sebastian Redl9137a522010-07-16 17:50:48 +00001704 F.StatCache = MyStatCache;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001705 break;
Douglas Gregor52e71082009-10-16 18:18:30 +00001706 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001707
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001708 case EXT_VECTOR_DECLS:
Sebastian Redla9f23682010-07-28 21:38:49 +00001709 // Optimization for the first block.
1710 if (ExtVectorDecls.empty())
1711 ExtVectorDecls.swap(Record);
1712 else
1713 ExtVectorDecls.insert(ExtVectorDecls.end(),
1714 Record.begin(), Record.end());
Douglas Gregorb81c1702009-04-27 20:06:05 +00001715 break;
1716
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001717 case VTABLE_USES:
Sebastian Redl40566802010-08-05 18:21:25 +00001718 // Later tables overwrite earlier ones.
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00001719 VTableUses.swap(Record);
1720 break;
1721
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001722 case DYNAMIC_CLASSES:
Sebastian Redl40566802010-08-05 18:21:25 +00001723 // Optimization for the first block.
1724 if (DynamicClasses.empty())
1725 DynamicClasses.swap(Record);
1726 else
1727 DynamicClasses.insert(DynamicClasses.end(),
1728 Record.begin(), Record.end());
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00001729 break;
1730
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001731 case PENDING_IMPLICIT_INSTANTIATIONS:
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00001732 // Optimization for the first block.
1733 if (PendingImplicitInstantiations.empty())
1734 PendingImplicitInstantiations.swap(Record);
1735 else
1736 PendingImplicitInstantiations.insert(
1737 PendingImplicitInstantiations.end(), Record.begin(), Record.end());
1738 break;
1739
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001740 case SEMA_DECL_REFS:
Sebastian Redl40566802010-08-05 18:21:25 +00001741 // Later tables overwrite earlier ones.
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00001742 SemaDeclRefs.swap(Record);
1743 break;
1744
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001745 case ORIGINAL_FILE_NAME:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001746 // The primary AST will be the last to get here, so it will be the one
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001747 // that's used.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +00001748 ActualOriginalFileName.assign(BlobStart, BlobLen);
1749 OriginalFileName = ActualOriginalFileName;
Douglas Gregore650c8c2009-07-07 00:12:59 +00001750 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001751 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001752
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001753 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek974be4d2010-02-12 23:31:14 +00001754 const std::string &CurBranch = getClangFullRepositoryVersion();
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001755 llvm::StringRef ASTBranch(BlobStart, BlobLen);
1756 if (llvm::StringRef(CurBranch) != ASTBranch && !DisableValidation) {
1757 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregor445e23e2009-10-05 21:07:28 +00001758 return IgnorePCH;
1759 }
1760 break;
1761 }
Sebastian Redl04e6fd42010-07-21 20:07:32 +00001762
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001763 case MACRO_DEFINITION_OFFSETS:
Sebastian Redl04e6fd42010-07-21 20:07:32 +00001764 F.MacroDefinitionOffsets = (const uint32_t *)BlobStart;
1765 F.NumPreallocatedPreprocessingEntities = Record[0];
1766 F.LocalNumMacroDefinitions = Record[1];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001767 break;
Sebastian Redl0b17c612010-08-13 00:28:03 +00001768
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001769 case DECL_REPLACEMENTS: {
Sebastian Redl0b17c612010-08-13 00:28:03 +00001770 if (Record.size() % 2 != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001771 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redl0b17c612010-08-13 00:28:03 +00001772 return Failure;
1773 }
1774 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001775 ReplacedDecls[static_cast<DeclID>(Record[I])] =
Sebastian Redl0b17c612010-08-13 00:28:03 +00001776 std::make_pair(&F, Record[I+1]);
1777 break;
1778 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001779 }
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001780 First = false;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001781 }
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001782 Error("premature end of bitstream in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001783 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001784}
1785
Sebastian Redl571db7f2010-08-18 23:56:56 +00001786ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName) {
1787 switch(ReadASTCore(FileName)) {
Sebastian Redlcdf3b832010-07-16 20:41:52 +00001788 case Failure: return Failure;
1789 case IgnorePCH: return IgnorePCH;
1790 case Success: break;
1791 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001792
1793 // Here comes stuff that we only do once the entire chain is loaded.
1794
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001795 // Allocate space for loaded identifiers, decls and types.
Sebastian Redl04e6fd42010-07-21 20:07:32 +00001796 unsigned TotalNumIdentifiers = 0, TotalNumTypes = 0, TotalNumDecls = 0,
Sebastian Redl725cd962010-08-04 20:40:17 +00001797 TotalNumPreallocatedPreprocessingEntities = 0, TotalNumMacroDefs = 0,
1798 TotalNumSelectors = 0;
Sebastian Redl12d6da02010-07-19 22:06:55 +00001799 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl2da08f92010-07-19 22:28:42 +00001800 TotalNumIdentifiers += Chain[I]->LocalNumIdentifiers;
Sebastian Redl12d6da02010-07-19 22:06:55 +00001801 TotalNumTypes += Chain[I]->LocalNumTypes;
1802 TotalNumDecls += Chain[I]->LocalNumDecls;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00001803 TotalNumPreallocatedPreprocessingEntities +=
1804 Chain[I]->NumPreallocatedPreprocessingEntities;
1805 TotalNumMacroDefs += Chain[I]->LocalNumMacroDefinitions;
Sebastian Redl725cd962010-08-04 20:40:17 +00001806 TotalNumSelectors += Chain[I]->LocalNumSelectors;
Sebastian Redl12d6da02010-07-19 22:06:55 +00001807 }
Sebastian Redl2da08f92010-07-19 22:28:42 +00001808 IdentifiersLoaded.resize(TotalNumIdentifiers);
Sebastian Redl12d6da02010-07-19 22:06:55 +00001809 TypesLoaded.resize(TotalNumTypes);
1810 DeclsLoaded.resize(TotalNumDecls);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00001811 MacroDefinitionsLoaded.resize(TotalNumMacroDefs);
1812 if (PP) {
1813 if (TotalNumIdentifiers > 0)
1814 PP->getHeaderSearchInfo().SetExternalLookup(this);
1815 if (TotalNumPreallocatedPreprocessingEntities > 0) {
1816 if (!PP->getPreprocessingRecord())
1817 PP->createPreprocessingRecord();
1818 PP->getPreprocessingRecord()->SetExternalSource(*this,
1819 TotalNumPreallocatedPreprocessingEntities);
1820 }
1821 }
Sebastian Redl725cd962010-08-04 20:40:17 +00001822 SelectorsLoaded.resize(TotalNumSelectors);
Sebastian Redl12d6da02010-07-19 22:06:55 +00001823
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001824 // Check the predefines buffers.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001825 if (!DisableValidation && CheckPredefinesBuffers())
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001826 return IgnorePCH;
1827
1828 if (PP) {
1829 // Initialization of keywords and pragmas occurs before the
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001830 // AST file is read, so there may be some identifiers that were
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001831 // loaded into the IdentifierTable before we intercepted the
1832 // creation of identifiers. Iterate through the list of known
1833 // identifiers and determine whether we have to establish
1834 // preprocessor definitions or top-level identifier declaration
1835 // chains for those identifiers.
1836 //
1837 // We copy the IdentifierInfo pointers to a small vector first,
1838 // since de-serializing declarations or macro definitions can add
1839 // new entries into the identifier table, invalidating the
1840 // iterators.
1841 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
1842 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
1843 IdEnd = PP->getIdentifierTable().end();
1844 Id != IdEnd; ++Id)
1845 Identifiers.push_back(Id->second);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00001846 // We need to search the tables in all files.
Sebastian Redl04e6fd42010-07-21 20:07:32 +00001847 for (unsigned J = 0, M = Chain.size(); J != M; ++J) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001848 ASTIdentifierLookupTable *IdTable
1849 = (ASTIdentifierLookupTable *)Chain[J]->IdentifierLookupTable;
1850 // Not all AST files necessarily have identifier tables, only the useful
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001851 // ones.
1852 if (!IdTable)
1853 continue;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00001854 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
1855 IdentifierInfo *II = Identifiers[I];
1856 // Look in the on-disk hash tables for an entry for this identifier
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001857 ASTIdentifierLookupTrait Info(*this, Chain[J]->Stream, II);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00001858 std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength());
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001859 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001860 if (Pos == IdTable->end())
1861 continue;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001862
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001863 // Dereferencing the iterator has the effect of populating the
1864 // IdentifierInfo node with the various declarations it needs.
1865 (void)*Pos;
1866 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001867 }
1868 }
1869
1870 if (Context)
1871 InitializeContext(*Context);
1872
1873 return Success;
1874}
1875
Sebastian Redl571db7f2010-08-18 23:56:56 +00001876ASTReader::ASTReadResult ASTReader::ReadASTCore(llvm::StringRef FileName) {
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001877 Chain.push_back(new PerFileData());
Sebastian Redl9137a522010-07-16 17:50:48 +00001878 PerFileData &F = *Chain.back();
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001879
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001880 // Set the AST file name.
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001881 F.FileName = FileName;
1882
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001883 // Open the AST file.
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001884 //
1885 // FIXME: This shouldn't be here, we should just take a raw_ostream.
1886 std::string ErrStr;
1887 F.Buffer.reset(llvm::MemoryBuffer::getFileOrSTDIN(FileName, &ErrStr));
1888 if (!F.Buffer) {
1889 Error(ErrStr.c_str());
1890 return IgnorePCH;
1891 }
1892
1893 // Initialize the stream
1894 F.StreamFile.init((const unsigned char *)F.Buffer->getBufferStart(),
1895 (const unsigned char *)F.Buffer->getBufferEnd());
Sebastian Redl9137a522010-07-16 17:50:48 +00001896 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001897 Stream.init(F.StreamFile);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00001898 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001899
1900 // Sniff for the signature.
1901 if (Stream.Read(8) != 'C' ||
1902 Stream.Read(8) != 'P' ||
1903 Stream.Read(8) != 'C' ||
1904 Stream.Read(8) != 'H') {
1905 Diag(diag::err_not_a_pch_file) << FileName;
1906 return Failure;
1907 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001908
Douglas Gregor2cf26342009-04-09 22:27:44 +00001909 while (!Stream.AtEndOfStream()) {
1910 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00001911
Douglas Gregore1d918e2009-04-10 23:10:45 +00001912 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001913 Error("invalid record at top-level of AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001914 return Failure;
1915 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001916
1917 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregor668c1a42009-04-21 22:25:48 +00001918
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001919 // We only know the AST subblock ID.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001920 switch (BlockID) {
1921 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001922 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001923 Error("malformed BlockInfoBlock in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001924 return Failure;
1925 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001926 break;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001927 case AST_BLOCK_ID:
Sebastian Redl571db7f2010-08-18 23:56:56 +00001928 switch (ReadASTBlock(F)) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001929 case Success:
1930 break;
1931
1932 case Failure:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001933 return Failure;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001934
1935 case IgnorePCH:
Douglas Gregor2bec0412009-04-10 21:16:55 +00001936 // FIXME: We could consider reading through to the end of this
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001937 // AST block, skipping subblocks, to see if there are other
1938 // AST blocks elsewhere.
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00001939
1940 // Clear out any preallocated source location entries, so that
1941 // the source manager does not try to resolve them later.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001942 SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00001943
1944 // Remove the stat cache.
Sebastian Redl9137a522010-07-16 17:50:48 +00001945 if (F.StatCache)
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001946 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00001947
Douglas Gregore1d918e2009-04-10 23:10:45 +00001948 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001949 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001950 break;
1951 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001952 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001953 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001954 return Failure;
1955 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001956 break;
1957 }
Mike Stump1eb44332009-09-09 15:08:12 +00001958 }
1959
Sebastian Redlcdf3b832010-07-16 20:41:52 +00001960 return Success;
1961}
1962
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001963void ASTReader::setPreprocessor(Preprocessor &pp) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001964 PP = &pp;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00001965
1966 unsigned TotalNum = 0;
1967 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
1968 TotalNum += Chain[I]->NumPreallocatedPreprocessingEntities;
1969 if (TotalNum) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001970 if (!PP->getPreprocessingRecord())
1971 PP->createPreprocessingRecord();
Sebastian Redl04e6fd42010-07-21 20:07:32 +00001972 PP->getPreprocessingRecord()->SetExternalSource(*this, TotalNum);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001973 }
1974}
1975
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001976void ASTReader::InitializeContext(ASTContext &Ctx) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001977 Context = &Ctx;
1978 assert(Context && "Passed null context!");
1979
1980 assert(PP && "Forgot to set Preprocessor ?");
1981 PP->getIdentifierTable().setExternalIdentifierLookup(this);
1982 PP->getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor88a35862010-01-04 19:18:44 +00001983 PP->setExternalSource(this);
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001984
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001985 // Load the translation unit declaration
Argyrios Kyrtzidis8871a442010-07-08 17:13:02 +00001986 GetTranslationUnitDecl();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001987
1988 // Load the special types.
1989 Context->setBuiltinVaListType(
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001990 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
1991 if (unsigned Id = SpecialTypes[SPECIAL_TYPE_OBJC_ID])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001992 Context->setObjCIdType(GetType(Id));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001993 if (unsigned Sel = SpecialTypes[SPECIAL_TYPE_OBJC_SELECTOR])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001994 Context->setObjCSelType(GetType(Sel));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001995 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001996 Context->setObjCProtoType(GetType(Proto));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001997 if (unsigned Class = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001998 Context->setObjCClassType(GetType(Class));
Steve Naroff14108da2009-07-10 23:34:53 +00001999
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002000 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002001 Context->setCFConstantStringType(GetType(String));
Mike Stump1eb44332009-09-09 15:08:12 +00002002 if (unsigned FastEnum
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002003 = SpecialTypes[SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002004 Context->setObjCFastEnumerationStateType(GetType(FastEnum));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002005 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002006 QualType FileType = GetType(File);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002007 if (FileType.isNull()) {
2008 Error("FILE type is NULL");
2009 return;
2010 }
John McCall183700f2009-09-21 23:43:11 +00002011 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002012 Context->setFILEDecl(Typedef->getDecl());
2013 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002014 const TagType *Tag = FileType->getAs<TagType>();
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002015 if (!Tag) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002016 Error("Invalid FILE type in AST file");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002017 return;
2018 }
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002019 Context->setFILEDecl(Tag->getDecl());
2020 }
2021 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002022 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
Mike Stump782fa302009-07-28 02:25:19 +00002023 QualType Jmp_bufType = GetType(Jmp_buf);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002024 if (Jmp_bufType.isNull()) {
2025 Error("jmp_bug type is NULL");
2026 return;
2027 }
John McCall183700f2009-09-21 23:43:11 +00002028 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
Mike Stump782fa302009-07-28 02:25:19 +00002029 Context->setjmp_bufDecl(Typedef->getDecl());
2030 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002031 const TagType *Tag = Jmp_bufType->getAs<TagType>();
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002032 if (!Tag) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002033 Error("Invalid jmp_buf type in AST file");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002034 return;
2035 }
Mike Stump782fa302009-07-28 02:25:19 +00002036 Context->setjmp_bufDecl(Tag->getDecl());
2037 }
2038 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002039 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
Mike Stump782fa302009-07-28 02:25:19 +00002040 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002041 if (Sigjmp_bufType.isNull()) {
2042 Error("sigjmp_buf type is NULL");
2043 return;
2044 }
John McCall183700f2009-09-21 23:43:11 +00002045 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
Mike Stump782fa302009-07-28 02:25:19 +00002046 Context->setsigjmp_bufDecl(Typedef->getDecl());
2047 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002048 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002049 assert(Tag && "Invalid sigjmp_buf type in AST file");
Mike Stump782fa302009-07-28 02:25:19 +00002050 Context->setsigjmp_bufDecl(Tag->getDecl());
2051 }
2052 }
Mike Stump1eb44332009-09-09 15:08:12 +00002053 if (unsigned ObjCIdRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002054 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION])
Douglas Gregord1571ac2009-08-21 00:27:50 +00002055 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
Mike Stump1eb44332009-09-09 15:08:12 +00002056 if (unsigned ObjCClassRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002057 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION])
Douglas Gregord1571ac2009-08-21 00:27:50 +00002058 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002059 if (unsigned String = SpecialTypes[SPECIAL_TYPE_BLOCK_DESCRIPTOR])
Mike Stumpadaaad32009-10-20 02:12:22 +00002060 Context->setBlockDescriptorType(GetType(String));
Mike Stump083c25e2009-10-22 00:49:09 +00002061 if (unsigned String
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002062 = SpecialTypes[SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR])
Mike Stump083c25e2009-10-22 00:49:09 +00002063 Context->setBlockDescriptorExtendedType(GetType(String));
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002064 if (unsigned ObjCSelRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002065 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION])
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002066 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002067 if (unsigned String = SpecialTypes[SPECIAL_TYPE_NS_CONSTANT_STRING])
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002068 Context->setNSConstantStringType(GetType(String));
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002069
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002070 if (SpecialTypes[SPECIAL_TYPE_INT128_INSTALLED])
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002071 Context->setInt128Installed();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002072}
2073
Douglas Gregorb64c1932009-05-12 01:31:05 +00002074/// \brief Retrieve the name of the original source file name
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002075/// directly from the AST file, without actually loading the AST
Douglas Gregorb64c1932009-05-12 01:31:05 +00002076/// file.
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002077std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00002078 Diagnostic &Diags) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002079 // Open the AST file.
Douglas Gregorb64c1932009-05-12 01:31:05 +00002080 std::string ErrStr;
2081 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002082 Buffer.reset(llvm::MemoryBuffer::getFile(ASTFileName.c_str(), &ErrStr));
Douglas Gregorb64c1932009-05-12 01:31:05 +00002083 if (!Buffer) {
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00002084 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002085 return std::string();
2086 }
2087
2088 // Initialize the stream
2089 llvm::BitstreamReader StreamFile;
2090 llvm::BitstreamCursor Stream;
Mike Stump1eb44332009-09-09 15:08:12 +00002091 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregorb64c1932009-05-12 01:31:05 +00002092 (const unsigned char *)Buffer->getBufferEnd());
2093 Stream.init(StreamFile);
2094
2095 // Sniff for the signature.
2096 if (Stream.Read(8) != 'C' ||
2097 Stream.Read(8) != 'P' ||
2098 Stream.Read(8) != 'C' ||
2099 Stream.Read(8) != 'H') {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002100 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002101 return std::string();
2102 }
2103
2104 RecordData Record;
2105 while (!Stream.AtEndOfStream()) {
2106 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002107
Douglas Gregorb64c1932009-05-12 01:31:05 +00002108 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2109 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump1eb44332009-09-09 15:08:12 +00002110
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002111 // We only know the AST subblock ID.
Douglas Gregorb64c1932009-05-12 01:31:05 +00002112 switch (BlockID) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002113 case AST_BLOCK_ID:
2114 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002115 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002116 return std::string();
2117 }
2118 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002119
Douglas Gregorb64c1932009-05-12 01:31:05 +00002120 default:
2121 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002122 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002123 return std::string();
2124 }
2125 break;
2126 }
2127 continue;
2128 }
2129
2130 if (Code == llvm::bitc::END_BLOCK) {
2131 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002132 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002133 return std::string();
2134 }
2135 continue;
2136 }
2137
2138 if (Code == llvm::bitc::DEFINE_ABBREV) {
2139 Stream.ReadAbbrevRecord();
2140 continue;
2141 }
2142
2143 Record.clear();
2144 const char *BlobStart = 0;
2145 unsigned BlobLen = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002146 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002147 == ORIGINAL_FILE_NAME)
Douglas Gregorb64c1932009-05-12 01:31:05 +00002148 return std::string(BlobStart, BlobLen);
Mike Stump1eb44332009-09-09 15:08:12 +00002149 }
Douglas Gregorb64c1932009-05-12 01:31:05 +00002150
2151 return std::string();
2152}
2153
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002154/// \brief Parse the record that corresponds to a LangOptions data
2155/// structure.
2156///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002157/// This routine parses the language options from the AST file and then gives
2158/// them to the AST listener if one is set.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002159///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002160/// \returns true if the listener deems the file unacceptable, false otherwise.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002161bool ASTReader::ParseLanguageOptions(
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002162 const llvm::SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002163 if (Listener) {
2164 LangOptions LangOpts;
Mike Stump1eb44332009-09-09 15:08:12 +00002165
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002166 #define PARSE_LANGOPT(Option) \
2167 LangOpts.Option = Record[Idx]; \
2168 ++Idx
Mike Stump1eb44332009-09-09 15:08:12 +00002169
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002170 unsigned Idx = 0;
2171 PARSE_LANGOPT(Trigraphs);
2172 PARSE_LANGOPT(BCPLComment);
2173 PARSE_LANGOPT(DollarIdents);
2174 PARSE_LANGOPT(AsmPreprocessor);
2175 PARSE_LANGOPT(GNUMode);
Chandler Carrutheb5d7b72010-04-17 20:17:31 +00002176 PARSE_LANGOPT(GNUKeywords);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002177 PARSE_LANGOPT(ImplicitInt);
2178 PARSE_LANGOPT(Digraphs);
2179 PARSE_LANGOPT(HexFloats);
2180 PARSE_LANGOPT(C99);
2181 PARSE_LANGOPT(Microsoft);
2182 PARSE_LANGOPT(CPlusPlus);
2183 PARSE_LANGOPT(CPlusPlus0x);
2184 PARSE_LANGOPT(CXXOperatorNames);
2185 PARSE_LANGOPT(ObjC1);
2186 PARSE_LANGOPT(ObjC2);
2187 PARSE_LANGOPT(ObjCNonFragileABI);
Fariborz Jahanian412e7982010-02-09 19:31:38 +00002188 PARSE_LANGOPT(ObjCNonFragileABI2);
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +00002189 PARSE_LANGOPT(NoConstantCFStrings);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002190 PARSE_LANGOPT(PascalStrings);
2191 PARSE_LANGOPT(WritableStrings);
2192 PARSE_LANGOPT(LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +00002193 PARSE_LANGOPT(AltiVec);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002194 PARSE_LANGOPT(Exceptions);
Daniel Dunbar73482882010-02-10 18:48:44 +00002195 PARSE_LANGOPT(SjLjExceptions);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002196 PARSE_LANGOPT(NeXTRuntime);
2197 PARSE_LANGOPT(Freestanding);
2198 PARSE_LANGOPT(NoBuiltin);
2199 PARSE_LANGOPT(ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +00002200 PARSE_LANGOPT(POSIXThreads);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002201 PARSE_LANGOPT(Blocks);
2202 PARSE_LANGOPT(EmitAllDecls);
2203 PARSE_LANGOPT(MathErrno);
Chris Lattnera4d71452010-06-26 21:25:03 +00002204 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
2205 Record[Idx++]);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002206 PARSE_LANGOPT(HeinousExtensions);
2207 PARSE_LANGOPT(Optimize);
2208 PARSE_LANGOPT(OptimizeSize);
2209 PARSE_LANGOPT(Static);
2210 PARSE_LANGOPT(PICLevel);
2211 PARSE_LANGOPT(GNUInline);
2212 PARSE_LANGOPT(NoInline);
2213 PARSE_LANGOPT(AccessControl);
2214 PARSE_LANGOPT(CharIsSigned);
John Thompsona6fda122009-11-05 20:14:16 +00002215 PARSE_LANGOPT(ShortWChar);
Chris Lattnera4d71452010-06-26 21:25:03 +00002216 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
2217 LangOpts.setVisibilityMode((LangOptions::VisibilityMode)Record[Idx++]);
Daniel Dunbarab8e2812009-09-21 04:16:19 +00002218 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
Chris Lattnera4d71452010-06-26 21:25:03 +00002219 Record[Idx++]);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002220 PARSE_LANGOPT(InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +00002221 PARSE_LANGOPT(OpenCL);
Mike Stump9c276ae2009-12-12 01:27:46 +00002222 PARSE_LANGOPT(CatchUndefined);
2223 // FIXME: Missing ElideConstructors?!
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002224 #undef PARSE_LANGOPT
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002225
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002226 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002227 }
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002228
2229 return false;
2230}
2231
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002232void ASTReader::ReadPreprocessedEntities() {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002233 ReadDefinedMacros();
2234}
2235
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002236/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002237ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002238 PerFileData *F = 0;
2239 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2240 F = Chain[N - I - 1];
2241 if (Index < F->LocalNumTypes)
2242 break;
2243 Index -= F->LocalNumTypes;
2244 }
2245 assert(F && F->LocalNumTypes > Index && "Broken chain");
Sebastian Redl971dd442010-07-20 22:55:31 +00002246 return RecordLocation(&F->DeclsCursor, F->TypeOffsets[Index]);
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002247}
2248
2249/// \brief Read and return the type with the given index..
Douglas Gregor2cf26342009-04-09 22:27:44 +00002250///
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002251/// The index is the type ID, shifted and minus the number of predefs. This
2252/// routine actually reads the record corresponding to the type at the given
2253/// location. It is a helper routine for GetType, which deals with reading type
2254/// IDs.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002255QualType ASTReader::ReadTypeRecord(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002256 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redl971dd442010-07-20 22:55:31 +00002257 llvm::BitstreamCursor &DeclsCursor = *Loc.first;
Sebastian Redl9137a522010-07-16 17:50:48 +00002258
Douglas Gregor0b748912009-04-14 21:18:50 +00002259 // Keep track of where we are in the stream, then jump back there
2260 // after reading this type.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002261 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregor0b748912009-04-14 21:18:50 +00002262
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00002263 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redl27372b42010-08-11 18:52:41 +00002264
Douglas Gregord89275b2009-07-06 18:54:52 +00002265 // Note that we are loading a type record.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00002266 Deserializing AType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00002267
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002268 DeclsCursor.JumpToBit(Loc.second);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002269 RecordData Record;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002270 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002271 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
2272 case TYPE_EXT_QUAL: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002273 if (Record.size() != 2) {
2274 Error("Incorrect encoding of extended qualifier type");
2275 return QualType();
2276 }
Douglas Gregor6d473962009-04-15 22:00:08 +00002277 QualType Base = GetType(Record[0]);
John McCall0953e762009-09-24 19:53:00 +00002278 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]);
2279 return Context->getQualifiedType(Base, Quals);
Douglas Gregor6d473962009-04-15 22:00:08 +00002280 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002281
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002282 case TYPE_COMPLEX: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002283 if (Record.size() != 1) {
2284 Error("Incorrect encoding of complex type");
2285 return QualType();
2286 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002287 QualType ElemType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002288 return Context->getComplexType(ElemType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002289 }
2290
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002291 case TYPE_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002292 if (Record.size() != 1) {
2293 Error("Incorrect encoding of pointer type");
2294 return QualType();
2295 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002296 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002297 return Context->getPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002298 }
2299
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002300 case TYPE_BLOCK_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002301 if (Record.size() != 1) {
2302 Error("Incorrect encoding of block pointer type");
2303 return QualType();
2304 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002305 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002306 return Context->getBlockPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002307 }
2308
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002309 case TYPE_LVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002310 if (Record.size() != 1) {
2311 Error("Incorrect encoding of lvalue reference type");
2312 return QualType();
2313 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002314 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002315 return Context->getLValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002316 }
2317
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002318 case TYPE_RVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002319 if (Record.size() != 1) {
2320 Error("Incorrect encoding of rvalue reference type");
2321 return QualType();
2322 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002323 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002324 return Context->getRValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002325 }
2326
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002327 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidis240437b2010-07-02 11:55:15 +00002328 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002329 Error("Incorrect encoding of member pointer type");
2330 return QualType();
2331 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002332 QualType PointeeType = GetType(Record[0]);
2333 QualType ClassType = GetType(Record[1]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002334 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002335 }
2336
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002337 case TYPE_CONSTANT_ARRAY: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002338 QualType ElementType = GetType(Record[0]);
2339 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2340 unsigned IndexTypeQuals = Record[2];
2341 unsigned Idx = 3;
2342 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002343 return Context->getConstantArrayType(ElementType, Size,
2344 ASM, IndexTypeQuals);
2345 }
2346
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002347 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002348 QualType ElementType = GetType(Record[0]);
2349 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2350 unsigned IndexTypeQuals = Record[2];
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002351 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002352 }
2353
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002354 case TYPE_VARIABLE_ARRAY: {
Douglas Gregor0b748912009-04-14 21:18:50 +00002355 QualType ElementType = GetType(Record[0]);
2356 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2357 unsigned IndexTypeQuals = Record[2];
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002358 SourceLocation LBLoc = SourceLocation::getFromRawEncoding(Record[3]);
2359 SourceLocation RBLoc = SourceLocation::getFromRawEncoding(Record[4]);
Sebastian Redl577d4792010-07-22 22:43:28 +00002360 return Context->getVariableArrayType(ElementType, ReadExpr(DeclsCursor),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002361 ASM, IndexTypeQuals,
2362 SourceRange(LBLoc, RBLoc));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002363 }
2364
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002365 case TYPE_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00002366 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002367 Error("incorrect encoding of vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002368 return QualType();
2369 }
2370
2371 QualType ElementType = GetType(Record[0]);
2372 unsigned NumElements = Record[1];
Chris Lattner788b0fd2010-06-23 06:00:24 +00002373 unsigned AltiVecSpec = Record[2];
2374 return Context->getVectorType(ElementType, NumElements,
2375 (VectorType::AltiVecSpecific)AltiVecSpec);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002376 }
2377
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002378 case TYPE_EXT_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00002379 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002380 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002381 return QualType();
2382 }
2383
2384 QualType ElementType = GetType(Record[0]);
2385 unsigned NumElements = Record[1];
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002386 return Context->getExtVectorType(ElementType, NumElements);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002387 }
2388
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002389 case TYPE_FUNCTION_NO_PROTO: {
Rafael Espindola425ef722010-03-30 22:15:11 +00002390 if (Record.size() != 4) {
Douglas Gregora02b1472009-04-28 21:53:25 +00002391 Error("incorrect encoding of no-proto function type");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002392 return QualType();
2393 }
2394 QualType ResultType = GetType(Record[0]);
Rafael Espindola425ef722010-03-30 22:15:11 +00002395 FunctionType::ExtInfo Info(Record[1], Record[2], (CallingConv)Record[3]);
Rafael Espindola264ba482010-03-30 20:24:48 +00002396 return Context->getFunctionNoProtoType(ResultType, Info);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002397 }
2398
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002399 case TYPE_FUNCTION_PROTO: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002400 QualType ResultType = GetType(Record[0]);
Douglas Gregor91236662009-12-22 18:11:50 +00002401 bool NoReturn = Record[1];
Rafael Espindola425ef722010-03-30 22:15:11 +00002402 unsigned RegParm = Record[2];
2403 CallingConv CallConv = (CallingConv)Record[3];
2404 unsigned Idx = 4;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002405 unsigned NumParams = Record[Idx++];
2406 llvm::SmallVector<QualType, 16> ParamTypes;
2407 for (unsigned I = 0; I != NumParams; ++I)
2408 ParamTypes.push_back(GetType(Record[Idx++]));
2409 bool isVariadic = Record[Idx++];
2410 unsigned Quals = Record[Idx++];
Sebastian Redl465226e2009-05-27 22:11:52 +00002411 bool hasExceptionSpec = Record[Idx++];
2412 bool hasAnyExceptionSpec = Record[Idx++];
2413 unsigned NumExceptions = Record[Idx++];
2414 llvm::SmallVector<QualType, 2> Exceptions;
2415 for (unsigned I = 0; I != NumExceptions; ++I)
2416 Exceptions.push_back(GetType(Record[Idx++]));
Jay Foadbeaaccd2009-05-21 09:52:38 +00002417 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
Sebastian Redl465226e2009-05-27 22:11:52 +00002418 isVariadic, Quals, hasExceptionSpec,
2419 hasAnyExceptionSpec, NumExceptions,
Rafael Espindola264ba482010-03-30 20:24:48 +00002420 Exceptions.data(),
Rafael Espindola425ef722010-03-30 22:15:11 +00002421 FunctionType::ExtInfo(NoReturn, RegParm,
2422 CallConv));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002423 }
2424
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002425 case TYPE_UNRESOLVED_USING:
John McCalled976492009-12-04 22:46:56 +00002426 return Context->getTypeDeclType(
2427 cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0])));
2428
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002429 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002430 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002431 Error("incorrect encoding of typedef type");
2432 return QualType();
2433 }
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002434 TypedefDecl *Decl = cast<TypedefDecl>(GetDecl(Record[0]));
2435 QualType Canonical = GetType(Record[1]);
2436 return Context->getTypedefType(Decl, Canonical);
2437 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002438
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002439 case TYPE_TYPEOF_EXPR:
Sebastian Redl577d4792010-07-22 22:43:28 +00002440 return Context->getTypeOfExprType(ReadExpr(DeclsCursor));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002441
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002442 case TYPE_TYPEOF: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002443 if (Record.size() != 1) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002444 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002445 return QualType();
2446 }
2447 QualType UnderlyingType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002448 return Context->getTypeOfType(UnderlyingType);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002449 }
Mike Stump1eb44332009-09-09 15:08:12 +00002450
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002451 case TYPE_DECLTYPE:
Sebastian Redl577d4792010-07-22 22:43:28 +00002452 return Context->getDecltypeType(ReadExpr(DeclsCursor));
Anders Carlsson395b4752009-06-24 19:06:50 +00002453
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002454 case TYPE_RECORD: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002455 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002456 Error("incorrect encoding of record type");
2457 return QualType();
2458 }
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002459 bool IsDependent = Record[0];
2460 QualType T = Context->getRecordType(cast<RecordDecl>(GetDecl(Record[1])));
2461 T->Dependent = IsDependent;
2462 return T;
2463 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002464
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002465 case TYPE_ENUM: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002466 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002467 Error("incorrect encoding of enum type");
2468 return QualType();
2469 }
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002470 bool IsDependent = Record[0];
2471 QualType T = Context->getEnumType(cast<EnumDecl>(GetDecl(Record[1])));
2472 T->Dependent = IsDependent;
2473 return T;
2474 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002475
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002476 case TYPE_ELABORATED: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00002477 unsigned Idx = 0;
2478 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2479 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2480 QualType NamedType = GetType(Record[Idx++]);
2481 return Context->getElaboratedType(Keyword, NNS, NamedType);
John McCall7da24312009-09-05 00:15:47 +00002482 }
2483
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002484 case TYPE_OBJC_INTERFACE: {
Chris Lattnerc6fa4452009-04-22 06:45:28 +00002485 unsigned Idx = 0;
2486 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
John McCallc12c5bb2010-05-15 11:32:37 +00002487 return Context->getObjCInterfaceType(ItfD);
2488 }
2489
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002490 case TYPE_OBJC_OBJECT: {
John McCallc12c5bb2010-05-15 11:32:37 +00002491 unsigned Idx = 0;
2492 QualType Base = GetType(Record[Idx++]);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00002493 unsigned NumProtos = Record[Idx++];
2494 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2495 for (unsigned I = 0; I != NumProtos; ++I)
2496 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
John McCallc12c5bb2010-05-15 11:32:37 +00002497 return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00002498 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002499
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002500 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00002501 unsigned Idx = 0;
John McCallc12c5bb2010-05-15 11:32:37 +00002502 QualType Pointee = GetType(Record[Idx++]);
2503 return Context->getObjCObjectPointerType(Pointee);
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00002504 }
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +00002505
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002506 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCall49a832b2009-10-18 09:09:24 +00002507 unsigned Idx = 0;
2508 QualType Parm = GetType(Record[Idx++]);
2509 QualType Replacement = GetType(Record[Idx++]);
2510 return
2511 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
2512 Replacement);
2513 }
John McCall3cb0ebd2010-03-10 03:28:59 +00002514
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002515 case TYPE_INJECTED_CLASS_NAME: {
John McCall3cb0ebd2010-03-10 03:28:59 +00002516 CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0]));
2517 QualType TST = GetType(Record[1]); // probably derivable
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00002518 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002519 // for AST reading, too much interdependencies.
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00002520 return
2521 QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCall3cb0ebd2010-03-10 03:28:59 +00002522 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00002523
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002524 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002525 unsigned Idx = 0;
2526 unsigned Depth = Record[Idx++];
2527 unsigned Index = Record[Idx++];
2528 bool Pack = Record[Idx++];
2529 IdentifierInfo *Name = GetIdentifierInfo(Record, Idx);
2530 return Context->getTemplateTypeParmType(Depth, Index, Pack, Name);
2531 }
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00002532
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002533 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00002534 unsigned Idx = 0;
2535 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2536 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2537 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +00002538 QualType Canon = GetType(Record[Idx++]);
2539 return Context->getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00002540 }
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00002541
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002542 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00002543 unsigned Idx = 0;
2544 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2545 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2546 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
2547 unsigned NumArgs = Record[Idx++];
2548 llvm::SmallVector<TemplateArgument, 8> Args;
2549 Args.reserve(NumArgs);
2550 while (NumArgs--)
Sebastian Redl577d4792010-07-22 22:43:28 +00002551 Args.push_back(ReadTemplateArgument(DeclsCursor, Record, Idx));
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00002552 return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
2553 Args.size(), Args.data());
2554 }
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00002555
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002556 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00002557 unsigned Idx = 0;
2558
2559 // ArrayType
2560 QualType ElementType = GetType(Record[Idx++]);
2561 ArrayType::ArraySizeModifier ASM
2562 = (ArrayType::ArraySizeModifier)Record[Idx++];
2563 unsigned IndexTypeQuals = Record[Idx++];
2564
2565 // DependentSizedArrayType
Sebastian Redl577d4792010-07-22 22:43:28 +00002566 Expr *NumElts = ReadExpr(DeclsCursor);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00002567 SourceRange Brackets = ReadSourceRange(Record, Idx);
2568
2569 return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
2570 IndexTypeQuals, Brackets);
2571 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00002572
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002573 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002574 unsigned Idx = 0;
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002575 bool IsDependent = Record[Idx++];
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002576 TemplateName Name = ReadTemplateName(Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002577 llvm::SmallVector<TemplateArgument, 8> Args;
Sebastian Redl577d4792010-07-22 22:43:28 +00002578 ReadTemplateArgumentList(Args, DeclsCursor, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00002579 QualType Canon = GetType(Record[Idx++]);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002580 QualType T;
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002581 if (Canon.isNull())
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002582 T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(),
2583 Args.size());
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002584 else
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002585 T = Context->getTemplateSpecializationType(Name, Args.data(),
2586 Args.size(), Canon);
2587 T->Dependent = IsDependent;
2588 return T;
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002589 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002590 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002591 // Suppress a GCC warning
2592 return QualType();
2593}
2594
John McCalla1ee0c52009-10-16 21:56:05 +00002595namespace {
2596
2597class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002598 ASTReader &Reader;
Sebastian Redl577d4792010-07-22 22:43:28 +00002599 llvm::BitstreamCursor &DeclsCursor;
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002600 const ASTReader::RecordData &Record;
John McCalla1ee0c52009-10-16 21:56:05 +00002601 unsigned &Idx;
2602
2603public:
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002604 TypeLocReader(ASTReader &Reader, llvm::BitstreamCursor &Cursor,
2605 const ASTReader::RecordData &Record, unsigned &Idx)
Sebastian Redl577d4792010-07-22 22:43:28 +00002606 : Reader(Reader), DeclsCursor(Cursor), Record(Record), Idx(Idx) { }
John McCalla1ee0c52009-10-16 21:56:05 +00002607
John McCall51bd8032009-10-18 01:05:36 +00002608 // We want compile-time assurance that we've enumerated all of
2609 // these, so unfortunately we have to declare them first, then
2610 // define them out-of-line.
2611#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +00002612#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +00002613 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00002614#include "clang/AST/TypeLocNodes.def"
2615
John McCall51bd8032009-10-18 01:05:36 +00002616 void VisitFunctionTypeLoc(FunctionTypeLoc);
2617 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00002618};
2619
2620}
2621
John McCall51bd8032009-10-18 01:05:36 +00002622void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCalla1ee0c52009-10-16 21:56:05 +00002623 // nothing to do
2624}
John McCall51bd8032009-10-18 01:05:36 +00002625void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +00002626 TL.setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2627 if (TL.needsExtraLocalData()) {
2628 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
2629 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
2630 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
2631 TL.setModeAttr(Record[Idx++]);
2632 }
John McCalla1ee0c52009-10-16 21:56:05 +00002633}
John McCall51bd8032009-10-18 01:05:36 +00002634void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
2635 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002636}
John McCall51bd8032009-10-18 01:05:36 +00002637void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
2638 TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002639}
John McCall51bd8032009-10-18 01:05:36 +00002640void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
2641 TL.setCaretLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002642}
John McCall51bd8032009-10-18 01:05:36 +00002643void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
2644 TL.setAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002645}
John McCall51bd8032009-10-18 01:05:36 +00002646void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
2647 TL.setAmpAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002648}
John McCall51bd8032009-10-18 01:05:36 +00002649void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
2650 TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002651}
John McCall51bd8032009-10-18 01:05:36 +00002652void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
2653 TL.setLBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2654 TL.setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002655 if (Record[Idx++])
Sebastian Redl577d4792010-07-22 22:43:28 +00002656 TL.setSizeExpr(Reader.ReadExpr(DeclsCursor));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002657 else
John McCall51bd8032009-10-18 01:05:36 +00002658 TL.setSizeExpr(0);
2659}
2660void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
2661 VisitArrayTypeLoc(TL);
2662}
2663void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
2664 VisitArrayTypeLoc(TL);
2665}
2666void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
2667 VisitArrayTypeLoc(TL);
2668}
2669void TypeLocReader::VisitDependentSizedArrayTypeLoc(
2670 DependentSizedArrayTypeLoc TL) {
2671 VisitArrayTypeLoc(TL);
2672}
2673void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
2674 DependentSizedExtVectorTypeLoc TL) {
2675 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2676}
2677void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
2678 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2679}
2680void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
2681 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2682}
2683void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
2684 TL.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2685 TL.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2686 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
John McCall86acc2a2009-10-23 01:28:53 +00002687 TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
John McCall51bd8032009-10-18 01:05:36 +00002688 }
2689}
2690void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
2691 VisitFunctionTypeLoc(TL);
2692}
2693void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
2694 VisitFunctionTypeLoc(TL);
2695}
John McCalled976492009-12-04 22:46:56 +00002696void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
2697 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2698}
John McCall51bd8032009-10-18 01:05:36 +00002699void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
2700 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2701}
2702void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +00002703 TL.setTypeofLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2704 TL.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2705 TL.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall51bd8032009-10-18 01:05:36 +00002706}
2707void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +00002708 TL.setTypeofLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2709 TL.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2710 TL.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Sebastian Redl577d4792010-07-22 22:43:28 +00002711 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(DeclsCursor, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00002712}
2713void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
2714 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2715}
2716void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
2717 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2718}
2719void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
2720 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2721}
John McCall51bd8032009-10-18 01:05:36 +00002722void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
2723 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2724}
John McCall49a832b2009-10-18 09:09:24 +00002725void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
2726 SubstTemplateTypeParmTypeLoc TL) {
2727 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2728}
John McCall51bd8032009-10-18 01:05:36 +00002729void TypeLocReader::VisitTemplateSpecializationTypeLoc(
2730 TemplateSpecializationTypeLoc TL) {
John McCall833ca992009-10-29 08:12:44 +00002731 TL.setTemplateNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2732 TL.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2733 TL.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2734 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
2735 TL.setArgLocInfo(i,
2736 Reader.GetTemplateArgumentLocInfo(TL.getTypePtr()->getArg(i).getKind(),
Sebastian Redl577d4792010-07-22 22:43:28 +00002737 DeclsCursor, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00002738}
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002739void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002740 TL.setKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2741 TL.setQualifierRange(Reader.ReadSourceRange(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00002742}
John McCall3cb0ebd2010-03-10 03:28:59 +00002743void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
2744 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2745}
Douglas Gregor4714c122010-03-31 17:34:00 +00002746void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002747 TL.setKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2748 TL.setQualifierRange(Reader.ReadSourceRange(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00002749 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2750}
John McCall33500952010-06-11 00:33:02 +00002751void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
2752 DependentTemplateSpecializationTypeLoc TL) {
2753 TL.setKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2754 TL.setQualifierRange(Reader.ReadSourceRange(Record, Idx));
2755 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2756 TL.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2757 TL.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2758 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
2759 TL.setArgLocInfo(I,
2760 Reader.GetTemplateArgumentLocInfo(TL.getTypePtr()->getArg(I).getKind(),
Sebastian Redl577d4792010-07-22 22:43:28 +00002761 DeclsCursor, Record, Idx));
John McCall33500952010-06-11 00:33:02 +00002762}
John McCall51bd8032009-10-18 01:05:36 +00002763void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
2764 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCallc12c5bb2010-05-15 11:32:37 +00002765}
2766void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
2767 TL.setHasBaseTypeAsWritten(Record[Idx++]);
John McCall51bd8032009-10-18 01:05:36 +00002768 TL.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2769 TL.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2770 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
2771 TL.setProtocolLoc(i, SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002772}
John McCall54e14c42009-10-22 22:37:11 +00002773void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
2774 TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall54e14c42009-10-22 22:37:11 +00002775}
John McCalla1ee0c52009-10-16 21:56:05 +00002776
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002777TypeSourceInfo *ASTReader::GetTypeSourceInfo(llvm::BitstreamCursor &DeclsCursor,
Sebastian Redl577d4792010-07-22 22:43:28 +00002778 const RecordData &Record,
John McCalla1ee0c52009-10-16 21:56:05 +00002779 unsigned &Idx) {
2780 QualType InfoTy = GetType(Record[Idx++]);
2781 if (InfoTy.isNull())
2782 return 0;
2783
John McCalla93c9342009-12-07 02:54:59 +00002784 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
Sebastian Redl577d4792010-07-22 22:43:28 +00002785 TypeLocReader TLR(*this, DeclsCursor, Record, Idx);
John McCalla93c9342009-12-07 02:54:59 +00002786 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCalla1ee0c52009-10-16 21:56:05 +00002787 TLR.Visit(TL);
John McCalla93c9342009-12-07 02:54:59 +00002788 return TInfo;
John McCalla1ee0c52009-10-16 21:56:05 +00002789}
Douglas Gregor2cf26342009-04-09 22:27:44 +00002790
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002791QualType ASTReader::GetType(TypeID ID) {
John McCall0953e762009-09-24 19:53:00 +00002792 unsigned FastQuals = ID & Qualifiers::FastMask;
2793 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002794
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002795 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002796 QualType T;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002797 switch ((PredefinedTypeIDs)Index) {
2798 case PREDEF_TYPE_NULL_ID: return QualType();
2799 case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
2800 case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002801
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002802 case PREDEF_TYPE_CHAR_U_ID:
2803 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregor2cf26342009-04-09 22:27:44 +00002804 // FIXME: Check that the signedness of CharTy is correct!
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002805 T = Context->CharTy;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002806 break;
2807
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002808 case PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break;
2809 case PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break;
2810 case PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break;
2811 case PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break;
2812 case PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break;
2813 case PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break;
2814 case PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break;
2815 case PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break;
2816 case PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break;
2817 case PREDEF_TYPE_INT_ID: T = Context->IntTy; break;
2818 case PREDEF_TYPE_LONG_ID: T = Context->LongTy; break;
2819 case PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break;
2820 case PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break;
2821 case PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break;
2822 case PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break;
2823 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break;
2824 case PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break;
2825 case PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break;
2826 case PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break;
2827 case PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break;
2828 case PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break;
2829 case PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break;
2830 case PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break;
2831 case PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002832 }
2833
2834 assert(!T.isNull() && "Unknown predefined type");
John McCall0953e762009-09-24 19:53:00 +00002835 return T.withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002836 }
2837
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002838 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002839 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl07a353c2010-07-14 20:26:45 +00002840 if (TypesLoaded[Index].isNull()) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002841 TypesLoaded[Index] = ReadTypeRecord(Index);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002842 TypesLoaded[Index]->setFromAST();
Sebastian Redl30c514c2010-07-14 23:45:08 +00002843 if (DeserializationListener)
Sebastian Redl1476ed42010-07-16 16:36:56 +00002844 DeserializationListener->TypeRead(ID >> Qualifiers::FastWidth,
2845 TypesLoaded[Index]);
Sebastian Redl07a353c2010-07-14 20:26:45 +00002846 }
Mike Stump1eb44332009-09-09 15:08:12 +00002847
John McCall0953e762009-09-24 19:53:00 +00002848 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002849}
2850
John McCall833ca992009-10-29 08:12:44 +00002851TemplateArgumentLocInfo
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002852ASTReader::GetTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Sebastian Redl577d4792010-07-22 22:43:28 +00002853 llvm::BitstreamCursor &DeclsCursor,
John McCall833ca992009-10-29 08:12:44 +00002854 const RecordData &Record,
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00002855 unsigned &Index) {
John McCall833ca992009-10-29 08:12:44 +00002856 switch (Kind) {
2857 case TemplateArgument::Expression:
Sebastian Redl577d4792010-07-22 22:43:28 +00002858 return ReadExpr(DeclsCursor);
John McCall833ca992009-10-29 08:12:44 +00002859 case TemplateArgument::Type:
Sebastian Redl577d4792010-07-22 22:43:28 +00002860 return GetTypeSourceInfo(DeclsCursor, Record, Index);
Douglas Gregor788cd062009-11-11 01:00:40 +00002861 case TemplateArgument::Template: {
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00002862 SourceRange QualifierRange = ReadSourceRange(Record, Index);
2863 SourceLocation TemplateNameLoc = ReadSourceLocation(Record, Index);
2864 return TemplateArgumentLocInfo(QualifierRange, TemplateNameLoc);
Douglas Gregor788cd062009-11-11 01:00:40 +00002865 }
John McCall833ca992009-10-29 08:12:44 +00002866 case TemplateArgument::Null:
2867 case TemplateArgument::Integral:
2868 case TemplateArgument::Declaration:
2869 case TemplateArgument::Pack:
2870 return TemplateArgumentLocInfo();
2871 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002872 llvm_unreachable("unexpected template argument loc");
John McCall833ca992009-10-29 08:12:44 +00002873 return TemplateArgumentLocInfo();
2874}
2875
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00002876TemplateArgumentLoc
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002877ASTReader::ReadTemplateArgumentLoc(llvm::BitstreamCursor &DeclsCursor,
Sebastian Redl577d4792010-07-22 22:43:28 +00002878 const RecordData &Record, unsigned &Index) {
2879 TemplateArgument Arg = ReadTemplateArgument(DeclsCursor, Record, Index);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00002880
2881 if (Arg.getKind() == TemplateArgument::Expression) {
2882 if (Record[Index++]) // bool InfoHasSameExpr.
2883 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
2884 }
2885 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(Arg.getKind(),
Sebastian Redl577d4792010-07-22 22:43:28 +00002886 DeclsCursor,
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00002887 Record, Index));
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002888}
2889
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002890Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall76bd1f32010-06-01 09:23:16 +00002891 return GetDecl(ID);
2892}
2893
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002894TranslationUnitDecl *ASTReader::GetTranslationUnitDecl() {
Sebastian Redl30c514c2010-07-14 23:45:08 +00002895 if (!DeclsLoaded[0]) {
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00002896 ReadDeclRecord(0, 0);
Sebastian Redl30c514c2010-07-14 23:45:08 +00002897 if (DeserializationListener)
Sebastian Redl1476ed42010-07-16 16:36:56 +00002898 DeserializationListener->DeclRead(1, DeclsLoaded[0]);
Sebastian Redl30c514c2010-07-14 23:45:08 +00002899 }
Argyrios Kyrtzidis8871a442010-07-08 17:13:02 +00002900
2901 return cast<TranslationUnitDecl>(DeclsLoaded[0]);
2902}
2903
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002904Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002905 if (ID == 0)
2906 return 0;
2907
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002908 if (ID > DeclsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002909 Error("declaration ID out-of-range for AST file");
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002910 return 0;
2911 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002912
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002913 unsigned Index = ID - 1;
Sebastian Redl30c514c2010-07-14 23:45:08 +00002914 if (!DeclsLoaded[Index]) {
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00002915 ReadDeclRecord(Index, ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00002916 if (DeserializationListener)
2917 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
2918 }
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002919
2920 return DeclsLoaded[Index];
Douglas Gregor2cf26342009-04-09 22:27:44 +00002921}
2922
Chris Lattner887e2b32009-04-27 05:46:25 +00002923/// \brief Resolve the offset of a statement into a statement.
2924///
2925/// This operation will read a new statement from the external
2926/// source each time it is called, and is meant to be used via a
2927/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002928Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00002929 // Offset here is a global offset across the entire chain.
2930 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2931 PerFileData &F = *Chain[N - I - 1];
2932 if (Offset < F.SizeInBits) {
2933 // Since we know that this statement is part of a decl, make sure to use
2934 // the decl cursor to read it.
2935 F.DeclsCursor.JumpToBit(Offset);
2936 return ReadStmtFromStream(F.DeclsCursor);
2937 }
2938 Offset -= F.SizeInBits;
2939 }
2940 llvm_unreachable("Broken chain");
Douglas Gregor250fc9c2009-04-18 00:07:54 +00002941}
2942
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002943bool ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +00002944 llvm::SmallVectorImpl<Decl*> &Decls) {
Mike Stump1eb44332009-09-09 15:08:12 +00002945 assert(DC->hasExternalLexicalStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00002946 "DeclContext has no lexical decls in storage");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002947
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00002948 // There might be lexical decls in multiple parts of the chain, for the TU
2949 // at least.
2950 DeclContextInfos &Infos = DeclContextOffsets[DC];
2951 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
2952 I != E; ++I) {
Sebastian Redl681d7232010-07-27 00:17:23 +00002953 // IDs can be 0 if this context doesn't contain declarations.
2954 if (!I->LexicalDecls)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00002955 continue;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00002956
2957 // Load all of the declaration IDs
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002958 for (const DeclID *ID = I->LexicalDecls,
Sebastian Redl681d7232010-07-27 00:17:23 +00002959 *IDE = ID + I->NumLexicalDecls;
2960 ID != IDE; ++ID)
2961 Decls.push_back(GetDecl(*ID));
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002962 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002963
Douglas Gregor25123082009-04-22 22:34:57 +00002964 ++NumLexicalDeclContextsRead;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002965 return false;
2966}
2967
John McCall76bd1f32010-06-01 09:23:16 +00002968DeclContext::lookup_result
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002969ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +00002970 DeclarationName Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00002971 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00002972 "DeclContext has no visible decls in storage");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002973
John McCall76bd1f32010-06-01 09:23:16 +00002974 llvm::SmallVector<VisibleDeclaration, 64> Decls;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00002975 // There might be lexical decls in multiple parts of the chain, for the TU
2976 // and namespaces.
2977 DeclContextInfos &Infos = DeclContextOffsets[DC];
2978 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
2979 I != E; ++I) {
2980 uint64_t Offset = I->OffsetToVisibleDecls;
2981 if (Offset == 0)
2982 continue;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002983
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00002984 llvm::BitstreamCursor &DeclsCursor = *I->Stream;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002985
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00002986 // Keep track of where we are in the stream, then jump back there
2987 // after reading this context.
2988 SavedStreamPosition SavedPosition(DeclsCursor);
2989
2990 // Load the record containing all of the declarations visible in
2991 // this context.
2992 DeclsCursor.JumpToBit(Offset);
2993 RecordData Record;
2994 unsigned Code = DeclsCursor.ReadCode();
2995 unsigned RecCode = DeclsCursor.ReadRecord(Code, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002996 if (RecCode != DECL_CONTEXT_VISIBLE) {
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00002997 Error("Expected visible block");
2998 return DeclContext::lookup_result(DeclContext::lookup_iterator(),
2999 DeclContext::lookup_iterator());
3000 }
3001
3002 if (Record.empty())
3003 continue;
3004
3005 unsigned Idx = 0;
3006 while (Idx < Record.size()) {
3007 Decls.push_back(VisibleDeclaration());
3008 Decls.back().Name = ReadDeclarationName(Record, Idx);
3009
3010 unsigned Size = Record[Idx++];
3011 llvm::SmallVector<unsigned, 4> &LoadedDecls = Decls.back().Declarations;
3012 LoadedDecls.reserve(Size);
3013 for (unsigned J = 0; J < Size; ++J)
3014 LoadedDecls.push_back(Record[Idx++]);
3015 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003016 }
3017
Douglas Gregor25123082009-04-22 22:34:57 +00003018 ++NumVisibleDeclContextsRead;
John McCall76bd1f32010-06-01 09:23:16 +00003019
3020 SetExternalVisibleDecls(DC, Decls);
3021 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003022}
3023
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003024void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003025 assert(Consumer);
3026 while (!InterestingDecls.empty()) {
3027 DeclGroupRef DG(InterestingDecls.front());
3028 InterestingDecls.pop_front();
Sebastian Redl27372b42010-08-11 18:52:41 +00003029 Consumer->HandleInterestingDecl(DG);
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003030 }
3031}
3032
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003033void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor0af2ca42009-04-22 19:09:20 +00003034 this->Consumer = Consumer;
3035
Douglas Gregorfdd01722009-04-14 00:24:19 +00003036 if (!Consumer)
3037 return;
3038
3039 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003040 // Force deserialization of this decl, which will cause it to be queued for
3041 // passing to the consumer.
Daniel Dunbar04a0b502009-09-17 03:06:44 +00003042 GetDecl(ExternalDefinitions[I]);
Douglas Gregorfdd01722009-04-14 00:24:19 +00003043 }
Douglas Gregorc62a2fe2009-04-25 00:41:30 +00003044
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003045 PassInterestingDeclsToConsumer();
Douglas Gregorfdd01722009-04-14 00:24:19 +00003046}
3047
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003048void ASTReader::PrintStats() {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003049 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregor2cf26342009-04-09 22:27:44 +00003050
Mike Stump1eb44332009-09-09 15:08:12 +00003051 unsigned NumTypesLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003052 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall0953e762009-09-24 19:53:00 +00003053 QualType());
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003054 unsigned NumDeclsLoaded
3055 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
3056 (Decl *)0);
3057 unsigned NumIdentifiersLoaded
3058 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
3059 IdentifiersLoaded.end(),
3060 (IdentifierInfo *)0);
Mike Stump1eb44332009-09-09 15:08:12 +00003061 unsigned NumSelectorsLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003062 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
3063 SelectorsLoaded.end(),
3064 Selector());
Douglas Gregor2d41cc12009-04-13 20:50:16 +00003065
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003066 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
3067 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00003068 if (TotalNumSLocEntries)
3069 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
3070 NumSLocEntriesRead, TotalNumSLocEntries,
3071 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003072 if (!TypesLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003073 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003074 NumTypesLoaded, (unsigned)TypesLoaded.size(),
3075 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
3076 if (!DeclsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003077 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003078 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
3079 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003080 if (!IdentifiersLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003081 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003082 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
3083 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redl725cd962010-08-04 20:40:17 +00003084 if (!SelectorsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003085 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redl725cd962010-08-04 20:40:17 +00003086 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
3087 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor83941df2009-04-25 17:48:32 +00003088 if (TotalNumStatements)
3089 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
3090 NumStatementsRead, TotalNumStatements,
3091 ((float)NumStatementsRead/TotalNumStatements * 100));
3092 if (TotalNumMacros)
3093 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
3094 NumMacrosRead, TotalNumMacros,
3095 ((float)NumMacrosRead/TotalNumMacros * 100));
3096 if (TotalLexicalDeclContexts)
3097 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
3098 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
3099 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
3100 * 100));
3101 if (TotalVisibleDeclContexts)
3102 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
3103 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
3104 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
3105 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003106 if (TotalNumMethodPoolEntries) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003107 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003108 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
3109 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor83941df2009-04-25 17:48:32 +00003110 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003111 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor83941df2009-04-25 17:48:32 +00003112 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003113 std::fprintf(stderr, "\n");
3114}
3115
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003116void ASTReader::InitializeSema(Sema &S) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00003117 SemaObj = &S;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003118 S.ExternalSource = this;
3119
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00003120 // Makes sure any declarations that were deserialized "too early"
3121 // still get added to the identifier's declaration chains.
Douglas Gregor914ed9d2010-08-13 03:15:25 +00003122 if (SemaObj->TUScope) {
3123 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
3124 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(PreloadedDecls[I]));
3125 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
3126 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00003127 }
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00003128 PreloadedDecls.clear();
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003129
3130 // If there were any tentative definitions, deserialize them and add
Sebastian Redle9d12b62010-01-31 22:27:38 +00003131 // them to Sema's list of tentative definitions.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003132 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
3133 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
Sebastian Redle9d12b62010-01-31 22:27:38 +00003134 SemaObj->TentativeDefinitions.push_back(Var);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003135 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00003136
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003137 // If there were any unused file scoped decls, deserialize them and add to
3138 // Sema's list of unused file scoped decls.
3139 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
3140 DeclaratorDecl *D = cast<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
3141 SemaObj->UnusedFileScopedDecls.push_back(D);
Tanya Lattnere6bbc012010-02-12 00:07:30 +00003142 }
Douglas Gregor14c22f22009-04-22 22:18:58 +00003143
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003144 // If there were any weak undeclared identifiers, deserialize them and add to
3145 // Sema's list of weak undeclared identifiers.
3146 if (!WeakUndeclaredIdentifiers.empty()) {
3147 unsigned Idx = 0;
3148 for (unsigned I = 0, N = WeakUndeclaredIdentifiers[Idx++]; I != N; ++I) {
3149 IdentifierInfo *WeakId = GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3150 IdentifierInfo *AliasId=GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3151 SourceLocation Loc = ReadSourceLocation(WeakUndeclaredIdentifiers, Idx);
3152 bool Used = WeakUndeclaredIdentifiers[Idx++];
3153 Sema::WeakInfo WI(AliasId, Loc);
3154 WI.setUsed(Used);
3155 SemaObj->WeakUndeclaredIdentifiers.insert(std::make_pair(WeakId, WI));
3156 }
3157 }
3158
Douglas Gregor14c22f22009-04-22 22:18:58 +00003159 // If there were any locally-scoped external declarations,
3160 // deserialize them and add them to Sema's table of locally-scoped
3161 // external declarations.
3162 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
3163 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
3164 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
3165 }
Douglas Gregorb81c1702009-04-27 20:06:05 +00003166
3167 // If there were any ext_vector type declarations, deserialize them
3168 // and add them to Sema's vector of such declarations.
3169 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I)
3170 SemaObj->ExtVectorDecls.push_back(
3171 cast<TypedefDecl>(GetDecl(ExtVectorDecls[I])));
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003172
3173 // FIXME: Do VTable uses and dynamic classes deserialize too much ?
3174 // Can we cut them down before writing them ?
3175
3176 // If there were any VTable uses, deserialize the information and add it
3177 // to Sema's vector and map of VTable uses.
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003178 if (!VTableUses.empty()) {
3179 unsigned Idx = 0;
3180 for (unsigned I = 0, N = VTableUses[Idx++]; I != N; ++I) {
3181 CXXRecordDecl *Class = cast<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
3182 SourceLocation Loc = ReadSourceLocation(VTableUses, Idx);
3183 bool DefinitionRequired = VTableUses[Idx++];
3184 SemaObj->VTableUses.push_back(std::make_pair(Class, Loc));
3185 SemaObj->VTablesUsed[Class] = DefinitionRequired;
3186 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003187 }
3188
3189 // If there were any dynamic classes declarations, deserialize them
3190 // and add them to Sema's vector of such declarations.
3191 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I)
3192 SemaObj->DynamicClasses.push_back(
3193 cast<CXXRecordDecl>(GetDecl(DynamicClasses[I])));
Fariborz Jahanian32019832010-07-23 19:11:11 +00003194
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003195 // If there were any pending implicit instantiations, deserialize them
3196 // and add them to Sema's queue of such instantiations.
3197 assert(PendingImplicitInstantiations.size() % 2 == 0 &&
3198 "Expected pairs of entries");
3199 for (unsigned Idx = 0, N = PendingImplicitInstantiations.size(); Idx < N;) {
3200 ValueDecl *D=cast<ValueDecl>(GetDecl(PendingImplicitInstantiations[Idx++]));
3201 SourceLocation Loc = ReadSourceLocation(PendingImplicitInstantiations, Idx);
3202 SemaObj->PendingImplicitInstantiations.push_back(std::make_pair(D, Loc));
3203 }
3204
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003205 // Load the offsets of the declarations that Sema references.
3206 // They will be lazily deserialized when needed.
3207 if (!SemaDeclRefs.empty()) {
3208 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
3209 SemaObj->StdNamespace = SemaDeclRefs[0];
3210 SemaObj->StdBadAlloc = SemaDeclRefs[1];
3211 }
3212
Fariborz Jahanian32019832010-07-23 19:11:11 +00003213 // If there are @selector references added them to its pool. This is for
3214 // implementation of -Wselector.
Sebastian Redl725cd962010-08-04 20:40:17 +00003215 if (!ReferencedSelectorsData.empty()) {
3216 unsigned int DataSize = ReferencedSelectorsData.size()-1;
Fariborz Jahanian32019832010-07-23 19:11:11 +00003217 unsigned I = 0;
3218 while (I < DataSize) {
Sebastian Redl725cd962010-08-04 20:40:17 +00003219 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
Fariborz Jahanian32019832010-07-23 19:11:11 +00003220 SourceLocation SelLoc =
Sebastian Redl725cd962010-08-04 20:40:17 +00003221 SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
Fariborz Jahanian32019832010-07-23 19:11:11 +00003222 SemaObj->ReferencedSelectors.insert(std::make_pair(Sel, SelLoc));
3223 }
3224 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00003225}
3226
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003227IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Sebastian Redld8c5abb2010-08-02 18:30:12 +00003228 // Try to find this name within our on-disk hash tables. We start with the
3229 // most recent one, since that one contains the most up-to-date info.
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003230 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003231 ASTIdentifierLookupTable *IdTable
3232 = (ASTIdentifierLookupTable *)Chain[I]->IdentifierLookupTable;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003233 if (!IdTable)
3234 continue;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003235 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003236 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003237 if (Pos == IdTable->end())
3238 continue;
Douglas Gregor668c1a42009-04-21 22:25:48 +00003239
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003240 // Dereferencing the iterator has the effect of building the
3241 // IdentifierInfo node and populating it with the various
3242 // declarations it needs.
Sebastian Redld8c5abb2010-08-02 18:30:12 +00003243 return *Pos;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003244 }
Sebastian Redld8c5abb2010-08-02 18:30:12 +00003245 return 0;
Douglas Gregor668c1a42009-04-21 22:25:48 +00003246}
3247
Mike Stump1eb44332009-09-09 15:08:12 +00003248std::pair<ObjCMethodList, ObjCMethodList>
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003249ASTReader::ReadMethodPool(Selector Sel) {
Sebastian Redl725cd962010-08-04 20:40:17 +00003250 // Find this selector in a hash table. We want to find the most recent entry.
3251 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3252 PerFileData &F = *Chain[I];
3253 if (!F.SelectorLookupTable)
3254 continue;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003255
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003256 ASTSelectorLookupTable *PoolTable
3257 = (ASTSelectorLookupTable*)F.SelectorLookupTable;
3258 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
Sebastian Redl725cd962010-08-04 20:40:17 +00003259 if (Pos != PoolTable->end()) {
3260 ++NumSelectorsRead;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003261 // FIXME: Not quite happy with the statistics here. We probably should
3262 // disable this tracking when called via LoadSelector.
3263 // Also, should entries without methods count as misses?
3264 ++NumMethodPoolEntriesRead;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003265 ASTSelectorLookupTrait::data_type Data = *Pos;
Sebastian Redl725cd962010-08-04 20:40:17 +00003266 if (DeserializationListener)
3267 DeserializationListener->SelectorRead(Data.ID, Sel);
3268 return std::make_pair(Data.Instance, Data.Factory);
3269 }
Douglas Gregor83941df2009-04-25 17:48:32 +00003270 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003271
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003272 ++NumMethodPoolMisses;
Sebastian Redl725cd962010-08-04 20:40:17 +00003273 return std::pair<ObjCMethodList, ObjCMethodList>();
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003274}
3275
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003276void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redle58aa892010-08-04 18:21:41 +00003277 // It would be complicated to avoid reading the methods anyway. So don't.
3278 ReadMethodPool(Sel);
3279}
3280
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003281void ASTReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00003282 assert(ID && "Non-zero identifier ID required");
Douglas Gregora02b1472009-04-28 21:53:25 +00003283 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003284 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003285 if (DeserializationListener)
3286 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregor668c1a42009-04-21 22:25:48 +00003287}
3288
Douglas Gregord89275b2009-07-06 18:54:52 +00003289/// \brief Set the globally-visible declarations associated with the given
3290/// identifier.
3291///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003292/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump1eb44332009-09-09 15:08:12 +00003293/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregord89275b2009-07-06 18:54:52 +00003294/// them.
3295///
3296/// \param II an IdentifierInfo that refers to one or more globally-visible
3297/// declarations.
3298///
3299/// \param DeclIDs the set of declaration IDs with the name @p II that are
3300/// visible at global scope.
3301///
3302/// \param Nonrecursive should be true to indicate that the caller knows that
3303/// this call is non-recursive, and therefore the globally-visible declarations
3304/// will not be placed onto the pending queue.
Mike Stump1eb44332009-09-09 15:08:12 +00003305void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003306ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Douglas Gregord89275b2009-07-06 18:54:52 +00003307 const llvm::SmallVectorImpl<uint32_t> &DeclIDs,
3308 bool Nonrecursive) {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00003309 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregord89275b2009-07-06 18:54:52 +00003310 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
3311 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
3312 PII.II = II;
3313 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I)
3314 PII.DeclIDs.push_back(DeclIDs[I]);
3315 return;
3316 }
Mike Stump1eb44332009-09-09 15:08:12 +00003317
Douglas Gregord89275b2009-07-06 18:54:52 +00003318 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
3319 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
3320 if (SemaObj) {
Douglas Gregor914ed9d2010-08-13 03:15:25 +00003321 if (SemaObj->TUScope) {
3322 // Introduce this declaration into the translation-unit scope
3323 // and add it to the declaration chain for this identifier, so
3324 // that (unqualified) name lookup will find it.
3325 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(D));
3326 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
3327 }
Douglas Gregord89275b2009-07-06 18:54:52 +00003328 } else {
3329 // Queue this declaration so that it will be added to the
3330 // translation unit scope and identifier's declaration chain
3331 // once a Sema object is known.
3332 PreloadedDecls.push_back(D);
3333 }
3334 }
3335}
3336
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003337IdentifierInfo *ASTReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00003338 if (ID == 0)
3339 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003340
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00003341 if (IdentifiersLoaded.empty()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003342 Error("no identifier table in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00003343 return 0;
3344 }
Mike Stump1eb44332009-09-09 15:08:12 +00003345
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003346 assert(PP && "Forgot to set Preprocessor ?");
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00003347 ID -= 1;
3348 if (!IdentifiersLoaded[ID]) {
3349 unsigned Index = ID;
3350 const char *Str = 0;
3351 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3352 PerFileData *F = Chain[N - I - 1];
3353 if (Index < F->LocalNumIdentifiers) {
3354 uint32_t Offset = F->IdentifierOffsets[Index];
3355 Str = F->IdentifierTableData + Offset;
3356 break;
3357 }
3358 Index -= F->LocalNumIdentifiers;
3359 }
3360 assert(Str && "Broken Chain");
Douglas Gregord6595a42009-04-25 21:04:17 +00003361
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003362 // All of the strings in the AST file are preceded by a 16-bit length.
3363 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenek231bc0b2009-10-23 04:45:31 +00003364 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
3365 // unsigned integers. This is important to avoid integer overflow when
3366 // we cast them to 'unsigned'.
Ted Kremenekff1ea462009-10-23 03:57:22 +00003367 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregor02fc7512009-04-28 20:01:51 +00003368 unsigned StrLen = (((unsigned) StrLenPtr[0])
3369 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00003370 IdentifiersLoaded[ID]
Kovarththanan Rajaratnam811f4262010-03-12 10:32:27 +00003371 = &PP->getIdentifierTable().get(Str, StrLen);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003372 if (DeserializationListener)
3373 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregorafaf3082009-04-11 00:14:32 +00003374 }
Mike Stump1eb44332009-09-09 15:08:12 +00003375
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00003376 return IdentifiersLoaded[ID];
Douglas Gregor2cf26342009-04-09 22:27:44 +00003377}
3378
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003379void ASTReader::ReadSLocEntry(unsigned ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00003380 ReadSLocEntryRecord(ID);
3381}
3382
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003383Selector ASTReader::DecodeSelector(unsigned ID) {
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003384 if (ID == 0)
3385 return Selector();
Mike Stump1eb44332009-09-09 15:08:12 +00003386
Sebastian Redl725cd962010-08-04 20:40:17 +00003387 if (ID > SelectorsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003388 Error("selector ID out of range in AST file");
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003389 return Selector();
3390 }
Douglas Gregor83941df2009-04-25 17:48:32 +00003391
Sebastian Redl725cd962010-08-04 20:40:17 +00003392 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003393 // Load this selector from the selector table.
Sebastian Redl725cd962010-08-04 20:40:17 +00003394 unsigned Idx = ID - 1;
3395 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3396 PerFileData &F = *Chain[N - I - 1];
3397 if (Idx < F.LocalNumSelectors) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003398 ASTSelectorLookupTrait Trait(*this);
Sebastian Redl725cd962010-08-04 20:40:17 +00003399 SelectorsLoaded[ID - 1] =
3400 Trait.ReadKey(F.SelectorLookupTableData + F.SelectorOffsets[Idx], 0);
3401 if (DeserializationListener)
3402 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
3403 break;
3404 }
3405 Idx -= F.LocalNumSelectors;
3406 }
Douglas Gregor83941df2009-04-25 17:48:32 +00003407 }
3408
Sebastian Redl725cd962010-08-04 20:40:17 +00003409 return SelectorsLoaded[ID - 1];
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003410}
3411
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003412Selector ASTReader::GetExternalSelector(uint32_t ID) {
Douglas Gregor719770d2010-04-06 17:30:22 +00003413 return DecodeSelector(ID);
3414}
3415
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003416uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redl725cd962010-08-04 20:40:17 +00003417 // ID 0 (the null selector) is considered an external selector.
3418 return getTotalNumSelectors() + 1;
Douglas Gregor719770d2010-04-06 17:30:22 +00003419}
3420
Mike Stump1eb44332009-09-09 15:08:12 +00003421DeclarationName
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003422ASTReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003423 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
3424 switch (Kind) {
3425 case DeclarationName::Identifier:
3426 return DeclarationName(GetIdentifierInfo(Record, Idx));
3427
3428 case DeclarationName::ObjCZeroArgSelector:
3429 case DeclarationName::ObjCOneArgSelector:
3430 case DeclarationName::ObjCMultiArgSelector:
Steve Naroffa7503a72009-04-23 15:15:40 +00003431 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00003432
3433 case DeclarationName::CXXConstructorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003434 return Context->DeclarationNames.getCXXConstructorName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00003435 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00003436
3437 case DeclarationName::CXXDestructorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003438 return Context->DeclarationNames.getCXXDestructorName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00003439 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00003440
3441 case DeclarationName::CXXConversionFunctionName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003442 return Context->DeclarationNames.getCXXConversionFunctionName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00003443 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00003444
3445 case DeclarationName::CXXOperatorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003446 return Context->DeclarationNames.getCXXOperatorName(
Douglas Gregor2cf26342009-04-09 22:27:44 +00003447 (OverloadedOperatorKind)Record[Idx++]);
3448
Sean Hunt3e518bd2009-11-29 07:34:05 +00003449 case DeclarationName::CXXLiteralOperatorName:
3450 return Context->DeclarationNames.getCXXLiteralOperatorName(
3451 GetIdentifierInfo(Record, Idx));
3452
Douglas Gregor2cf26342009-04-09 22:27:44 +00003453 case DeclarationName::CXXUsingDirective:
3454 return DeclarationName::getUsingDirectiveName();
3455 }
3456
3457 // Required to silence GCC warning
3458 return DeclarationName();
3459}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003460
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003461TemplateName
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003462ASTReader::ReadTemplateName(const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003463 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
3464 switch (Kind) {
3465 case TemplateName::Template:
3466 return TemplateName(cast_or_null<TemplateDecl>(GetDecl(Record[Idx++])));
3467
3468 case TemplateName::OverloadedTemplate: {
3469 unsigned size = Record[Idx++];
3470 UnresolvedSet<8> Decls;
3471 while (size--)
3472 Decls.addDecl(cast<NamedDecl>(GetDecl(Record[Idx++])));
3473
3474 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
3475 }
3476
3477 case TemplateName::QualifiedTemplate: {
3478 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3479 bool hasTemplKeyword = Record[Idx++];
3480 TemplateDecl *Template = cast<TemplateDecl>(GetDecl(Record[Idx++]));
3481 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
3482 }
3483
3484 case TemplateName::DependentTemplate: {
3485 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3486 if (Record[Idx++]) // isIdentifier
3487 return Context->getDependentTemplateName(NNS,
3488 GetIdentifierInfo(Record, Idx));
3489 return Context->getDependentTemplateName(NNS,
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003490 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003491 }
3492 }
3493
3494 assert(0 && "Unhandled template name kind!");
3495 return TemplateName();
3496}
3497
3498TemplateArgument
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003499ASTReader::ReadTemplateArgument(llvm::BitstreamCursor &DeclsCursor,
Sebastian Redl577d4792010-07-22 22:43:28 +00003500 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003501 switch ((TemplateArgument::ArgKind)Record[Idx++]) {
3502 case TemplateArgument::Null:
3503 return TemplateArgument();
3504 case TemplateArgument::Type:
3505 return TemplateArgument(GetType(Record[Idx++]));
3506 case TemplateArgument::Declaration:
3507 return TemplateArgument(GetDecl(Record[Idx++]));
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00003508 case TemplateArgument::Integral: {
3509 llvm::APSInt Value = ReadAPSInt(Record, Idx);
3510 QualType T = GetType(Record[Idx++]);
3511 return TemplateArgument(Value, T);
3512 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003513 case TemplateArgument::Template:
3514 return TemplateArgument(ReadTemplateName(Record, Idx));
3515 case TemplateArgument::Expression:
Sebastian Redl577d4792010-07-22 22:43:28 +00003516 return TemplateArgument(ReadExpr(DeclsCursor));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003517 case TemplateArgument::Pack: {
3518 unsigned NumArgs = Record[Idx++];
3519 llvm::SmallVector<TemplateArgument, 8> Args;
3520 Args.reserve(NumArgs);
3521 while (NumArgs--)
Sebastian Redl577d4792010-07-22 22:43:28 +00003522 Args.push_back(ReadTemplateArgument(DeclsCursor, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003523 TemplateArgument TemplArg;
3524 TemplArg.setArgumentPack(Args.data(), Args.size(), /*CopyArgs=*/true);
3525 return TemplArg;
3526 }
3527 }
3528
3529 assert(0 && "Unhandled template argument kind!");
3530 return TemplateArgument();
3531}
3532
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003533TemplateParameterList *
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003534ASTReader::ReadTemplateParameterList(const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003535 SourceLocation TemplateLoc = ReadSourceLocation(Record, Idx);
3536 SourceLocation LAngleLoc = ReadSourceLocation(Record, Idx);
3537 SourceLocation RAngleLoc = ReadSourceLocation(Record, Idx);
3538
3539 unsigned NumParams = Record[Idx++];
3540 llvm::SmallVector<NamedDecl *, 16> Params;
3541 Params.reserve(NumParams);
3542 while (NumParams--)
3543 Params.push_back(cast<NamedDecl>(GetDecl(Record[Idx++])));
3544
3545 TemplateParameterList* TemplateParams =
3546 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
3547 Params.data(), Params.size(), RAngleLoc);
3548 return TemplateParams;
3549}
3550
3551void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003552ASTReader::
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003553ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs,
Sebastian Redl577d4792010-07-22 22:43:28 +00003554 llvm::BitstreamCursor &DeclsCursor,
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003555 const RecordData &Record, unsigned &Idx) {
3556 unsigned NumTemplateArgs = Record[Idx++];
3557 TemplArgs.reserve(NumTemplateArgs);
3558 while (NumTemplateArgs--)
Sebastian Redl577d4792010-07-22 22:43:28 +00003559 TemplArgs.push_back(ReadTemplateArgument(DeclsCursor, Record, Idx));
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003560}
3561
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00003562/// \brief Read a UnresolvedSet structure.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003563void ASTReader::ReadUnresolvedSet(UnresolvedSetImpl &Set,
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00003564 const RecordData &Record, unsigned &Idx) {
3565 unsigned NumDecls = Record[Idx++];
3566 while (NumDecls--) {
3567 NamedDecl *D = cast<NamedDecl>(GetDecl(Record[Idx++]));
3568 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
3569 Set.addDecl(D, AS);
3570 }
3571}
3572
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003573CXXBaseSpecifier
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003574ASTReader::ReadCXXBaseSpecifier(llvm::BitstreamCursor &DeclsCursor,
Nick Lewycky56062202010-07-26 16:56:01 +00003575 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003576 bool isVirtual = static_cast<bool>(Record[Idx++]);
3577 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
3578 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Nick Lewycky56062202010-07-26 16:56:01 +00003579 TypeSourceInfo *TInfo = GetTypeSourceInfo(DeclsCursor, Record, Idx);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003580 SourceRange Range = ReadSourceRange(Record, Idx);
Nick Lewycky56062202010-07-26 16:56:01 +00003581 return CXXBaseSpecifier(Range, isVirtual, isBaseOfClass, AS, TInfo);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003582}
3583
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003584std::pair<CXXBaseOrMemberInitializer **, unsigned>
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003585ASTReader::ReadCXXBaseOrMemberInitializers(llvm::BitstreamCursor &Cursor,
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003586 const RecordData &Record,
3587 unsigned &Idx) {
3588 CXXBaseOrMemberInitializer **BaseOrMemberInitializers = 0;
3589 unsigned NumInitializers = Record[Idx++];
3590 if (NumInitializers) {
3591 ASTContext &C = *getContext();
3592
3593 BaseOrMemberInitializers
3594 = new (C) CXXBaseOrMemberInitializer*[NumInitializers];
3595 for (unsigned i=0; i != NumInitializers; ++i) {
3596 TypeSourceInfo *BaseClassInfo = 0;
3597 bool IsBaseVirtual = false;
3598 FieldDecl *Member = 0;
3599
3600 bool IsBaseInitializer = Record[Idx++];
3601 if (IsBaseInitializer) {
3602 BaseClassInfo = GetTypeSourceInfo(Cursor, Record, Idx);
3603 IsBaseVirtual = Record[Idx++];
3604 } else {
3605 Member = cast<FieldDecl>(GetDecl(Record[Idx++]));
3606 }
3607 SourceLocation MemberLoc = ReadSourceLocation(Record, Idx);
3608 Expr *Init = ReadExpr(Cursor);
3609 FieldDecl *AnonUnionMember
3610 = cast_or_null<FieldDecl>(GetDecl(Record[Idx++]));
3611 SourceLocation LParenLoc = ReadSourceLocation(Record, Idx);
3612 SourceLocation RParenLoc = ReadSourceLocation(Record, Idx);
3613 bool IsWritten = Record[Idx++];
3614 unsigned SourceOrderOrNumArrayIndices;
3615 llvm::SmallVector<VarDecl *, 8> Indices;
3616 if (IsWritten) {
3617 SourceOrderOrNumArrayIndices = Record[Idx++];
3618 } else {
3619 SourceOrderOrNumArrayIndices = Record[Idx++];
3620 Indices.reserve(SourceOrderOrNumArrayIndices);
3621 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
3622 Indices.push_back(cast<VarDecl>(GetDecl(Record[Idx++])));
3623 }
3624
3625 CXXBaseOrMemberInitializer *BOMInit;
3626 if (IsBaseInitializer) {
3627 BOMInit = new (C) CXXBaseOrMemberInitializer(C, BaseClassInfo,
3628 IsBaseVirtual, LParenLoc,
3629 Init, RParenLoc);
3630 } else if (IsWritten) {
3631 BOMInit = new (C) CXXBaseOrMemberInitializer(C, Member, MemberLoc,
3632 LParenLoc, Init, RParenLoc);
3633 } else {
3634 BOMInit = CXXBaseOrMemberInitializer::Create(C, Member, MemberLoc,
3635 LParenLoc, Init, RParenLoc,
3636 Indices.data(),
3637 Indices.size());
3638 }
3639
3640 BOMInit->setAnonUnionMember(AnonUnionMember);
3641 BaseOrMemberInitializers[i] = BOMInit;
3642 }
3643 }
3644
3645 return std::make_pair(BaseOrMemberInitializers, NumInitializers);
3646}
3647
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003648NestedNameSpecifier *
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003649ASTReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003650 unsigned N = Record[Idx++];
3651 NestedNameSpecifier *NNS = 0, *Prev = 0;
3652 for (unsigned I = 0; I != N; ++I) {
3653 NestedNameSpecifier::SpecifierKind Kind
3654 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
3655 switch (Kind) {
3656 case NestedNameSpecifier::Identifier: {
3657 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
3658 NNS = NestedNameSpecifier::Create(*Context, Prev, II);
3659 break;
3660 }
3661
3662 case NestedNameSpecifier::Namespace: {
3663 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
3664 NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
3665 break;
3666 }
3667
3668 case NestedNameSpecifier::TypeSpec:
3669 case NestedNameSpecifier::TypeSpecWithTemplate: {
3670 Type *T = GetType(Record[Idx++]).getTypePtr();
3671 bool Template = Record[Idx++];
3672 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
3673 break;
3674 }
3675
3676 case NestedNameSpecifier::Global: {
3677 NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
3678 // No associated value, and there can't be a prefix.
3679 break;
3680 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003681 }
Argyrios Kyrtzidisd2bb2c02010-07-07 15:46:30 +00003682 Prev = NNS;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003683 }
3684 return NNS;
3685}
3686
3687SourceRange
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003688ASTReader::ReadSourceRange(const RecordData &Record, unsigned &Idx) {
Daniel Dunbar8ee59392010-06-02 15:47:10 +00003689 SourceLocation beg = SourceLocation::getFromRawEncoding(Record[Idx++]);
3690 SourceLocation end = SourceLocation::getFromRawEncoding(Record[Idx++]);
3691 return SourceRange(beg, end);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003692}
3693
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003694/// \brief Read an integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003695llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003696 unsigned BitWidth = Record[Idx++];
3697 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
3698 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
3699 Idx += NumWords;
3700 return Result;
3701}
3702
3703/// \brief Read a signed integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003704llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003705 bool isUnsigned = Record[Idx++];
3706 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
3707}
3708
Douglas Gregor17fc2232009-04-14 21:55:33 +00003709/// \brief Read a floating-point value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003710llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00003711 return llvm::APFloat(ReadAPInt(Record, Idx));
3712}
3713
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003714// \brief Read a string
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003715std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003716 unsigned Len = Record[Idx++];
Jay Foadbeaaccd2009-05-21 09:52:38 +00003717 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003718 Idx += Len;
3719 return Result;
3720}
3721
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003722CXXTemporary *ASTReader::ReadCXXTemporary(const RecordData &Record,
Chris Lattnerd2598362010-05-10 00:25:06 +00003723 unsigned &Idx) {
3724 CXXDestructorDecl *Decl = cast<CXXDestructorDecl>(GetDecl(Record[Idx++]));
3725 return CXXTemporary::Create(*Context, Decl);
3726}
3727
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003728DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00003729 return Diag(SourceLocation(), DiagID);
3730}
3731
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003732DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003733 return Diags.Report(FullSourceLoc(Loc, SourceMgr), DiagID);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003734}
Douglas Gregor025452f2009-04-17 00:04:06 +00003735
Douglas Gregor668c1a42009-04-21 22:25:48 +00003736/// \brief Retrieve the identifier table associated with the
3737/// preprocessor.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003738IdentifierTable &ASTReader::getIdentifierTable() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003739 assert(PP && "Forgot to set Preprocessor ?");
3740 return PP->getIdentifierTable();
Douglas Gregor668c1a42009-04-21 22:25:48 +00003741}
3742
Douglas Gregor025452f2009-04-17 00:04:06 +00003743/// \brief Record that the given ID maps to the given switch-case
3744/// statement.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003745void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Douglas Gregor025452f2009-04-17 00:04:06 +00003746 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
3747 SwitchCaseStmts[ID] = SC;
3748}
3749
3750/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003751SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Douglas Gregor025452f2009-04-17 00:04:06 +00003752 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
3753 return SwitchCaseStmts[ID];
3754}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00003755
3756/// \brief Record that the given label statement has been
3757/// deserialized and has the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003758void ASTReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
Mike Stump1eb44332009-09-09 15:08:12 +00003759 assert(LabelStmts.find(ID) == LabelStmts.end() &&
Douglas Gregor1de05fe2009-04-17 18:18:49 +00003760 "Deserialized label twice");
3761 LabelStmts[ID] = S;
3762
3763 // If we've already seen any goto statements that point to this
3764 // label, resolve them now.
3765 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
3766 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
3767 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
3768 Goto->second->setLabel(S);
3769 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00003770
3771 // If we've already seen any address-label statements that point to
3772 // this label, resolve them now.
3773 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
Mike Stump1eb44332009-09-09 15:08:12 +00003774 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00003775 = UnresolvedAddrLabelExprs.equal_range(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00003776 for (AddrLabelIter AddrLabel = AddrLabels.first;
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00003777 AddrLabel != AddrLabels.second; ++AddrLabel)
3778 AddrLabel->second->setLabel(S);
3779 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor1de05fe2009-04-17 18:18:49 +00003780}
3781
3782/// \brief Set the label of the given statement to the label
3783/// identified by ID.
3784///
3785/// Depending on the order in which the label and other statements
3786/// referencing that label occur, this operation may complete
3787/// immediately (updating the statement) or it may queue the
3788/// statement to be back-patched later.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003789void ASTReader::SetLabelOf(GotoStmt *S, unsigned ID) {
Douglas Gregor1de05fe2009-04-17 18:18:49 +00003790 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3791 if (Label != LabelStmts.end()) {
3792 // We've already seen this label, so set the label of the goto and
3793 // we're done.
3794 S->setLabel(Label->second);
3795 } else {
3796 // We haven't seen this label yet, so add this goto to the set of
3797 // unresolved goto statements.
3798 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
3799 }
3800}
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00003801
3802/// \brief Set the label of the given expression to the label
3803/// identified by ID.
3804///
3805/// Depending on the order in which the label and other statements
3806/// referencing that label occur, this operation may complete
3807/// immediately (updating the statement) or it may queue the
3808/// statement to be back-patched later.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003809void ASTReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00003810 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3811 if (Label != LabelStmts.end()) {
3812 // We've already seen this label, so set the label of the
3813 // label-address expression and we're done.
3814 S->setLabel(Label->second);
3815 } else {
3816 // We haven't seen this label yet, so add this label-address
3817 // expression to the set of unresolved label-address expressions.
3818 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
3819 }
3820}
Douglas Gregord89275b2009-07-06 18:54:52 +00003821
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003822void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00003823 assert(NumCurrentElementsDeserializing &&
3824 "FinishedDeserializing not paired with StartedDeserializing");
3825 if (NumCurrentElementsDeserializing == 1) {
Douglas Gregord89275b2009-07-06 18:54:52 +00003826 // If any identifiers with corresponding top-level declarations have
3827 // been loaded, load those declarations now.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00003828 while (!PendingIdentifierInfos.empty()) {
3829 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
3830 PendingIdentifierInfos.front().DeclIDs, true);
3831 PendingIdentifierInfos.pop_front();
Douglas Gregord89275b2009-07-06 18:54:52 +00003832 }
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003833
3834 // We are not in recursive loading, so it's safe to pass the "interesting"
3835 // decls to the consumer.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00003836 if (Consumer)
3837 PassInterestingDeclsToConsumer();
Douglas Gregord89275b2009-07-06 18:54:52 +00003838 }
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00003839 --NumCurrentElementsDeserializing;
Douglas Gregord89275b2009-07-06 18:54:52 +00003840}