blob: 8448f424f9d3e93af7907527f5f6c03a0fab3a71 [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;
44
45//===----------------------------------------------------------------------===//
Sebastian Redl3c7f4132010-08-18 23:57:06 +000046// PCH validator implementation
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000047//===----------------------------------------------------------------------===//
48
Sebastian Redl571db7f2010-08-18 23:56:56 +000049ASTReaderListener::~ASTReaderListener() {}
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000050
51bool
52PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts) {
53 const LangOptions &PPLangOpts = PP.getLangOptions();
54#define PARSE_LANGOPT_BENIGN(Option)
55#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
56 if (PPLangOpts.Option != LangOpts.Option) { \
57 Reader.Diag(DiagID) << LangOpts.Option << PPLangOpts.Option; \
58 return true; \
59 }
60
61 PARSE_LANGOPT_BENIGN(Trigraphs);
62 PARSE_LANGOPT_BENIGN(BCPLComment);
63 PARSE_LANGOPT_BENIGN(DollarIdents);
64 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
65 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
Chandler Carrutheb5d7b72010-04-17 20:17:31 +000066 PARSE_LANGOPT_IMPORTANT(GNUKeywords, diag::warn_pch_gnu_keywords);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000067 PARSE_LANGOPT_BENIGN(ImplicitInt);
68 PARSE_LANGOPT_BENIGN(Digraphs);
69 PARSE_LANGOPT_BENIGN(HexFloats);
70 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
71 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
72 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
73 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
74 PARSE_LANGOPT_BENIGN(CXXOperatorName);
75 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
76 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
77 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
Fariborz Jahanian412e7982010-02-09 19:31:38 +000078 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI2, diag::warn_pch_nonfragile_abi2);
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +000079 PARSE_LANGOPT_IMPORTANT(NoConstantCFStrings,
80 diag::warn_pch_no_constant_cfstrings);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000081 PARSE_LANGOPT_BENIGN(PascalStrings);
82 PARSE_LANGOPT_BENIGN(WritableStrings);
Mike Stump1eb44332009-09-09 15:08:12 +000083 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000084 diag::warn_pch_lax_vector_conversions);
Nate Begeman69cfb9b2009-06-25 22:57:40 +000085 PARSE_LANGOPT_IMPORTANT(AltiVec, diag::warn_pch_altivec);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000086 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
Daniel Dunbar73482882010-02-10 18:48:44 +000087 PARSE_LANGOPT_IMPORTANT(SjLjExceptions, diag::warn_pch_sjlj_exceptions);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000088 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
89 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
90 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
Mike Stump1eb44332009-09-09 15:08:12 +000091 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000092 diag::warn_pch_thread_safe_statics);
Daniel Dunbar5345c392009-09-03 04:54:28 +000093 PARSE_LANGOPT_IMPORTANT(POSIXThreads, diag::warn_pch_posix_threads);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000094 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
95 PARSE_LANGOPT_BENIGN(EmitAllDecls);
96 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
Chris Lattnera4d71452010-06-26 21:25:03 +000097 PARSE_LANGOPT_BENIGN(getSignedOverflowBehavior());
Mike Stump1eb44332009-09-09 15:08:12 +000098 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000099 diag::warn_pch_heinous_extensions);
100 // FIXME: Most of the options below are benign if the macro wasn't
101 // used. Unfortunately, this means that a PCH compiled without
102 // optimization can't be used with optimization turned on, even
103 // though the only thing that changes is whether __OPTIMIZE__ was
104 // defined... but if __OPTIMIZE__ never showed up in the header, it
105 // doesn't matter. We could consider making this some special kind
106 // of check.
107 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
108 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
109 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
110 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
111 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
112 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
113 PARSE_LANGOPT_IMPORTANT(AccessControl, diag::warn_pch_access_control);
114 PARSE_LANGOPT_IMPORTANT(CharIsSigned, diag::warn_pch_char_signed);
John Thompsona6fda122009-11-05 20:14:16 +0000115 PARSE_LANGOPT_IMPORTANT(ShortWChar, diag::warn_pch_short_wchar);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000116 if ((PPLangOpts.getGCMode() != 0) != (LangOpts.getGCMode() != 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000117 Reader.Diag(diag::warn_pch_gc_mode)
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000118 << LangOpts.getGCMode() << PPLangOpts.getGCMode();
119 return true;
120 }
121 PARSE_LANGOPT_BENIGN(getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000122 PARSE_LANGOPT_IMPORTANT(getStackProtectorMode(),
123 diag::warn_pch_stack_protector);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000124 PARSE_LANGOPT_BENIGN(InstantiationDepth);
Nate Begeman69cfb9b2009-06-25 22:57:40 +0000125 PARSE_LANGOPT_IMPORTANT(OpenCL, diag::warn_pch_opencl);
Mike Stump9c276ae2009-12-12 01:27:46 +0000126 PARSE_LANGOPT_BENIGN(CatchUndefined);
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000127 PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors);
Douglas Gregora0068fc2010-07-09 17:35:33 +0000128 PARSE_LANGOPT_BENIGN(SpellChecking);
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +0000129#undef PARSE_LANGOPT_IMPORTANT
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000130#undef PARSE_LANGOPT_BENIGN
131
132 return false;
133}
134
Daniel Dunbardc3c0d22009-11-11 00:52:11 +0000135bool PCHValidator::ReadTargetTriple(llvm::StringRef Triple) {
136 if (Triple == PP.getTargetInfo().getTriple().str())
137 return false;
138
139 Reader.Diag(diag::warn_pch_target_triple)
140 << Triple << PP.getTargetInfo().getTriple().str();
141 return true;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000142}
143
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000144struct EmptyStringRef {
Benjamin Kramerec1b1cc2010-07-14 23:19:41 +0000145 bool operator ()(llvm::StringRef r) const { return r.empty(); }
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000146};
147struct EmptyBlock {
148 bool operator ()(const PCHPredefinesBlock &r) const { return r.Data.empty(); }
149};
150
151static bool EqualConcatenations(llvm::SmallVector<llvm::StringRef, 2> L,
152 PCHPredefinesBlocks R) {
153 // First, sum up the lengths.
154 unsigned LL = 0, RL = 0;
155 for (unsigned I = 0, N = L.size(); I != N; ++I) {
156 LL += L[I].size();
157 }
158 for (unsigned I = 0, N = R.size(); I != N; ++I) {
159 RL += R[I].Data.size();
160 }
161 if (LL != RL)
162 return false;
163 if (LL == 0 && RL == 0)
164 return true;
165
166 // Kick out empty parts, they confuse the algorithm below.
167 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
168 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
169
170 // Do it the hard way. At this point, both vectors must be non-empty.
171 llvm::StringRef LR = L[0], RR = R[0].Data;
172 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
Daniel Dunbarc76c9e02010-07-16 00:00:11 +0000173 (void) RN;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000174 for (;;) {
175 // Compare the current pieces.
176 if (LR.size() == RR.size()) {
177 // If they're the same length, it's pretty easy.
178 if (LR != RR)
179 return false;
180 // Both pieces are done, advance.
181 ++LI;
182 ++RI;
183 // If either string is done, they're both done, since they're the same
184 // length.
185 if (LI == LN) {
186 assert(RI == RN && "Strings not the same length after all?");
187 return true;
188 }
189 LR = L[LI];
190 RR = R[RI].Data;
191 } else if (LR.size() < RR.size()) {
192 // Right piece is longer.
193 if (!RR.startswith(LR))
194 return false;
195 ++LI;
196 assert(LI != LN && "Strings not the same length after all?");
197 RR = RR.substr(LR.size());
198 LR = L[LI];
199 } else {
200 // Left piece is longer.
201 if (!LR.startswith(RR))
202 return false;
203 ++RI;
204 assert(RI != RN && "Strings not the same length after all?");
205 LR = LR.substr(RR.size());
206 RR = R[RI].Data;
207 }
208 }
209}
210
211static std::pair<FileID, llvm::StringRef::size_type>
212FindMacro(const PCHPredefinesBlocks &Buffers, llvm::StringRef MacroDef) {
213 std::pair<FileID, llvm::StringRef::size_type> Res;
214 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
215 Res.second = Buffers[I].Data.find(MacroDef);
216 if (Res.second != llvm::StringRef::npos) {
217 Res.first = Buffers[I].BufferID;
218 break;
219 }
220 }
221 return Res;
222}
223
224bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000225 llvm::StringRef OriginalFileName,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000226 std::string &SuggestedPredefines) {
Daniel Dunbarc7162932009-11-11 23:58:53 +0000227 // We are in the context of an implicit include, so the predefines buffer will
228 // have a #include entry for the PCH file itself (as normalized by the
229 // preprocessor initialization). Find it and skip over it in the checking
230 // below.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000231 llvm::SmallString<256> PCHInclude;
232 PCHInclude += "#include \"";
Daniel Dunbarc7162932009-11-11 23:58:53 +0000233 PCHInclude += NormalizeDashIncludePath(OriginalFileName);
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000234 PCHInclude += "\"\n";
235 std::pair<llvm::StringRef,llvm::StringRef> Split =
236 llvm::StringRef(PP.getPredefines()).split(PCHInclude.str());
237 llvm::StringRef Left = Split.first, Right = Split.second;
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000238 if (Left == PP.getPredefines()) {
239 Error("Missing PCH include entry!");
240 return true;
241 }
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000242
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000243 // If the concatenation of all the PCH buffers is equal to the adjusted
244 // command line, we're done.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000245 llvm::SmallVector<llvm::StringRef, 2> CommandLine;
246 CommandLine.push_back(Left);
247 CommandLine.push_back(Right);
248 if (EqualConcatenations(CommandLine, Buffers))
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000249 return false;
250
251 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000252
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000253 // The predefines buffers are different. Determine what the differences are,
254 // and whether they require us to reject the PCH file.
Daniel Dunbare6750492009-11-13 16:46:11 +0000255 llvm::SmallVector<llvm::StringRef, 8> PCHLines;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000256 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
257 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Daniel Dunbare6750492009-11-13 16:46:11 +0000258
259 llvm::SmallVector<llvm::StringRef, 8> CmdLineLines;
260 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
261 Right.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000262
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000263 // Sort both sets of predefined buffer lines, since we allow some extra
264 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000265 std::sort(CmdLineLines.begin(), CmdLineLines.end());
266 std::sort(PCHLines.begin(), PCHLines.end());
267
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000268 // Determine which predefines that were used to build the PCH file are missing
269 // from the command line.
270 std::vector<llvm::StringRef> MissingPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000271 std::set_difference(PCHLines.begin(), PCHLines.end(),
272 CmdLineLines.begin(), CmdLineLines.end(),
273 std::back_inserter(MissingPredefines));
274
275 bool MissingDefines = false;
276 bool ConflictingDefines = false;
277 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000278 llvm::StringRef Missing = MissingPredefines[I];
279 if (!Missing.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000280 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
281 return true;
282 }
Mike Stump1eb44332009-09-09 15:08:12 +0000283
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000284 // This is a macro definition. Determine the name of the macro we're
285 // defining.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000286 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000287 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000288 = Missing.find_first_of("( \n\r", StartOfMacroName);
289 assert(EndOfMacroName != std::string::npos &&
290 "Couldn't find the end of the macro name");
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000291 llvm::StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000292
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000293 // Determine whether this macro was given a different definition on the
294 // command line.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000295 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000296 std::string::size_type MacroDefLen = MacroDefStart.size();
Daniel Dunbare6750492009-11-13 16:46:11 +0000297 llvm::SmallVector<llvm::StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000298 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
299 MacroDefStart);
300 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000301 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000302 // Different macro; we're done.
303 ConflictPos = CmdLineLines.end();
Mike Stump1eb44332009-09-09 15:08:12 +0000304 break;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000305 }
Mike Stump1eb44332009-09-09 15:08:12 +0000306
307 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000308 "Invalid #define in predefines buffer?");
Mike Stump1eb44332009-09-09 15:08:12 +0000309 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000310 (*ConflictPos)[MacroDefLen] != '(')
311 continue; // Longer macro name; keep trying.
Mike Stump1eb44332009-09-09 15:08:12 +0000312
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000313 // We found a conflicting macro definition.
314 break;
315 }
Mike Stump1eb44332009-09-09 15:08:12 +0000316
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000317 if (ConflictPos != CmdLineLines.end()) {
318 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
319 << MacroName;
320
321 // Show the definition of this macro within the PCH file.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000322 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
323 FindMacro(Buffers, Missing);
324 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
325 SourceLocation PCHMissingLoc =
326 SourceMgr.getLocForStartOfFile(MacroLoc.first)
327 .getFileLocWithOffset(MacroLoc.second);
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000328 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000329
330 ConflictingDefines = true;
331 continue;
332 }
Mike Stump1eb44332009-09-09 15:08:12 +0000333
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000334 // If the macro doesn't conflict, then we'll just pick up the macro
335 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000336 if (ConflictingDefines)
337 continue; // Don't complain if there are already conflicting defs
Mike Stump1eb44332009-09-09 15:08:12 +0000338
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000339 if (!MissingDefines) {
340 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
341 MissingDefines = true;
342 }
343
344 // Show the definition of this macro within the PCH file.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000345 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
346 FindMacro(Buffers, Missing);
347 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
348 SourceLocation PCHMissingLoc =
349 SourceMgr.getLocForStartOfFile(MacroLoc.first)
350 .getFileLocWithOffset(MacroLoc.second);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000351 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
352 }
Mike Stump1eb44332009-09-09 15:08:12 +0000353
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000354 if (ConflictingDefines)
355 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000357 // Determine what predefines were introduced based on command-line
358 // parameters that were not present when building the PCH
359 // file. Extra #defines are okay, so long as the identifiers being
360 // defined were not used within the precompiled header.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000361 std::vector<llvm::StringRef> ExtraPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000362 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
363 PCHLines.begin(), PCHLines.end(),
Mike Stump1eb44332009-09-09 15:08:12 +0000364 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000365 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000366 llvm::StringRef &Extra = ExtraPredefines[I];
367 if (!Extra.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000368 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
369 return true;
370 }
371
372 // This is an extra macro definition. Determine the name of the
373 // macro we're defining.
374 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000375 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000376 = Extra.find_first_of("( \n\r", StartOfMacroName);
377 assert(EndOfMacroName != std::string::npos &&
378 "Couldn't find the end of the macro name");
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000379 llvm::StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000380
381 // Check whether this name was used somewhere in the PCH file. If
382 // so, defining it as a macro could change behavior, so we reject
383 // the PCH file.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000384 if (IdentifierInfo *II = Reader.get(MacroName)) {
Daniel Dunbar4fda42e2009-11-11 00:52:00 +0000385 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000386 return true;
387 }
388
389 // Add this definition to the suggested predefines buffer.
390 SuggestedPredefines += Extra;
391 SuggestedPredefines += '\n';
392 }
393
394 // If we get here, it's because the predefines buffer had compatible
395 // contents. Accept the PCH file.
396 return false;
397}
398
Douglas Gregor12fab312010-03-16 16:35:32 +0000399void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
400 unsigned ID) {
401 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
402 ++NumHeaderInfos;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000403}
404
405void PCHValidator::ReadCounter(unsigned Value) {
406 PP.setCounterValue(Value);
407}
408
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000409//===----------------------------------------------------------------------===//
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000410// AST reader implementation
Douglas Gregor668c1a42009-04-21 22:25:48 +0000411//===----------------------------------------------------------------------===//
412
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000413ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
Douglas Gregorfae3b2f2010-07-27 00:27:13 +0000414 const char *isysroot, bool DisableValidation)
Sebastian Redl30c514c2010-07-14 23:45:08 +0000415 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
416 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
417 Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
Sebastian Redl725cd962010-08-04 20:40:17 +0000418 Consumer(0), isysroot(isysroot), DisableValidation(DisableValidation),
419 NumStatHits(0), NumStatMisses(0), NumSLocEntriesRead(0),
420 TotalNumSLocEntries(0), NumStatementsRead(0), TotalNumStatements(0),
Sebastian Redlfa78dec2010-08-04 21:22:45 +0000421 NumMacrosRead(0), TotalNumMacros(0), NumSelectorsRead(0),
422 NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
423 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
Sebastian Redl725cd962010-08-04 20:40:17 +0000424 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
425 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000426 RelocatablePCH = false;
427}
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000428
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000429ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
Douglas Gregorfae3b2f2010-07-27 00:27:13 +0000430 Diagnostic &Diags, const char *isysroot,
431 bool DisableValidation)
Sebastian Redl30c514c2010-07-14 23:45:08 +0000432 : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
Sebastian Redl9137a522010-07-16 17:50:48 +0000433 Diags(Diags), SemaObj(0), PP(0), Context(0), Consumer(0),
Sebastian Redl725cd962010-08-04 20:40:17 +0000434 isysroot(isysroot), DisableValidation(DisableValidation), NumStatHits(0),
435 NumStatMisses(0), NumSLocEntriesRead(0), TotalNumSLocEntries(0),
436 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
Sebastian Redlfa78dec2010-08-04 21:22:45 +0000437 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
438 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
Sebastian Redl725cd962010-08-04 20:40:17 +0000439 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
440 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
441 NumCurrentElementsDeserializing(0) {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000442 RelocatablePCH = false;
443}
Chris Lattner4c6f9522009-04-27 05:14:47 +0000444
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000445ASTReader::~ASTReader() {
Sebastian Redl9137a522010-07-16 17:50:48 +0000446 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
447 delete Chain[e - i - 1];
448}
449
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000450ASTReader::PerFileData::PerFileData()
Sebastian Redl12d6da02010-07-19 22:06:55 +0000451 : StatCache(0), LocalNumSLocEntries(0), LocalNumTypes(0), TypeOffsets(0),
Sebastian Redl2da08f92010-07-19 22:28:42 +0000452 LocalNumDecls(0), DeclOffsets(0), LocalNumIdentifiers(0),
Sebastian Redl04e6fd42010-07-21 20:07:32 +0000453 IdentifierOffsets(0), IdentifierTableData(0), IdentifierLookupTable(0),
454 LocalNumMacroDefinitions(0), MacroDefinitionOffsets(0),
Sebastian Redl725cd962010-08-04 20:40:17 +0000455 NumPreallocatedPreprocessingEntities(0), SelectorLookupTable(0),
456 SelectorLookupTableData(0), SelectorOffsets(0), LocalNumSelectors(0)
Sebastian Redl9137a522010-07-16 17:50:48 +0000457{}
Chris Lattner4c6f9522009-04-27 05:14:47 +0000458
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000459void
Sebastian Redl571db7f2010-08-18 23:56:56 +0000460ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000461 DeserializationListener = Listener;
462 if (DeserializationListener)
463 DeserializationListener->SetReader(this);
464}
465
Chris Lattner4c6f9522009-04-27 05:14:47 +0000466
Douglas Gregor668c1a42009-04-21 22:25:48 +0000467namespace {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000468class ASTSelectorLookupTrait {
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000469 ASTReader &Reader;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000470
471public:
Sebastian Redl5d050072010-08-04 17:20:04 +0000472 struct data_type {
473 pch::SelectorID ID;
474 ObjCMethodList Instance, Factory;
475 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000476
477 typedef Selector external_key_type;
478 typedef external_key_type internal_key_type;
479
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000480 explicit ASTSelectorLookupTrait(ASTReader &Reader) : Reader(Reader) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000481
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000482 static bool EqualKey(const internal_key_type& a,
483 const internal_key_type& b) {
484 return a == b;
485 }
Mike Stump1eb44332009-09-09 15:08:12 +0000486
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000487 static unsigned ComputeHash(Selector Sel) {
488 unsigned N = Sel.getNumArgs();
489 if (N == 0)
490 ++N;
491 unsigned R = 5381;
492 for (unsigned I = 0; I != N; ++I)
493 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
Daniel Dunbar2596e422009-10-17 23:52:28 +0000494 R = llvm::HashString(II->getName(), R);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000495 return R;
496 }
Mike Stump1eb44332009-09-09 15:08:12 +0000497
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000498 // This hopefully will just get inlined and removed by the optimizer.
499 static const internal_key_type&
500 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump1eb44332009-09-09 15:08:12 +0000501
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000502 static std::pair<unsigned, unsigned>
503 ReadKeyDataLength(const unsigned char*& d) {
504 using namespace clang::io;
505 unsigned KeyLen = ReadUnalignedLE16(d);
506 unsigned DataLen = ReadUnalignedLE16(d);
507 return std::make_pair(KeyLen, DataLen);
508 }
Mike Stump1eb44332009-09-09 15:08:12 +0000509
Douglas Gregor83941df2009-04-25 17:48:32 +0000510 internal_key_type ReadKey(const unsigned char* d, unsigned) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000511 using namespace clang::io;
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000512 SelectorTable &SelTable = Reader.getContext()->Selectors;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000513 unsigned N = ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +0000514 IdentifierInfo *FirstII
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000515 = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
516 if (N == 0)
517 return SelTable.getNullarySelector(FirstII);
518 else if (N == 1)
519 return SelTable.getUnarySelector(FirstII);
520
521 llvm::SmallVector<IdentifierInfo *, 16> Args;
522 Args.push_back(FirstII);
523 for (unsigned I = 1; I != N; ++I)
524 Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)));
525
Douglas Gregor75fdb232009-05-22 22:45:36 +0000526 return SelTable.getSelector(N, Args.data());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000527 }
Mike Stump1eb44332009-09-09 15:08:12 +0000528
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000529 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
530 using namespace clang::io;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000531
532 data_type Result;
533
Sebastian Redl5d050072010-08-04 17:20:04 +0000534 Result.ID = ReadUnalignedLE32(d);
535 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
536 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
537
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000538 // Load instance methods
539 ObjCMethodList *Prev = 0;
540 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000541 ObjCMethodDecl *Method
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000542 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl5d050072010-08-04 17:20:04 +0000543 if (!Result.Instance.Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000544 // This is the first method, which is the easy case.
Sebastian Redl5d050072010-08-04 17:20:04 +0000545 Result.Instance.Method = Method;
546 Prev = &Result.Instance;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000547 continue;
548 }
549
Ted Kremenek298ed872010-02-11 00:53:01 +0000550 ObjCMethodList *Mem =
551 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
552 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000553 Prev = Prev->Next;
554 }
555
556 // Load factory methods
557 Prev = 0;
558 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000559 ObjCMethodDecl *Method
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000560 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl5d050072010-08-04 17:20:04 +0000561 if (!Result.Factory.Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000562 // This is the first method, which is the easy case.
Sebastian Redl5d050072010-08-04 17:20:04 +0000563 Result.Factory.Method = Method;
564 Prev = &Result.Factory;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000565 continue;
566 }
567
Ted Kremenek298ed872010-02-11 00:53:01 +0000568 ObjCMethodList *Mem =
569 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
570 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000571 Prev = Prev->Next;
572 }
573
574 return Result;
575 }
576};
Mike Stump1eb44332009-09-09 15:08:12 +0000577
578} // end anonymous namespace
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000579
580/// \brief The on-disk hash table used for the global method pool.
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000581typedef OnDiskChainedHashTable<ASTSelectorLookupTrait>
582 ASTSelectorLookupTable;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000583
584namespace {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000585class ASTIdentifierLookupTrait {
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000586 ASTReader &Reader;
Sebastian Redld27d3fc2010-07-21 22:31:37 +0000587 llvm::BitstreamCursor &Stream;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000588
589 // If we know the IdentifierInfo in advance, it is here and we will
590 // not build a new one. Used when deserializing information about an
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000591 // identifier that was constructed before the AST file was read.
Douglas Gregor668c1a42009-04-21 22:25:48 +0000592 IdentifierInfo *KnownII;
593
594public:
595 typedef IdentifierInfo * data_type;
596
597 typedef const std::pair<const char*, unsigned> external_key_type;
598
599 typedef external_key_type internal_key_type;
600
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000601 ASTIdentifierLookupTrait(ASTReader &Reader, llvm::BitstreamCursor &Stream,
Sebastian Redld27d3fc2010-07-21 22:31:37 +0000602 IdentifierInfo *II = 0)
603 : Reader(Reader), Stream(Stream), KnownII(II) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000604
Douglas Gregor668c1a42009-04-21 22:25:48 +0000605 static bool EqualKey(const internal_key_type& a,
606 const internal_key_type& b) {
607 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
608 : false;
609 }
Mike Stump1eb44332009-09-09 15:08:12 +0000610
Douglas Gregor668c1a42009-04-21 22:25:48 +0000611 static unsigned ComputeHash(const internal_key_type& a) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000612 return llvm::HashString(llvm::StringRef(a.first, a.second));
Douglas Gregor668c1a42009-04-21 22:25:48 +0000613 }
Mike Stump1eb44332009-09-09 15:08:12 +0000614
Douglas Gregor668c1a42009-04-21 22:25:48 +0000615 // This hopefully will just get inlined and removed by the optimizer.
616 static const internal_key_type&
617 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump1eb44332009-09-09 15:08:12 +0000618
Douglas Gregor668c1a42009-04-21 22:25:48 +0000619 static std::pair<unsigned, unsigned>
620 ReadKeyDataLength(const unsigned char*& d) {
621 using namespace clang::io;
Douglas Gregor5f8e3302009-04-25 20:26:24 +0000622 unsigned DataLen = ReadUnalignedLE16(d);
Douglas Gregord6595a42009-04-25 21:04:17 +0000623 unsigned KeyLen = ReadUnalignedLE16(d);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000624 return std::make_pair(KeyLen, DataLen);
625 }
Mike Stump1eb44332009-09-09 15:08:12 +0000626
Douglas Gregor668c1a42009-04-21 22:25:48 +0000627 static std::pair<const char*, unsigned>
628 ReadKey(const unsigned char* d, unsigned n) {
629 assert(n >= 2 && d[n-1] == '\0');
630 return std::make_pair((const char*) d, n-1);
631 }
Mike Stump1eb44332009-09-09 15:08:12 +0000632
633 IdentifierInfo *ReadData(const internal_key_type& k,
Douglas Gregor668c1a42009-04-21 22:25:48 +0000634 const unsigned char* d,
635 unsigned DataLen) {
636 using namespace clang::io;
Douglas Gregora92193e2009-04-28 21:18:29 +0000637 pch::IdentID ID = ReadUnalignedLE32(d);
638 bool IsInteresting = ID & 0x01;
639
640 // Wipe out the "is interesting" bit.
641 ID = ID >> 1;
642
643 if (!IsInteresting) {
Sebastian Redl083abdf2010-07-27 23:01:28 +0000644 // For uninteresting identifiers, just build the IdentifierInfo
Douglas Gregora92193e2009-04-28 21:18:29 +0000645 // and associate it with the persistent ID.
646 IdentifierInfo *II = KnownII;
647 if (!II)
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000648 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregora92193e2009-04-28 21:18:29 +0000649 Reader.SetIdentifierInfo(ID, II);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000650 II->setIsFromAST();
Douglas Gregora92193e2009-04-28 21:18:29 +0000651 return II;
652 }
653
Douglas Gregor5998da52009-04-28 21:32:13 +0000654 unsigned Bits = ReadUnalignedLE16(d);
Douglas Gregor2deaea32009-04-22 18:49:13 +0000655 bool CPlusPlusOperatorKeyword = Bits & 0x01;
656 Bits >>= 1;
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000657 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
658 Bits >>= 1;
Douglas Gregor2deaea32009-04-22 18:49:13 +0000659 bool Poisoned = Bits & 0x01;
660 Bits >>= 1;
661 bool ExtensionToken = Bits & 0x01;
662 Bits >>= 1;
663 bool hasMacroDefinition = Bits & 0x01;
664 Bits >>= 1;
665 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
666 Bits >>= 10;
Mike Stump1eb44332009-09-09 15:08:12 +0000667
Douglas Gregor2deaea32009-04-22 18:49:13 +0000668 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregor5998da52009-04-28 21:32:13 +0000669 DataLen -= 6;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000670
671 // Build the IdentifierInfo itself and link the identifier ID with
672 // the new IdentifierInfo.
673 IdentifierInfo *II = KnownII;
674 if (!II)
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000675 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000676 Reader.SetIdentifierInfo(ID, II);
677
Douglas Gregor2deaea32009-04-22 18:49:13 +0000678 // Set or check the various bits in the IdentifierInfo structure.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000679 // Token IDs are read-only.
680 if (HasRevertedTokenIDToIdentifier)
681 II->RevertTokenIDToIdentifier();
Douglas Gregor2deaea32009-04-22 18:49:13 +0000682 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
Mike Stump1eb44332009-09-09 15:08:12 +0000683 assert(II->isExtensionToken() == ExtensionToken &&
Douglas Gregor2deaea32009-04-22 18:49:13 +0000684 "Incorrect extension token flag");
685 (void)ExtensionToken;
686 II->setIsPoisoned(Poisoned);
687 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
688 "Incorrect C++ operator keyword flag");
689 (void)CPlusPlusOperatorKeyword;
690
Douglas Gregor37e26842009-04-21 23:56:24 +0000691 // If this identifier is a macro, deserialize the macro
692 // definition.
693 if (hasMacroDefinition) {
Douglas Gregor5998da52009-04-28 21:32:13 +0000694 uint32_t Offset = ReadUnalignedLE32(d);
Sebastian Redld27d3fc2010-07-21 22:31:37 +0000695 Reader.ReadMacroRecord(Stream, Offset);
Douglas Gregor5998da52009-04-28 21:32:13 +0000696 DataLen -= 4;
Douglas Gregor37e26842009-04-21 23:56:24 +0000697 }
Douglas Gregor668c1a42009-04-21 22:25:48 +0000698
699 // Read all of the declarations visible at global scope with this
700 // name.
Chris Lattner6bf690f2009-04-27 22:17:41 +0000701 if (Reader.getContext() == 0) return II;
Douglas Gregord89275b2009-07-06 18:54:52 +0000702 if (DataLen > 0) {
703 llvm::SmallVector<uint32_t, 4> DeclIDs;
704 for (; DataLen > 0; DataLen -= 4)
705 DeclIDs.push_back(ReadUnalignedLE32(d));
706 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000707 }
Mike Stump1eb44332009-09-09 15:08:12 +0000708
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000709 II->setIsFromAST();
Douglas Gregor668c1a42009-04-21 22:25:48 +0000710 return II;
711 }
712};
Mike Stump1eb44332009-09-09 15:08:12 +0000713
714} // end anonymous namespace
Douglas Gregor668c1a42009-04-21 22:25:48 +0000715
716/// \brief The on-disk hash table used to contain information about
717/// all of the identifiers in the program.
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000718typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait>
719 ASTIdentifierLookupTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000720
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000721void ASTReader::Error(const char *Msg) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000722 Diag(diag::err_fe_pch_malformed) << Msg;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000723}
724
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000725/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000726bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000727 if (Listener)
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000728 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000729 ActualOriginalFileName,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000730 SuggestedPredefines);
Douglas Gregore721f952009-04-28 18:58:38 +0000731 return false;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000732}
733
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000734//===----------------------------------------------------------------------===//
735// Source Manager Deserialization
736//===----------------------------------------------------------------------===//
737
Douglas Gregorbd945002009-04-13 16:31:14 +0000738/// \brief Read the line table in the source manager block.
739/// \returns true if ther was an error.
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000740bool ASTReader::ParseLineTable(llvm::SmallVectorImpl<uint64_t> &Record) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000741 unsigned Idx = 0;
742 LineTableInfo &LineTable = SourceMgr.getLineTable();
743
744 // Parse the file names
Douglas Gregorff0a9872009-04-13 17:12:42 +0000745 std::map<int, int> FileIDs;
746 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000747 // Extract the file name
748 unsigned FilenameLen = Record[Idx++];
749 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
750 Idx += FilenameLen;
Douglas Gregore650c8c2009-07-07 00:12:59 +0000751 MaybeAddSystemRootToFilename(Filename);
Mike Stump1eb44332009-09-09 15:08:12 +0000752 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
Douglas Gregorff0a9872009-04-13 17:12:42 +0000753 Filename.size());
Douglas Gregorbd945002009-04-13 16:31:14 +0000754 }
755
756 // Parse the line entries
757 std::vector<LineEntry> Entries;
758 while (Idx < Record.size()) {
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000759 int FID = Record[Idx++];
Douglas Gregorbd945002009-04-13 16:31:14 +0000760
761 // Extract the line entries
762 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000763 assert(NumEntries && "Numentries is 00000");
Douglas Gregorbd945002009-04-13 16:31:14 +0000764 Entries.clear();
765 Entries.reserve(NumEntries);
766 for (unsigned I = 0; I != NumEntries; ++I) {
767 unsigned FileOffset = Record[Idx++];
768 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000769 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump1eb44332009-09-09 15:08:12 +0000770 SrcMgr::CharacteristicKind FileKind
Douglas Gregorbd945002009-04-13 16:31:14 +0000771 = (SrcMgr::CharacteristicKind)Record[Idx++];
772 unsigned IncludeOffset = Record[Idx++];
773 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
774 FileKind, IncludeOffset));
775 }
776 LineTable.AddEntry(FID, Entries);
777 }
778
779 return false;
780}
781
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000782namespace {
783
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000784class ASTStatData {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000785public:
786 const bool hasStat;
787 const ino_t ino;
788 const dev_t dev;
789 const mode_t mode;
790 const time_t mtime;
791 const off_t size;
Mike Stump1eb44332009-09-09 15:08:12 +0000792
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000793 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Mike Stump1eb44332009-09-09 15:08:12 +0000794 : hasStat(true), ino(i), dev(d), mode(mo), mtime(m), size(s) {}
795
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000796 ASTStatData()
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000797 : hasStat(false), ino(0), dev(0), mode(0), mtime(0), size(0) {}
798};
799
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000800class ASTStatLookupTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000801 public:
802 typedef const char *external_key_type;
803 typedef const char *internal_key_type;
804
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000805 typedef ASTStatData data_type;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000806
807 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000808 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000809 }
810
811 static internal_key_type GetInternalKey(const char *path) { return path; }
812
813 static bool EqualKey(internal_key_type a, internal_key_type b) {
814 return strcmp(a, b) == 0;
815 }
816
817 static std::pair<unsigned, unsigned>
818 ReadKeyDataLength(const unsigned char*& d) {
819 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
820 unsigned DataLen = (unsigned) *d++;
821 return std::make_pair(KeyLen + 1, DataLen);
822 }
823
824 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
825 return (const char *)d;
826 }
827
828 static data_type ReadData(const internal_key_type, const unsigned char *d,
829 unsigned /*DataLen*/) {
830 using namespace clang::io;
831
832 if (*d++ == 1)
833 return data_type();
834
835 ino_t ino = (ino_t) ReadUnalignedLE32(d);
836 dev_t dev = (dev_t) ReadUnalignedLE32(d);
837 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +0000838 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000839 off_t size = (off_t) ReadUnalignedLE64(d);
840 return data_type(ino, dev, mode, mtime, size);
841 }
842};
843
844/// \brief stat() cache for precompiled headers.
845///
846/// This cache is very similar to the stat cache used by pretokenized
847/// headers.
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000848class ASTStatCache : public StatSysCallCache {
849 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000850 CacheTy *Cache;
851
852 unsigned &NumStatHits, &NumStatMisses;
Mike Stump1eb44332009-09-09 15:08:12 +0000853public:
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000854 ASTStatCache(const unsigned char *Buckets,
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000855 const unsigned char *Base,
856 unsigned &NumStatHits,
Mike Stump1eb44332009-09-09 15:08:12 +0000857 unsigned &NumStatMisses)
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000858 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
859 Cache = CacheTy::Create(Buckets, Base);
860 }
861
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000862 ~ASTStatCache() { delete Cache; }
Mike Stump1eb44332009-09-09 15:08:12 +0000863
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000864 int stat(const char *path, struct stat *buf) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000865 // Do the lookup for the file's data in the AST file.
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000866 CacheTy::iterator I = Cache->find(path);
867
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000868 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000869 if (I == Cache->end()) {
870 ++NumStatMisses;
Douglas Gregor52e71082009-10-16 18:18:30 +0000871 return StatSysCallCache::stat(path, buf);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000872 }
Mike Stump1eb44332009-09-09 15:08:12 +0000873
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000874 ++NumStatHits;
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000875 ASTStatData Data = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000876
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000877 if (!Data.hasStat)
878 return 1;
879
880 buf->st_ino = Data.ino;
881 buf->st_dev = Data.dev;
882 buf->st_mtime = Data.mtime;
883 buf->st_mode = Data.mode;
884 buf->st_size = Data.size;
885 return 0;
886 }
887};
888} // end anonymous namespace
889
890
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000891/// \brief Read a source manager block
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000892ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(PerFileData &F) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000893 using namespace SrcMgr;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000894
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000895 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +0000896
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000897 // Set the source-location entry cursor to the current position in
898 // the stream. This cursor will be used to read the contents of the
899 // source manager block initially, and then lazily read
900 // source-location entries as needed.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000901 SLocEntryCursor = F.Stream;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000902
903 // The stream itself is going to skip over the source manager block.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000904 if (F.Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000905 Error("malformed block record in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000906 return Failure;
907 }
908
909 // Enter the source manager block.
910 if (SLocEntryCursor.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000911 Error("malformed source manager block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +0000912 return Failure;
913 }
Douglas Gregor14f79002009-04-10 03:52:48 +0000914
Douglas Gregor14f79002009-04-10 03:52:48 +0000915 RecordData Record;
916 while (true) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000917 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregor14f79002009-04-10 03:52:48 +0000918 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000919 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000920 Error("error at end of Source Manager block in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +0000921 return Failure;
922 }
Douglas Gregore1d918e2009-04-10 23:10:45 +0000923 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +0000924 }
Mike Stump1eb44332009-09-09 15:08:12 +0000925
Douglas Gregor14f79002009-04-10 03:52:48 +0000926 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
927 // No known subblocks, always skip them.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000928 SLocEntryCursor.ReadSubBlockID();
929 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000930 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +0000931 return Failure;
932 }
Douglas Gregor14f79002009-04-10 03:52:48 +0000933 continue;
934 }
Mike Stump1eb44332009-09-09 15:08:12 +0000935
Douglas Gregor14f79002009-04-10 03:52:48 +0000936 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000937 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregor14f79002009-04-10 03:52:48 +0000938 continue;
939 }
Mike Stump1eb44332009-09-09 15:08:12 +0000940
Douglas Gregor14f79002009-04-10 03:52:48 +0000941 // Read a record.
942 const char *BlobStart;
943 unsigned BlobLen;
944 Record.clear();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000945 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000946 default: // Default behavior: ignore.
947 break;
948
Chris Lattner2c78b872009-04-14 23:22:57 +0000949 case pch::SM_LINE_TABLE:
Sebastian Redl518d8cb2010-07-20 21:20:32 +0000950 if (ParseLineTable(Record))
Douglas Gregorbd945002009-04-13 16:31:14 +0000951 return Failure;
Chris Lattner2c78b872009-04-14 23:22:57 +0000952 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +0000953
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000954 case pch::SM_SLOC_FILE_ENTRY:
955 case pch::SM_SLOC_BUFFER_ENTRY:
956 case pch::SM_SLOC_INSTANTIATION_ENTRY:
957 // Once we hit one of the source location entries, we're done.
958 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +0000959 }
960 }
961}
962
Sebastian Redl190faf72010-07-20 21:50:20 +0000963/// \brief Get a cursor that's correctly positioned for reading the source
964/// location entry with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000965llvm::BitstreamCursor &ASTReader::SLocCursorForID(unsigned ID) {
Sebastian Redl190faf72010-07-20 21:50:20 +0000966 assert(ID != 0 && ID <= TotalNumSLocEntries &&
967 "SLocCursorForID should only be called for real IDs.");
968
969 ID -= 1;
970 PerFileData *F = 0;
971 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
972 F = Chain[N - I - 1];
973 if (ID < F->LocalNumSLocEntries)
974 break;
975 ID -= F->LocalNumSLocEntries;
976 }
977 assert(F && F->LocalNumSLocEntries > ID && "Chain corrupted");
978
979 F->SLocEntryCursor.JumpToBit(F->SLocOffsets[ID]);
980 return F->SLocEntryCursor;
981}
982
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000983/// \brief Read in the source location entry with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000984ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(unsigned ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000985 if (ID == 0)
986 return Success;
987
988 if (ID > TotalNumSLocEntries) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000989 Error("source location entry ID out-of-range for AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000990 return Failure;
991 }
992
Sebastian Redl190faf72010-07-20 21:50:20 +0000993 llvm::BitstreamCursor &SLocEntryCursor = SLocCursorForID(ID);
Sebastian Redl9137a522010-07-16 17:50:48 +0000994
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000995 ++NumSLocEntriesRead;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000996 unsigned Code = SLocEntryCursor.ReadCode();
997 if (Code == llvm::bitc::END_BLOCK ||
998 Code == llvm::bitc::ENTER_SUBBLOCK ||
999 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001000 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001001 return Failure;
1002 }
1003
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001004 RecordData Record;
1005 const char *BlobStart;
1006 unsigned BlobLen;
1007 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1008 default:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001009 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001010 return Failure;
1011
1012 case pch::SM_SLOC_FILE_ENTRY: {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001013 std::string Filename(BlobStart, BlobStart + BlobLen);
1014 MaybeAddSystemRootToFilename(Filename);
1015 const FileEntry *File = FileMgr.getFile(Filename);
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001016 if (File == 0) {
1017 std::string ErrorStr = "could not find file '";
Douglas Gregore650c8c2009-07-07 00:12:59 +00001018 ErrorStr += Filename;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001019 ErrorStr += "' referenced by AST file";
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001020 Error(ErrorStr.c_str());
1021 return Failure;
1022 }
Mike Stump1eb44332009-09-09 15:08:12 +00001023
Douglas Gregor2d52be52010-03-21 22:49:54 +00001024 if (Record.size() < 10) {
Ted Kremenek1857f622010-03-18 21:23:05 +00001025 Error("source location entry is incorrect");
1026 return Failure;
1027 }
1028
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001029 if (!DisableValidation &&
1030 ((off_t)Record[4] != File->getSize()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001031#if !defined(LLVM_ON_WIN32)
1032 // In our regression testing, the Windows file system seems to
1033 // have inconsistent modification times that sometimes
1034 // erroneously trigger this error-handling path.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001035 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001036#endif
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001037 )) {
Douglas Gregor2d52be52010-03-21 22:49:54 +00001038 Diag(diag::err_fe_pch_file_modified)
1039 << Filename;
1040 return Failure;
1041 }
1042
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001043 FileID FID = SourceMgr.createFileID(File,
1044 SourceLocation::getFromRawEncoding(Record[1]),
1045 (SrcMgr::CharacteristicKind)Record[2],
1046 ID, Record[0]);
1047 if (Record[3])
1048 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile())
1049 .setHasLineDirectives();
1050
Douglas Gregor12fab312010-03-16 16:35:32 +00001051 // Reconstruct header-search information for this file.
1052 HeaderFileInfo HFI;
Douglas Gregor2d52be52010-03-21 22:49:54 +00001053 HFI.isImport = Record[6];
1054 HFI.DirInfo = Record[7];
1055 HFI.NumIncludes = Record[8];
1056 HFI.ControllingMacroID = Record[9];
Douglas Gregor12fab312010-03-16 16:35:32 +00001057 if (Listener)
1058 Listener->ReadHeaderFileInfo(HFI, File->getUID());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001059 break;
1060 }
1061
1062 case pch::SM_SLOC_BUFFER_ENTRY: {
1063 const char *Name = BlobStart;
1064 unsigned Offset = Record[0];
1065 unsigned Code = SLocEntryCursor.ReadCode();
1066 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001067 unsigned RecCode
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001068 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001069
1070 if (RecCode != pch::SM_SLOC_BUFFER_BLOB) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001071 Error("AST record has invalid code");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001072 return Failure;
1073 }
1074
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001075 llvm::MemoryBuffer *Buffer
Chris Lattnera0a270c2010-04-05 22:42:27 +00001076 = llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(BlobStart, BlobLen - 1),
1077 Name);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001078 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset);
Mike Stump1eb44332009-09-09 15:08:12 +00001079
Douglas Gregor92b059e2009-04-28 20:33:11 +00001080 if (strcmp(Name, "<built-in>") == 0) {
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +00001081 PCHPredefinesBlock Block = {
1082 BufferID,
1083 llvm::StringRef(BlobStart, BlobLen - 1)
1084 };
1085 PCHPredefinesBuffers.push_back(Block);
Douglas Gregor92b059e2009-04-28 20:33:11 +00001086 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001087
1088 break;
1089 }
1090
1091 case pch::SM_SLOC_INSTANTIATION_ENTRY: {
Mike Stump1eb44332009-09-09 15:08:12 +00001092 SourceLocation SpellingLoc
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001093 = SourceLocation::getFromRawEncoding(Record[1]);
1094 SourceMgr.createInstantiationLoc(SpellingLoc,
1095 SourceLocation::getFromRawEncoding(Record[2]),
1096 SourceLocation::getFromRawEncoding(Record[3]),
1097 Record[4],
1098 ID,
1099 Record[0]);
1100 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001101 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001102 }
1103
1104 return Success;
1105}
1106
Chris Lattner6367f6d2009-04-27 01:05:14 +00001107/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1108/// specified cursor. Read the abbreviations that are at the top of the block
1109/// and then leave the cursor pointing into the block.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001110bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattner6367f6d2009-04-27 01:05:14 +00001111 unsigned BlockID) {
1112 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001113 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001114 return Failure;
1115 }
Mike Stump1eb44332009-09-09 15:08:12 +00001116
Chris Lattner6367f6d2009-04-27 01:05:14 +00001117 while (true) {
1118 unsigned Code = Cursor.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00001119
Chris Lattner6367f6d2009-04-27 01:05:14 +00001120 // We expect all abbrevs to be at the start of the block.
1121 if (Code != llvm::bitc::DEFINE_ABBREV)
1122 return false;
1123 Cursor.ReadAbbrevRecord();
1124 }
1125}
1126
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001127void ASTReader::ReadMacroRecord(llvm::BitstreamCursor &Stream, uint64_t Offset){
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001128 assert(PP && "Forgot to set Preprocessor ?");
Mike Stump1eb44332009-09-09 15:08:12 +00001129
Douglas Gregor37e26842009-04-21 23:56:24 +00001130 // Keep track of where we are in the stream, then jump back there
1131 // after reading this macro.
1132 SavedStreamPosition SavedPosition(Stream);
1133
1134 Stream.JumpToBit(Offset);
1135 RecordData Record;
1136 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1137 MacroInfo *Macro = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001138
Douglas Gregor37e26842009-04-21 23:56:24 +00001139 while (true) {
1140 unsigned Code = Stream.ReadCode();
1141 switch (Code) {
1142 case llvm::bitc::END_BLOCK:
1143 return;
1144
1145 case llvm::bitc::ENTER_SUBBLOCK:
1146 // No known subblocks, always skip them.
1147 Stream.ReadSubBlockID();
1148 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001149 Error("malformed block record in AST file");
Douglas Gregor37e26842009-04-21 23:56:24 +00001150 return;
1151 }
1152 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001153
Douglas Gregor37e26842009-04-21 23:56:24 +00001154 case llvm::bitc::DEFINE_ABBREV:
1155 Stream.ReadAbbrevRecord();
1156 continue;
1157 default: break;
1158 }
1159
1160 // Read a record.
1161 Record.clear();
1162 pch::PreprocessorRecordTypes RecType =
1163 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1164 switch (RecType) {
Douglas Gregor37e26842009-04-21 23:56:24 +00001165 case pch::PP_MACRO_OBJECT_LIKE:
1166 case pch::PP_MACRO_FUNCTION_LIKE: {
1167 // If we already have a macro, that means that we've hit the end
1168 // of the definition of the macro we were looking for. We're
1169 // done.
1170 if (Macro)
1171 return;
1172
1173 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1174 if (II == 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001175 Error("macro must have a name in AST file");
Douglas Gregor37e26842009-04-21 23:56:24 +00001176 return;
1177 }
1178 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
1179 bool isUsed = Record[2];
Mike Stump1eb44332009-09-09 15:08:12 +00001180
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001181 MacroInfo *MI = PP->AllocateMacroInfo(Loc);
Douglas Gregor37e26842009-04-21 23:56:24 +00001182 MI->setIsUsed(isUsed);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001183 MI->setIsFromAST();
Mike Stump1eb44332009-09-09 15:08:12 +00001184
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001185 unsigned NextIndex = 3;
Douglas Gregor37e26842009-04-21 23:56:24 +00001186 if (RecType == pch::PP_MACRO_FUNCTION_LIKE) {
1187 // Decode function-like macro info.
1188 bool isC99VarArgs = Record[3];
1189 bool isGNUVarArgs = Record[4];
1190 MacroArgs.clear();
1191 unsigned NumArgs = Record[5];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001192 NextIndex = 6 + NumArgs;
Douglas Gregor37e26842009-04-21 23:56:24 +00001193 for (unsigned i = 0; i != NumArgs; ++i)
1194 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1195
1196 // Install function-like macro info.
1197 MI->setIsFunctionLike();
1198 if (isC99VarArgs) MI->setIsC99Varargs();
1199 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor75fdb232009-05-22 22:45:36 +00001200 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001201 PP->getPreprocessorAllocator());
Douglas Gregor37e26842009-04-21 23:56:24 +00001202 }
1203
1204 // Finally, install the macro.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001205 PP->setMacroInfo(II, MI);
Douglas Gregor37e26842009-04-21 23:56:24 +00001206
1207 // Remember that we saw this macro last so that we add the tokens that
1208 // form its body to it.
1209 Macro = MI;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001210
1211 if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) {
1212 // We have a macro definition. Load it now.
1213 PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro,
1214 getMacroDefinition(Record[NextIndex]));
1215 }
1216
Douglas Gregor37e26842009-04-21 23:56:24 +00001217 ++NumMacrosRead;
1218 break;
1219 }
Mike Stump1eb44332009-09-09 15:08:12 +00001220
Douglas Gregor37e26842009-04-21 23:56:24 +00001221 case pch::PP_TOKEN: {
1222 // If we see a TOKEN before a PP_MACRO_*, then the file is
1223 // erroneous, just pretend we didn't see this.
1224 if (Macro == 0) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001225
Douglas Gregor37e26842009-04-21 23:56:24 +00001226 Token Tok;
1227 Tok.startToken();
1228 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
1229 Tok.setLength(Record[1]);
1230 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1231 Tok.setIdentifierInfo(II);
1232 Tok.setKind((tok::TokenKind)Record[3]);
1233 Tok.setFlag((Token::TokenFlags)Record[4]);
1234 Macro->AddTokenToBody(Tok);
1235 break;
1236 }
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001237
1238 case pch::PP_MACRO_INSTANTIATION: {
1239 // If we already have a macro, that means that we've hit the end
1240 // of the definition of the macro we were looking for. We're
1241 // done.
1242 if (Macro)
1243 return;
1244
1245 if (!PP->getPreprocessingRecord()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001246 Error("missing preprocessing record in AST file");
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001247 return;
1248 }
1249
1250 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1251 if (PPRec.getPreprocessedEntity(Record[0]))
1252 return;
1253
1254 MacroInstantiation *MI
1255 = new (PPRec) MacroInstantiation(DecodeIdentifierInfo(Record[3]),
1256 SourceRange(
1257 SourceLocation::getFromRawEncoding(Record[1]),
1258 SourceLocation::getFromRawEncoding(Record[2])),
1259 getMacroDefinition(Record[4]));
1260 PPRec.SetPreallocatedEntity(Record[0], MI);
1261 return;
1262 }
1263
1264 case pch::PP_MACRO_DEFINITION: {
1265 // If we already have a macro, that means that we've hit the end
1266 // of the definition of the macro we were looking for. We're
1267 // done.
1268 if (Macro)
1269 return;
1270
1271 if (!PP->getPreprocessingRecord()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001272 Error("missing preprocessing record in AST file");
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001273 return;
1274 }
1275
1276 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1277 if (PPRec.getPreprocessedEntity(Record[0]))
1278 return;
1279
1280 if (Record[1] >= MacroDefinitionsLoaded.size()) {
1281 Error("out-of-bounds macro definition record");
1282 return;
1283 }
1284
1285 MacroDefinition *MD
1286 = new (PPRec) MacroDefinition(DecodeIdentifierInfo(Record[4]),
1287 SourceLocation::getFromRawEncoding(Record[5]),
1288 SourceRange(
1289 SourceLocation::getFromRawEncoding(Record[2]),
1290 SourceLocation::getFromRawEncoding(Record[3])));
1291 PPRec.SetPreallocatedEntity(Record[0], MD);
1292 MacroDefinitionsLoaded[Record[1]] = MD;
1293 return;
1294 }
Steve Naroff83d63c72009-04-24 20:03:17 +00001295 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001296 }
1297}
1298
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001299void ASTReader::ReadDefinedMacros() {
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001300 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1301 llvm::BitstreamCursor &MacroCursor = Chain[N - I - 1]->MacroCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001302
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001303 // If there was no preprocessor block, skip this file.
1304 if (!MacroCursor.getBitStreamReader())
1305 continue;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001306
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001307 llvm::BitstreamCursor Cursor = MacroCursor;
1308 if (Cursor.EnterSubBlock(pch::PREPROCESSOR_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001309 Error("malformed preprocessor block record in AST file");
Douglas Gregor88a35862010-01-04 19:18:44 +00001310 return;
1311 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001312
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001313 RecordData Record;
1314 while (true) {
1315 unsigned Code = Cursor.ReadCode();
1316 if (Code == llvm::bitc::END_BLOCK) {
1317 if (Cursor.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001318 Error("error at end of preprocessor block in AST file");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001319 return;
1320 }
1321 break;
Douglas Gregor88a35862010-01-04 19:18:44 +00001322 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001323
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001324 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1325 // No known subblocks, always skip them.
1326 Cursor.ReadSubBlockID();
1327 if (Cursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001328 Error("malformed block record in AST file");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001329 return;
1330 }
1331 continue;
1332 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001333
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001334 if (Code == llvm::bitc::DEFINE_ABBREV) {
1335 Cursor.ReadAbbrevRecord();
1336 continue;
1337 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001338
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001339 // Read a record.
1340 const char *BlobStart;
1341 unsigned BlobLen;
1342 Record.clear();
1343 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1344 default: // Default behavior: ignore.
1345 break;
Douglas Gregor88a35862010-01-04 19:18:44 +00001346
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001347 case pch::PP_MACRO_OBJECT_LIKE:
1348 case pch::PP_MACRO_FUNCTION_LIKE:
1349 DecodeIdentifierInfo(Record[0]);
1350 break;
1351
1352 case pch::PP_TOKEN:
1353 // Ignore tokens.
1354 break;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001355
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001356 case pch::PP_MACRO_INSTANTIATION:
1357 case pch::PP_MACRO_DEFINITION:
1358 // Read the macro record.
1359 ReadMacroRecord(Chain[N - I - 1]->Stream, Cursor.GetCurrentBitNo());
1360 break;
1361 }
Douglas Gregor88a35862010-01-04 19:18:44 +00001362 }
1363 }
1364}
1365
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001366MacroDefinition *ASTReader::getMacroDefinition(pch::IdentID ID) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001367 if (ID == 0 || ID >= MacroDefinitionsLoaded.size())
1368 return 0;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001369
1370 if (!MacroDefinitionsLoaded[ID]) {
1371 unsigned Index = ID;
1372 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1373 PerFileData &F = *Chain[N - I - 1];
1374 if (Index < F.LocalNumMacroDefinitions) {
1375 ReadMacroRecord(F.Stream, F.MacroDefinitionOffsets[Index]);
1376 break;
1377 }
1378 Index -= F.LocalNumMacroDefinitions;
1379 }
1380 assert(MacroDefinitionsLoaded[ID] && "Broken chain");
1381 }
1382
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001383 return MacroDefinitionsLoaded[ID];
1384}
1385
Douglas Gregore650c8c2009-07-07 00:12:59 +00001386/// \brief If we are loading a relocatable PCH file, and the filename is
1387/// not an absolute path, add the system root to the beginning of the file
1388/// name.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001389void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001390 // If this is not a relocatable PCH file, there's nothing to do.
1391 if (!RelocatablePCH)
1392 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001393
Daniel Dunbard5b21972009-11-18 19:50:41 +00001394 if (Filename.empty() || llvm::sys::Path(Filename).isAbsolute())
Douglas Gregore650c8c2009-07-07 00:12:59 +00001395 return;
1396
Douglas Gregore650c8c2009-07-07 00:12:59 +00001397 if (isysroot == 0) {
1398 // If no system root was given, default to '/'
1399 Filename.insert(Filename.begin(), '/');
1400 return;
1401 }
Mike Stump1eb44332009-09-09 15:08:12 +00001402
Douglas Gregore650c8c2009-07-07 00:12:59 +00001403 unsigned Length = strlen(isysroot);
1404 if (isysroot[Length - 1] != '/')
1405 Filename.insert(Filename.begin(), '/');
Mike Stump1eb44332009-09-09 15:08:12 +00001406
Douglas Gregore650c8c2009-07-07 00:12:59 +00001407 Filename.insert(Filename.begin(), isysroot, isysroot + Length);
1408}
1409
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001410ASTReader::ASTReadResult
Sebastian Redl571db7f2010-08-18 23:56:56 +00001411ASTReader::ReadASTBlock(PerFileData &F) {
Sebastian Redl9137a522010-07-16 17:50:48 +00001412 llvm::BitstreamCursor &Stream = F.Stream;
1413
Sebastian Redlf29f0a22010-08-18 23:57:22 +00001414 if (Stream.EnterSubBlock(pch::AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001415 Error("malformed block record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001416 return Failure;
1417 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001418
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001419 // Read all of the records and blocks for the ASt file.
Douglas Gregor8038d512009-04-10 17:25:41 +00001420 RecordData Record;
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001421 bool First = true;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001422 while (!Stream.AtEndOfStream()) {
1423 unsigned Code = Stream.ReadCode();
1424 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001425 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001426 Error("error at end of module block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001427 return Failure;
1428 }
Chris Lattner7356a312009-04-11 21:15:38 +00001429
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001430 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001431 }
1432
1433 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1434 switch (Stream.ReadSubBlockID()) {
Douglas Gregor61d60ee2009-10-17 00:13:19 +00001435 case pch::DECLTYPES_BLOCK_ID:
Chris Lattner6367f6d2009-04-27 01:05:14 +00001436 // We lazily load the decls block, but we want to set up the
1437 // DeclsCursor cursor to point into it. Clone our current bitcode
1438 // cursor to it, enter the block and read the abbrevs in that block.
1439 // With the main cursor, we just skip over it.
Sebastian Redl9137a522010-07-16 17:50:48 +00001440 F.DeclsCursor = Stream;
Chris Lattner6367f6d2009-04-27 01:05:14 +00001441 if (Stream.SkipBlock() || // Skip with the main cursor.
1442 // Read the abbrevs.
Sebastian Redl9137a522010-07-16 17:50:48 +00001443 ReadBlockAbbrevs(F.DeclsCursor, pch::DECLTYPES_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001444 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001445 return Failure;
1446 }
1447 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001448
Chris Lattner7356a312009-04-11 21:15:38 +00001449 case pch::PREPROCESSOR_BLOCK_ID:
Sebastian Redl9137a522010-07-16 17:50:48 +00001450 F.MacroCursor = Stream;
Douglas Gregor88a35862010-01-04 19:18:44 +00001451 if (PP)
1452 PP->setExternalSource(this);
1453
Chris Lattner7356a312009-04-11 21:15:38 +00001454 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001455 Error("malformed block record in AST file");
Chris Lattner7356a312009-04-11 21:15:38 +00001456 return Failure;
1457 }
1458 break;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001459
Douglas Gregor14f79002009-04-10 03:52:48 +00001460 case pch::SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001461 switch (ReadSourceManagerBlock(F)) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00001462 case Success:
1463 break;
1464
1465 case Failure:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001466 Error("malformed source manager block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001467 return Failure;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001468
1469 case IgnorePCH:
1470 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001471 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001472 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001473 }
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001474 First = false;
Douglas Gregor8038d512009-04-10 17:25:41 +00001475 continue;
1476 }
1477
1478 if (Code == llvm::bitc::DEFINE_ABBREV) {
1479 Stream.ReadAbbrevRecord();
1480 continue;
1481 }
1482
1483 // Read and process a record.
1484 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00001485 const char *BlobStart = 0;
1486 unsigned BlobLen = 0;
Sebastian Redlf29f0a22010-08-18 23:57:22 +00001487 switch ((pch::ASTRecordTypes)Stream.ReadRecord(Code, Record,
Douglas Gregor2bec0412009-04-10 21:16:55 +00001488 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001489 default: // Default behavior: ignore.
1490 break;
1491
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001492 case pch::METADATA: {
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001493 if (Record[0] != pch::VERSION_MAJOR && !DisableValidation) {
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001494 Diag(Record[0] < pch::VERSION_MAJOR? diag::warn_pch_version_too_old
1495 : diag::warn_pch_version_too_new);
1496 return IgnorePCH;
1497 }
1498
1499 RelocatablePCH = Record[4];
1500 if (Listener) {
1501 std::string TargetTriple(BlobStart, BlobLen);
1502 if (Listener->ReadTargetTriple(TargetTriple))
1503 return IgnorePCH;
1504 }
1505 break;
1506 }
1507
1508 case pch::CHAINED_METADATA: {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001509 if (!First) {
1510 Error("CHAINED_METADATA is not first record in block");
1511 return Failure;
1512 }
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001513 if (Record[0] != pch::VERSION_MAJOR && !DisableValidation) {
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001514 Diag(Record[0] < pch::VERSION_MAJOR? diag::warn_pch_version_too_old
1515 : diag::warn_pch_version_too_new);
1516 return IgnorePCH;
1517 }
1518
1519 // Load the chained file.
Sebastian Redl571db7f2010-08-18 23:56:56 +00001520 switch(ReadASTCore(llvm::StringRef(BlobStart, BlobLen))) {
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001521 case Failure: return Failure;
1522 // If we have to ignore the dependency, we'll have to ignore this too.
1523 case IgnorePCH: return IgnorePCH;
1524 case Success: break;
1525 }
1526 break;
1527 }
1528
Douglas Gregor8038d512009-04-10 17:25:41 +00001529 case pch::TYPE_OFFSET:
Sebastian Redl12d6da02010-07-19 22:06:55 +00001530 if (F.LocalNumTypes != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001531 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001532 return Failure;
1533 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00001534 F.TypeOffsets = (const uint32_t *)BlobStart;
1535 F.LocalNumTypes = Record[0];
Douglas Gregor8038d512009-04-10 17:25:41 +00001536 break;
1537
1538 case pch::DECL_OFFSET:
Sebastian Redl12d6da02010-07-19 22:06:55 +00001539 if (F.LocalNumDecls != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001540 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001541 return Failure;
1542 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00001543 F.DeclOffsets = (const uint32_t *)BlobStart;
1544 F.LocalNumDecls = Record[0];
Douglas Gregor8038d512009-04-10 17:25:41 +00001545 break;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001546
Sebastian Redld692af72010-07-27 18:24:41 +00001547 case pch::TU_UPDATE_LEXICAL: {
1548 DeclContextInfo Info = {
1549 /* No visible information */ 0, 0,
1550 reinterpret_cast<const pch::DeclID *>(BlobStart),
1551 BlobLen / sizeof(pch::DeclID)
1552 };
1553 DeclContextOffsets[Context->getTranslationUnitDecl()].push_back(Info);
1554 break;
1555 }
1556
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00001557 case pch::REDECLS_UPDATE_LATEST: {
1558 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
1559 for (unsigned i = 0, e = Record.size(); i < e; i += 2) {
1560 pch::DeclID First = Record[i], Latest = Record[i+1];
1561 assert((FirstLatestDeclIDs.find(First) == FirstLatestDeclIDs.end() ||
1562 Latest > FirstLatestDeclIDs[First]) &&
1563 "The new latest is supposed to come after the previous latest");
1564 FirstLatestDeclIDs[First] = Latest;
1565 }
1566 break;
1567 }
1568
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001569 case pch::LANGUAGE_OPTIONS:
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001570 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001571 return IgnorePCH;
1572 break;
Douglas Gregor2bec0412009-04-10 21:16:55 +00001573
Douglas Gregorafaf3082009-04-11 00:14:32 +00001574 case pch::IDENTIFIER_TABLE:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001575 F.IdentifierTableData = BlobStart;
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001576 if (Record[0]) {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001577 F.IdentifierLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001578 = ASTIdentifierLookupTable::Create(
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001579 (const unsigned char *)F.IdentifierTableData + Record[0],
1580 (const unsigned char *)F.IdentifierTableData,
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001581 ASTIdentifierLookupTrait(*this, F.Stream));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001582 if (PP)
1583 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001584 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001585 break;
1586
1587 case pch::IDENTIFIER_OFFSET:
Sebastian Redl2da08f92010-07-19 22:28:42 +00001588 if (F.LocalNumIdentifiers != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001589 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00001590 return Failure;
1591 }
Sebastian Redl2da08f92010-07-19 22:28:42 +00001592 F.IdentifierOffsets = (const uint32_t *)BlobStart;
1593 F.LocalNumIdentifiers = Record[0];
Douglas Gregorafaf3082009-04-11 00:14:32 +00001594 break;
Douglas Gregorfdd01722009-04-14 00:24:19 +00001595
1596 case pch::EXTERNAL_DEFINITIONS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001597 // Optimization for the first block.
1598 if (ExternalDefinitions.empty())
1599 ExternalDefinitions.swap(Record);
1600 else
1601 ExternalDefinitions.insert(ExternalDefinitions.end(),
1602 Record.begin(), Record.end());
Douglas Gregorfdd01722009-04-14 00:24:19 +00001603 break;
Douglas Gregor3e1af842009-04-17 22:13:46 +00001604
Douglas Gregorad1de002009-04-18 05:55:16 +00001605 case pch::SPECIAL_TYPES:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001606 // Optimization for the first block
1607 if (SpecialTypes.empty())
1608 SpecialTypes.swap(Record);
1609 else
1610 SpecialTypes.insert(SpecialTypes.end(), Record.begin(), Record.end());
Douglas Gregorad1de002009-04-18 05:55:16 +00001611 break;
1612
Douglas Gregor3e1af842009-04-17 22:13:46 +00001613 case pch::STATISTICS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001614 TotalNumStatements += Record[0];
1615 TotalNumMacros += Record[1];
1616 TotalLexicalDeclContexts += Record[2];
1617 TotalVisibleDeclContexts += Record[3];
Douglas Gregor3e1af842009-04-17 22:13:46 +00001618 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001619
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00001620 case pch::TENTATIVE_DEFINITIONS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001621 // Optimization for the first block.
1622 if (TentativeDefinitions.empty())
1623 TentativeDefinitions.swap(Record);
1624 else
1625 TentativeDefinitions.insert(TentativeDefinitions.end(),
1626 Record.begin(), Record.end());
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00001627 break;
Douglas Gregor14c22f22009-04-22 22:18:58 +00001628
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00001629 case pch::UNUSED_FILESCOPED_DECLS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001630 // Optimization for the first block.
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00001631 if (UnusedFileScopedDecls.empty())
1632 UnusedFileScopedDecls.swap(Record);
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001633 else
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00001634 UnusedFileScopedDecls.insert(UnusedFileScopedDecls.end(),
1635 Record.begin(), Record.end());
Tanya Lattnere6bbc012010-02-12 00:07:30 +00001636 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001637
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00001638 case pch::WEAK_UNDECLARED_IDENTIFIERS:
Sebastian Redl40566802010-08-05 18:21:25 +00001639 // Later blocks overwrite earlier ones.
1640 WeakUndeclaredIdentifiers.swap(Record);
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00001641 break;
1642
Douglas Gregor14c22f22009-04-22 22:18:58 +00001643 case pch::LOCALLY_SCOPED_EXTERNAL_DECLS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001644 // Optimization for the first block.
1645 if (LocallyScopedExternalDecls.empty())
1646 LocallyScopedExternalDecls.swap(Record);
1647 else
1648 LocallyScopedExternalDecls.insert(LocallyScopedExternalDecls.end(),
1649 Record.begin(), Record.end());
Douglas Gregor14c22f22009-04-22 22:18:58 +00001650 break;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001651
Douglas Gregor83941df2009-04-25 17:48:32 +00001652 case pch::SELECTOR_OFFSETS:
Sebastian Redl059612d2010-08-03 21:58:15 +00001653 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redl725cd962010-08-04 20:40:17 +00001654 F.LocalNumSelectors = Record[0];
Douglas Gregor83941df2009-04-25 17:48:32 +00001655 break;
1656
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001657 case pch::METHOD_POOL:
Sebastian Redl725cd962010-08-04 20:40:17 +00001658 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor83941df2009-04-25 17:48:32 +00001659 if (Record[0])
Sebastian Redl725cd962010-08-04 20:40:17 +00001660 F.SelectorLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001661 = ASTSelectorLookupTable::Create(
Sebastian Redl725cd962010-08-04 20:40:17 +00001662 F.SelectorLookupTableData + Record[0],
1663 F.SelectorLookupTableData,
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001664 ASTSelectorLookupTrait(*this));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00001665 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001666 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001667
Fariborz Jahanian32019832010-07-23 19:11:11 +00001668 case pch::REFERENCED_SELECTOR_POOL: {
Sebastian Redl725cd962010-08-04 20:40:17 +00001669 ReferencedSelectorsData.insert(ReferencedSelectorsData.end(),
1670 Record.begin(), Record.end());
Fariborz Jahanian32019832010-07-23 19:11:11 +00001671 break;
Sebastian Redl681d7232010-07-27 00:17:23 +00001672 }
Fariborz Jahanian32019832010-07-23 19:11:11 +00001673
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001674 case pch::PP_COUNTER_VALUE:
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001675 if (!Record.empty() && Listener)
1676 Listener->ReadCounter(Record[0]);
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001677 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001678
1679 case pch::SOURCE_LOCATION_OFFSETS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001680 F.SLocOffsets = (const uint32_t *)BlobStart;
1681 F.LocalNumSLocEntries = Record[0];
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001682 // We cannot delay this until the entire chain is loaded, because then
1683 // source location preloads would also have to be delayed.
1684 // FIXME: Is there a reason not to do that?
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001685 TotalNumSLocEntries += F.LocalNumSLocEntries;
Douglas Gregor445e23e2009-10-05 21:07:28 +00001686 SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, Record[1]);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001687 break;
1688
1689 case pch::SOURCE_LOCATION_PRELOADS:
1690 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001691 ASTReadResult Result = ReadSLocEntryRecord(Record[I]);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001692 if (Result != Success)
1693 return Result;
1694 }
1695 break;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001696
Douglas Gregor52e71082009-10-16 18:18:30 +00001697 case pch::STAT_CACHE: {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001698 ASTStatCache *MyStatCache =
1699 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
Douglas Gregor52e71082009-10-16 18:18:30 +00001700 (const unsigned char *)BlobStart,
1701 NumStatHits, NumStatMisses);
1702 FileMgr.addStatCache(MyStatCache);
Sebastian Redl9137a522010-07-16 17:50:48 +00001703 F.StatCache = MyStatCache;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001704 break;
Douglas Gregor52e71082009-10-16 18:18:30 +00001705 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001706
Douglas Gregorb81c1702009-04-27 20:06:05 +00001707 case pch::EXT_VECTOR_DECLS:
Sebastian Redla9f23682010-07-28 21:38:49 +00001708 // Optimization for the first block.
1709 if (ExtVectorDecls.empty())
1710 ExtVectorDecls.swap(Record);
1711 else
1712 ExtVectorDecls.insert(ExtVectorDecls.end(),
1713 Record.begin(), Record.end());
Douglas Gregorb81c1702009-04-27 20:06:05 +00001714 break;
1715
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00001716 case pch::VTABLE_USES:
Sebastian Redl40566802010-08-05 18:21:25 +00001717 // Later tables overwrite earlier ones.
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00001718 VTableUses.swap(Record);
1719 break;
1720
1721 case pch::DYNAMIC_CLASSES:
Sebastian Redl40566802010-08-05 18:21:25 +00001722 // Optimization for the first block.
1723 if (DynamicClasses.empty())
1724 DynamicClasses.swap(Record);
1725 else
1726 DynamicClasses.insert(DynamicClasses.end(),
1727 Record.begin(), Record.end());
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00001728 break;
1729
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00001730 case pch::PENDING_IMPLICIT_INSTANTIATIONS:
1731 // Optimization for the first block.
1732 if (PendingImplicitInstantiations.empty())
1733 PendingImplicitInstantiations.swap(Record);
1734 else
1735 PendingImplicitInstantiations.insert(
1736 PendingImplicitInstantiations.end(), Record.begin(), Record.end());
1737 break;
1738
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00001739 case pch::SEMA_DECL_REFS:
Sebastian Redl40566802010-08-05 18:21:25 +00001740 // Later tables overwrite earlier ones.
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00001741 SemaDeclRefs.swap(Record);
1742 break;
1743
Douglas Gregorb64c1932009-05-12 01:31:05 +00001744 case pch::ORIGINAL_FILE_NAME:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001745 // The primary AST will be the last to get here, so it will be the one
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001746 // that's used.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +00001747 ActualOriginalFileName.assign(BlobStart, BlobLen);
1748 OriginalFileName = ActualOriginalFileName;
Douglas Gregore650c8c2009-07-07 00:12:59 +00001749 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001750 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001751
Ted Kremenek5b4ec632010-01-22 20:59:36 +00001752 case pch::VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek974be4d2010-02-12 23:31:14 +00001753 const std::string &CurBranch = getClangFullRepositoryVersion();
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001754 llvm::StringRef ASTBranch(BlobStart, BlobLen);
1755 if (llvm::StringRef(CurBranch) != ASTBranch && !DisableValidation) {
1756 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregor445e23e2009-10-05 21:07:28 +00001757 return IgnorePCH;
1758 }
1759 break;
1760 }
Sebastian Redl04e6fd42010-07-21 20:07:32 +00001761
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001762 case pch::MACRO_DEFINITION_OFFSETS:
Sebastian Redl04e6fd42010-07-21 20:07:32 +00001763 F.MacroDefinitionOffsets = (const uint32_t *)BlobStart;
1764 F.NumPreallocatedPreprocessingEntities = Record[0];
1765 F.LocalNumMacroDefinitions = Record[1];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001766 break;
Sebastian Redl0b17c612010-08-13 00:28:03 +00001767
1768 case pch::DECL_REPLACEMENTS: {
1769 if (Record.size() % 2 != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001770 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redl0b17c612010-08-13 00:28:03 +00001771 return Failure;
1772 }
1773 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
1774 ReplacedDecls[static_cast<pch::DeclID>(Record[I])] =
1775 std::make_pair(&F, Record[I+1]);
1776 break;
1777 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001778 }
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001779 First = false;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001780 }
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001781 Error("premature end of bitstream in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001782 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001783}
1784
Sebastian Redl571db7f2010-08-18 23:56:56 +00001785ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName) {
1786 switch(ReadASTCore(FileName)) {
Sebastian Redlcdf3b832010-07-16 20:41:52 +00001787 case Failure: return Failure;
1788 case IgnorePCH: return IgnorePCH;
1789 case Success: break;
1790 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001791
1792 // Here comes stuff that we only do once the entire chain is loaded.
1793
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001794 // Allocate space for loaded identifiers, decls and types.
Sebastian Redl04e6fd42010-07-21 20:07:32 +00001795 unsigned TotalNumIdentifiers = 0, TotalNumTypes = 0, TotalNumDecls = 0,
Sebastian Redl725cd962010-08-04 20:40:17 +00001796 TotalNumPreallocatedPreprocessingEntities = 0, TotalNumMacroDefs = 0,
1797 TotalNumSelectors = 0;
Sebastian Redl12d6da02010-07-19 22:06:55 +00001798 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl2da08f92010-07-19 22:28:42 +00001799 TotalNumIdentifiers += Chain[I]->LocalNumIdentifiers;
Sebastian Redl12d6da02010-07-19 22:06:55 +00001800 TotalNumTypes += Chain[I]->LocalNumTypes;
1801 TotalNumDecls += Chain[I]->LocalNumDecls;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00001802 TotalNumPreallocatedPreprocessingEntities +=
1803 Chain[I]->NumPreallocatedPreprocessingEntities;
1804 TotalNumMacroDefs += Chain[I]->LocalNumMacroDefinitions;
Sebastian Redl725cd962010-08-04 20:40:17 +00001805 TotalNumSelectors += Chain[I]->LocalNumSelectors;
Sebastian Redl12d6da02010-07-19 22:06:55 +00001806 }
Sebastian Redl2da08f92010-07-19 22:28:42 +00001807 IdentifiersLoaded.resize(TotalNumIdentifiers);
Sebastian Redl12d6da02010-07-19 22:06:55 +00001808 TypesLoaded.resize(TotalNumTypes);
1809 DeclsLoaded.resize(TotalNumDecls);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00001810 MacroDefinitionsLoaded.resize(TotalNumMacroDefs);
1811 if (PP) {
1812 if (TotalNumIdentifiers > 0)
1813 PP->getHeaderSearchInfo().SetExternalLookup(this);
1814 if (TotalNumPreallocatedPreprocessingEntities > 0) {
1815 if (!PP->getPreprocessingRecord())
1816 PP->createPreprocessingRecord();
1817 PP->getPreprocessingRecord()->SetExternalSource(*this,
1818 TotalNumPreallocatedPreprocessingEntities);
1819 }
1820 }
Sebastian Redl725cd962010-08-04 20:40:17 +00001821 SelectorsLoaded.resize(TotalNumSelectors);
Sebastian Redl12d6da02010-07-19 22:06:55 +00001822
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001823 // Check the predefines buffers.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001824 if (!DisableValidation && CheckPredefinesBuffers())
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001825 return IgnorePCH;
1826
1827 if (PP) {
1828 // Initialization of keywords and pragmas occurs before the
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001829 // AST file is read, so there may be some identifiers that were
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001830 // loaded into the IdentifierTable before we intercepted the
1831 // creation of identifiers. Iterate through the list of known
1832 // identifiers and determine whether we have to establish
1833 // preprocessor definitions or top-level identifier declaration
1834 // chains for those identifiers.
1835 //
1836 // We copy the IdentifierInfo pointers to a small vector first,
1837 // since de-serializing declarations or macro definitions can add
1838 // new entries into the identifier table, invalidating the
1839 // iterators.
1840 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
1841 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
1842 IdEnd = PP->getIdentifierTable().end();
1843 Id != IdEnd; ++Id)
1844 Identifiers.push_back(Id->second);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00001845 // We need to search the tables in all files.
Sebastian Redl04e6fd42010-07-21 20:07:32 +00001846 for (unsigned J = 0, M = Chain.size(); J != M; ++J) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001847 ASTIdentifierLookupTable *IdTable
1848 = (ASTIdentifierLookupTable *)Chain[J]->IdentifierLookupTable;
1849 // Not all AST files necessarily have identifier tables, only the useful
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001850 // ones.
1851 if (!IdTable)
1852 continue;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00001853 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
1854 IdentifierInfo *II = Identifiers[I];
1855 // Look in the on-disk hash tables for an entry for this identifier
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001856 ASTIdentifierLookupTrait Info(*this, Chain[J]->Stream, II);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00001857 std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength());
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001858 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001859 if (Pos == IdTable->end())
1860 continue;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001861
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001862 // Dereferencing the iterator has the effect of populating the
1863 // IdentifierInfo node with the various declarations it needs.
1864 (void)*Pos;
1865 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001866 }
1867 }
1868
1869 if (Context)
1870 InitializeContext(*Context);
1871
1872 return Success;
1873}
1874
Sebastian Redl571db7f2010-08-18 23:56:56 +00001875ASTReader::ASTReadResult ASTReader::ReadASTCore(llvm::StringRef FileName) {
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001876 Chain.push_back(new PerFileData());
Sebastian Redl9137a522010-07-16 17:50:48 +00001877 PerFileData &F = *Chain.back();
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001878
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001879 // Set the AST file name.
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001880 F.FileName = FileName;
1881
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001882 // Open the AST file.
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001883 //
1884 // FIXME: This shouldn't be here, we should just take a raw_ostream.
1885 std::string ErrStr;
1886 F.Buffer.reset(llvm::MemoryBuffer::getFileOrSTDIN(FileName, &ErrStr));
1887 if (!F.Buffer) {
1888 Error(ErrStr.c_str());
1889 return IgnorePCH;
1890 }
1891
1892 // Initialize the stream
1893 F.StreamFile.init((const unsigned char *)F.Buffer->getBufferStart(),
1894 (const unsigned char *)F.Buffer->getBufferEnd());
Sebastian Redl9137a522010-07-16 17:50:48 +00001895 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001896 Stream.init(F.StreamFile);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00001897 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001898
1899 // Sniff for the signature.
1900 if (Stream.Read(8) != 'C' ||
1901 Stream.Read(8) != 'P' ||
1902 Stream.Read(8) != 'C' ||
1903 Stream.Read(8) != 'H') {
1904 Diag(diag::err_not_a_pch_file) << FileName;
1905 return Failure;
1906 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001907
Douglas Gregor2cf26342009-04-09 22:27:44 +00001908 while (!Stream.AtEndOfStream()) {
1909 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00001910
Douglas Gregore1d918e2009-04-10 23:10:45 +00001911 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001912 Error("invalid record at top-level of AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001913 return Failure;
1914 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001915
1916 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregor668c1a42009-04-21 22:25:48 +00001917
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001918 // We only know the AST subblock ID.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001919 switch (BlockID) {
1920 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001921 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001922 Error("malformed BlockInfoBlock in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001923 return Failure;
1924 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001925 break;
Sebastian Redlf29f0a22010-08-18 23:57:22 +00001926 case pch::AST_BLOCK_ID:
Sebastian Redl571db7f2010-08-18 23:56:56 +00001927 switch (ReadASTBlock(F)) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001928 case Success:
1929 break;
1930
1931 case Failure:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001932 return Failure;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001933
1934 case IgnorePCH:
Douglas Gregor2bec0412009-04-10 21:16:55 +00001935 // FIXME: We could consider reading through to the end of this
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001936 // AST block, skipping subblocks, to see if there are other
1937 // AST blocks elsewhere.
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00001938
1939 // Clear out any preallocated source location entries, so that
1940 // the source manager does not try to resolve them later.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001941 SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00001942
1943 // Remove the stat cache.
Sebastian Redl9137a522010-07-16 17:50:48 +00001944 if (F.StatCache)
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001945 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00001946
Douglas Gregore1d918e2009-04-10 23:10:45 +00001947 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001948 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001949 break;
1950 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001951 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001952 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001953 return Failure;
1954 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001955 break;
1956 }
Mike Stump1eb44332009-09-09 15:08:12 +00001957 }
1958
Sebastian Redlcdf3b832010-07-16 20:41:52 +00001959 return Success;
1960}
1961
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001962void ASTReader::setPreprocessor(Preprocessor &pp) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001963 PP = &pp;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00001964
1965 unsigned TotalNum = 0;
1966 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
1967 TotalNum += Chain[I]->NumPreallocatedPreprocessingEntities;
1968 if (TotalNum) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001969 if (!PP->getPreprocessingRecord())
1970 PP->createPreprocessingRecord();
Sebastian Redl04e6fd42010-07-21 20:07:32 +00001971 PP->getPreprocessingRecord()->SetExternalSource(*this, TotalNum);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001972 }
1973}
1974
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001975void ASTReader::InitializeContext(ASTContext &Ctx) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001976 Context = &Ctx;
1977 assert(Context && "Passed null context!");
1978
1979 assert(PP && "Forgot to set Preprocessor ?");
1980 PP->getIdentifierTable().setExternalIdentifierLookup(this);
1981 PP->getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor88a35862010-01-04 19:18:44 +00001982 PP->setExternalSource(this);
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001983
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001984 // Load the translation unit declaration
Argyrios Kyrtzidis8871a442010-07-08 17:13:02 +00001985 GetTranslationUnitDecl();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001986
1987 // Load the special types.
1988 Context->setBuiltinVaListType(
1989 GetType(SpecialTypes[pch::SPECIAL_TYPE_BUILTIN_VA_LIST]));
1990 if (unsigned Id = SpecialTypes[pch::SPECIAL_TYPE_OBJC_ID])
1991 Context->setObjCIdType(GetType(Id));
1992 if (unsigned Sel = SpecialTypes[pch::SPECIAL_TYPE_OBJC_SELECTOR])
1993 Context->setObjCSelType(GetType(Sel));
1994 if (unsigned Proto = SpecialTypes[pch::SPECIAL_TYPE_OBJC_PROTOCOL])
1995 Context->setObjCProtoType(GetType(Proto));
1996 if (unsigned Class = SpecialTypes[pch::SPECIAL_TYPE_OBJC_CLASS])
1997 Context->setObjCClassType(GetType(Class));
Steve Naroff14108da2009-07-10 23:34:53 +00001998
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001999 if (unsigned String = SpecialTypes[pch::SPECIAL_TYPE_CF_CONSTANT_STRING])
2000 Context->setCFConstantStringType(GetType(String));
Mike Stump1eb44332009-09-09 15:08:12 +00002001 if (unsigned FastEnum
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002002 = SpecialTypes[pch::SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
2003 Context->setObjCFastEnumerationStateType(GetType(FastEnum));
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002004 if (unsigned File = SpecialTypes[pch::SPECIAL_TYPE_FILE]) {
2005 QualType FileType = GetType(File);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002006 if (FileType.isNull()) {
2007 Error("FILE type is NULL");
2008 return;
2009 }
John McCall183700f2009-09-21 23:43:11 +00002010 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002011 Context->setFILEDecl(Typedef->getDecl());
2012 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002013 const TagType *Tag = FileType->getAs<TagType>();
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002014 if (!Tag) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002015 Error("Invalid FILE type in AST file");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002016 return;
2017 }
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002018 Context->setFILEDecl(Tag->getDecl());
2019 }
2020 }
Mike Stump782fa302009-07-28 02:25:19 +00002021 if (unsigned Jmp_buf = SpecialTypes[pch::SPECIAL_TYPE_jmp_buf]) {
2022 QualType Jmp_bufType = GetType(Jmp_buf);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002023 if (Jmp_bufType.isNull()) {
2024 Error("jmp_bug type is NULL");
2025 return;
2026 }
John McCall183700f2009-09-21 23:43:11 +00002027 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
Mike Stump782fa302009-07-28 02:25:19 +00002028 Context->setjmp_bufDecl(Typedef->getDecl());
2029 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002030 const TagType *Tag = Jmp_bufType->getAs<TagType>();
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002031 if (!Tag) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002032 Error("Invalid jmp_buf type in AST file");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002033 return;
2034 }
Mike Stump782fa302009-07-28 02:25:19 +00002035 Context->setjmp_bufDecl(Tag->getDecl());
2036 }
2037 }
2038 if (unsigned Sigjmp_buf = SpecialTypes[pch::SPECIAL_TYPE_sigjmp_buf]) {
2039 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002040 if (Sigjmp_bufType.isNull()) {
2041 Error("sigjmp_buf type is NULL");
2042 return;
2043 }
John McCall183700f2009-09-21 23:43:11 +00002044 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
Mike Stump782fa302009-07-28 02:25:19 +00002045 Context->setsigjmp_bufDecl(Typedef->getDecl());
2046 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002047 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002048 assert(Tag && "Invalid sigjmp_buf type in AST file");
Mike Stump782fa302009-07-28 02:25:19 +00002049 Context->setsigjmp_bufDecl(Tag->getDecl());
2050 }
2051 }
Mike Stump1eb44332009-09-09 15:08:12 +00002052 if (unsigned ObjCIdRedef
Douglas Gregord1571ac2009-08-21 00:27:50 +00002053 = SpecialTypes[pch::SPECIAL_TYPE_OBJC_ID_REDEFINITION])
2054 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
Mike Stump1eb44332009-09-09 15:08:12 +00002055 if (unsigned ObjCClassRedef
Douglas Gregord1571ac2009-08-21 00:27:50 +00002056 = SpecialTypes[pch::SPECIAL_TYPE_OBJC_CLASS_REDEFINITION])
2057 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
Mike Stumpadaaad32009-10-20 02:12:22 +00002058 if (unsigned String = SpecialTypes[pch::SPECIAL_TYPE_BLOCK_DESCRIPTOR])
2059 Context->setBlockDescriptorType(GetType(String));
Mike Stump083c25e2009-10-22 00:49:09 +00002060 if (unsigned String
2061 = SpecialTypes[pch::SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR])
2062 Context->setBlockDescriptorExtendedType(GetType(String));
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002063 if (unsigned ObjCSelRedef
2064 = SpecialTypes[pch::SPECIAL_TYPE_OBJC_SEL_REDEFINITION])
2065 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
2066 if (unsigned String = SpecialTypes[pch::SPECIAL_TYPE_NS_CONSTANT_STRING])
2067 Context->setNSConstantStringType(GetType(String));
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002068
2069 if (SpecialTypes[pch::SPECIAL_TYPE_INT128_INSTALLED])
2070 Context->setInt128Installed();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002071}
2072
Douglas Gregorb64c1932009-05-12 01:31:05 +00002073/// \brief Retrieve the name of the original source file name
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002074/// directly from the AST file, without actually loading the AST
Douglas Gregorb64c1932009-05-12 01:31:05 +00002075/// file.
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002076std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00002077 Diagnostic &Diags) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002078 // Open the AST file.
Douglas Gregorb64c1932009-05-12 01:31:05 +00002079 std::string ErrStr;
2080 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002081 Buffer.reset(llvm::MemoryBuffer::getFile(ASTFileName.c_str(), &ErrStr));
Douglas Gregorb64c1932009-05-12 01:31:05 +00002082 if (!Buffer) {
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00002083 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002084 return std::string();
2085 }
2086
2087 // Initialize the stream
2088 llvm::BitstreamReader StreamFile;
2089 llvm::BitstreamCursor Stream;
Mike Stump1eb44332009-09-09 15:08:12 +00002090 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregorb64c1932009-05-12 01:31:05 +00002091 (const unsigned char *)Buffer->getBufferEnd());
2092 Stream.init(StreamFile);
2093
2094 // Sniff for the signature.
2095 if (Stream.Read(8) != 'C' ||
2096 Stream.Read(8) != 'P' ||
2097 Stream.Read(8) != 'C' ||
2098 Stream.Read(8) != 'H') {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002099 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002100 return std::string();
2101 }
2102
2103 RecordData Record;
2104 while (!Stream.AtEndOfStream()) {
2105 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002106
Douglas Gregorb64c1932009-05-12 01:31:05 +00002107 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2108 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump1eb44332009-09-09 15:08:12 +00002109
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002110 // We only know the AST subblock ID.
Douglas Gregorb64c1932009-05-12 01:31:05 +00002111 switch (BlockID) {
Sebastian Redlf29f0a22010-08-18 23:57:22 +00002112 case pch::AST_BLOCK_ID:
2113 if (Stream.EnterSubBlock(pch::AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002114 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002115 return std::string();
2116 }
2117 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002118
Douglas Gregorb64c1932009-05-12 01:31:05 +00002119 default:
2120 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002121 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002122 return std::string();
2123 }
2124 break;
2125 }
2126 continue;
2127 }
2128
2129 if (Code == llvm::bitc::END_BLOCK) {
2130 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002131 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002132 return std::string();
2133 }
2134 continue;
2135 }
2136
2137 if (Code == llvm::bitc::DEFINE_ABBREV) {
2138 Stream.ReadAbbrevRecord();
2139 continue;
2140 }
2141
2142 Record.clear();
2143 const char *BlobStart = 0;
2144 unsigned BlobLen = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002145 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Douglas Gregorb64c1932009-05-12 01:31:05 +00002146 == pch::ORIGINAL_FILE_NAME)
2147 return std::string(BlobStart, BlobLen);
Mike Stump1eb44332009-09-09 15:08:12 +00002148 }
Douglas Gregorb64c1932009-05-12 01:31:05 +00002149
2150 return std::string();
2151}
2152
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002153/// \brief Parse the record that corresponds to a LangOptions data
2154/// structure.
2155///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002156/// This routine parses the language options from the AST file and then gives
2157/// them to the AST listener if one is set.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002158///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002159/// \returns true if the listener deems the file unacceptable, false otherwise.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002160bool ASTReader::ParseLanguageOptions(
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002161 const llvm::SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002162 if (Listener) {
2163 LangOptions LangOpts;
Mike Stump1eb44332009-09-09 15:08:12 +00002164
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002165 #define PARSE_LANGOPT(Option) \
2166 LangOpts.Option = Record[Idx]; \
2167 ++Idx
Mike Stump1eb44332009-09-09 15:08:12 +00002168
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002169 unsigned Idx = 0;
2170 PARSE_LANGOPT(Trigraphs);
2171 PARSE_LANGOPT(BCPLComment);
2172 PARSE_LANGOPT(DollarIdents);
2173 PARSE_LANGOPT(AsmPreprocessor);
2174 PARSE_LANGOPT(GNUMode);
Chandler Carrutheb5d7b72010-04-17 20:17:31 +00002175 PARSE_LANGOPT(GNUKeywords);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002176 PARSE_LANGOPT(ImplicitInt);
2177 PARSE_LANGOPT(Digraphs);
2178 PARSE_LANGOPT(HexFloats);
2179 PARSE_LANGOPT(C99);
2180 PARSE_LANGOPT(Microsoft);
2181 PARSE_LANGOPT(CPlusPlus);
2182 PARSE_LANGOPT(CPlusPlus0x);
2183 PARSE_LANGOPT(CXXOperatorNames);
2184 PARSE_LANGOPT(ObjC1);
2185 PARSE_LANGOPT(ObjC2);
2186 PARSE_LANGOPT(ObjCNonFragileABI);
Fariborz Jahanian412e7982010-02-09 19:31:38 +00002187 PARSE_LANGOPT(ObjCNonFragileABI2);
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +00002188 PARSE_LANGOPT(NoConstantCFStrings);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002189 PARSE_LANGOPT(PascalStrings);
2190 PARSE_LANGOPT(WritableStrings);
2191 PARSE_LANGOPT(LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +00002192 PARSE_LANGOPT(AltiVec);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002193 PARSE_LANGOPT(Exceptions);
Daniel Dunbar73482882010-02-10 18:48:44 +00002194 PARSE_LANGOPT(SjLjExceptions);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002195 PARSE_LANGOPT(NeXTRuntime);
2196 PARSE_LANGOPT(Freestanding);
2197 PARSE_LANGOPT(NoBuiltin);
2198 PARSE_LANGOPT(ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +00002199 PARSE_LANGOPT(POSIXThreads);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002200 PARSE_LANGOPT(Blocks);
2201 PARSE_LANGOPT(EmitAllDecls);
2202 PARSE_LANGOPT(MathErrno);
Chris Lattnera4d71452010-06-26 21:25:03 +00002203 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
2204 Record[Idx++]);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002205 PARSE_LANGOPT(HeinousExtensions);
2206 PARSE_LANGOPT(Optimize);
2207 PARSE_LANGOPT(OptimizeSize);
2208 PARSE_LANGOPT(Static);
2209 PARSE_LANGOPT(PICLevel);
2210 PARSE_LANGOPT(GNUInline);
2211 PARSE_LANGOPT(NoInline);
2212 PARSE_LANGOPT(AccessControl);
2213 PARSE_LANGOPT(CharIsSigned);
John Thompsona6fda122009-11-05 20:14:16 +00002214 PARSE_LANGOPT(ShortWChar);
Chris Lattnera4d71452010-06-26 21:25:03 +00002215 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
2216 LangOpts.setVisibilityMode((LangOptions::VisibilityMode)Record[Idx++]);
Daniel Dunbarab8e2812009-09-21 04:16:19 +00002217 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
Chris Lattnera4d71452010-06-26 21:25:03 +00002218 Record[Idx++]);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002219 PARSE_LANGOPT(InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +00002220 PARSE_LANGOPT(OpenCL);
Mike Stump9c276ae2009-12-12 01:27:46 +00002221 PARSE_LANGOPT(CatchUndefined);
2222 // FIXME: Missing ElideConstructors?!
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002223 #undef PARSE_LANGOPT
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002224
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002225 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002226 }
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002227
2228 return false;
2229}
2230
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002231void ASTReader::ReadPreprocessedEntities() {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002232 ReadDefinedMacros();
2233}
2234
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002235/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002236ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002237 PerFileData *F = 0;
2238 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2239 F = Chain[N - I - 1];
2240 if (Index < F->LocalNumTypes)
2241 break;
2242 Index -= F->LocalNumTypes;
2243 }
2244 assert(F && F->LocalNumTypes > Index && "Broken chain");
Sebastian Redl971dd442010-07-20 22:55:31 +00002245 return RecordLocation(&F->DeclsCursor, F->TypeOffsets[Index]);
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002246}
2247
2248/// \brief Read and return the type with the given index..
Douglas Gregor2cf26342009-04-09 22:27:44 +00002249///
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002250/// The index is the type ID, shifted and minus the number of predefs. This
2251/// routine actually reads the record corresponding to the type at the given
2252/// location. It is a helper routine for GetType, which deals with reading type
2253/// IDs.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002254QualType ASTReader::ReadTypeRecord(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002255 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redl971dd442010-07-20 22:55:31 +00002256 llvm::BitstreamCursor &DeclsCursor = *Loc.first;
Sebastian Redl9137a522010-07-16 17:50:48 +00002257
Douglas Gregor0b748912009-04-14 21:18:50 +00002258 // Keep track of where we are in the stream, then jump back there
2259 // after reading this type.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002260 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregor0b748912009-04-14 21:18:50 +00002261
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00002262 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redl27372b42010-08-11 18:52:41 +00002263
Douglas Gregord89275b2009-07-06 18:54:52 +00002264 // Note that we are loading a type record.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00002265 Deserializing AType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00002266
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002267 DeclsCursor.JumpToBit(Loc.second);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002268 RecordData Record;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002269 unsigned Code = DeclsCursor.ReadCode();
2270 switch ((pch::TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
Douglas Gregor6d473962009-04-15 22:00:08 +00002271 case pch::TYPE_EXT_QUAL: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002272 if (Record.size() != 2) {
2273 Error("Incorrect encoding of extended qualifier type");
2274 return QualType();
2275 }
Douglas Gregor6d473962009-04-15 22:00:08 +00002276 QualType Base = GetType(Record[0]);
John McCall0953e762009-09-24 19:53:00 +00002277 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]);
2278 return Context->getQualifiedType(Base, Quals);
Douglas Gregor6d473962009-04-15 22:00:08 +00002279 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002280
Douglas Gregor2cf26342009-04-09 22:27:44 +00002281 case pch::TYPE_COMPLEX: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002282 if (Record.size() != 1) {
2283 Error("Incorrect encoding of complex type");
2284 return QualType();
2285 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002286 QualType ElemType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002287 return Context->getComplexType(ElemType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002288 }
2289
2290 case pch::TYPE_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002291 if (Record.size() != 1) {
2292 Error("Incorrect encoding of pointer type");
2293 return QualType();
2294 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002295 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002296 return Context->getPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002297 }
2298
2299 case pch::TYPE_BLOCK_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002300 if (Record.size() != 1) {
2301 Error("Incorrect encoding of block pointer type");
2302 return QualType();
2303 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002304 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002305 return Context->getBlockPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002306 }
2307
2308 case pch::TYPE_LVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002309 if (Record.size() != 1) {
2310 Error("Incorrect encoding of lvalue reference type");
2311 return QualType();
2312 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002313 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002314 return Context->getLValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002315 }
2316
2317 case pch::TYPE_RVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002318 if (Record.size() != 1) {
2319 Error("Incorrect encoding of rvalue reference type");
2320 return QualType();
2321 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002322 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002323 return Context->getRValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002324 }
2325
2326 case pch::TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidis240437b2010-07-02 11:55:15 +00002327 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002328 Error("Incorrect encoding of member pointer type");
2329 return QualType();
2330 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002331 QualType PointeeType = GetType(Record[0]);
2332 QualType ClassType = GetType(Record[1]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002333 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002334 }
2335
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002336 case pch::TYPE_CONSTANT_ARRAY: {
2337 QualType ElementType = GetType(Record[0]);
2338 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2339 unsigned IndexTypeQuals = Record[2];
2340 unsigned Idx = 3;
2341 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002342 return Context->getConstantArrayType(ElementType, Size,
2343 ASM, IndexTypeQuals);
2344 }
2345
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002346 case pch::TYPE_INCOMPLETE_ARRAY: {
2347 QualType ElementType = GetType(Record[0]);
2348 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2349 unsigned IndexTypeQuals = Record[2];
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002350 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002351 }
2352
2353 case pch::TYPE_VARIABLE_ARRAY: {
Douglas Gregor0b748912009-04-14 21:18:50 +00002354 QualType ElementType = GetType(Record[0]);
2355 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2356 unsigned IndexTypeQuals = Record[2];
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002357 SourceLocation LBLoc = SourceLocation::getFromRawEncoding(Record[3]);
2358 SourceLocation RBLoc = SourceLocation::getFromRawEncoding(Record[4]);
Sebastian Redl577d4792010-07-22 22:43:28 +00002359 return Context->getVariableArrayType(ElementType, ReadExpr(DeclsCursor),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002360 ASM, IndexTypeQuals,
2361 SourceRange(LBLoc, RBLoc));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002362 }
2363
2364 case pch::TYPE_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00002365 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002366 Error("incorrect encoding of vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002367 return QualType();
2368 }
2369
2370 QualType ElementType = GetType(Record[0]);
2371 unsigned NumElements = Record[1];
Chris Lattner788b0fd2010-06-23 06:00:24 +00002372 unsigned AltiVecSpec = Record[2];
2373 return Context->getVectorType(ElementType, NumElements,
2374 (VectorType::AltiVecSpecific)AltiVecSpec);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002375 }
2376
2377 case pch::TYPE_EXT_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00002378 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002379 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002380 return QualType();
2381 }
2382
2383 QualType ElementType = GetType(Record[0]);
2384 unsigned NumElements = Record[1];
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002385 return Context->getExtVectorType(ElementType, NumElements);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002386 }
2387
2388 case pch::TYPE_FUNCTION_NO_PROTO: {
Rafael Espindola425ef722010-03-30 22:15:11 +00002389 if (Record.size() != 4) {
Douglas Gregora02b1472009-04-28 21:53:25 +00002390 Error("incorrect encoding of no-proto function type");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002391 return QualType();
2392 }
2393 QualType ResultType = GetType(Record[0]);
Rafael Espindola425ef722010-03-30 22:15:11 +00002394 FunctionType::ExtInfo Info(Record[1], Record[2], (CallingConv)Record[3]);
Rafael Espindola264ba482010-03-30 20:24:48 +00002395 return Context->getFunctionNoProtoType(ResultType, Info);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002396 }
2397
2398 case pch::TYPE_FUNCTION_PROTO: {
2399 QualType ResultType = GetType(Record[0]);
Douglas Gregor91236662009-12-22 18:11:50 +00002400 bool NoReturn = Record[1];
Rafael Espindola425ef722010-03-30 22:15:11 +00002401 unsigned RegParm = Record[2];
2402 CallingConv CallConv = (CallingConv)Record[3];
2403 unsigned Idx = 4;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002404 unsigned NumParams = Record[Idx++];
2405 llvm::SmallVector<QualType, 16> ParamTypes;
2406 for (unsigned I = 0; I != NumParams; ++I)
2407 ParamTypes.push_back(GetType(Record[Idx++]));
2408 bool isVariadic = Record[Idx++];
2409 unsigned Quals = Record[Idx++];
Sebastian Redl465226e2009-05-27 22:11:52 +00002410 bool hasExceptionSpec = Record[Idx++];
2411 bool hasAnyExceptionSpec = Record[Idx++];
2412 unsigned NumExceptions = Record[Idx++];
2413 llvm::SmallVector<QualType, 2> Exceptions;
2414 for (unsigned I = 0; I != NumExceptions; ++I)
2415 Exceptions.push_back(GetType(Record[Idx++]));
Jay Foadbeaaccd2009-05-21 09:52:38 +00002416 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
Sebastian Redl465226e2009-05-27 22:11:52 +00002417 isVariadic, Quals, hasExceptionSpec,
2418 hasAnyExceptionSpec, NumExceptions,
Rafael Espindola264ba482010-03-30 20:24:48 +00002419 Exceptions.data(),
Rafael Espindola425ef722010-03-30 22:15:11 +00002420 FunctionType::ExtInfo(NoReturn, RegParm,
2421 CallConv));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002422 }
2423
John McCalled976492009-12-04 22:46:56 +00002424 case pch::TYPE_UNRESOLVED_USING:
2425 return Context->getTypeDeclType(
2426 cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0])));
2427
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002428 case pch::TYPE_TYPEDEF: {
2429 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002430 Error("incorrect encoding of typedef type");
2431 return QualType();
2432 }
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002433 TypedefDecl *Decl = cast<TypedefDecl>(GetDecl(Record[0]));
2434 QualType Canonical = GetType(Record[1]);
2435 return Context->getTypedefType(Decl, Canonical);
2436 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002437
2438 case pch::TYPE_TYPEOF_EXPR:
Sebastian Redl577d4792010-07-22 22:43:28 +00002439 return Context->getTypeOfExprType(ReadExpr(DeclsCursor));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002440
2441 case pch::TYPE_TYPEOF: {
2442 if (Record.size() != 1) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002443 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002444 return QualType();
2445 }
2446 QualType UnderlyingType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002447 return Context->getTypeOfType(UnderlyingType);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002448 }
Mike Stump1eb44332009-09-09 15:08:12 +00002449
Anders Carlsson395b4752009-06-24 19:06:50 +00002450 case pch::TYPE_DECLTYPE:
Sebastian Redl577d4792010-07-22 22:43:28 +00002451 return Context->getDecltypeType(ReadExpr(DeclsCursor));
Anders Carlsson395b4752009-06-24 19:06:50 +00002452
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002453 case pch::TYPE_RECORD: {
2454 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002455 Error("incorrect encoding of record type");
2456 return QualType();
2457 }
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002458 bool IsDependent = Record[0];
2459 QualType T = Context->getRecordType(cast<RecordDecl>(GetDecl(Record[1])));
2460 T->Dependent = IsDependent;
2461 return T;
2462 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002463
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002464 case pch::TYPE_ENUM: {
2465 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002466 Error("incorrect encoding of enum type");
2467 return QualType();
2468 }
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002469 bool IsDependent = Record[0];
2470 QualType T = Context->getEnumType(cast<EnumDecl>(GetDecl(Record[1])));
2471 T->Dependent = IsDependent;
2472 return T;
2473 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002474
John McCall7da24312009-09-05 00:15:47 +00002475 case pch::TYPE_ELABORATED: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00002476 unsigned Idx = 0;
2477 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2478 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2479 QualType NamedType = GetType(Record[Idx++]);
2480 return Context->getElaboratedType(Keyword, NNS, NamedType);
John McCall7da24312009-09-05 00:15:47 +00002481 }
2482
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002483 case pch::TYPE_OBJC_INTERFACE: {
Chris Lattnerc6fa4452009-04-22 06:45:28 +00002484 unsigned Idx = 0;
2485 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
John McCallc12c5bb2010-05-15 11:32:37 +00002486 return Context->getObjCInterfaceType(ItfD);
2487 }
2488
2489 case pch::TYPE_OBJC_OBJECT: {
2490 unsigned Idx = 0;
2491 QualType Base = GetType(Record[Idx++]);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00002492 unsigned NumProtos = Record[Idx++];
2493 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2494 for (unsigned I = 0; I != NumProtos; ++I)
2495 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
John McCallc12c5bb2010-05-15 11:32:37 +00002496 return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00002497 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002498
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002499 case pch::TYPE_OBJC_OBJECT_POINTER: {
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00002500 unsigned Idx = 0;
John McCallc12c5bb2010-05-15 11:32:37 +00002501 QualType Pointee = GetType(Record[Idx++]);
2502 return Context->getObjCObjectPointerType(Pointee);
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00002503 }
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +00002504
John McCall49a832b2009-10-18 09:09:24 +00002505 case pch::TYPE_SUBST_TEMPLATE_TYPE_PARM: {
2506 unsigned Idx = 0;
2507 QualType Parm = GetType(Record[Idx++]);
2508 QualType Replacement = GetType(Record[Idx++]);
2509 return
2510 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
2511 Replacement);
2512 }
John McCall3cb0ebd2010-03-10 03:28:59 +00002513
2514 case pch::TYPE_INJECTED_CLASS_NAME: {
2515 CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0]));
2516 QualType TST = GetType(Record[1]); // probably derivable
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00002517 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002518 // for AST reading, too much interdependencies.
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00002519 return
2520 QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCall3cb0ebd2010-03-10 03:28:59 +00002521 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00002522
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002523 case pch::TYPE_TEMPLATE_TYPE_PARM: {
2524 unsigned Idx = 0;
2525 unsigned Depth = Record[Idx++];
2526 unsigned Index = Record[Idx++];
2527 bool Pack = Record[Idx++];
2528 IdentifierInfo *Name = GetIdentifierInfo(Record, Idx);
2529 return Context->getTemplateTypeParmType(Depth, Index, Pack, Name);
2530 }
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00002531
2532 case pch::TYPE_DEPENDENT_NAME: {
2533 unsigned Idx = 0;
2534 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2535 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2536 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +00002537 QualType Canon = GetType(Record[Idx++]);
2538 return Context->getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00002539 }
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00002540
2541 case pch::TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
2542 unsigned Idx = 0;
2543 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2544 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2545 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
2546 unsigned NumArgs = Record[Idx++];
2547 llvm::SmallVector<TemplateArgument, 8> Args;
2548 Args.reserve(NumArgs);
2549 while (NumArgs--)
Sebastian Redl577d4792010-07-22 22:43:28 +00002550 Args.push_back(ReadTemplateArgument(DeclsCursor, Record, Idx));
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00002551 return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
2552 Args.size(), Args.data());
2553 }
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00002554
2555 case pch::TYPE_DEPENDENT_SIZED_ARRAY: {
2556 unsigned Idx = 0;
2557
2558 // ArrayType
2559 QualType ElementType = GetType(Record[Idx++]);
2560 ArrayType::ArraySizeModifier ASM
2561 = (ArrayType::ArraySizeModifier)Record[Idx++];
2562 unsigned IndexTypeQuals = Record[Idx++];
2563
2564 // DependentSizedArrayType
Sebastian Redl577d4792010-07-22 22:43:28 +00002565 Expr *NumElts = ReadExpr(DeclsCursor);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00002566 SourceRange Brackets = ReadSourceRange(Record, Idx);
2567
2568 return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
2569 IndexTypeQuals, Brackets);
2570 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00002571
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002572 case pch::TYPE_TEMPLATE_SPECIALIZATION: {
2573 unsigned Idx = 0;
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002574 bool IsDependent = Record[Idx++];
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002575 TemplateName Name = ReadTemplateName(Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002576 llvm::SmallVector<TemplateArgument, 8> Args;
Sebastian Redl577d4792010-07-22 22:43:28 +00002577 ReadTemplateArgumentList(Args, DeclsCursor, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00002578 QualType Canon = GetType(Record[Idx++]);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002579 QualType T;
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002580 if (Canon.isNull())
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002581 T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(),
2582 Args.size());
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002583 else
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002584 T = Context->getTemplateSpecializationType(Name, Args.data(),
2585 Args.size(), Canon);
2586 T->Dependent = IsDependent;
2587 return T;
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002588 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002589 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002590 // Suppress a GCC warning
2591 return QualType();
2592}
2593
John McCalla1ee0c52009-10-16 21:56:05 +00002594namespace {
2595
2596class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002597 ASTReader &Reader;
Sebastian Redl577d4792010-07-22 22:43:28 +00002598 llvm::BitstreamCursor &DeclsCursor;
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002599 const ASTReader::RecordData &Record;
John McCalla1ee0c52009-10-16 21:56:05 +00002600 unsigned &Idx;
2601
2602public:
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002603 TypeLocReader(ASTReader &Reader, llvm::BitstreamCursor &Cursor,
2604 const ASTReader::RecordData &Record, unsigned &Idx)
Sebastian Redl577d4792010-07-22 22:43:28 +00002605 : Reader(Reader), DeclsCursor(Cursor), Record(Record), Idx(Idx) { }
John McCalla1ee0c52009-10-16 21:56:05 +00002606
John McCall51bd8032009-10-18 01:05:36 +00002607 // We want compile-time assurance that we've enumerated all of
2608 // these, so unfortunately we have to declare them first, then
2609 // define them out-of-line.
2610#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +00002611#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +00002612 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00002613#include "clang/AST/TypeLocNodes.def"
2614
John McCall51bd8032009-10-18 01:05:36 +00002615 void VisitFunctionTypeLoc(FunctionTypeLoc);
2616 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00002617};
2618
2619}
2620
John McCall51bd8032009-10-18 01:05:36 +00002621void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCalla1ee0c52009-10-16 21:56:05 +00002622 // nothing to do
2623}
John McCall51bd8032009-10-18 01:05:36 +00002624void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +00002625 TL.setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2626 if (TL.needsExtraLocalData()) {
2627 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
2628 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
2629 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
2630 TL.setModeAttr(Record[Idx++]);
2631 }
John McCalla1ee0c52009-10-16 21:56:05 +00002632}
John McCall51bd8032009-10-18 01:05:36 +00002633void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
2634 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002635}
John McCall51bd8032009-10-18 01:05:36 +00002636void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
2637 TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002638}
John McCall51bd8032009-10-18 01:05:36 +00002639void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
2640 TL.setCaretLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002641}
John McCall51bd8032009-10-18 01:05:36 +00002642void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
2643 TL.setAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002644}
John McCall51bd8032009-10-18 01:05:36 +00002645void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
2646 TL.setAmpAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002647}
John McCall51bd8032009-10-18 01:05:36 +00002648void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
2649 TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002650}
John McCall51bd8032009-10-18 01:05:36 +00002651void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
2652 TL.setLBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2653 TL.setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002654 if (Record[Idx++])
Sebastian Redl577d4792010-07-22 22:43:28 +00002655 TL.setSizeExpr(Reader.ReadExpr(DeclsCursor));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002656 else
John McCall51bd8032009-10-18 01:05:36 +00002657 TL.setSizeExpr(0);
2658}
2659void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
2660 VisitArrayTypeLoc(TL);
2661}
2662void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
2663 VisitArrayTypeLoc(TL);
2664}
2665void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
2666 VisitArrayTypeLoc(TL);
2667}
2668void TypeLocReader::VisitDependentSizedArrayTypeLoc(
2669 DependentSizedArrayTypeLoc TL) {
2670 VisitArrayTypeLoc(TL);
2671}
2672void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
2673 DependentSizedExtVectorTypeLoc TL) {
2674 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2675}
2676void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
2677 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2678}
2679void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
2680 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2681}
2682void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
2683 TL.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2684 TL.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2685 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
John McCall86acc2a2009-10-23 01:28:53 +00002686 TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
John McCall51bd8032009-10-18 01:05:36 +00002687 }
2688}
2689void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
2690 VisitFunctionTypeLoc(TL);
2691}
2692void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
2693 VisitFunctionTypeLoc(TL);
2694}
John McCalled976492009-12-04 22:46:56 +00002695void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
2696 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2697}
John McCall51bd8032009-10-18 01:05:36 +00002698void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
2699 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2700}
2701void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +00002702 TL.setTypeofLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2703 TL.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2704 TL.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall51bd8032009-10-18 01:05:36 +00002705}
2706void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +00002707 TL.setTypeofLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2708 TL.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2709 TL.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Sebastian Redl577d4792010-07-22 22:43:28 +00002710 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(DeclsCursor, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00002711}
2712void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
2713 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2714}
2715void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
2716 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2717}
2718void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
2719 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2720}
John McCall51bd8032009-10-18 01:05:36 +00002721void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
2722 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2723}
John McCall49a832b2009-10-18 09:09:24 +00002724void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
2725 SubstTemplateTypeParmTypeLoc TL) {
2726 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2727}
John McCall51bd8032009-10-18 01:05:36 +00002728void TypeLocReader::VisitTemplateSpecializationTypeLoc(
2729 TemplateSpecializationTypeLoc TL) {
John McCall833ca992009-10-29 08:12:44 +00002730 TL.setTemplateNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2731 TL.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2732 TL.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2733 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
2734 TL.setArgLocInfo(i,
2735 Reader.GetTemplateArgumentLocInfo(TL.getTypePtr()->getArg(i).getKind(),
Sebastian Redl577d4792010-07-22 22:43:28 +00002736 DeclsCursor, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00002737}
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002738void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002739 TL.setKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2740 TL.setQualifierRange(Reader.ReadSourceRange(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00002741}
John McCall3cb0ebd2010-03-10 03:28:59 +00002742void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
2743 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2744}
Douglas Gregor4714c122010-03-31 17:34:00 +00002745void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002746 TL.setKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2747 TL.setQualifierRange(Reader.ReadSourceRange(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00002748 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2749}
John McCall33500952010-06-11 00:33:02 +00002750void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
2751 DependentTemplateSpecializationTypeLoc TL) {
2752 TL.setKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2753 TL.setQualifierRange(Reader.ReadSourceRange(Record, Idx));
2754 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2755 TL.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2756 TL.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2757 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
2758 TL.setArgLocInfo(I,
2759 Reader.GetTemplateArgumentLocInfo(TL.getTypePtr()->getArg(I).getKind(),
Sebastian Redl577d4792010-07-22 22:43:28 +00002760 DeclsCursor, Record, Idx));
John McCall33500952010-06-11 00:33:02 +00002761}
John McCall51bd8032009-10-18 01:05:36 +00002762void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
2763 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCallc12c5bb2010-05-15 11:32:37 +00002764}
2765void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
2766 TL.setHasBaseTypeAsWritten(Record[Idx++]);
John McCall51bd8032009-10-18 01:05:36 +00002767 TL.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2768 TL.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2769 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
2770 TL.setProtocolLoc(i, SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002771}
John McCall54e14c42009-10-22 22:37:11 +00002772void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
2773 TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall54e14c42009-10-22 22:37:11 +00002774}
John McCalla1ee0c52009-10-16 21:56:05 +00002775
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002776TypeSourceInfo *ASTReader::GetTypeSourceInfo(llvm::BitstreamCursor &DeclsCursor,
Sebastian Redl577d4792010-07-22 22:43:28 +00002777 const RecordData &Record,
John McCalla1ee0c52009-10-16 21:56:05 +00002778 unsigned &Idx) {
2779 QualType InfoTy = GetType(Record[Idx++]);
2780 if (InfoTy.isNull())
2781 return 0;
2782
John McCalla93c9342009-12-07 02:54:59 +00002783 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
Sebastian Redl577d4792010-07-22 22:43:28 +00002784 TypeLocReader TLR(*this, DeclsCursor, Record, Idx);
John McCalla93c9342009-12-07 02:54:59 +00002785 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCalla1ee0c52009-10-16 21:56:05 +00002786 TLR.Visit(TL);
John McCalla93c9342009-12-07 02:54:59 +00002787 return TInfo;
John McCalla1ee0c52009-10-16 21:56:05 +00002788}
Douglas Gregor2cf26342009-04-09 22:27:44 +00002789
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002790QualType ASTReader::GetType(pch::TypeID ID) {
John McCall0953e762009-09-24 19:53:00 +00002791 unsigned FastQuals = ID & Qualifiers::FastMask;
2792 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002793
2794 if (Index < pch::NUM_PREDEF_TYPE_IDS) {
2795 QualType T;
2796 switch ((pch::PredefinedTypeIDs)Index) {
2797 case pch::PREDEF_TYPE_NULL_ID: return QualType();
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002798 case pch::PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
2799 case pch::PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002800
2801 case pch::PREDEF_TYPE_CHAR_U_ID:
2802 case pch::PREDEF_TYPE_CHAR_S_ID:
2803 // FIXME: Check that the signedness of CharTy is correct!
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002804 T = Context->CharTy;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002805 break;
2806
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002807 case pch::PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break;
2808 case pch::PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break;
2809 case pch::PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break;
2810 case pch::PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break;
2811 case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002812 case pch::PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break;
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002813 case pch::PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break;
2814 case pch::PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break;
2815 case pch::PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break;
2816 case pch::PREDEF_TYPE_INT_ID: T = Context->IntTy; break;
2817 case pch::PREDEF_TYPE_LONG_ID: T = Context->LongTy; break;
2818 case pch::PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002819 case pch::PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break;
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002820 case pch::PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break;
2821 case pch::PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break;
2822 case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break;
2823 case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break;
2824 case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002825 case pch::PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002826 case pch::PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break;
2827 case pch::PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break;
Steve Naroffde2e22d2009-07-15 18:40:39 +00002828 case pch::PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break;
2829 case pch::PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00002830 case pch::PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002831 }
2832
2833 assert(!T.isNull() && "Unknown predefined type");
John McCall0953e762009-09-24 19:53:00 +00002834 return T.withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002835 }
2836
2837 Index -= pch::NUM_PREDEF_TYPE_IDS;
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002838 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl07a353c2010-07-14 20:26:45 +00002839 if (TypesLoaded[Index].isNull()) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002840 TypesLoaded[Index] = ReadTypeRecord(Index);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002841 TypesLoaded[Index]->setFromAST();
Sebastian Redl30c514c2010-07-14 23:45:08 +00002842 if (DeserializationListener)
Sebastian Redl1476ed42010-07-16 16:36:56 +00002843 DeserializationListener->TypeRead(ID >> Qualifiers::FastWidth,
2844 TypesLoaded[Index]);
Sebastian Redl07a353c2010-07-14 20:26:45 +00002845 }
Mike Stump1eb44332009-09-09 15:08:12 +00002846
John McCall0953e762009-09-24 19:53:00 +00002847 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002848}
2849
John McCall833ca992009-10-29 08:12:44 +00002850TemplateArgumentLocInfo
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002851ASTReader::GetTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Sebastian Redl577d4792010-07-22 22:43:28 +00002852 llvm::BitstreamCursor &DeclsCursor,
John McCall833ca992009-10-29 08:12:44 +00002853 const RecordData &Record,
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00002854 unsigned &Index) {
John McCall833ca992009-10-29 08:12:44 +00002855 switch (Kind) {
2856 case TemplateArgument::Expression:
Sebastian Redl577d4792010-07-22 22:43:28 +00002857 return ReadExpr(DeclsCursor);
John McCall833ca992009-10-29 08:12:44 +00002858 case TemplateArgument::Type:
Sebastian Redl577d4792010-07-22 22:43:28 +00002859 return GetTypeSourceInfo(DeclsCursor, Record, Index);
Douglas Gregor788cd062009-11-11 01:00:40 +00002860 case TemplateArgument::Template: {
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00002861 SourceRange QualifierRange = ReadSourceRange(Record, Index);
2862 SourceLocation TemplateNameLoc = ReadSourceLocation(Record, Index);
2863 return TemplateArgumentLocInfo(QualifierRange, TemplateNameLoc);
Douglas Gregor788cd062009-11-11 01:00:40 +00002864 }
John McCall833ca992009-10-29 08:12:44 +00002865 case TemplateArgument::Null:
2866 case TemplateArgument::Integral:
2867 case TemplateArgument::Declaration:
2868 case TemplateArgument::Pack:
2869 return TemplateArgumentLocInfo();
2870 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002871 llvm_unreachable("unexpected template argument loc");
John McCall833ca992009-10-29 08:12:44 +00002872 return TemplateArgumentLocInfo();
2873}
2874
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00002875TemplateArgumentLoc
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002876ASTReader::ReadTemplateArgumentLoc(llvm::BitstreamCursor &DeclsCursor,
Sebastian Redl577d4792010-07-22 22:43:28 +00002877 const RecordData &Record, unsigned &Index) {
2878 TemplateArgument Arg = ReadTemplateArgument(DeclsCursor, Record, Index);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00002879
2880 if (Arg.getKind() == TemplateArgument::Expression) {
2881 if (Record[Index++]) // bool InfoHasSameExpr.
2882 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
2883 }
2884 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(Arg.getKind(),
Sebastian Redl577d4792010-07-22 22:43:28 +00002885 DeclsCursor,
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00002886 Record, Index));
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002887}
2888
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002889Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall76bd1f32010-06-01 09:23:16 +00002890 return GetDecl(ID);
2891}
2892
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002893TranslationUnitDecl *ASTReader::GetTranslationUnitDecl() {
Sebastian Redl30c514c2010-07-14 23:45:08 +00002894 if (!DeclsLoaded[0]) {
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00002895 ReadDeclRecord(0, 0);
Sebastian Redl30c514c2010-07-14 23:45:08 +00002896 if (DeserializationListener)
Sebastian Redl1476ed42010-07-16 16:36:56 +00002897 DeserializationListener->DeclRead(1, DeclsLoaded[0]);
Sebastian Redl30c514c2010-07-14 23:45:08 +00002898 }
Argyrios Kyrtzidis8871a442010-07-08 17:13:02 +00002899
2900 return cast<TranslationUnitDecl>(DeclsLoaded[0]);
2901}
2902
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002903Decl *ASTReader::GetDecl(pch::DeclID ID) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002904 if (ID == 0)
2905 return 0;
2906
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002907 if (ID > DeclsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002908 Error("declaration ID out-of-range for AST file");
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002909 return 0;
2910 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002911
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002912 unsigned Index = ID - 1;
Sebastian Redl30c514c2010-07-14 23:45:08 +00002913 if (!DeclsLoaded[Index]) {
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00002914 ReadDeclRecord(Index, ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00002915 if (DeserializationListener)
2916 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
2917 }
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002918
2919 return DeclsLoaded[Index];
Douglas Gregor2cf26342009-04-09 22:27:44 +00002920}
2921
Chris Lattner887e2b32009-04-27 05:46:25 +00002922/// \brief Resolve the offset of a statement into a statement.
2923///
2924/// This operation will read a new statement from the external
2925/// source each time it is called, and is meant to be used via a
2926/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002927Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00002928 // Offset here is a global offset across the entire chain.
2929 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2930 PerFileData &F = *Chain[N - I - 1];
2931 if (Offset < F.SizeInBits) {
2932 // Since we know that this statement is part of a decl, make sure to use
2933 // the decl cursor to read it.
2934 F.DeclsCursor.JumpToBit(Offset);
2935 return ReadStmtFromStream(F.DeclsCursor);
2936 }
2937 Offset -= F.SizeInBits;
2938 }
2939 llvm_unreachable("Broken chain");
Douglas Gregor250fc9c2009-04-18 00:07:54 +00002940}
2941
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002942bool ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +00002943 llvm::SmallVectorImpl<Decl*> &Decls) {
Mike Stump1eb44332009-09-09 15:08:12 +00002944 assert(DC->hasExternalLexicalStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00002945 "DeclContext has no lexical decls in storage");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002946
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00002947 // There might be lexical decls in multiple parts of the chain, for the TU
2948 // at least.
2949 DeclContextInfos &Infos = DeclContextOffsets[DC];
2950 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
2951 I != E; ++I) {
Sebastian Redl681d7232010-07-27 00:17:23 +00002952 // IDs can be 0 if this context doesn't contain declarations.
2953 if (!I->LexicalDecls)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00002954 continue;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00002955
2956 // Load all of the declaration IDs
Sebastian Redl681d7232010-07-27 00:17:23 +00002957 for (const pch::DeclID *ID = I->LexicalDecls,
2958 *IDE = ID + I->NumLexicalDecls;
2959 ID != IDE; ++ID)
2960 Decls.push_back(GetDecl(*ID));
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002961 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002962
Douglas Gregor25123082009-04-22 22:34:57 +00002963 ++NumLexicalDeclContextsRead;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002964 return false;
2965}
2966
John McCall76bd1f32010-06-01 09:23:16 +00002967DeclContext::lookup_result
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002968ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +00002969 DeclarationName Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00002970 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00002971 "DeclContext has no visible decls in storage");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002972
John McCall76bd1f32010-06-01 09:23:16 +00002973 llvm::SmallVector<VisibleDeclaration, 64> Decls;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00002974 // There might be lexical decls in multiple parts of the chain, for the TU
2975 // and namespaces.
2976 DeclContextInfos &Infos = DeclContextOffsets[DC];
2977 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
2978 I != E; ++I) {
2979 uint64_t Offset = I->OffsetToVisibleDecls;
2980 if (Offset == 0)
2981 continue;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002982
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00002983 llvm::BitstreamCursor &DeclsCursor = *I->Stream;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002984
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00002985 // Keep track of where we are in the stream, then jump back there
2986 // after reading this context.
2987 SavedStreamPosition SavedPosition(DeclsCursor);
2988
2989 // Load the record containing all of the declarations visible in
2990 // this context.
2991 DeclsCursor.JumpToBit(Offset);
2992 RecordData Record;
2993 unsigned Code = DeclsCursor.ReadCode();
2994 unsigned RecCode = DeclsCursor.ReadRecord(Code, Record);
2995 if (RecCode != pch::DECL_CONTEXT_VISIBLE) {
2996 Error("Expected visible block");
2997 return DeclContext::lookup_result(DeclContext::lookup_iterator(),
2998 DeclContext::lookup_iterator());
2999 }
3000
3001 if (Record.empty())
3002 continue;
3003
3004 unsigned Idx = 0;
3005 while (Idx < Record.size()) {
3006 Decls.push_back(VisibleDeclaration());
3007 Decls.back().Name = ReadDeclarationName(Record, Idx);
3008
3009 unsigned Size = Record[Idx++];
3010 llvm::SmallVector<unsigned, 4> &LoadedDecls = Decls.back().Declarations;
3011 LoadedDecls.reserve(Size);
3012 for (unsigned J = 0; J < Size; ++J)
3013 LoadedDecls.push_back(Record[Idx++]);
3014 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003015 }
3016
Douglas Gregor25123082009-04-22 22:34:57 +00003017 ++NumVisibleDeclContextsRead;
John McCall76bd1f32010-06-01 09:23:16 +00003018
3019 SetExternalVisibleDecls(DC, Decls);
3020 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003021}
3022
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003023void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003024 assert(Consumer);
3025 while (!InterestingDecls.empty()) {
3026 DeclGroupRef DG(InterestingDecls.front());
3027 InterestingDecls.pop_front();
Sebastian Redl27372b42010-08-11 18:52:41 +00003028 Consumer->HandleInterestingDecl(DG);
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003029 }
3030}
3031
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003032void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor0af2ca42009-04-22 19:09:20 +00003033 this->Consumer = Consumer;
3034
Douglas Gregorfdd01722009-04-14 00:24:19 +00003035 if (!Consumer)
3036 return;
3037
3038 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003039 // Force deserialization of this decl, which will cause it to be queued for
3040 // passing to the consumer.
Daniel Dunbar04a0b502009-09-17 03:06:44 +00003041 GetDecl(ExternalDefinitions[I]);
Douglas Gregorfdd01722009-04-14 00:24:19 +00003042 }
Douglas Gregorc62a2fe2009-04-25 00:41:30 +00003043
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003044 PassInterestingDeclsToConsumer();
Douglas Gregorfdd01722009-04-14 00:24:19 +00003045}
3046
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003047void ASTReader::PrintStats() {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003048 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregor2cf26342009-04-09 22:27:44 +00003049
Mike Stump1eb44332009-09-09 15:08:12 +00003050 unsigned NumTypesLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003051 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall0953e762009-09-24 19:53:00 +00003052 QualType());
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003053 unsigned NumDeclsLoaded
3054 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
3055 (Decl *)0);
3056 unsigned NumIdentifiersLoaded
3057 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
3058 IdentifiersLoaded.end(),
3059 (IdentifierInfo *)0);
Mike Stump1eb44332009-09-09 15:08:12 +00003060 unsigned NumSelectorsLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003061 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
3062 SelectorsLoaded.end(),
3063 Selector());
Douglas Gregor2d41cc12009-04-13 20:50:16 +00003064
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003065 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
3066 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00003067 if (TotalNumSLocEntries)
3068 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
3069 NumSLocEntriesRead, TotalNumSLocEntries,
3070 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003071 if (!TypesLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003072 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003073 NumTypesLoaded, (unsigned)TypesLoaded.size(),
3074 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
3075 if (!DeclsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003076 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003077 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
3078 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003079 if (!IdentifiersLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003080 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003081 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
3082 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redl725cd962010-08-04 20:40:17 +00003083 if (!SelectorsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003084 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redl725cd962010-08-04 20:40:17 +00003085 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
3086 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor83941df2009-04-25 17:48:32 +00003087 if (TotalNumStatements)
3088 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
3089 NumStatementsRead, TotalNumStatements,
3090 ((float)NumStatementsRead/TotalNumStatements * 100));
3091 if (TotalNumMacros)
3092 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
3093 NumMacrosRead, TotalNumMacros,
3094 ((float)NumMacrosRead/TotalNumMacros * 100));
3095 if (TotalLexicalDeclContexts)
3096 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
3097 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
3098 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
3099 * 100));
3100 if (TotalVisibleDeclContexts)
3101 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
3102 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
3103 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
3104 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003105 if (TotalNumMethodPoolEntries) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003106 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003107 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
3108 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor83941df2009-04-25 17:48:32 +00003109 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003110 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor83941df2009-04-25 17:48:32 +00003111 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003112 std::fprintf(stderr, "\n");
3113}
3114
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003115void ASTReader::InitializeSema(Sema &S) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00003116 SemaObj = &S;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003117 S.ExternalSource = this;
3118
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00003119 // Makes sure any declarations that were deserialized "too early"
3120 // still get added to the identifier's declaration chains.
Douglas Gregor914ed9d2010-08-13 03:15:25 +00003121 if (SemaObj->TUScope) {
3122 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
3123 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(PreloadedDecls[I]));
3124 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
3125 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00003126 }
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00003127 PreloadedDecls.clear();
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003128
3129 // If there were any tentative definitions, deserialize them and add
Sebastian Redle9d12b62010-01-31 22:27:38 +00003130 // them to Sema's list of tentative definitions.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003131 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
3132 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
Sebastian Redle9d12b62010-01-31 22:27:38 +00003133 SemaObj->TentativeDefinitions.push_back(Var);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003134 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00003135
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003136 // If there were any unused file scoped decls, deserialize them and add to
3137 // Sema's list of unused file scoped decls.
3138 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
3139 DeclaratorDecl *D = cast<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
3140 SemaObj->UnusedFileScopedDecls.push_back(D);
Tanya Lattnere6bbc012010-02-12 00:07:30 +00003141 }
Douglas Gregor14c22f22009-04-22 22:18:58 +00003142
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003143 // If there were any weak undeclared identifiers, deserialize them and add to
3144 // Sema's list of weak undeclared identifiers.
3145 if (!WeakUndeclaredIdentifiers.empty()) {
3146 unsigned Idx = 0;
3147 for (unsigned I = 0, N = WeakUndeclaredIdentifiers[Idx++]; I != N; ++I) {
3148 IdentifierInfo *WeakId = GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3149 IdentifierInfo *AliasId=GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3150 SourceLocation Loc = ReadSourceLocation(WeakUndeclaredIdentifiers, Idx);
3151 bool Used = WeakUndeclaredIdentifiers[Idx++];
3152 Sema::WeakInfo WI(AliasId, Loc);
3153 WI.setUsed(Used);
3154 SemaObj->WeakUndeclaredIdentifiers.insert(std::make_pair(WeakId, WI));
3155 }
3156 }
3157
Douglas Gregor14c22f22009-04-22 22:18:58 +00003158 // If there were any locally-scoped external declarations,
3159 // deserialize them and add them to Sema's table of locally-scoped
3160 // external declarations.
3161 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
3162 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
3163 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
3164 }
Douglas Gregorb81c1702009-04-27 20:06:05 +00003165
3166 // If there were any ext_vector type declarations, deserialize them
3167 // and add them to Sema's vector of such declarations.
3168 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I)
3169 SemaObj->ExtVectorDecls.push_back(
3170 cast<TypedefDecl>(GetDecl(ExtVectorDecls[I])));
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003171
3172 // FIXME: Do VTable uses and dynamic classes deserialize too much ?
3173 // Can we cut them down before writing them ?
3174
3175 // If there were any VTable uses, deserialize the information and add it
3176 // to Sema's vector and map of VTable uses.
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003177 if (!VTableUses.empty()) {
3178 unsigned Idx = 0;
3179 for (unsigned I = 0, N = VTableUses[Idx++]; I != N; ++I) {
3180 CXXRecordDecl *Class = cast<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
3181 SourceLocation Loc = ReadSourceLocation(VTableUses, Idx);
3182 bool DefinitionRequired = VTableUses[Idx++];
3183 SemaObj->VTableUses.push_back(std::make_pair(Class, Loc));
3184 SemaObj->VTablesUsed[Class] = DefinitionRequired;
3185 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003186 }
3187
3188 // If there were any dynamic classes declarations, deserialize them
3189 // and add them to Sema's vector of such declarations.
3190 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I)
3191 SemaObj->DynamicClasses.push_back(
3192 cast<CXXRecordDecl>(GetDecl(DynamicClasses[I])));
Fariborz Jahanian32019832010-07-23 19:11:11 +00003193
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003194 // If there were any pending implicit instantiations, deserialize them
3195 // and add them to Sema's queue of such instantiations.
3196 assert(PendingImplicitInstantiations.size() % 2 == 0 &&
3197 "Expected pairs of entries");
3198 for (unsigned Idx = 0, N = PendingImplicitInstantiations.size(); Idx < N;) {
3199 ValueDecl *D=cast<ValueDecl>(GetDecl(PendingImplicitInstantiations[Idx++]));
3200 SourceLocation Loc = ReadSourceLocation(PendingImplicitInstantiations, Idx);
3201 SemaObj->PendingImplicitInstantiations.push_back(std::make_pair(D, Loc));
3202 }
3203
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003204 // Load the offsets of the declarations that Sema references.
3205 // They will be lazily deserialized when needed.
3206 if (!SemaDeclRefs.empty()) {
3207 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
3208 SemaObj->StdNamespace = SemaDeclRefs[0];
3209 SemaObj->StdBadAlloc = SemaDeclRefs[1];
3210 }
3211
Fariborz Jahanian32019832010-07-23 19:11:11 +00003212 // If there are @selector references added them to its pool. This is for
3213 // implementation of -Wselector.
Sebastian Redl725cd962010-08-04 20:40:17 +00003214 if (!ReferencedSelectorsData.empty()) {
3215 unsigned int DataSize = ReferencedSelectorsData.size()-1;
Fariborz Jahanian32019832010-07-23 19:11:11 +00003216 unsigned I = 0;
3217 while (I < DataSize) {
Sebastian Redl725cd962010-08-04 20:40:17 +00003218 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
Fariborz Jahanian32019832010-07-23 19:11:11 +00003219 SourceLocation SelLoc =
Sebastian Redl725cd962010-08-04 20:40:17 +00003220 SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
Fariborz Jahanian32019832010-07-23 19:11:11 +00003221 SemaObj->ReferencedSelectors.insert(std::make_pair(Sel, SelLoc));
3222 }
3223 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00003224}
3225
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003226IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Sebastian Redld8c5abb2010-08-02 18:30:12 +00003227 // Try to find this name within our on-disk hash tables. We start with the
3228 // most recent one, since that one contains the most up-to-date info.
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003229 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003230 ASTIdentifierLookupTable *IdTable
3231 = (ASTIdentifierLookupTable *)Chain[I]->IdentifierLookupTable;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003232 if (!IdTable)
3233 continue;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003234 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003235 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003236 if (Pos == IdTable->end())
3237 continue;
Douglas Gregor668c1a42009-04-21 22:25:48 +00003238
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003239 // Dereferencing the iterator has the effect of building the
3240 // IdentifierInfo node and populating it with the various
3241 // declarations it needs.
Sebastian Redld8c5abb2010-08-02 18:30:12 +00003242 return *Pos;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003243 }
Sebastian Redld8c5abb2010-08-02 18:30:12 +00003244 return 0;
Douglas Gregor668c1a42009-04-21 22:25:48 +00003245}
3246
Mike Stump1eb44332009-09-09 15:08:12 +00003247std::pair<ObjCMethodList, ObjCMethodList>
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003248ASTReader::ReadMethodPool(Selector Sel) {
Sebastian Redl725cd962010-08-04 20:40:17 +00003249 // Find this selector in a hash table. We want to find the most recent entry.
3250 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3251 PerFileData &F = *Chain[I];
3252 if (!F.SelectorLookupTable)
3253 continue;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003254
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003255 ASTSelectorLookupTable *PoolTable
3256 = (ASTSelectorLookupTable*)F.SelectorLookupTable;
3257 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
Sebastian Redl725cd962010-08-04 20:40:17 +00003258 if (Pos != PoolTable->end()) {
3259 ++NumSelectorsRead;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003260 // FIXME: Not quite happy with the statistics here. We probably should
3261 // disable this tracking when called via LoadSelector.
3262 // Also, should entries without methods count as misses?
3263 ++NumMethodPoolEntriesRead;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003264 ASTSelectorLookupTrait::data_type Data = *Pos;
Sebastian Redl725cd962010-08-04 20:40:17 +00003265 if (DeserializationListener)
3266 DeserializationListener->SelectorRead(Data.ID, Sel);
3267 return std::make_pair(Data.Instance, Data.Factory);
3268 }
Douglas Gregor83941df2009-04-25 17:48:32 +00003269 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003270
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003271 ++NumMethodPoolMisses;
Sebastian Redl725cd962010-08-04 20:40:17 +00003272 return std::pair<ObjCMethodList, ObjCMethodList>();
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003273}
3274
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003275void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redle58aa892010-08-04 18:21:41 +00003276 // It would be complicated to avoid reading the methods anyway. So don't.
3277 ReadMethodPool(Sel);
3278}
3279
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003280void ASTReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00003281 assert(ID && "Non-zero identifier ID required");
Douglas Gregora02b1472009-04-28 21:53:25 +00003282 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003283 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003284 if (DeserializationListener)
3285 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregor668c1a42009-04-21 22:25:48 +00003286}
3287
Douglas Gregord89275b2009-07-06 18:54:52 +00003288/// \brief Set the globally-visible declarations associated with the given
3289/// identifier.
3290///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003291/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump1eb44332009-09-09 15:08:12 +00003292/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregord89275b2009-07-06 18:54:52 +00003293/// them.
3294///
3295/// \param II an IdentifierInfo that refers to one or more globally-visible
3296/// declarations.
3297///
3298/// \param DeclIDs the set of declaration IDs with the name @p II that are
3299/// visible at global scope.
3300///
3301/// \param Nonrecursive should be true to indicate that the caller knows that
3302/// this call is non-recursive, and therefore the globally-visible declarations
3303/// will not be placed onto the pending queue.
Mike Stump1eb44332009-09-09 15:08:12 +00003304void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003305ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Douglas Gregord89275b2009-07-06 18:54:52 +00003306 const llvm::SmallVectorImpl<uint32_t> &DeclIDs,
3307 bool Nonrecursive) {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00003308 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregord89275b2009-07-06 18:54:52 +00003309 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
3310 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
3311 PII.II = II;
3312 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I)
3313 PII.DeclIDs.push_back(DeclIDs[I]);
3314 return;
3315 }
Mike Stump1eb44332009-09-09 15:08:12 +00003316
Douglas Gregord89275b2009-07-06 18:54:52 +00003317 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
3318 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
3319 if (SemaObj) {
Douglas Gregor914ed9d2010-08-13 03:15:25 +00003320 if (SemaObj->TUScope) {
3321 // Introduce this declaration into the translation-unit scope
3322 // and add it to the declaration chain for this identifier, so
3323 // that (unqualified) name lookup will find it.
3324 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(D));
3325 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
3326 }
Douglas Gregord89275b2009-07-06 18:54:52 +00003327 } else {
3328 // Queue this declaration so that it will be added to the
3329 // translation unit scope and identifier's declaration chain
3330 // once a Sema object is known.
3331 PreloadedDecls.push_back(D);
3332 }
3333 }
3334}
3335
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003336IdentifierInfo *ASTReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00003337 if (ID == 0)
3338 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003339
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00003340 if (IdentifiersLoaded.empty()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003341 Error("no identifier table in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00003342 return 0;
3343 }
Mike Stump1eb44332009-09-09 15:08:12 +00003344
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003345 assert(PP && "Forgot to set Preprocessor ?");
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00003346 ID -= 1;
3347 if (!IdentifiersLoaded[ID]) {
3348 unsigned Index = ID;
3349 const char *Str = 0;
3350 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3351 PerFileData *F = Chain[N - I - 1];
3352 if (Index < F->LocalNumIdentifiers) {
3353 uint32_t Offset = F->IdentifierOffsets[Index];
3354 Str = F->IdentifierTableData + Offset;
3355 break;
3356 }
3357 Index -= F->LocalNumIdentifiers;
3358 }
3359 assert(Str && "Broken Chain");
Douglas Gregord6595a42009-04-25 21:04:17 +00003360
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003361 // All of the strings in the AST file are preceded by a 16-bit length.
3362 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenek231bc0b2009-10-23 04:45:31 +00003363 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
3364 // unsigned integers. This is important to avoid integer overflow when
3365 // we cast them to 'unsigned'.
Ted Kremenekff1ea462009-10-23 03:57:22 +00003366 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregor02fc7512009-04-28 20:01:51 +00003367 unsigned StrLen = (((unsigned) StrLenPtr[0])
3368 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00003369 IdentifiersLoaded[ID]
Kovarththanan Rajaratnam811f4262010-03-12 10:32:27 +00003370 = &PP->getIdentifierTable().get(Str, StrLen);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003371 if (DeserializationListener)
3372 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregorafaf3082009-04-11 00:14:32 +00003373 }
Mike Stump1eb44332009-09-09 15:08:12 +00003374
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00003375 return IdentifiersLoaded[ID];
Douglas Gregor2cf26342009-04-09 22:27:44 +00003376}
3377
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003378void ASTReader::ReadSLocEntry(unsigned ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00003379 ReadSLocEntryRecord(ID);
3380}
3381
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003382Selector ASTReader::DecodeSelector(unsigned ID) {
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003383 if (ID == 0)
3384 return Selector();
Mike Stump1eb44332009-09-09 15:08:12 +00003385
Sebastian Redl725cd962010-08-04 20:40:17 +00003386 if (ID > SelectorsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003387 Error("selector ID out of range in AST file");
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003388 return Selector();
3389 }
Douglas Gregor83941df2009-04-25 17:48:32 +00003390
Sebastian Redl725cd962010-08-04 20:40:17 +00003391 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003392 // Load this selector from the selector table.
Sebastian Redl725cd962010-08-04 20:40:17 +00003393 unsigned Idx = ID - 1;
3394 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3395 PerFileData &F = *Chain[N - I - 1];
3396 if (Idx < F.LocalNumSelectors) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003397 ASTSelectorLookupTrait Trait(*this);
Sebastian Redl725cd962010-08-04 20:40:17 +00003398 SelectorsLoaded[ID - 1] =
3399 Trait.ReadKey(F.SelectorLookupTableData + F.SelectorOffsets[Idx], 0);
3400 if (DeserializationListener)
3401 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
3402 break;
3403 }
3404 Idx -= F.LocalNumSelectors;
3405 }
Douglas Gregor83941df2009-04-25 17:48:32 +00003406 }
3407
Sebastian Redl725cd962010-08-04 20:40:17 +00003408 return SelectorsLoaded[ID - 1];
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003409}
3410
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003411Selector ASTReader::GetExternalSelector(uint32_t ID) {
Douglas Gregor719770d2010-04-06 17:30:22 +00003412 return DecodeSelector(ID);
3413}
3414
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003415uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redl725cd962010-08-04 20:40:17 +00003416 // ID 0 (the null selector) is considered an external selector.
3417 return getTotalNumSelectors() + 1;
Douglas Gregor719770d2010-04-06 17:30:22 +00003418}
3419
Mike Stump1eb44332009-09-09 15:08:12 +00003420DeclarationName
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003421ASTReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003422 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
3423 switch (Kind) {
3424 case DeclarationName::Identifier:
3425 return DeclarationName(GetIdentifierInfo(Record, Idx));
3426
3427 case DeclarationName::ObjCZeroArgSelector:
3428 case DeclarationName::ObjCOneArgSelector:
3429 case DeclarationName::ObjCMultiArgSelector:
Steve Naroffa7503a72009-04-23 15:15:40 +00003430 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00003431
3432 case DeclarationName::CXXConstructorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003433 return Context->DeclarationNames.getCXXConstructorName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00003434 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00003435
3436 case DeclarationName::CXXDestructorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003437 return Context->DeclarationNames.getCXXDestructorName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00003438 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00003439
3440 case DeclarationName::CXXConversionFunctionName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003441 return Context->DeclarationNames.getCXXConversionFunctionName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00003442 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00003443
3444 case DeclarationName::CXXOperatorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003445 return Context->DeclarationNames.getCXXOperatorName(
Douglas Gregor2cf26342009-04-09 22:27:44 +00003446 (OverloadedOperatorKind)Record[Idx++]);
3447
Sean Hunt3e518bd2009-11-29 07:34:05 +00003448 case DeclarationName::CXXLiteralOperatorName:
3449 return Context->DeclarationNames.getCXXLiteralOperatorName(
3450 GetIdentifierInfo(Record, Idx));
3451
Douglas Gregor2cf26342009-04-09 22:27:44 +00003452 case DeclarationName::CXXUsingDirective:
3453 return DeclarationName::getUsingDirectiveName();
3454 }
3455
3456 // Required to silence GCC warning
3457 return DeclarationName();
3458}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003459
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003460TemplateName
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003461ASTReader::ReadTemplateName(const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003462 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
3463 switch (Kind) {
3464 case TemplateName::Template:
3465 return TemplateName(cast_or_null<TemplateDecl>(GetDecl(Record[Idx++])));
3466
3467 case TemplateName::OverloadedTemplate: {
3468 unsigned size = Record[Idx++];
3469 UnresolvedSet<8> Decls;
3470 while (size--)
3471 Decls.addDecl(cast<NamedDecl>(GetDecl(Record[Idx++])));
3472
3473 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
3474 }
3475
3476 case TemplateName::QualifiedTemplate: {
3477 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3478 bool hasTemplKeyword = Record[Idx++];
3479 TemplateDecl *Template = cast<TemplateDecl>(GetDecl(Record[Idx++]));
3480 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
3481 }
3482
3483 case TemplateName::DependentTemplate: {
3484 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3485 if (Record[Idx++]) // isIdentifier
3486 return Context->getDependentTemplateName(NNS,
3487 GetIdentifierInfo(Record, Idx));
3488 return Context->getDependentTemplateName(NNS,
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003489 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003490 }
3491 }
3492
3493 assert(0 && "Unhandled template name kind!");
3494 return TemplateName();
3495}
3496
3497TemplateArgument
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003498ASTReader::ReadTemplateArgument(llvm::BitstreamCursor &DeclsCursor,
Sebastian Redl577d4792010-07-22 22:43:28 +00003499 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003500 switch ((TemplateArgument::ArgKind)Record[Idx++]) {
3501 case TemplateArgument::Null:
3502 return TemplateArgument();
3503 case TemplateArgument::Type:
3504 return TemplateArgument(GetType(Record[Idx++]));
3505 case TemplateArgument::Declaration:
3506 return TemplateArgument(GetDecl(Record[Idx++]));
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00003507 case TemplateArgument::Integral: {
3508 llvm::APSInt Value = ReadAPSInt(Record, Idx);
3509 QualType T = GetType(Record[Idx++]);
3510 return TemplateArgument(Value, T);
3511 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003512 case TemplateArgument::Template:
3513 return TemplateArgument(ReadTemplateName(Record, Idx));
3514 case TemplateArgument::Expression:
Sebastian Redl577d4792010-07-22 22:43:28 +00003515 return TemplateArgument(ReadExpr(DeclsCursor));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003516 case TemplateArgument::Pack: {
3517 unsigned NumArgs = Record[Idx++];
3518 llvm::SmallVector<TemplateArgument, 8> Args;
3519 Args.reserve(NumArgs);
3520 while (NumArgs--)
Sebastian Redl577d4792010-07-22 22:43:28 +00003521 Args.push_back(ReadTemplateArgument(DeclsCursor, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003522 TemplateArgument TemplArg;
3523 TemplArg.setArgumentPack(Args.data(), Args.size(), /*CopyArgs=*/true);
3524 return TemplArg;
3525 }
3526 }
3527
3528 assert(0 && "Unhandled template argument kind!");
3529 return TemplateArgument();
3530}
3531
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003532TemplateParameterList *
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003533ASTReader::ReadTemplateParameterList(const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003534 SourceLocation TemplateLoc = ReadSourceLocation(Record, Idx);
3535 SourceLocation LAngleLoc = ReadSourceLocation(Record, Idx);
3536 SourceLocation RAngleLoc = ReadSourceLocation(Record, Idx);
3537
3538 unsigned NumParams = Record[Idx++];
3539 llvm::SmallVector<NamedDecl *, 16> Params;
3540 Params.reserve(NumParams);
3541 while (NumParams--)
3542 Params.push_back(cast<NamedDecl>(GetDecl(Record[Idx++])));
3543
3544 TemplateParameterList* TemplateParams =
3545 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
3546 Params.data(), Params.size(), RAngleLoc);
3547 return TemplateParams;
3548}
3549
3550void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003551ASTReader::
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003552ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs,
Sebastian Redl577d4792010-07-22 22:43:28 +00003553 llvm::BitstreamCursor &DeclsCursor,
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003554 const RecordData &Record, unsigned &Idx) {
3555 unsigned NumTemplateArgs = Record[Idx++];
3556 TemplArgs.reserve(NumTemplateArgs);
3557 while (NumTemplateArgs--)
Sebastian Redl577d4792010-07-22 22:43:28 +00003558 TemplArgs.push_back(ReadTemplateArgument(DeclsCursor, Record, Idx));
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003559}
3560
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00003561/// \brief Read a UnresolvedSet structure.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003562void ASTReader::ReadUnresolvedSet(UnresolvedSetImpl &Set,
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00003563 const RecordData &Record, unsigned &Idx) {
3564 unsigned NumDecls = Record[Idx++];
3565 while (NumDecls--) {
3566 NamedDecl *D = cast<NamedDecl>(GetDecl(Record[Idx++]));
3567 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
3568 Set.addDecl(D, AS);
3569 }
3570}
3571
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003572CXXBaseSpecifier
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003573ASTReader::ReadCXXBaseSpecifier(llvm::BitstreamCursor &DeclsCursor,
Nick Lewycky56062202010-07-26 16:56:01 +00003574 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003575 bool isVirtual = static_cast<bool>(Record[Idx++]);
3576 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
3577 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Nick Lewycky56062202010-07-26 16:56:01 +00003578 TypeSourceInfo *TInfo = GetTypeSourceInfo(DeclsCursor, Record, Idx);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003579 SourceRange Range = ReadSourceRange(Record, Idx);
Nick Lewycky56062202010-07-26 16:56:01 +00003580 return CXXBaseSpecifier(Range, isVirtual, isBaseOfClass, AS, TInfo);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003581}
3582
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003583std::pair<CXXBaseOrMemberInitializer **, unsigned>
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003584ASTReader::ReadCXXBaseOrMemberInitializers(llvm::BitstreamCursor &Cursor,
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003585 const RecordData &Record,
3586 unsigned &Idx) {
3587 CXXBaseOrMemberInitializer **BaseOrMemberInitializers = 0;
3588 unsigned NumInitializers = Record[Idx++];
3589 if (NumInitializers) {
3590 ASTContext &C = *getContext();
3591
3592 BaseOrMemberInitializers
3593 = new (C) CXXBaseOrMemberInitializer*[NumInitializers];
3594 for (unsigned i=0; i != NumInitializers; ++i) {
3595 TypeSourceInfo *BaseClassInfo = 0;
3596 bool IsBaseVirtual = false;
3597 FieldDecl *Member = 0;
3598
3599 bool IsBaseInitializer = Record[Idx++];
3600 if (IsBaseInitializer) {
3601 BaseClassInfo = GetTypeSourceInfo(Cursor, Record, Idx);
3602 IsBaseVirtual = Record[Idx++];
3603 } else {
3604 Member = cast<FieldDecl>(GetDecl(Record[Idx++]));
3605 }
3606 SourceLocation MemberLoc = ReadSourceLocation(Record, Idx);
3607 Expr *Init = ReadExpr(Cursor);
3608 FieldDecl *AnonUnionMember
3609 = cast_or_null<FieldDecl>(GetDecl(Record[Idx++]));
3610 SourceLocation LParenLoc = ReadSourceLocation(Record, Idx);
3611 SourceLocation RParenLoc = ReadSourceLocation(Record, Idx);
3612 bool IsWritten = Record[Idx++];
3613 unsigned SourceOrderOrNumArrayIndices;
3614 llvm::SmallVector<VarDecl *, 8> Indices;
3615 if (IsWritten) {
3616 SourceOrderOrNumArrayIndices = Record[Idx++];
3617 } else {
3618 SourceOrderOrNumArrayIndices = Record[Idx++];
3619 Indices.reserve(SourceOrderOrNumArrayIndices);
3620 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
3621 Indices.push_back(cast<VarDecl>(GetDecl(Record[Idx++])));
3622 }
3623
3624 CXXBaseOrMemberInitializer *BOMInit;
3625 if (IsBaseInitializer) {
3626 BOMInit = new (C) CXXBaseOrMemberInitializer(C, BaseClassInfo,
3627 IsBaseVirtual, LParenLoc,
3628 Init, RParenLoc);
3629 } else if (IsWritten) {
3630 BOMInit = new (C) CXXBaseOrMemberInitializer(C, Member, MemberLoc,
3631 LParenLoc, Init, RParenLoc);
3632 } else {
3633 BOMInit = CXXBaseOrMemberInitializer::Create(C, Member, MemberLoc,
3634 LParenLoc, Init, RParenLoc,
3635 Indices.data(),
3636 Indices.size());
3637 }
3638
3639 BOMInit->setAnonUnionMember(AnonUnionMember);
3640 BaseOrMemberInitializers[i] = BOMInit;
3641 }
3642 }
3643
3644 return std::make_pair(BaseOrMemberInitializers, NumInitializers);
3645}
3646
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003647NestedNameSpecifier *
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003648ASTReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003649 unsigned N = Record[Idx++];
3650 NestedNameSpecifier *NNS = 0, *Prev = 0;
3651 for (unsigned I = 0; I != N; ++I) {
3652 NestedNameSpecifier::SpecifierKind Kind
3653 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
3654 switch (Kind) {
3655 case NestedNameSpecifier::Identifier: {
3656 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
3657 NNS = NestedNameSpecifier::Create(*Context, Prev, II);
3658 break;
3659 }
3660
3661 case NestedNameSpecifier::Namespace: {
3662 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
3663 NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
3664 break;
3665 }
3666
3667 case NestedNameSpecifier::TypeSpec:
3668 case NestedNameSpecifier::TypeSpecWithTemplate: {
3669 Type *T = GetType(Record[Idx++]).getTypePtr();
3670 bool Template = Record[Idx++];
3671 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
3672 break;
3673 }
3674
3675 case NestedNameSpecifier::Global: {
3676 NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
3677 // No associated value, and there can't be a prefix.
3678 break;
3679 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003680 }
Argyrios Kyrtzidisd2bb2c02010-07-07 15:46:30 +00003681 Prev = NNS;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003682 }
3683 return NNS;
3684}
3685
3686SourceRange
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003687ASTReader::ReadSourceRange(const RecordData &Record, unsigned &Idx) {
Daniel Dunbar8ee59392010-06-02 15:47:10 +00003688 SourceLocation beg = SourceLocation::getFromRawEncoding(Record[Idx++]);
3689 SourceLocation end = SourceLocation::getFromRawEncoding(Record[Idx++]);
3690 return SourceRange(beg, end);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003691}
3692
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003693/// \brief Read an integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003694llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003695 unsigned BitWidth = Record[Idx++];
3696 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
3697 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
3698 Idx += NumWords;
3699 return Result;
3700}
3701
3702/// \brief Read a signed integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003703llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003704 bool isUnsigned = Record[Idx++];
3705 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
3706}
3707
Douglas Gregor17fc2232009-04-14 21:55:33 +00003708/// \brief Read a floating-point value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003709llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00003710 return llvm::APFloat(ReadAPInt(Record, Idx));
3711}
3712
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003713// \brief Read a string
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003714std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003715 unsigned Len = Record[Idx++];
Jay Foadbeaaccd2009-05-21 09:52:38 +00003716 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003717 Idx += Len;
3718 return Result;
3719}
3720
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003721CXXTemporary *ASTReader::ReadCXXTemporary(const RecordData &Record,
Chris Lattnerd2598362010-05-10 00:25:06 +00003722 unsigned &Idx) {
3723 CXXDestructorDecl *Decl = cast<CXXDestructorDecl>(GetDecl(Record[Idx++]));
3724 return CXXTemporary::Create(*Context, Decl);
3725}
3726
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003727DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00003728 return Diag(SourceLocation(), DiagID);
3729}
3730
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003731DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003732 return Diags.Report(FullSourceLoc(Loc, SourceMgr), DiagID);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003733}
Douglas Gregor025452f2009-04-17 00:04:06 +00003734
Douglas Gregor668c1a42009-04-21 22:25:48 +00003735/// \brief Retrieve the identifier table associated with the
3736/// preprocessor.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003737IdentifierTable &ASTReader::getIdentifierTable() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003738 assert(PP && "Forgot to set Preprocessor ?");
3739 return PP->getIdentifierTable();
Douglas Gregor668c1a42009-04-21 22:25:48 +00003740}
3741
Douglas Gregor025452f2009-04-17 00:04:06 +00003742/// \brief Record that the given ID maps to the given switch-case
3743/// statement.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003744void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Douglas Gregor025452f2009-04-17 00:04:06 +00003745 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
3746 SwitchCaseStmts[ID] = SC;
3747}
3748
3749/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003750SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Douglas Gregor025452f2009-04-17 00:04:06 +00003751 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
3752 return SwitchCaseStmts[ID];
3753}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00003754
3755/// \brief Record that the given label statement has been
3756/// deserialized and has the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003757void ASTReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
Mike Stump1eb44332009-09-09 15:08:12 +00003758 assert(LabelStmts.find(ID) == LabelStmts.end() &&
Douglas Gregor1de05fe2009-04-17 18:18:49 +00003759 "Deserialized label twice");
3760 LabelStmts[ID] = S;
3761
3762 // If we've already seen any goto statements that point to this
3763 // label, resolve them now.
3764 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
3765 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
3766 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
3767 Goto->second->setLabel(S);
3768 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00003769
3770 // If we've already seen any address-label statements that point to
3771 // this label, resolve them now.
3772 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
Mike Stump1eb44332009-09-09 15:08:12 +00003773 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00003774 = UnresolvedAddrLabelExprs.equal_range(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00003775 for (AddrLabelIter AddrLabel = AddrLabels.first;
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00003776 AddrLabel != AddrLabels.second; ++AddrLabel)
3777 AddrLabel->second->setLabel(S);
3778 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor1de05fe2009-04-17 18:18:49 +00003779}
3780
3781/// \brief Set the label of the given statement to the label
3782/// identified by ID.
3783///
3784/// Depending on the order in which the label and other statements
3785/// referencing that label occur, this operation may complete
3786/// immediately (updating the statement) or it may queue the
3787/// statement to be back-patched later.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003788void ASTReader::SetLabelOf(GotoStmt *S, unsigned ID) {
Douglas Gregor1de05fe2009-04-17 18:18:49 +00003789 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3790 if (Label != LabelStmts.end()) {
3791 // We've already seen this label, so set the label of the goto and
3792 // we're done.
3793 S->setLabel(Label->second);
3794 } else {
3795 // We haven't seen this label yet, so add this goto to the set of
3796 // unresolved goto statements.
3797 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
3798 }
3799}
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00003800
3801/// \brief Set the label of the given expression to the label
3802/// identified by ID.
3803///
3804/// Depending on the order in which the label and other statements
3805/// referencing that label occur, this operation may complete
3806/// immediately (updating the statement) or it may queue the
3807/// statement to be back-patched later.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003808void ASTReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00003809 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3810 if (Label != LabelStmts.end()) {
3811 // We've already seen this label, so set the label of the
3812 // label-address expression and we're done.
3813 S->setLabel(Label->second);
3814 } else {
3815 // We haven't seen this label yet, so add this label-address
3816 // expression to the set of unresolved label-address expressions.
3817 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
3818 }
3819}
Douglas Gregord89275b2009-07-06 18:54:52 +00003820
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003821void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00003822 assert(NumCurrentElementsDeserializing &&
3823 "FinishedDeserializing not paired with StartedDeserializing");
3824 if (NumCurrentElementsDeserializing == 1) {
Douglas Gregord89275b2009-07-06 18:54:52 +00003825 // If any identifiers with corresponding top-level declarations have
3826 // been loaded, load those declarations now.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00003827 while (!PendingIdentifierInfos.empty()) {
3828 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
3829 PendingIdentifierInfos.front().DeclIDs, true);
3830 PendingIdentifierInfos.pop_front();
Douglas Gregord89275b2009-07-06 18:54:52 +00003831 }
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003832
3833 // We are not in recursive loading, so it's safe to pass the "interesting"
3834 // decls to the consumer.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00003835 if (Consumer)
3836 PassInterestingDeclsToConsumer();
Douglas Gregord89275b2009-07-06 18:54:52 +00003837 }
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00003838 --NumCurrentElementsDeserializing;
Douglas Gregord89275b2009-07-06 18:54:52 +00003839}