blob: 6fe1b7068ecd017ab29349c451c902268026379e [file] [log] [blame]
Sebastian Redl904c9c82010-08-18 23:57:11 +00001//===--- ASTReader.cpp - AST File Reader ------------------------*- C++ -*-===//
Douglas Gregor2cf26342009-04-09 22:27:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Sebastian Redlc43b54c2010-08-18 23:56:43 +000010// This file defines the ASTReader class, which reads AST files.
Douglas Gregor2cf26342009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
Chris Lattner4c6f9522009-04-27 05:14:47 +000013
Sebastian Redl6ab7cd82010-08-18 23:57:17 +000014#include "clang/Serialization/ASTReader.h"
15#include "clang/Serialization/ASTDeserializationListener.h"
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +000016#include "ASTCommon.h"
Douglas Gregor0a0428e2009-04-10 20:39:37 +000017#include "clang/Frontend/FrontendDiagnostic.h"
Daniel Dunbarc7162932009-11-11 23:58:53 +000018#include "clang/Frontend/Utils.h"
Douglas Gregore737f502010-08-12 20:07:10 +000019#include "clang/Sema/Sema.h"
Douglas Gregorfdd01722009-04-14 00:24:19 +000020#include "clang/AST/ASTConsumer.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000021#include "clang/AST/ASTContext.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000022#include "clang/AST/Expr.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000023#include "clang/AST/Type.h"
John McCalla1ee0c52009-10-16 21:56:05 +000024#include "clang/AST/TypeLocVisitor.h"
Chris Lattner42d42b52009-04-10 21:41:48 +000025#include "clang/Lex/MacroInfo.h"
Douglas Gregor6a5a23f2010-03-19 21:51:54 +000026#include "clang/Lex/PreprocessingRecord.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000027#include "clang/Lex/Preprocessor.h"
Steve Naroff83d63c72009-04-24 20:03:17 +000028#include "clang/Lex/HeaderSearch.h"
Douglas Gregor668c1a42009-04-21 22:25:48 +000029#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000030#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000031#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000032#include "clang/Basic/FileManager.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000033#include "clang/Basic/TargetInfo.h"
Douglas Gregor445e23e2009-10-05 21:07:28 +000034#include "clang/Basic/Version.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000035#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000036#include "llvm/Bitcode/BitstreamReader.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000037#include "llvm/Support/MemoryBuffer.h"
John McCall833ca992009-10-29 08:12:44 +000038#include "llvm/Support/ErrorHandling.h"
Daniel Dunbard5b21972009-11-18 19:50:41 +000039#include "llvm/System/Path.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000040#include <algorithm>
Douglas Gregore721f952009-04-28 18:58:38 +000041#include <iterator>
Douglas Gregor2cf26342009-04-09 22:27:44 +000042#include <cstdio>
Douglas Gregor4fed3f42009-04-27 18:38:38 +000043#include <sys/stat.h>
Douglas Gregor2cf26342009-04-09 22:27:44 +000044using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000045using namespace clang::serialization;
Douglas Gregor2cf26342009-04-09 22:27:44 +000046
47//===----------------------------------------------------------------------===//
Sebastian Redl3c7f4132010-08-18 23:57:06 +000048// PCH validator implementation
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000049//===----------------------------------------------------------------------===//
50
Sebastian Redl571db7f2010-08-18 23:56:56 +000051ASTReaderListener::~ASTReaderListener() {}
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000052
53bool
54PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts) {
55 const LangOptions &PPLangOpts = PP.getLangOptions();
56#define PARSE_LANGOPT_BENIGN(Option)
57#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
58 if (PPLangOpts.Option != LangOpts.Option) { \
59 Reader.Diag(DiagID) << LangOpts.Option << PPLangOpts.Option; \
60 return true; \
61 }
62
63 PARSE_LANGOPT_BENIGN(Trigraphs);
64 PARSE_LANGOPT_BENIGN(BCPLComment);
65 PARSE_LANGOPT_BENIGN(DollarIdents);
66 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
67 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
Chandler Carrutheb5d7b72010-04-17 20:17:31 +000068 PARSE_LANGOPT_IMPORTANT(GNUKeywords, diag::warn_pch_gnu_keywords);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000069 PARSE_LANGOPT_BENIGN(ImplicitInt);
70 PARSE_LANGOPT_BENIGN(Digraphs);
71 PARSE_LANGOPT_BENIGN(HexFloats);
72 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
73 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
74 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
75 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
76 PARSE_LANGOPT_BENIGN(CXXOperatorName);
77 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
78 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
79 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
Fariborz Jahanian412e7982010-02-09 19:31:38 +000080 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI2, diag::warn_pch_nonfragile_abi2);
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +000081 PARSE_LANGOPT_IMPORTANT(NoConstantCFStrings,
82 diag::warn_pch_no_constant_cfstrings);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000083 PARSE_LANGOPT_BENIGN(PascalStrings);
84 PARSE_LANGOPT_BENIGN(WritableStrings);
Mike Stump1eb44332009-09-09 15:08:12 +000085 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000086 diag::warn_pch_lax_vector_conversions);
Nate Begeman69cfb9b2009-06-25 22:57:40 +000087 PARSE_LANGOPT_IMPORTANT(AltiVec, diag::warn_pch_altivec);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000088 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
Daniel Dunbar73482882010-02-10 18:48:44 +000089 PARSE_LANGOPT_IMPORTANT(SjLjExceptions, diag::warn_pch_sjlj_exceptions);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000090 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
91 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
92 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
Mike Stump1eb44332009-09-09 15:08:12 +000093 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000094 diag::warn_pch_thread_safe_statics);
Daniel Dunbar5345c392009-09-03 04:54:28 +000095 PARSE_LANGOPT_IMPORTANT(POSIXThreads, diag::warn_pch_posix_threads);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000096 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
97 PARSE_LANGOPT_BENIGN(EmitAllDecls);
98 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
Chris Lattnera4d71452010-06-26 21:25:03 +000099 PARSE_LANGOPT_BENIGN(getSignedOverflowBehavior());
Mike Stump1eb44332009-09-09 15:08:12 +0000100 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000101 diag::warn_pch_heinous_extensions);
102 // FIXME: Most of the options below are benign if the macro wasn't
103 // used. Unfortunately, this means that a PCH compiled without
104 // optimization can't be used with optimization turned on, even
105 // though the only thing that changes is whether __OPTIMIZE__ was
106 // defined... but if __OPTIMIZE__ never showed up in the header, it
107 // doesn't matter. We could consider making this some special kind
108 // of check.
109 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
110 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
111 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
112 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
113 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
114 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
115 PARSE_LANGOPT_IMPORTANT(AccessControl, diag::warn_pch_access_control);
116 PARSE_LANGOPT_IMPORTANT(CharIsSigned, diag::warn_pch_char_signed);
John Thompsona6fda122009-11-05 20:14:16 +0000117 PARSE_LANGOPT_IMPORTANT(ShortWChar, diag::warn_pch_short_wchar);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000118 if ((PPLangOpts.getGCMode() != 0) != (LangOpts.getGCMode() != 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000119 Reader.Diag(diag::warn_pch_gc_mode)
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000120 << LangOpts.getGCMode() << PPLangOpts.getGCMode();
121 return true;
122 }
123 PARSE_LANGOPT_BENIGN(getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000124 PARSE_LANGOPT_IMPORTANT(getStackProtectorMode(),
125 diag::warn_pch_stack_protector);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000126 PARSE_LANGOPT_BENIGN(InstantiationDepth);
Nate Begeman69cfb9b2009-06-25 22:57:40 +0000127 PARSE_LANGOPT_IMPORTANT(OpenCL, diag::warn_pch_opencl);
Mike Stump9c276ae2009-12-12 01:27:46 +0000128 PARSE_LANGOPT_BENIGN(CatchUndefined);
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000129 PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors);
Douglas Gregora0068fc2010-07-09 17:35:33 +0000130 PARSE_LANGOPT_BENIGN(SpellChecking);
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +0000131#undef PARSE_LANGOPT_IMPORTANT
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000132#undef PARSE_LANGOPT_BENIGN
133
134 return false;
135}
136
Daniel Dunbardc3c0d22009-11-11 00:52:11 +0000137bool PCHValidator::ReadTargetTriple(llvm::StringRef Triple) {
138 if (Triple == PP.getTargetInfo().getTriple().str())
139 return false;
140
141 Reader.Diag(diag::warn_pch_target_triple)
142 << Triple << PP.getTargetInfo().getTriple().str();
143 return true;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000144}
145
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000146struct EmptyStringRef {
Benjamin Kramerec1b1cc2010-07-14 23:19:41 +0000147 bool operator ()(llvm::StringRef r) const { return r.empty(); }
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000148};
149struct EmptyBlock {
150 bool operator ()(const PCHPredefinesBlock &r) const { return r.Data.empty(); }
151};
152
153static bool EqualConcatenations(llvm::SmallVector<llvm::StringRef, 2> L,
154 PCHPredefinesBlocks R) {
155 // First, sum up the lengths.
156 unsigned LL = 0, RL = 0;
157 for (unsigned I = 0, N = L.size(); I != N; ++I) {
158 LL += L[I].size();
159 }
160 for (unsigned I = 0, N = R.size(); I != N; ++I) {
161 RL += R[I].Data.size();
162 }
163 if (LL != RL)
164 return false;
165 if (LL == 0 && RL == 0)
166 return true;
167
168 // Kick out empty parts, they confuse the algorithm below.
169 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
170 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
171
172 // Do it the hard way. At this point, both vectors must be non-empty.
173 llvm::StringRef LR = L[0], RR = R[0].Data;
174 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
Daniel Dunbarc76c9e02010-07-16 00:00:11 +0000175 (void) RN;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000176 for (;;) {
177 // Compare the current pieces.
178 if (LR.size() == RR.size()) {
179 // If they're the same length, it's pretty easy.
180 if (LR != RR)
181 return false;
182 // Both pieces are done, advance.
183 ++LI;
184 ++RI;
185 // If either string is done, they're both done, since they're the same
186 // length.
187 if (LI == LN) {
188 assert(RI == RN && "Strings not the same length after all?");
189 return true;
190 }
191 LR = L[LI];
192 RR = R[RI].Data;
193 } else if (LR.size() < RR.size()) {
194 // Right piece is longer.
195 if (!RR.startswith(LR))
196 return false;
197 ++LI;
198 assert(LI != LN && "Strings not the same length after all?");
199 RR = RR.substr(LR.size());
200 LR = L[LI];
201 } else {
202 // Left piece is longer.
203 if (!LR.startswith(RR))
204 return false;
205 ++RI;
206 assert(RI != RN && "Strings not the same length after all?");
207 LR = LR.substr(RR.size());
208 RR = R[RI].Data;
209 }
210 }
211}
212
213static std::pair<FileID, llvm::StringRef::size_type>
214FindMacro(const PCHPredefinesBlocks &Buffers, llvm::StringRef MacroDef) {
215 std::pair<FileID, llvm::StringRef::size_type> Res;
216 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
217 Res.second = Buffers[I].Data.find(MacroDef);
218 if (Res.second != llvm::StringRef::npos) {
219 Res.first = Buffers[I].BufferID;
220 break;
221 }
222 }
223 return Res;
224}
225
226bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000227 llvm::StringRef OriginalFileName,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000228 std::string &SuggestedPredefines) {
Daniel Dunbarc7162932009-11-11 23:58:53 +0000229 // We are in the context of an implicit include, so the predefines buffer will
230 // have a #include entry for the PCH file itself (as normalized by the
231 // preprocessor initialization). Find it and skip over it in the checking
232 // below.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000233 llvm::SmallString<256> PCHInclude;
234 PCHInclude += "#include \"";
Daniel Dunbarc7162932009-11-11 23:58:53 +0000235 PCHInclude += NormalizeDashIncludePath(OriginalFileName);
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000236 PCHInclude += "\"\n";
237 std::pair<llvm::StringRef,llvm::StringRef> Split =
238 llvm::StringRef(PP.getPredefines()).split(PCHInclude.str());
239 llvm::StringRef Left = Split.first, Right = Split.second;
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000240 if (Left == PP.getPredefines()) {
241 Error("Missing PCH include entry!");
242 return true;
243 }
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000244
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000245 // If the concatenation of all the PCH buffers is equal to the adjusted
246 // command line, we're done.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000247 llvm::SmallVector<llvm::StringRef, 2> CommandLine;
248 CommandLine.push_back(Left);
249 CommandLine.push_back(Right);
250 if (EqualConcatenations(CommandLine, Buffers))
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000251 return false;
252
253 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000254
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000255 // The predefines buffers are different. Determine what the differences are,
256 // and whether they require us to reject the PCH file.
Daniel Dunbare6750492009-11-13 16:46:11 +0000257 llvm::SmallVector<llvm::StringRef, 8> PCHLines;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000258 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
259 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Daniel Dunbare6750492009-11-13 16:46:11 +0000260
261 llvm::SmallVector<llvm::StringRef, 8> CmdLineLines;
262 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
263 Right.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000264
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000265 // Sort both sets of predefined buffer lines, since we allow some extra
266 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000267 std::sort(CmdLineLines.begin(), CmdLineLines.end());
268 std::sort(PCHLines.begin(), PCHLines.end());
269
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000270 // Determine which predefines that were used to build the PCH file are missing
271 // from the command line.
272 std::vector<llvm::StringRef> MissingPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000273 std::set_difference(PCHLines.begin(), PCHLines.end(),
274 CmdLineLines.begin(), CmdLineLines.end(),
275 std::back_inserter(MissingPredefines));
276
277 bool MissingDefines = false;
278 bool ConflictingDefines = false;
279 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000280 llvm::StringRef Missing = MissingPredefines[I];
281 if (!Missing.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000282 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
283 return true;
284 }
Mike Stump1eb44332009-09-09 15:08:12 +0000285
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000286 // This is a macro definition. Determine the name of the macro we're
287 // defining.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000288 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000289 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000290 = Missing.find_first_of("( \n\r", StartOfMacroName);
291 assert(EndOfMacroName != std::string::npos &&
292 "Couldn't find the end of the macro name");
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000293 llvm::StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000294
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000295 // Determine whether this macro was given a different definition on the
296 // command line.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000297 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000298 std::string::size_type MacroDefLen = MacroDefStart.size();
Daniel Dunbare6750492009-11-13 16:46:11 +0000299 llvm::SmallVector<llvm::StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000300 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
301 MacroDefStart);
302 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000303 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000304 // Different macro; we're done.
305 ConflictPos = CmdLineLines.end();
Mike Stump1eb44332009-09-09 15:08:12 +0000306 break;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000307 }
Mike Stump1eb44332009-09-09 15:08:12 +0000308
309 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000310 "Invalid #define in predefines buffer?");
Mike Stump1eb44332009-09-09 15:08:12 +0000311 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000312 (*ConflictPos)[MacroDefLen] != '(')
313 continue; // Longer macro name; keep trying.
Mike Stump1eb44332009-09-09 15:08:12 +0000314
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000315 // We found a conflicting macro definition.
316 break;
317 }
Mike Stump1eb44332009-09-09 15:08:12 +0000318
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000319 if (ConflictPos != CmdLineLines.end()) {
320 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
321 << MacroName;
322
323 // Show the definition of this macro within the PCH file.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000324 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
325 FindMacro(Buffers, Missing);
326 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
327 SourceLocation PCHMissingLoc =
328 SourceMgr.getLocForStartOfFile(MacroLoc.first)
329 .getFileLocWithOffset(MacroLoc.second);
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000330 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000331
332 ConflictingDefines = true;
333 continue;
334 }
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000336 // If the macro doesn't conflict, then we'll just pick up the macro
337 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000338 if (ConflictingDefines)
339 continue; // Don't complain if there are already conflicting defs
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000341 if (!MissingDefines) {
342 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
343 MissingDefines = true;
344 }
345
346 // Show the definition of this macro within the PCH file.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000347 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
348 FindMacro(Buffers, Missing);
349 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
350 SourceLocation PCHMissingLoc =
351 SourceMgr.getLocForStartOfFile(MacroLoc.first)
352 .getFileLocWithOffset(MacroLoc.second);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000353 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
354 }
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000356 if (ConflictingDefines)
357 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000359 // Determine what predefines were introduced based on command-line
360 // parameters that were not present when building the PCH
361 // file. Extra #defines are okay, so long as the identifiers being
362 // defined were not used within the precompiled header.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000363 std::vector<llvm::StringRef> ExtraPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000364 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
365 PCHLines.begin(), PCHLines.end(),
Mike Stump1eb44332009-09-09 15:08:12 +0000366 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000367 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000368 llvm::StringRef &Extra = ExtraPredefines[I];
369 if (!Extra.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000370 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
371 return true;
372 }
373
374 // This is an extra macro definition. Determine the name of the
375 // macro we're defining.
376 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000377 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000378 = Extra.find_first_of("( \n\r", StartOfMacroName);
379 assert(EndOfMacroName != std::string::npos &&
380 "Couldn't find the end of the macro name");
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000381 llvm::StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000382
383 // Check whether this name was used somewhere in the PCH file. If
384 // so, defining it as a macro could change behavior, so we reject
385 // the PCH file.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000386 if (IdentifierInfo *II = Reader.get(MacroName)) {
Daniel Dunbar4fda42e2009-11-11 00:52:00 +0000387 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000388 return true;
389 }
390
391 // Add this definition to the suggested predefines buffer.
392 SuggestedPredefines += Extra;
393 SuggestedPredefines += '\n';
394 }
395
396 // If we get here, it's because the predefines buffer had compatible
397 // contents. Accept the PCH file.
398 return false;
399}
400
Douglas Gregor12fab312010-03-16 16:35:32 +0000401void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
402 unsigned ID) {
403 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
404 ++NumHeaderInfos;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000405}
406
407void PCHValidator::ReadCounter(unsigned Value) {
408 PP.setCounterValue(Value);
409}
410
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000411//===----------------------------------------------------------------------===//
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000412// AST reader implementation
Douglas Gregor668c1a42009-04-21 22:25:48 +0000413//===----------------------------------------------------------------------===//
414
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000415ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
Douglas Gregorfae3b2f2010-07-27 00:27:13 +0000416 const char *isysroot, bool DisableValidation)
Sebastian Redl30c514c2010-07-14 23:45:08 +0000417 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
418 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
419 Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
Sebastian Redl725cd962010-08-04 20:40:17 +0000420 Consumer(0), isysroot(isysroot), DisableValidation(DisableValidation),
421 NumStatHits(0), NumStatMisses(0), NumSLocEntriesRead(0),
422 TotalNumSLocEntries(0), NumStatementsRead(0), TotalNumStatements(0),
Sebastian Redlfa78dec2010-08-04 21:22:45 +0000423 NumMacrosRead(0), TotalNumMacros(0), NumSelectorsRead(0),
424 NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
425 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
Sebastian Redl725cd962010-08-04 20:40:17 +0000426 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
427 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000428 RelocatablePCH = false;
429}
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000430
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000431ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
Douglas Gregorfae3b2f2010-07-27 00:27:13 +0000432 Diagnostic &Diags, const char *isysroot,
433 bool DisableValidation)
Sebastian Redl30c514c2010-07-14 23:45:08 +0000434 : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
Sebastian Redl9137a522010-07-16 17:50:48 +0000435 Diags(Diags), SemaObj(0), PP(0), Context(0), Consumer(0),
Sebastian Redl725cd962010-08-04 20:40:17 +0000436 isysroot(isysroot), DisableValidation(DisableValidation), NumStatHits(0),
437 NumStatMisses(0), NumSLocEntriesRead(0), TotalNumSLocEntries(0),
438 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
Sebastian Redlfa78dec2010-08-04 21:22:45 +0000439 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
440 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
Sebastian Redl725cd962010-08-04 20:40:17 +0000441 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
442 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
443 NumCurrentElementsDeserializing(0) {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000444 RelocatablePCH = false;
445}
Chris Lattner4c6f9522009-04-27 05:14:47 +0000446
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000447ASTReader::~ASTReader() {
Sebastian Redl9137a522010-07-16 17:50:48 +0000448 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
449 delete Chain[e - i - 1];
450}
451
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000452void
Sebastian Redl571db7f2010-08-18 23:56:56 +0000453ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000454 DeserializationListener = Listener;
455 if (DeserializationListener)
456 DeserializationListener->SetReader(this);
457}
458
Chris Lattner4c6f9522009-04-27 05:14:47 +0000459
Douglas Gregor668c1a42009-04-21 22:25:48 +0000460namespace {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000461class ASTSelectorLookupTrait {
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000462 ASTReader &Reader;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000463
464public:
Sebastian Redl5d050072010-08-04 17:20:04 +0000465 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000466 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +0000467 ObjCMethodList Instance, Factory;
468 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000469
470 typedef Selector external_key_type;
471 typedef external_key_type internal_key_type;
472
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000473 explicit ASTSelectorLookupTrait(ASTReader &Reader) : Reader(Reader) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000474
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000475 static bool EqualKey(const internal_key_type& a,
476 const internal_key_type& b) {
477 return a == b;
478 }
Mike Stump1eb44332009-09-09 15:08:12 +0000479
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000480 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +0000481 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000482 }
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000484 // This hopefully will just get inlined and removed by the optimizer.
485 static const internal_key_type&
486 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000488 static std::pair<unsigned, unsigned>
489 ReadKeyDataLength(const unsigned char*& d) {
490 using namespace clang::io;
491 unsigned KeyLen = ReadUnalignedLE16(d);
492 unsigned DataLen = ReadUnalignedLE16(d);
493 return std::make_pair(KeyLen, DataLen);
494 }
Mike Stump1eb44332009-09-09 15:08:12 +0000495
Douglas Gregor83941df2009-04-25 17:48:32 +0000496 internal_key_type ReadKey(const unsigned char* d, unsigned) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000497 using namespace clang::io;
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000498 SelectorTable &SelTable = Reader.getContext()->Selectors;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000499 unsigned N = ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +0000500 IdentifierInfo *FirstII
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000501 = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
502 if (N == 0)
503 return SelTable.getNullarySelector(FirstII);
504 else if (N == 1)
505 return SelTable.getUnarySelector(FirstII);
506
507 llvm::SmallVector<IdentifierInfo *, 16> Args;
508 Args.push_back(FirstII);
509 for (unsigned I = 1; I != N; ++I)
510 Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)));
511
Douglas Gregor75fdb232009-05-22 22:45:36 +0000512 return SelTable.getSelector(N, Args.data());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000513 }
Mike Stump1eb44332009-09-09 15:08:12 +0000514
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000515 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
516 using namespace clang::io;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000517
518 data_type Result;
519
Sebastian Redl5d050072010-08-04 17:20:04 +0000520 Result.ID = ReadUnalignedLE32(d);
521 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
522 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
523
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000524 // Load instance methods
525 ObjCMethodList *Prev = 0;
526 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000527 ObjCMethodDecl *Method
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000528 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl5d050072010-08-04 17:20:04 +0000529 if (!Result.Instance.Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000530 // This is the first method, which is the easy case.
Sebastian Redl5d050072010-08-04 17:20:04 +0000531 Result.Instance.Method = Method;
532 Prev = &Result.Instance;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000533 continue;
534 }
535
Ted Kremenek298ed872010-02-11 00:53:01 +0000536 ObjCMethodList *Mem =
537 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
538 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000539 Prev = Prev->Next;
540 }
541
542 // Load factory methods
543 Prev = 0;
544 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000545 ObjCMethodDecl *Method
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000546 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl5d050072010-08-04 17:20:04 +0000547 if (!Result.Factory.Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000548 // This is the first method, which is the easy case.
Sebastian Redl5d050072010-08-04 17:20:04 +0000549 Result.Factory.Method = Method;
550 Prev = &Result.Factory;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000551 continue;
552 }
553
Ted Kremenek298ed872010-02-11 00:53:01 +0000554 ObjCMethodList *Mem =
555 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
556 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000557 Prev = Prev->Next;
558 }
559
560 return Result;
561 }
562};
Mike Stump1eb44332009-09-09 15:08:12 +0000563
564} // end anonymous namespace
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000565
566/// \brief The on-disk hash table used for the global method pool.
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000567typedef OnDiskChainedHashTable<ASTSelectorLookupTrait>
568 ASTSelectorLookupTable;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000569
570namespace {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000571class ASTIdentifierLookupTrait {
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000572 ASTReader &Reader;
Sebastian Redld27d3fc2010-07-21 22:31:37 +0000573 llvm::BitstreamCursor &Stream;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000574
575 // If we know the IdentifierInfo in advance, it is here and we will
576 // not build a new one. Used when deserializing information about an
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000577 // identifier that was constructed before the AST file was read.
Douglas Gregor668c1a42009-04-21 22:25:48 +0000578 IdentifierInfo *KnownII;
579
580public:
581 typedef IdentifierInfo * data_type;
582
583 typedef const std::pair<const char*, unsigned> external_key_type;
584
585 typedef external_key_type internal_key_type;
586
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000587 ASTIdentifierLookupTrait(ASTReader &Reader, llvm::BitstreamCursor &Stream,
Sebastian Redld27d3fc2010-07-21 22:31:37 +0000588 IdentifierInfo *II = 0)
589 : Reader(Reader), Stream(Stream), KnownII(II) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000590
Douglas Gregor668c1a42009-04-21 22:25:48 +0000591 static bool EqualKey(const internal_key_type& a,
592 const internal_key_type& b) {
593 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
594 : false;
595 }
Mike Stump1eb44332009-09-09 15:08:12 +0000596
Douglas Gregor668c1a42009-04-21 22:25:48 +0000597 static unsigned ComputeHash(const internal_key_type& a) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000598 return llvm::HashString(llvm::StringRef(a.first, a.second));
Douglas Gregor668c1a42009-04-21 22:25:48 +0000599 }
Mike Stump1eb44332009-09-09 15:08:12 +0000600
Douglas Gregor668c1a42009-04-21 22:25:48 +0000601 // This hopefully will just get inlined and removed by the optimizer.
602 static const internal_key_type&
603 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump1eb44332009-09-09 15:08:12 +0000604
Douglas Gregor668c1a42009-04-21 22:25:48 +0000605 static std::pair<unsigned, unsigned>
606 ReadKeyDataLength(const unsigned char*& d) {
607 using namespace clang::io;
Douglas Gregor5f8e3302009-04-25 20:26:24 +0000608 unsigned DataLen = ReadUnalignedLE16(d);
Douglas Gregord6595a42009-04-25 21:04:17 +0000609 unsigned KeyLen = ReadUnalignedLE16(d);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000610 return std::make_pair(KeyLen, DataLen);
611 }
Mike Stump1eb44332009-09-09 15:08:12 +0000612
Douglas Gregor668c1a42009-04-21 22:25:48 +0000613 static std::pair<const char*, unsigned>
614 ReadKey(const unsigned char* d, unsigned n) {
615 assert(n >= 2 && d[n-1] == '\0');
616 return std::make_pair((const char*) d, n-1);
617 }
Mike Stump1eb44332009-09-09 15:08:12 +0000618
619 IdentifierInfo *ReadData(const internal_key_type& k,
Douglas Gregor668c1a42009-04-21 22:25:48 +0000620 const unsigned char* d,
621 unsigned DataLen) {
622 using namespace clang::io;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000623 IdentID ID = ReadUnalignedLE32(d);
Douglas Gregora92193e2009-04-28 21:18:29 +0000624 bool IsInteresting = ID & 0x01;
625
626 // Wipe out the "is interesting" bit.
627 ID = ID >> 1;
628
629 if (!IsInteresting) {
Sebastian Redl083abdf2010-07-27 23:01:28 +0000630 // For uninteresting identifiers, just build the IdentifierInfo
Douglas Gregora92193e2009-04-28 21:18:29 +0000631 // and associate it with the persistent ID.
632 IdentifierInfo *II = KnownII;
633 if (!II)
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000634 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregora92193e2009-04-28 21:18:29 +0000635 Reader.SetIdentifierInfo(ID, II);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000636 II->setIsFromAST();
Douglas Gregora92193e2009-04-28 21:18:29 +0000637 return II;
638 }
639
Douglas Gregor5998da52009-04-28 21:32:13 +0000640 unsigned Bits = ReadUnalignedLE16(d);
Douglas Gregor2deaea32009-04-22 18:49:13 +0000641 bool CPlusPlusOperatorKeyword = Bits & 0x01;
642 Bits >>= 1;
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000643 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
644 Bits >>= 1;
Douglas Gregor2deaea32009-04-22 18:49:13 +0000645 bool Poisoned = Bits & 0x01;
646 Bits >>= 1;
647 bool ExtensionToken = Bits & 0x01;
648 Bits >>= 1;
649 bool hasMacroDefinition = Bits & 0x01;
650 Bits >>= 1;
651 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
652 Bits >>= 10;
Mike Stump1eb44332009-09-09 15:08:12 +0000653
Douglas Gregor2deaea32009-04-22 18:49:13 +0000654 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregor5998da52009-04-28 21:32:13 +0000655 DataLen -= 6;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000656
657 // Build the IdentifierInfo itself and link the identifier ID with
658 // the new IdentifierInfo.
659 IdentifierInfo *II = KnownII;
660 if (!II)
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000661 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000662 Reader.SetIdentifierInfo(ID, II);
663
Douglas Gregor2deaea32009-04-22 18:49:13 +0000664 // Set or check the various bits in the IdentifierInfo structure.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000665 // Token IDs are read-only.
666 if (HasRevertedTokenIDToIdentifier)
667 II->RevertTokenIDToIdentifier();
Douglas Gregor2deaea32009-04-22 18:49:13 +0000668 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
Mike Stump1eb44332009-09-09 15:08:12 +0000669 assert(II->isExtensionToken() == ExtensionToken &&
Douglas Gregor2deaea32009-04-22 18:49:13 +0000670 "Incorrect extension token flag");
671 (void)ExtensionToken;
672 II->setIsPoisoned(Poisoned);
673 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
674 "Incorrect C++ operator keyword flag");
675 (void)CPlusPlusOperatorKeyword;
676
Douglas Gregor37e26842009-04-21 23:56:24 +0000677 // If this identifier is a macro, deserialize the macro
678 // definition.
679 if (hasMacroDefinition) {
Douglas Gregor5998da52009-04-28 21:32:13 +0000680 uint32_t Offset = ReadUnalignedLE32(d);
Sebastian Redld27d3fc2010-07-21 22:31:37 +0000681 Reader.ReadMacroRecord(Stream, Offset);
Douglas Gregor5998da52009-04-28 21:32:13 +0000682 DataLen -= 4;
Douglas Gregor37e26842009-04-21 23:56:24 +0000683 }
Douglas Gregor668c1a42009-04-21 22:25:48 +0000684
685 // Read all of the declarations visible at global scope with this
686 // name.
Chris Lattner6bf690f2009-04-27 22:17:41 +0000687 if (Reader.getContext() == 0) return II;
Douglas Gregord89275b2009-07-06 18:54:52 +0000688 if (DataLen > 0) {
689 llvm::SmallVector<uint32_t, 4> DeclIDs;
690 for (; DataLen > 0; DataLen -= 4)
691 DeclIDs.push_back(ReadUnalignedLE32(d));
692 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000693 }
Mike Stump1eb44332009-09-09 15:08:12 +0000694
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000695 II->setIsFromAST();
Douglas Gregor668c1a42009-04-21 22:25:48 +0000696 return II;
697 }
698};
Mike Stump1eb44332009-09-09 15:08:12 +0000699
700} // end anonymous namespace
Douglas Gregor668c1a42009-04-21 22:25:48 +0000701
702/// \brief The on-disk hash table used to contain information about
703/// all of the identifiers in the program.
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000704typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait>
705 ASTIdentifierLookupTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000706
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000707namespace {
708class ASTDeclContextNameLookupTrait {
709 ASTReader &Reader;
710
711public:
712 /// \brief Pair of begin/end iterators for DeclIDs.
713 typedef std::pair<DeclID *, DeclID *> data_type;
714
715 /// \brief Special internal key for declaration names.
716 /// The hash table creates keys for comparison; we do not create
717 /// a DeclarationName for the internal key to avoid deserializing types.
718 struct DeclNameKey {
719 DeclarationName::NameKind Kind;
720 uint64_t Data;
721 DeclNameKey() : Kind((DeclarationName::NameKind)0), Data(0) { }
722 };
723
724 typedef DeclarationName external_key_type;
725 typedef DeclNameKey internal_key_type;
726
727 explicit ASTDeclContextNameLookupTrait(ASTReader &Reader) : Reader(Reader) { }
728
729 static bool EqualKey(const internal_key_type& a,
730 const internal_key_type& b) {
731 return a.Kind == b.Kind && a.Data == b.Data;
732 }
733
734 unsigned ComputeHash(const DeclNameKey &Key) const {
735 llvm::FoldingSetNodeID ID;
736 ID.AddInteger(Key.Kind);
737
738 switch (Key.Kind) {
739 case DeclarationName::Identifier:
740 case DeclarationName::CXXLiteralOperatorName:
741 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
742 break;
743 case DeclarationName::ObjCZeroArgSelector:
744 case DeclarationName::ObjCOneArgSelector:
745 case DeclarationName::ObjCMultiArgSelector:
746 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
747 break;
748 case DeclarationName::CXXConstructorName:
749 case DeclarationName::CXXDestructorName:
750 case DeclarationName::CXXConversionFunctionName:
751 ID.AddInteger((TypeID)Key.Data);
752 break;
753 case DeclarationName::CXXOperatorName:
754 ID.AddInteger((OverloadedOperatorKind)Key.Data);
755 break;
756 case DeclarationName::CXXUsingDirective:
757 break;
758 }
759
760 return ID.ComputeHash();
761 }
762
763 internal_key_type GetInternalKey(const external_key_type& Name) const {
764 DeclNameKey Key;
765 Key.Kind = Name.getNameKind();
766 switch (Name.getNameKind()) {
767 case DeclarationName::Identifier:
768 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
769 break;
770 case DeclarationName::ObjCZeroArgSelector:
771 case DeclarationName::ObjCOneArgSelector:
772 case DeclarationName::ObjCMultiArgSelector:
773 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
774 break;
775 case DeclarationName::CXXConstructorName:
776 case DeclarationName::CXXDestructorName:
777 case DeclarationName::CXXConversionFunctionName:
778 Key.Data = Reader.GetTypeID(Name.getCXXNameType());
779 break;
780 case DeclarationName::CXXOperatorName:
781 Key.Data = Name.getCXXOverloadedOperator();
782 break;
783 case DeclarationName::CXXLiteralOperatorName:
784 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
785 break;
786 case DeclarationName::CXXUsingDirective:
787 break;
788 }
789
790 return Key;
791 }
792
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000793 external_key_type GetExternalKey(const internal_key_type& Key) const {
794 ASTContext *Context = Reader.getContext();
795 switch (Key.Kind) {
796 case DeclarationName::Identifier:
797 return DeclarationName((IdentifierInfo*)Key.Data);
798
799 case DeclarationName::ObjCZeroArgSelector:
800 case DeclarationName::ObjCOneArgSelector:
801 case DeclarationName::ObjCMultiArgSelector:
802 return DeclarationName(Selector(Key.Data));
803
804 case DeclarationName::CXXConstructorName:
805 return Context->DeclarationNames.getCXXConstructorName(
806 Context->getCanonicalType(Reader.GetType(Key.Data)));
807
808 case DeclarationName::CXXDestructorName:
809 return Context->DeclarationNames.getCXXDestructorName(
810 Context->getCanonicalType(Reader.GetType(Key.Data)));
811
812 case DeclarationName::CXXConversionFunctionName:
813 return Context->DeclarationNames.getCXXConversionFunctionName(
814 Context->getCanonicalType(Reader.GetType(Key.Data)));
815
816 case DeclarationName::CXXOperatorName:
817 return Context->DeclarationNames.getCXXOperatorName(
818 (OverloadedOperatorKind)Key.Data);
819
820 case DeclarationName::CXXLiteralOperatorName:
821 return Context->DeclarationNames.getCXXLiteralOperatorName(
822 (IdentifierInfo*)Key.Data);
823
824 case DeclarationName::CXXUsingDirective:
825 return DeclarationName::getUsingDirectiveName();
826 }
827
828 llvm_unreachable("Invalid Name Kind ?");
829 }
830
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000831 static std::pair<unsigned, unsigned>
832 ReadKeyDataLength(const unsigned char*& d) {
833 using namespace clang::io;
834 unsigned KeyLen = ReadUnalignedLE16(d);
835 unsigned DataLen = ReadUnalignedLE16(d);
836 return std::make_pair(KeyLen, DataLen);
837 }
838
839 internal_key_type ReadKey(const unsigned char* d, unsigned) {
840 using namespace clang::io;
841
842 DeclNameKey Key;
843 Key.Kind = (DeclarationName::NameKind)*d++;
844 switch (Key.Kind) {
845 case DeclarationName::Identifier:
846 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
847 break;
848 case DeclarationName::ObjCZeroArgSelector:
849 case DeclarationName::ObjCOneArgSelector:
850 case DeclarationName::ObjCMultiArgSelector:
851 Key.Data =
852 (uint64_t)Reader.DecodeSelector(ReadUnalignedLE32(d)).getAsOpaquePtr();
853 break;
854 case DeclarationName::CXXConstructorName:
855 case DeclarationName::CXXDestructorName:
856 case DeclarationName::CXXConversionFunctionName:
857 Key.Data = ReadUnalignedLE32(d); // TypeID
858 break;
859 case DeclarationName::CXXOperatorName:
860 Key.Data = *d++; // OverloadedOperatorKind
861 break;
862 case DeclarationName::CXXLiteralOperatorName:
863 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
864 break;
865 case DeclarationName::CXXUsingDirective:
866 break;
867 }
868
869 return Key;
870 }
871
872 data_type ReadData(internal_key_type, const unsigned char* d,
873 unsigned DataLen) {
874 using namespace clang::io;
875 unsigned NumDecls = ReadUnalignedLE16(d);
876 DeclID *Start = (DeclID *)d;
877 return std::make_pair(Start, Start + NumDecls);
878 }
879};
880
881} // end anonymous namespace
882
883/// \brief The on-disk hash table used for the DeclContext's Name lookup table.
884typedef OnDiskChainedHashTable<ASTDeclContextNameLookupTrait>
885 ASTDeclContextNameLookupTable;
886
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000887bool ASTReader::ReadDeclContextStorage(llvm::BitstreamCursor &Cursor,
888 const std::pair<uint64_t, uint64_t> &Offsets,
889 DeclContextInfo &Info) {
890 SavedStreamPosition SavedPosition(Cursor);
891 // First the lexical decls.
892 if (Offsets.first != 0) {
893 Cursor.JumpToBit(Offsets.first);
894
895 RecordData Record;
896 const char *Blob;
897 unsigned BlobLen;
898 unsigned Code = Cursor.ReadCode();
899 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
900 if (RecCode != DECL_CONTEXT_LEXICAL) {
901 Error("Expected lexical block");
902 return true;
903 }
904
905 Info.LexicalDecls = reinterpret_cast<const DeclID*>(Blob);
906 Info.NumLexicalDecls = BlobLen / sizeof(DeclID);
907 } else {
908 Info.LexicalDecls = 0;
909 Info.NumLexicalDecls = 0;
910 }
911
912 // Now the lookup table.
913 if (Offsets.second != 0) {
914 Cursor.JumpToBit(Offsets.second);
915
916 RecordData Record;
917 const char *Blob;
918 unsigned BlobLen;
919 unsigned Code = Cursor.ReadCode();
920 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
921 if (RecCode != DECL_CONTEXT_VISIBLE) {
922 Error("Expected visible lookup table block");
923 return true;
924 }
925 Info.NameLookupTableData
926 = ASTDeclContextNameLookupTable::Create(
927 (const unsigned char *)Blob + Record[0],
928 (const unsigned char *)Blob,
929 ASTDeclContextNameLookupTrait(*this));
930 }
931
932 return false;
933}
934
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000935void ASTReader::Error(const char *Msg) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000936 Diag(diag::err_fe_pch_malformed) << Msg;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000937}
938
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000939/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000940bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000941 if (Listener)
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000942 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000943 ActualOriginalFileName,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000944 SuggestedPredefines);
Douglas Gregore721f952009-04-28 18:58:38 +0000945 return false;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000946}
947
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000948//===----------------------------------------------------------------------===//
949// Source Manager Deserialization
950//===----------------------------------------------------------------------===//
951
Douglas Gregorbd945002009-04-13 16:31:14 +0000952/// \brief Read the line table in the source manager block.
953/// \returns true if ther was an error.
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000954bool ASTReader::ParseLineTable(llvm::SmallVectorImpl<uint64_t> &Record) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000955 unsigned Idx = 0;
956 LineTableInfo &LineTable = SourceMgr.getLineTable();
957
958 // Parse the file names
Douglas Gregorff0a9872009-04-13 17:12:42 +0000959 std::map<int, int> FileIDs;
960 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000961 // Extract the file name
962 unsigned FilenameLen = Record[Idx++];
963 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
964 Idx += FilenameLen;
Douglas Gregore650c8c2009-07-07 00:12:59 +0000965 MaybeAddSystemRootToFilename(Filename);
Mike Stump1eb44332009-09-09 15:08:12 +0000966 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
Douglas Gregorff0a9872009-04-13 17:12:42 +0000967 Filename.size());
Douglas Gregorbd945002009-04-13 16:31:14 +0000968 }
969
970 // Parse the line entries
971 std::vector<LineEntry> Entries;
972 while (Idx < Record.size()) {
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000973 int FID = Record[Idx++];
Douglas Gregorbd945002009-04-13 16:31:14 +0000974
975 // Extract the line entries
976 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000977 assert(NumEntries && "Numentries is 00000");
Douglas Gregorbd945002009-04-13 16:31:14 +0000978 Entries.clear();
979 Entries.reserve(NumEntries);
980 for (unsigned I = 0; I != NumEntries; ++I) {
981 unsigned FileOffset = Record[Idx++];
982 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000983 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump1eb44332009-09-09 15:08:12 +0000984 SrcMgr::CharacteristicKind FileKind
Douglas Gregorbd945002009-04-13 16:31:14 +0000985 = (SrcMgr::CharacteristicKind)Record[Idx++];
986 unsigned IncludeOffset = Record[Idx++];
987 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
988 FileKind, IncludeOffset));
989 }
990 LineTable.AddEntry(FID, Entries);
991 }
992
993 return false;
994}
995
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000996namespace {
997
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000998class ASTStatData {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000999public:
1000 const bool hasStat;
1001 const ino_t ino;
1002 const dev_t dev;
1003 const mode_t mode;
1004 const time_t mtime;
1005 const off_t size;
Mike Stump1eb44332009-09-09 15:08:12 +00001006
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001007 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Mike Stump1eb44332009-09-09 15:08:12 +00001008 : hasStat(true), ino(i), dev(d), mode(mo), mtime(m), size(s) {}
1009
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001010 ASTStatData()
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001011 : hasStat(false), ino(0), dev(0), mode(0), mtime(0), size(0) {}
1012};
1013
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001014class ASTStatLookupTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001015 public:
1016 typedef const char *external_key_type;
1017 typedef const char *internal_key_type;
1018
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001019 typedef ASTStatData data_type;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001020
1021 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001022 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001023 }
1024
1025 static internal_key_type GetInternalKey(const char *path) { return path; }
1026
1027 static bool EqualKey(internal_key_type a, internal_key_type b) {
1028 return strcmp(a, b) == 0;
1029 }
1030
1031 static std::pair<unsigned, unsigned>
1032 ReadKeyDataLength(const unsigned char*& d) {
1033 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1034 unsigned DataLen = (unsigned) *d++;
1035 return std::make_pair(KeyLen + 1, DataLen);
1036 }
1037
1038 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1039 return (const char *)d;
1040 }
1041
1042 static data_type ReadData(const internal_key_type, const unsigned char *d,
1043 unsigned /*DataLen*/) {
1044 using namespace clang::io;
1045
1046 if (*d++ == 1)
1047 return data_type();
1048
1049 ino_t ino = (ino_t) ReadUnalignedLE32(d);
1050 dev_t dev = (dev_t) ReadUnalignedLE32(d);
1051 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +00001052 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001053 off_t size = (off_t) ReadUnalignedLE64(d);
1054 return data_type(ino, dev, mode, mtime, size);
1055 }
1056};
1057
1058/// \brief stat() cache for precompiled headers.
1059///
1060/// This cache is very similar to the stat cache used by pretokenized
1061/// headers.
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001062class ASTStatCache : public StatSysCallCache {
1063 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001064 CacheTy *Cache;
1065
1066 unsigned &NumStatHits, &NumStatMisses;
Mike Stump1eb44332009-09-09 15:08:12 +00001067public:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001068 ASTStatCache(const unsigned char *Buckets,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001069 const unsigned char *Base,
1070 unsigned &NumStatHits,
Mike Stump1eb44332009-09-09 15:08:12 +00001071 unsigned &NumStatMisses)
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001072 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
1073 Cache = CacheTy::Create(Buckets, Base);
1074 }
1075
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001076 ~ASTStatCache() { delete Cache; }
Mike Stump1eb44332009-09-09 15:08:12 +00001077
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001078 int stat(const char *path, struct stat *buf) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001079 // Do the lookup for the file's data in the AST file.
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001080 CacheTy::iterator I = Cache->find(path);
1081
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001082 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001083 if (I == Cache->end()) {
1084 ++NumStatMisses;
Douglas Gregor52e71082009-10-16 18:18:30 +00001085 return StatSysCallCache::stat(path, buf);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001086 }
Mike Stump1eb44332009-09-09 15:08:12 +00001087
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001088 ++NumStatHits;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001089 ASTStatData Data = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001090
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001091 if (!Data.hasStat)
1092 return 1;
1093
1094 buf->st_ino = Data.ino;
1095 buf->st_dev = Data.dev;
1096 buf->st_mtime = Data.mtime;
1097 buf->st_mode = Data.mode;
1098 buf->st_size = Data.size;
1099 return 0;
1100 }
1101};
1102} // end anonymous namespace
1103
1104
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001105/// \brief Read a source manager block
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001106ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(PerFileData &F) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001107 using namespace SrcMgr;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001108
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001109 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001110
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001111 // Set the source-location entry cursor to the current position in
1112 // the stream. This cursor will be used to read the contents of the
1113 // source manager block initially, and then lazily read
1114 // source-location entries as needed.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001115 SLocEntryCursor = F.Stream;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001116
1117 // The stream itself is going to skip over the source manager block.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001118 if (F.Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001119 Error("malformed block record in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001120 return Failure;
1121 }
1122
1123 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001124 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001125 Error("malformed source manager block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001126 return Failure;
1127 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001128
Douglas Gregor14f79002009-04-10 03:52:48 +00001129 RecordData Record;
1130 while (true) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001131 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregor14f79002009-04-10 03:52:48 +00001132 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001133 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001134 Error("error at end of Source Manager block in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001135 return Failure;
1136 }
Douglas Gregore1d918e2009-04-10 23:10:45 +00001137 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +00001138 }
Mike Stump1eb44332009-09-09 15:08:12 +00001139
Douglas Gregor14f79002009-04-10 03:52:48 +00001140 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1141 // No known subblocks, always skip them.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001142 SLocEntryCursor.ReadSubBlockID();
1143 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001144 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001145 return Failure;
1146 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001147 continue;
1148 }
Mike Stump1eb44332009-09-09 15:08:12 +00001149
Douglas Gregor14f79002009-04-10 03:52:48 +00001150 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001151 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregor14f79002009-04-10 03:52:48 +00001152 continue;
1153 }
Mike Stump1eb44332009-09-09 15:08:12 +00001154
Douglas Gregor14f79002009-04-10 03:52:48 +00001155 // Read a record.
1156 const char *BlobStart;
1157 unsigned BlobLen;
1158 Record.clear();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001159 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001160 default: // Default behavior: ignore.
1161 break;
1162
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001163 case SM_LINE_TABLE:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001164 if (ParseLineTable(Record))
Douglas Gregorbd945002009-04-13 16:31:14 +00001165 return Failure;
Chris Lattner2c78b872009-04-14 23:22:57 +00001166 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001167
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001168 case SM_SLOC_FILE_ENTRY:
1169 case SM_SLOC_BUFFER_ENTRY:
1170 case SM_SLOC_INSTANTIATION_ENTRY:
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001171 // Once we hit one of the source location entries, we're done.
1172 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +00001173 }
1174 }
1175}
1176
Sebastian Redl190faf72010-07-20 21:50:20 +00001177/// \brief Get a cursor that's correctly positioned for reading the source
1178/// location entry with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001179llvm::BitstreamCursor &ASTReader::SLocCursorForID(unsigned ID) {
Sebastian Redl190faf72010-07-20 21:50:20 +00001180 assert(ID != 0 && ID <= TotalNumSLocEntries &&
1181 "SLocCursorForID should only be called for real IDs.");
1182
1183 ID -= 1;
1184 PerFileData *F = 0;
1185 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1186 F = Chain[N - I - 1];
1187 if (ID < F->LocalNumSLocEntries)
1188 break;
1189 ID -= F->LocalNumSLocEntries;
1190 }
1191 assert(F && F->LocalNumSLocEntries > ID && "Chain corrupted");
1192
1193 F->SLocEntryCursor.JumpToBit(F->SLocOffsets[ID]);
1194 return F->SLocEntryCursor;
1195}
1196
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001197/// \brief Read in the source location entry with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001198ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(unsigned ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001199 if (ID == 0)
1200 return Success;
1201
1202 if (ID > TotalNumSLocEntries) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001203 Error("source location entry ID out-of-range for AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001204 return Failure;
1205 }
1206
Sebastian Redl190faf72010-07-20 21:50:20 +00001207 llvm::BitstreamCursor &SLocEntryCursor = SLocCursorForID(ID);
Sebastian Redl9137a522010-07-16 17:50:48 +00001208
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001209 ++NumSLocEntriesRead;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001210 unsigned Code = SLocEntryCursor.ReadCode();
1211 if (Code == llvm::bitc::END_BLOCK ||
1212 Code == llvm::bitc::ENTER_SUBBLOCK ||
1213 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001214 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001215 return Failure;
1216 }
1217
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001218 RecordData Record;
1219 const char *BlobStart;
1220 unsigned BlobLen;
1221 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1222 default:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001223 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001224 return Failure;
1225
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001226 case SM_SLOC_FILE_ENTRY: {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001227 std::string Filename(BlobStart, BlobStart + BlobLen);
1228 MaybeAddSystemRootToFilename(Filename);
1229 const FileEntry *File = FileMgr.getFile(Filename);
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001230 if (File == 0) {
1231 std::string ErrorStr = "could not find file '";
Douglas Gregore650c8c2009-07-07 00:12:59 +00001232 ErrorStr += Filename;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001233 ErrorStr += "' referenced by AST file";
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001234 Error(ErrorStr.c_str());
1235 return Failure;
1236 }
Mike Stump1eb44332009-09-09 15:08:12 +00001237
Douglas Gregor2d52be52010-03-21 22:49:54 +00001238 if (Record.size() < 10) {
Ted Kremenek1857f622010-03-18 21:23:05 +00001239 Error("source location entry is incorrect");
1240 return Failure;
1241 }
1242
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001243 if (!DisableValidation &&
1244 ((off_t)Record[4] != File->getSize()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001245#if !defined(LLVM_ON_WIN32)
1246 // In our regression testing, the Windows file system seems to
1247 // have inconsistent modification times that sometimes
1248 // erroneously trigger this error-handling path.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001249 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001250#endif
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001251 )) {
Douglas Gregor2d52be52010-03-21 22:49:54 +00001252 Diag(diag::err_fe_pch_file_modified)
1253 << Filename;
1254 return Failure;
1255 }
1256
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001257 FileID FID = SourceMgr.createFileID(File,
1258 SourceLocation::getFromRawEncoding(Record[1]),
1259 (SrcMgr::CharacteristicKind)Record[2],
1260 ID, Record[0]);
1261 if (Record[3])
1262 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile())
1263 .setHasLineDirectives();
1264
Douglas Gregor12fab312010-03-16 16:35:32 +00001265 // Reconstruct header-search information for this file.
1266 HeaderFileInfo HFI;
Douglas Gregor2d52be52010-03-21 22:49:54 +00001267 HFI.isImport = Record[6];
1268 HFI.DirInfo = Record[7];
1269 HFI.NumIncludes = Record[8];
1270 HFI.ControllingMacroID = Record[9];
Douglas Gregor12fab312010-03-16 16:35:32 +00001271 if (Listener)
1272 Listener->ReadHeaderFileInfo(HFI, File->getUID());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001273 break;
1274 }
1275
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001276 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001277 const char *Name = BlobStart;
1278 unsigned Offset = Record[0];
1279 unsigned Code = SLocEntryCursor.ReadCode();
1280 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001281 unsigned RecCode
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001282 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001283
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001284 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001285 Error("AST record has invalid code");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001286 return Failure;
1287 }
1288
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001289 llvm::MemoryBuffer *Buffer
Chris Lattnera0a270c2010-04-05 22:42:27 +00001290 = llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(BlobStart, BlobLen - 1),
1291 Name);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001292 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset);
Mike Stump1eb44332009-09-09 15:08:12 +00001293
Douglas Gregor92b059e2009-04-28 20:33:11 +00001294 if (strcmp(Name, "<built-in>") == 0) {
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +00001295 PCHPredefinesBlock Block = {
1296 BufferID,
1297 llvm::StringRef(BlobStart, BlobLen - 1)
1298 };
1299 PCHPredefinesBuffers.push_back(Block);
Douglas Gregor92b059e2009-04-28 20:33:11 +00001300 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001301
1302 break;
1303 }
1304
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001305 case SM_SLOC_INSTANTIATION_ENTRY: {
Mike Stump1eb44332009-09-09 15:08:12 +00001306 SourceLocation SpellingLoc
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001307 = SourceLocation::getFromRawEncoding(Record[1]);
1308 SourceMgr.createInstantiationLoc(SpellingLoc,
1309 SourceLocation::getFromRawEncoding(Record[2]),
1310 SourceLocation::getFromRawEncoding(Record[3]),
1311 Record[4],
1312 ID,
1313 Record[0]);
1314 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001315 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001316 }
1317
1318 return Success;
1319}
1320
Chris Lattner6367f6d2009-04-27 01:05:14 +00001321/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1322/// specified cursor. Read the abbreviations that are at the top of the block
1323/// and then leave the cursor pointing into the block.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001324bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattner6367f6d2009-04-27 01:05:14 +00001325 unsigned BlockID) {
1326 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001327 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001328 return Failure;
1329 }
Mike Stump1eb44332009-09-09 15:08:12 +00001330
Chris Lattner6367f6d2009-04-27 01:05:14 +00001331 while (true) {
1332 unsigned Code = Cursor.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00001333
Chris Lattner6367f6d2009-04-27 01:05:14 +00001334 // We expect all abbrevs to be at the start of the block.
1335 if (Code != llvm::bitc::DEFINE_ABBREV)
1336 return false;
1337 Cursor.ReadAbbrevRecord();
1338 }
1339}
1340
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001341void ASTReader::ReadMacroRecord(llvm::BitstreamCursor &Stream, uint64_t Offset){
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001342 assert(PP && "Forgot to set Preprocessor ?");
Mike Stump1eb44332009-09-09 15:08:12 +00001343
Douglas Gregor37e26842009-04-21 23:56:24 +00001344 // Keep track of where we are in the stream, then jump back there
1345 // after reading this macro.
1346 SavedStreamPosition SavedPosition(Stream);
1347
1348 Stream.JumpToBit(Offset);
1349 RecordData Record;
1350 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1351 MacroInfo *Macro = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001352
Douglas Gregor37e26842009-04-21 23:56:24 +00001353 while (true) {
1354 unsigned Code = Stream.ReadCode();
1355 switch (Code) {
1356 case llvm::bitc::END_BLOCK:
1357 return;
1358
1359 case llvm::bitc::ENTER_SUBBLOCK:
1360 // No known subblocks, always skip them.
1361 Stream.ReadSubBlockID();
1362 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001363 Error("malformed block record in AST file");
Douglas Gregor37e26842009-04-21 23:56:24 +00001364 return;
1365 }
1366 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001367
Douglas Gregor37e26842009-04-21 23:56:24 +00001368 case llvm::bitc::DEFINE_ABBREV:
1369 Stream.ReadAbbrevRecord();
1370 continue;
1371 default: break;
1372 }
1373
1374 // Read a record.
1375 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001376 PreprocessorRecordTypes RecType =
1377 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
Douglas Gregor37e26842009-04-21 23:56:24 +00001378 switch (RecType) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001379 case PP_MACRO_OBJECT_LIKE:
1380 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001381 // If we already have a macro, that means that we've hit the end
1382 // of the definition of the macro we were looking for. We're
1383 // done.
1384 if (Macro)
1385 return;
1386
1387 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1388 if (II == 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001389 Error("macro must have a name in AST file");
Douglas Gregor37e26842009-04-21 23:56:24 +00001390 return;
1391 }
1392 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
1393 bool isUsed = Record[2];
Mike Stump1eb44332009-09-09 15:08:12 +00001394
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001395 MacroInfo *MI = PP->AllocateMacroInfo(Loc);
Douglas Gregor37e26842009-04-21 23:56:24 +00001396 MI->setIsUsed(isUsed);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001397 MI->setIsFromAST();
Mike Stump1eb44332009-09-09 15:08:12 +00001398
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001399 unsigned NextIndex = 3;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001400 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregor37e26842009-04-21 23:56:24 +00001401 // Decode function-like macro info.
1402 bool isC99VarArgs = Record[3];
1403 bool isGNUVarArgs = Record[4];
1404 MacroArgs.clear();
1405 unsigned NumArgs = Record[5];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001406 NextIndex = 6 + NumArgs;
Douglas Gregor37e26842009-04-21 23:56:24 +00001407 for (unsigned i = 0; i != NumArgs; ++i)
1408 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1409
1410 // Install function-like macro info.
1411 MI->setIsFunctionLike();
1412 if (isC99VarArgs) MI->setIsC99Varargs();
1413 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor75fdb232009-05-22 22:45:36 +00001414 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001415 PP->getPreprocessorAllocator());
Douglas Gregor37e26842009-04-21 23:56:24 +00001416 }
1417
1418 // Finally, install the macro.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001419 PP->setMacroInfo(II, MI);
Douglas Gregor37e26842009-04-21 23:56:24 +00001420
1421 // Remember that we saw this macro last so that we add the tokens that
1422 // form its body to it.
1423 Macro = MI;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001424
1425 if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) {
1426 // We have a macro definition. Load it now.
1427 PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro,
1428 getMacroDefinition(Record[NextIndex]));
1429 }
1430
Douglas Gregor37e26842009-04-21 23:56:24 +00001431 ++NumMacrosRead;
1432 break;
1433 }
Mike Stump1eb44332009-09-09 15:08:12 +00001434
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001435 case PP_TOKEN: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001436 // If we see a TOKEN before a PP_MACRO_*, then the file is
1437 // erroneous, just pretend we didn't see this.
1438 if (Macro == 0) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001439
Douglas Gregor37e26842009-04-21 23:56:24 +00001440 Token Tok;
1441 Tok.startToken();
1442 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
1443 Tok.setLength(Record[1]);
1444 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1445 Tok.setIdentifierInfo(II);
1446 Tok.setKind((tok::TokenKind)Record[3]);
1447 Tok.setFlag((Token::TokenFlags)Record[4]);
1448 Macro->AddTokenToBody(Tok);
1449 break;
1450 }
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001451
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001452 case PP_MACRO_INSTANTIATION: {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001453 // If we already have a macro, that means that we've hit the end
1454 // of the definition of the macro we were looking for. We're
1455 // done.
1456 if (Macro)
1457 return;
1458
1459 if (!PP->getPreprocessingRecord()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001460 Error("missing preprocessing record in AST file");
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001461 return;
1462 }
1463
1464 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1465 if (PPRec.getPreprocessedEntity(Record[0]))
1466 return;
1467
1468 MacroInstantiation *MI
1469 = new (PPRec) MacroInstantiation(DecodeIdentifierInfo(Record[3]),
1470 SourceRange(
1471 SourceLocation::getFromRawEncoding(Record[1]),
1472 SourceLocation::getFromRawEncoding(Record[2])),
1473 getMacroDefinition(Record[4]));
1474 PPRec.SetPreallocatedEntity(Record[0], MI);
1475 return;
1476 }
1477
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001478 case PP_MACRO_DEFINITION: {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001479 // If we already have a macro, that means that we've hit the end
1480 // of the definition of the macro we were looking for. We're
1481 // done.
1482 if (Macro)
1483 return;
1484
1485 if (!PP->getPreprocessingRecord()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001486 Error("missing preprocessing record in AST file");
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001487 return;
1488 }
1489
1490 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1491 if (PPRec.getPreprocessedEntity(Record[0]))
1492 return;
1493
1494 if (Record[1] >= MacroDefinitionsLoaded.size()) {
1495 Error("out-of-bounds macro definition record");
1496 return;
1497 }
1498
1499 MacroDefinition *MD
1500 = new (PPRec) MacroDefinition(DecodeIdentifierInfo(Record[4]),
1501 SourceLocation::getFromRawEncoding(Record[5]),
1502 SourceRange(
1503 SourceLocation::getFromRawEncoding(Record[2]),
1504 SourceLocation::getFromRawEncoding(Record[3])));
1505 PPRec.SetPreallocatedEntity(Record[0], MD);
1506 MacroDefinitionsLoaded[Record[1]] = MD;
1507 return;
1508 }
Steve Naroff83d63c72009-04-24 20:03:17 +00001509 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001510 }
1511}
1512
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001513void ASTReader::ReadDefinedMacros() {
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001514 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1515 llvm::BitstreamCursor &MacroCursor = Chain[N - I - 1]->MacroCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001516
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001517 // If there was no preprocessor block, skip this file.
1518 if (!MacroCursor.getBitStreamReader())
1519 continue;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001520
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001521 llvm::BitstreamCursor Cursor = MacroCursor;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001522 if (Cursor.EnterSubBlock(PREPROCESSOR_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001523 Error("malformed preprocessor block record in AST file");
Douglas Gregor88a35862010-01-04 19:18:44 +00001524 return;
1525 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001526
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001527 RecordData Record;
1528 while (true) {
1529 unsigned Code = Cursor.ReadCode();
1530 if (Code == llvm::bitc::END_BLOCK) {
1531 if (Cursor.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001532 Error("error at end of preprocessor block in AST file");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001533 return;
1534 }
1535 break;
Douglas Gregor88a35862010-01-04 19:18:44 +00001536 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001537
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001538 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1539 // No known subblocks, always skip them.
1540 Cursor.ReadSubBlockID();
1541 if (Cursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001542 Error("malformed block record in AST file");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001543 return;
1544 }
1545 continue;
1546 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001547
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001548 if (Code == llvm::bitc::DEFINE_ABBREV) {
1549 Cursor.ReadAbbrevRecord();
1550 continue;
1551 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001552
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001553 // Read a record.
1554 const char *BlobStart;
1555 unsigned BlobLen;
1556 Record.clear();
1557 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1558 default: // Default behavior: ignore.
1559 break;
Douglas Gregor88a35862010-01-04 19:18:44 +00001560
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001561 case PP_MACRO_OBJECT_LIKE:
1562 case PP_MACRO_FUNCTION_LIKE:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001563 DecodeIdentifierInfo(Record[0]);
1564 break;
1565
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001566 case PP_TOKEN:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001567 // Ignore tokens.
1568 break;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001569
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001570 case PP_MACRO_INSTANTIATION:
1571 case PP_MACRO_DEFINITION:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001572 // Read the macro record.
1573 ReadMacroRecord(Chain[N - I - 1]->Stream, Cursor.GetCurrentBitNo());
1574 break;
1575 }
Douglas Gregor88a35862010-01-04 19:18:44 +00001576 }
1577 }
1578}
1579
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001580MacroDefinition *ASTReader::getMacroDefinition(IdentID ID) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001581 if (ID == 0 || ID >= MacroDefinitionsLoaded.size())
1582 return 0;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001583
1584 if (!MacroDefinitionsLoaded[ID]) {
1585 unsigned Index = ID;
1586 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1587 PerFileData &F = *Chain[N - I - 1];
1588 if (Index < F.LocalNumMacroDefinitions) {
1589 ReadMacroRecord(F.Stream, F.MacroDefinitionOffsets[Index]);
1590 break;
1591 }
1592 Index -= F.LocalNumMacroDefinitions;
1593 }
1594 assert(MacroDefinitionsLoaded[ID] && "Broken chain");
1595 }
1596
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001597 return MacroDefinitionsLoaded[ID];
1598}
1599
Douglas Gregore650c8c2009-07-07 00:12:59 +00001600/// \brief If we are loading a relocatable PCH file, and the filename is
1601/// not an absolute path, add the system root to the beginning of the file
1602/// name.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001603void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001604 // If this is not a relocatable PCH file, there's nothing to do.
1605 if (!RelocatablePCH)
1606 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001607
Daniel Dunbard5b21972009-11-18 19:50:41 +00001608 if (Filename.empty() || llvm::sys::Path(Filename).isAbsolute())
Douglas Gregore650c8c2009-07-07 00:12:59 +00001609 return;
1610
Douglas Gregore650c8c2009-07-07 00:12:59 +00001611 if (isysroot == 0) {
1612 // If no system root was given, default to '/'
1613 Filename.insert(Filename.begin(), '/');
1614 return;
1615 }
Mike Stump1eb44332009-09-09 15:08:12 +00001616
Douglas Gregore650c8c2009-07-07 00:12:59 +00001617 unsigned Length = strlen(isysroot);
1618 if (isysroot[Length - 1] != '/')
1619 Filename.insert(Filename.begin(), '/');
Mike Stump1eb44332009-09-09 15:08:12 +00001620
Douglas Gregore650c8c2009-07-07 00:12:59 +00001621 Filename.insert(Filename.begin(), isysroot, isysroot + Length);
1622}
1623
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001624ASTReader::ASTReadResult
Sebastian Redl571db7f2010-08-18 23:56:56 +00001625ASTReader::ReadASTBlock(PerFileData &F) {
Sebastian Redl9137a522010-07-16 17:50:48 +00001626 llvm::BitstreamCursor &Stream = F.Stream;
1627
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001628 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001629 Error("malformed block record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001630 return Failure;
1631 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001632
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001633 // Read all of the records and blocks for the ASt file.
Douglas Gregor8038d512009-04-10 17:25:41 +00001634 RecordData Record;
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001635 bool First = true;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001636 while (!Stream.AtEndOfStream()) {
1637 unsigned Code = Stream.ReadCode();
1638 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001639 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001640 Error("error at end of module block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001641 return Failure;
1642 }
Chris Lattner7356a312009-04-11 21:15:38 +00001643
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001644 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001645 }
1646
1647 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1648 switch (Stream.ReadSubBlockID()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001649 case DECLTYPES_BLOCK_ID:
Chris Lattner6367f6d2009-04-27 01:05:14 +00001650 // We lazily load the decls block, but we want to set up the
1651 // DeclsCursor cursor to point into it. Clone our current bitcode
1652 // cursor to it, enter the block and read the abbrevs in that block.
1653 // With the main cursor, we just skip over it.
Sebastian Redl9137a522010-07-16 17:50:48 +00001654 F.DeclsCursor = Stream;
Chris Lattner6367f6d2009-04-27 01:05:14 +00001655 if (Stream.SkipBlock() || // Skip with the main cursor.
1656 // Read the abbrevs.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001657 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001658 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001659 return Failure;
1660 }
1661 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001662
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001663 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl9137a522010-07-16 17:50:48 +00001664 F.MacroCursor = Stream;
Douglas Gregor88a35862010-01-04 19:18:44 +00001665 if (PP)
1666 PP->setExternalSource(this);
1667
Chris Lattner7356a312009-04-11 21:15:38 +00001668 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001669 Error("malformed block record in AST file");
Chris Lattner7356a312009-04-11 21:15:38 +00001670 return Failure;
1671 }
1672 break;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001673
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001674 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001675 switch (ReadSourceManagerBlock(F)) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00001676 case Success:
1677 break;
1678
1679 case Failure:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001680 Error("malformed source manager block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001681 return Failure;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001682
1683 case IgnorePCH:
1684 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001685 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001686 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001687 }
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001688 First = false;
Douglas Gregor8038d512009-04-10 17:25:41 +00001689 continue;
1690 }
1691
1692 if (Code == llvm::bitc::DEFINE_ABBREV) {
1693 Stream.ReadAbbrevRecord();
1694 continue;
1695 }
1696
1697 // Read and process a record.
1698 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00001699 const char *BlobStart = 0;
1700 unsigned BlobLen = 0;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001701 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Douglas Gregor2bec0412009-04-10 21:16:55 +00001702 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001703 default: // Default behavior: ignore.
1704 break;
1705
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001706 case METADATA: {
1707 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1708 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001709 : diag::warn_pch_version_too_new);
1710 return IgnorePCH;
1711 }
1712
1713 RelocatablePCH = Record[4];
1714 if (Listener) {
1715 std::string TargetTriple(BlobStart, BlobLen);
1716 if (Listener->ReadTargetTriple(TargetTriple))
1717 return IgnorePCH;
1718 }
1719 break;
1720 }
1721
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001722 case CHAINED_METADATA: {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001723 if (!First) {
1724 Error("CHAINED_METADATA is not first record in block");
1725 return Failure;
1726 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001727 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1728 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001729 : diag::warn_pch_version_too_new);
1730 return IgnorePCH;
1731 }
1732
1733 // Load the chained file.
Sebastian Redl571db7f2010-08-18 23:56:56 +00001734 switch(ReadASTCore(llvm::StringRef(BlobStart, BlobLen))) {
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001735 case Failure: return Failure;
1736 // If we have to ignore the dependency, we'll have to ignore this too.
1737 case IgnorePCH: return IgnorePCH;
1738 case Success: break;
1739 }
1740 break;
1741 }
1742
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001743 case TYPE_OFFSET:
Sebastian Redl12d6da02010-07-19 22:06:55 +00001744 if (F.LocalNumTypes != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001745 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001746 return Failure;
1747 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00001748 F.TypeOffsets = (const uint32_t *)BlobStart;
1749 F.LocalNumTypes = Record[0];
Douglas Gregor8038d512009-04-10 17:25:41 +00001750 break;
1751
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001752 case DECL_OFFSET:
Sebastian Redl12d6da02010-07-19 22:06:55 +00001753 if (F.LocalNumDecls != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001754 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001755 return Failure;
1756 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00001757 F.DeclOffsets = (const uint32_t *)BlobStart;
1758 F.LocalNumDecls = Record[0];
Douglas Gregor8038d512009-04-10 17:25:41 +00001759 break;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001760
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001761 case TU_UPDATE_LEXICAL: {
Sebastian Redld692af72010-07-27 18:24:41 +00001762 DeclContextInfo Info = {
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001763 /* No visible information */ 0,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001764 reinterpret_cast<const DeclID *>(BlobStart),
1765 BlobLen / sizeof(DeclID)
Sebastian Redld692af72010-07-27 18:24:41 +00001766 };
1767 DeclContextOffsets[Context->getTranslationUnitDecl()].push_back(Info);
1768 break;
1769 }
1770
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001771 case REDECLS_UPDATE_LATEST: {
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00001772 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
1773 for (unsigned i = 0, e = Record.size(); i < e; i += 2) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001774 DeclID First = Record[i], Latest = Record[i+1];
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00001775 assert((FirstLatestDeclIDs.find(First) == FirstLatestDeclIDs.end() ||
1776 Latest > FirstLatestDeclIDs[First]) &&
1777 "The new latest is supposed to come after the previous latest");
1778 FirstLatestDeclIDs[First] = Latest;
1779 }
1780 break;
1781 }
1782
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001783 case LANGUAGE_OPTIONS:
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001784 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001785 return IgnorePCH;
1786 break;
Douglas Gregor2bec0412009-04-10 21:16:55 +00001787
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001788 case IDENTIFIER_TABLE:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001789 F.IdentifierTableData = BlobStart;
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001790 if (Record[0]) {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001791 F.IdentifierLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001792 = ASTIdentifierLookupTable::Create(
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001793 (const unsigned char *)F.IdentifierTableData + Record[0],
1794 (const unsigned char *)F.IdentifierTableData,
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001795 ASTIdentifierLookupTrait(*this, F.Stream));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001796 if (PP)
1797 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001798 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001799 break;
1800
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001801 case IDENTIFIER_OFFSET:
Sebastian Redl2da08f92010-07-19 22:28:42 +00001802 if (F.LocalNumIdentifiers != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001803 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00001804 return Failure;
1805 }
Sebastian Redl2da08f92010-07-19 22:28:42 +00001806 F.IdentifierOffsets = (const uint32_t *)BlobStart;
1807 F.LocalNumIdentifiers = Record[0];
Douglas Gregorafaf3082009-04-11 00:14:32 +00001808 break;
Douglas Gregorfdd01722009-04-14 00:24:19 +00001809
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001810 case EXTERNAL_DEFINITIONS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001811 // Optimization for the first block.
1812 if (ExternalDefinitions.empty())
1813 ExternalDefinitions.swap(Record);
1814 else
1815 ExternalDefinitions.insert(ExternalDefinitions.end(),
1816 Record.begin(), Record.end());
Douglas Gregorfdd01722009-04-14 00:24:19 +00001817 break;
Douglas Gregor3e1af842009-04-17 22:13:46 +00001818
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001819 case SPECIAL_TYPES:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001820 // Optimization for the first block
1821 if (SpecialTypes.empty())
1822 SpecialTypes.swap(Record);
1823 else
1824 SpecialTypes.insert(SpecialTypes.end(), Record.begin(), Record.end());
Douglas Gregorad1de002009-04-18 05:55:16 +00001825 break;
1826
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001827 case STATISTICS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001828 TotalNumStatements += Record[0];
1829 TotalNumMacros += Record[1];
1830 TotalLexicalDeclContexts += Record[2];
1831 TotalVisibleDeclContexts += Record[3];
Douglas Gregor3e1af842009-04-17 22:13:46 +00001832 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001833
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001834 case TENTATIVE_DEFINITIONS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001835 // Optimization for the first block.
1836 if (TentativeDefinitions.empty())
1837 TentativeDefinitions.swap(Record);
1838 else
1839 TentativeDefinitions.insert(TentativeDefinitions.end(),
1840 Record.begin(), Record.end());
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00001841 break;
Douglas Gregor14c22f22009-04-22 22:18:58 +00001842
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001843 case UNUSED_FILESCOPED_DECLS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001844 // Optimization for the first block.
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00001845 if (UnusedFileScopedDecls.empty())
1846 UnusedFileScopedDecls.swap(Record);
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001847 else
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00001848 UnusedFileScopedDecls.insert(UnusedFileScopedDecls.end(),
1849 Record.begin(), Record.end());
Tanya Lattnere6bbc012010-02-12 00:07:30 +00001850 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001851
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001852 case WEAK_UNDECLARED_IDENTIFIERS:
Sebastian Redl40566802010-08-05 18:21:25 +00001853 // Later blocks overwrite earlier ones.
1854 WeakUndeclaredIdentifiers.swap(Record);
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00001855 break;
1856
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001857 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001858 // Optimization for the first block.
1859 if (LocallyScopedExternalDecls.empty())
1860 LocallyScopedExternalDecls.swap(Record);
1861 else
1862 LocallyScopedExternalDecls.insert(LocallyScopedExternalDecls.end(),
1863 Record.begin(), Record.end());
Douglas Gregor14c22f22009-04-22 22:18:58 +00001864 break;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001865
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001866 case SELECTOR_OFFSETS:
Sebastian Redl059612d2010-08-03 21:58:15 +00001867 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redl725cd962010-08-04 20:40:17 +00001868 F.LocalNumSelectors = Record[0];
Douglas Gregor83941df2009-04-25 17:48:32 +00001869 break;
1870
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001871 case METHOD_POOL:
Sebastian Redl725cd962010-08-04 20:40:17 +00001872 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor83941df2009-04-25 17:48:32 +00001873 if (Record[0])
Sebastian Redl725cd962010-08-04 20:40:17 +00001874 F.SelectorLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001875 = ASTSelectorLookupTable::Create(
Sebastian Redl725cd962010-08-04 20:40:17 +00001876 F.SelectorLookupTableData + Record[0],
1877 F.SelectorLookupTableData,
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001878 ASTSelectorLookupTrait(*this));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00001879 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001880 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001881
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001882 case REFERENCED_SELECTOR_POOL: {
Sebastian Redl725cd962010-08-04 20:40:17 +00001883 ReferencedSelectorsData.insert(ReferencedSelectorsData.end(),
1884 Record.begin(), Record.end());
Fariborz Jahanian32019832010-07-23 19:11:11 +00001885 break;
Sebastian Redl681d7232010-07-27 00:17:23 +00001886 }
Fariborz Jahanian32019832010-07-23 19:11:11 +00001887
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001888 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001889 if (!Record.empty() && Listener)
1890 Listener->ReadCounter(Record[0]);
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001891 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001892
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001893 case SOURCE_LOCATION_OFFSETS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001894 F.SLocOffsets = (const uint32_t *)BlobStart;
1895 F.LocalNumSLocEntries = Record[0];
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001896 // We cannot delay this until the entire chain is loaded, because then
1897 // source location preloads would also have to be delayed.
1898 // FIXME: Is there a reason not to do that?
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001899 TotalNumSLocEntries += F.LocalNumSLocEntries;
Douglas Gregor445e23e2009-10-05 21:07:28 +00001900 SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, Record[1]);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001901 break;
1902
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001903 case SOURCE_LOCATION_PRELOADS:
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001904 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001905 ASTReadResult Result = ReadSLocEntryRecord(Record[I]);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001906 if (Result != Success)
1907 return Result;
1908 }
1909 break;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001910
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001911 case STAT_CACHE: {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001912 ASTStatCache *MyStatCache =
1913 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
Douglas Gregor52e71082009-10-16 18:18:30 +00001914 (const unsigned char *)BlobStart,
1915 NumStatHits, NumStatMisses);
1916 FileMgr.addStatCache(MyStatCache);
Sebastian Redl9137a522010-07-16 17:50:48 +00001917 F.StatCache = MyStatCache;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001918 break;
Douglas Gregor52e71082009-10-16 18:18:30 +00001919 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001920
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001921 case EXT_VECTOR_DECLS:
Sebastian Redla9f23682010-07-28 21:38:49 +00001922 // Optimization for the first block.
1923 if (ExtVectorDecls.empty())
1924 ExtVectorDecls.swap(Record);
1925 else
1926 ExtVectorDecls.insert(ExtVectorDecls.end(),
1927 Record.begin(), Record.end());
Douglas Gregorb81c1702009-04-27 20:06:05 +00001928 break;
1929
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001930 case VTABLE_USES:
Sebastian Redl40566802010-08-05 18:21:25 +00001931 // Later tables overwrite earlier ones.
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00001932 VTableUses.swap(Record);
1933 break;
1934
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001935 case DYNAMIC_CLASSES:
Sebastian Redl40566802010-08-05 18:21:25 +00001936 // Optimization for the first block.
1937 if (DynamicClasses.empty())
1938 DynamicClasses.swap(Record);
1939 else
1940 DynamicClasses.insert(DynamicClasses.end(),
1941 Record.begin(), Record.end());
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00001942 break;
1943
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001944 case PENDING_IMPLICIT_INSTANTIATIONS:
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00001945 // Optimization for the first block.
1946 if (PendingImplicitInstantiations.empty())
1947 PendingImplicitInstantiations.swap(Record);
1948 else
1949 PendingImplicitInstantiations.insert(
1950 PendingImplicitInstantiations.end(), Record.begin(), Record.end());
1951 break;
1952
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001953 case SEMA_DECL_REFS:
Sebastian Redl40566802010-08-05 18:21:25 +00001954 // Later tables overwrite earlier ones.
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00001955 SemaDeclRefs.swap(Record);
1956 break;
1957
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001958 case ORIGINAL_FILE_NAME:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001959 // The primary AST will be the last to get here, so it will be the one
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001960 // that's used.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +00001961 ActualOriginalFileName.assign(BlobStart, BlobLen);
1962 OriginalFileName = ActualOriginalFileName;
Douglas Gregore650c8c2009-07-07 00:12:59 +00001963 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001964 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001965
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001966 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek974be4d2010-02-12 23:31:14 +00001967 const std::string &CurBranch = getClangFullRepositoryVersion();
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001968 llvm::StringRef ASTBranch(BlobStart, BlobLen);
1969 if (llvm::StringRef(CurBranch) != ASTBranch && !DisableValidation) {
1970 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregor445e23e2009-10-05 21:07:28 +00001971 return IgnorePCH;
1972 }
1973 break;
1974 }
Sebastian Redl04e6fd42010-07-21 20:07:32 +00001975
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001976 case MACRO_DEFINITION_OFFSETS:
Sebastian Redl04e6fd42010-07-21 20:07:32 +00001977 F.MacroDefinitionOffsets = (const uint32_t *)BlobStart;
1978 F.NumPreallocatedPreprocessingEntities = Record[0];
1979 F.LocalNumMacroDefinitions = Record[1];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001980 break;
Sebastian Redl0b17c612010-08-13 00:28:03 +00001981
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001982 case DECL_REPLACEMENTS: {
Sebastian Redl0b17c612010-08-13 00:28:03 +00001983 if (Record.size() % 2 != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001984 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redl0b17c612010-08-13 00:28:03 +00001985 return Failure;
1986 }
1987 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001988 ReplacedDecls[static_cast<DeclID>(Record[I])] =
Sebastian Redl0b17c612010-08-13 00:28:03 +00001989 std::make_pair(&F, Record[I+1]);
1990 break;
1991 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001992 }
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001993 First = false;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001994 }
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001995 Error("premature end of bitstream in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001996 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001997}
1998
Sebastian Redl571db7f2010-08-18 23:56:56 +00001999ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName) {
2000 switch(ReadASTCore(FileName)) {
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002001 case Failure: return Failure;
2002 case IgnorePCH: return IgnorePCH;
2003 case Success: break;
2004 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002005
2006 // Here comes stuff that we only do once the entire chain is loaded.
2007
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002008 // Allocate space for loaded identifiers, decls and types.
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002009 unsigned TotalNumIdentifiers = 0, TotalNumTypes = 0, TotalNumDecls = 0,
Sebastian Redl725cd962010-08-04 20:40:17 +00002010 TotalNumPreallocatedPreprocessingEntities = 0, TotalNumMacroDefs = 0,
2011 TotalNumSelectors = 0;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002012 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl2da08f92010-07-19 22:28:42 +00002013 TotalNumIdentifiers += Chain[I]->LocalNumIdentifiers;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002014 TotalNumTypes += Chain[I]->LocalNumTypes;
2015 TotalNumDecls += Chain[I]->LocalNumDecls;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002016 TotalNumPreallocatedPreprocessingEntities +=
2017 Chain[I]->NumPreallocatedPreprocessingEntities;
2018 TotalNumMacroDefs += Chain[I]->LocalNumMacroDefinitions;
Sebastian Redl725cd962010-08-04 20:40:17 +00002019 TotalNumSelectors += Chain[I]->LocalNumSelectors;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002020 }
Sebastian Redl2da08f92010-07-19 22:28:42 +00002021 IdentifiersLoaded.resize(TotalNumIdentifiers);
Sebastian Redl12d6da02010-07-19 22:06:55 +00002022 TypesLoaded.resize(TotalNumTypes);
2023 DeclsLoaded.resize(TotalNumDecls);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002024 MacroDefinitionsLoaded.resize(TotalNumMacroDefs);
2025 if (PP) {
2026 if (TotalNumIdentifiers > 0)
2027 PP->getHeaderSearchInfo().SetExternalLookup(this);
2028 if (TotalNumPreallocatedPreprocessingEntities > 0) {
2029 if (!PP->getPreprocessingRecord())
2030 PP->createPreprocessingRecord();
2031 PP->getPreprocessingRecord()->SetExternalSource(*this,
2032 TotalNumPreallocatedPreprocessingEntities);
2033 }
2034 }
Sebastian Redl725cd962010-08-04 20:40:17 +00002035 SelectorsLoaded.resize(TotalNumSelectors);
Sebastian Redl12d6da02010-07-19 22:06:55 +00002036
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002037 // Check the predefines buffers.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00002038 if (!DisableValidation && CheckPredefinesBuffers())
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002039 return IgnorePCH;
2040
2041 if (PP) {
2042 // Initialization of keywords and pragmas occurs before the
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002043 // AST file is read, so there may be some identifiers that were
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002044 // loaded into the IdentifierTable before we intercepted the
2045 // creation of identifiers. Iterate through the list of known
2046 // identifiers and determine whether we have to establish
2047 // preprocessor definitions or top-level identifier declaration
2048 // chains for those identifiers.
2049 //
2050 // We copy the IdentifierInfo pointers to a small vector first,
2051 // since de-serializing declarations or macro definitions can add
2052 // new entries into the identifier table, invalidating the
2053 // iterators.
2054 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
2055 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
2056 IdEnd = PP->getIdentifierTable().end();
2057 Id != IdEnd; ++Id)
2058 Identifiers.push_back(Id->second);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002059 // We need to search the tables in all files.
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002060 for (unsigned J = 0, M = Chain.size(); J != M; ++J) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002061 ASTIdentifierLookupTable *IdTable
2062 = (ASTIdentifierLookupTable *)Chain[J]->IdentifierLookupTable;
2063 // Not all AST files necessarily have identifier tables, only the useful
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00002064 // ones.
2065 if (!IdTable)
2066 continue;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002067 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2068 IdentifierInfo *II = Identifiers[I];
2069 // Look in the on-disk hash tables for an entry for this identifier
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002070 ASTIdentifierLookupTrait Info(*this, Chain[J]->Stream, II);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002071 std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength());
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002072 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002073 if (Pos == IdTable->end())
2074 continue;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002075
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002076 // Dereferencing the iterator has the effect of populating the
2077 // IdentifierInfo node with the various declarations it needs.
2078 (void)*Pos;
2079 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002080 }
2081 }
2082
2083 if (Context)
2084 InitializeContext(*Context);
2085
2086 return Success;
2087}
2088
Sebastian Redl571db7f2010-08-18 23:56:56 +00002089ASTReader::ASTReadResult ASTReader::ReadASTCore(llvm::StringRef FileName) {
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002090 Chain.push_back(new PerFileData());
Sebastian Redl9137a522010-07-16 17:50:48 +00002091 PerFileData &F = *Chain.back();
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002092
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002093 // Set the AST file name.
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002094 F.FileName = FileName;
2095
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002096 // Open the AST file.
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002097 //
2098 // FIXME: This shouldn't be here, we should just take a raw_ostream.
2099 std::string ErrStr;
2100 F.Buffer.reset(llvm::MemoryBuffer::getFileOrSTDIN(FileName, &ErrStr));
2101 if (!F.Buffer) {
2102 Error(ErrStr.c_str());
2103 return IgnorePCH;
2104 }
2105
2106 // Initialize the stream
2107 F.StreamFile.init((const unsigned char *)F.Buffer->getBufferStart(),
2108 (const unsigned char *)F.Buffer->getBufferEnd());
Sebastian Redl9137a522010-07-16 17:50:48 +00002109 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002110 Stream.init(F.StreamFile);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002111 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002112
2113 // Sniff for the signature.
2114 if (Stream.Read(8) != 'C' ||
2115 Stream.Read(8) != 'P' ||
2116 Stream.Read(8) != 'C' ||
2117 Stream.Read(8) != 'H') {
2118 Diag(diag::err_not_a_pch_file) << FileName;
2119 return Failure;
2120 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002121
Douglas Gregor2cf26342009-04-09 22:27:44 +00002122 while (!Stream.AtEndOfStream()) {
2123 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002124
Douglas Gregore1d918e2009-04-10 23:10:45 +00002125 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002126 Error("invalid record at top-level of AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002127 return Failure;
2128 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002129
2130 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregor668c1a42009-04-21 22:25:48 +00002131
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002132 // We only know the AST subblock ID.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002133 switch (BlockID) {
2134 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002135 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002136 Error("malformed BlockInfoBlock in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002137 return Failure;
2138 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002139 break;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002140 case AST_BLOCK_ID:
Sebastian Redl571db7f2010-08-18 23:56:56 +00002141 switch (ReadASTBlock(F)) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002142 case Success:
2143 break;
2144
2145 case Failure:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002146 return Failure;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002147
2148 case IgnorePCH:
Douglas Gregor2bec0412009-04-10 21:16:55 +00002149 // FIXME: We could consider reading through to the end of this
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002150 // AST block, skipping subblocks, to see if there are other
2151 // AST blocks elsewhere.
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002152
2153 // Clear out any preallocated source location entries, so that
2154 // the source manager does not try to resolve them later.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002155 SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002156
2157 // Remove the stat cache.
Sebastian Redl9137a522010-07-16 17:50:48 +00002158 if (F.StatCache)
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002159 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002160
Douglas Gregore1d918e2009-04-10 23:10:45 +00002161 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002162 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002163 break;
2164 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002165 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002166 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002167 return Failure;
2168 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002169 break;
2170 }
Mike Stump1eb44332009-09-09 15:08:12 +00002171 }
2172
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002173 return Success;
2174}
2175
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002176void ASTReader::setPreprocessor(Preprocessor &pp) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002177 PP = &pp;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002178
2179 unsigned TotalNum = 0;
2180 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
2181 TotalNum += Chain[I]->NumPreallocatedPreprocessingEntities;
2182 if (TotalNum) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002183 if (!PP->getPreprocessingRecord())
2184 PP->createPreprocessingRecord();
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002185 PP->getPreprocessingRecord()->SetExternalSource(*this, TotalNum);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002186 }
2187}
2188
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002189void ASTReader::InitializeContext(ASTContext &Ctx) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002190 Context = &Ctx;
2191 assert(Context && "Passed null context!");
2192
2193 assert(PP && "Forgot to set Preprocessor ?");
2194 PP->getIdentifierTable().setExternalIdentifierLookup(this);
2195 PP->getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor88a35862010-01-04 19:18:44 +00002196 PP->setExternalSource(this);
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002197
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002198 // Load the translation unit declaration
Argyrios Kyrtzidis8871a442010-07-08 17:13:02 +00002199 GetTranslationUnitDecl();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002200
2201 // Load the special types.
2202 Context->setBuiltinVaListType(
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002203 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
2204 if (unsigned Id = SpecialTypes[SPECIAL_TYPE_OBJC_ID])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002205 Context->setObjCIdType(GetType(Id));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002206 if (unsigned Sel = SpecialTypes[SPECIAL_TYPE_OBJC_SELECTOR])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002207 Context->setObjCSelType(GetType(Sel));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002208 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002209 Context->setObjCProtoType(GetType(Proto));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002210 if (unsigned Class = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002211 Context->setObjCClassType(GetType(Class));
Steve Naroff14108da2009-07-10 23:34:53 +00002212
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002213 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002214 Context->setCFConstantStringType(GetType(String));
Mike Stump1eb44332009-09-09 15:08:12 +00002215 if (unsigned FastEnum
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002216 = SpecialTypes[SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002217 Context->setObjCFastEnumerationStateType(GetType(FastEnum));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002218 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002219 QualType FileType = GetType(File);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002220 if (FileType.isNull()) {
2221 Error("FILE type is NULL");
2222 return;
2223 }
John McCall183700f2009-09-21 23:43:11 +00002224 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002225 Context->setFILEDecl(Typedef->getDecl());
2226 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002227 const TagType *Tag = FileType->getAs<TagType>();
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002228 if (!Tag) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002229 Error("Invalid FILE type in AST file");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002230 return;
2231 }
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002232 Context->setFILEDecl(Tag->getDecl());
2233 }
2234 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002235 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
Mike Stump782fa302009-07-28 02:25:19 +00002236 QualType Jmp_bufType = GetType(Jmp_buf);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002237 if (Jmp_bufType.isNull()) {
2238 Error("jmp_bug type is NULL");
2239 return;
2240 }
John McCall183700f2009-09-21 23:43:11 +00002241 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
Mike Stump782fa302009-07-28 02:25:19 +00002242 Context->setjmp_bufDecl(Typedef->getDecl());
2243 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002244 const TagType *Tag = Jmp_bufType->getAs<TagType>();
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002245 if (!Tag) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002246 Error("Invalid jmp_buf type in AST file");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002247 return;
2248 }
Mike Stump782fa302009-07-28 02:25:19 +00002249 Context->setjmp_bufDecl(Tag->getDecl());
2250 }
2251 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002252 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
Mike Stump782fa302009-07-28 02:25:19 +00002253 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002254 if (Sigjmp_bufType.isNull()) {
2255 Error("sigjmp_buf type is NULL");
2256 return;
2257 }
John McCall183700f2009-09-21 23:43:11 +00002258 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
Mike Stump782fa302009-07-28 02:25:19 +00002259 Context->setsigjmp_bufDecl(Typedef->getDecl());
2260 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002261 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002262 assert(Tag && "Invalid sigjmp_buf type in AST file");
Mike Stump782fa302009-07-28 02:25:19 +00002263 Context->setsigjmp_bufDecl(Tag->getDecl());
2264 }
2265 }
Mike Stump1eb44332009-09-09 15:08:12 +00002266 if (unsigned ObjCIdRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002267 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION])
Douglas Gregord1571ac2009-08-21 00:27:50 +00002268 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
Mike Stump1eb44332009-09-09 15:08:12 +00002269 if (unsigned ObjCClassRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002270 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION])
Douglas Gregord1571ac2009-08-21 00:27:50 +00002271 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002272 if (unsigned String = SpecialTypes[SPECIAL_TYPE_BLOCK_DESCRIPTOR])
Mike Stumpadaaad32009-10-20 02:12:22 +00002273 Context->setBlockDescriptorType(GetType(String));
Mike Stump083c25e2009-10-22 00:49:09 +00002274 if (unsigned String
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002275 = SpecialTypes[SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR])
Mike Stump083c25e2009-10-22 00:49:09 +00002276 Context->setBlockDescriptorExtendedType(GetType(String));
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002277 if (unsigned ObjCSelRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002278 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION])
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002279 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002280 if (unsigned String = SpecialTypes[SPECIAL_TYPE_NS_CONSTANT_STRING])
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002281 Context->setNSConstantStringType(GetType(String));
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002282
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002283 if (SpecialTypes[SPECIAL_TYPE_INT128_INSTALLED])
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002284 Context->setInt128Installed();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002285}
2286
Douglas Gregorb64c1932009-05-12 01:31:05 +00002287/// \brief Retrieve the name of the original source file name
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002288/// directly from the AST file, without actually loading the AST
Douglas Gregorb64c1932009-05-12 01:31:05 +00002289/// file.
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002290std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00002291 Diagnostic &Diags) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002292 // Open the AST file.
Douglas Gregorb64c1932009-05-12 01:31:05 +00002293 std::string ErrStr;
2294 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002295 Buffer.reset(llvm::MemoryBuffer::getFile(ASTFileName.c_str(), &ErrStr));
Douglas Gregorb64c1932009-05-12 01:31:05 +00002296 if (!Buffer) {
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00002297 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002298 return std::string();
2299 }
2300
2301 // Initialize the stream
2302 llvm::BitstreamReader StreamFile;
2303 llvm::BitstreamCursor Stream;
Mike Stump1eb44332009-09-09 15:08:12 +00002304 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregorb64c1932009-05-12 01:31:05 +00002305 (const unsigned char *)Buffer->getBufferEnd());
2306 Stream.init(StreamFile);
2307
2308 // Sniff for the signature.
2309 if (Stream.Read(8) != 'C' ||
2310 Stream.Read(8) != 'P' ||
2311 Stream.Read(8) != 'C' ||
2312 Stream.Read(8) != 'H') {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002313 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002314 return std::string();
2315 }
2316
2317 RecordData Record;
2318 while (!Stream.AtEndOfStream()) {
2319 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002320
Douglas Gregorb64c1932009-05-12 01:31:05 +00002321 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2322 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump1eb44332009-09-09 15:08:12 +00002323
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002324 // We only know the AST subblock ID.
Douglas Gregorb64c1932009-05-12 01:31:05 +00002325 switch (BlockID) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002326 case AST_BLOCK_ID:
2327 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002328 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002329 return std::string();
2330 }
2331 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002332
Douglas Gregorb64c1932009-05-12 01:31:05 +00002333 default:
2334 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002335 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002336 return std::string();
2337 }
2338 break;
2339 }
2340 continue;
2341 }
2342
2343 if (Code == llvm::bitc::END_BLOCK) {
2344 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002345 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002346 return std::string();
2347 }
2348 continue;
2349 }
2350
2351 if (Code == llvm::bitc::DEFINE_ABBREV) {
2352 Stream.ReadAbbrevRecord();
2353 continue;
2354 }
2355
2356 Record.clear();
2357 const char *BlobStart = 0;
2358 unsigned BlobLen = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002359 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002360 == ORIGINAL_FILE_NAME)
Douglas Gregorb64c1932009-05-12 01:31:05 +00002361 return std::string(BlobStart, BlobLen);
Mike Stump1eb44332009-09-09 15:08:12 +00002362 }
Douglas Gregorb64c1932009-05-12 01:31:05 +00002363
2364 return std::string();
2365}
2366
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002367/// \brief Parse the record that corresponds to a LangOptions data
2368/// structure.
2369///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002370/// This routine parses the language options from the AST file and then gives
2371/// them to the AST listener if one is set.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002372///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002373/// \returns true if the listener deems the file unacceptable, false otherwise.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002374bool ASTReader::ParseLanguageOptions(
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002375 const llvm::SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002376 if (Listener) {
2377 LangOptions LangOpts;
Mike Stump1eb44332009-09-09 15:08:12 +00002378
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002379 #define PARSE_LANGOPT(Option) \
2380 LangOpts.Option = Record[Idx]; \
2381 ++Idx
Mike Stump1eb44332009-09-09 15:08:12 +00002382
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002383 unsigned Idx = 0;
2384 PARSE_LANGOPT(Trigraphs);
2385 PARSE_LANGOPT(BCPLComment);
2386 PARSE_LANGOPT(DollarIdents);
2387 PARSE_LANGOPT(AsmPreprocessor);
2388 PARSE_LANGOPT(GNUMode);
Chandler Carrutheb5d7b72010-04-17 20:17:31 +00002389 PARSE_LANGOPT(GNUKeywords);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002390 PARSE_LANGOPT(ImplicitInt);
2391 PARSE_LANGOPT(Digraphs);
2392 PARSE_LANGOPT(HexFloats);
2393 PARSE_LANGOPT(C99);
2394 PARSE_LANGOPT(Microsoft);
2395 PARSE_LANGOPT(CPlusPlus);
2396 PARSE_LANGOPT(CPlusPlus0x);
2397 PARSE_LANGOPT(CXXOperatorNames);
2398 PARSE_LANGOPT(ObjC1);
2399 PARSE_LANGOPT(ObjC2);
2400 PARSE_LANGOPT(ObjCNonFragileABI);
Fariborz Jahanian412e7982010-02-09 19:31:38 +00002401 PARSE_LANGOPT(ObjCNonFragileABI2);
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +00002402 PARSE_LANGOPT(NoConstantCFStrings);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002403 PARSE_LANGOPT(PascalStrings);
2404 PARSE_LANGOPT(WritableStrings);
2405 PARSE_LANGOPT(LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +00002406 PARSE_LANGOPT(AltiVec);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002407 PARSE_LANGOPT(Exceptions);
Daniel Dunbar73482882010-02-10 18:48:44 +00002408 PARSE_LANGOPT(SjLjExceptions);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002409 PARSE_LANGOPT(NeXTRuntime);
2410 PARSE_LANGOPT(Freestanding);
2411 PARSE_LANGOPT(NoBuiltin);
2412 PARSE_LANGOPT(ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +00002413 PARSE_LANGOPT(POSIXThreads);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002414 PARSE_LANGOPT(Blocks);
2415 PARSE_LANGOPT(EmitAllDecls);
2416 PARSE_LANGOPT(MathErrno);
Chris Lattnera4d71452010-06-26 21:25:03 +00002417 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
2418 Record[Idx++]);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002419 PARSE_LANGOPT(HeinousExtensions);
2420 PARSE_LANGOPT(Optimize);
2421 PARSE_LANGOPT(OptimizeSize);
2422 PARSE_LANGOPT(Static);
2423 PARSE_LANGOPT(PICLevel);
2424 PARSE_LANGOPT(GNUInline);
2425 PARSE_LANGOPT(NoInline);
2426 PARSE_LANGOPT(AccessControl);
2427 PARSE_LANGOPT(CharIsSigned);
John Thompsona6fda122009-11-05 20:14:16 +00002428 PARSE_LANGOPT(ShortWChar);
Chris Lattnera4d71452010-06-26 21:25:03 +00002429 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
2430 LangOpts.setVisibilityMode((LangOptions::VisibilityMode)Record[Idx++]);
Daniel Dunbarab8e2812009-09-21 04:16:19 +00002431 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
Chris Lattnera4d71452010-06-26 21:25:03 +00002432 Record[Idx++]);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002433 PARSE_LANGOPT(InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +00002434 PARSE_LANGOPT(OpenCL);
Mike Stump9c276ae2009-12-12 01:27:46 +00002435 PARSE_LANGOPT(CatchUndefined);
2436 // FIXME: Missing ElideConstructors?!
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002437 #undef PARSE_LANGOPT
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002438
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002439 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002440 }
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002441
2442 return false;
2443}
2444
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002445void ASTReader::ReadPreprocessedEntities() {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002446 ReadDefinedMacros();
2447}
2448
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002449/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002450ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002451 PerFileData *F = 0;
2452 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2453 F = Chain[N - I - 1];
2454 if (Index < F->LocalNumTypes)
2455 break;
2456 Index -= F->LocalNumTypes;
2457 }
2458 assert(F && F->LocalNumTypes > Index && "Broken chain");
Sebastian Redl971dd442010-07-20 22:55:31 +00002459 return RecordLocation(&F->DeclsCursor, F->TypeOffsets[Index]);
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002460}
2461
2462/// \brief Read and return the type with the given index..
Douglas Gregor2cf26342009-04-09 22:27:44 +00002463///
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002464/// The index is the type ID, shifted and minus the number of predefs. This
2465/// routine actually reads the record corresponding to the type at the given
2466/// location. It is a helper routine for GetType, which deals with reading type
2467/// IDs.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002468QualType ASTReader::ReadTypeRecord(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002469 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redl971dd442010-07-20 22:55:31 +00002470 llvm::BitstreamCursor &DeclsCursor = *Loc.first;
Sebastian Redl9137a522010-07-16 17:50:48 +00002471
Douglas Gregor0b748912009-04-14 21:18:50 +00002472 // Keep track of where we are in the stream, then jump back there
2473 // after reading this type.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002474 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregor0b748912009-04-14 21:18:50 +00002475
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00002476 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redl27372b42010-08-11 18:52:41 +00002477
Douglas Gregord89275b2009-07-06 18:54:52 +00002478 // Note that we are loading a type record.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00002479 Deserializing AType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00002480
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002481 DeclsCursor.JumpToBit(Loc.second);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002482 RecordData Record;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002483 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002484 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
2485 case TYPE_EXT_QUAL: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002486 if (Record.size() != 2) {
2487 Error("Incorrect encoding of extended qualifier type");
2488 return QualType();
2489 }
Douglas Gregor6d473962009-04-15 22:00:08 +00002490 QualType Base = GetType(Record[0]);
John McCall0953e762009-09-24 19:53:00 +00002491 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]);
2492 return Context->getQualifiedType(Base, Quals);
Douglas Gregor6d473962009-04-15 22:00:08 +00002493 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002494
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002495 case TYPE_COMPLEX: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002496 if (Record.size() != 1) {
2497 Error("Incorrect encoding of complex type");
2498 return QualType();
2499 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002500 QualType ElemType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002501 return Context->getComplexType(ElemType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002502 }
2503
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002504 case TYPE_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002505 if (Record.size() != 1) {
2506 Error("Incorrect encoding of pointer type");
2507 return QualType();
2508 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002509 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002510 return Context->getPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002511 }
2512
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002513 case TYPE_BLOCK_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002514 if (Record.size() != 1) {
2515 Error("Incorrect encoding of block pointer type");
2516 return QualType();
2517 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002518 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002519 return Context->getBlockPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002520 }
2521
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002522 case TYPE_LVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002523 if (Record.size() != 1) {
2524 Error("Incorrect encoding of lvalue reference type");
2525 return QualType();
2526 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002527 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002528 return Context->getLValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002529 }
2530
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002531 case TYPE_RVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002532 if (Record.size() != 1) {
2533 Error("Incorrect encoding of rvalue reference type");
2534 return QualType();
2535 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002536 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002537 return Context->getRValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002538 }
2539
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002540 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidis240437b2010-07-02 11:55:15 +00002541 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002542 Error("Incorrect encoding of member pointer type");
2543 return QualType();
2544 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002545 QualType PointeeType = GetType(Record[0]);
2546 QualType ClassType = GetType(Record[1]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002547 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002548 }
2549
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002550 case TYPE_CONSTANT_ARRAY: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002551 QualType ElementType = GetType(Record[0]);
2552 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2553 unsigned IndexTypeQuals = Record[2];
2554 unsigned Idx = 3;
2555 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002556 return Context->getConstantArrayType(ElementType, Size,
2557 ASM, IndexTypeQuals);
2558 }
2559
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002560 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002561 QualType ElementType = GetType(Record[0]);
2562 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2563 unsigned IndexTypeQuals = Record[2];
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002564 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002565 }
2566
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002567 case TYPE_VARIABLE_ARRAY: {
Douglas Gregor0b748912009-04-14 21:18:50 +00002568 QualType ElementType = GetType(Record[0]);
2569 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2570 unsigned IndexTypeQuals = Record[2];
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002571 SourceLocation LBLoc = SourceLocation::getFromRawEncoding(Record[3]);
2572 SourceLocation RBLoc = SourceLocation::getFromRawEncoding(Record[4]);
Sebastian Redl577d4792010-07-22 22:43:28 +00002573 return Context->getVariableArrayType(ElementType, ReadExpr(DeclsCursor),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002574 ASM, IndexTypeQuals,
2575 SourceRange(LBLoc, RBLoc));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002576 }
2577
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002578 case TYPE_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00002579 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002580 Error("incorrect encoding of vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002581 return QualType();
2582 }
2583
2584 QualType ElementType = GetType(Record[0]);
2585 unsigned NumElements = Record[1];
Chris Lattner788b0fd2010-06-23 06:00:24 +00002586 unsigned AltiVecSpec = Record[2];
2587 return Context->getVectorType(ElementType, NumElements,
2588 (VectorType::AltiVecSpecific)AltiVecSpec);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002589 }
2590
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002591 case TYPE_EXT_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00002592 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002593 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002594 return QualType();
2595 }
2596
2597 QualType ElementType = GetType(Record[0]);
2598 unsigned NumElements = Record[1];
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002599 return Context->getExtVectorType(ElementType, NumElements);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002600 }
2601
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002602 case TYPE_FUNCTION_NO_PROTO: {
Rafael Espindola425ef722010-03-30 22:15:11 +00002603 if (Record.size() != 4) {
Douglas Gregora02b1472009-04-28 21:53:25 +00002604 Error("incorrect encoding of no-proto function type");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002605 return QualType();
2606 }
2607 QualType ResultType = GetType(Record[0]);
Rafael Espindola425ef722010-03-30 22:15:11 +00002608 FunctionType::ExtInfo Info(Record[1], Record[2], (CallingConv)Record[3]);
Rafael Espindola264ba482010-03-30 20:24:48 +00002609 return Context->getFunctionNoProtoType(ResultType, Info);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002610 }
2611
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002612 case TYPE_FUNCTION_PROTO: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002613 QualType ResultType = GetType(Record[0]);
Douglas Gregor91236662009-12-22 18:11:50 +00002614 bool NoReturn = Record[1];
Rafael Espindola425ef722010-03-30 22:15:11 +00002615 unsigned RegParm = Record[2];
2616 CallingConv CallConv = (CallingConv)Record[3];
2617 unsigned Idx = 4;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002618 unsigned NumParams = Record[Idx++];
2619 llvm::SmallVector<QualType, 16> ParamTypes;
2620 for (unsigned I = 0; I != NumParams; ++I)
2621 ParamTypes.push_back(GetType(Record[Idx++]));
2622 bool isVariadic = Record[Idx++];
2623 unsigned Quals = Record[Idx++];
Sebastian Redl465226e2009-05-27 22:11:52 +00002624 bool hasExceptionSpec = Record[Idx++];
2625 bool hasAnyExceptionSpec = Record[Idx++];
2626 unsigned NumExceptions = Record[Idx++];
2627 llvm::SmallVector<QualType, 2> Exceptions;
2628 for (unsigned I = 0; I != NumExceptions; ++I)
2629 Exceptions.push_back(GetType(Record[Idx++]));
Jay Foadbeaaccd2009-05-21 09:52:38 +00002630 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
Sebastian Redl465226e2009-05-27 22:11:52 +00002631 isVariadic, Quals, hasExceptionSpec,
2632 hasAnyExceptionSpec, NumExceptions,
Rafael Espindola264ba482010-03-30 20:24:48 +00002633 Exceptions.data(),
Rafael Espindola425ef722010-03-30 22:15:11 +00002634 FunctionType::ExtInfo(NoReturn, RegParm,
2635 CallConv));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002636 }
2637
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002638 case TYPE_UNRESOLVED_USING:
John McCalled976492009-12-04 22:46:56 +00002639 return Context->getTypeDeclType(
2640 cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0])));
2641
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002642 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002643 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002644 Error("incorrect encoding of typedef type");
2645 return QualType();
2646 }
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002647 TypedefDecl *Decl = cast<TypedefDecl>(GetDecl(Record[0]));
2648 QualType Canonical = GetType(Record[1]);
2649 return Context->getTypedefType(Decl, Canonical);
2650 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002651
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002652 case TYPE_TYPEOF_EXPR:
Sebastian Redl577d4792010-07-22 22:43:28 +00002653 return Context->getTypeOfExprType(ReadExpr(DeclsCursor));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002654
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002655 case TYPE_TYPEOF: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002656 if (Record.size() != 1) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002657 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002658 return QualType();
2659 }
2660 QualType UnderlyingType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002661 return Context->getTypeOfType(UnderlyingType);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002662 }
Mike Stump1eb44332009-09-09 15:08:12 +00002663
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002664 case TYPE_DECLTYPE:
Sebastian Redl577d4792010-07-22 22:43:28 +00002665 return Context->getDecltypeType(ReadExpr(DeclsCursor));
Anders Carlsson395b4752009-06-24 19:06:50 +00002666
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002667 case TYPE_RECORD: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002668 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002669 Error("incorrect encoding of record type");
2670 return QualType();
2671 }
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002672 bool IsDependent = Record[0];
2673 QualType T = Context->getRecordType(cast<RecordDecl>(GetDecl(Record[1])));
2674 T->Dependent = IsDependent;
2675 return T;
2676 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002677
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002678 case TYPE_ENUM: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002679 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002680 Error("incorrect encoding of enum type");
2681 return QualType();
2682 }
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002683 bool IsDependent = Record[0];
2684 QualType T = Context->getEnumType(cast<EnumDecl>(GetDecl(Record[1])));
2685 T->Dependent = IsDependent;
2686 return T;
2687 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002688
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002689 case TYPE_ELABORATED: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00002690 unsigned Idx = 0;
2691 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2692 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2693 QualType NamedType = GetType(Record[Idx++]);
2694 return Context->getElaboratedType(Keyword, NNS, NamedType);
John McCall7da24312009-09-05 00:15:47 +00002695 }
2696
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002697 case TYPE_OBJC_INTERFACE: {
Chris Lattnerc6fa4452009-04-22 06:45:28 +00002698 unsigned Idx = 0;
2699 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
John McCallc12c5bb2010-05-15 11:32:37 +00002700 return Context->getObjCInterfaceType(ItfD);
2701 }
2702
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002703 case TYPE_OBJC_OBJECT: {
John McCallc12c5bb2010-05-15 11:32:37 +00002704 unsigned Idx = 0;
2705 QualType Base = GetType(Record[Idx++]);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00002706 unsigned NumProtos = Record[Idx++];
2707 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2708 for (unsigned I = 0; I != NumProtos; ++I)
2709 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
John McCallc12c5bb2010-05-15 11:32:37 +00002710 return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00002711 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002712
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002713 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00002714 unsigned Idx = 0;
John McCallc12c5bb2010-05-15 11:32:37 +00002715 QualType Pointee = GetType(Record[Idx++]);
2716 return Context->getObjCObjectPointerType(Pointee);
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00002717 }
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +00002718
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002719 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCall49a832b2009-10-18 09:09:24 +00002720 unsigned Idx = 0;
2721 QualType Parm = GetType(Record[Idx++]);
2722 QualType Replacement = GetType(Record[Idx++]);
2723 return
2724 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
2725 Replacement);
2726 }
John McCall3cb0ebd2010-03-10 03:28:59 +00002727
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002728 case TYPE_INJECTED_CLASS_NAME: {
John McCall3cb0ebd2010-03-10 03:28:59 +00002729 CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0]));
2730 QualType TST = GetType(Record[1]); // probably derivable
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00002731 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002732 // for AST reading, too much interdependencies.
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00002733 return
2734 QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCall3cb0ebd2010-03-10 03:28:59 +00002735 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00002736
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002737 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002738 unsigned Idx = 0;
2739 unsigned Depth = Record[Idx++];
2740 unsigned Index = Record[Idx++];
2741 bool Pack = Record[Idx++];
2742 IdentifierInfo *Name = GetIdentifierInfo(Record, Idx);
2743 return Context->getTemplateTypeParmType(Depth, Index, Pack, Name);
2744 }
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00002745
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002746 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00002747 unsigned Idx = 0;
2748 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2749 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2750 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +00002751 QualType Canon = GetType(Record[Idx++]);
2752 return Context->getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00002753 }
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00002754
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002755 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00002756 unsigned Idx = 0;
2757 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2758 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2759 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
2760 unsigned NumArgs = Record[Idx++];
2761 llvm::SmallVector<TemplateArgument, 8> Args;
2762 Args.reserve(NumArgs);
2763 while (NumArgs--)
Sebastian Redl577d4792010-07-22 22:43:28 +00002764 Args.push_back(ReadTemplateArgument(DeclsCursor, Record, Idx));
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00002765 return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
2766 Args.size(), Args.data());
2767 }
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00002768
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002769 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00002770 unsigned Idx = 0;
2771
2772 // ArrayType
2773 QualType ElementType = GetType(Record[Idx++]);
2774 ArrayType::ArraySizeModifier ASM
2775 = (ArrayType::ArraySizeModifier)Record[Idx++];
2776 unsigned IndexTypeQuals = Record[Idx++];
2777
2778 // DependentSizedArrayType
Sebastian Redl577d4792010-07-22 22:43:28 +00002779 Expr *NumElts = ReadExpr(DeclsCursor);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00002780 SourceRange Brackets = ReadSourceRange(Record, Idx);
2781
2782 return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
2783 IndexTypeQuals, Brackets);
2784 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00002785
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002786 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002787 unsigned Idx = 0;
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002788 bool IsDependent = Record[Idx++];
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002789 TemplateName Name = ReadTemplateName(Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002790 llvm::SmallVector<TemplateArgument, 8> Args;
Sebastian Redl577d4792010-07-22 22:43:28 +00002791 ReadTemplateArgumentList(Args, DeclsCursor, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00002792 QualType Canon = GetType(Record[Idx++]);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002793 QualType T;
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002794 if (Canon.isNull())
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002795 T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(),
2796 Args.size());
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002797 else
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002798 T = Context->getTemplateSpecializationType(Name, Args.data(),
2799 Args.size(), Canon);
2800 T->Dependent = IsDependent;
2801 return T;
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002802 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002803 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002804 // Suppress a GCC warning
2805 return QualType();
2806}
2807
John McCalla1ee0c52009-10-16 21:56:05 +00002808namespace {
2809
2810class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002811 ASTReader &Reader;
Sebastian Redl577d4792010-07-22 22:43:28 +00002812 llvm::BitstreamCursor &DeclsCursor;
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002813 const ASTReader::RecordData &Record;
John McCalla1ee0c52009-10-16 21:56:05 +00002814 unsigned &Idx;
2815
2816public:
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002817 TypeLocReader(ASTReader &Reader, llvm::BitstreamCursor &Cursor,
2818 const ASTReader::RecordData &Record, unsigned &Idx)
Sebastian Redl577d4792010-07-22 22:43:28 +00002819 : Reader(Reader), DeclsCursor(Cursor), Record(Record), Idx(Idx) { }
John McCalla1ee0c52009-10-16 21:56:05 +00002820
John McCall51bd8032009-10-18 01:05:36 +00002821 // We want compile-time assurance that we've enumerated all of
2822 // these, so unfortunately we have to declare them first, then
2823 // define them out-of-line.
2824#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +00002825#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +00002826 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00002827#include "clang/AST/TypeLocNodes.def"
2828
John McCall51bd8032009-10-18 01:05:36 +00002829 void VisitFunctionTypeLoc(FunctionTypeLoc);
2830 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00002831};
2832
2833}
2834
John McCall51bd8032009-10-18 01:05:36 +00002835void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCalla1ee0c52009-10-16 21:56:05 +00002836 // nothing to do
2837}
John McCall51bd8032009-10-18 01:05:36 +00002838void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +00002839 TL.setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2840 if (TL.needsExtraLocalData()) {
2841 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
2842 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
2843 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
2844 TL.setModeAttr(Record[Idx++]);
2845 }
John McCalla1ee0c52009-10-16 21:56:05 +00002846}
John McCall51bd8032009-10-18 01:05:36 +00002847void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
2848 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002849}
John McCall51bd8032009-10-18 01:05:36 +00002850void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
2851 TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002852}
John McCall51bd8032009-10-18 01:05:36 +00002853void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
2854 TL.setCaretLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002855}
John McCall51bd8032009-10-18 01:05:36 +00002856void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
2857 TL.setAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002858}
John McCall51bd8032009-10-18 01:05:36 +00002859void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
2860 TL.setAmpAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002861}
John McCall51bd8032009-10-18 01:05:36 +00002862void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
2863 TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002864}
John McCall51bd8032009-10-18 01:05:36 +00002865void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
2866 TL.setLBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2867 TL.setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002868 if (Record[Idx++])
Sebastian Redl577d4792010-07-22 22:43:28 +00002869 TL.setSizeExpr(Reader.ReadExpr(DeclsCursor));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002870 else
John McCall51bd8032009-10-18 01:05:36 +00002871 TL.setSizeExpr(0);
2872}
2873void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
2874 VisitArrayTypeLoc(TL);
2875}
2876void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
2877 VisitArrayTypeLoc(TL);
2878}
2879void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
2880 VisitArrayTypeLoc(TL);
2881}
2882void TypeLocReader::VisitDependentSizedArrayTypeLoc(
2883 DependentSizedArrayTypeLoc TL) {
2884 VisitArrayTypeLoc(TL);
2885}
2886void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
2887 DependentSizedExtVectorTypeLoc TL) {
2888 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2889}
2890void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
2891 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2892}
2893void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
2894 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2895}
2896void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
2897 TL.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2898 TL.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2899 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
John McCall86acc2a2009-10-23 01:28:53 +00002900 TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
John McCall51bd8032009-10-18 01:05:36 +00002901 }
2902}
2903void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
2904 VisitFunctionTypeLoc(TL);
2905}
2906void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
2907 VisitFunctionTypeLoc(TL);
2908}
John McCalled976492009-12-04 22:46:56 +00002909void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
2910 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2911}
John McCall51bd8032009-10-18 01:05:36 +00002912void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
2913 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2914}
2915void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +00002916 TL.setTypeofLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2917 TL.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2918 TL.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall51bd8032009-10-18 01:05:36 +00002919}
2920void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +00002921 TL.setTypeofLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2922 TL.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2923 TL.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Sebastian Redl577d4792010-07-22 22:43:28 +00002924 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(DeclsCursor, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00002925}
2926void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
2927 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2928}
2929void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
2930 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2931}
2932void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
2933 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2934}
John McCall51bd8032009-10-18 01:05:36 +00002935void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
2936 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2937}
John McCall49a832b2009-10-18 09:09:24 +00002938void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
2939 SubstTemplateTypeParmTypeLoc TL) {
2940 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2941}
John McCall51bd8032009-10-18 01:05:36 +00002942void TypeLocReader::VisitTemplateSpecializationTypeLoc(
2943 TemplateSpecializationTypeLoc TL) {
John McCall833ca992009-10-29 08:12:44 +00002944 TL.setTemplateNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2945 TL.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2946 TL.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2947 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
2948 TL.setArgLocInfo(i,
2949 Reader.GetTemplateArgumentLocInfo(TL.getTypePtr()->getArg(i).getKind(),
Sebastian Redl577d4792010-07-22 22:43:28 +00002950 DeclsCursor, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00002951}
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002952void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002953 TL.setKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2954 TL.setQualifierRange(Reader.ReadSourceRange(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00002955}
John McCall3cb0ebd2010-03-10 03:28:59 +00002956void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
2957 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2958}
Douglas Gregor4714c122010-03-31 17:34:00 +00002959void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002960 TL.setKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2961 TL.setQualifierRange(Reader.ReadSourceRange(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00002962 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2963}
John McCall33500952010-06-11 00:33:02 +00002964void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
2965 DependentTemplateSpecializationTypeLoc TL) {
2966 TL.setKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2967 TL.setQualifierRange(Reader.ReadSourceRange(Record, Idx));
2968 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2969 TL.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2970 TL.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2971 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
2972 TL.setArgLocInfo(I,
2973 Reader.GetTemplateArgumentLocInfo(TL.getTypePtr()->getArg(I).getKind(),
Sebastian Redl577d4792010-07-22 22:43:28 +00002974 DeclsCursor, Record, Idx));
John McCall33500952010-06-11 00:33:02 +00002975}
John McCall51bd8032009-10-18 01:05:36 +00002976void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
2977 TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCallc12c5bb2010-05-15 11:32:37 +00002978}
2979void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
2980 TL.setHasBaseTypeAsWritten(Record[Idx++]);
John McCall51bd8032009-10-18 01:05:36 +00002981 TL.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2982 TL.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
2983 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
2984 TL.setProtocolLoc(i, SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCalla1ee0c52009-10-16 21:56:05 +00002985}
John McCall54e14c42009-10-22 22:37:11 +00002986void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
2987 TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall54e14c42009-10-22 22:37:11 +00002988}
John McCalla1ee0c52009-10-16 21:56:05 +00002989
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002990TypeSourceInfo *ASTReader::GetTypeSourceInfo(llvm::BitstreamCursor &DeclsCursor,
Sebastian Redl577d4792010-07-22 22:43:28 +00002991 const RecordData &Record,
John McCalla1ee0c52009-10-16 21:56:05 +00002992 unsigned &Idx) {
2993 QualType InfoTy = GetType(Record[Idx++]);
2994 if (InfoTy.isNull())
2995 return 0;
2996
John McCalla93c9342009-12-07 02:54:59 +00002997 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
Sebastian Redl577d4792010-07-22 22:43:28 +00002998 TypeLocReader TLR(*this, DeclsCursor, Record, Idx);
John McCalla93c9342009-12-07 02:54:59 +00002999 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCalla1ee0c52009-10-16 21:56:05 +00003000 TLR.Visit(TL);
John McCalla93c9342009-12-07 02:54:59 +00003001 return TInfo;
John McCalla1ee0c52009-10-16 21:56:05 +00003002}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003003
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003004QualType ASTReader::GetType(TypeID ID) {
John McCall0953e762009-09-24 19:53:00 +00003005 unsigned FastQuals = ID & Qualifiers::FastMask;
3006 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003007
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003008 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003009 QualType T;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003010 switch ((PredefinedTypeIDs)Index) {
3011 case PREDEF_TYPE_NULL_ID: return QualType();
3012 case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
3013 case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003014
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003015 case PREDEF_TYPE_CHAR_U_ID:
3016 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregor2cf26342009-04-09 22:27:44 +00003017 // FIXME: Check that the signedness of CharTy is correct!
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003018 T = Context->CharTy;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003019 break;
3020
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003021 case PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break;
3022 case PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break;
3023 case PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break;
3024 case PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break;
3025 case PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break;
3026 case PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break;
3027 case PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break;
3028 case PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break;
3029 case PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break;
3030 case PREDEF_TYPE_INT_ID: T = Context->IntTy; break;
3031 case PREDEF_TYPE_LONG_ID: T = Context->LongTy; break;
3032 case PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break;
3033 case PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break;
3034 case PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break;
3035 case PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break;
3036 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break;
3037 case PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break;
3038 case PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break;
3039 case PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break;
3040 case PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break;
3041 case PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break;
3042 case PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break;
3043 case PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break;
3044 case PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003045 }
3046
3047 assert(!T.isNull() && "Unknown predefined type");
John McCall0953e762009-09-24 19:53:00 +00003048 return T.withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003049 }
3050
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003051 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003052 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl07a353c2010-07-14 20:26:45 +00003053 if (TypesLoaded[Index].isNull()) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003054 TypesLoaded[Index] = ReadTypeRecord(Index);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003055 TypesLoaded[Index]->setFromAST();
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003056 TypeIdxs[TypesLoaded[Index]] = TypeIdx::fromTypeID(ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003057 if (DeserializationListener)
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003058 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1476ed42010-07-16 16:36:56 +00003059 TypesLoaded[Index]);
Sebastian Redl07a353c2010-07-14 20:26:45 +00003060 }
Mike Stump1eb44332009-09-09 15:08:12 +00003061
John McCall0953e762009-09-24 19:53:00 +00003062 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003063}
3064
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003065TypeID ASTReader::GetTypeID(QualType T) const {
3066 return MakeTypeID(T,
3067 std::bind1st(std::mem_fun(&ASTReader::GetTypeIdx), this));
3068}
3069
3070TypeIdx ASTReader::GetTypeIdx(QualType T) const {
3071 if (T.isNull())
3072 return TypeIdx();
3073 assert(!T.getLocalFastQualifiers());
3074
3075 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3076 // GetTypeIdx is mostly used for computing the hash of DeclarationNames and
3077 // comparing keys of ASTDeclContextNameLookupTable.
3078 // If the type didn't come from the AST file use a specially marked index
3079 // so that any hash/key comparison fail since no such index is stored
3080 // in a AST file.
3081 if (I == TypeIdxs.end())
3082 return TypeIdx(-1);
3083 return I->second;
3084}
3085
John McCall833ca992009-10-29 08:12:44 +00003086TemplateArgumentLocInfo
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003087ASTReader::GetTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Sebastian Redl577d4792010-07-22 22:43:28 +00003088 llvm::BitstreamCursor &DeclsCursor,
John McCall833ca992009-10-29 08:12:44 +00003089 const RecordData &Record,
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003090 unsigned &Index) {
John McCall833ca992009-10-29 08:12:44 +00003091 switch (Kind) {
3092 case TemplateArgument::Expression:
Sebastian Redl577d4792010-07-22 22:43:28 +00003093 return ReadExpr(DeclsCursor);
John McCall833ca992009-10-29 08:12:44 +00003094 case TemplateArgument::Type:
Sebastian Redl577d4792010-07-22 22:43:28 +00003095 return GetTypeSourceInfo(DeclsCursor, Record, Index);
Douglas Gregor788cd062009-11-11 01:00:40 +00003096 case TemplateArgument::Template: {
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003097 SourceRange QualifierRange = ReadSourceRange(Record, Index);
3098 SourceLocation TemplateNameLoc = ReadSourceLocation(Record, Index);
3099 return TemplateArgumentLocInfo(QualifierRange, TemplateNameLoc);
Douglas Gregor788cd062009-11-11 01:00:40 +00003100 }
John McCall833ca992009-10-29 08:12:44 +00003101 case TemplateArgument::Null:
3102 case TemplateArgument::Integral:
3103 case TemplateArgument::Declaration:
3104 case TemplateArgument::Pack:
3105 return TemplateArgumentLocInfo();
3106 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00003107 llvm_unreachable("unexpected template argument loc");
John McCall833ca992009-10-29 08:12:44 +00003108 return TemplateArgumentLocInfo();
3109}
3110
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003111TemplateArgumentLoc
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003112ASTReader::ReadTemplateArgumentLoc(llvm::BitstreamCursor &DeclsCursor,
Sebastian Redl577d4792010-07-22 22:43:28 +00003113 const RecordData &Record, unsigned &Index) {
3114 TemplateArgument Arg = ReadTemplateArgument(DeclsCursor, Record, Index);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003115
3116 if (Arg.getKind() == TemplateArgument::Expression) {
3117 if (Record[Index++]) // bool InfoHasSameExpr.
3118 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
3119 }
3120 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(Arg.getKind(),
Sebastian Redl577d4792010-07-22 22:43:28 +00003121 DeclsCursor,
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003122 Record, Index));
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003123}
3124
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003125Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall76bd1f32010-06-01 09:23:16 +00003126 return GetDecl(ID);
3127}
3128
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003129TranslationUnitDecl *ASTReader::GetTranslationUnitDecl() {
Sebastian Redl30c514c2010-07-14 23:45:08 +00003130 if (!DeclsLoaded[0]) {
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00003131 ReadDeclRecord(0, 0);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003132 if (DeserializationListener)
Sebastian Redl1476ed42010-07-16 16:36:56 +00003133 DeserializationListener->DeclRead(1, DeclsLoaded[0]);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003134 }
Argyrios Kyrtzidis8871a442010-07-08 17:13:02 +00003135
3136 return cast<TranslationUnitDecl>(DeclsLoaded[0]);
3137}
3138
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003139Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003140 if (ID == 0)
3141 return 0;
3142
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003143 if (ID > DeclsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003144 Error("declaration ID out-of-range for AST file");
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003145 return 0;
3146 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003147
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003148 unsigned Index = ID - 1;
Sebastian Redl30c514c2010-07-14 23:45:08 +00003149 if (!DeclsLoaded[Index]) {
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00003150 ReadDeclRecord(Index, ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003151 if (DeserializationListener)
3152 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
3153 }
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003154
3155 return DeclsLoaded[Index];
Douglas Gregor2cf26342009-04-09 22:27:44 +00003156}
3157
Chris Lattner887e2b32009-04-27 05:46:25 +00003158/// \brief Resolve the offset of a statement into a statement.
3159///
3160/// This operation will read a new statement from the external
3161/// source each time it is called, and is meant to be used via a
3162/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003163Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003164 // Offset here is a global offset across the entire chain.
3165 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3166 PerFileData &F = *Chain[N - I - 1];
3167 if (Offset < F.SizeInBits) {
3168 // Since we know that this statement is part of a decl, make sure to use
3169 // the decl cursor to read it.
3170 F.DeclsCursor.JumpToBit(Offset);
3171 return ReadStmtFromStream(F.DeclsCursor);
3172 }
3173 Offset -= F.SizeInBits;
3174 }
3175 llvm_unreachable("Broken chain");
Douglas Gregor250fc9c2009-04-18 00:07:54 +00003176}
3177
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003178bool ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +00003179 llvm::SmallVectorImpl<Decl*> &Decls) {
Mike Stump1eb44332009-09-09 15:08:12 +00003180 assert(DC->hasExternalLexicalStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00003181 "DeclContext has no lexical decls in storage");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003182
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003183 // There might be lexical decls in multiple parts of the chain, for the TU
3184 // at least.
3185 DeclContextInfos &Infos = DeclContextOffsets[DC];
3186 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3187 I != E; ++I) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003188 // IDs can be 0 if this context doesn't contain declarations.
3189 if (!I->LexicalDecls)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003190 continue;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003191
3192 // Load all of the declaration IDs
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003193 for (const DeclID *ID = I->LexicalDecls,
Sebastian Redl681d7232010-07-27 00:17:23 +00003194 *IDE = ID + I->NumLexicalDecls;
3195 ID != IDE; ++ID)
3196 Decls.push_back(GetDecl(*ID));
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003197 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003198
Douglas Gregor25123082009-04-22 22:34:57 +00003199 ++NumLexicalDeclContextsRead;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003200 return false;
3201}
3202
John McCall76bd1f32010-06-01 09:23:16 +00003203DeclContext::lookup_result
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003204ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +00003205 DeclarationName Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00003206 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00003207 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003208 if (!Name)
3209 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
3210 DeclContext::lookup_iterator(0));
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003211
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003212 llvm::SmallVector<NamedDecl *, 64> Decls;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003213 // There might be lexical decls in multiple parts of the chain, for the TU
3214 // and namespaces.
3215 DeclContextInfos &Infos = DeclContextOffsets[DC];
3216 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3217 I != E; ++I) {
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003218 if (!I->NameLookupTableData)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003219 continue;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003220
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003221 ASTDeclContextNameLookupTable *LookupTable =
3222 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3223 ASTDeclContextNameLookupTable::iterator Pos = LookupTable->find(Name);
3224 if (Pos == LookupTable->end())
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003225 continue;
3226
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003227 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
3228 for (; Data.first != Data.second; ++Data.first)
3229 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00003230 }
3231
Douglas Gregor25123082009-04-22 22:34:57 +00003232 ++NumVisibleDeclContextsRead;
John McCall76bd1f32010-06-01 09:23:16 +00003233
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003234 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall76bd1f32010-06-01 09:23:16 +00003235 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003236}
3237
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00003238void ASTReader::MaterializeVisibleDecls(const DeclContext *DC) {
3239 assert(DC->hasExternalVisibleStorage() &&
3240 "DeclContext has no visible decls in storage");
3241
3242 llvm::SmallVector<NamedDecl *, 64> Decls;
3243 // There might be visible decls in multiple parts of the chain, for the TU
3244 // and namespaces.
3245 DeclContextInfos &Infos = DeclContextOffsets[DC];
3246 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3247 I != E; ++I) {
3248 if (!I->NameLookupTableData)
3249 continue;
3250
3251 ASTDeclContextNameLookupTable *LookupTable =
3252 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3253 for (ASTDeclContextNameLookupTable::item_iterator
3254 ItemI = LookupTable->item_begin(),
3255 ItemEnd = LookupTable->item_end() ; ItemI != ItemEnd; ++ItemI) {
3256 ASTDeclContextNameLookupTable::item_iterator::value_type Val
3257 = *ItemI;
3258 ASTDeclContextNameLookupTrait::data_type Data = Val.second;
3259 Decls.clear();
3260 for (; Data.first != Data.second; ++Data.first)
3261 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
3262 MaterializeVisibleDeclsForName(DC, Val.first, Decls);
3263 }
3264 }
3265}
3266
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003267void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003268 assert(Consumer);
3269 while (!InterestingDecls.empty()) {
3270 DeclGroupRef DG(InterestingDecls.front());
3271 InterestingDecls.pop_front();
Sebastian Redl27372b42010-08-11 18:52:41 +00003272 Consumer->HandleInterestingDecl(DG);
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003273 }
3274}
3275
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003276void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor0af2ca42009-04-22 19:09:20 +00003277 this->Consumer = Consumer;
3278
Douglas Gregorfdd01722009-04-14 00:24:19 +00003279 if (!Consumer)
3280 return;
3281
3282 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003283 // Force deserialization of this decl, which will cause it to be queued for
3284 // passing to the consumer.
Daniel Dunbar04a0b502009-09-17 03:06:44 +00003285 GetDecl(ExternalDefinitions[I]);
Douglas Gregorfdd01722009-04-14 00:24:19 +00003286 }
Douglas Gregorc62a2fe2009-04-25 00:41:30 +00003287
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003288 PassInterestingDeclsToConsumer();
Douglas Gregorfdd01722009-04-14 00:24:19 +00003289}
3290
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003291void ASTReader::PrintStats() {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003292 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregor2cf26342009-04-09 22:27:44 +00003293
Mike Stump1eb44332009-09-09 15:08:12 +00003294 unsigned NumTypesLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003295 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall0953e762009-09-24 19:53:00 +00003296 QualType());
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003297 unsigned NumDeclsLoaded
3298 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
3299 (Decl *)0);
3300 unsigned NumIdentifiersLoaded
3301 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
3302 IdentifiersLoaded.end(),
3303 (IdentifierInfo *)0);
Mike Stump1eb44332009-09-09 15:08:12 +00003304 unsigned NumSelectorsLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003305 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
3306 SelectorsLoaded.end(),
3307 Selector());
Douglas Gregor2d41cc12009-04-13 20:50:16 +00003308
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003309 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
3310 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00003311 if (TotalNumSLocEntries)
3312 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
3313 NumSLocEntriesRead, TotalNumSLocEntries,
3314 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003315 if (!TypesLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003316 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003317 NumTypesLoaded, (unsigned)TypesLoaded.size(),
3318 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
3319 if (!DeclsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003320 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003321 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
3322 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003323 if (!IdentifiersLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003324 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003325 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
3326 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redl725cd962010-08-04 20:40:17 +00003327 if (!SelectorsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003328 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redl725cd962010-08-04 20:40:17 +00003329 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
3330 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor83941df2009-04-25 17:48:32 +00003331 if (TotalNumStatements)
3332 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
3333 NumStatementsRead, TotalNumStatements,
3334 ((float)NumStatementsRead/TotalNumStatements * 100));
3335 if (TotalNumMacros)
3336 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
3337 NumMacrosRead, TotalNumMacros,
3338 ((float)NumMacrosRead/TotalNumMacros * 100));
3339 if (TotalLexicalDeclContexts)
3340 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
3341 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
3342 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
3343 * 100));
3344 if (TotalVisibleDeclContexts)
3345 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
3346 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
3347 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
3348 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003349 if (TotalNumMethodPoolEntries) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003350 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003351 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
3352 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor83941df2009-04-25 17:48:32 +00003353 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003354 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor83941df2009-04-25 17:48:32 +00003355 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003356 std::fprintf(stderr, "\n");
3357}
3358
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003359void ASTReader::InitializeSema(Sema &S) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00003360 SemaObj = &S;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003361 S.ExternalSource = this;
3362
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00003363 // Makes sure any declarations that were deserialized "too early"
3364 // still get added to the identifier's declaration chains.
Douglas Gregor914ed9d2010-08-13 03:15:25 +00003365 if (SemaObj->TUScope) {
3366 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
John McCalld226f652010-08-21 09:40:31 +00003367 SemaObj->TUScope->AddDecl(PreloadedDecls[I]);
Douglas Gregor914ed9d2010-08-13 03:15:25 +00003368 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
3369 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00003370 }
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00003371 PreloadedDecls.clear();
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003372
3373 // If there were any tentative definitions, deserialize them and add
Sebastian Redle9d12b62010-01-31 22:27:38 +00003374 // them to Sema's list of tentative definitions.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003375 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
3376 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
Sebastian Redle9d12b62010-01-31 22:27:38 +00003377 SemaObj->TentativeDefinitions.push_back(Var);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003378 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00003379
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003380 // If there were any unused file scoped decls, deserialize them and add to
3381 // Sema's list of unused file scoped decls.
3382 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
3383 DeclaratorDecl *D = cast<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
3384 SemaObj->UnusedFileScopedDecls.push_back(D);
Tanya Lattnere6bbc012010-02-12 00:07:30 +00003385 }
Douglas Gregor14c22f22009-04-22 22:18:58 +00003386
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003387 // If there were any weak undeclared identifiers, deserialize them and add to
3388 // Sema's list of weak undeclared identifiers.
3389 if (!WeakUndeclaredIdentifiers.empty()) {
3390 unsigned Idx = 0;
3391 for (unsigned I = 0, N = WeakUndeclaredIdentifiers[Idx++]; I != N; ++I) {
3392 IdentifierInfo *WeakId = GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3393 IdentifierInfo *AliasId=GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3394 SourceLocation Loc = ReadSourceLocation(WeakUndeclaredIdentifiers, Idx);
3395 bool Used = WeakUndeclaredIdentifiers[Idx++];
3396 Sema::WeakInfo WI(AliasId, Loc);
3397 WI.setUsed(Used);
3398 SemaObj->WeakUndeclaredIdentifiers.insert(std::make_pair(WeakId, WI));
3399 }
3400 }
3401
Douglas Gregor14c22f22009-04-22 22:18:58 +00003402 // If there were any locally-scoped external declarations,
3403 // deserialize them and add them to Sema's table of locally-scoped
3404 // external declarations.
3405 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
3406 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
3407 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
3408 }
Douglas Gregorb81c1702009-04-27 20:06:05 +00003409
3410 // If there were any ext_vector type declarations, deserialize them
3411 // and add them to Sema's vector of such declarations.
3412 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I)
3413 SemaObj->ExtVectorDecls.push_back(
3414 cast<TypedefDecl>(GetDecl(ExtVectorDecls[I])));
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003415
3416 // FIXME: Do VTable uses and dynamic classes deserialize too much ?
3417 // Can we cut them down before writing them ?
3418
3419 // If there were any VTable uses, deserialize the information and add it
3420 // to Sema's vector and map of VTable uses.
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003421 if (!VTableUses.empty()) {
3422 unsigned Idx = 0;
3423 for (unsigned I = 0, N = VTableUses[Idx++]; I != N; ++I) {
3424 CXXRecordDecl *Class = cast<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
3425 SourceLocation Loc = ReadSourceLocation(VTableUses, Idx);
3426 bool DefinitionRequired = VTableUses[Idx++];
3427 SemaObj->VTableUses.push_back(std::make_pair(Class, Loc));
3428 SemaObj->VTablesUsed[Class] = DefinitionRequired;
3429 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003430 }
3431
3432 // If there were any dynamic classes declarations, deserialize them
3433 // and add them to Sema's vector of such declarations.
3434 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I)
3435 SemaObj->DynamicClasses.push_back(
3436 cast<CXXRecordDecl>(GetDecl(DynamicClasses[I])));
Fariborz Jahanian32019832010-07-23 19:11:11 +00003437
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003438 // If there were any pending implicit instantiations, deserialize them
3439 // and add them to Sema's queue of such instantiations.
3440 assert(PendingImplicitInstantiations.size() % 2 == 0 &&
3441 "Expected pairs of entries");
3442 for (unsigned Idx = 0, N = PendingImplicitInstantiations.size(); Idx < N;) {
3443 ValueDecl *D=cast<ValueDecl>(GetDecl(PendingImplicitInstantiations[Idx++]));
3444 SourceLocation Loc = ReadSourceLocation(PendingImplicitInstantiations, Idx);
3445 SemaObj->PendingImplicitInstantiations.push_back(std::make_pair(D, Loc));
3446 }
3447
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003448 // Load the offsets of the declarations that Sema references.
3449 // They will be lazily deserialized when needed.
3450 if (!SemaDeclRefs.empty()) {
3451 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
3452 SemaObj->StdNamespace = SemaDeclRefs[0];
3453 SemaObj->StdBadAlloc = SemaDeclRefs[1];
3454 }
3455
Fariborz Jahanian32019832010-07-23 19:11:11 +00003456 // If there are @selector references added them to its pool. This is for
3457 // implementation of -Wselector.
Sebastian Redl725cd962010-08-04 20:40:17 +00003458 if (!ReferencedSelectorsData.empty()) {
3459 unsigned int DataSize = ReferencedSelectorsData.size()-1;
Fariborz Jahanian32019832010-07-23 19:11:11 +00003460 unsigned I = 0;
3461 while (I < DataSize) {
Sebastian Redl725cd962010-08-04 20:40:17 +00003462 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
Fariborz Jahanian32019832010-07-23 19:11:11 +00003463 SourceLocation SelLoc =
Sebastian Redl725cd962010-08-04 20:40:17 +00003464 SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
Fariborz Jahanian32019832010-07-23 19:11:11 +00003465 SemaObj->ReferencedSelectors.insert(std::make_pair(Sel, SelLoc));
3466 }
3467 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00003468}
3469
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003470IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Sebastian Redld8c5abb2010-08-02 18:30:12 +00003471 // Try to find this name within our on-disk hash tables. We start with the
3472 // most recent one, since that one contains the most up-to-date info.
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003473 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003474 ASTIdentifierLookupTable *IdTable
3475 = (ASTIdentifierLookupTable *)Chain[I]->IdentifierLookupTable;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003476 if (!IdTable)
3477 continue;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003478 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003479 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003480 if (Pos == IdTable->end())
3481 continue;
Douglas Gregor668c1a42009-04-21 22:25:48 +00003482
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003483 // Dereferencing the iterator has the effect of building the
3484 // IdentifierInfo node and populating it with the various
3485 // declarations it needs.
Sebastian Redld8c5abb2010-08-02 18:30:12 +00003486 return *Pos;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003487 }
Sebastian Redld8c5abb2010-08-02 18:30:12 +00003488 return 0;
Douglas Gregor668c1a42009-04-21 22:25:48 +00003489}
3490
Mike Stump1eb44332009-09-09 15:08:12 +00003491std::pair<ObjCMethodList, ObjCMethodList>
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003492ASTReader::ReadMethodPool(Selector Sel) {
Sebastian Redl725cd962010-08-04 20:40:17 +00003493 // Find this selector in a hash table. We want to find the most recent entry.
3494 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3495 PerFileData &F = *Chain[I];
3496 if (!F.SelectorLookupTable)
3497 continue;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003498
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003499 ASTSelectorLookupTable *PoolTable
3500 = (ASTSelectorLookupTable*)F.SelectorLookupTable;
3501 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
Sebastian Redl725cd962010-08-04 20:40:17 +00003502 if (Pos != PoolTable->end()) {
3503 ++NumSelectorsRead;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003504 // FIXME: Not quite happy with the statistics here. We probably should
3505 // disable this tracking when called via LoadSelector.
3506 // Also, should entries without methods count as misses?
3507 ++NumMethodPoolEntriesRead;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003508 ASTSelectorLookupTrait::data_type Data = *Pos;
Sebastian Redl725cd962010-08-04 20:40:17 +00003509 if (DeserializationListener)
3510 DeserializationListener->SelectorRead(Data.ID, Sel);
3511 return std::make_pair(Data.Instance, Data.Factory);
3512 }
Douglas Gregor83941df2009-04-25 17:48:32 +00003513 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003514
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003515 ++NumMethodPoolMisses;
Sebastian Redl725cd962010-08-04 20:40:17 +00003516 return std::pair<ObjCMethodList, ObjCMethodList>();
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003517}
3518
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003519void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redle58aa892010-08-04 18:21:41 +00003520 // It would be complicated to avoid reading the methods anyway. So don't.
3521 ReadMethodPool(Sel);
3522}
3523
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003524void ASTReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00003525 assert(ID && "Non-zero identifier ID required");
Douglas Gregora02b1472009-04-28 21:53:25 +00003526 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003527 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003528 if (DeserializationListener)
3529 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregor668c1a42009-04-21 22:25:48 +00003530}
3531
Douglas Gregord89275b2009-07-06 18:54:52 +00003532/// \brief Set the globally-visible declarations associated with the given
3533/// identifier.
3534///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003535/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump1eb44332009-09-09 15:08:12 +00003536/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregord89275b2009-07-06 18:54:52 +00003537/// them.
3538///
3539/// \param II an IdentifierInfo that refers to one or more globally-visible
3540/// declarations.
3541///
3542/// \param DeclIDs the set of declaration IDs with the name @p II that are
3543/// visible at global scope.
3544///
3545/// \param Nonrecursive should be true to indicate that the caller knows that
3546/// this call is non-recursive, and therefore the globally-visible declarations
3547/// will not be placed onto the pending queue.
Mike Stump1eb44332009-09-09 15:08:12 +00003548void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003549ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Douglas Gregord89275b2009-07-06 18:54:52 +00003550 const llvm::SmallVectorImpl<uint32_t> &DeclIDs,
3551 bool Nonrecursive) {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00003552 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregord89275b2009-07-06 18:54:52 +00003553 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
3554 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
3555 PII.II = II;
3556 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I)
3557 PII.DeclIDs.push_back(DeclIDs[I]);
3558 return;
3559 }
Mike Stump1eb44332009-09-09 15:08:12 +00003560
Douglas Gregord89275b2009-07-06 18:54:52 +00003561 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
3562 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
3563 if (SemaObj) {
Douglas Gregor914ed9d2010-08-13 03:15:25 +00003564 if (SemaObj->TUScope) {
3565 // Introduce this declaration into the translation-unit scope
3566 // and add it to the declaration chain for this identifier, so
3567 // that (unqualified) name lookup will find it.
John McCalld226f652010-08-21 09:40:31 +00003568 SemaObj->TUScope->AddDecl(D);
Douglas Gregor914ed9d2010-08-13 03:15:25 +00003569 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
3570 }
Douglas Gregord89275b2009-07-06 18:54:52 +00003571 } else {
3572 // Queue this declaration so that it will be added to the
3573 // translation unit scope and identifier's declaration chain
3574 // once a Sema object is known.
3575 PreloadedDecls.push_back(D);
3576 }
3577 }
3578}
3579
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003580IdentifierInfo *ASTReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00003581 if (ID == 0)
3582 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003583
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00003584 if (IdentifiersLoaded.empty()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003585 Error("no identifier table in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00003586 return 0;
3587 }
Mike Stump1eb44332009-09-09 15:08:12 +00003588
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003589 assert(PP && "Forgot to set Preprocessor ?");
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00003590 ID -= 1;
3591 if (!IdentifiersLoaded[ID]) {
3592 unsigned Index = ID;
3593 const char *Str = 0;
3594 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3595 PerFileData *F = Chain[N - I - 1];
3596 if (Index < F->LocalNumIdentifiers) {
3597 uint32_t Offset = F->IdentifierOffsets[Index];
3598 Str = F->IdentifierTableData + Offset;
3599 break;
3600 }
3601 Index -= F->LocalNumIdentifiers;
3602 }
3603 assert(Str && "Broken Chain");
Douglas Gregord6595a42009-04-25 21:04:17 +00003604
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003605 // All of the strings in the AST file are preceded by a 16-bit length.
3606 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenek231bc0b2009-10-23 04:45:31 +00003607 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
3608 // unsigned integers. This is important to avoid integer overflow when
3609 // we cast them to 'unsigned'.
Ted Kremenekff1ea462009-10-23 03:57:22 +00003610 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregor02fc7512009-04-28 20:01:51 +00003611 unsigned StrLen = (((unsigned) StrLenPtr[0])
3612 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00003613 IdentifiersLoaded[ID]
Kovarththanan Rajaratnam811f4262010-03-12 10:32:27 +00003614 = &PP->getIdentifierTable().get(Str, StrLen);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003615 if (DeserializationListener)
3616 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregorafaf3082009-04-11 00:14:32 +00003617 }
Mike Stump1eb44332009-09-09 15:08:12 +00003618
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00003619 return IdentifiersLoaded[ID];
Douglas Gregor2cf26342009-04-09 22:27:44 +00003620}
3621
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003622void ASTReader::ReadSLocEntry(unsigned ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00003623 ReadSLocEntryRecord(ID);
3624}
3625
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003626Selector ASTReader::DecodeSelector(unsigned ID) {
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003627 if (ID == 0)
3628 return Selector();
Mike Stump1eb44332009-09-09 15:08:12 +00003629
Sebastian Redl725cd962010-08-04 20:40:17 +00003630 if (ID > SelectorsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003631 Error("selector ID out of range in AST file");
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003632 return Selector();
3633 }
Douglas Gregor83941df2009-04-25 17:48:32 +00003634
Sebastian Redl725cd962010-08-04 20:40:17 +00003635 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003636 // Load this selector from the selector table.
Sebastian Redl725cd962010-08-04 20:40:17 +00003637 unsigned Idx = ID - 1;
3638 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3639 PerFileData &F = *Chain[N - I - 1];
3640 if (Idx < F.LocalNumSelectors) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003641 ASTSelectorLookupTrait Trait(*this);
Sebastian Redl725cd962010-08-04 20:40:17 +00003642 SelectorsLoaded[ID - 1] =
3643 Trait.ReadKey(F.SelectorLookupTableData + F.SelectorOffsets[Idx], 0);
3644 if (DeserializationListener)
3645 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
3646 break;
3647 }
3648 Idx -= F.LocalNumSelectors;
3649 }
Douglas Gregor83941df2009-04-25 17:48:32 +00003650 }
3651
Sebastian Redl725cd962010-08-04 20:40:17 +00003652 return SelectorsLoaded[ID - 1];
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003653}
3654
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003655Selector ASTReader::GetExternalSelector(uint32_t ID) {
Douglas Gregor719770d2010-04-06 17:30:22 +00003656 return DecodeSelector(ID);
3657}
3658
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003659uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redl725cd962010-08-04 20:40:17 +00003660 // ID 0 (the null selector) is considered an external selector.
3661 return getTotalNumSelectors() + 1;
Douglas Gregor719770d2010-04-06 17:30:22 +00003662}
3663
Mike Stump1eb44332009-09-09 15:08:12 +00003664DeclarationName
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003665ASTReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003666 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
3667 switch (Kind) {
3668 case DeclarationName::Identifier:
3669 return DeclarationName(GetIdentifierInfo(Record, Idx));
3670
3671 case DeclarationName::ObjCZeroArgSelector:
3672 case DeclarationName::ObjCOneArgSelector:
3673 case DeclarationName::ObjCMultiArgSelector:
Steve Naroffa7503a72009-04-23 15:15:40 +00003674 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00003675
3676 case DeclarationName::CXXConstructorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003677 return Context->DeclarationNames.getCXXConstructorName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00003678 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00003679
3680 case DeclarationName::CXXDestructorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003681 return Context->DeclarationNames.getCXXDestructorName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00003682 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00003683
3684 case DeclarationName::CXXConversionFunctionName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003685 return Context->DeclarationNames.getCXXConversionFunctionName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00003686 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00003687
3688 case DeclarationName::CXXOperatorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003689 return Context->DeclarationNames.getCXXOperatorName(
Douglas Gregor2cf26342009-04-09 22:27:44 +00003690 (OverloadedOperatorKind)Record[Idx++]);
3691
Sean Hunt3e518bd2009-11-29 07:34:05 +00003692 case DeclarationName::CXXLiteralOperatorName:
3693 return Context->DeclarationNames.getCXXLiteralOperatorName(
3694 GetIdentifierInfo(Record, Idx));
3695
Douglas Gregor2cf26342009-04-09 22:27:44 +00003696 case DeclarationName::CXXUsingDirective:
3697 return DeclarationName::getUsingDirectiveName();
3698 }
3699
3700 // Required to silence GCC warning
3701 return DeclarationName();
3702}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003703
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003704TemplateName
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003705ASTReader::ReadTemplateName(const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003706 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
3707 switch (Kind) {
3708 case TemplateName::Template:
3709 return TemplateName(cast_or_null<TemplateDecl>(GetDecl(Record[Idx++])));
3710
3711 case TemplateName::OverloadedTemplate: {
3712 unsigned size = Record[Idx++];
3713 UnresolvedSet<8> Decls;
3714 while (size--)
3715 Decls.addDecl(cast<NamedDecl>(GetDecl(Record[Idx++])));
3716
3717 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
3718 }
3719
3720 case TemplateName::QualifiedTemplate: {
3721 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3722 bool hasTemplKeyword = Record[Idx++];
3723 TemplateDecl *Template = cast<TemplateDecl>(GetDecl(Record[Idx++]));
3724 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
3725 }
3726
3727 case TemplateName::DependentTemplate: {
3728 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3729 if (Record[Idx++]) // isIdentifier
3730 return Context->getDependentTemplateName(NNS,
3731 GetIdentifierInfo(Record, Idx));
3732 return Context->getDependentTemplateName(NNS,
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003733 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003734 }
3735 }
3736
3737 assert(0 && "Unhandled template name kind!");
3738 return TemplateName();
3739}
3740
3741TemplateArgument
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003742ASTReader::ReadTemplateArgument(llvm::BitstreamCursor &DeclsCursor,
Sebastian Redl577d4792010-07-22 22:43:28 +00003743 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003744 switch ((TemplateArgument::ArgKind)Record[Idx++]) {
3745 case TemplateArgument::Null:
3746 return TemplateArgument();
3747 case TemplateArgument::Type:
3748 return TemplateArgument(GetType(Record[Idx++]));
3749 case TemplateArgument::Declaration:
3750 return TemplateArgument(GetDecl(Record[Idx++]));
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00003751 case TemplateArgument::Integral: {
3752 llvm::APSInt Value = ReadAPSInt(Record, Idx);
3753 QualType T = GetType(Record[Idx++]);
3754 return TemplateArgument(Value, T);
3755 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003756 case TemplateArgument::Template:
3757 return TemplateArgument(ReadTemplateName(Record, Idx));
3758 case TemplateArgument::Expression:
Sebastian Redl577d4792010-07-22 22:43:28 +00003759 return TemplateArgument(ReadExpr(DeclsCursor));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003760 case TemplateArgument::Pack: {
3761 unsigned NumArgs = Record[Idx++];
3762 llvm::SmallVector<TemplateArgument, 8> Args;
3763 Args.reserve(NumArgs);
3764 while (NumArgs--)
Sebastian Redl577d4792010-07-22 22:43:28 +00003765 Args.push_back(ReadTemplateArgument(DeclsCursor, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003766 TemplateArgument TemplArg;
3767 TemplArg.setArgumentPack(Args.data(), Args.size(), /*CopyArgs=*/true);
3768 return TemplArg;
3769 }
3770 }
3771
3772 assert(0 && "Unhandled template argument kind!");
3773 return TemplateArgument();
3774}
3775
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003776TemplateParameterList *
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003777ASTReader::ReadTemplateParameterList(const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003778 SourceLocation TemplateLoc = ReadSourceLocation(Record, Idx);
3779 SourceLocation LAngleLoc = ReadSourceLocation(Record, Idx);
3780 SourceLocation RAngleLoc = ReadSourceLocation(Record, Idx);
3781
3782 unsigned NumParams = Record[Idx++];
3783 llvm::SmallVector<NamedDecl *, 16> Params;
3784 Params.reserve(NumParams);
3785 while (NumParams--)
3786 Params.push_back(cast<NamedDecl>(GetDecl(Record[Idx++])));
3787
3788 TemplateParameterList* TemplateParams =
3789 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
3790 Params.data(), Params.size(), RAngleLoc);
3791 return TemplateParams;
3792}
3793
3794void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003795ASTReader::
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003796ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs,
Sebastian Redl577d4792010-07-22 22:43:28 +00003797 llvm::BitstreamCursor &DeclsCursor,
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003798 const RecordData &Record, unsigned &Idx) {
3799 unsigned NumTemplateArgs = Record[Idx++];
3800 TemplArgs.reserve(NumTemplateArgs);
3801 while (NumTemplateArgs--)
Sebastian Redl577d4792010-07-22 22:43:28 +00003802 TemplArgs.push_back(ReadTemplateArgument(DeclsCursor, Record, Idx));
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003803}
3804
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00003805/// \brief Read a UnresolvedSet structure.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003806void ASTReader::ReadUnresolvedSet(UnresolvedSetImpl &Set,
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00003807 const RecordData &Record, unsigned &Idx) {
3808 unsigned NumDecls = Record[Idx++];
3809 while (NumDecls--) {
3810 NamedDecl *D = cast<NamedDecl>(GetDecl(Record[Idx++]));
3811 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
3812 Set.addDecl(D, AS);
3813 }
3814}
3815
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003816CXXBaseSpecifier
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003817ASTReader::ReadCXXBaseSpecifier(llvm::BitstreamCursor &DeclsCursor,
Nick Lewycky56062202010-07-26 16:56:01 +00003818 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003819 bool isVirtual = static_cast<bool>(Record[Idx++]);
3820 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
3821 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Nick Lewycky56062202010-07-26 16:56:01 +00003822 TypeSourceInfo *TInfo = GetTypeSourceInfo(DeclsCursor, Record, Idx);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003823 SourceRange Range = ReadSourceRange(Record, Idx);
Nick Lewycky56062202010-07-26 16:56:01 +00003824 return CXXBaseSpecifier(Range, isVirtual, isBaseOfClass, AS, TInfo);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003825}
3826
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003827std::pair<CXXBaseOrMemberInitializer **, unsigned>
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003828ASTReader::ReadCXXBaseOrMemberInitializers(llvm::BitstreamCursor &Cursor,
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003829 const RecordData &Record,
3830 unsigned &Idx) {
3831 CXXBaseOrMemberInitializer **BaseOrMemberInitializers = 0;
3832 unsigned NumInitializers = Record[Idx++];
3833 if (NumInitializers) {
3834 ASTContext &C = *getContext();
3835
3836 BaseOrMemberInitializers
3837 = new (C) CXXBaseOrMemberInitializer*[NumInitializers];
3838 for (unsigned i=0; i != NumInitializers; ++i) {
3839 TypeSourceInfo *BaseClassInfo = 0;
3840 bool IsBaseVirtual = false;
3841 FieldDecl *Member = 0;
3842
3843 bool IsBaseInitializer = Record[Idx++];
3844 if (IsBaseInitializer) {
3845 BaseClassInfo = GetTypeSourceInfo(Cursor, Record, Idx);
3846 IsBaseVirtual = Record[Idx++];
3847 } else {
3848 Member = cast<FieldDecl>(GetDecl(Record[Idx++]));
3849 }
3850 SourceLocation MemberLoc = ReadSourceLocation(Record, Idx);
3851 Expr *Init = ReadExpr(Cursor);
3852 FieldDecl *AnonUnionMember
3853 = cast_or_null<FieldDecl>(GetDecl(Record[Idx++]));
3854 SourceLocation LParenLoc = ReadSourceLocation(Record, Idx);
3855 SourceLocation RParenLoc = ReadSourceLocation(Record, Idx);
3856 bool IsWritten = Record[Idx++];
3857 unsigned SourceOrderOrNumArrayIndices;
3858 llvm::SmallVector<VarDecl *, 8> Indices;
3859 if (IsWritten) {
3860 SourceOrderOrNumArrayIndices = Record[Idx++];
3861 } else {
3862 SourceOrderOrNumArrayIndices = Record[Idx++];
3863 Indices.reserve(SourceOrderOrNumArrayIndices);
3864 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
3865 Indices.push_back(cast<VarDecl>(GetDecl(Record[Idx++])));
3866 }
3867
3868 CXXBaseOrMemberInitializer *BOMInit;
3869 if (IsBaseInitializer) {
3870 BOMInit = new (C) CXXBaseOrMemberInitializer(C, BaseClassInfo,
3871 IsBaseVirtual, LParenLoc,
3872 Init, RParenLoc);
3873 } else if (IsWritten) {
3874 BOMInit = new (C) CXXBaseOrMemberInitializer(C, Member, MemberLoc,
3875 LParenLoc, Init, RParenLoc);
3876 } else {
3877 BOMInit = CXXBaseOrMemberInitializer::Create(C, Member, MemberLoc,
3878 LParenLoc, Init, RParenLoc,
3879 Indices.data(),
3880 Indices.size());
3881 }
3882
3883 BOMInit->setAnonUnionMember(AnonUnionMember);
3884 BaseOrMemberInitializers[i] = BOMInit;
3885 }
3886 }
3887
3888 return std::make_pair(BaseOrMemberInitializers, NumInitializers);
3889}
3890
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003891NestedNameSpecifier *
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003892ASTReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003893 unsigned N = Record[Idx++];
3894 NestedNameSpecifier *NNS = 0, *Prev = 0;
3895 for (unsigned I = 0; I != N; ++I) {
3896 NestedNameSpecifier::SpecifierKind Kind
3897 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
3898 switch (Kind) {
3899 case NestedNameSpecifier::Identifier: {
3900 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
3901 NNS = NestedNameSpecifier::Create(*Context, Prev, II);
3902 break;
3903 }
3904
3905 case NestedNameSpecifier::Namespace: {
3906 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
3907 NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
3908 break;
3909 }
3910
3911 case NestedNameSpecifier::TypeSpec:
3912 case NestedNameSpecifier::TypeSpecWithTemplate: {
3913 Type *T = GetType(Record[Idx++]).getTypePtr();
3914 bool Template = Record[Idx++];
3915 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
3916 break;
3917 }
3918
3919 case NestedNameSpecifier::Global: {
3920 NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
3921 // No associated value, and there can't be a prefix.
3922 break;
3923 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003924 }
Argyrios Kyrtzidisd2bb2c02010-07-07 15:46:30 +00003925 Prev = NNS;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003926 }
3927 return NNS;
3928}
3929
3930SourceRange
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003931ASTReader::ReadSourceRange(const RecordData &Record, unsigned &Idx) {
Daniel Dunbar8ee59392010-06-02 15:47:10 +00003932 SourceLocation beg = SourceLocation::getFromRawEncoding(Record[Idx++]);
3933 SourceLocation end = SourceLocation::getFromRawEncoding(Record[Idx++]);
3934 return SourceRange(beg, end);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003935}
3936
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003937/// \brief Read an integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003938llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003939 unsigned BitWidth = Record[Idx++];
3940 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
3941 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
3942 Idx += NumWords;
3943 return Result;
3944}
3945
3946/// \brief Read a signed integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003947llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003948 bool isUnsigned = Record[Idx++];
3949 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
3950}
3951
Douglas Gregor17fc2232009-04-14 21:55:33 +00003952/// \brief Read a floating-point value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003953llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00003954 return llvm::APFloat(ReadAPInt(Record, Idx));
3955}
3956
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003957// \brief Read a string
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003958std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003959 unsigned Len = Record[Idx++];
Jay Foadbeaaccd2009-05-21 09:52:38 +00003960 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003961 Idx += Len;
3962 return Result;
3963}
3964
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003965CXXTemporary *ASTReader::ReadCXXTemporary(const RecordData &Record,
Chris Lattnerd2598362010-05-10 00:25:06 +00003966 unsigned &Idx) {
3967 CXXDestructorDecl *Decl = cast<CXXDestructorDecl>(GetDecl(Record[Idx++]));
3968 return CXXTemporary::Create(*Context, Decl);
3969}
3970
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003971DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00003972 return Diag(SourceLocation(), DiagID);
3973}
3974
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003975DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003976 return Diags.Report(FullSourceLoc(Loc, SourceMgr), DiagID);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003977}
Douglas Gregor025452f2009-04-17 00:04:06 +00003978
Douglas Gregor668c1a42009-04-21 22:25:48 +00003979/// \brief Retrieve the identifier table associated with the
3980/// preprocessor.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003981IdentifierTable &ASTReader::getIdentifierTable() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003982 assert(PP && "Forgot to set Preprocessor ?");
3983 return PP->getIdentifierTable();
Douglas Gregor668c1a42009-04-21 22:25:48 +00003984}
3985
Douglas Gregor025452f2009-04-17 00:04:06 +00003986/// \brief Record that the given ID maps to the given switch-case
3987/// statement.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003988void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Douglas Gregor025452f2009-04-17 00:04:06 +00003989 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
3990 SwitchCaseStmts[ID] = SC;
3991}
3992
3993/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003994SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Douglas Gregor025452f2009-04-17 00:04:06 +00003995 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
3996 return SwitchCaseStmts[ID];
3997}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00003998
3999/// \brief Record that the given label statement has been
4000/// deserialized and has the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004001void ASTReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
Mike Stump1eb44332009-09-09 15:08:12 +00004002 assert(LabelStmts.find(ID) == LabelStmts.end() &&
Douglas Gregor1de05fe2009-04-17 18:18:49 +00004003 "Deserialized label twice");
4004 LabelStmts[ID] = S;
4005
4006 // If we've already seen any goto statements that point to this
4007 // label, resolve them now.
4008 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
4009 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
4010 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
4011 Goto->second->setLabel(S);
4012 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00004013
4014 // If we've already seen any address-label statements that point to
4015 // this label, resolve them now.
4016 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
Mike Stump1eb44332009-09-09 15:08:12 +00004017 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00004018 = UnresolvedAddrLabelExprs.equal_range(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00004019 for (AddrLabelIter AddrLabel = AddrLabels.first;
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00004020 AddrLabel != AddrLabels.second; ++AddrLabel)
4021 AddrLabel->second->setLabel(S);
4022 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor1de05fe2009-04-17 18:18:49 +00004023}
4024
4025/// \brief Set the label of the given statement to the label
4026/// identified by ID.
4027///
4028/// Depending on the order in which the label and other statements
4029/// referencing that label occur, this operation may complete
4030/// immediately (updating the statement) or it may queue the
4031/// statement to be back-patched later.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004032void ASTReader::SetLabelOf(GotoStmt *S, unsigned ID) {
Douglas Gregor1de05fe2009-04-17 18:18:49 +00004033 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
4034 if (Label != LabelStmts.end()) {
4035 // We've already seen this label, so set the label of the goto and
4036 // we're done.
4037 S->setLabel(Label->second);
4038 } else {
4039 // We haven't seen this label yet, so add this goto to the set of
4040 // unresolved goto statements.
4041 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
4042 }
4043}
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00004044
4045/// \brief Set the label of the given expression to the label
4046/// identified by ID.
4047///
4048/// Depending on the order in which the label and other statements
4049/// referencing that label occur, this operation may complete
4050/// immediately (updating the statement) or it may queue the
4051/// statement to be back-patched later.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004052void ASTReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00004053 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
4054 if (Label != LabelStmts.end()) {
4055 // We've already seen this label, so set the label of the
4056 // label-address expression and we're done.
4057 S->setLabel(Label->second);
4058 } else {
4059 // We haven't seen this label yet, so add this label-address
4060 // expression to the set of unresolved label-address expressions.
4061 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
4062 }
4063}
Douglas Gregord89275b2009-07-06 18:54:52 +00004064
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004065void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004066 assert(NumCurrentElementsDeserializing &&
4067 "FinishedDeserializing not paired with StartedDeserializing");
4068 if (NumCurrentElementsDeserializing == 1) {
Douglas Gregord89275b2009-07-06 18:54:52 +00004069 // If any identifiers with corresponding top-level declarations have
4070 // been loaded, load those declarations now.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004071 while (!PendingIdentifierInfos.empty()) {
4072 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
4073 PendingIdentifierInfos.front().DeclIDs, true);
4074 PendingIdentifierInfos.pop_front();
Douglas Gregord89275b2009-07-06 18:54:52 +00004075 }
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00004076
4077 // We are not in recursive loading, so it's safe to pass the "interesting"
4078 // decls to the consumer.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004079 if (Consumer)
4080 PassInterestingDeclsToConsumer();
Douglas Gregord89275b2009-07-06 18:54:52 +00004081 }
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004082 --NumCurrentElementsDeserializing;
Douglas Gregord89275b2009-07-06 18:54:52 +00004083}
Douglas Gregor501c1032010-08-19 00:28:17 +00004084
4085ASTReader::PerFileData::PerFileData()
4086 : StatCache(0), LocalNumSLocEntries(0), LocalNumTypes(0), TypeOffsets(0),
4087 LocalNumDecls(0), DeclOffsets(0), LocalNumIdentifiers(0),
4088 IdentifierOffsets(0), IdentifierTableData(0), IdentifierLookupTable(0),
4089 LocalNumMacroDefinitions(0), MacroDefinitionOffsets(0),
4090 NumPreallocatedPreprocessingEntities(0), SelectorLookupTable(0),
4091 SelectorLookupTableData(0), SelectorOffsets(0), LocalNumSelectors(0)
4092{}
4093
4094ASTReader::PerFileData::~PerFileData() {
4095 delete static_cast<ASTIdentifierLookupTable *>(IdentifierLookupTable);
4096 delete static_cast<ASTSelectorLookupTable *>(SelectorLookupTable);
4097}
4098