blob: 10b01d8c9f9073eea576cf15a052afd2592ec856 [file] [log] [blame]
Sebastian Redl904c9c82010-08-18 23:57:11 +00001//===--- ASTReader.cpp - AST File Reader ------------------------*- C++ -*-===//
Douglas Gregor2cf26342009-04-09 22:27:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Sebastian Redlc43b54c2010-08-18 23:56:43 +000010// This file defines the ASTReader class, which reads AST files.
Douglas Gregor2cf26342009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
Chris Lattner4c6f9522009-04-27 05:14:47 +000013
Sebastian Redl6ab7cd82010-08-18 23:57:17 +000014#include "clang/Serialization/ASTReader.h"
15#include "clang/Serialization/ASTDeserializationListener.h"
Douglas Gregor98339b92011-08-25 20:47:51 +000016#include "clang/Serialization/ModuleManager.h"
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +000017#include "ASTCommon.h"
Douglas Gregor98339b92011-08-25 20:47:51 +000018#include "ASTReaderInternals.h"
Douglas Gregor0a0428e2009-04-10 20:39:37 +000019#include "clang/Frontend/FrontendDiagnostic.h"
Daniel Dunbarc7162932009-11-11 23:58:53 +000020#include "clang/Frontend/Utils.h"
Douglas Gregore737f502010-08-12 20:07:10 +000021#include "clang/Sema/Sema.h"
John McCall5f1e0942010-08-24 08:50:51 +000022#include "clang/Sema/Scope.h"
Douglas Gregorfdd01722009-04-14 00:24:19 +000023#include "clang/AST/ASTConsumer.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000024#include "clang/AST/ASTContext.h"
John McCall2a7fb272010-08-25 05:32:35 +000025#include "clang/AST/DeclTemplate.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000026#include "clang/AST/Expr.h"
John McCall7a1fad32010-08-24 07:32:53 +000027#include "clang/AST/ExprCXX.h"
Douglas Gregor5f791bb2011-02-28 23:58:31 +000028#include "clang/AST/NestedNameSpecifier.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000029#include "clang/AST/Type.h"
John McCalla1ee0c52009-10-16 21:56:05 +000030#include "clang/AST/TypeLocVisitor.h"
Chris Lattner42d42b52009-04-10 21:41:48 +000031#include "clang/Lex/MacroInfo.h"
Douglas Gregor6a5a23f2010-03-19 21:51:54 +000032#include "clang/Lex/PreprocessingRecord.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000033#include "clang/Lex/Preprocessor.h"
Steve Naroff83d63c72009-04-24 20:03:17 +000034#include "clang/Lex/HeaderSearch.h"
Douglas Gregor668c1a42009-04-21 22:25:48 +000035#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000036#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000037#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000038#include "clang/Basic/FileManager.h"
Chris Lattner10e286a2010-11-23 19:19:34 +000039#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000040#include "clang/Basic/TargetInfo.h"
Douglas Gregor445e23e2009-10-05 21:07:28 +000041#include "clang/Basic/Version.h"
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000042#include "clang/Basic/VersionTuple.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000043#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000044#include "llvm/Bitcode/BitstreamReader.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000045#include "llvm/Support/MemoryBuffer.h"
John McCall833ca992009-10-29 08:12:44 +000046#include "llvm/Support/ErrorHandling.h"
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000047#include "llvm/Support/FileSystem.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000048#include "llvm/Support/Path.h"
Michael J. Spencer3a321e22010-12-09 17:36:38 +000049#include "llvm/Support/system_error.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000050#include <algorithm>
Douglas Gregore721f952009-04-28 18:58:38 +000051#include <iterator>
Douglas Gregor2cf26342009-04-09 22:27:44 +000052#include <cstdio>
Douglas Gregor4fed3f42009-04-27 18:38:38 +000053#include <sys/stat.h>
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000054
Douglas Gregor2cf26342009-04-09 22:27:44 +000055using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000056using namespace clang::serialization;
Douglas Gregor98339b92011-08-25 20:47:51 +000057using namespace clang::serialization::reader;
Douglas Gregor2cf26342009-04-09 22:27:44 +000058
59//===----------------------------------------------------------------------===//
Sebastian Redl3c7f4132010-08-18 23:57:06 +000060// PCH validator implementation
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000061//===----------------------------------------------------------------------===//
62
Sebastian Redl571db7f2010-08-18 23:56:56 +000063ASTReaderListener::~ASTReaderListener() {}
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000064
65bool
66PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts) {
67 const LangOptions &PPLangOpts = PP.getLangOptions();
68#define PARSE_LANGOPT_BENIGN(Option)
69#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
70 if (PPLangOpts.Option != LangOpts.Option) { \
71 Reader.Diag(DiagID) << LangOpts.Option << PPLangOpts.Option; \
72 return true; \
73 }
74
75 PARSE_LANGOPT_BENIGN(Trigraphs);
76 PARSE_LANGOPT_BENIGN(BCPLComment);
77 PARSE_LANGOPT_BENIGN(DollarIdents);
78 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
79 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
Chandler Carrutheb5d7b72010-04-17 20:17:31 +000080 PARSE_LANGOPT_IMPORTANT(GNUKeywords, diag::warn_pch_gnu_keywords);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000081 PARSE_LANGOPT_BENIGN(ImplicitInt);
82 PARSE_LANGOPT_BENIGN(Digraphs);
83 PARSE_LANGOPT_BENIGN(HexFloats);
84 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
Peter Collingbourne7e7fbd02011-04-15 00:35:23 +000085 PARSE_LANGOPT_IMPORTANT(C1X, diag::warn_pch_c1x);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000086 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
Michael J. Spencerdae4ac42010-10-21 05:21:48 +000087 PARSE_LANGOPT_BENIGN(MSCVersion);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000088 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
89 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
90 PARSE_LANGOPT_BENIGN(CXXOperatorName);
91 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
92 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
93 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
Fariborz Jahanian412e7982010-02-09 19:31:38 +000094 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI2, diag::warn_pch_nonfragile_abi2);
Fariborz Jahanianf84109e2011-01-07 18:59:25 +000095 PARSE_LANGOPT_IMPORTANT(AppleKext, diag::warn_pch_apple_kext);
Ted Kremenekc32647d2010-12-23 21:35:43 +000096 PARSE_LANGOPT_IMPORTANT(ObjCDefaultSynthProperties,
97 diag::warn_pch_objc_auto_properties);
Douglas Gregor74da19f2011-06-14 23:20:43 +000098 PARSE_LANGOPT_BENIGN(ObjCInferRelatedResultType)
Michael J. Spencer20249a12010-10-21 03:16:25 +000099 PARSE_LANGOPT_IMPORTANT(NoConstantCFStrings,
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +0000100 diag::warn_pch_no_constant_cfstrings);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000101 PARSE_LANGOPT_BENIGN(PascalStrings);
102 PARSE_LANGOPT_BENIGN(WritableStrings);
Mike Stump1eb44332009-09-09 15:08:12 +0000103 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000104 diag::warn_pch_lax_vector_conversions);
Nate Begeman69cfb9b2009-06-25 22:57:40 +0000105 PARSE_LANGOPT_IMPORTANT(AltiVec, diag::warn_pch_altivec);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000106 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
Anders Carlssonda4b7cf2011-02-19 23:53:54 +0000107 PARSE_LANGOPT_IMPORTANT(ObjCExceptions, diag::warn_pch_objc_exceptions);
Anders Carlsson7da99b02011-02-23 03:04:54 +0000108 PARSE_LANGOPT_IMPORTANT(CXXExceptions, diag::warn_pch_cxx_exceptions);
109 PARSE_LANGOPT_IMPORTANT(SjLjExceptions, diag::warn_pch_sjlj_exceptions);
Douglas Gregor6f755502011-02-01 15:15:22 +0000110 PARSE_LANGOPT_IMPORTANT(MSBitfields, diag::warn_pch_ms_bitfields);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000111 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
112 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
113 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
Mike Stump1eb44332009-09-09 15:08:12 +0000114 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000115 diag::warn_pch_thread_safe_statics);
Daniel Dunbar5345c392009-09-03 04:54:28 +0000116 PARSE_LANGOPT_IMPORTANT(POSIXThreads, diag::warn_pch_posix_threads);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000117 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
118 PARSE_LANGOPT_BENIGN(EmitAllDecls);
119 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
Chris Lattnera4d71452010-06-26 21:25:03 +0000120 PARSE_LANGOPT_BENIGN(getSignedOverflowBehavior());
Mike Stump1eb44332009-09-09 15:08:12 +0000121 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000122 diag::warn_pch_heinous_extensions);
123 // FIXME: Most of the options below are benign if the macro wasn't
124 // used. Unfortunately, this means that a PCH compiled without
125 // optimization can't be used with optimization turned on, even
126 // though the only thing that changes is whether __OPTIMIZE__ was
127 // defined... but if __OPTIMIZE__ never showed up in the header, it
128 // doesn't matter. We could consider making this some special kind
129 // of check.
130 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
131 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
132 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
133 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
134 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
135 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
Chandler Carruth0d2d1bc2011-04-23 20:05:38 +0000136 PARSE_LANGOPT_IMPORTANT(Deprecated, diag::warn_pch_deprecated);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000137 PARSE_LANGOPT_IMPORTANT(AccessControl, diag::warn_pch_access_control);
138 PARSE_LANGOPT_IMPORTANT(CharIsSigned, diag::warn_pch_char_signed);
John Thompsona6fda122009-11-05 20:14:16 +0000139 PARSE_LANGOPT_IMPORTANT(ShortWChar, diag::warn_pch_short_wchar);
Argyrios Kyrtzidis9a2b9d72010-10-08 00:25:19 +0000140 PARSE_LANGOPT_IMPORTANT(ShortEnums, diag::warn_pch_short_enums);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000141 if ((PPLangOpts.getGCMode() != 0) != (LangOpts.getGCMode() != 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000142 Reader.Diag(diag::warn_pch_gc_mode)
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000143 << LangOpts.getGCMode() << PPLangOpts.getGCMode();
144 return true;
145 }
146 PARSE_LANGOPT_BENIGN(getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000147 PARSE_LANGOPT_IMPORTANT(getStackProtectorMode(),
148 diag::warn_pch_stack_protector);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000149 PARSE_LANGOPT_BENIGN(InstantiationDepth);
Nate Begeman69cfb9b2009-06-25 22:57:40 +0000150 PARSE_LANGOPT_IMPORTANT(OpenCL, diag::warn_pch_opencl);
Peter Collingbourne08a53262010-12-01 19:14:57 +0000151 PARSE_LANGOPT_IMPORTANT(CUDA, diag::warn_pch_cuda);
Mike Stump9c276ae2009-12-12 01:27:46 +0000152 PARSE_LANGOPT_BENIGN(CatchUndefined);
John McCallf85e1932011-06-15 23:02:42 +0000153 PARSE_LANGOPT_BENIGN(DefaultFPContract);
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000154 PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors);
Douglas Gregora0068fc2010-07-09 17:35:33 +0000155 PARSE_LANGOPT_BENIGN(SpellChecking);
John McCallf85e1932011-06-15 23:02:42 +0000156 PARSE_LANGOPT_IMPORTANT(ObjCAutoRefCount, diag::warn_pch_auto_ref_count);
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +0000157#undef PARSE_LANGOPT_IMPORTANT
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000158#undef PARSE_LANGOPT_BENIGN
159
160 return false;
161}
162
Chris Lattner5f9e2722011-07-23 10:55:15 +0000163bool PCHValidator::ReadTargetTriple(StringRef Triple) {
Daniel Dunbardc3c0d22009-11-11 00:52:11 +0000164 if (Triple == PP.getTargetInfo().getTriple().str())
165 return false;
166
167 Reader.Diag(diag::warn_pch_target_triple)
168 << Triple << PP.getTargetInfo().getTriple().str();
169 return true;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000170}
171
Benjamin Kramer54353f42010-11-25 18:29:30 +0000172namespace {
173 struct EmptyStringRef {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000174 bool operator ()(StringRef r) const { return r.empty(); }
Benjamin Kramer54353f42010-11-25 18:29:30 +0000175 };
176 struct EmptyBlock {
177 bool operator ()(const PCHPredefinesBlock &r) const {return r.Data.empty();}
178 };
179}
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000180
Chris Lattner5f9e2722011-07-23 10:55:15 +0000181static bool EqualConcatenations(SmallVector<StringRef, 2> L,
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000182 PCHPredefinesBlocks R) {
183 // First, sum up the lengths.
184 unsigned LL = 0, RL = 0;
185 for (unsigned I = 0, N = L.size(); I != N; ++I) {
186 LL += L[I].size();
187 }
188 for (unsigned I = 0, N = R.size(); I != N; ++I) {
189 RL += R[I].Data.size();
190 }
191 if (LL != RL)
192 return false;
193 if (LL == 0 && RL == 0)
194 return true;
195
196 // Kick out empty parts, they confuse the algorithm below.
197 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
198 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
199
200 // Do it the hard way. At this point, both vectors must be non-empty.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000201 StringRef LR = L[0], RR = R[0].Data;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000202 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
Daniel Dunbarc76c9e02010-07-16 00:00:11 +0000203 (void) RN;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000204 for (;;) {
205 // Compare the current pieces.
206 if (LR.size() == RR.size()) {
207 // If they're the same length, it's pretty easy.
208 if (LR != RR)
209 return false;
210 // Both pieces are done, advance.
211 ++LI;
212 ++RI;
213 // If either string is done, they're both done, since they're the same
214 // length.
215 if (LI == LN) {
216 assert(RI == RN && "Strings not the same length after all?");
217 return true;
218 }
219 LR = L[LI];
220 RR = R[RI].Data;
221 } else if (LR.size() < RR.size()) {
222 // Right piece is longer.
223 if (!RR.startswith(LR))
224 return false;
225 ++LI;
226 assert(LI != LN && "Strings not the same length after all?");
227 RR = RR.substr(LR.size());
228 LR = L[LI];
229 } else {
230 // Left piece is longer.
231 if (!LR.startswith(RR))
232 return false;
233 ++RI;
234 assert(RI != RN && "Strings not the same length after all?");
235 LR = LR.substr(RR.size());
236 RR = R[RI].Data;
237 }
238 }
239}
240
Chris Lattner5f9e2722011-07-23 10:55:15 +0000241static std::pair<FileID, StringRef::size_type>
242FindMacro(const PCHPredefinesBlocks &Buffers, StringRef MacroDef) {
243 std::pair<FileID, StringRef::size_type> Res;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000244 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
245 Res.second = Buffers[I].Data.find(MacroDef);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000246 if (Res.second != StringRef::npos) {
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000247 Res.first = Buffers[I].BufferID;
248 break;
249 }
250 }
251 return Res;
252}
253
254bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000255 StringRef OriginalFileName,
Nick Lewycky277a6e72011-02-23 21:16:44 +0000256 std::string &SuggestedPredefines,
257 FileManager &FileMgr) {
Daniel Dunbarc7162932009-11-11 23:58:53 +0000258 // We are in the context of an implicit include, so the predefines buffer will
259 // have a #include entry for the PCH file itself (as normalized by the
260 // preprocessor initialization). Find it and skip over it in the checking
261 // below.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000262 llvm::SmallString<256> PCHInclude;
263 PCHInclude += "#include \"";
Nick Lewycky277a6e72011-02-23 21:16:44 +0000264 PCHInclude += NormalizeDashIncludePath(OriginalFileName, FileMgr);
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000265 PCHInclude += "\"\n";
Chris Lattner5f9e2722011-07-23 10:55:15 +0000266 std::pair<StringRef,StringRef> Split =
267 StringRef(PP.getPredefines()).split(PCHInclude.str());
268 StringRef Left = Split.first, Right = Split.second;
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000269 if (Left == PP.getPredefines()) {
270 Error("Missing PCH include entry!");
271 return true;
272 }
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000273
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000274 // If the concatenation of all the PCH buffers is equal to the adjusted
275 // command line, we're done.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000276 SmallVector<StringRef, 2> CommandLine;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000277 CommandLine.push_back(Left);
278 CommandLine.push_back(Right);
279 if (EqualConcatenations(CommandLine, Buffers))
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000280 return false;
281
282 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000283
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000284 // The predefines buffers are different. Determine what the differences are,
285 // and whether they require us to reject the PCH file.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000286 SmallVector<StringRef, 8> PCHLines;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000287 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
288 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Daniel Dunbare6750492009-11-13 16:46:11 +0000289
Chris Lattner5f9e2722011-07-23 10:55:15 +0000290 SmallVector<StringRef, 8> CmdLineLines;
Daniel Dunbare6750492009-11-13 16:46:11 +0000291 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000292
293 // Pick out implicit #includes after the PCH and don't consider them for
294 // validation; we will insert them into SuggestedPredefines so that the
295 // preprocessor includes them.
296 std::string IncludesAfterPCH;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000297 SmallVector<StringRef, 8> AfterPCHLines;
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000298 Right.split(AfterPCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
299 for (unsigned i = 0, e = AfterPCHLines.size(); i != e; ++i) {
300 if (AfterPCHLines[i].startswith("#include ")) {
301 IncludesAfterPCH += AfterPCHLines[i];
302 IncludesAfterPCH += '\n';
303 } else {
304 CmdLineLines.push_back(AfterPCHLines[i]);
305 }
306 }
307
308 // Make sure we add the includes last into SuggestedPredefines before we
309 // exit this function.
310 struct AddIncludesRAII {
311 std::string &SuggestedPredefines;
312 std::string &IncludesAfterPCH;
313
314 AddIncludesRAII(std::string &SuggestedPredefines,
315 std::string &IncludesAfterPCH)
316 : SuggestedPredefines(SuggestedPredefines),
317 IncludesAfterPCH(IncludesAfterPCH) { }
318 ~AddIncludesRAII() {
319 SuggestedPredefines += IncludesAfterPCH;
320 }
321 } AddIncludes(SuggestedPredefines, IncludesAfterPCH);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000322
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000323 // Sort both sets of predefined buffer lines, since we allow some extra
324 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000325 std::sort(CmdLineLines.begin(), CmdLineLines.end());
326 std::sort(PCHLines.begin(), PCHLines.end());
327
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000328 // Determine which predefines that were used to build the PCH file are missing
329 // from the command line.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000330 std::vector<StringRef> MissingPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000331 std::set_difference(PCHLines.begin(), PCHLines.end(),
332 CmdLineLines.begin(), CmdLineLines.end(),
333 std::back_inserter(MissingPredefines));
334
335 bool MissingDefines = false;
336 bool ConflictingDefines = false;
337 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000338 StringRef Missing = MissingPredefines[I];
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000339 if (Missing.startswith("#include ")) {
340 // An -include was specified when generating the PCH; it is included in
341 // the PCH, just ignore it.
342 continue;
343 }
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000344 if (!Missing.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000345 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
346 return true;
347 }
Mike Stump1eb44332009-09-09 15:08:12 +0000348
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000349 // This is a macro definition. Determine the name of the macro we're
350 // defining.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000351 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000352 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000353 = Missing.find_first_of("( \n\r", StartOfMacroName);
354 assert(EndOfMacroName != std::string::npos &&
355 "Couldn't find the end of the macro name");
Chris Lattner5f9e2722011-07-23 10:55:15 +0000356 StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000357
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000358 // Determine whether this macro was given a different definition on the
359 // command line.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000360 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000361 std::string::size_type MacroDefLen = MacroDefStart.size();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000362 SmallVector<StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000363 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
364 MacroDefStart);
365 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000366 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000367 // Different macro; we're done.
368 ConflictPos = CmdLineLines.end();
Mike Stump1eb44332009-09-09 15:08:12 +0000369 break;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000370 }
Mike Stump1eb44332009-09-09 15:08:12 +0000371
372 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000373 "Invalid #define in predefines buffer?");
Mike Stump1eb44332009-09-09 15:08:12 +0000374 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000375 (*ConflictPos)[MacroDefLen] != '(')
376 continue; // Longer macro name; keep trying.
Mike Stump1eb44332009-09-09 15:08:12 +0000377
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000378 // We found a conflicting macro definition.
379 break;
380 }
Mike Stump1eb44332009-09-09 15:08:12 +0000381
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000382 if (ConflictPos != CmdLineLines.end()) {
383 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
384 << MacroName;
385
386 // Show the definition of this macro within the PCH file.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000387 std::pair<FileID, StringRef::size_type> MacroLoc =
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000388 FindMacro(Buffers, Missing);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000389 assert(MacroLoc.second!=StringRef::npos && "Unable to find macro!");
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000390 SourceLocation PCHMissingLoc =
391 SourceMgr.getLocForStartOfFile(MacroLoc.first)
392 .getFileLocWithOffset(MacroLoc.second);
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000393 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000394
395 ConflictingDefines = true;
396 continue;
397 }
Mike Stump1eb44332009-09-09 15:08:12 +0000398
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000399 // If the macro doesn't conflict, then we'll just pick up the macro
400 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000401 if (ConflictingDefines)
402 continue; // Don't complain if there are already conflicting defs
Mike Stump1eb44332009-09-09 15:08:12 +0000403
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000404 if (!MissingDefines) {
405 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
406 MissingDefines = true;
407 }
408
409 // Show the definition of this macro within the PCH file.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000410 std::pair<FileID, StringRef::size_type> MacroLoc =
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000411 FindMacro(Buffers, Missing);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000412 assert(MacroLoc.second!=StringRef::npos && "Unable to find macro!");
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000413 SourceLocation PCHMissingLoc =
414 SourceMgr.getLocForStartOfFile(MacroLoc.first)
415 .getFileLocWithOffset(MacroLoc.second);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000416 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
417 }
Mike Stump1eb44332009-09-09 15:08:12 +0000418
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000419 if (ConflictingDefines)
420 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000421
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000422 // Determine what predefines were introduced based on command-line
423 // parameters that were not present when building the PCH
424 // file. Extra #defines are okay, so long as the identifiers being
425 // defined were not used within the precompiled header.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000426 std::vector<StringRef> ExtraPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000427 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
428 PCHLines.begin(), PCHLines.end(),
Mike Stump1eb44332009-09-09 15:08:12 +0000429 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000430 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000431 StringRef &Extra = ExtraPredefines[I];
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000432 if (!Extra.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000433 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
434 return true;
435 }
436
437 // This is an extra macro definition. Determine the name of the
438 // macro we're defining.
439 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000440 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000441 = Extra.find_first_of("( \n\r", StartOfMacroName);
442 assert(EndOfMacroName != std::string::npos &&
443 "Couldn't find the end of the macro name");
Chris Lattner5f9e2722011-07-23 10:55:15 +0000444 StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000445
446 // Check whether this name was used somewhere in the PCH file. If
447 // so, defining it as a macro could change behavior, so we reject
448 // the PCH file.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000449 if (IdentifierInfo *II = Reader.get(MacroName)) {
Daniel Dunbar4fda42e2009-11-11 00:52:00 +0000450 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000451 return true;
452 }
453
454 // Add this definition to the suggested predefines buffer.
455 SuggestedPredefines += Extra;
456 SuggestedPredefines += '\n';
457 }
458
459 // If we get here, it's because the predefines buffer had compatible
460 // contents. Accept the PCH file.
461 return false;
462}
463
Douglas Gregor12fab312010-03-16 16:35:32 +0000464void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
465 unsigned ID) {
466 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
467 ++NumHeaderInfos;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000468}
469
470void PCHValidator::ReadCounter(unsigned Value) {
471 PP.setCounterValue(Value);
472}
473
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000474//===----------------------------------------------------------------------===//
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000475// AST reader implementation
Douglas Gregor668c1a42009-04-21 22:25:48 +0000476//===----------------------------------------------------------------------===//
477
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000478void
Sebastian Redl571db7f2010-08-18 23:56:56 +0000479ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000480 DeserializationListener = Listener;
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000481}
482
Chris Lattner4c6f9522009-04-27 05:14:47 +0000483
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000484
Douglas Gregor98339b92011-08-25 20:47:51 +0000485unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
486 return serialization::ComputeHash(Sel);
487}
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000488
Mike Stump1eb44332009-09-09 15:08:12 +0000489
Douglas Gregor98339b92011-08-25 20:47:51 +0000490std::pair<unsigned, unsigned>
491ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
492 using namespace clang::io;
493 unsigned KeyLen = ReadUnalignedLE16(d);
494 unsigned DataLen = ReadUnalignedLE16(d);
495 return std::make_pair(KeyLen, DataLen);
496}
497
498ASTSelectorLookupTrait::internal_key_type
499ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
500 using namespace clang::io;
501 SelectorTable &SelTable = Reader.getContext()->Selectors;
502 unsigned N = ReadUnalignedLE16(d);
503 IdentifierInfo *FirstII
504 = Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
505 if (N == 0)
506 return SelTable.getNullarySelector(FirstII);
507 else if (N == 1)
508 return SelTable.getUnarySelector(FirstII);
509
510 SmallVector<IdentifierInfo *, 16> Args;
511 Args.push_back(FirstII);
512 for (unsigned I = 1; I != N; ++I)
513 Args.push_back(Reader.getLocalIdentifier(F, ReadUnalignedLE32(d)));
514
515 return SelTable.getSelector(N, Args.data());
516}
517
518ASTSelectorLookupTrait::data_type
519ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
520 unsigned DataLen) {
521 using namespace clang::io;
522
523 data_type Result;
524
525 Result.ID = Reader.getGlobalSelectorID(F, ReadUnalignedLE32(d));
526 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
527 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
528
529 // Load instance methods
Douglas Gregor98339b92011-08-25 20:47:51 +0000530 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
531 if (ObjCMethodDecl *Method
532 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
533 Result.Instance.push_back(Method);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000534 }
Mike Stump1eb44332009-09-09 15:08:12 +0000535
Douglas Gregor98339b92011-08-25 20:47:51 +0000536 // Load factory methods
Douglas Gregor98339b92011-08-25 20:47:51 +0000537 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
538 if (ObjCMethodDecl *Method
539 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
540 Result.Factory.push_back(Method);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000541 }
Mike Stump1eb44332009-09-09 15:08:12 +0000542
Douglas Gregor98339b92011-08-25 20:47:51 +0000543 return Result;
544}
Mike Stump1eb44332009-09-09 15:08:12 +0000545
Douglas Gregor98339b92011-08-25 20:47:51 +0000546unsigned ASTIdentifierLookupTrait::ComputeHash(const internal_key_type& a) {
547 return llvm::HashString(StringRef(a.first, a.second));
548}
Mike Stump1eb44332009-09-09 15:08:12 +0000549
Douglas Gregor98339b92011-08-25 20:47:51 +0000550std::pair<unsigned, unsigned>
551ASTIdentifierLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
552 using namespace clang::io;
553 unsigned DataLen = ReadUnalignedLE16(d);
554 unsigned KeyLen = ReadUnalignedLE16(d);
555 return std::make_pair(KeyLen, DataLen);
556}
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000557
Douglas Gregor98339b92011-08-25 20:47:51 +0000558std::pair<const char*, unsigned>
559ASTIdentifierLookupTrait::ReadKey(const unsigned char* d, unsigned n) {
560 assert(n >= 2 && d[n-1] == '\0');
561 return std::make_pair((const char*) d, n-1);
562}
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000563
Douglas Gregor98339b92011-08-25 20:47:51 +0000564IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
565 const unsigned char* d,
566 unsigned DataLen) {
567 using namespace clang::io;
568 unsigned RawID = ReadUnalignedLE32(d);
569 bool IsInteresting = RawID & 0x01;
Mike Stump1eb44332009-09-09 15:08:12 +0000570
Douglas Gregor98339b92011-08-25 20:47:51 +0000571 // Wipe out the "is interesting" bit.
572 RawID = RawID >> 1;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000573
Douglas Gregor98339b92011-08-25 20:47:51 +0000574 IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
575 if (!IsInteresting) {
576 // For uninteresting identifiers, just build the IdentifierInfo
577 // and associate it with the persistent ID.
Douglas Gregor668c1a42009-04-21 22:25:48 +0000578 IdentifierInfo *II = KnownII;
579 if (!II)
Douglas Gregor6ec60e02011-08-03 21:49:18 +0000580 II = &Reader.getIdentifierTable().getOwn(StringRef(k.first, k.second));
Douglas Gregor668c1a42009-04-21 22:25:48 +0000581 Reader.SetIdentifierInfo(ID, II);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000582 II->setIsFromAST();
Douglas Gregor668c1a42009-04-21 22:25:48 +0000583 return II;
584 }
Mike Stump1eb44332009-09-09 15:08:12 +0000585
Douglas Gregor98339b92011-08-25 20:47:51 +0000586 unsigned Bits = ReadUnalignedLE16(d);
587 bool CPlusPlusOperatorKeyword = Bits & 0x01;
588 Bits >>= 1;
589 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
590 Bits >>= 1;
591 bool Poisoned = Bits & 0x01;
592 Bits >>= 1;
593 bool ExtensionToken = Bits & 0x01;
594 Bits >>= 1;
595 bool hasMacroDefinition = Bits & 0x01;
596 Bits >>= 1;
597 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
598 Bits >>= 10;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000599
Douglas Gregor98339b92011-08-25 20:47:51 +0000600 assert(Bits == 0 && "Extra bits in the identifier?");
601 DataLen -= 6;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000602
Douglas Gregor98339b92011-08-25 20:47:51 +0000603 // Build the IdentifierInfo itself and link the identifier ID with
604 // the new IdentifierInfo.
605 IdentifierInfo *II = KnownII;
606 if (!II)
607 II = &Reader.getIdentifierTable().getOwn(StringRef(k.first, k.second));
608 Reader.SetIdentifierInfo(ID, II);
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000609
Douglas Gregor98339b92011-08-25 20:47:51 +0000610 // Set or check the various bits in the IdentifierInfo structure.
611 // Token IDs are read-only.
612 if (HasRevertedTokenIDToIdentifier)
613 II->RevertTokenIDToIdentifier();
614 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
615 assert(II->isExtensionToken() == ExtensionToken &&
616 "Incorrect extension token flag");
617 (void)ExtensionToken;
618 if (Poisoned)
619 II->setIsPoisoned(true);
620 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
621 "Incorrect C++ operator keyword flag");
622 (void)CPlusPlusOperatorKeyword;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000623
Douglas Gregor98339b92011-08-25 20:47:51 +0000624 // If this identifier is a macro, deserialize the macro
625 // definition.
626 if (hasMacroDefinition) {
627 // FIXME: Check for conflicts?
628 uint32_t Offset = ReadUnalignedLE32(d);
629 Reader.SetIdentifierIsMacro(II, F, Offset);
630 DataLen -= 4;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000631 }
632
Douglas Gregor98339b92011-08-25 20:47:51 +0000633 // Read all of the declarations visible at global scope with this
634 // name.
635 if (Reader.getContext() == 0) return II;
636 if (DataLen > 0) {
637 SmallVector<uint32_t, 4> DeclIDs;
638 for (; DataLen > 0; DataLen -= 4)
639 DeclIDs.push_back(Reader.getGlobalDeclID(F, ReadUnalignedLE32(d)));
640 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000641 }
642
Douglas Gregor98339b92011-08-25 20:47:51 +0000643 II->setIsFromAST();
644 return II;
645}
Michael J. Spencer20249a12010-10-21 03:16:25 +0000646
Douglas Gregor98339b92011-08-25 20:47:51 +0000647unsigned
648ASTDeclContextNameLookupTrait::ComputeHash(const DeclNameKey &Key) const {
649 llvm::FoldingSetNodeID ID;
650 ID.AddInteger(Key.Kind);
651
652 switch (Key.Kind) {
653 case DeclarationName::Identifier:
654 case DeclarationName::CXXLiteralOperatorName:
655 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
656 break;
657 case DeclarationName::ObjCZeroArgSelector:
658 case DeclarationName::ObjCOneArgSelector:
659 case DeclarationName::ObjCMultiArgSelector:
660 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
661 break;
662 case DeclarationName::CXXOperatorName:
663 ID.AddInteger((OverloadedOperatorKind)Key.Data);
664 break;
665 case DeclarationName::CXXConstructorName:
666 case DeclarationName::CXXDestructorName:
667 case DeclarationName::CXXConversionFunctionName:
668 case DeclarationName::CXXUsingDirective:
669 break;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000670 }
671
Douglas Gregor98339b92011-08-25 20:47:51 +0000672 return ID.ComputeHash();
673}
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000674
Douglas Gregor98339b92011-08-25 20:47:51 +0000675ASTDeclContextNameLookupTrait::internal_key_type
676ASTDeclContextNameLookupTrait::GetInternalKey(
677 const external_key_type& Name) const {
678 DeclNameKey Key;
679 Key.Kind = Name.getNameKind();
680 switch (Name.getNameKind()) {
681 case DeclarationName::Identifier:
682 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
683 break;
684 case DeclarationName::ObjCZeroArgSelector:
685 case DeclarationName::ObjCOneArgSelector:
686 case DeclarationName::ObjCMultiArgSelector:
687 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
688 break;
689 case DeclarationName::CXXOperatorName:
690 Key.Data = Name.getCXXOverloadedOperator();
691 break;
692 case DeclarationName::CXXLiteralOperatorName:
693 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
694 break;
695 case DeclarationName::CXXConstructorName:
696 case DeclarationName::CXXDestructorName:
697 case DeclarationName::CXXConversionFunctionName:
698 case DeclarationName::CXXUsingDirective:
699 Key.Data = 0;
700 break;
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000701 }
702
Douglas Gregor98339b92011-08-25 20:47:51 +0000703 return Key;
704}
705
706ASTDeclContextNameLookupTrait::external_key_type
707ASTDeclContextNameLookupTrait::GetExternalKey(
708 const internal_key_type& Key) const {
709 ASTContext *Context = Reader.getContext();
710 switch (Key.Kind) {
711 case DeclarationName::Identifier:
712 return DeclarationName((IdentifierInfo*)Key.Data);
713
714 case DeclarationName::ObjCZeroArgSelector:
715 case DeclarationName::ObjCOneArgSelector:
716 case DeclarationName::ObjCMultiArgSelector:
717 return DeclarationName(Selector(Key.Data));
718
719 case DeclarationName::CXXConstructorName:
720 return Context->DeclarationNames.getCXXConstructorName(
721 Context->getCanonicalType(Reader.getLocalType(F, Key.Data)));
722
723 case DeclarationName::CXXDestructorName:
724 return Context->DeclarationNames.getCXXDestructorName(
725 Context->getCanonicalType(Reader.getLocalType(F, Key.Data)));
726
727 case DeclarationName::CXXConversionFunctionName:
728 return Context->DeclarationNames.getCXXConversionFunctionName(
729 Context->getCanonicalType(Reader.getLocalType(F, Key.Data)));
730
731 case DeclarationName::CXXOperatorName:
732 return Context->DeclarationNames.getCXXOperatorName(
733 (OverloadedOperatorKind)Key.Data);
734
735 case DeclarationName::CXXLiteralOperatorName:
736 return Context->DeclarationNames.getCXXLiteralOperatorName(
737 (IdentifierInfo*)Key.Data);
738
739 case DeclarationName::CXXUsingDirective:
740 return DeclarationName::getUsingDirectiveName();
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000741 }
742
Douglas Gregor98339b92011-08-25 20:47:51 +0000743 llvm_unreachable("Invalid Name Kind ?");
744}
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000745
Douglas Gregor98339b92011-08-25 20:47:51 +0000746std::pair<unsigned, unsigned>
747ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
748 using namespace clang::io;
749 unsigned KeyLen = ReadUnalignedLE16(d);
750 unsigned DataLen = ReadUnalignedLE16(d);
751 return std::make_pair(KeyLen, DataLen);
752}
Michael J. Spencer20249a12010-10-21 03:16:25 +0000753
Douglas Gregor98339b92011-08-25 20:47:51 +0000754ASTDeclContextNameLookupTrait::internal_key_type
755ASTDeclContextNameLookupTrait::ReadKey(const unsigned char* d, unsigned) {
756 using namespace clang::io;
757
758 DeclNameKey Key;
759 Key.Kind = (DeclarationName::NameKind)*d++;
760 switch (Key.Kind) {
761 case DeclarationName::Identifier:
762 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
763 break;
764 case DeclarationName::ObjCZeroArgSelector:
765 case DeclarationName::ObjCOneArgSelector:
766 case DeclarationName::ObjCMultiArgSelector:
767 Key.Data =
768 (uint64_t)Reader.getLocalSelector(F, ReadUnalignedLE32(d))
769 .getAsOpaquePtr();
770 break;
771 case DeclarationName::CXXOperatorName:
772 Key.Data = *d++; // OverloadedOperatorKind
773 break;
774 case DeclarationName::CXXLiteralOperatorName:
775 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
776 break;
777 case DeclarationName::CXXConstructorName:
778 case DeclarationName::CXXDestructorName:
779 case DeclarationName::CXXConversionFunctionName:
780 case DeclarationName::CXXUsingDirective:
781 Key.Data = 0;
782 break;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000783 }
784
Douglas Gregor98339b92011-08-25 20:47:51 +0000785 return Key;
786}
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000787
Douglas Gregor98339b92011-08-25 20:47:51 +0000788ASTDeclContextNameLookupTrait::data_type
789ASTDeclContextNameLookupTrait::ReadData(internal_key_type,
790 const unsigned char* d,
791 unsigned DataLen) {
792 using namespace clang::io;
793 unsigned NumDecls = ReadUnalignedLE16(d);
794 DeclID *Start = (DeclID *)d;
795 return std::make_pair(Start, Start + NumDecls);
796}
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000797
Douglas Gregor0d95f772011-08-24 19:03:07 +0000798bool ASTReader::ReadDeclContextStorage(Module &M,
799 llvm::BitstreamCursor &Cursor,
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000800 const std::pair<uint64_t, uint64_t> &Offsets,
801 DeclContextInfo &Info) {
802 SavedStreamPosition SavedPosition(Cursor);
803 // First the lexical decls.
804 if (Offsets.first != 0) {
805 Cursor.JumpToBit(Offsets.first);
806
807 RecordData Record;
808 const char *Blob;
809 unsigned BlobLen;
810 unsigned Code = Cursor.ReadCode();
811 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
812 if (RecCode != DECL_CONTEXT_LEXICAL) {
813 Error("Expected lexical block");
814 return true;
815 }
816
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000817 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
818 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000819 }
820
821 // Now the lookup table.
822 if (Offsets.second != 0) {
823 Cursor.JumpToBit(Offsets.second);
824
825 RecordData Record;
826 const char *Blob;
827 unsigned BlobLen;
828 unsigned Code = Cursor.ReadCode();
829 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
830 if (RecCode != DECL_CONTEXT_VISIBLE) {
831 Error("Expected visible lookup table block");
832 return true;
833 }
834 Info.NameLookupTableData
835 = ASTDeclContextNameLookupTable::Create(
836 (const unsigned char *)Blob + Record[0],
837 (const unsigned char *)Blob,
Douglas Gregor0d95f772011-08-24 19:03:07 +0000838 ASTDeclContextNameLookupTrait(*this, M));
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000839 }
840
841 return false;
842}
843
Chris Lattner5f9e2722011-07-23 10:55:15 +0000844void ASTReader::Error(StringRef Msg) {
Argyrios Kyrtzidis8d8f2c22011-04-25 22:23:56 +0000845 Error(diag::err_fe_pch_malformed, Msg);
846}
847
848void ASTReader::Error(unsigned DiagID,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000849 StringRef Arg1, StringRef Arg2) {
Argyrios Kyrtzidis8d8f2c22011-04-25 22:23:56 +0000850 if (Diags.isDiagnosticInFlight())
851 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
852 else
853 Diag(DiagID) << Arg1 << Arg2;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000854}
855
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000856/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000857bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000858 if (Listener)
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000859 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000860 ActualOriginalFileName,
Nick Lewycky277a6e72011-02-23 21:16:44 +0000861 SuggestedPredefines,
862 FileMgr);
Douglas Gregore721f952009-04-28 18:58:38 +0000863 return false;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000864}
865
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000866//===----------------------------------------------------------------------===//
867// Source Manager Deserialization
868//===----------------------------------------------------------------------===//
869
Douglas Gregorbd945002009-04-13 16:31:14 +0000870/// \brief Read the line table in the source manager block.
Sebastian Redlc3632732010-10-05 15:59:54 +0000871/// \returns true if there was an error.
Douglas Gregor72a9ae12011-07-22 16:00:58 +0000872bool ASTReader::ParseLineTable(Module &F,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000873 SmallVectorImpl<uint64_t> &Record) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000874 unsigned Idx = 0;
875 LineTableInfo &LineTable = SourceMgr.getLineTable();
876
877 // Parse the file names
Douglas Gregorff0a9872009-04-13 17:12:42 +0000878 std::map<int, int> FileIDs;
879 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000880 // Extract the file name
881 unsigned FilenameLen = Record[Idx++];
882 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
883 Idx += FilenameLen;
Douglas Gregore650c8c2009-07-07 00:12:59 +0000884 MaybeAddSystemRootToFilename(Filename);
Jay Foad65aa6882011-06-21 15:13:30 +0000885 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
Douglas Gregorbd945002009-04-13 16:31:14 +0000886 }
887
888 // Parse the line entries
889 std::vector<LineEntry> Entries;
890 while (Idx < Record.size()) {
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000891 int FID = Record[Idx++];
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000892 assert(FID >= 0 && "Serialized line entries for non-local file.");
893 // Remap FileID from 1-based old view.
894 FID += F.SLocEntryBaseID - 1;
Douglas Gregorbd945002009-04-13 16:31:14 +0000895
896 // Extract the line entries
897 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000898 assert(NumEntries && "Numentries is 00000");
Douglas Gregorbd945002009-04-13 16:31:14 +0000899 Entries.clear();
900 Entries.reserve(NumEntries);
901 for (unsigned I = 0; I != NumEntries; ++I) {
902 unsigned FileOffset = Record[Idx++];
903 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000904 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump1eb44332009-09-09 15:08:12 +0000905 SrcMgr::CharacteristicKind FileKind
Douglas Gregorbd945002009-04-13 16:31:14 +0000906 = (SrcMgr::CharacteristicKind)Record[Idx++];
907 unsigned IncludeOffset = Record[Idx++];
908 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
909 FileKind, IncludeOffset));
910 }
911 LineTable.AddEntry(FID, Entries);
912 }
913
914 return false;
915}
916
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000917namespace {
918
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000919class ASTStatData {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000920public:
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000921 const ino_t ino;
922 const dev_t dev;
923 const mode_t mode;
924 const time_t mtime;
925 const off_t size;
Mike Stump1eb44332009-09-09 15:08:12 +0000926
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000927 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Chris Lattner74e976b2010-11-23 19:28:12 +0000928 : ino(i), dev(d), mode(mo), mtime(m), size(s) {}
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000929};
930
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000931class ASTStatLookupTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000932 public:
933 typedef const char *external_key_type;
934 typedef const char *internal_key_type;
935
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000936 typedef ASTStatData data_type;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000937
938 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000939 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000940 }
941
942 static internal_key_type GetInternalKey(const char *path) { return path; }
943
944 static bool EqualKey(internal_key_type a, internal_key_type b) {
945 return strcmp(a, b) == 0;
946 }
947
948 static std::pair<unsigned, unsigned>
949 ReadKeyDataLength(const unsigned char*& d) {
950 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
951 unsigned DataLen = (unsigned) *d++;
952 return std::make_pair(KeyLen + 1, DataLen);
953 }
954
955 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
956 return (const char *)d;
957 }
958
959 static data_type ReadData(const internal_key_type, const unsigned char *d,
960 unsigned /*DataLen*/) {
961 using namespace clang::io;
962
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000963 ino_t ino = (ino_t) ReadUnalignedLE32(d);
964 dev_t dev = (dev_t) ReadUnalignedLE32(d);
965 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +0000966 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000967 off_t size = (off_t) ReadUnalignedLE64(d);
968 return data_type(ino, dev, mode, mtime, size);
969 }
970};
971
972/// \brief stat() cache for precompiled headers.
973///
974/// This cache is very similar to the stat cache used by pretokenized
975/// headers.
Chris Lattner10e286a2010-11-23 19:19:34 +0000976class ASTStatCache : public FileSystemStatCache {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000977 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000978 CacheTy *Cache;
979
980 unsigned &NumStatHits, &NumStatMisses;
Mike Stump1eb44332009-09-09 15:08:12 +0000981public:
Chris Lattner74e976b2010-11-23 19:28:12 +0000982 ASTStatCache(const unsigned char *Buckets, const unsigned char *Base,
983 unsigned &NumStatHits, unsigned &NumStatMisses)
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000984 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
985 Cache = CacheTy::Create(Buckets, Base);
986 }
987
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000988 ~ASTStatCache() { delete Cache; }
Mike Stump1eb44332009-09-09 15:08:12 +0000989
Chris Lattner898a0612010-11-23 21:17:56 +0000990 LookupResult getStat(const char *Path, struct stat &StatBuf,
991 int *FileDescriptor) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000992 // Do the lookup for the file's data in the AST file.
Chris Lattner10e286a2010-11-23 19:19:34 +0000993 CacheTy::iterator I = Cache->find(Path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000994
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000995 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000996 if (I == Cache->end()) {
997 ++NumStatMisses;
Chris Lattner898a0612010-11-23 21:17:56 +0000998 return statChained(Path, StatBuf, FileDescriptor);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000999 }
Mike Stump1eb44332009-09-09 15:08:12 +00001000
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001001 ++NumStatHits;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001002 ASTStatData Data = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001003
Chris Lattner10e286a2010-11-23 19:19:34 +00001004 StatBuf.st_ino = Data.ino;
1005 StatBuf.st_dev = Data.dev;
1006 StatBuf.st_mtime = Data.mtime;
1007 StatBuf.st_mode = Data.mode;
1008 StatBuf.st_size = Data.size;
Chris Lattnerd6f61112010-11-23 20:05:15 +00001009 return CacheExists;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001010 }
1011};
1012} // end anonymous namespace
1013
1014
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001015/// \brief Read a source manager block
Douglas Gregor72a9ae12011-07-22 16:00:58 +00001016ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(Module &F) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001017 using namespace SrcMgr;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001018
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001019 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001020
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001021 // Set the source-location entry cursor to the current position in
1022 // the stream. This cursor will be used to read the contents of the
1023 // source manager block initially, and then lazily read
1024 // source-location entries as needed.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001025 SLocEntryCursor = F.Stream;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001026
1027 // The stream itself is going to skip over the source manager block.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001028 if (F.Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001029 Error("malformed block record in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001030 return Failure;
1031 }
1032
1033 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001034 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001035 Error("malformed source manager block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001036 return Failure;
1037 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001038
Douglas Gregor14f79002009-04-10 03:52:48 +00001039 RecordData Record;
1040 while (true) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001041 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregor14f79002009-04-10 03:52:48 +00001042 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001043 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001044 Error("error at end of Source Manager block in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001045 return Failure;
1046 }
Douglas Gregore1d918e2009-04-10 23:10:45 +00001047 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +00001048 }
Mike Stump1eb44332009-09-09 15:08:12 +00001049
Douglas Gregor14f79002009-04-10 03:52:48 +00001050 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1051 // No known subblocks, always skip them.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001052 SLocEntryCursor.ReadSubBlockID();
1053 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001054 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001055 return Failure;
1056 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001057 continue;
1058 }
Mike Stump1eb44332009-09-09 15:08:12 +00001059
Douglas Gregor14f79002009-04-10 03:52:48 +00001060 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001061 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregor14f79002009-04-10 03:52:48 +00001062 continue;
1063 }
Mike Stump1eb44332009-09-09 15:08:12 +00001064
Douglas Gregor14f79002009-04-10 03:52:48 +00001065 // Read a record.
1066 const char *BlobStart;
1067 unsigned BlobLen;
1068 Record.clear();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001069 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001070 default: // Default behavior: ignore.
1071 break;
1072
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001073 case SM_SLOC_FILE_ENTRY:
1074 case SM_SLOC_BUFFER_ENTRY:
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001075 case SM_SLOC_EXPANSION_ENTRY:
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001076 // Once we hit one of the source location entries, we're done.
1077 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +00001078 }
1079 }
1080}
1081
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001082/// \brief If a header file is not found at the path that we expect it to be
1083/// and the PCH file was moved from its original location, try to resolve the
1084/// file by assuming that header+PCH were moved together and the header is in
1085/// the same place relative to the PCH.
1086static std::string
1087resolveFileRelativeToOriginalDir(const std::string &Filename,
1088 const std::string &OriginalDir,
1089 const std::string &CurrDir) {
1090 assert(OriginalDir != CurrDir &&
1091 "No point trying to resolve the file if the PCH dir didn't change");
1092 using namespace llvm::sys;
1093 llvm::SmallString<128> filePath(Filename);
1094 fs::make_absolute(filePath);
1095 assert(path::is_absolute(OriginalDir));
1096 llvm::SmallString<128> currPCHPath(CurrDir);
1097
1098 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1099 fileDirE = path::end(path::parent_path(filePath));
1100 path::const_iterator origDirI = path::begin(OriginalDir),
1101 origDirE = path::end(OriginalDir);
1102 // Skip the common path components from filePath and OriginalDir.
1103 while (fileDirI != fileDirE && origDirI != origDirE &&
1104 *fileDirI == *origDirI) {
1105 ++fileDirI;
1106 ++origDirI;
1107 }
1108 for (; origDirI != origDirE; ++origDirI)
1109 path::append(currPCHPath, "..");
1110 path::append(currPCHPath, fileDirI, fileDirE);
1111 path::append(currPCHPath, path::filename(Filename));
1112 return currPCHPath.str();
1113}
1114
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001115/// \brief Read in the source location entry with the given ID.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001116ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(int ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001117 if (ID == 0)
1118 return Success;
1119
Douglas Gregor0cdd7982011-07-21 18:46:38 +00001120 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001121 Error("source location entry ID out-of-range for AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001122 return Failure;
1123 }
1124
Douglas Gregor72a9ae12011-07-22 16:00:58 +00001125 Module *F = GlobalSLocEntryMap.find(-ID)->second;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001126 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
Sebastian Redlc3632732010-10-05 15:59:54 +00001127 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001128 unsigned BaseOffset = F->SLocEntryBaseOffset;
Sebastian Redl9137a522010-07-16 17:50:48 +00001129
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001130 ++NumSLocEntriesRead;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001131 unsigned Code = SLocEntryCursor.ReadCode();
1132 if (Code == llvm::bitc::END_BLOCK ||
1133 Code == llvm::bitc::ENTER_SUBBLOCK ||
1134 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001135 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001136 return Failure;
1137 }
1138
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001139 RecordData Record;
1140 const char *BlobStart;
1141 unsigned BlobLen;
1142 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1143 default:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001144 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001145 return Failure;
1146
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001147 case SM_SLOC_FILE_ENTRY: {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001148 std::string Filename(BlobStart, BlobStart + BlobLen);
1149 MaybeAddSystemRootToFilename(Filename);
Chris Lattner39b49bc2010-11-23 08:35:12 +00001150 const FileEntry *File = FileMgr.getFile(Filename);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001151 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1152 OriginalDir != CurrentDir) {
1153 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1154 OriginalDir,
1155 CurrentDir);
1156 if (!resolved.empty())
1157 File = FileMgr.getFile(resolved);
1158 }
Axel Naumann04331162011-01-27 10:55:51 +00001159 if (File == 0)
1160 File = FileMgr.getVirtualFile(Filename, (off_t)Record[4],
1161 (time_t)Record[5]);
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001162 if (File == 0) {
1163 std::string ErrorStr = "could not find file '";
Douglas Gregore650c8c2009-07-07 00:12:59 +00001164 ErrorStr += Filename;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001165 ErrorStr += "' referenced by AST file";
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001166 Error(ErrorStr.c_str());
1167 return Failure;
1168 }
Mike Stump1eb44332009-09-09 15:08:12 +00001169
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001170 if (Record.size() < 6) {
Ted Kremenek1857f622010-03-18 21:23:05 +00001171 Error("source location entry is incorrect");
1172 return Failure;
1173 }
1174
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001175 if (!DisableValidation &&
1176 ((off_t)Record[4] != File->getSize()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001177#if !defined(LLVM_ON_WIN32)
1178 // In our regression testing, the Windows file system seems to
1179 // have inconsistent modification times that sometimes
1180 // erroneously trigger this error-handling path.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001181 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001182#endif
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001183 )) {
Argyrios Kyrtzidis8d8f2c22011-04-25 22:23:56 +00001184 Error(diag::err_fe_pch_file_modified, Filename);
Douglas Gregor2d52be52010-03-21 22:49:54 +00001185 return Failure;
1186 }
1187
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001188 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregor72a9ae12011-07-22 16:00:58 +00001189 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001190 // This is the module's main file.
1191 IncludeLoc = getImportLocation(F);
1192 }
1193 FileID FID = SourceMgr.createFileID(File, IncludeLoc,
Chris Lattner75dfb652010-11-23 09:19:42 +00001194 (SrcMgr::CharacteristicKind)Record[2],
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001195 ID, BaseOffset + Record[0]);
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001196 SrcMgr::FileInfo &FileInfo =
1197 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1198 FileInfo.NumCreatedFIDs = Record[6];
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001199 if (Record[3])
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001200 FileInfo.setHasLineDirectives();
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001201
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001202 break;
1203 }
1204
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001205 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001206 const char *Name = BlobStart;
1207 unsigned Offset = Record[0];
1208 unsigned Code = SLocEntryCursor.ReadCode();
1209 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001210 unsigned RecCode
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001211 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001212
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001213 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001214 Error("AST record has invalid code");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001215 return Failure;
1216 }
1217
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001218 llvm::MemoryBuffer *Buffer
Chris Lattner5f9e2722011-07-23 10:55:15 +00001219 = llvm::MemoryBuffer::getMemBuffer(StringRef(BlobStart, BlobLen - 1),
Chris Lattnera0a270c2010-04-05 22:42:27 +00001220 Name);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001221 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID,
1222 BaseOffset + Offset);
Mike Stump1eb44332009-09-09 15:08:12 +00001223
Douglas Gregor92b059e2009-04-28 20:33:11 +00001224 if (strcmp(Name, "<built-in>") == 0) {
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +00001225 PCHPredefinesBlock Block = {
1226 BufferID,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001227 StringRef(BlobStart, BlobLen - 1)
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +00001228 };
1229 PCHPredefinesBuffers.push_back(Block);
Douglas Gregor92b059e2009-04-28 20:33:11 +00001230 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001231
1232 break;
1233 }
1234
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001235 case SM_SLOC_EXPANSION_ENTRY: {
Sebastian Redlc3632732010-10-05 15:59:54 +00001236 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Chandler Carruthbf340e42011-07-26 03:03:05 +00001237 SourceMgr.createExpansionLoc(SpellingLoc,
Sebastian Redlc3632732010-10-05 15:59:54 +00001238 ReadSourceLocation(*F, Record[2]),
1239 ReadSourceLocation(*F, Record[3]),
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001240 Record[4],
1241 ID,
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001242 BaseOffset + Record[0]);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001243 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001244 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001245 }
1246
1247 return Success;
1248}
1249
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001250/// \brief Find the location where the module F is imported.
Douglas Gregor72a9ae12011-07-22 16:00:58 +00001251SourceLocation ASTReader::getImportLocation(Module *F) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001252 if (F->ImportLoc.isValid())
1253 return F->ImportLoc;
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001254
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001255 // Otherwise we have a PCH. It's considered to be "imported" at the first
1256 // location of its includer.
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001257 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001258 // Main file is the importer. We assume that it is the first entry in the
1259 // entry table. We can't ask the manager, because at the time of PCH loading
1260 // the main file entry doesn't exist yet.
1261 // The very first entry is the invalid instantiation loc, which takes up
1262 // offsets 0 and 1.
1263 return SourceLocation::getFromRawEncoding(2U);
1264 }
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001265 //return F->Loaders[0]->FirstLoc;
1266 return F->ImportedBy[0]->FirstLoc;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001267}
1268
Chris Lattner6367f6d2009-04-27 01:05:14 +00001269/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1270/// specified cursor. Read the abbreviations that are at the top of the block
1271/// and then leave the cursor pointing into the block.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001272bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattner6367f6d2009-04-27 01:05:14 +00001273 unsigned BlockID) {
1274 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001275 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001276 return Failure;
1277 }
Mike Stump1eb44332009-09-09 15:08:12 +00001278
Chris Lattner6367f6d2009-04-27 01:05:14 +00001279 while (true) {
Douglas Gregorecdcb882010-10-20 22:00:55 +00001280 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattner6367f6d2009-04-27 01:05:14 +00001281 unsigned Code = Cursor.ReadCode();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001282
Chris Lattner6367f6d2009-04-27 01:05:14 +00001283 // We expect all abbrevs to be at the start of the block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001284 if (Code != llvm::bitc::DEFINE_ABBREV) {
1285 Cursor.JumpToBit(Offset);
Chris Lattner6367f6d2009-04-27 01:05:14 +00001286 return false;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001287 }
Chris Lattner6367f6d2009-04-27 01:05:14 +00001288 Cursor.ReadAbbrevRecord();
1289 }
1290}
1291
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001292void ASTReader::ReadMacroRecord(Module &F, uint64_t Offset) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001293 assert(PP && "Forgot to set Preprocessor ?");
Douglas Gregorecdcb882010-10-20 22:00:55 +00001294 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump1eb44332009-09-09 15:08:12 +00001295
Douglas Gregor37e26842009-04-21 23:56:24 +00001296 // Keep track of where we are in the stream, then jump back there
1297 // after reading this macro.
1298 SavedStreamPosition SavedPosition(Stream);
1299
1300 Stream.JumpToBit(Offset);
1301 RecordData Record;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001302 SmallVector<IdentifierInfo*, 16> MacroArgs;
Douglas Gregor37e26842009-04-21 23:56:24 +00001303 MacroInfo *Macro = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001304
Douglas Gregor37e26842009-04-21 23:56:24 +00001305 while (true) {
1306 unsigned Code = Stream.ReadCode();
1307 switch (Code) {
1308 case llvm::bitc::END_BLOCK:
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001309 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001310
1311 case llvm::bitc::ENTER_SUBBLOCK:
1312 // No known subblocks, always skip them.
1313 Stream.ReadSubBlockID();
1314 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001315 Error("malformed block record in AST file");
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001316 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001317 }
1318 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001319
Douglas Gregor37e26842009-04-21 23:56:24 +00001320 case llvm::bitc::DEFINE_ABBREV:
1321 Stream.ReadAbbrevRecord();
1322 continue;
1323 default: break;
1324 }
1325
1326 // Read a record.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001327 const char *BlobStart = 0;
1328 unsigned BlobLen = 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001329 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001330 PreprocessorRecordTypes RecType =
Michael J. Spencer20249a12010-10-21 03:16:25 +00001331 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregorecdcb882010-10-20 22:00:55 +00001332 BlobLen);
Douglas Gregor37e26842009-04-21 23:56:24 +00001333 switch (RecType) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001334 case PP_MACRO_OBJECT_LIKE:
1335 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001336 // If we already have a macro, that means that we've hit the end
1337 // of the definition of the macro we were looking for. We're
1338 // done.
1339 if (Macro)
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001340 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001341
Douglas Gregor95eab172011-07-28 20:55:49 +00001342 IdentifierInfo *II = getLocalIdentifier(F, Record[0]);
Douglas Gregor37e26842009-04-21 23:56:24 +00001343 if (II == 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001344 Error("macro must have a name in AST file");
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001345 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001346 }
Sebastian Redlc3632732010-10-05 15:59:54 +00001347 SourceLocation Loc = ReadSourceLocation(F, Record[1]);
Douglas Gregor37e26842009-04-21 23:56:24 +00001348 bool isUsed = Record[2];
Mike Stump1eb44332009-09-09 15:08:12 +00001349
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001350 MacroInfo *MI = PP->AllocateMacroInfo(Loc);
Douglas Gregor37e26842009-04-21 23:56:24 +00001351 MI->setIsUsed(isUsed);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001352 MI->setIsFromAST();
Mike Stump1eb44332009-09-09 15:08:12 +00001353
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001354 unsigned NextIndex = 3;
Douglas Gregor7143aab2011-09-01 17:04:32 +00001355 MI->setExportLocation(ReadSourceLocation(F, Record, NextIndex));
1356
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001357 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregor37e26842009-04-21 23:56:24 +00001358 // Decode function-like macro info.
Douglas Gregor7143aab2011-09-01 17:04:32 +00001359 bool isC99VarArgs = Record[NextIndex++];
1360 bool isGNUVarArgs = Record[NextIndex++];
Douglas Gregor37e26842009-04-21 23:56:24 +00001361 MacroArgs.clear();
Douglas Gregor7143aab2011-09-01 17:04:32 +00001362 unsigned NumArgs = Record[NextIndex++];
Douglas Gregor37e26842009-04-21 23:56:24 +00001363 for (unsigned i = 0; i != NumArgs; ++i)
Douglas Gregor7143aab2011-09-01 17:04:32 +00001364 MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001365
1366 // Install function-like macro info.
1367 MI->setIsFunctionLike();
1368 if (isC99VarArgs) MI->setIsC99Varargs();
1369 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor75fdb232009-05-22 22:45:36 +00001370 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001371 PP->getPreprocessorAllocator());
Douglas Gregor37e26842009-04-21 23:56:24 +00001372 }
1373
1374 // Finally, install the macro.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001375 PP->setMacroInfo(II, MI);
Douglas Gregor37e26842009-04-21 23:56:24 +00001376
1377 // Remember that we saw this macro last so that we add the tokens that
1378 // form its body to it.
1379 Macro = MI;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001380
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001381 if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) {
1382 // We have a macro definition. Load it now.
1383 PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro,
Douglas Gregore144c702011-07-28 22:16:57 +00001384 getLocalMacroDefinition(F, Record[NextIndex]));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001385 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001386
Douglas Gregor37e26842009-04-21 23:56:24 +00001387 ++NumMacrosRead;
1388 break;
1389 }
Mike Stump1eb44332009-09-09 15:08:12 +00001390
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001391 case PP_TOKEN: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001392 // If we see a TOKEN before a PP_MACRO_*, then the file is
1393 // erroneous, just pretend we didn't see this.
1394 if (Macro == 0) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001395
Douglas Gregor37e26842009-04-21 23:56:24 +00001396 Token Tok;
1397 Tok.startToken();
Sebastian Redlc3632732010-10-05 15:59:54 +00001398 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001399 Tok.setLength(Record[1]);
Douglas Gregor95eab172011-07-28 20:55:49 +00001400 if (IdentifierInfo *II = getLocalIdentifier(F, Record[2]))
Douglas Gregor37e26842009-04-21 23:56:24 +00001401 Tok.setIdentifierInfo(II);
1402 Tok.setKind((tok::TokenKind)Record[3]);
1403 Tok.setFlag((Token::TokenFlags)Record[4]);
1404 Macro->AddTokenToBody(Tok);
1405 break;
1406 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001407 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001408 }
Douglas Gregor89d99802010-11-30 06:16:57 +00001409
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001410 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001411}
1412
Douglas Gregor72a9ae12011-07-22 16:00:58 +00001413PreprocessedEntity *ASTReader::LoadPreprocessedEntity(Module &F) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001414 assert(PP && "Forgot to set Preprocessor ?");
1415 unsigned Code = F.PreprocessorDetailCursor.ReadCode();
1416 switch (Code) {
1417 case llvm::bitc::END_BLOCK:
1418 return 0;
1419
1420 case llvm::bitc::ENTER_SUBBLOCK:
1421 Error("unexpected subblock record in preprocessor detail block");
1422 return 0;
1423
1424 case llvm::bitc::DEFINE_ABBREV:
1425 Error("unexpected abbrevation record in preprocessor detail block");
1426 return 0;
1427
1428 default:
1429 break;
1430 }
1431
1432 if (!PP->getPreprocessingRecord()) {
1433 Error("no preprocessing record");
1434 return 0;
1435 }
1436
1437 // Read the record.
1438 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1439 const char *BlobStart = 0;
1440 unsigned BlobLen = 0;
1441 RecordData Record;
1442 PreprocessorDetailRecordTypes RecType =
1443 (PreprocessorDetailRecordTypes)F.PreprocessorDetailCursor.ReadRecord(
1444 Code, Record, BlobStart, BlobLen);
1445 switch (RecType) {
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001446 case PPD_MACRO_EXPANSION: {
Douglas Gregor86c67d82011-07-28 22:39:26 +00001447 PreprocessedEntityID GlobalID = getGlobalPreprocessedEntityID(F, Record[0]);
Douglas Gregor7338a922011-08-04 17:06:18 +00001448 if (PreprocessedEntity *PE = PPRec.getLoadedPreprocessedEntity(GlobalID-1))
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001449 return PE;
1450
Chandler Carruth9e5bb852011-07-14 08:20:46 +00001451 MacroExpansion *ME =
Douglas Gregor95eab172011-07-28 20:55:49 +00001452 new (PPRec) MacroExpansion(getLocalIdentifier(F, Record[3]),
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001453 SourceRange(ReadSourceLocation(F, Record[1]),
1454 ReadSourceLocation(F, Record[2])),
Douglas Gregore144c702011-07-28 22:16:57 +00001455 getLocalMacroDefinition(F, Record[4]));
Douglas Gregor7338a922011-08-04 17:06:18 +00001456 PPRec.setLoadedPreallocatedEntity(GlobalID - 1, ME);
Chandler Carruth9e5bb852011-07-14 08:20:46 +00001457 return ME;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001458 }
1459
1460 case PPD_MACRO_DEFINITION: {
Douglas Gregor86c67d82011-07-28 22:39:26 +00001461 PreprocessedEntityID GlobalID = getGlobalPreprocessedEntityID(F, Record[0]);
Douglas Gregor7338a922011-08-04 17:06:18 +00001462 if (PreprocessedEntity *PE = PPRec.getLoadedPreprocessedEntity(GlobalID-1))
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001463 return PE;
Douglas Gregor86c67d82011-07-28 22:39:26 +00001464
1465 unsigned MacroDefID = getGlobalMacroDefinitionID(F, Record[1]);
1466 if (MacroDefID > MacroDefinitionsLoaded.size()) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001467 Error("out-of-bounds macro definition record");
1468 return 0;
1469 }
1470
1471 // Decode the identifier info and then check again; if the macro is
1472 // still defined and associated with the identifier,
Douglas Gregor95eab172011-07-28 20:55:49 +00001473 IdentifierInfo *II = getLocalIdentifier(F, Record[4]);
Douglas Gregor86c67d82011-07-28 22:39:26 +00001474 if (!MacroDefinitionsLoaded[MacroDefID - 1]) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001475 MacroDefinition *MD
1476 = new (PPRec) MacroDefinition(II,
1477 ReadSourceLocation(F, Record[5]),
1478 SourceRange(
1479 ReadSourceLocation(F, Record[2]),
1480 ReadSourceLocation(F, Record[3])));
1481
Douglas Gregor7338a922011-08-04 17:06:18 +00001482 PPRec.setLoadedPreallocatedEntity(GlobalID - 1, MD);
Douglas Gregor86c67d82011-07-28 22:39:26 +00001483 MacroDefinitionsLoaded[MacroDefID - 1] = MD;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001484
1485 if (DeserializationListener)
Douglas Gregor86c67d82011-07-28 22:39:26 +00001486 DeserializationListener->MacroDefinitionRead(MacroDefID, MD);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001487 }
1488
Douglas Gregor86c67d82011-07-28 22:39:26 +00001489 return MacroDefinitionsLoaded[MacroDefID - 1];
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001490 }
1491
1492 case PPD_INCLUSION_DIRECTIVE: {
Douglas Gregor86c67d82011-07-28 22:39:26 +00001493 PreprocessedEntityID GlobalID = getGlobalPreprocessedEntityID(F, Record[0]);
Douglas Gregor7338a922011-08-04 17:06:18 +00001494 if (PreprocessedEntity *PE = PPRec.getLoadedPreprocessedEntity(GlobalID-1))
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001495 return PE;
1496
1497 const char *FullFileNameStart = BlobStart + Record[3];
1498 const FileEntry *File
Chris Lattner5f9e2722011-07-23 10:55:15 +00001499 = PP->getFileManager().getFile(StringRef(FullFileNameStart,
Douglas Gregor86c67d82011-07-28 22:39:26 +00001500 BlobLen - Record[3]));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001501
1502 // FIXME: Stable encoding
1503 InclusionDirective::InclusionKind Kind
1504 = static_cast<InclusionDirective::InclusionKind>(Record[5]);
1505 InclusionDirective *ID
1506 = new (PPRec) InclusionDirective(PPRec, Kind,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001507 StringRef(BlobStart, Record[3]),
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001508 Record[4],
1509 File,
1510 SourceRange(ReadSourceLocation(F, Record[1]),
1511 ReadSourceLocation(F, Record[2])));
Douglas Gregor7338a922011-08-04 17:06:18 +00001512 PPRec.setLoadedPreallocatedEntity(GlobalID - 1, ID);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001513 return ID;
1514 }
1515 }
1516
1517 Error("invalid offset in preprocessor detail block");
1518 return 0;
1519}
1520
Douglas Gregor86c67d82011-07-28 22:39:26 +00001521PreprocessedEntityID
1522ASTReader::getGlobalPreprocessedEntityID(Module &M, unsigned LocalID) {
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001523 ContinuousRangeMap<uint32_t, int, 2>::iterator
1524 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1525 assert(I != M.PreprocessedEntityRemap.end()
1526 && "Invalid index into preprocessed entity index remap");
1527
1528 return LocalID + I->second;
Douglas Gregor86c67d82011-07-28 22:39:26 +00001529}
1530
Douglas Gregor98339b92011-08-25 20:47:51 +00001531unsigned HeaderFileInfoTrait::ComputeHash(const char *path) {
1532 return llvm::HashString(llvm::sys::path::filename(path));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001533}
Douglas Gregor98339b92011-08-25 20:47:51 +00001534
1535HeaderFileInfoTrait::internal_key_type
1536HeaderFileInfoTrait::GetInternalKey(const char *path) { return path; }
1537
1538bool HeaderFileInfoTrait::EqualKey(internal_key_type a, internal_key_type b) {
1539 if (strcmp(a, b) == 0)
1540 return true;
1541
1542 if (llvm::sys::path::filename(a) != llvm::sys::path::filename(b))
1543 return false;
1544
1545 // The file names match, but the path names don't. stat() the files to
1546 // see if they are the same.
1547 struct stat StatBufA, StatBufB;
1548 if (StatSimpleCache(a, &StatBufA) || StatSimpleCache(b, &StatBufB))
1549 return false;
1550
1551 return StatBufA.st_ino == StatBufB.st_ino;
1552}
1553
1554std::pair<unsigned, unsigned>
1555HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1556 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1557 unsigned DataLen = (unsigned) *d++;
1558 return std::make_pair(KeyLen + 1, DataLen);
1559}
1560
1561HeaderFileInfoTrait::data_type
1562HeaderFileInfoTrait::ReadData(const internal_key_type, const unsigned char *d,
1563 unsigned DataLen) {
1564 const unsigned char *End = d + DataLen;
1565 using namespace clang::io;
1566 HeaderFileInfo HFI;
1567 unsigned Flags = *d++;
1568 HFI.isImport = (Flags >> 5) & 0x01;
1569 HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1570 HFI.DirInfo = (Flags >> 2) & 0x03;
1571 HFI.Resolved = (Flags >> 1) & 0x01;
1572 HFI.IndexHeaderMapHeader = Flags & 0x01;
1573 HFI.NumIncludes = ReadUnalignedLE16(d);
1574 HFI.ControllingMacroID = Reader.getGlobalDeclID(M, ReadUnalignedLE32(d));
1575 if (unsigned FrameworkOffset = ReadUnalignedLE32(d)) {
1576 // The framework offset is 1 greater than the actual offset,
1577 // since 0 is used as an indicator for "no framework name".
1578 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1579 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1580 }
1581
1582 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1583 (void)End;
1584
1585 // This HeaderFileInfo was externally loaded.
1586 HFI.External = true;
1587 return HFI;
1588}
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001589
Douglas Gregor72a9ae12011-07-22 16:00:58 +00001590void ASTReader::SetIdentifierIsMacro(IdentifierInfo *II, Module &F,
Douglas Gregor2d2689a2011-07-28 21:16:51 +00001591 uint64_t LocalOffset) {
Douglas Gregor295a2a62010-10-30 00:23:06 +00001592 // Note that this identifier has a macro definition.
1593 II->setHasMacroDefinition(true);
1594
Douglas Gregor8f1231b2011-07-22 06:10:01 +00001595 // Adjust the offset to a global offset.
Douglas Gregor2d2689a2011-07-28 21:16:51 +00001596 UnreadMacroRecordOffsets[II] = F.GlobalBitOffset + LocalOffset;
Douglas Gregor295a2a62010-10-30 00:23:06 +00001597}
1598
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001599void ASTReader::ReadDefinedMacros() {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00001600 for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1601 E = ModuleMgr.rend(); I != E; ++I) {
1602 llvm::BitstreamCursor &MacroCursor = (*I)->MacroCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001603
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001604 // If there was no preprocessor block, skip this file.
1605 if (!MacroCursor.getBitStreamReader())
1606 continue;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001607
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001608 llvm::BitstreamCursor Cursor = MacroCursor;
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00001609 Cursor.JumpToBit((*I)->MacroStartOffset);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001610
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001611 RecordData Record;
1612 while (true) {
1613 unsigned Code = Cursor.ReadCode();
Douglas Gregorecdcb882010-10-20 22:00:55 +00001614 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001615 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001616
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001617 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1618 // No known subblocks, always skip them.
1619 Cursor.ReadSubBlockID();
1620 if (Cursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001621 Error("malformed block record in AST file");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001622 return;
1623 }
1624 continue;
1625 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001626
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001627 if (Code == llvm::bitc::DEFINE_ABBREV) {
1628 Cursor.ReadAbbrevRecord();
1629 continue;
1630 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001631
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001632 // Read a record.
1633 const char *BlobStart;
1634 unsigned BlobLen;
1635 Record.clear();
1636 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1637 default: // Default behavior: ignore.
1638 break;
Douglas Gregor88a35862010-01-04 19:18:44 +00001639
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001640 case PP_MACRO_OBJECT_LIKE:
1641 case PP_MACRO_FUNCTION_LIKE:
Douglas Gregor95eab172011-07-28 20:55:49 +00001642 getLocalIdentifier(**I, Record[0]);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001643 break;
1644
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001645 case PP_TOKEN:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001646 // Ignore tokens.
1647 break;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001648 }
Douglas Gregor88a35862010-01-04 19:18:44 +00001649 }
1650 }
Douglas Gregor295a2a62010-10-30 00:23:06 +00001651
1652 // Drain the unread macro-record offsets map.
1653 while (!UnreadMacroRecordOffsets.empty())
1654 LoadMacroDefinition(UnreadMacroRecordOffsets.begin());
1655}
1656
1657void ASTReader::LoadMacroDefinition(
1658 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos) {
1659 assert(Pos != UnreadMacroRecordOffsets.end() && "Unknown macro definition");
Douglas Gregor295a2a62010-10-30 00:23:06 +00001660 uint64_t Offset = Pos->second;
1661 UnreadMacroRecordOffsets.erase(Pos);
1662
Douglas Gregor8f1231b2011-07-22 06:10:01 +00001663 RecordLocation Loc = getLocalBitOffset(Offset);
1664 ReadMacroRecord(*Loc.F, Loc.Offset);
Douglas Gregor295a2a62010-10-30 00:23:06 +00001665}
1666
1667void ASTReader::LoadMacroDefinition(IdentifierInfo *II) {
1668 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos
1669 = UnreadMacroRecordOffsets.find(II);
1670 LoadMacroDefinition(Pos);
Douglas Gregor88a35862010-01-04 19:18:44 +00001671}
1672
Sebastian Redlf73c93f2010-09-15 19:54:06 +00001673MacroDefinition *ASTReader::getMacroDefinition(MacroID ID) {
Douglas Gregor77424bc2010-10-02 19:29:26 +00001674 if (ID == 0 || ID > MacroDefinitionsLoaded.size())
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001675 return 0;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001676
Douglas Gregor77424bc2010-10-02 19:29:26 +00001677 if (!MacroDefinitionsLoaded[ID - 1]) {
Douglas Gregor14ad03b2011-07-20 01:29:15 +00001678 GlobalMacroDefinitionMapType::iterator I =GlobalMacroDefinitionMap.find(ID);
1679 assert(I != GlobalMacroDefinitionMap.end() &&
1680 "Corrupted global macro definition map");
Douglas Gregor9827a802011-07-29 00:56:45 +00001681 Module &M = *I->second;
1682 unsigned Index = ID - 1 - M.BaseMacroDefinitionID;
1683 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
1684 M.PreprocessorDetailCursor.JumpToBit(M.MacroDefinitionOffsets[Index]);
1685 LoadPreprocessedEntity(M);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001686 }
1687
Douglas Gregor77424bc2010-10-02 19:29:26 +00001688 return MacroDefinitionsLoaded[ID - 1];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001689}
1690
Chris Lattner5f9e2722011-07-23 10:55:15 +00001691const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) {
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00001692 std::string Filename = filenameStrRef;
1693 MaybeAddSystemRootToFilename(Filename);
1694 const FileEntry *File = FileMgr.getFile(Filename);
1695 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1696 OriginalDir != CurrentDir) {
1697 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1698 OriginalDir,
1699 CurrentDir);
1700 if (!resolved.empty())
1701 File = FileMgr.getFile(resolved);
1702 }
1703
1704 return File;
1705}
1706
Douglas Gregore144c702011-07-28 22:16:57 +00001707MacroID ASTReader::getGlobalMacroDefinitionID(Module &M, unsigned LocalID) {
Douglas Gregorfb2d9e02011-08-04 16:36:56 +00001708 if (LocalID < NUM_PREDEF_MACRO_IDS)
1709 return LocalID;
1710
1711 ContinuousRangeMap<uint32_t, int, 2>::iterator I
1712 = M.MacroDefinitionRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
1713 assert(I != M.MacroDefinitionRemap.end() &&
1714 "Invalid index into macro definition ID remap");
1715
1716 return LocalID + I->second;
Douglas Gregore144c702011-07-28 22:16:57 +00001717}
1718
Douglas Gregore650c8c2009-07-07 00:12:59 +00001719/// \brief If we are loading a relocatable PCH file, and the filename is
1720/// not an absolute path, add the system root to the beginning of the file
1721/// name.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001722void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001723 // If this is not a relocatable PCH file, there's nothing to do.
1724 if (!RelocatablePCH)
1725 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001726
Michael J. Spencer256053b2010-12-17 21:22:22 +00001727 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
Douglas Gregore650c8c2009-07-07 00:12:59 +00001728 return;
1729
Douglas Gregor832d6202011-07-22 16:35:34 +00001730 if (isysroot.empty()) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001731 // If no system root was given, default to '/'
1732 Filename.insert(Filename.begin(), '/');
1733 return;
1734 }
Mike Stump1eb44332009-09-09 15:08:12 +00001735
Douglas Gregor832d6202011-07-22 16:35:34 +00001736 unsigned Length = isysroot.size();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001737 if (isysroot[Length - 1] != '/')
1738 Filename.insert(Filename.begin(), '/');
Mike Stump1eb44332009-09-09 15:08:12 +00001739
Douglas Gregor832d6202011-07-22 16:35:34 +00001740 Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end());
Douglas Gregore650c8c2009-07-07 00:12:59 +00001741}
1742
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001743ASTReader::ASTReadResult
Douglas Gregor72a9ae12011-07-22 16:00:58 +00001744ASTReader::ReadASTBlock(Module &F) {
Sebastian Redl9137a522010-07-16 17:50:48 +00001745 llvm::BitstreamCursor &Stream = F.Stream;
1746
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001747 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001748 Error("malformed block record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001749 return Failure;
1750 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001751
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001752 // Read all of the records and blocks for the ASt file.
Douglas Gregor8038d512009-04-10 17:25:41 +00001753 RecordData Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001754 while (!Stream.AtEndOfStream()) {
1755 unsigned Code = Stream.ReadCode();
1756 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001757 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001758 Error("error at end of module block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001759 return Failure;
1760 }
Chris Lattner7356a312009-04-11 21:15:38 +00001761
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001762 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001763 }
1764
1765 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1766 switch (Stream.ReadSubBlockID()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001767 case DECLTYPES_BLOCK_ID:
Chris Lattner6367f6d2009-04-27 01:05:14 +00001768 // We lazily load the decls block, but we want to set up the
1769 // DeclsCursor cursor to point into it. Clone our current bitcode
1770 // cursor to it, enter the block and read the abbrevs in that block.
1771 // With the main cursor, we just skip over it.
Sebastian Redl9137a522010-07-16 17:50:48 +00001772 F.DeclsCursor = Stream;
Chris Lattner6367f6d2009-04-27 01:05:14 +00001773 if (Stream.SkipBlock() || // Skip with the main cursor.
1774 // Read the abbrevs.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001775 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001776 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001777 return Failure;
1778 }
1779 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001780
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00001781 case DECL_UPDATES_BLOCK_ID:
1782 if (Stream.SkipBlock()) {
1783 Error("malformed block record in AST file");
1784 return Failure;
1785 }
1786 break;
1787
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001788 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl9137a522010-07-16 17:50:48 +00001789 F.MacroCursor = Stream;
Douglas Gregor88a35862010-01-04 19:18:44 +00001790 if (PP)
1791 PP->setExternalSource(this);
1792
Douglas Gregorecdcb882010-10-20 22:00:55 +00001793 if (Stream.SkipBlock() ||
1794 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001795 Error("malformed block record in AST file");
Chris Lattner7356a312009-04-11 21:15:38 +00001796 return Failure;
1797 }
Douglas Gregorecdcb882010-10-20 22:00:55 +00001798 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattner7356a312009-04-11 21:15:38 +00001799 break;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001800
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001801 case PREPROCESSOR_DETAIL_BLOCK_ID:
1802 F.PreprocessorDetailCursor = Stream;
1803 if (Stream.SkipBlock() ||
1804 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
1805 PREPROCESSOR_DETAIL_BLOCK_ID)) {
1806 Error("malformed preprocessor detail record in AST file");
1807 return Failure;
1808 }
1809 F.PreprocessorDetailStartOffset
1810 = F.PreprocessorDetailCursor.GetCurrentBitNo();
1811 break;
1812
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001813 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001814 switch (ReadSourceManagerBlock(F)) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00001815 case Success:
1816 break;
1817
1818 case Failure:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001819 Error("malformed source manager block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001820 return Failure;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001821
1822 case IgnorePCH:
1823 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001824 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001825 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001826 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001827 continue;
1828 }
1829
1830 if (Code == llvm::bitc::DEFINE_ABBREV) {
1831 Stream.ReadAbbrevRecord();
1832 continue;
1833 }
1834
1835 // Read and process a record.
1836 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00001837 const char *BlobStart = 0;
1838 unsigned BlobLen = 0;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001839 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00001840 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001841 default: // Default behavior: ignore.
1842 break;
1843
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001844 case METADATA: {
1845 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1846 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001847 : diag::warn_pch_version_too_new);
1848 return IgnorePCH;
1849 }
1850
1851 RelocatablePCH = Record[4];
1852 if (Listener) {
1853 std::string TargetTriple(BlobStart, BlobLen);
1854 if (Listener->ReadTargetTriple(TargetTriple))
1855 return IgnorePCH;
1856 }
1857 break;
1858 }
1859
Douglas Gregore95b9192011-08-17 21:07:30 +00001860 case IMPORTS: {
1861 // Load each of the imported PCH files.
1862 unsigned Idx = 0, N = Record.size();
1863 while (Idx < N) {
1864 // Read information about the AST file.
1865 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
1866 unsigned Length = Record[Idx++];
1867 llvm::SmallString<128> ImportedFile(Record.begin() + Idx,
1868 Record.begin() + Idx + Length);
1869 Idx += Length;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001870
Douglas Gregore95b9192011-08-17 21:07:30 +00001871 // Load the AST file.
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001872 switch(ReadASTCore(ImportedFile, ImportedKind, &F)) {
Douglas Gregore95b9192011-08-17 21:07:30 +00001873 case Failure: return Failure;
1874 // If we have to ignore the dependency, we'll have to ignore this too.
1875 case IgnorePCH: return IgnorePCH;
1876 case Success: break;
1877 }
1878 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001879 break;
1880 }
1881
Douglas Gregora119da02011-08-02 16:26:37 +00001882 case TYPE_OFFSET: {
Sebastian Redl12d6da02010-07-19 22:06:55 +00001883 if (F.LocalNumTypes != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001884 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001885 return Failure;
1886 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00001887 F.TypeOffsets = (const uint32_t *)BlobStart;
1888 F.LocalNumTypes = Record[0];
Douglas Gregore3605012011-08-02 18:32:54 +00001889 unsigned LocalBaseTypeIndex = Record[1];
1890 F.BaseTypeIndex = getTotalNumTypes();
Douglas Gregor1e849b62011-07-29 00:21:44 +00001891
Douglas Gregora119da02011-08-02 16:26:37 +00001892 if (F.LocalNumTypes > 0) {
1893 // Introduce the global -> local mapping for types within this module.
1894 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
1895
1896 // Introduce the local -> global mapping for types within this module.
Douglas Gregore3605012011-08-02 18:32:54 +00001897 F.TypeRemap.insert(std::make_pair(LocalBaseTypeIndex,
1898 F.BaseTypeIndex - LocalBaseTypeIndex));
Douglas Gregora119da02011-08-02 16:26:37 +00001899
1900 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
1901 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001902 break;
Douglas Gregora119da02011-08-02 16:26:37 +00001903 }
1904
Douglas Gregor496c7092011-08-03 15:48:04 +00001905 case DECL_OFFSET: {
Sebastian Redl12d6da02010-07-19 22:06:55 +00001906 if (F.LocalNumDecls != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001907 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001908 return Failure;
1909 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00001910 F.DeclOffsets = (const uint32_t *)BlobStart;
1911 F.LocalNumDecls = Record[0];
Douglas Gregor496c7092011-08-03 15:48:04 +00001912 unsigned LocalBaseDeclID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00001913 F.BaseDeclID = getTotalNumDecls();
Douglas Gregor96e973f2011-07-20 00:27:43 +00001914
Douglas Gregor496c7092011-08-03 15:48:04 +00001915 if (F.LocalNumDecls > 0) {
1916 // Introduce the global -> local mapping for declarations within this
1917 // module.
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00001918 GlobalDeclMap.insert(
1919 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
Douglas Gregor496c7092011-08-03 15:48:04 +00001920
1921 // Introduce the local -> global mapping for declarations within this
1922 // module.
1923 F.DeclRemap.insert(std::make_pair(LocalBaseDeclID,
1924 F.BaseDeclID - LocalBaseDeclID));
1925
1926 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
1927 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001928 break;
Douglas Gregor496c7092011-08-03 15:48:04 +00001929 }
1930
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001931 case TU_UPDATE_LEXICAL: {
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00001932 DeclContext *TU = Context ? Context->getTranslationUnitDecl() : 0;
Douglas Gregor0d95f772011-08-24 19:03:07 +00001933 DeclContextInfo &Info = F.DeclContextInfos[TU];
1934 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(BlobStart);
1935 Info.NumLexicalDecls
1936 = static_cast<unsigned int>(BlobLen / sizeof(KindDeclIDPair));
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00001937 if (TU)
1938 TU->setHasExternalLexicalStorage(true);
1939
Sebastian Redld692af72010-07-27 18:24:41 +00001940 break;
1941 }
1942
Sebastian Redle1dde812010-08-24 00:50:04 +00001943 case UPDATE_VISIBLE: {
Douglas Gregor496c7092011-08-03 15:48:04 +00001944 unsigned Idx = 0;
1945 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
Sebastian Redle1dde812010-08-24 00:50:04 +00001946 void *Table = ASTDeclContextNameLookupTable::Create(
Douglas Gregor496c7092011-08-03 15:48:04 +00001947 (const unsigned char *)BlobStart + Record[Idx++],
Sebastian Redle1dde812010-08-24 00:50:04 +00001948 (const unsigned char *)BlobStart,
Douglas Gregor393f2492011-07-22 00:38:23 +00001949 ASTDeclContextNameLookupTrait(*this, F));
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00001950 if (ID == PREDEF_DECL_TRANSLATION_UNIT_ID && Context) { // Is it the TU?
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00001951 DeclContext *TU = Context->getTranslationUnitDecl();
Douglas Gregor0d95f772011-08-24 19:03:07 +00001952 F.DeclContextInfos[TU].NameLookupTableData = Table;
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00001953 TU->setHasExternalVisibleStorage(true);
Sebastian Redle1dde812010-08-24 00:50:04 +00001954 } else
Douglas Gregor496c7092011-08-03 15:48:04 +00001955 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
Sebastian Redle1dde812010-08-24 00:50:04 +00001956 break;
1957 }
1958
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001959 case REDECLS_UPDATE_LATEST: {
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00001960 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
Douglas Gregor496c7092011-08-03 15:48:04 +00001961 for (unsigned i = 0, e = Record.size(); i < e; /* in loop */) {
1962 DeclID First = ReadDeclID(F, Record, i);
1963 DeclID Latest = ReadDeclID(F, Record, i);
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00001964 FirstLatestDeclIDs[First] = Latest;
1965 }
1966 break;
1967 }
1968
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001969 case LANGUAGE_OPTIONS:
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001970 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001971 return IgnorePCH;
1972 break;
Douglas Gregor2bec0412009-04-10 21:16:55 +00001973
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001974 case IDENTIFIER_TABLE:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001975 F.IdentifierTableData = BlobStart;
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001976 if (Record[0]) {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001977 F.IdentifierLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001978 = ASTIdentifierLookupTable::Create(
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001979 (const unsigned char *)F.IdentifierTableData + Record[0],
1980 (const unsigned char *)F.IdentifierTableData,
Sebastian Redlc3632732010-10-05 15:59:54 +00001981 ASTIdentifierLookupTrait(*this, F));
Douglas Gregor0cdd7982011-07-21 18:46:38 +00001982 if (PP) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001983 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor0cdd7982011-07-21 18:46:38 +00001984 PP->getHeaderSearchInfo().SetExternalLookup(this);
1985 }
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001986 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001987 break;
1988
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001989 case IDENTIFIER_OFFSET: {
Sebastian Redl2da08f92010-07-19 22:28:42 +00001990 if (F.LocalNumIdentifiers != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001991 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00001992 return Failure;
1993 }
Sebastian Redl2da08f92010-07-19 22:28:42 +00001994 F.IdentifierOffsets = (const uint32_t *)BlobStart;
1995 F.LocalNumIdentifiers = Record[0];
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001996 unsigned LocalBaseIdentifierID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00001997 F.BaseIdentifierID = getTotalNumIdentifiers();
Douglas Gregor67268d02011-07-20 00:59:32 +00001998
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001999 if (F.LocalNumIdentifiers > 0) {
2000 // Introduce the global -> local mapping for identifiers within this
2001 // module.
2002 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
2003 &F));
2004
2005 // Introduce the local -> global mapping for identifiers within this
2006 // module.
2007 F.IdentifierRemap.insert(
2008 std::make_pair(LocalBaseIdentifierID,
2009 F.BaseIdentifierID - LocalBaseIdentifierID));
2010
2011 IdentifiersLoaded.resize(IdentifiersLoaded.size()
2012 + F.LocalNumIdentifiers);
2013 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002014 break;
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002015 }
2016
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002017 case EXTERNAL_DEFINITIONS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002018 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2019 ExternalDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregorfdd01722009-04-14 00:24:19 +00002020 break;
Douglas Gregor3e1af842009-04-17 22:13:46 +00002021
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002022 case SPECIAL_TYPES:
Douglas Gregor393f2492011-07-22 00:38:23 +00002023 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2024 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
Douglas Gregorad1de002009-04-18 05:55:16 +00002025 break;
2026
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002027 case STATISTICS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002028 TotalNumStatements += Record[0];
2029 TotalNumMacros += Record[1];
2030 TotalLexicalDeclContexts += Record[2];
2031 TotalVisibleDeclContexts += Record[3];
Douglas Gregor3e1af842009-04-17 22:13:46 +00002032 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002033
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002034 case UNUSED_FILESCOPED_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002035 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2036 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002037 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002038
Sean Huntebcbe1d2011-05-04 23:29:54 +00002039 case DELEGATING_CTORS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002040 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2041 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
Sean Huntebcbe1d2011-05-04 23:29:54 +00002042 break;
2043
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002044 case WEAK_UNDECLARED_IDENTIFIERS:
Douglas Gregor31e37b22011-07-28 18:09:57 +00002045 if (Record.size() % 4 != 0) {
2046 Error("invalid weak identifiers record");
2047 return Failure;
2048 }
2049
2050 // FIXME: Ignore weak undeclared identifiers from non-original PCH
2051 // files. This isn't the way to do it :)
2052 WeakUndeclaredIdentifiers.clear();
2053
2054 // Translate the weak, undeclared identifiers into global IDs.
2055 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2056 WeakUndeclaredIdentifiers.push_back(
2057 getGlobalIdentifierID(F, Record[I++]));
2058 WeakUndeclaredIdentifiers.push_back(
2059 getGlobalIdentifierID(F, Record[I++]));
2060 WeakUndeclaredIdentifiers.push_back(
2061 ReadSourceLocation(F, Record, I).getRawEncoding());
2062 WeakUndeclaredIdentifiers.push_back(Record[I++]);
2063 }
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002064 break;
2065
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002066 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002067 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2068 LocallyScopedExternalDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregor14c22f22009-04-22 22:18:58 +00002069 break;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002070
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002071 case SELECTOR_OFFSETS: {
Sebastian Redl059612d2010-08-03 21:58:15 +00002072 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redl725cd962010-08-04 20:40:17 +00002073 F.LocalNumSelectors = Record[0];
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002074 unsigned LocalBaseSelectorID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00002075 F.BaseSelectorID = getTotalNumSelectors();
Douglas Gregor96958cb2011-07-20 01:10:58 +00002076
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002077 if (F.LocalNumSelectors > 0) {
2078 // Introduce the global -> local mapping for selectors within this
2079 // module.
2080 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2081
2082 // Introduce the local -> global mapping for selectors within this
2083 // module.
2084 F.SelectorRemap.insert(std::make_pair(LocalBaseSelectorID,
2085 F.BaseSelectorID - LocalBaseSelectorID));
Douglas Gregor83941df2009-04-25 17:48:32 +00002086
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002087 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
2088 }
2089 break;
2090 }
2091
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002092 case METHOD_POOL:
Sebastian Redl725cd962010-08-04 20:40:17 +00002093 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor83941df2009-04-25 17:48:32 +00002094 if (Record[0])
Sebastian Redl725cd962010-08-04 20:40:17 +00002095 F.SelectorLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002096 = ASTSelectorLookupTable::Create(
Sebastian Redl725cd962010-08-04 20:40:17 +00002097 F.SelectorLookupTableData + Record[0],
2098 F.SelectorLookupTableData,
Douglas Gregor409448c2011-07-21 22:35:25 +00002099 ASTSelectorLookupTrait(*this, F));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002100 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002101 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002102
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002103 case REFERENCED_SELECTOR_POOL:
Douglas Gregor8451ec72011-07-28 14:41:43 +00002104 if (!Record.empty()) {
2105 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2106 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2107 Record[Idx++]));
2108 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2109 getRawEncoding());
2110 }
2111 }
Fariborz Jahanian32019832010-07-23 19:11:11 +00002112 break;
2113
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002114 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002115 if (!Record.empty() && Listener)
2116 Listener->ReadCounter(Record[0]);
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002117 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002118
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002119 case SOURCE_LOCATION_OFFSETS: {
2120 F.SLocEntryOffsets = (const uint32_t *)BlobStart;
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002121 F.LocalNumSLocEntries = Record[0];
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002122 llvm::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
2123 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries, Record[1]);
2124 // Make our entry in the range map. BaseID is negative and growing, so
2125 // we invert it. Because we invert it, though, we need the other end of
2126 // the range.
2127 unsigned RangeStart =
2128 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2129 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2130 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2131
2132 // Initialize the remapping table.
2133 // Invalid stays invalid.
2134 F.SLocRemap.insert(std::make_pair(0U, 0));
2135 // This module. Base was 2 when being compiled.
2136 F.SLocRemap.insert(std::make_pair(2U,
2137 static_cast<int>(F.SLocEntryBaseOffset - 2)));
Douglas Gregor0cdd7982011-07-21 18:46:38 +00002138
2139 TotalNumSLocEntries += F.LocalNumSLocEntries;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002140 break;
2141 }
2142
Douglas Gregor5d51a1d2011-08-01 16:01:55 +00002143 case MODULE_OFFSET_MAP: {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002144 // Additional remapping information.
2145 const unsigned char *Data = (const unsigned char*)BlobStart;
2146 const unsigned char *DataEnd = Data + BlobLen;
Douglas Gregorf33740e2011-08-02 10:56:51 +00002147
2148 // Continuous range maps we may be updating in our module.
2149 ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap);
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002150 ContinuousRangeMap<uint32_t, int, 2>::Builder
2151 IdentifierRemap(F.IdentifierRemap);
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002152 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002153 PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2154 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregorfb2d9e02011-08-04 16:36:56 +00002155 MacroDefinitionRemap(F.MacroDefinitionRemap);
2156 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002157 SelectorRemap(F.SelectorRemap);
Douglas Gregor496c7092011-08-03 15:48:04 +00002158 ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap);
Douglas Gregora119da02011-08-02 16:26:37 +00002159 ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap);
2160
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002161 while(Data < DataEnd) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002162 uint16_t Len = io::ReadUnalignedLE16(Data);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002163 StringRef Name = StringRef((const char*)Data, Len);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002164 Data += Len;
Jonathan D. Turner48d2c3f2011-07-26 18:21:30 +00002165 Module *OM = ModuleMgr.lookup(Name);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002166 if (!OM) {
2167 Error("SourceLocation remap refers to unknown module");
2168 return Failure;
2169 }
Douglas Gregorf33740e2011-08-02 10:56:51 +00002170
2171 uint32_t SLocOffset = io::ReadUnalignedLE32(Data);
2172 uint32_t IdentifierIDOffset = io::ReadUnalignedLE32(Data);
2173 uint32_t PreprocessedEntityIDOffset = io::ReadUnalignedLE32(Data);
2174 uint32_t MacroDefinitionIDOffset = io::ReadUnalignedLE32(Data);
2175 uint32_t SelectorIDOffset = io::ReadUnalignedLE32(Data);
2176 uint32_t DeclIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregora119da02011-08-02 16:26:37 +00002177 uint32_t TypeIndexOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002178
2179 // Source location offset is mapped to OM->SLocEntryBaseOffset.
2180 SLocRemap.insert(std::make_pair(SLocOffset,
2181 static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset)));
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002182 IdentifierRemap.insert(
2183 std::make_pair(IdentifierIDOffset,
2184 OM->BaseIdentifierID - IdentifierIDOffset));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002185 PreprocessedEntityRemap.insert(
2186 std::make_pair(PreprocessedEntityIDOffset,
2187 OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset));
Douglas Gregorfb2d9e02011-08-04 16:36:56 +00002188 MacroDefinitionRemap.insert(
2189 std::make_pair(MacroDefinitionIDOffset,
2190 OM->BaseMacroDefinitionID - MacroDefinitionIDOffset));
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002191 SelectorRemap.insert(std::make_pair(SelectorIDOffset,
2192 OM->BaseSelectorID - SelectorIDOffset));
Douglas Gregor496c7092011-08-03 15:48:04 +00002193 DeclRemap.insert(std::make_pair(DeclIDOffset,
2194 OM->BaseDeclID - DeclIDOffset));
2195
Douglas Gregora119da02011-08-02 16:26:37 +00002196 TypeRemap.insert(std::make_pair(TypeIndexOffset,
Douglas Gregore3605012011-08-02 18:32:54 +00002197 OM->BaseTypeIndex - TypeIndexOffset));
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002198 }
2199 break;
2200 }
2201
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002202 case SOURCE_MANAGER_LINE_TABLE:
2203 if (ParseLineTable(F, Record))
2204 return Failure;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002205 break;
2206
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00002207 case FILE_SOURCE_LOCATION_OFFSETS:
2208 F.SLocFileOffsets = (const uint32_t *)BlobStart;
2209 F.LocalNumSLocFileEntries = Record[0];
2210 break;
2211
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002212 case SOURCE_LOCATION_PRELOADS: {
2213 // Need to transform from the local view (1-based IDs) to the global view,
2214 // which is based off F.SLocEntryBaseID.
Douglas Gregorf249bf32011-08-25 21:09:44 +00002215 if (!F.PreloadSLocEntries.empty()) {
2216 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
2217 return Failure;
2218 }
2219
2220 F.PreloadSLocEntries.swap(Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002221 break;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002222 }
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002223
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002224 case STAT_CACHE: {
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00002225 if (!DisableStatCache) {
2226 ASTStatCache *MyStatCache =
2227 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
2228 (const unsigned char *)BlobStart,
2229 NumStatHits, NumStatMisses);
2230 FileMgr.addStatCache(MyStatCache);
2231 F.StatCache = MyStatCache;
2232 }
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002233 break;
Douglas Gregor52e71082009-10-16 18:18:30 +00002234 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002235
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002236 case EXT_VECTOR_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002237 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2238 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregorb81c1702009-04-27 20:06:05 +00002239 break;
2240
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002241 case VTABLE_USES:
Douglas Gregordfe65432011-07-28 19:11:31 +00002242 if (Record.size() % 3 != 0) {
2243 Error("Invalid VTABLE_USES record");
2244 return Failure;
2245 }
2246
Sebastian Redl40566802010-08-05 18:21:25 +00002247 // Later tables overwrite earlier ones.
Douglas Gregordfe65432011-07-28 19:11:31 +00002248 // FIXME: Modules will have some trouble with this. This is clearly not
2249 // the right way to do this.
Douglas Gregor409448c2011-07-21 22:35:25 +00002250 VTableUses.clear();
Douglas Gregordfe65432011-07-28 19:11:31 +00002251
2252 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
2253 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
2254 VTableUses.push_back(
2255 ReadSourceLocation(F, Record, Idx).getRawEncoding());
2256 VTableUses.push_back(Record[Idx++]);
2257 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002258 break;
2259
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002260 case DYNAMIC_CLASSES:
Douglas Gregor409448c2011-07-21 22:35:25 +00002261 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2262 DynamicClasses.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002263 break;
2264
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002265 case PENDING_IMPLICIT_INSTANTIATIONS:
Douglas Gregorf2abb522011-07-28 19:26:52 +00002266 if (PendingInstantiations.size() % 2 != 0) {
2267 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
2268 return Failure;
2269 }
2270
2271 // Later lists of pending instantiations overwrite earlier ones.
2272 // FIXME: This is most certainly wrong for modules.
2273 PendingInstantiations.clear();
2274 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2275 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
2276 PendingInstantiations.push_back(
2277 ReadSourceLocation(F, Record, I).getRawEncoding());
2278 }
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002279 break;
2280
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002281 case SEMA_DECL_REFS:
Sebastian Redl40566802010-08-05 18:21:25 +00002282 // Later tables overwrite earlier ones.
Douglas Gregor409448c2011-07-21 22:35:25 +00002283 // FIXME: Modules will have some trouble with this.
2284 SemaDeclRefs.clear();
2285 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2286 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002287 break;
2288
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002289 case ORIGINAL_FILE_NAME:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002290 // The primary AST will be the last to get here, so it will be the one
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002291 // that's used.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +00002292 ActualOriginalFileName.assign(BlobStart, BlobLen);
2293 OriginalFileName = ActualOriginalFileName;
Douglas Gregore650c8c2009-07-07 00:12:59 +00002294 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregorb64c1932009-05-12 01:31:05 +00002295 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002296
Douglas Gregor31d375f2011-05-06 21:43:30 +00002297 case ORIGINAL_FILE_ID:
2298 OriginalFileID = FileID::get(Record[0]);
2299 break;
2300
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002301 case ORIGINAL_PCH_DIR:
2302 // The primary AST will be the last to get here, so it will be the one
2303 // that's used.
2304 OriginalDir.assign(BlobStart, BlobLen);
2305 break;
2306
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002307 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek974be4d2010-02-12 23:31:14 +00002308 const std::string &CurBranch = getClangFullRepositoryVersion();
Chris Lattner5f9e2722011-07-23 10:55:15 +00002309 StringRef ASTBranch(BlobStart, BlobLen);
2310 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002311 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregor445e23e2009-10-05 21:07:28 +00002312 return IgnorePCH;
2313 }
2314 break;
2315 }
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002316
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002317 case MACRO_DEFINITION_OFFSETS: {
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002318 F.MacroDefinitionOffsets = (const uint32_t *)BlobStart;
2319 F.NumPreallocatedPreprocessingEntities = Record[0];
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002320 unsigned LocalBasePreprocessedEntityID = Record[1];
2321 F.LocalNumMacroDefinitions = Record[2];
2322 unsigned LocalBaseMacroID = Record[3];
Douglas Gregorfb2d9e02011-08-04 16:36:56 +00002323
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002324 unsigned StartingID;
2325 if (PP) {
2326 if (!PP->getPreprocessingRecord())
2327 PP->createPreprocessingRecord(true);
2328 if (!PP->getPreprocessingRecord()->getExternalSource())
2329 PP->getPreprocessingRecord()->SetExternalSource(*this);
2330 StartingID
2331 = PP->getPreprocessingRecord()
2332 ->allocateLoadedEntities(F.NumPreallocatedPreprocessingEntities);
2333 } else {
2334 // FIXME: We'll eventually want to kill this path, since it assumes
2335 // a particular allocation strategy in the preprocessing record.
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002336 StartingID = getTotalNumPreprocessedEntities()
2337 - F.NumPreallocatedPreprocessingEntities;
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002338 }
Douglas Gregor9827a802011-07-29 00:56:45 +00002339 F.BaseMacroDefinitionID = getTotalNumMacroDefinitions();
2340 F.BasePreprocessedEntityID = StartingID;
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002341
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002342 if (F.NumPreallocatedPreprocessingEntities > 0) {
2343 // Introduce the global -> local mapping for preprocessed entities in
2344 // this module.
2345 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
2346
2347 // Introduce the local -> global mapping for preprocessed entities in
2348 // this module.
2349 F.PreprocessedEntityRemap.insert(
2350 std::make_pair(LocalBasePreprocessedEntityID,
2351 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
2352 }
2353
2354
Douglas Gregorfb2d9e02011-08-04 16:36:56 +00002355 if (F.LocalNumMacroDefinitions > 0) {
2356 // Introduce the global -> local mapping for macro definitions within
2357 // this module.
2358 GlobalMacroDefinitionMap.insert(
2359 std::make_pair(getTotalNumMacroDefinitions() + 1, &F));
2360
2361 // Introduce the local -> global mapping for macro definitions within
2362 // this module.
2363 F.MacroDefinitionRemap.insert(
2364 std::make_pair(LocalBaseMacroID,
2365 F.BaseMacroDefinitionID - LocalBaseMacroID));
2366
2367 MacroDefinitionsLoaded.resize(
Douglas Gregor14ad03b2011-07-20 01:29:15 +00002368 MacroDefinitionsLoaded.size() + F.LocalNumMacroDefinitions);
Douglas Gregorfb2d9e02011-08-04 16:36:56 +00002369 }
2370
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002371 break;
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002372 }
2373
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002374 case DECL_UPDATE_OFFSETS: {
2375 if (Record.size() % 2 != 0) {
2376 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2377 return Failure;
2378 }
2379 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Douglas Gregor496c7092011-08-03 15:48:04 +00002380 DeclUpdateOffsets[getGlobalDeclID(F, Record[I])]
2381 .push_back(std::make_pair(&F, Record[I+1]));
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002382 break;
2383 }
2384
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002385 case DECL_REPLACEMENTS: {
Sebastian Redl0b17c612010-08-13 00:28:03 +00002386 if (Record.size() % 2 != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002387 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redl0b17c612010-08-13 00:28:03 +00002388 return Failure;
2389 }
2390 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Douglas Gregor496c7092011-08-03 15:48:04 +00002391 ReplacedDecls[getGlobalDeclID(F, Record[I])]
2392 = std::make_pair(&F, Record[I+1]);
Sebastian Redl0b17c612010-08-13 00:28:03 +00002393 break;
2394 }
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00002395
2396 case OBJC_CHAINED_CATEGORIES: {
2397 if (Record.size() % 3 != 0) {
2398 Error("invalid OBJC_CHAINED_CATEGORIES block in AST file");
2399 return Failure;
2400 }
2401 for (unsigned I = 0, N = Record.size(); I != N; I += 3) {
2402 serialization::GlobalDeclID GlobID = getGlobalDeclID(F, Record[I]);
2403 F.ChainedObjCCategories[GlobID] = std::make_pair(Record[I+1],
2404 Record[I+2]);
2405 ObjCChainedCategoriesInterfaces.insert(GlobID);
2406 }
2407 break;
2408 }
Douglas Gregor7c789c12010-10-29 22:39:52 +00002409
2410 case CXX_BASE_SPECIFIER_OFFSETS: {
2411 if (F.LocalNumCXXBaseSpecifiers != 0) {
2412 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2413 return Failure;
2414 }
2415
2416 F.LocalNumCXXBaseSpecifiers = Record[0];
2417 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
Jonathan D. Turner1da90142011-07-21 21:15:19 +00002418 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers;
Douglas Gregor7c789c12010-10-29 22:39:52 +00002419 break;
2420 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002421
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002422 case DIAG_PRAGMA_MAPPINGS:
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002423 if (Record.size() % 2 != 0) {
2424 Error("invalid DIAG_USER_MAPPINGS block in AST file");
2425 return Failure;
2426 }
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002427
2428 if (F.PragmaDiagMappings.empty())
2429 F.PragmaDiagMappings.swap(Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002430 else
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002431 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
2432 Record.begin(), Record.end());
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002433 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002434
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002435 case CUDA_SPECIAL_DECL_REFS:
2436 // Later tables overwrite earlier ones.
Douglas Gregor409448c2011-07-21 22:35:25 +00002437 // FIXME: Modules will have trouble with this.
2438 CUDASpecialDeclRefs.clear();
2439 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2440 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002441 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002442
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002443 case HEADER_SEARCH_TABLE: {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002444 F.HeaderFileInfoTableData = BlobStart;
2445 F.LocalNumHeaderFileInfos = Record[1];
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002446 F.HeaderFileFrameworkStrings = BlobStart + Record[2];
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002447 if (Record[0]) {
2448 F.HeaderFileInfoTable
2449 = HeaderFileInfoLookupTable::Create(
2450 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002451 (const unsigned char *)F.HeaderFileInfoTableData,
Douglas Gregor95eab172011-07-28 20:55:49 +00002452 HeaderFileInfoTrait(*this, F,
2453 PP? &PP->getHeaderSearchInfo() : 0,
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002454 BlobStart + Record[2]));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002455 if (PP)
2456 PP->getHeaderSearchInfo().SetExternalSource(this);
2457 }
2458 break;
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002459 }
2460
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002461 case FP_PRAGMA_OPTIONS:
2462 // Later tables overwrite earlier ones.
2463 FPPragmaOptions.swap(Record);
2464 break;
2465
2466 case OPENCL_EXTENSIONS:
2467 // Later tables overwrite earlier ones.
2468 OpenCLExtensions.swap(Record);
2469 break;
Sean Huntebcbe1d2011-05-04 23:29:54 +00002470
2471 case TENTATIVE_DEFINITIONS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002472 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2473 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Sean Huntebcbe1d2011-05-04 23:29:54 +00002474 break;
Douglas Gregord8bba9c2011-06-28 16:20:02 +00002475
2476 case KNOWN_NAMESPACES:
Douglas Gregor409448c2011-07-21 22:35:25 +00002477 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2478 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregord8bba9c2011-06-28 16:20:02 +00002479 break;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002480 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002481 }
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002482 Error("premature end of bitstream in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002483 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002484}
2485
Douglas Gregorc69a2922011-08-25 20:58:51 +00002486ASTReader::ASTReadResult ASTReader::validateFileEntries(Module &M) {
2487 llvm::BitstreamCursor &SLocEntryCursor = M.SLocEntryCursor;
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002488
Douglas Gregorc69a2922011-08-25 20:58:51 +00002489 for (unsigned i = 0, e = M.LocalNumSLocFileEntries; i != e; ++i) {
2490 SLocEntryCursor.JumpToBit(M.SLocFileOffsets[i]);
2491 unsigned Code = SLocEntryCursor.ReadCode();
2492 if (Code == llvm::bitc::END_BLOCK ||
2493 Code == llvm::bitc::ENTER_SUBBLOCK ||
2494 Code == llvm::bitc::DEFINE_ABBREV) {
2495 Error("incorrectly-formatted source location entry in AST file");
2496 return Failure;
2497 }
2498
2499 RecordData Record;
2500 const char *BlobStart;
2501 unsigned BlobLen;
2502 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
2503 default:
2504 Error("incorrectly-formatted source location entry in AST file");
2505 return Failure;
2506
2507 case SM_SLOC_FILE_ENTRY: {
2508 StringRef Filename(BlobStart, BlobLen);
2509 const FileEntry *File = getFileEntry(Filename);
2510
2511 if (File == 0) {
2512 std::string ErrorStr = "could not find file '";
2513 ErrorStr += Filename;
2514 ErrorStr += "' referenced by AST file";
2515 Error(ErrorStr.c_str());
2516 return IgnorePCH;
2517 }
2518
2519 if (Record.size() < 6) {
2520 Error("source location entry is incorrect");
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002521 return Failure;
2522 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002523
Douglas Gregorc69a2922011-08-25 20:58:51 +00002524 // The stat info from the FileEntry came from the cached stat
2525 // info of the PCH, so we cannot trust it.
2526 struct stat StatBuf;
2527 if (::stat(File->getName(), &StatBuf) != 0) {
2528 StatBuf.st_size = File->getSize();
2529 StatBuf.st_mtime = File->getModificationTime();
2530 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002531
Douglas Gregorc69a2922011-08-25 20:58:51 +00002532 if (((off_t)Record[4] != StatBuf.st_size
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002533#if !defined(LLVM_ON_WIN32)
Douglas Gregorc69a2922011-08-25 20:58:51 +00002534 // In our regression testing, the Windows file system seems to
2535 // have inconsistent modification times that sometimes
2536 // erroneously trigger this error-handling path.
2537 || (time_t)Record[5] != StatBuf.st_mtime
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002538#endif
Douglas Gregorc69a2922011-08-25 20:58:51 +00002539 )) {
2540 Error(diag::err_fe_pch_file_modified, Filename);
2541 return IgnorePCH;
2542 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002543
Douglas Gregorc69a2922011-08-25 20:58:51 +00002544 break;
2545 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002546 }
2547 }
2548
2549 return Success;
2550}
2551
Douglas Gregor46c03c32011-08-25 21:19:59 +00002552namespace {
2553 /// \brief Visitor class used to look up identifirs in an AST file.
2554 class IdentifierLookupVisitor {
2555 StringRef Name;
2556 IdentifierInfo *Found;
2557 public:
2558 explicit IdentifierLookupVisitor(StringRef Name) : Name(Name), Found() { }
2559
2560 static bool visit(Module &M, void *UserData) {
2561 IdentifierLookupVisitor *This
2562 = static_cast<IdentifierLookupVisitor *>(UserData);
2563
2564 ASTIdentifierLookupTable *IdTable
Douglas Gregor2ea054f2011-08-26 22:04:51 +00002565 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
Douglas Gregor46c03c32011-08-25 21:19:59 +00002566 if (!IdTable)
2567 return false;
2568
2569 std::pair<const char*, unsigned> Key(This->Name.begin(),
2570 This->Name.size());
2571 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
2572 if (Pos == IdTable->end())
2573 return false;
2574
2575 // Dereferencing the iterator has the effect of building the
2576 // IdentifierInfo node and populating it with the various
2577 // declarations it needs.
2578 This->Found = *Pos;
2579 return true;
2580 }
2581
2582 // \brief Retrieve the identifier info found within the module
2583 // files.
2584 IdentifierInfo *getIdentifierInfo() const { return Found; }
2585 };
2586}
2587
2588
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002589ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
Douglas Gregor72a9ae12011-07-22 16:00:58 +00002590 ModuleKind Type) {
Douglas Gregor10bc00f2011-08-18 04:12:04 +00002591 switch(ReadASTCore(FileName, Type, /*ImportedBy=*/0)) {
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002592 case Failure: return Failure;
2593 case IgnorePCH: return IgnorePCH;
2594 case Success: break;
2595 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002596
2597 // Here comes stuff that we only do once the entire chain is loaded.
Douglas Gregor0cdd7982011-07-21 18:46:38 +00002598
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002599 // Check the predefines buffers.
Douglas Gregora7d445a2011-07-27 16:30:06 +00002600 if (!DisableValidation && Type != MK_Module && CheckPredefinesBuffers())
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002601 return IgnorePCH;
2602
2603 if (PP) {
2604 // Initialization of keywords and pragmas occurs before the
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002605 // AST file is read, so there may be some identifiers that were
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002606 // loaded into the IdentifierTable before we intercepted the
2607 // creation of identifiers. Iterate through the list of known
2608 // identifiers and determine whether we have to establish
2609 // preprocessor definitions or top-level identifier declaration
2610 // chains for those identifiers.
2611 //
2612 // We copy the IdentifierInfo pointers to a small vector first,
2613 // since de-serializing declarations or macro definitions can add
2614 // new entries into the identifier table, invalidating the
2615 // iterators.
Douglas Gregora7d445a2011-07-27 16:30:06 +00002616 //
2617 // FIXME: We need a lazier way to load this information, e.g., by marking
2618 // the identifier data as 'dirty', so that it will be looked up in the
2619 // AST file(s) if it is uttered in the source. This could save us some
2620 // module load time.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002621 SmallVector<IdentifierInfo *, 128> Identifiers;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002622 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
2623 IdEnd = PP->getIdentifierTable().end();
2624 Id != IdEnd; ++Id)
2625 Identifiers.push_back(Id->second);
Douglas Gregor46c03c32011-08-25 21:19:59 +00002626
2627 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2628 IdentifierLookupVisitor Visitor(Identifiers[I]->getName());
2629 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002630 }
2631 }
2632
2633 if (Context)
2634 InitializeContext(*Context);
2635
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002636 if (DeserializationListener)
2637 DeserializationListener->ReaderInitialized(this);
2638
Douglas Gregor414cb642010-11-30 05:23:00 +00002639 // If this AST file is a precompiled preamble, then set the main file ID of
2640 // the source manager to the file source file from which the preamble was
2641 // built. This is the only valid way to use a precompiled preamble.
Douglas Gregor72a9ae12011-07-22 16:00:58 +00002642 if (Type == MK_Preamble) {
Douglas Gregor31d375f2011-05-06 21:43:30 +00002643 if (OriginalFileID.isInvalid()) {
2644 SourceLocation Loc
2645 = SourceMgr.getLocation(FileMgr.getFile(getOriginalSourceFile()), 1, 1);
2646 if (Loc.isValid())
2647 OriginalFileID = SourceMgr.getDecomposedLoc(Loc).first;
Douglas Gregor414cb642010-11-30 05:23:00 +00002648 }
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002649 else {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00002650 OriginalFileID = FileID::get(ModuleMgr.getPrimaryModule().SLocEntryBaseID
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002651 + OriginalFileID.getOpaqueValue() - 1);
2652 }
2653
Douglas Gregor31d375f2011-05-06 21:43:30 +00002654 if (!OriginalFileID.isInvalid())
2655 SourceMgr.SetPreambleFileID(OriginalFileID);
Douglas Gregor414cb642010-11-30 05:23:00 +00002656 }
2657
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002658 return Success;
2659}
2660
Chris Lattner5f9e2722011-07-23 10:55:15 +00002661ASTReader::ASTReadResult ASTReader::ReadASTCore(StringRef FileName,
Douglas Gregor10bc00f2011-08-18 04:12:04 +00002662 ModuleKind Type,
2663 Module *ImportedBy) {
Douglas Gregorfac4ece2011-08-19 02:29:29 +00002664 Module *M;
2665 bool NewModule;
2666 std::string ErrorStr;
2667 llvm::tie(M, NewModule) = ModuleMgr.addModule(FileName, Type, ImportedBy,
2668 ErrorStr);
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002669
Douglas Gregorfac4ece2011-08-19 02:29:29 +00002670 if (!M) {
2671 // We couldn't load the module.
2672 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
2673 + ErrorStr;
2674 Error(Msg);
2675 return Failure;
2676 }
2677
2678 if (!NewModule) {
2679 // We've already loaded this module.
2680 return Success;
2681 }
2682
2683 // FIXME: This seems rather a hack. Should CurrentDir be part of the
2684 // module?
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002685 if (FileName != "-") {
2686 CurrentDir = llvm::sys::path::parent_path(FileName);
2687 if (CurrentDir.empty()) CurrentDir = ".";
2688 }
2689
Douglas Gregorfac4ece2011-08-19 02:29:29 +00002690 Module &F = *M;
Sebastian Redl9137a522010-07-16 17:50:48 +00002691 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002692 Stream.init(F.StreamFile);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002693 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Douglas Gregor8f1231b2011-07-22 06:10:01 +00002694
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002695 // Sniff for the signature.
2696 if (Stream.Read(8) != 'C' ||
2697 Stream.Read(8) != 'P' ||
2698 Stream.Read(8) != 'C' ||
2699 Stream.Read(8) != 'H') {
2700 Diag(diag::err_not_a_pch_file) << FileName;
2701 return Failure;
2702 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002703
Douglas Gregor2cf26342009-04-09 22:27:44 +00002704 while (!Stream.AtEndOfStream()) {
2705 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002706
Douglas Gregore1d918e2009-04-10 23:10:45 +00002707 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002708 Error("invalid record at top-level of AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002709 return Failure;
2710 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002711
2712 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregor668c1a42009-04-21 22:25:48 +00002713
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002714 // We only know the AST subblock ID.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002715 switch (BlockID) {
2716 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002717 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002718 Error("malformed BlockInfoBlock in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002719 return Failure;
2720 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002721 break;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002722 case AST_BLOCK_ID:
Sebastian Redl571db7f2010-08-18 23:56:56 +00002723 switch (ReadASTBlock(F)) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002724 case Success:
2725 break;
2726
2727 case Failure:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002728 return Failure;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002729
2730 case IgnorePCH:
Douglas Gregor2bec0412009-04-10 21:16:55 +00002731 // FIXME: We could consider reading through to the end of this
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002732 // AST block, skipping subblocks, to see if there are other
2733 // AST blocks elsewhere.
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002734
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002735 // FIXME: We can't clear loaded slocentries anymore.
2736 //SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002737
2738 // Remove the stat cache.
Sebastian Redl9137a522010-07-16 17:50:48 +00002739 if (F.StatCache)
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002740 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002741
Douglas Gregore1d918e2009-04-10 23:10:45 +00002742 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002743 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002744 break;
2745 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002746 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002747 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002748 return Failure;
2749 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002750 break;
2751 }
Mike Stump1eb44332009-09-09 15:08:12 +00002752 }
Douglas Gregor8f1231b2011-07-22 06:10:01 +00002753
Douglas Gregor72a9ae12011-07-22 16:00:58 +00002754 // Once read, set the Module bit base offset and update the size in
Douglas Gregor8f1231b2011-07-22 06:10:01 +00002755 // bits of all files we've seen.
2756 F.GlobalBitOffset = TotalModulesSizeInBits;
2757 TotalModulesSizeInBits += F.SizeInBits;
2758 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
Douglas Gregorc69a2922011-08-25 20:58:51 +00002759
2760 // Make sure that the files this module was built against are still available.
2761 if (!DisableValidation) {
2762 switch(validateFileEntries(*M)) {
2763 case Failure: return Failure;
2764 case IgnorePCH: return IgnorePCH;
2765 case Success: break;
2766 }
2767 }
Douglas Gregorf249bf32011-08-25 21:09:44 +00002768
2769 // Preload SLocEntries.
2770 for (unsigned I = 0, N = M->PreloadSLocEntries.size(); I != N; ++I) {
2771 int Index = int(M->PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
2772 ASTReadResult Result = ReadSLocEntryRecord(Index);
2773 if (Result != Success)
2774 return Failure;
2775 }
2776
Douglas Gregorc69a2922011-08-25 20:58:51 +00002777
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002778 return Success;
2779}
2780
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002781void ASTReader::setPreprocessor(Preprocessor &pp) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002782 PP = &pp;
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002783
2784 if (unsigned N = getTotalNumPreprocessedEntities()) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002785 if (!PP->getPreprocessingRecord())
Douglas Gregordca8ee82011-05-06 16:33:08 +00002786 PP->createPreprocessingRecord(true);
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002787 PP->getPreprocessingRecord()->SetExternalSource(*this);
2788 PP->getPreprocessingRecord()->allocateLoadedEntities(N);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002789 }
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002790
2791 PP->getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor0cdd7982011-07-21 18:46:38 +00002792 PP->getHeaderSearchInfo().SetExternalSource(this);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002793}
2794
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002795void ASTReader::InitializeContext(ASTContext &Ctx) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002796 Context = &Ctx;
2797 assert(Context && "Passed null context!");
Douglas Gregor2ea054f2011-08-26 22:04:51 +00002798
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002799 assert(PP && "Forgot to set Preprocessor ?");
2800 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor88a35862010-01-04 19:18:44 +00002801 PP->setExternalSource(this);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002802
Douglas Gregor0d95f772011-08-24 19:03:07 +00002803 // If we have any update blocks for the TU waiting, we have to add
2804 // them before we deserialize anything.
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002805 TranslationUnitDecl *TU = Ctx.getTranslationUnitDecl();
Douglas Gregor0d95f772011-08-24 19:03:07 +00002806 for (ModuleIterator M = ModuleMgr.begin(), MEnd = ModuleMgr.end();
2807 M != MEnd; ++M) {
2808 Module::DeclContextInfosMap::iterator DCU
2809 = (*M)->DeclContextInfos.find(0);
2810 if (DCU != (*M)->DeclContextInfos.end()) {
2811 // Insertion could invalidate map, so grab value first.
2812 DeclContextInfo Info = DCU->second;
2813 (*M)->DeclContextInfos.erase(DCU);
2814 (*M)->DeclContextInfos[TU] = Info;
2815 }
Douglas Gregor3747ee72010-10-01 01:18:02 +00002816 }
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002817
2818 // If there's a listener, notify them that we "read" the translation unit.
2819 if (DeserializationListener)
2820 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID, TU);
Douglas Gregor3747ee72010-10-01 01:18:02 +00002821
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002822 // Make sure we load the declaration update records for the translation unit,
2823 // if there are any.
2824 loadDeclUpdateRecords(PREDEF_DECL_TRANSLATION_UNIT_ID, TU);
2825
2826 // Note that the translation unit has external lexical and visible storage.
2827 TU->setHasExternalLexicalStorage(true);
2828 TU->setHasExternalVisibleStorage(true);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002829
Douglas Gregor5f957282011-08-11 22:18:49 +00002830 // FIXME: Find a better way to deal with collisions between these
2831 // built-in types. Right now, we just ignore the problem.
2832
2833 // Load the special types.
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002834 if (Context->getBuiltinVaListType().isNull()) {
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002835 Context->setBuiltinVaListType(
2836 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
Douglas Gregor5f957282011-08-11 22:18:49 +00002837 }
2838
Douglas Gregor5f957282011-08-11 22:18:49 +00002839 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL]) {
2840 if (Context->ObjCProtoType.isNull())
Douglas Gregor30403a62011-08-11 22:04:35 +00002841 Context->ObjCProtoType = GetType(Proto);
Douglas Gregor5f957282011-08-11 22:18:49 +00002842 }
2843
Douglas Gregor5f957282011-08-11 22:18:49 +00002844 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
2845 if (!Context->CFConstantStringTypeDecl)
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002846 Context->setCFConstantStringType(GetType(String));
Douglas Gregor5f957282011-08-11 22:18:49 +00002847 }
2848
2849 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
2850 QualType FileType = GetType(File);
2851 if (FileType.isNull()) {
2852 Error("FILE type is NULL");
2853 return;
2854 }
2855
2856 if (!Context->FILEDecl) {
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002857 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
2858 Context->setFILEDecl(Typedef->getDecl());
2859 else {
2860 const TagType *Tag = FileType->getAs<TagType>();
2861 if (!Tag) {
2862 Error("Invalid FILE type in AST file");
2863 return;
2864 }
2865 Context->setFILEDecl(Tag->getDecl());
2866 }
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002867 }
Douglas Gregor5f957282011-08-11 22:18:49 +00002868 }
2869
2870 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
2871 QualType Jmp_bufType = GetType(Jmp_buf);
2872 if (Jmp_bufType.isNull()) {
2873 Error("jmp_buf type is NULL");
2874 return;
2875 }
2876
2877 if (!Context->jmp_bufDecl) {
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002878 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
2879 Context->setjmp_bufDecl(Typedef->getDecl());
2880 else {
2881 const TagType *Tag = Jmp_bufType->getAs<TagType>();
2882 if (!Tag) {
2883 Error("Invalid jmp_buf type in AST file");
2884 return;
2885 }
2886 Context->setjmp_bufDecl(Tag->getDecl());
2887 }
Mike Stump782fa302009-07-28 02:25:19 +00002888 }
Douglas Gregor5f957282011-08-11 22:18:49 +00002889 }
2890
2891 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
2892 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
2893 if (Sigjmp_bufType.isNull()) {
2894 Error("sigjmp_buf type is NULL");
2895 return;
2896 }
2897
2898 if (!Context->sigjmp_bufDecl) {
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002899 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
2900 Context->setsigjmp_bufDecl(Typedef->getDecl());
2901 else {
2902 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
2903 assert(Tag && "Invalid sigjmp_buf type in AST file");
2904 Context->setsigjmp_bufDecl(Tag->getDecl());
2905 }
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002906 }
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002907 }
Richard Smithad762fc2011-04-14 22:09:26 +00002908
Douglas Gregor5f957282011-08-11 22:18:49 +00002909 if (unsigned ObjCIdRedef
2910 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
2911 if (Context->ObjCIdRedefinitionType.isNull())
2912 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
2913 }
2914
2915 if (unsigned ObjCClassRedef
2916 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
2917 if (Context->ObjCClassRedefinitionType.isNull())
2918 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
2919 }
2920
2921 if (unsigned ObjCSelRedef
2922 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
2923 if (Context->ObjCSelRedefinitionType.isNull())
2924 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
2925 }
2926
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002927 ReadPragmaDiagnosticMappings(Context->getDiagnostics());
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002928
2929 // If there were any CUDA special declarations, deserialize them.
2930 if (!CUDASpecialDeclRefs.empty()) {
2931 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
2932 Context->setcudaConfigureCallDecl(
2933 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
2934 }
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002935}
2936
Douglas Gregorb64c1932009-05-12 01:31:05 +00002937/// \brief Retrieve the name of the original source file name
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002938/// directly from the AST file, without actually loading the AST
Douglas Gregorb64c1932009-05-12 01:31:05 +00002939/// file.
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002940std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00002941 FileManager &FileMgr,
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00002942 Diagnostic &Diags) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002943 // Open the AST file.
Douglas Gregorb64c1932009-05-12 01:31:05 +00002944 std::string ErrStr;
2945 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner39b49bc2010-11-23 08:35:12 +00002946 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregorb64c1932009-05-12 01:31:05 +00002947 if (!Buffer) {
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00002948 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002949 return std::string();
2950 }
2951
2952 // Initialize the stream
2953 llvm::BitstreamReader StreamFile;
2954 llvm::BitstreamCursor Stream;
Mike Stump1eb44332009-09-09 15:08:12 +00002955 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregorb64c1932009-05-12 01:31:05 +00002956 (const unsigned char *)Buffer->getBufferEnd());
2957 Stream.init(StreamFile);
2958
2959 // Sniff for the signature.
2960 if (Stream.Read(8) != 'C' ||
2961 Stream.Read(8) != 'P' ||
2962 Stream.Read(8) != 'C' ||
2963 Stream.Read(8) != 'H') {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002964 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002965 return std::string();
2966 }
2967
2968 RecordData Record;
2969 while (!Stream.AtEndOfStream()) {
2970 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002971
Douglas Gregorb64c1932009-05-12 01:31:05 +00002972 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2973 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump1eb44332009-09-09 15:08:12 +00002974
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002975 // We only know the AST subblock ID.
Douglas Gregorb64c1932009-05-12 01:31:05 +00002976 switch (BlockID) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002977 case AST_BLOCK_ID:
2978 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002979 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002980 return std::string();
2981 }
2982 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002983
Douglas Gregorb64c1932009-05-12 01:31:05 +00002984 default:
2985 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002986 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002987 return std::string();
2988 }
2989 break;
2990 }
2991 continue;
2992 }
2993
2994 if (Code == llvm::bitc::END_BLOCK) {
2995 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002996 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002997 return std::string();
2998 }
2999 continue;
3000 }
3001
3002 if (Code == llvm::bitc::DEFINE_ABBREV) {
3003 Stream.ReadAbbrevRecord();
3004 continue;
3005 }
3006
3007 Record.clear();
3008 const char *BlobStart = 0;
3009 unsigned BlobLen = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003010 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003011 == ORIGINAL_FILE_NAME)
Douglas Gregorb64c1932009-05-12 01:31:05 +00003012 return std::string(BlobStart, BlobLen);
Mike Stump1eb44332009-09-09 15:08:12 +00003013 }
Douglas Gregorb64c1932009-05-12 01:31:05 +00003014
3015 return std::string();
3016}
3017
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003018/// \brief Parse the record that corresponds to a LangOptions data
3019/// structure.
3020///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003021/// This routine parses the language options from the AST file and then gives
3022/// them to the AST listener if one is set.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003023///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003024/// \returns true if the listener deems the file unacceptable, false otherwise.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003025bool ASTReader::ParseLanguageOptions(
Chris Lattner5f9e2722011-07-23 10:55:15 +00003026 const SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003027 if (Listener) {
3028 LangOptions LangOpts;
Mike Stump1eb44332009-09-09 15:08:12 +00003029
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003030 #define PARSE_LANGOPT(Option) \
3031 LangOpts.Option = Record[Idx]; \
3032 ++Idx
Mike Stump1eb44332009-09-09 15:08:12 +00003033
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003034 unsigned Idx = 0;
3035 PARSE_LANGOPT(Trigraphs);
3036 PARSE_LANGOPT(BCPLComment);
3037 PARSE_LANGOPT(DollarIdents);
3038 PARSE_LANGOPT(AsmPreprocessor);
3039 PARSE_LANGOPT(GNUMode);
Chandler Carrutheb5d7b72010-04-17 20:17:31 +00003040 PARSE_LANGOPT(GNUKeywords);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003041 PARSE_LANGOPT(ImplicitInt);
3042 PARSE_LANGOPT(Digraphs);
3043 PARSE_LANGOPT(HexFloats);
3044 PARSE_LANGOPT(C99);
Peter Collingbourne7e7fbd02011-04-15 00:35:23 +00003045 PARSE_LANGOPT(C1X);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003046 PARSE_LANGOPT(Microsoft);
3047 PARSE_LANGOPT(CPlusPlus);
3048 PARSE_LANGOPT(CPlusPlus0x);
3049 PARSE_LANGOPT(CXXOperatorNames);
3050 PARSE_LANGOPT(ObjC1);
3051 PARSE_LANGOPT(ObjC2);
3052 PARSE_LANGOPT(ObjCNonFragileABI);
Fariborz Jahanian412e7982010-02-09 19:31:38 +00003053 PARSE_LANGOPT(ObjCNonFragileABI2);
Fariborz Jahanianf84109e2011-01-07 18:59:25 +00003054 PARSE_LANGOPT(AppleKext);
Ted Kremenekc32647d2010-12-23 21:35:43 +00003055 PARSE_LANGOPT(ObjCDefaultSynthProperties);
Douglas Gregor74da19f2011-06-14 23:20:43 +00003056 PARSE_LANGOPT(ObjCInferRelatedResultType);
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +00003057 PARSE_LANGOPT(NoConstantCFStrings);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003058 PARSE_LANGOPT(PascalStrings);
3059 PARSE_LANGOPT(WritableStrings);
3060 PARSE_LANGOPT(LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +00003061 PARSE_LANGOPT(AltiVec);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003062 PARSE_LANGOPT(Exceptions);
Anders Carlssonda4b7cf2011-02-19 23:53:54 +00003063 PARSE_LANGOPT(ObjCExceptions);
Anders Carlsson7da99b02011-02-23 03:04:54 +00003064 PARSE_LANGOPT(CXXExceptions);
3065 PARSE_LANGOPT(SjLjExceptions);
Douglas Gregor6f755502011-02-01 15:15:22 +00003066 PARSE_LANGOPT(MSBitfields);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003067 PARSE_LANGOPT(NeXTRuntime);
3068 PARSE_LANGOPT(Freestanding);
3069 PARSE_LANGOPT(NoBuiltin);
3070 PARSE_LANGOPT(ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +00003071 PARSE_LANGOPT(POSIXThreads);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003072 PARSE_LANGOPT(Blocks);
3073 PARSE_LANGOPT(EmitAllDecls);
3074 PARSE_LANGOPT(MathErrno);
Chris Lattnera4d71452010-06-26 21:25:03 +00003075 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
3076 Record[Idx++]);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003077 PARSE_LANGOPT(HeinousExtensions);
3078 PARSE_LANGOPT(Optimize);
3079 PARSE_LANGOPT(OptimizeSize);
3080 PARSE_LANGOPT(Static);
3081 PARSE_LANGOPT(PICLevel);
3082 PARSE_LANGOPT(GNUInline);
3083 PARSE_LANGOPT(NoInline);
Chandler Carruth0d2d1bc2011-04-23 20:05:38 +00003084 PARSE_LANGOPT(Deprecated);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003085 PARSE_LANGOPT(AccessControl);
3086 PARSE_LANGOPT(CharIsSigned);
John Thompsona6fda122009-11-05 20:14:16 +00003087 PARSE_LANGOPT(ShortWChar);
Argyrios Kyrtzidisb1bdced2011-01-15 02:56:16 +00003088 PARSE_LANGOPT(ShortEnums);
Chris Lattnera4d71452010-06-26 21:25:03 +00003089 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
John McCall1fb0caa2010-10-22 21:05:15 +00003090 LangOpts.setVisibilityMode((Visibility)Record[Idx++]);
Daniel Dunbarab8e2812009-09-21 04:16:19 +00003091 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
Chris Lattnera4d71452010-06-26 21:25:03 +00003092 Record[Idx++]);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003093 PARSE_LANGOPT(InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +00003094 PARSE_LANGOPT(OpenCL);
Peter Collingbourne08a53262010-12-01 19:14:57 +00003095 PARSE_LANGOPT(CUDA);
Mike Stump9c276ae2009-12-12 01:27:46 +00003096 PARSE_LANGOPT(CatchUndefined);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003097 PARSE_LANGOPT(DefaultFPContract);
Roman Divacky1c51b1c2011-03-01 17:36:40 +00003098 PARSE_LANGOPT(ElideConstructors);
3099 PARSE_LANGOPT(SpellChecking);
Roman Divackycfe9af22011-03-01 17:40:53 +00003100 PARSE_LANGOPT(MRTD);
John McCallf85e1932011-06-15 23:02:42 +00003101 PARSE_LANGOPT(ObjCAutoRefCount);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003102 #undef PARSE_LANGOPT
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003103
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003104 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003105 }
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003106
3107 return false;
3108}
3109
Douglas Gregorb95cd092011-08-25 18:03:05 +00003110namespace {
3111 /// \brief Visitor used by ASTReader::ReadPreprocessedEntities() to load
3112 /// all of the preprocessed entities within a module.
3113 class ReadPreprocessedEntitiesVisitor {
3114 ASTReader &Reader;
3115
3116 public:
3117 explicit ReadPreprocessedEntitiesVisitor(ASTReader &Reader)
3118 : Reader(Reader) { }
3119
3120 static bool visit(Module &M, bool Preorder, void *UserData) {
3121 if (Preorder)
3122 return false;
3123
3124 ReadPreprocessedEntitiesVisitor *This
3125 = static_cast<ReadPreprocessedEntitiesVisitor *>(UserData);
3126
3127 if (!M.PreprocessorDetailCursor.getBitStreamReader())
3128 return false;
3129
3130 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
3131 M.PreprocessorDetailCursor.JumpToBit(M.PreprocessorDetailStartOffset);
3132 while (This->Reader.LoadPreprocessedEntity(M)) { }
3133 return false;
3134 }
3135 };
3136}
Douglas Gregor4800a5c2011-02-08 21:58:10 +00003137
Douglas Gregorb95cd092011-08-25 18:03:05 +00003138void ASTReader::ReadPreprocessedEntities() {
3139 ReadPreprocessedEntitiesVisitor Visitor(*this);
3140 ModuleMgr.visitDepthFirst(&ReadPreprocessedEntitiesVisitor::visit, &Visitor);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00003141}
3142
Douglas Gregor0a480292011-02-11 19:46:30 +00003143PreprocessedEntity *ASTReader::ReadPreprocessedEntityAtOffset(uint64_t Offset) {
Douglas Gregor8f1231b2011-07-22 06:10:01 +00003144 RecordLocation Loc = getLocalBitOffset(Offset);
Douglas Gregor89d99802010-11-30 06:16:57 +00003145
Douglas Gregor4800a5c2011-02-08 21:58:10 +00003146 // Keep track of where we are in the stream, then jump back there
3147 // after reading this entity.
Douglas Gregor8f1231b2011-07-22 06:10:01 +00003148 SavedStreamPosition SavedPosition(Loc.F->PreprocessorDetailCursor);
3149 Loc.F->PreprocessorDetailCursor.JumpToBit(Loc.Offset);
3150 return LoadPreprocessedEntity(*Loc.F);
Douglas Gregor89d99802010-11-30 06:16:57 +00003151}
3152
Douglas Gregord10a3812011-08-25 18:14:34 +00003153namespace {
3154 /// \brief Visitor used to search for information about a header file.
3155 class HeaderFileInfoVisitor {
3156 ASTReader &Reader;
3157 const FileEntry *FE;
3158
3159 llvm::Optional<HeaderFileInfo> HFI;
3160
3161 public:
3162 HeaderFileInfoVisitor(ASTReader &Reader, const FileEntry *FE)
3163 : Reader(Reader), FE(FE) { }
3164
3165 static bool visit(Module &M, void *UserData) {
3166 HeaderFileInfoVisitor *This
3167 = static_cast<HeaderFileInfoVisitor *>(UserData);
3168
3169 HeaderFileInfoTrait Trait(This->Reader, M,
3170 &This->Reader.getPreprocessor().getHeaderSearchInfo(),
3171 M.HeaderFileFrameworkStrings,
3172 This->FE->getName());
3173
3174 HeaderFileInfoLookupTable *Table
3175 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
3176 if (!Table)
3177 return false;
3178
3179 // Look in the on-disk hash table for an entry for this file name.
3180 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE->getName(),
3181 &Trait);
3182 if (Pos == Table->end())
3183 return false;
3184
3185 This->HFI = *Pos;
3186 return true;
3187 }
3188
3189 llvm::Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
3190 };
3191}
3192
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003193HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Douglas Gregord10a3812011-08-25 18:14:34 +00003194 HeaderFileInfoVisitor Visitor(*this, FE);
3195 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
3196 if (llvm::Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003197 if (Listener)
Douglas Gregord10a3812011-08-25 18:14:34 +00003198 Listener->ReadHeaderFileInfo(*HFI, FE->getUID());
3199 return *HFI;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003200 }
3201
3202 return HeaderFileInfo();
3203}
3204
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003205void ASTReader::ReadPragmaDiagnosticMappings(Diagnostic &Diag) {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00003206 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
3207 Module &F = *(*I);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00003208 unsigned Idx = 0;
3209 while (Idx < F.PragmaDiagMappings.size()) {
3210 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
3211 while (1) {
3212 assert(Idx < F.PragmaDiagMappings.size() &&
3213 "Invalid data, didn't find '-1' marking end of diag/map pairs");
3214 if (Idx >= F.PragmaDiagMappings.size()) {
3215 break; // Something is messed up but at least avoid infinite loop in
3216 // release build.
3217 }
3218 unsigned DiagID = F.PragmaDiagMappings[Idx++];
3219 if (DiagID == (unsigned)-1) {
3220 break; // no more diag/map pairs for this location.
3221 }
3222 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++];
3223 Diag.setDiagnosticMapping(DiagID, Map, Loc);
3224 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003225 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00003226 }
3227}
3228
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003229/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003230ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Douglas Gregora119da02011-08-02 16:26:37 +00003231 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
Jonathan D. Turnere9b76c12011-07-20 21:31:32 +00003232 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
Douglas Gregor1e849b62011-07-29 00:21:44 +00003233 Module *M = I->second;
Douglas Gregore3605012011-08-02 18:32:54 +00003234 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003235}
3236
3237/// \brief Read and return the type with the given index..
Douglas Gregor2cf26342009-04-09 22:27:44 +00003238///
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003239/// The index is the type ID, shifted and minus the number of predefs. This
3240/// routine actually reads the record corresponding to the type at the given
3241/// location. It is a helper routine for GetType, which deals with reading type
3242/// IDs.
Douglas Gregor393f2492011-07-22 00:38:23 +00003243QualType ASTReader::readTypeRecord(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003244 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00003245 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00003246
Douglas Gregor0b748912009-04-14 21:18:50 +00003247 // Keep track of where we are in the stream, then jump back there
3248 // after reading this type.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003249 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregor0b748912009-04-14 21:18:50 +00003250
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003251 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redl27372b42010-08-11 18:52:41 +00003252
Douglas Gregord89275b2009-07-06 18:54:52 +00003253 // Note that we are loading a type record.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00003254 Deserializing AType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00003255
Douglas Gregor393f2492011-07-22 00:38:23 +00003256 unsigned Idx = 0;
Sebastian Redlc3632732010-10-05 15:59:54 +00003257 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003258 RecordData Record;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003259 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003260 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
3261 case TYPE_EXT_QUAL: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003262 if (Record.size() != 2) {
3263 Error("Incorrect encoding of extended qualifier type");
3264 return QualType();
3265 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003266 QualType Base = readType(*Loc.F, Record, Idx);
3267 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
John McCall0953e762009-09-24 19:53:00 +00003268 return Context->getQualifiedType(Base, Quals);
Douglas Gregor6d473962009-04-15 22:00:08 +00003269 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003270
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003271 case TYPE_COMPLEX: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003272 if (Record.size() != 1) {
3273 Error("Incorrect encoding of complex type");
3274 return QualType();
3275 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003276 QualType ElemType = readType(*Loc.F, Record, Idx);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003277 return Context->getComplexType(ElemType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003278 }
3279
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003280 case TYPE_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003281 if (Record.size() != 1) {
3282 Error("Incorrect encoding of pointer type");
3283 return QualType();
3284 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003285 QualType PointeeType = readType(*Loc.F, Record, Idx);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003286 return Context->getPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003287 }
3288
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003289 case TYPE_BLOCK_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003290 if (Record.size() != 1) {
3291 Error("Incorrect encoding of block pointer type");
3292 return QualType();
3293 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003294 QualType PointeeType = readType(*Loc.F, Record, Idx);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003295 return Context->getBlockPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003296 }
3297
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003298 case TYPE_LVALUE_REFERENCE: {
Richard Smithdf1550f2011-04-12 10:38:03 +00003299 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003300 Error("Incorrect encoding of lvalue reference type");
3301 return QualType();
3302 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003303 QualType PointeeType = readType(*Loc.F, Record, Idx);
Richard Smithdf1550f2011-04-12 10:38:03 +00003304 return Context->getLValueReferenceType(PointeeType, Record[1]);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003305 }
3306
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003307 case TYPE_RVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003308 if (Record.size() != 1) {
3309 Error("Incorrect encoding of rvalue reference type");
3310 return QualType();
3311 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003312 QualType PointeeType = readType(*Loc.F, Record, Idx);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003313 return Context->getRValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003314 }
3315
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003316 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidis240437b2010-07-02 11:55:15 +00003317 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003318 Error("Incorrect encoding of member pointer type");
3319 return QualType();
3320 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003321 QualType PointeeType = readType(*Loc.F, Record, Idx);
3322 QualType ClassType = readType(*Loc.F, Record, Idx);
Douglas Gregor1ab55e92010-12-10 17:03:06 +00003323 if (PointeeType.isNull() || ClassType.isNull())
3324 return QualType();
3325
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003326 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003327 }
3328
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003329 case TYPE_CONSTANT_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003330 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003331 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3332 unsigned IndexTypeQuals = Record[2];
3333 unsigned Idx = 3;
3334 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003335 return Context->getConstantArrayType(ElementType, Size,
3336 ASM, IndexTypeQuals);
3337 }
3338
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003339 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003340 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003341 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3342 unsigned IndexTypeQuals = Record[2];
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003343 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003344 }
3345
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003346 case TYPE_VARIABLE_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003347 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregor0b748912009-04-14 21:18:50 +00003348 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3349 unsigned IndexTypeQuals = Record[2];
Sebastian Redlc3632732010-10-05 15:59:54 +00003350 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
3351 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
3352 return Context->getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003353 ASM, IndexTypeQuals,
3354 SourceRange(LBLoc, RBLoc));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003355 }
3356
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003357 case TYPE_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00003358 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003359 Error("incorrect encoding of vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003360 return QualType();
3361 }
3362
Douglas Gregor393f2492011-07-22 00:38:23 +00003363 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003364 unsigned NumElements = Record[1];
Bob Wilsone86d78c2010-11-10 21:56:12 +00003365 unsigned VecKind = Record[2];
Chris Lattner788b0fd2010-06-23 06:00:24 +00003366 return Context->getVectorType(ElementType, NumElements,
Bob Wilsone86d78c2010-11-10 21:56:12 +00003367 (VectorType::VectorKind)VecKind);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003368 }
3369
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003370 case TYPE_EXT_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00003371 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003372 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003373 return QualType();
3374 }
3375
Douglas Gregor393f2492011-07-22 00:38:23 +00003376 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003377 unsigned NumElements = Record[1];
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003378 return Context->getExtVectorType(ElementType, NumElements);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003379 }
3380
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003381 case TYPE_FUNCTION_NO_PROTO: {
John McCallf85e1932011-06-15 23:02:42 +00003382 if (Record.size() != 6) {
Douglas Gregora02b1472009-04-28 21:53:25 +00003383 Error("incorrect encoding of no-proto function type");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003384 return QualType();
3385 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003386 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCallf85e1932011-06-15 23:02:42 +00003387 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
3388 (CallingConv)Record[4], Record[5]);
Rafael Espindola264ba482010-03-30 20:24:48 +00003389 return Context->getFunctionNoProtoType(ResultType, Info);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003390 }
3391
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003392 case TYPE_FUNCTION_PROTO: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003393 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCalle23cf432010-12-14 08:05:40 +00003394
3395 FunctionProtoType::ExtProtoInfo EPI;
3396 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
Eli Friedmana49218e2011-04-09 08:18:08 +00003397 /*hasregparm*/ Record[2],
3398 /*regparm*/ Record[3],
John McCallf85e1932011-06-15 23:02:42 +00003399 static_cast<CallingConv>(Record[4]),
3400 /*produces*/ Record[5]);
John McCalle23cf432010-12-14 08:05:40 +00003401
John McCallf85e1932011-06-15 23:02:42 +00003402 unsigned Idx = 6;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003403 unsigned NumParams = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00003404 SmallVector<QualType, 16> ParamTypes;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003405 for (unsigned I = 0; I != NumParams; ++I)
Douglas Gregor393f2492011-07-22 00:38:23 +00003406 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
John McCalle23cf432010-12-14 08:05:40 +00003407
3408 EPI.Variadic = Record[Idx++];
3409 EPI.TypeQuals = Record[Idx++];
Douglas Gregorc938c162011-01-26 05:01:58 +00003410 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
Sebastian Redl60618fa2011-03-12 11:50:43 +00003411 ExceptionSpecificationType EST =
3412 static_cast<ExceptionSpecificationType>(Record[Idx++]);
3413 EPI.ExceptionSpecType = EST;
3414 if (EST == EST_Dynamic) {
3415 EPI.NumExceptions = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00003416 SmallVector<QualType, 2> Exceptions;
Sebastian Redl60618fa2011-03-12 11:50:43 +00003417 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
Douglas Gregor393f2492011-07-22 00:38:23 +00003418 Exceptions.push_back(readType(*Loc.F, Record, Idx));
Sebastian Redl60618fa2011-03-12 11:50:43 +00003419 EPI.Exceptions = Exceptions.data();
3420 } else if (EST == EST_ComputedNoexcept) {
3421 EPI.NoexceptExpr = ReadExpr(*Loc.F);
3422 }
Jay Foadbeaaccd2009-05-21 09:52:38 +00003423 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
John McCalle23cf432010-12-14 08:05:40 +00003424 EPI);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003425 }
3426
Douglas Gregor409448c2011-07-21 22:35:25 +00003427 case TYPE_UNRESOLVED_USING: {
3428 unsigned Idx = 0;
John McCalled976492009-12-04 22:46:56 +00003429 return Context->getTypeDeclType(
Douglas Gregor409448c2011-07-21 22:35:25 +00003430 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
3431 }
3432
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003433 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003434 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003435 Error("incorrect encoding of typedef type");
3436 return QualType();
3437 }
Douglas Gregor409448c2011-07-21 22:35:25 +00003438 unsigned Idx = 0;
3439 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00003440 QualType Canonical = readType(*Loc.F, Record, Idx);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00003441 if (!Canonical.isNull())
3442 Canonical = Context->getCanonicalType(Canonical);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003443 return Context->getTypedefType(Decl, Canonical);
3444 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003445
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003446 case TYPE_TYPEOF_EXPR:
Sebastian Redlc3632732010-10-05 15:59:54 +00003447 return Context->getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003448
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003449 case TYPE_TYPEOF: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003450 if (Record.size() != 1) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003451 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003452 return QualType();
3453 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003454 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003455 return Context->getTypeOfType(UnderlyingType);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003456 }
Mike Stump1eb44332009-09-09 15:08:12 +00003457
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003458 case TYPE_DECLTYPE:
Sebastian Redlc3632732010-10-05 15:59:54 +00003459 return Context->getDecltypeType(ReadExpr(*Loc.F));
Anders Carlsson395b4752009-06-24 19:06:50 +00003460
Sean Huntca63c202011-05-24 22:41:36 +00003461 case TYPE_UNARY_TRANSFORM: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003462 QualType BaseType = readType(*Loc.F, Record, Idx);
3463 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Sean Huntca63c202011-05-24 22:41:36 +00003464 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
3465 return Context->getUnaryTransformType(BaseType, UnderlyingType, UKind);
3466 }
3467
Richard Smith34b41d92011-02-20 03:19:35 +00003468 case TYPE_AUTO:
Douglas Gregor393f2492011-07-22 00:38:23 +00003469 return Context->getAutoType(readType(*Loc.F, Record, Idx));
Richard Smith34b41d92011-02-20 03:19:35 +00003470
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003471 case TYPE_RECORD: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003472 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003473 Error("incorrect encoding of record type");
3474 return QualType();
3475 }
Douglas Gregor409448c2011-07-21 22:35:25 +00003476 unsigned Idx = 0;
3477 bool IsDependent = Record[Idx++];
3478 QualType T
3479 = Context->getRecordType(ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx));
John McCallf4c73712011-01-19 06:33:43 +00003480 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003481 return T;
3482 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003483
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003484 case TYPE_ENUM: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003485 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003486 Error("incorrect encoding of enum type");
3487 return QualType();
3488 }
Douglas Gregor409448c2011-07-21 22:35:25 +00003489 unsigned Idx = 0;
3490 bool IsDependent = Record[Idx++];
3491 QualType T
3492 = Context->getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
John McCallf4c73712011-01-19 06:33:43 +00003493 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003494 return T;
3495 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003496
John McCall9d156a72011-01-06 01:58:22 +00003497 case TYPE_ATTRIBUTED: {
3498 if (Record.size() != 3) {
3499 Error("incorrect encoding of attributed type");
3500 return QualType();
3501 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003502 QualType modifiedType = readType(*Loc.F, Record, Idx);
3503 QualType equivalentType = readType(*Loc.F, Record, Idx);
John McCall9d156a72011-01-06 01:58:22 +00003504 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
3505 return Context->getAttributedType(kind, modifiedType, equivalentType);
3506 }
3507
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003508 case TYPE_PAREN: {
3509 if (Record.size() != 1) {
3510 Error("incorrect encoding of paren type");
3511 return QualType();
3512 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003513 QualType InnerType = readType(*Loc.F, Record, Idx);
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003514 return Context->getParenType(InnerType);
3515 }
3516
Douglas Gregor7536dd52010-12-20 02:24:11 +00003517 case TYPE_PACK_EXPANSION: {
Douglas Gregorf9997a02011-02-01 15:24:58 +00003518 if (Record.size() != 2) {
Douglas Gregor7536dd52010-12-20 02:24:11 +00003519 Error("incorrect encoding of pack expansion type");
3520 return QualType();
3521 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003522 QualType Pattern = readType(*Loc.F, Record, Idx);
Douglas Gregor7536dd52010-12-20 02:24:11 +00003523 if (Pattern.isNull())
3524 return QualType();
Douglas Gregorcded4f62011-01-14 17:04:44 +00003525 llvm::Optional<unsigned> NumExpansions;
3526 if (Record[1])
3527 NumExpansions = Record[1] - 1;
3528 return Context->getPackExpansionType(Pattern, NumExpansions);
Douglas Gregor7536dd52010-12-20 02:24:11 +00003529 }
3530
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003531 case TYPE_ELABORATED: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003532 unsigned Idx = 0;
3533 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00003534 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00003535 QualType NamedType = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003536 return Context->getElaboratedType(Keyword, NNS, NamedType);
John McCall7da24312009-09-05 00:15:47 +00003537 }
3538
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003539 case TYPE_OBJC_INTERFACE: {
Chris Lattnerc6fa4452009-04-22 06:45:28 +00003540 unsigned Idx = 0;
Douglas Gregor409448c2011-07-21 22:35:25 +00003541 ObjCInterfaceDecl *ItfD
3542 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
John McCallc12c5bb2010-05-15 11:32:37 +00003543 return Context->getObjCInterfaceType(ItfD);
3544 }
3545
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003546 case TYPE_OBJC_OBJECT: {
John McCallc12c5bb2010-05-15 11:32:37 +00003547 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00003548 QualType Base = readType(*Loc.F, Record, Idx);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00003549 unsigned NumProtos = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00003550 SmallVector<ObjCProtocolDecl*, 4> Protos;
Chris Lattnerc6fa4452009-04-22 06:45:28 +00003551 for (unsigned I = 0; I != NumProtos; ++I)
Douglas Gregor409448c2011-07-21 22:35:25 +00003552 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
Michael J. Spencer20249a12010-10-21 03:16:25 +00003553 return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00003554 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003555
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003556 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00003557 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00003558 QualType Pointee = readType(*Loc.F, Record, Idx);
John McCallc12c5bb2010-05-15 11:32:37 +00003559 return Context->getObjCObjectPointerType(Pointee);
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00003560 }
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +00003561
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003562 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCall49a832b2009-10-18 09:09:24 +00003563 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00003564 QualType Parm = readType(*Loc.F, Record, Idx);
3565 QualType Replacement = readType(*Loc.F, Record, Idx);
John McCall49a832b2009-10-18 09:09:24 +00003566 return
3567 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
3568 Replacement);
3569 }
John McCall3cb0ebd2010-03-10 03:28:59 +00003570
Douglas Gregorc3069d62011-01-14 02:55:32 +00003571 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
3572 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00003573 QualType Parm = readType(*Loc.F, Record, Idx);
Douglas Gregorc3069d62011-01-14 02:55:32 +00003574 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
3575 return Context->getSubstTemplateTypeParmPackType(
3576 cast<TemplateTypeParmType>(Parm),
3577 ArgPack);
3578 }
3579
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003580 case TYPE_INJECTED_CLASS_NAME: {
Douglas Gregor409448c2011-07-21 22:35:25 +00003581 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00003582 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00003583 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003584 // for AST reading, too much interdependencies.
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00003585 return
3586 QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCall3cb0ebd2010-03-10 03:28:59 +00003587 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003588
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003589 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003590 unsigned Idx = 0;
3591 unsigned Depth = Record[Idx++];
3592 unsigned Index = Record[Idx++];
3593 bool Pack = Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00003594 TemplateTypeParmDecl *D
3595 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
Chandler Carruth4fb86f82011-05-01 00:51:33 +00003596 return Context->getTemplateTypeParmType(Depth, Index, Pack, D);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003597 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003598
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003599 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00003600 unsigned Idx = 0;
3601 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00003602 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor95eab172011-07-28 20:55:49 +00003603 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00003604 QualType Canon = readType(*Loc.F, Record, Idx);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00003605 if (!Canon.isNull())
3606 Canon = Context->getCanonicalType(Canon);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +00003607 return Context->getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00003608 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003609
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003610 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003611 unsigned Idx = 0;
3612 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00003613 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor95eab172011-07-28 20:55:49 +00003614 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003615 unsigned NumArgs = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00003616 SmallVector<TemplateArgument, 8> Args;
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003617 Args.reserve(NumArgs);
3618 while (NumArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00003619 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003620 return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
3621 Args.size(), Args.data());
3622 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003623
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003624 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00003625 unsigned Idx = 0;
3626
3627 // ArrayType
Douglas Gregor393f2492011-07-22 00:38:23 +00003628 QualType ElementType = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00003629 ArrayType::ArraySizeModifier ASM
3630 = (ArrayType::ArraySizeModifier)Record[Idx++];
3631 unsigned IndexTypeQuals = Record[Idx++];
3632
3633 // DependentSizedArrayType
Sebastian Redlc3632732010-10-05 15:59:54 +00003634 Expr *NumElts = ReadExpr(*Loc.F);
3635 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00003636
3637 return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
3638 IndexTypeQuals, Brackets);
3639 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003640
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003641 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003642 unsigned Idx = 0;
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003643 bool IsDependent = Record[Idx++];
Douglas Gregor1aee05d2011-01-15 06:45:20 +00003644 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
Chris Lattner5f9e2722011-07-23 10:55:15 +00003645 SmallVector<TemplateArgument, 8> Args;
Sebastian Redlc3632732010-10-05 15:59:54 +00003646 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00003647 QualType Underlying = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003648 QualType T;
Richard Smith3e4c6c42011-05-05 21:57:07 +00003649 if (Underlying.isNull())
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003650 T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(),
3651 Args.size());
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003652 else
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003653 T = Context->getTemplateSpecializationType(Name, Args.data(),
Richard Smith3e4c6c42011-05-05 21:57:07 +00003654 Args.size(), Underlying);
John McCallf4c73712011-01-19 06:33:43 +00003655 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003656 return T;
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003657 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003658 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003659 // Suppress a GCC warning
3660 return QualType();
3661}
3662
Sebastian Redlc3632732010-10-05 15:59:54 +00003663class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003664 ASTReader &Reader;
Douglas Gregor72a9ae12011-07-22 16:00:58 +00003665 Module &F;
Sebastian Redl577d4792010-07-22 22:43:28 +00003666 llvm::BitstreamCursor &DeclsCursor;
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003667 const ASTReader::RecordData &Record;
John McCalla1ee0c52009-10-16 21:56:05 +00003668 unsigned &Idx;
3669
Sebastian Redlc3632732010-10-05 15:59:54 +00003670 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
3671 unsigned &I) {
3672 return Reader.ReadSourceLocation(F, R, I);
3673 }
3674
Douglas Gregor409448c2011-07-21 22:35:25 +00003675 template<typename T>
3676 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
3677 return Reader.ReadDeclAs<T>(F, Record, Idx);
3678 }
3679
John McCalla1ee0c52009-10-16 21:56:05 +00003680public:
Douglas Gregor72a9ae12011-07-22 16:00:58 +00003681 TypeLocReader(ASTReader &Reader, Module &F,
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003682 const ASTReader::RecordData &Record, unsigned &Idx)
Sebastian Redlc3632732010-10-05 15:59:54 +00003683 : Reader(Reader), F(F), DeclsCursor(F.DeclsCursor), Record(Record), Idx(Idx)
3684 { }
John McCalla1ee0c52009-10-16 21:56:05 +00003685
John McCall51bd8032009-10-18 01:05:36 +00003686 // We want compile-time assurance that we've enumerated all of
3687 // these, so unfortunately we have to declare them first, then
3688 // define them out-of-line.
3689#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +00003690#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +00003691 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00003692#include "clang/AST/TypeLocNodes.def"
3693
John McCall51bd8032009-10-18 01:05:36 +00003694 void VisitFunctionTypeLoc(FunctionTypeLoc);
3695 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00003696};
3697
John McCall51bd8032009-10-18 01:05:36 +00003698void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCalla1ee0c52009-10-16 21:56:05 +00003699 // nothing to do
3700}
John McCall51bd8032009-10-18 01:05:36 +00003701void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003702 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorddf889a2010-01-18 18:04:31 +00003703 if (TL.needsExtraLocalData()) {
3704 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
3705 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
3706 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
3707 TL.setModeAttr(Record[Idx++]);
3708 }
John McCalla1ee0c52009-10-16 21:56:05 +00003709}
John McCall51bd8032009-10-18 01:05:36 +00003710void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003711 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003712}
John McCall51bd8032009-10-18 01:05:36 +00003713void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003714 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003715}
John McCall51bd8032009-10-18 01:05:36 +00003716void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003717 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003718}
John McCall51bd8032009-10-18 01:05:36 +00003719void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003720 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003721}
John McCall51bd8032009-10-18 01:05:36 +00003722void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003723 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003724}
John McCall51bd8032009-10-18 01:05:36 +00003725void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003726 TL.setStarLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +00003727 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003728}
John McCall51bd8032009-10-18 01:05:36 +00003729void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003730 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
3731 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003732 if (Record[Idx++])
Sebastian Redlc3632732010-10-05 15:59:54 +00003733 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003734 else
John McCall51bd8032009-10-18 01:05:36 +00003735 TL.setSizeExpr(0);
3736}
3737void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
3738 VisitArrayTypeLoc(TL);
3739}
3740void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
3741 VisitArrayTypeLoc(TL);
3742}
3743void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
3744 VisitArrayTypeLoc(TL);
3745}
3746void TypeLocReader::VisitDependentSizedArrayTypeLoc(
3747 DependentSizedArrayTypeLoc TL) {
3748 VisitArrayTypeLoc(TL);
3749}
3750void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
3751 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003752 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003753}
3754void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003755 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003756}
3757void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003758 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003759}
3760void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +00003761 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
3762 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
Douglas Gregordab60ad2010-10-01 18:44:50 +00003763 TL.setTrailingReturn(Record[Idx++]);
John McCall51bd8032009-10-18 01:05:36 +00003764 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
Douglas Gregor409448c2011-07-21 22:35:25 +00003765 TL.setArg(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003766 }
3767}
3768void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
3769 VisitFunctionTypeLoc(TL);
3770}
3771void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
3772 VisitFunctionTypeLoc(TL);
3773}
John McCalled976492009-12-04 22:46:56 +00003774void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003775 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalled976492009-12-04 22:46:56 +00003776}
John McCall51bd8032009-10-18 01:05:36 +00003777void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003778 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003779}
3780void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003781 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3782 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3783 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003784}
3785void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003786 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3787 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3788 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3789 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003790}
3791void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003792 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003793}
Sean Huntca63c202011-05-24 22:41:36 +00003794void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
3795 TL.setKWLoc(ReadSourceLocation(Record, Idx));
3796 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3797 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3798 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
3799}
Richard Smith34b41d92011-02-20 03:19:35 +00003800void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
3801 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3802}
John McCall51bd8032009-10-18 01:05:36 +00003803void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003804 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003805}
3806void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003807 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003808}
John McCall9d156a72011-01-06 01:58:22 +00003809void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
3810 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
3811 if (TL.hasAttrOperand()) {
3812 SourceRange range;
3813 range.setBegin(ReadSourceLocation(Record, Idx));
3814 range.setEnd(ReadSourceLocation(Record, Idx));
3815 TL.setAttrOperandParensRange(range);
3816 }
3817 if (TL.hasAttrExprOperand()) {
3818 if (Record[Idx++])
3819 TL.setAttrExprOperand(Reader.ReadExpr(F));
3820 else
3821 TL.setAttrExprOperand(0);
3822 } else if (TL.hasAttrEnumOperand())
3823 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
3824}
John McCall51bd8032009-10-18 01:05:36 +00003825void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003826 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003827}
John McCall49a832b2009-10-18 09:09:24 +00003828void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
3829 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003830 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall49a832b2009-10-18 09:09:24 +00003831}
Douglas Gregorc3069d62011-01-14 02:55:32 +00003832void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
3833 SubstTemplateTypeParmPackTypeLoc TL) {
3834 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3835}
John McCall51bd8032009-10-18 01:05:36 +00003836void TypeLocReader::VisitTemplateSpecializationTypeLoc(
3837 TemplateSpecializationTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003838 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
3839 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3840 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall833ca992009-10-29 08:12:44 +00003841 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
3842 TL.setArgLocInfo(i,
Sebastian Redlc3632732010-10-05 15:59:54 +00003843 Reader.GetTemplateArgumentLocInfo(F,
3844 TL.getTypePtr()->getArg(i).getKind(),
3845 Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003846}
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003847void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
3848 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3849 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3850}
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003851void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003852 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor9e876872011-03-01 18:12:44 +00003853 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003854}
John McCall3cb0ebd2010-03-10 03:28:59 +00003855void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003856 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall3cb0ebd2010-03-10 03:28:59 +00003857}
Douglas Gregor4714c122010-03-31 17:34:00 +00003858void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003859 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor2494dd02011-03-01 01:34:45 +00003860 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00003861 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003862}
John McCall33500952010-06-11 00:33:02 +00003863void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
3864 DependentTemplateSpecializationTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003865 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor94fdffa2011-03-01 20:11:18 +00003866 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00003867 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3868 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3869 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall33500952010-06-11 00:33:02 +00003870 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
3871 TL.setArgLocInfo(I,
Sebastian Redlc3632732010-10-05 15:59:54 +00003872 Reader.GetTemplateArgumentLocInfo(F,
3873 TL.getTypePtr()->getArg(I).getKind(),
3874 Record, Idx));
John McCall33500952010-06-11 00:33:02 +00003875}
Douglas Gregor7536dd52010-12-20 02:24:11 +00003876void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
3877 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
3878}
John McCall51bd8032009-10-18 01:05:36 +00003879void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003880 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallc12c5bb2010-05-15 11:32:37 +00003881}
3882void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3883 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00003884 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3885 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003886 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redlc3632732010-10-05 15:59:54 +00003887 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003888}
John McCall54e14c42009-10-22 22:37:11 +00003889void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003890 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall54e14c42009-10-22 22:37:11 +00003891}
John McCalla1ee0c52009-10-16 21:56:05 +00003892
Douglas Gregor72a9ae12011-07-22 16:00:58 +00003893TypeSourceInfo *ASTReader::GetTypeSourceInfo(Module &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00003894 const RecordData &Record,
John McCalla1ee0c52009-10-16 21:56:05 +00003895 unsigned &Idx) {
Douglas Gregor393f2492011-07-22 00:38:23 +00003896 QualType InfoTy = readType(F, Record, Idx);
John McCalla1ee0c52009-10-16 21:56:05 +00003897 if (InfoTy.isNull())
3898 return 0;
3899
John McCalla93c9342009-12-07 02:54:59 +00003900 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
Sebastian Redlc3632732010-10-05 15:59:54 +00003901 TypeLocReader TLR(*this, F, Record, Idx);
John McCalla93c9342009-12-07 02:54:59 +00003902 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCalla1ee0c52009-10-16 21:56:05 +00003903 TLR.Visit(TL);
John McCalla93c9342009-12-07 02:54:59 +00003904 return TInfo;
John McCalla1ee0c52009-10-16 21:56:05 +00003905}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003906
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003907QualType ASTReader::GetType(TypeID ID) {
John McCall0953e762009-09-24 19:53:00 +00003908 unsigned FastQuals = ID & Qualifiers::FastMask;
3909 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003910
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003911 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003912 QualType T;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003913 switch ((PredefinedTypeIDs)Index) {
3914 case PREDEF_TYPE_NULL_ID: return QualType();
3915 case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
3916 case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003917
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003918 case PREDEF_TYPE_CHAR_U_ID:
3919 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregor2cf26342009-04-09 22:27:44 +00003920 // FIXME: Check that the signedness of CharTy is correct!
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003921 T = Context->CharTy;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003922 break;
3923
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003924 case PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break;
3925 case PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break;
3926 case PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break;
3927 case PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break;
3928 case PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break;
3929 case PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break;
3930 case PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break;
3931 case PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break;
3932 case PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break;
3933 case PREDEF_TYPE_INT_ID: T = Context->IntTy; break;
3934 case PREDEF_TYPE_LONG_ID: T = Context->LongTy; break;
3935 case PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break;
3936 case PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break;
3937 case PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break;
3938 case PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break;
3939 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break;
3940 case PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break;
John McCall864c0412011-04-26 20:42:42 +00003941 case PREDEF_TYPE_BOUND_MEMBER: T = Context->BoundMemberTy; break;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003942 case PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break;
John McCall1de4d4e2011-04-07 08:22:57 +00003943 case PREDEF_TYPE_UNKNOWN_ANY: T = Context->UnknownAnyTy; break;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003944 case PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break;
3945 case PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break;
3946 case PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break;
3947 case PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break;
3948 case PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break;
3949 case PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break;
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003950 case PREDEF_TYPE_AUTO_DEDUCT: T = Context->getAutoDeductType(); break;
3951
3952 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
3953 T = Context->getAutoRRefDeductType();
3954 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003955 }
3956
3957 assert(!T.isNull() && "Unknown predefined type");
John McCall0953e762009-09-24 19:53:00 +00003958 return T.withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003959 }
3960
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003961 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003962 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl07a353c2010-07-14 20:26:45 +00003963 if (TypesLoaded[Index].isNull()) {
Douglas Gregor393f2492011-07-22 00:38:23 +00003964 TypesLoaded[Index] = readTypeRecord(Index);
Douglas Gregor97475832010-10-05 18:37:06 +00003965 if (TypesLoaded[Index].isNull())
3966 return QualType();
3967
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003968 TypesLoaded[Index]->setFromAST();
Sebastian Redl30c514c2010-07-14 23:45:08 +00003969 if (DeserializationListener)
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003970 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1476ed42010-07-16 16:36:56 +00003971 TypesLoaded[Index]);
Sebastian Redl07a353c2010-07-14 20:26:45 +00003972 }
Mike Stump1eb44332009-09-09 15:08:12 +00003973
John McCall0953e762009-09-24 19:53:00 +00003974 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003975}
3976
Douglas Gregor72a9ae12011-07-22 16:00:58 +00003977QualType ASTReader::getLocalType(Module &F, unsigned LocalID) {
Douglas Gregor393f2492011-07-22 00:38:23 +00003978 return GetType(getGlobalTypeID(F, LocalID));
3979}
3980
3981serialization::TypeID
Douglas Gregor72a9ae12011-07-22 16:00:58 +00003982ASTReader::getGlobalTypeID(Module &F, unsigned LocalID) const {
Douglas Gregora119da02011-08-02 16:26:37 +00003983 unsigned FastQuals = LocalID & Qualifiers::FastMask;
3984 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
3985
3986 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
3987 return LocalID;
3988
3989 ContinuousRangeMap<uint32_t, int, 2>::iterator I
3990 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
3991 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
3992
3993 unsigned GlobalIndex = LocalIndex + I->second;
3994 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
3995}
3996
John McCall833ca992009-10-29 08:12:44 +00003997TemplateArgumentLocInfo
Douglas Gregor72a9ae12011-07-22 16:00:58 +00003998ASTReader::GetTemplateArgumentLocInfo(Module &F,
Sebastian Redlc3632732010-10-05 15:59:54 +00003999 TemplateArgument::ArgKind Kind,
John McCall833ca992009-10-29 08:12:44 +00004000 const RecordData &Record,
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00004001 unsigned &Index) {
John McCall833ca992009-10-29 08:12:44 +00004002 switch (Kind) {
4003 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00004004 return ReadExpr(F);
John McCall833ca992009-10-29 08:12:44 +00004005 case TemplateArgument::Type:
Sebastian Redlc3632732010-10-05 15:59:54 +00004006 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor788cd062009-11-11 01:00:40 +00004007 case TemplateArgument::Template: {
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004008 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
4009 Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00004010 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004011 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregora7fc9012011-01-05 18:58:31 +00004012 SourceLocation());
4013 }
4014 case TemplateArgument::TemplateExpansion: {
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004015 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
4016 Index);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004017 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorba68eca2011-01-05 17:40:24 +00004018 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004019 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregorba68eca2011-01-05 17:40:24 +00004020 EllipsisLoc);
Douglas Gregor788cd062009-11-11 01:00:40 +00004021 }
John McCall833ca992009-10-29 08:12:44 +00004022 case TemplateArgument::Null:
4023 case TemplateArgument::Integral:
4024 case TemplateArgument::Declaration:
4025 case TemplateArgument::Pack:
4026 return TemplateArgumentLocInfo();
4027 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00004028 llvm_unreachable("unexpected template argument loc");
John McCall833ca992009-10-29 08:12:44 +00004029 return TemplateArgumentLocInfo();
4030}
4031
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004032TemplateArgumentLoc
Douglas Gregor72a9ae12011-07-22 16:00:58 +00004033ASTReader::ReadTemplateArgumentLoc(Module &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00004034 const RecordData &Record, unsigned &Index) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004035 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004036
4037 if (Arg.getKind() == TemplateArgument::Expression) {
4038 if (Record[Index++]) // bool InfoHasSameExpr.
4039 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
4040 }
Sebastian Redlc3632732010-10-05 15:59:54 +00004041 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00004042 Record, Index));
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004043}
4044
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004045Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall76bd1f32010-06-01 09:23:16 +00004046 return GetDecl(ID);
4047}
4048
Douglas Gregore92b8a12011-08-04 00:01:48 +00004049uint64_t ASTReader::readCXXBaseSpecifiers(Module &M, const RecordData &Record,
4050 unsigned &Idx){
4051 if (Idx >= Record.size())
Douglas Gregor7c789c12010-10-29 22:39:52 +00004052 return 0;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004053
Douglas Gregore92b8a12011-08-04 00:01:48 +00004054 unsigned LocalID = Record[Idx++];
4055 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004056}
4057
4058CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004059 RecordLocation Loc = getLocalBitOffset(Offset);
4060 llvm::BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004061 SavedStreamPosition SavedPosition(Cursor);
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004062 Cursor.JumpToBit(Loc.Offset);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004063 ReadingKindTracker ReadingKind(Read_Decl, *this);
4064 RecordData Record;
4065 unsigned Code = Cursor.ReadCode();
4066 unsigned RecCode = Cursor.ReadRecord(Code, Record);
4067 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
4068 Error("Malformed AST file: missing C++ base specifiers");
4069 return 0;
4070 }
4071
4072 unsigned Idx = 0;
4073 unsigned NumBases = Record[Idx++];
4074 void *Mem = Context->Allocate(sizeof(CXXBaseSpecifier) * NumBases);
4075 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
4076 for (unsigned I = 0; I != NumBases; ++I)
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004077 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004078 return Bases;
4079}
4080
Douglas Gregor409448c2011-07-21 22:35:25 +00004081serialization::DeclID
Douglas Gregor72a9ae12011-07-22 16:00:58 +00004082ASTReader::getGlobalDeclID(Module &F, unsigned LocalID) const {
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004083 if (LocalID < NUM_PREDEF_DECL_IDS)
Douglas Gregor496c7092011-08-03 15:48:04 +00004084 return LocalID;
4085
4086 ContinuousRangeMap<uint32_t, int, 2>::iterator I
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004087 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
Douglas Gregor496c7092011-08-03 15:48:04 +00004088 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
4089
4090 return LocalID + I->second;
Douglas Gregor409448c2011-07-21 22:35:25 +00004091}
4092
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004093bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
4094 Module &M) const {
4095 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
4096 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
4097 return &M == I->second;
4098}
4099
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004100Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004101 if (ID < NUM_PREDEF_DECL_IDS) {
4102 switch ((PredefinedDeclIDs)ID) {
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004103 case PREDEF_DECL_NULL_ID:
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004104 return 0;
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004105
4106 case PREDEF_DECL_TRANSLATION_UNIT_ID:
4107 assert(Context && "No context available?");
4108 return Context->getTranslationUnitDecl();
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00004109
4110 case PREDEF_DECL_OBJC_ID_ID:
4111 assert(Context && "No context available?");
4112 return Context->getObjCIdDecl();
Douglas Gregor79d67262011-08-12 05:59:41 +00004113
Douglas Gregor7a27ea52011-08-12 06:17:30 +00004114 case PREDEF_DECL_OBJC_SEL_ID:
4115 assert(Context && "No context available?");
4116 return Context->getObjCSelDecl();
4117
Douglas Gregor79d67262011-08-12 05:59:41 +00004118 case PREDEF_DECL_OBJC_CLASS_ID:
4119 assert(Context && "No context available?");
4120 return Context->getObjCClassDecl();
Douglas Gregor772eeae2011-08-12 06:49:56 +00004121
4122 case PREDEF_DECL_INT_128_ID:
4123 assert(Context && "No context available?");
4124 return Context->getInt128Decl();
4125
4126 case PREDEF_DECL_UNSIGNED_INT_128_ID:
4127 assert(Context && "No context available?");
4128 return Context->getUInt128Decl();
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004129 }
4130
Douglas Gregor2cf26342009-04-09 22:27:44 +00004131 return 0;
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004132 }
4133
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004134 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
4135
4136 if (Index > DeclsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004137 Error("declaration ID out-of-range for AST file");
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004138 return 0;
4139 }
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004140
4141if (!DeclsLoaded[Index]) {
Douglas Gregor496c7092011-08-03 15:48:04 +00004142 ReadDeclRecord(ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00004143 if (DeserializationListener)
4144 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
4145 }
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004146
4147 return DeclsLoaded[Index];
Douglas Gregor2cf26342009-04-09 22:27:44 +00004148}
4149
Douglas Gregor72a9ae12011-07-22 16:00:58 +00004150serialization::DeclID ASTReader::ReadDeclID(Module &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00004151 const RecordData &Record,
4152 unsigned &Idx) {
4153 if (Idx >= Record.size()) {
4154 Error("Corrupted AST file");
4155 return 0;
4156 }
4157
4158 return getGlobalDeclID(F, Record[Idx++]);
4159}
4160
Chris Lattner887e2b32009-04-27 05:46:25 +00004161/// \brief Resolve the offset of a statement into a statement.
4162///
4163/// This operation will read a new statement from the external
4164/// source each time it is called, and is meant to be used via a
4165/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004166Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00004167 // Switch case IDs are per Decl.
4168 ClearSwitchCaseIDs();
4169
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00004170 // Offset here is a global offset across the entire chain.
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004171 RecordLocation Loc = getLocalBitOffset(Offset);
4172 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
4173 return ReadStmtFromStream(*Loc.F);
Douglas Gregor250fc9c2009-04-18 00:07:54 +00004174}
4175
Douglas Gregor851c75a2011-08-24 21:27:34 +00004176namespace {
4177 class FindExternalLexicalDeclsVisitor {
4178 ASTReader &Reader;
4179 const DeclContext *DC;
4180 bool (*isKindWeWant)(Decl::Kind);
Douglas Gregor2ea054f2011-08-26 22:04:51 +00004181
Douglas Gregor851c75a2011-08-24 21:27:34 +00004182 SmallVectorImpl<Decl*> &Decls;
4183 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
4184
4185 public:
4186 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
4187 bool (*isKindWeWant)(Decl::Kind),
4188 SmallVectorImpl<Decl*> &Decls)
4189 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
4190 {
4191 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
4192 PredefsVisited[I] = false;
4193 }
4194
4195 static bool visit(Module &M, bool Preorder, void *UserData) {
4196 if (Preorder)
4197 return false;
4198
4199 FindExternalLexicalDeclsVisitor *This
4200 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
4201
4202 Module::DeclContextInfosMap::iterator Info
4203 = M.DeclContextInfos.find(This->DC);
4204 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
4205 return false;
4206
4207 // Load all of the declaration IDs
4208 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
4209 *IDE = ID + Info->second.NumLexicalDecls;
4210 ID != IDE; ++ID) {
4211 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
4212 continue;
4213
4214 // Don't add predefined declarations to the lexical context more
4215 // than once.
4216 if (ID->second < NUM_PREDEF_DECL_IDS) {
4217 if (This->PredefsVisited[ID->second])
4218 continue;
4219
4220 This->PredefsVisited[ID->second] = true;
4221 }
4222
Douglas Gregor2ea054f2011-08-26 22:04:51 +00004223 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
4224 if (!This->DC->isDeclInLexicalTraversal(D))
4225 This->Decls.push_back(D);
4226 }
Douglas Gregor851c75a2011-08-24 21:27:34 +00004227 }
4228
4229 return false;
4230 }
4231 };
4232}
4233
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00004234ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00004235 bool (*isKindWeWant)(Decl::Kind),
Chris Lattner5f9e2722011-07-23 10:55:15 +00004236 SmallVectorImpl<Decl*> &Decls) {
Douglas Gregor0d95f772011-08-24 19:03:07 +00004237 // There might be lexical decls in multiple modules, for the TU at
Douglas Gregor851c75a2011-08-24 21:27:34 +00004238 // least. Walk all of the modules in the order they were loaded.
4239 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
4240 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
Douglas Gregor25123082009-04-22 22:34:57 +00004241 ++NumLexicalDeclContextsRead;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00004242 return ELR_Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004243}
4244
Douglas Gregor0d95f772011-08-24 19:03:07 +00004245namespace {
4246 /// \brief Module visitor used to perform name lookup into a
4247 /// declaration context.
4248 class DeclContextNameLookupVisitor {
4249 ASTReader &Reader;
4250 const DeclContext *DC;
4251 DeclarationName Name;
4252 SmallVectorImpl<NamedDecl *> &Decls;
4253
4254 public:
4255 DeclContextNameLookupVisitor(ASTReader &Reader,
4256 const DeclContext *DC, DeclarationName Name,
4257 SmallVectorImpl<NamedDecl *> &Decls)
4258 : Reader(Reader), DC(DC), Name(Name), Decls(Decls) { }
4259
4260 static bool visit(Module &M, void *UserData) {
4261 DeclContextNameLookupVisitor *This
4262 = static_cast<DeclContextNameLookupVisitor *>(UserData);
4263
4264 // Check whether we have any visible declaration information for
4265 // this context in this module.
4266 Module::DeclContextInfosMap::iterator Info
4267 = M.DeclContextInfos.find(This->DC);
4268 if (Info == M.DeclContextInfos.end() || !Info->second.NameLookupTableData)
4269 return false;
4270
4271 // Look for this name within this module.
4272 ASTDeclContextNameLookupTable *LookupTable =
4273 (ASTDeclContextNameLookupTable*)Info->second.NameLookupTableData;
4274 ASTDeclContextNameLookupTable::iterator Pos
4275 = LookupTable->find(This->Name);
4276 if (Pos == LookupTable->end())
4277 return false;
4278
4279 bool FoundAnything = false;
4280 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
4281 for (; Data.first != Data.second; ++Data.first) {
4282 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
4283 if (!ND)
4284 continue;
4285
4286 if (ND->getDeclName() != This->Name) {
4287 assert(!This->Name.getCXXNameType().isNull() &&
4288 "Name mismatch without a type");
4289 continue;
4290 }
4291
4292 // Record this declaration.
4293 FoundAnything = true;
4294 This->Decls.push_back(ND);
4295 }
4296
4297 return FoundAnything;
4298 }
4299 };
4300}
4301
John McCall76bd1f32010-06-01 09:23:16 +00004302DeclContext::lookup_result
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004303ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +00004304 DeclarationName Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00004305 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00004306 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00004307 if (!Name)
4308 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
4309 DeclContext::lookup_iterator(0));
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004310
Chris Lattner5f9e2722011-07-23 10:55:15 +00004311 SmallVector<NamedDecl *, 64> Decls;
Douglas Gregor0d95f772011-08-24 19:03:07 +00004312 DeclContextNameLookupVisitor Visitor(*this, DC, Name, Decls);
4313 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
Douglas Gregor25123082009-04-22 22:34:57 +00004314 ++NumVisibleDeclContextsRead;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00004315 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall76bd1f32010-06-01 09:23:16 +00004316 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004317}
4318
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004319void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00004320 assert(Consumer);
4321 while (!InterestingDecls.empty()) {
4322 DeclGroupRef DG(InterestingDecls.front());
4323 InterestingDecls.pop_front();
Sebastian Redl27372b42010-08-11 18:52:41 +00004324 Consumer->HandleInterestingDecl(DG);
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00004325 }
4326}
4327
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004328void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor0af2ca42009-04-22 19:09:20 +00004329 this->Consumer = Consumer;
4330
Douglas Gregorfdd01722009-04-14 00:24:19 +00004331 if (!Consumer)
4332 return;
4333
4334 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00004335 // Force deserialization of this decl, which will cause it to be queued for
4336 // passing to the consumer.
Daniel Dunbar04a0b502009-09-17 03:06:44 +00004337 GetDecl(ExternalDefinitions[I]);
Douglas Gregorfdd01722009-04-14 00:24:19 +00004338 }
Douglas Gregorc62a2fe2009-04-25 00:41:30 +00004339
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00004340 PassInterestingDeclsToConsumer();
Douglas Gregorfdd01722009-04-14 00:24:19 +00004341}
4342
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004343void ASTReader::PrintStats() {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004344 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregor2cf26342009-04-09 22:27:44 +00004345
Mike Stump1eb44332009-09-09 15:08:12 +00004346 unsigned NumTypesLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00004347 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall0953e762009-09-24 19:53:00 +00004348 QualType());
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00004349 unsigned NumDeclsLoaded
4350 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
4351 (Decl *)0);
4352 unsigned NumIdentifiersLoaded
4353 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
4354 IdentifiersLoaded.end(),
4355 (IdentifierInfo *)0);
Mike Stump1eb44332009-09-09 15:08:12 +00004356 unsigned NumSelectorsLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00004357 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
4358 SelectorsLoaded.end(),
4359 Selector());
Douglas Gregor2d41cc12009-04-13 20:50:16 +00004360
Douglas Gregor4fed3f42009-04-27 18:38:38 +00004361 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
4362 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor0cdd7982011-07-21 18:46:38 +00004363 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00004364 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
4365 NumSLocEntriesRead, TotalNumSLocEntries,
4366 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004367 if (!TypesLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00004368 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004369 NumTypesLoaded, (unsigned)TypesLoaded.size(),
4370 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
4371 if (!DeclsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00004372 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004373 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
4374 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00004375 if (!IdentifiersLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00004376 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00004377 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
4378 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redl725cd962010-08-04 20:40:17 +00004379 if (!SelectorsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00004380 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redl725cd962010-08-04 20:40:17 +00004381 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
4382 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor83941df2009-04-25 17:48:32 +00004383 if (TotalNumStatements)
4384 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
4385 NumStatementsRead, TotalNumStatements,
4386 ((float)NumStatementsRead/TotalNumStatements * 100));
4387 if (TotalNumMacros)
4388 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
4389 NumMacrosRead, TotalNumMacros,
4390 ((float)NumMacrosRead/TotalNumMacros * 100));
4391 if (TotalLexicalDeclContexts)
4392 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
4393 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
4394 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
4395 * 100));
4396 if (TotalVisibleDeclContexts)
4397 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
4398 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
4399 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
4400 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004401 if (TotalNumMethodPoolEntries) {
Douglas Gregor83941df2009-04-25 17:48:32 +00004402 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004403 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
4404 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor83941df2009-04-25 17:48:32 +00004405 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004406 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor83941df2009-04-25 17:48:32 +00004407 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00004408 std::fprintf(stderr, "\n");
Douglas Gregor23d7df52011-07-21 19:50:14 +00004409 dump();
4410 std::fprintf(stderr, "\n");
4411}
4412
Douglas Gregor72a9ae12011-07-22 16:00:58 +00004413template<typename Key, typename Module, unsigned InitialCapacity>
Douglas Gregor23d7df52011-07-21 19:50:14 +00004414static void
Chris Lattner5f9e2722011-07-23 10:55:15 +00004415dumpModuleIDMap(StringRef Name,
Douglas Gregor72a9ae12011-07-22 16:00:58 +00004416 const ContinuousRangeMap<Key, Module *,
Douglas Gregor23d7df52011-07-21 19:50:14 +00004417 InitialCapacity> &Map) {
4418 if (Map.begin() == Map.end())
4419 return;
4420
Douglas Gregor72a9ae12011-07-22 16:00:58 +00004421 typedef ContinuousRangeMap<Key, Module *, InitialCapacity> MapType;
Douglas Gregor23d7df52011-07-21 19:50:14 +00004422 llvm::errs() << Name << ":\n";
4423 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
4424 I != IEnd; ++I) {
4425 llvm::errs() << " " << I->first << " -> " << I->second->FileName
4426 << "\n";
4427 }
4428}
4429
Douglas Gregor23d7df52011-07-21 19:50:14 +00004430void ASTReader::dump() {
Douglas Gregor8df5c9b2011-08-02 11:12:41 +00004431 llvm::errs() << "*** PCH/Module Remappings:\n";
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004432 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
Douglas Gregor23d7df52011-07-21 19:50:14 +00004433 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
Douglas Gregor1e849b62011-07-29 00:21:44 +00004434 dumpModuleIDMap("Global type map", GlobalTypeMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00004435 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00004436 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
4437 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
4438 dumpModuleIDMap("Global macro definition map", GlobalMacroDefinitionMap);
4439 dumpModuleIDMap("Global preprocessed entity map",
4440 GlobalPreprocessedEntityMap);
Douglas Gregor8df5c9b2011-08-02 11:12:41 +00004441
4442 llvm::errs() << "\n*** PCH/Modules Loaded:";
4443 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
4444 MEnd = ModuleMgr.end();
4445 M != MEnd; ++M)
4446 (*M)->dump();
Douglas Gregor2cf26342009-04-09 22:27:44 +00004447}
4448
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00004449/// Return the amount of memory used by memory buffers, breaking down
4450/// by heap-backed versus mmap'ed memory.
4451void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00004452 for (ModuleConstIterator I = ModuleMgr.begin(),
4453 E = ModuleMgr.end(); I != E; ++I) {
4454 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00004455 size_t bytes = buf->getBufferSize();
4456 switch (buf->getBufferKind()) {
4457 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
4458 sizes.malloc_bytes += bytes;
4459 break;
4460 case llvm::MemoryBuffer::MemoryBuffer_MMap:
4461 sizes.mmap_bytes += bytes;
4462 break;
4463 }
4464 }
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00004465 }
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00004466}
4467
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004468void ASTReader::InitializeSema(Sema &S) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00004469 SemaObj = &S;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00004470 S.ExternalSource = this;
4471
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00004472 // Makes sure any declarations that were deserialized "too early"
4473 // still get added to the identifier's declaration chains.
Douglas Gregor76dc8892010-09-24 23:29:12 +00004474 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
4475 if (SemaObj->TUScope)
John McCalld226f652010-08-21 09:40:31 +00004476 SemaObj->TUScope->AddDecl(PreloadedDecls[I]);
Douglas Gregor76dc8892010-09-24 23:29:12 +00004477
4478 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregor668c1a42009-04-21 22:25:48 +00004479 }
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00004480 PreloadedDecls.clear();
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004481
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00004482 // Load the offsets of the declarations that Sema references.
4483 // They will be lazily deserialized when needed.
4484 if (!SemaDeclRefs.empty()) {
4485 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
Douglas Gregor1e5b6f62011-07-28 00:57:24 +00004486 if (!SemaObj->StdNamespace)
4487 SemaObj->StdNamespace = SemaDeclRefs[0];
4488 if (!SemaObj->StdBadAlloc)
4489 SemaObj->StdBadAlloc = SemaDeclRefs[1];
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00004490 }
4491
Peter Collingbourne84bccea2011-02-15 19:46:30 +00004492 if (!FPPragmaOptions.empty()) {
4493 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
4494 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
4495 }
4496
4497 if (!OpenCLExtensions.empty()) {
4498 unsigned I = 0;
4499#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
4500#include "clang/Basic/OpenCLExtensions.def"
4501
4502 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
4503 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00004504}
4505
Douglas Gregor211f6e82011-08-20 04:39:52 +00004506IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Douglas Gregor211f6e82011-08-20 04:39:52 +00004507 IdentifierLookupVisitor Visitor(StringRef(NameStart, NameEnd - NameStart));
4508 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
4509 return Visitor.getIdentifierInfo();
Douglas Gregor668c1a42009-04-21 22:25:48 +00004510}
4511
Douglas Gregor95f42922010-10-14 22:11:03 +00004512namespace clang {
4513 /// \brief An identifier-lookup iterator that enumerates all of the
4514 /// identifiers stored within a set of AST files.
4515 class ASTIdentifierIterator : public IdentifierIterator {
4516 /// \brief The AST reader whose identifiers are being enumerated.
4517 const ASTReader &Reader;
4518
4519 /// \brief The current index into the chain of AST files stored in
4520 /// the AST reader.
4521 unsigned Index;
4522
4523 /// \brief The current position within the identifier lookup table
4524 /// of the current AST file.
4525 ASTIdentifierLookupTable::key_iterator Current;
4526
4527 /// \brief The end position within the identifier lookup table of
4528 /// the current AST file.
4529 ASTIdentifierLookupTable::key_iterator End;
4530
4531 public:
4532 explicit ASTIdentifierIterator(const ASTReader &Reader);
4533
Chris Lattner5f9e2722011-07-23 10:55:15 +00004534 virtual StringRef Next();
Douglas Gregor95f42922010-10-14 22:11:03 +00004535 };
4536}
4537
4538ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00004539 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
Douglas Gregor95f42922010-10-14 22:11:03 +00004540 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00004541 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
Douglas Gregor95f42922010-10-14 22:11:03 +00004542 Current = IdTable->key_begin();
4543 End = IdTable->key_end();
4544}
4545
Chris Lattner5f9e2722011-07-23 10:55:15 +00004546StringRef ASTIdentifierIterator::Next() {
Douglas Gregor95f42922010-10-14 22:11:03 +00004547 while (Current == End) {
4548 // If we have exhausted all of our AST files, we're done.
4549 if (Index == 0)
Chris Lattner5f9e2722011-07-23 10:55:15 +00004550 return StringRef();
Douglas Gregor95f42922010-10-14 22:11:03 +00004551
4552 --Index;
4553 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00004554 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
4555 IdentifierLookupTable;
Douglas Gregor95f42922010-10-14 22:11:03 +00004556 Current = IdTable->key_begin();
4557 End = IdTable->key_end();
4558 }
4559
4560 // We have any identifiers remaining in the current AST file; return
4561 // the next one.
4562 std::pair<const char*, unsigned> Key = *Current;
4563 ++Current;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004564 return StringRef(Key.first, Key.second);
Douglas Gregor95f42922010-10-14 22:11:03 +00004565}
4566
4567IdentifierIterator *ASTReader::getIdentifiers() const {
4568 return new ASTIdentifierIterator(*this);
4569}
4570
Douglas Gregor3d15ab82011-08-25 14:51:20 +00004571namespace clang { namespace serialization {
4572 class ReadMethodPoolVisitor {
4573 ASTReader &Reader;
4574 Selector Sel;
4575 llvm::SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
4576 llvm::SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00004577
Douglas Gregor3d15ab82011-08-25 14:51:20 +00004578 /// \brief Build an ObjCMethodList from a vector of Objective-C method
4579 /// declarations.
4580 ObjCMethodList
4581 buildObjCMethodList(const SmallVectorImpl<ObjCMethodDecl *> &Vec) const
4582 {
4583 ObjCMethodList List;
4584 ObjCMethodList *Prev = 0;
4585 for (unsigned I = 0, N = Vec.size(); I != N; ++I) {
4586 if (!List.Method) {
4587 // This is the first method, which is the easy case.
4588 List.Method = Vec[I];
4589 Prev = &List;
4590 continue;
4591 }
4592
4593 ObjCMethodList *Mem =
4594 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
4595 Prev->Next = new (Mem) ObjCMethodList(Vec[I], 0);
4596 Prev = Prev->Next;
4597 }
4598
4599 return List;
4600 }
4601
4602 public:
4603 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel)
4604 : Reader(Reader), Sel(Sel) { }
4605
4606 static bool visit(Module &M, void *UserData) {
4607 ReadMethodPoolVisitor *This
4608 = static_cast<ReadMethodPoolVisitor *>(UserData);
4609
4610 if (!M.SelectorLookupTable)
4611 return false;
4612
4613 ASTSelectorLookupTable *PoolTable
4614 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
4615 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
4616 if (Pos == PoolTable->end())
4617 return false;
4618
4619 ++This->Reader.NumSelectorsRead;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004620 // FIXME: Not quite happy with the statistics here. We probably should
4621 // disable this tracking when called via LoadSelector.
4622 // Also, should entries without methods count as misses?
Douglas Gregor3d15ab82011-08-25 14:51:20 +00004623 ++This->Reader.NumMethodPoolEntriesRead;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004624 ASTSelectorLookupTrait::data_type Data = *Pos;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00004625 if (This->Reader.DeserializationListener)
4626 This->Reader.DeserializationListener->SelectorRead(Data.ID,
4627 This->Sel);
4628
4629 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
4630 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
4631 return true;
Sebastian Redl725cd962010-08-04 20:40:17 +00004632 }
Douglas Gregor3d15ab82011-08-25 14:51:20 +00004633
4634 /// \brief Retrieve the instance methods found by this visitor.
4635 ObjCMethodList getInstanceMethods() const {
4636 return buildObjCMethodList(InstanceMethods);
4637 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00004638
Douglas Gregor3d15ab82011-08-25 14:51:20 +00004639 /// \brief Retrieve the instance methods found by this visitor.
4640 ObjCMethodList getFactoryMethods() const {
4641 return buildObjCMethodList(FactoryMethods);
4642 }
4643 };
4644} } // end namespace clang::serialization
4645
4646std::pair<ObjCMethodList, ObjCMethodList>
4647ASTReader::ReadMethodPool(Selector Sel) {
4648 ReadMethodPoolVisitor Visitor(*this, Sel);
4649 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
4650 std::pair<ObjCMethodList, ObjCMethodList> Result;
4651 Result.first = Visitor.getInstanceMethods();
4652 Result.second = Visitor.getFactoryMethods();
4653
4654 if (!Result.first.Method && !Result.second.Method)
4655 ++NumMethodPoolMisses;
4656 return Result;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00004657}
4658
Douglas Gregord8bba9c2011-06-28 16:20:02 +00004659void ASTReader::ReadKnownNamespaces(
Chris Lattner5f9e2722011-07-23 10:55:15 +00004660 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +00004661 Namespaces.clear();
4662
4663 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
4664 if (NamespaceDecl *Namespace
4665 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
4666 Namespaces.push_back(Namespace);
4667 }
4668}
4669
Douglas Gregora8623202011-07-27 20:58:46 +00004670void ASTReader::ReadTentativeDefinitions(
4671 SmallVectorImpl<VarDecl *> &TentativeDefs) {
4672 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
4673 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
4674 if (Var)
4675 TentativeDefs.push_back(Var);
4676 }
4677 TentativeDefinitions.clear();
4678}
4679
Douglas Gregora2ee20a2011-07-27 21:45:57 +00004680void ASTReader::ReadUnusedFileScopedDecls(
4681 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
4682 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
4683 DeclaratorDecl *D
4684 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
4685 if (D)
4686 Decls.push_back(D);
4687 }
4688 UnusedFileScopedDecls.clear();
4689}
4690
Douglas Gregor0129b562011-07-27 21:57:17 +00004691void ASTReader::ReadDelegatingConstructors(
4692 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
4693 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
4694 CXXConstructorDecl *D
4695 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
4696 if (D)
4697 Decls.push_back(D);
4698 }
4699 DelegatingCtorDecls.clear();
4700}
4701
Douglas Gregord58a0a52011-07-28 00:39:29 +00004702void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
4703 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
4704 TypedefNameDecl *D
4705 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
4706 if (D)
4707 Decls.push_back(D);
4708 }
4709 ExtVectorDecls.clear();
4710}
4711
Douglas Gregora126f172011-07-28 00:53:40 +00004712void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
4713 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
4714 CXXRecordDecl *D
4715 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
4716 if (D)
4717 Decls.push_back(D);
4718 }
4719 DynamicClasses.clear();
4720}
4721
Douglas Gregorec12ce22011-07-28 14:20:37 +00004722void
4723ASTReader::ReadLocallyScopedExternalDecls(SmallVectorImpl<NamedDecl *> &Decls) {
4724 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
4725 NamedDecl *D
4726 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
4727 if (D)
4728 Decls.push_back(D);
4729 }
4730 LocallyScopedExternalDecls.clear();
4731}
4732
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00004733void ASTReader::ReadReferencedSelectors(
4734 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
4735 if (ReferencedSelectorsData.empty())
4736 return;
4737
4738 // If there are @selector references added them to its pool. This is for
4739 // implementation of -Wselector.
4740 unsigned int DataSize = ReferencedSelectorsData.size()-1;
4741 unsigned I = 0;
4742 while (I < DataSize) {
4743 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
4744 SourceLocation SelLoc
4745 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
4746 Sels.push_back(std::make_pair(Sel, SelLoc));
4747 }
4748 ReferencedSelectorsData.clear();
4749}
4750
Douglas Gregor31e37b22011-07-28 18:09:57 +00004751void ASTReader::ReadWeakUndeclaredIdentifiers(
4752 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
4753 if (WeakUndeclaredIdentifiers.empty())
4754 return;
4755
4756 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
4757 IdentifierInfo *WeakId
4758 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
4759 IdentifierInfo *AliasId
4760 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
4761 SourceLocation Loc
4762 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
4763 bool Used = WeakUndeclaredIdentifiers[I++];
4764 WeakInfo WI(AliasId, Loc);
4765 WI.setUsed(Used);
4766 WeakIDs.push_back(std::make_pair(WeakId, WI));
4767 }
4768 WeakUndeclaredIdentifiers.clear();
4769}
4770
Douglas Gregordfe65432011-07-28 19:11:31 +00004771void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
4772 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
4773 ExternalVTableUse VT;
4774 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
4775 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
4776 VT.DefinitionRequired = VTableUses[Idx++];
4777 VTables.push_back(VT);
4778 }
4779
4780 VTableUses.clear();
4781}
4782
Douglas Gregor6e4a3f52011-07-28 19:49:54 +00004783void ASTReader::ReadPendingInstantiations(
4784 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
4785 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
4786 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
4787 SourceLocation Loc
4788 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
4789 Pending.push_back(std::make_pair(D, Loc));
4790 }
4791 PendingInstantiations.clear();
4792}
4793
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004794void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redle58aa892010-08-04 18:21:41 +00004795 // It would be complicated to avoid reading the methods anyway. So don't.
4796 ReadMethodPool(Sel);
4797}
4798
Douglas Gregor95eab172011-07-28 20:55:49 +00004799void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00004800 assert(ID && "Non-zero identifier ID required");
Douglas Gregora02b1472009-04-28 21:53:25 +00004801 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00004802 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004803 if (DeserializationListener)
4804 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregor668c1a42009-04-21 22:25:48 +00004805}
4806
Douglas Gregord89275b2009-07-06 18:54:52 +00004807/// \brief Set the globally-visible declarations associated with the given
4808/// identifier.
4809///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004810/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump1eb44332009-09-09 15:08:12 +00004811/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregord89275b2009-07-06 18:54:52 +00004812/// them.
4813///
4814/// \param II an IdentifierInfo that refers to one or more globally-visible
4815/// declarations.
4816///
4817/// \param DeclIDs the set of declaration IDs with the name @p II that are
4818/// visible at global scope.
4819///
4820/// \param Nonrecursive should be true to indicate that the caller knows that
4821/// this call is non-recursive, and therefore the globally-visible declarations
4822/// will not be placed onto the pending queue.
Mike Stump1eb44332009-09-09 15:08:12 +00004823void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004824ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004825 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregord89275b2009-07-06 18:54:52 +00004826 bool Nonrecursive) {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004827 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregord89275b2009-07-06 18:54:52 +00004828 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
4829 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
4830 PII.II = II;
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00004831 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregord89275b2009-07-06 18:54:52 +00004832 return;
4833 }
Mike Stump1eb44332009-09-09 15:08:12 +00004834
Douglas Gregord89275b2009-07-06 18:54:52 +00004835 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
4836 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
4837 if (SemaObj) {
Douglas Gregor914ed9d2010-08-13 03:15:25 +00004838 if (SemaObj->TUScope) {
4839 // Introduce this declaration into the translation-unit scope
4840 // and add it to the declaration chain for this identifier, so
4841 // that (unqualified) name lookup will find it.
John McCalld226f652010-08-21 09:40:31 +00004842 SemaObj->TUScope->AddDecl(D);
Douglas Gregor914ed9d2010-08-13 03:15:25 +00004843 }
Douglas Gregor76dc8892010-09-24 23:29:12 +00004844 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
Douglas Gregord89275b2009-07-06 18:54:52 +00004845 } else {
4846 // Queue this declaration so that it will be added to the
4847 // translation unit scope and identifier's declaration chain
4848 // once a Sema object is known.
4849 PreloadedDecls.push_back(D);
4850 }
4851 }
4852}
4853
Douglas Gregor95eab172011-07-28 20:55:49 +00004854IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00004855 if (ID == 0)
4856 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00004857
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004858 if (IdentifiersLoaded.empty()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004859 Error("no identifier table in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00004860 return 0;
4861 }
Mike Stump1eb44332009-09-09 15:08:12 +00004862
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00004863 assert(PP && "Forgot to set Preprocessor ?");
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004864 ID -= 1;
4865 if (!IdentifiersLoaded[ID]) {
Douglas Gregor67268d02011-07-20 00:59:32 +00004866 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
4867 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
Douglas Gregor9827a802011-07-29 00:56:45 +00004868 Module *M = I->second;
4869 unsigned Index = ID - M->BaseIdentifierID;
4870 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
Douglas Gregord6595a42009-04-25 21:04:17 +00004871
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004872 // All of the strings in the AST file are preceded by a 16-bit length.
4873 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenek231bc0b2009-10-23 04:45:31 +00004874 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
4875 // unsigned integers. This is important to avoid integer overflow when
4876 // we cast them to 'unsigned'.
Ted Kremenekff1ea462009-10-23 03:57:22 +00004877 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregor02fc7512009-04-28 20:01:51 +00004878 unsigned StrLen = (((unsigned) StrLenPtr[0])
4879 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004880 IdentifiersLoaded[ID]
Chris Lattner5f9e2722011-07-23 10:55:15 +00004881 = &PP->getIdentifierTable().get(StringRef(Str, StrLen));
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004882 if (DeserializationListener)
4883 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregorafaf3082009-04-11 00:14:32 +00004884 }
Mike Stump1eb44332009-09-09 15:08:12 +00004885
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004886 return IdentifiersLoaded[ID];
Douglas Gregor2cf26342009-04-09 22:27:44 +00004887}
4888
Douglas Gregor95eab172011-07-28 20:55:49 +00004889IdentifierInfo *ASTReader::getLocalIdentifier(Module &M, unsigned LocalID) {
4890 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
4891}
4892
4893IdentifierID ASTReader::getGlobalIdentifierID(Module &M, unsigned LocalID) {
Douglas Gregor6ec60e02011-08-03 21:49:18 +00004894 if (LocalID < NUM_PREDEF_IDENT_IDS)
4895 return LocalID;
4896
4897 ContinuousRangeMap<uint32_t, int, 2>::iterator I
4898 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
4899 assert(I != M.IdentifierRemap.end()
4900 && "Invalid index into identifier index remap");
4901
4902 return LocalID + I->second;
Douglas Gregor95eab172011-07-28 20:55:49 +00004903}
4904
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004905bool ASTReader::ReadSLocEntry(int ID) {
Douglas Gregore23ac652011-04-20 00:21:03 +00004906 return ReadSLocEntryRecord(ID) != Success;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00004907}
4908
Douglas Gregor2d2689a2011-07-28 21:16:51 +00004909Selector ASTReader::getLocalSelector(Module &M, unsigned LocalID) {
4910 return DecodeSelector(getGlobalSelectorID(M, LocalID));
4911}
4912
4913Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004914 if (ID == 0)
4915 return Selector();
Mike Stump1eb44332009-09-09 15:08:12 +00004916
Sebastian Redl725cd962010-08-04 20:40:17 +00004917 if (ID > SelectorsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004918 Error("selector ID out of range in AST file");
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004919 return Selector();
4920 }
Douglas Gregor83941df2009-04-25 17:48:32 +00004921
Sebastian Redl725cd962010-08-04 20:40:17 +00004922 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor83941df2009-04-25 17:48:32 +00004923 // Load this selector from the selector table.
Douglas Gregor96958cb2011-07-20 01:10:58 +00004924 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
4925 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
Douglas Gregor9827a802011-07-29 00:56:45 +00004926 Module &M = *I->second;
4927 ASTSelectorLookupTrait Trait(*this, M);
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00004928 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
Douglas Gregor96958cb2011-07-20 01:10:58 +00004929 SelectorsLoaded[ID - 1] =
Douglas Gregor9827a802011-07-29 00:56:45 +00004930 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
Douglas Gregor96958cb2011-07-20 01:10:58 +00004931 if (DeserializationListener)
4932 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
Douglas Gregor83941df2009-04-25 17:48:32 +00004933 }
4934
Sebastian Redl725cd962010-08-04 20:40:17 +00004935 return SelectorsLoaded[ID - 1];
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004936}
4937
Douglas Gregor8451ec72011-07-28 14:41:43 +00004938Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
Douglas Gregor719770d2010-04-06 17:30:22 +00004939 return DecodeSelector(ID);
4940}
4941
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004942uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redl725cd962010-08-04 20:40:17 +00004943 // ID 0 (the null selector) is considered an external selector.
4944 return getTotalNumSelectors() + 1;
Douglas Gregor719770d2010-04-06 17:30:22 +00004945}
4946
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00004947serialization::SelectorID
4948ASTReader::getGlobalSelectorID(Module &M, unsigned LocalID) const {
4949 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
4950 return LocalID;
4951
4952 ContinuousRangeMap<uint32_t, int, 2>::iterator I
4953 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
4954 assert(I != M.SelectorRemap.end()
4955 && "Invalid index into identifier index remap");
4956
4957 return LocalID + I->second;
Douglas Gregor8451ec72011-07-28 14:41:43 +00004958}
4959
Mike Stump1eb44332009-09-09 15:08:12 +00004960DeclarationName
Douglas Gregor72a9ae12011-07-22 16:00:58 +00004961ASTReader::ReadDeclarationName(Module &F,
Douglas Gregor393f2492011-07-22 00:38:23 +00004962 const RecordData &Record, unsigned &Idx) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004963 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
4964 switch (Kind) {
4965 case DeclarationName::Identifier:
Douglas Gregor95eab172011-07-28 20:55:49 +00004966 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004967
4968 case DeclarationName::ObjCZeroArgSelector:
4969 case DeclarationName::ObjCOneArgSelector:
4970 case DeclarationName::ObjCMultiArgSelector:
Douglas Gregor2d2689a2011-07-28 21:16:51 +00004971 return DeclarationName(ReadSelector(F, Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004972
4973 case DeclarationName::CXXConstructorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004974 return Context->DeclarationNames.getCXXConstructorName(
Douglas Gregor393f2492011-07-22 00:38:23 +00004975 Context->getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004976
4977 case DeclarationName::CXXDestructorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004978 return Context->DeclarationNames.getCXXDestructorName(
Douglas Gregor393f2492011-07-22 00:38:23 +00004979 Context->getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004980
4981 case DeclarationName::CXXConversionFunctionName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004982 return Context->DeclarationNames.getCXXConversionFunctionName(
Douglas Gregor393f2492011-07-22 00:38:23 +00004983 Context->getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004984
4985 case DeclarationName::CXXOperatorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004986 return Context->DeclarationNames.getCXXOperatorName(
Douglas Gregor2cf26342009-04-09 22:27:44 +00004987 (OverloadedOperatorKind)Record[Idx++]);
4988
Sean Hunt3e518bd2009-11-29 07:34:05 +00004989 case DeclarationName::CXXLiteralOperatorName:
4990 return Context->DeclarationNames.getCXXLiteralOperatorName(
Douglas Gregor95eab172011-07-28 20:55:49 +00004991 GetIdentifierInfo(F, Record, Idx));
Sean Hunt3e518bd2009-11-29 07:34:05 +00004992
Douglas Gregor2cf26342009-04-09 22:27:44 +00004993 case DeclarationName::CXXUsingDirective:
4994 return DeclarationName::getUsingDirectiveName();
4995 }
4996
4997 // Required to silence GCC warning
4998 return DeclarationName();
4999}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00005000
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005001void ASTReader::ReadDeclarationNameLoc(Module &F,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005002 DeclarationNameLoc &DNLoc,
5003 DeclarationName Name,
5004 const RecordData &Record, unsigned &Idx) {
5005 switch (Name.getNameKind()) {
5006 case DeclarationName::CXXConstructorName:
5007 case DeclarationName::CXXDestructorName:
5008 case DeclarationName::CXXConversionFunctionName:
5009 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
5010 break;
5011
5012 case DeclarationName::CXXOperatorName:
5013 DNLoc.CXXOperatorName.BeginOpNameLoc
5014 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5015 DNLoc.CXXOperatorName.EndOpNameLoc
5016 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5017 break;
5018
5019 case DeclarationName::CXXLiteralOperatorName:
5020 DNLoc.CXXLiteralOperatorName.OpNameLoc
5021 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5022 break;
5023
5024 case DeclarationName::Identifier:
5025 case DeclarationName::ObjCZeroArgSelector:
5026 case DeclarationName::ObjCOneArgSelector:
5027 case DeclarationName::ObjCMultiArgSelector:
5028 case DeclarationName::CXXUsingDirective:
5029 break;
5030 }
5031}
5032
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005033void ASTReader::ReadDeclarationNameInfo(Module &F,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005034 DeclarationNameInfo &NameInfo,
5035 const RecordData &Record, unsigned &Idx) {
Douglas Gregor393f2492011-07-22 00:38:23 +00005036 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005037 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
5038 DeclarationNameLoc DNLoc;
5039 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
5040 NameInfo.setInfo(DNLoc);
5041}
5042
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005043void ASTReader::ReadQualifierInfo(Module &F, QualifierInfo &Info,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005044 const RecordData &Record, unsigned &Idx) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00005045 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005046 unsigned NumTPLists = Record[Idx++];
5047 Info.NumTemplParamLists = NumTPLists;
5048 if (NumTPLists) {
5049 Info.TemplParamLists = new (*Context) TemplateParameterList*[NumTPLists];
5050 for (unsigned i=0; i != NumTPLists; ++i)
5051 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
5052 }
5053}
5054
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005055TemplateName
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005056ASTReader::ReadTemplateName(Module &F, const RecordData &Record,
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005057 unsigned &Idx) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00005058 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005059 switch (Kind) {
5060 case TemplateName::Template:
Douglas Gregor409448c2011-07-21 22:35:25 +00005061 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005062
5063 case TemplateName::OverloadedTemplate: {
5064 unsigned size = Record[Idx++];
5065 UnresolvedSet<8> Decls;
5066 while (size--)
Douglas Gregor409448c2011-07-21 22:35:25 +00005067 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005068
5069 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
5070 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00005071
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005072 case TemplateName::QualifiedTemplate: {
Douglas Gregor409448c2011-07-21 22:35:25 +00005073 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005074 bool hasTemplKeyword = Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00005075 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005076 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
5077 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00005078
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005079 case TemplateName::DependentTemplate: {
Douglas Gregor409448c2011-07-21 22:35:25 +00005080 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005081 if (Record[Idx++]) // isIdentifier
5082 return Context->getDependentTemplateName(NNS,
Douglas Gregor95eab172011-07-28 20:55:49 +00005083 GetIdentifierInfo(F, Record,
5084 Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005085 return Context->getDependentTemplateName(NNS,
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00005086 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005087 }
John McCall14606042011-06-30 08:33:18 +00005088
5089 case TemplateName::SubstTemplateTemplateParm: {
5090 TemplateTemplateParmDecl *param
Douglas Gregor409448c2011-07-21 22:35:25 +00005091 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
John McCall14606042011-06-30 08:33:18 +00005092 if (!param) return TemplateName();
5093 TemplateName replacement = ReadTemplateName(F, Record, Idx);
5094 return Context->getSubstTemplateTemplateParm(param, replacement);
5095 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005096
5097 case TemplateName::SubstTemplateTemplateParmPack: {
5098 TemplateTemplateParmDecl *Param
Douglas Gregor409448c2011-07-21 22:35:25 +00005099 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005100 if (!Param)
5101 return TemplateName();
5102
5103 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
5104 if (ArgPack.getKind() != TemplateArgument::Pack)
5105 return TemplateName();
5106
5107 return Context->getSubstTemplateTemplateParmPack(Param, ArgPack);
5108 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005109 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00005110
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005111 assert(0 && "Unhandled template name kind!");
5112 return TemplateName();
5113}
5114
5115TemplateArgument
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005116ASTReader::ReadTemplateArgument(Module &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00005117 const RecordData &Record, unsigned &Idx) {
Douglas Gregora7fc9012011-01-05 18:58:31 +00005118 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
5119 switch (Kind) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005120 case TemplateArgument::Null:
5121 return TemplateArgument();
5122 case TemplateArgument::Type:
Douglas Gregor393f2492011-07-22 00:38:23 +00005123 return TemplateArgument(readType(F, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005124 case TemplateArgument::Declaration:
Douglas Gregor409448c2011-07-21 22:35:25 +00005125 return TemplateArgument(ReadDecl(F, Record, Idx));
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00005126 case TemplateArgument::Integral: {
5127 llvm::APSInt Value = ReadAPSInt(Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00005128 QualType T = readType(F, Record, Idx);
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00005129 return TemplateArgument(Value, T);
5130 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00005131 case TemplateArgument::Template:
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005132 return TemplateArgument(ReadTemplateName(F, Record, Idx));
Douglas Gregora7fc9012011-01-05 18:58:31 +00005133 case TemplateArgument::TemplateExpansion: {
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005134 TemplateName Name = ReadTemplateName(F, Record, Idx);
Douglas Gregor2be29f42011-01-14 23:41:42 +00005135 llvm::Optional<unsigned> NumTemplateExpansions;
5136 if (unsigned NumExpansions = Record[Idx++])
5137 NumTemplateExpansions = NumExpansions - 1;
5138 return TemplateArgument(Name, NumTemplateExpansions);
Douglas Gregorba68eca2011-01-05 17:40:24 +00005139 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005140 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00005141 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005142 case TemplateArgument::Pack: {
5143 unsigned NumArgs = Record[Idx++];
Douglas Gregor910f8002010-11-07 23:05:16 +00005144 TemplateArgument *Args = new (*Context) TemplateArgument[NumArgs];
5145 for (unsigned I = 0; I != NumArgs; ++I)
5146 Args[I] = ReadTemplateArgument(F, Record, Idx);
5147 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005148 }
5149 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00005150
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005151 assert(0 && "Unhandled template argument kind!");
5152 return TemplateArgument();
5153}
5154
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005155TemplateParameterList *
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005156ASTReader::ReadTemplateParameterList(Module &F,
Sebastian Redlc3632732010-10-05 15:59:54 +00005157 const RecordData &Record, unsigned &Idx) {
5158 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
5159 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
5160 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005161
5162 unsigned NumParams = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00005163 SmallVector<NamedDecl *, 16> Params;
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005164 Params.reserve(NumParams);
5165 while (NumParams--)
Douglas Gregor409448c2011-07-21 22:35:25 +00005166 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
Michael J. Spencer20249a12010-10-21 03:16:25 +00005167
5168 TemplateParameterList* TemplateParams =
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005169 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
5170 Params.data(), Params.size(), RAngleLoc);
5171 return TemplateParams;
5172}
5173
5174void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005175ASTReader::
Chris Lattner5f9e2722011-07-23 10:55:15 +00005176ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs,
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005177 Module &F, const RecordData &Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00005178 unsigned &Idx) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005179 unsigned NumTemplateArgs = Record[Idx++];
5180 TemplArgs.reserve(NumTemplateArgs);
5181 while (NumTemplateArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00005182 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005183}
5184
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00005185/// \brief Read a UnresolvedSet structure.
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005186void ASTReader::ReadUnresolvedSet(Module &F, UnresolvedSetImpl &Set,
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00005187 const RecordData &Record, unsigned &Idx) {
5188 unsigned NumDecls = Record[Idx++];
5189 while (NumDecls--) {
Douglas Gregor409448c2011-07-21 22:35:25 +00005190 NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx);
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00005191 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
5192 Set.addDecl(D, AS);
5193 }
5194}
5195
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00005196CXXBaseSpecifier
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005197ASTReader::ReadCXXBaseSpecifier(Module &F,
Nick Lewycky56062202010-07-26 16:56:01 +00005198 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00005199 bool isVirtual = static_cast<bool>(Record[Idx++]);
5200 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
5201 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redlf677ea32011-02-05 19:23:19 +00005202 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00005203 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
5204 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00005205 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlf677ea32011-02-05 19:23:19 +00005206 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
Douglas Gregorf90b27a2011-01-03 22:36:02 +00005207 EllipsisLoc);
Sebastian Redlf677ea32011-02-05 19:23:19 +00005208 Result.setInheritConstructors(inheritConstructors);
5209 return Result;
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00005210}
5211
Sean Huntcbb67482011-01-08 20:30:50 +00005212std::pair<CXXCtorInitializer **, unsigned>
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005213ASTReader::ReadCXXCtorInitializers(Module &F, const RecordData &Record,
Sean Huntcbb67482011-01-08 20:30:50 +00005214 unsigned &Idx) {
5215 CXXCtorInitializer **CtorInitializers = 0;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005216 unsigned NumInitializers = Record[Idx++];
5217 if (NumInitializers) {
5218 ASTContext &C = *getContext();
5219
Sean Huntcbb67482011-01-08 20:30:50 +00005220 CtorInitializers
5221 = new (C) CXXCtorInitializer*[NumInitializers];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005222 for (unsigned i=0; i != NumInitializers; ++i) {
5223 TypeSourceInfo *BaseClassInfo = 0;
5224 bool IsBaseVirtual = false;
5225 FieldDecl *Member = 0;
Francois Pichet00eb3f92010-12-04 09:14:42 +00005226 IndirectFieldDecl *IndirectMember = 0;
Sean Hunt156b6402011-05-04 01:19:08 +00005227 CXXConstructorDecl *Target = 0;
Michael J. Spencer20249a12010-10-21 03:16:25 +00005228
Sean Hunt156b6402011-05-04 01:19:08 +00005229 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
5230 switch (Type) {
5231 case CTOR_INITIALIZER_BASE:
Sebastian Redlc3632732010-10-05 15:59:54 +00005232 BaseClassInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005233 IsBaseVirtual = Record[Idx++];
Sean Hunt156b6402011-05-04 01:19:08 +00005234 break;
5235
5236 case CTOR_INITIALIZER_DELEGATING:
Douglas Gregor409448c2011-07-21 22:35:25 +00005237 Target = ReadDeclAs<CXXConstructorDecl>(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00005238 break;
5239
5240 case CTOR_INITIALIZER_MEMBER:
Douglas Gregor409448c2011-07-21 22:35:25 +00005241 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00005242 break;
5243
5244 case CTOR_INITIALIZER_INDIRECT_MEMBER:
Douglas Gregor409448c2011-07-21 22:35:25 +00005245 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00005246 break;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005247 }
Sean Hunt156b6402011-05-04 01:19:08 +00005248
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00005249 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlc3632732010-10-05 15:59:54 +00005250 Expr *Init = ReadExpr(F);
Sebastian Redlc3632732010-10-05 15:59:54 +00005251 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
5252 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005253 bool IsWritten = Record[Idx++];
5254 unsigned SourceOrderOrNumArrayIndices;
Chris Lattner5f9e2722011-07-23 10:55:15 +00005255 SmallVector<VarDecl *, 8> Indices;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005256 if (IsWritten) {
5257 SourceOrderOrNumArrayIndices = Record[Idx++];
5258 } else {
5259 SourceOrderOrNumArrayIndices = Record[Idx++];
5260 Indices.reserve(SourceOrderOrNumArrayIndices);
5261 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
Douglas Gregor409448c2011-07-21 22:35:25 +00005262 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005263 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00005264
Sean Huntcbb67482011-01-08 20:30:50 +00005265 CXXCtorInitializer *BOMInit;
Sean Hunt156b6402011-05-04 01:19:08 +00005266 if (Type == CTOR_INITIALIZER_BASE) {
Sean Huntcbb67482011-01-08 20:30:50 +00005267 BOMInit = new (C) CXXCtorInitializer(C, BaseClassInfo, IsBaseVirtual,
5268 LParenLoc, Init, RParenLoc,
5269 MemberOrEllipsisLoc);
Sean Hunt156b6402011-05-04 01:19:08 +00005270 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
5271 BOMInit = new (C) CXXCtorInitializer(C, MemberOrEllipsisLoc, LParenLoc,
5272 Target, Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005273 } else if (IsWritten) {
Francois Pichet00eb3f92010-12-04 09:14:42 +00005274 if (Member)
Sean Huntcbb67482011-01-08 20:30:50 +00005275 BOMInit = new (C) CXXCtorInitializer(C, Member, MemberOrEllipsisLoc,
5276 LParenLoc, Init, RParenLoc);
Francois Pichet00eb3f92010-12-04 09:14:42 +00005277 else
Sean Huntcbb67482011-01-08 20:30:50 +00005278 BOMInit = new (C) CXXCtorInitializer(C, IndirectMember,
5279 MemberOrEllipsisLoc, LParenLoc,
5280 Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005281 } else {
Sean Huntcbb67482011-01-08 20:30:50 +00005282 BOMInit = CXXCtorInitializer::Create(C, Member, MemberOrEllipsisLoc,
5283 LParenLoc, Init, RParenLoc,
5284 Indices.data(), Indices.size());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005285 }
5286
Argyrios Kyrtzidisf84cde12010-09-06 19:04:27 +00005287 if (IsWritten)
5288 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Sean Huntcbb67482011-01-08 20:30:50 +00005289 CtorInitializers[i] = BOMInit;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005290 }
5291 }
5292
Sean Huntcbb67482011-01-08 20:30:50 +00005293 return std::make_pair(CtorInitializers, NumInitializers);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005294}
5295
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005296NestedNameSpecifier *
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005297ASTReader::ReadNestedNameSpecifier(Module &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00005298 const RecordData &Record, unsigned &Idx) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005299 unsigned N = Record[Idx++];
5300 NestedNameSpecifier *NNS = 0, *Prev = 0;
5301 for (unsigned I = 0; I != N; ++I) {
5302 NestedNameSpecifier::SpecifierKind Kind
5303 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
5304 switch (Kind) {
5305 case NestedNameSpecifier::Identifier: {
Douglas Gregor95eab172011-07-28 20:55:49 +00005306 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005307 NNS = NestedNameSpecifier::Create(*Context, Prev, II);
5308 break;
5309 }
5310
5311 case NestedNameSpecifier::Namespace: {
Douglas Gregor409448c2011-07-21 22:35:25 +00005312 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005313 NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
5314 break;
5315 }
5316
Douglas Gregor14aba762011-02-24 02:36:08 +00005317 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor409448c2011-07-21 22:35:25 +00005318 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregor14aba762011-02-24 02:36:08 +00005319 NNS = NestedNameSpecifier::Create(*Context, Prev, Alias);
5320 break;
5321 }
5322
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005323 case NestedNameSpecifier::TypeSpec:
5324 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregor393f2492011-07-22 00:38:23 +00005325 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
Douglas Gregor1ab55e92010-12-10 17:03:06 +00005326 if (!T)
5327 return 0;
5328
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005329 bool Template = Record[Idx++];
5330 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
5331 break;
5332 }
5333
5334 case NestedNameSpecifier::Global: {
5335 NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
5336 // No associated value, and there can't be a prefix.
5337 break;
5338 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005339 }
Argyrios Kyrtzidisd2bb2c02010-07-07 15:46:30 +00005340 Prev = NNS;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005341 }
5342 return NNS;
5343}
5344
Douglas Gregordc355712011-02-25 00:36:19 +00005345NestedNameSpecifierLoc
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005346ASTReader::ReadNestedNameSpecifierLoc(Module &F, const RecordData &Record,
Douglas Gregordc355712011-02-25 00:36:19 +00005347 unsigned &Idx) {
5348 unsigned N = Record[Idx++];
Douglas Gregor5f791bb2011-02-28 23:58:31 +00005349 NestedNameSpecifierLocBuilder Builder;
Douglas Gregordc355712011-02-25 00:36:19 +00005350 for (unsigned I = 0; I != N; ++I) {
5351 NestedNameSpecifier::SpecifierKind Kind
5352 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
5353 switch (Kind) {
5354 case NestedNameSpecifier::Identifier: {
Douglas Gregor95eab172011-07-28 20:55:49 +00005355 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00005356 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor5f791bb2011-02-28 23:58:31 +00005357 Builder.Extend(*Context, II, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00005358 break;
5359 }
5360
5361 case NestedNameSpecifier::Namespace: {
Douglas Gregor409448c2011-07-21 22:35:25 +00005362 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00005363 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor5f791bb2011-02-28 23:58:31 +00005364 Builder.Extend(*Context, NS, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00005365 break;
5366 }
5367
5368 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor409448c2011-07-21 22:35:25 +00005369 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00005370 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor5f791bb2011-02-28 23:58:31 +00005371 Builder.Extend(*Context, Alias, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00005372 break;
5373 }
5374
5375 case NestedNameSpecifier::TypeSpec:
5376 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregordc355712011-02-25 00:36:19 +00005377 bool Template = Record[Idx++];
5378 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
5379 if (!T)
5380 return NestedNameSpecifierLoc();
Douglas Gregordc355712011-02-25 00:36:19 +00005381 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor5f791bb2011-02-28 23:58:31 +00005382
5383 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
5384 Builder.Extend(*Context,
5385 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
5386 T->getTypeLoc(), ColonColonLoc);
Douglas Gregordc355712011-02-25 00:36:19 +00005387 break;
5388 }
5389
5390 case NestedNameSpecifier::Global: {
Douglas Gregordc355712011-02-25 00:36:19 +00005391 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor5f791bb2011-02-28 23:58:31 +00005392 Builder.MakeGlobal(*Context, ColonColonLoc);
Douglas Gregordc355712011-02-25 00:36:19 +00005393 break;
5394 }
5395 }
Douglas Gregordc355712011-02-25 00:36:19 +00005396 }
5397
Douglas Gregor5f791bb2011-02-28 23:58:31 +00005398 return Builder.getWithLocInContext(*Context);
Douglas Gregordc355712011-02-25 00:36:19 +00005399}
5400
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005401SourceRange
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005402ASTReader::ReadSourceRange(Module &F, const RecordData &Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00005403 unsigned &Idx) {
5404 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
5405 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar8ee59392010-06-02 15:47:10 +00005406 return SourceRange(beg, end);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005407}
5408
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00005409/// \brief Read an integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005410llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00005411 unsigned BitWidth = Record[Idx++];
5412 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
5413 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
5414 Idx += NumWords;
5415 return Result;
5416}
5417
5418/// \brief Read a signed integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005419llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00005420 bool isUnsigned = Record[Idx++];
5421 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
5422}
5423
Douglas Gregor17fc2232009-04-14 21:55:33 +00005424/// \brief Read a floating-point value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005425llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00005426 return llvm::APFloat(ReadAPInt(Record, Idx));
5427}
5428
Douglas Gregor68a2eb02009-04-15 21:30:51 +00005429// \brief Read a string
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005430std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00005431 unsigned Len = Record[Idx++];
Jay Foadbeaaccd2009-05-21 09:52:38 +00005432 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00005433 Idx += Len;
5434 return Result;
5435}
5436
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005437VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
5438 unsigned &Idx) {
5439 unsigned Major = Record[Idx++];
5440 unsigned Minor = Record[Idx++];
5441 unsigned Subminor = Record[Idx++];
5442 if (Minor == 0)
5443 return VersionTuple(Major);
5444 if (Subminor == 0)
5445 return VersionTuple(Major, Minor - 1);
5446 return VersionTuple(Major, Minor - 1, Subminor - 1);
5447}
5448
Douglas Gregor72a9ae12011-07-22 16:00:58 +00005449CXXTemporary *ASTReader::ReadCXXTemporary(Module &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00005450 const RecordData &Record,
Chris Lattnerd2598362010-05-10 00:25:06 +00005451 unsigned &Idx) {
Douglas Gregor409448c2011-07-21 22:35:25 +00005452 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
Chris Lattnerd2598362010-05-10 00:25:06 +00005453 return CXXTemporary::Create(*Context, Decl);
5454}
5455
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005456DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00005457 return Diag(SourceLocation(), DiagID);
5458}
5459
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005460DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00005461 return Diags.Report(Loc, DiagID);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00005462}
Douglas Gregor025452f2009-04-17 00:04:06 +00005463
Douglas Gregor668c1a42009-04-21 22:25:48 +00005464/// \brief Retrieve the identifier table associated with the
5465/// preprocessor.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005466IdentifierTable &ASTReader::getIdentifierTable() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00005467 assert(PP && "Forgot to set Preprocessor ?");
5468 return PP->getIdentifierTable();
Douglas Gregor668c1a42009-04-21 22:25:48 +00005469}
5470
Douglas Gregor025452f2009-04-17 00:04:06 +00005471/// \brief Record that the given ID maps to the given switch-case
5472/// statement.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005473void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Douglas Gregor025452f2009-04-17 00:04:06 +00005474 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
5475 SwitchCaseStmts[ID] = SC;
5476}
5477
5478/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005479SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Douglas Gregor025452f2009-04-17 00:04:06 +00005480 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
5481 return SwitchCaseStmts[ID];
5482}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00005483
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00005484void ASTReader::ClearSwitchCaseIDs() {
5485 SwitchCaseStmts.clear();
5486}
5487
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005488void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00005489 assert(NumCurrentElementsDeserializing &&
5490 "FinishedDeserializing not paired with StartedDeserializing");
5491 if (NumCurrentElementsDeserializing == 1) {
Douglas Gregord89275b2009-07-06 18:54:52 +00005492 // If any identifiers with corresponding top-level declarations have
5493 // been loaded, load those declarations now.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00005494 while (!PendingIdentifierInfos.empty()) {
5495 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
5496 PendingIdentifierInfos.front().DeclIDs, true);
5497 PendingIdentifierInfos.pop_front();
Douglas Gregord89275b2009-07-06 18:54:52 +00005498 }
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005499
Argyrios Kyrtzidis0895d152011-02-12 07:50:47 +00005500 // Ready to load previous declarations of Decls that were delayed.
5501 while (!PendingPreviousDecls.empty()) {
5502 loadAndAttachPreviousDecl(PendingPreviousDecls.front().first,
5503 PendingPreviousDecls.front().second);
5504 PendingPreviousDecls.pop_front();
5505 }
5506
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005507 // We are not in recursive loading, so it's safe to pass the "interesting"
5508 // decls to the consumer.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00005509 if (Consumer)
5510 PassInterestingDeclsToConsumer();
Argyrios Kyrtzidis134db1f2010-10-24 17:26:31 +00005511
5512 assert(PendingForwardRefs.size() == 0 &&
5513 "Some forward refs did not get linked to the definition!");
Douglas Gregord89275b2009-07-06 18:54:52 +00005514 }
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00005515 --NumCurrentElementsDeserializing;
Douglas Gregord89275b2009-07-06 18:54:52 +00005516}
Douglas Gregor501c1032010-08-19 00:28:17 +00005517
Sebastian Redle1dde812010-08-24 00:50:04 +00005518ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
Douglas Gregor832d6202011-07-22 16:35:34 +00005519 StringRef isysroot, bool DisableValidation,
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00005520 bool DisableStatCache)
Sebastian Redle1dde812010-08-24 00:50:04 +00005521 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
5522 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
5523 Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
Jonathan D. Turner1afb6612011-07-28 17:20:23 +00005524 Consumer(0), ModuleMgr(FileMgr.getFileSystemOptions()),
5525 RelocatablePCH(false), isysroot(isysroot),
Douglas Gregorf62d43d2011-07-19 16:10:42 +00005526 DisableValidation(DisableValidation),
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00005527 DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0),
Douglas Gregorf62d43d2011-07-19 16:10:42 +00005528 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00005529 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
5530 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
5531 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
5532 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
Jonathan D. Turner1da90142011-07-21 21:15:19 +00005533 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
5534 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
5535 NumCXXBaseSpecifiersLoaded(0)
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00005536{
Douglas Gregorf62d43d2011-07-19 16:10:42 +00005537 SourceMgr.setExternalSLocEntrySource(this);
Sebastian Redle1dde812010-08-24 00:50:04 +00005538}
5539
5540ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
Douglas Gregor832d6202011-07-22 16:35:34 +00005541 Diagnostic &Diags, StringRef isysroot,
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00005542 bool DisableValidation, bool DisableStatCache)
Sebastian Redle1dde812010-08-24 00:50:04 +00005543 : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
Jonathan D. Turner1afb6612011-07-28 17:20:23 +00005544 Diags(Diags), SemaObj(0), PP(0), Context(0),
5545 Consumer(0), ModuleMgr(FileMgr.getFileSystemOptions()),
Douglas Gregorf62d43d2011-07-19 16:10:42 +00005546 RelocatablePCH(false), isysroot(isysroot),
5547 DisableValidation(DisableValidation), DisableStatCache(DisableStatCache),
5548 NumStatHits(0), NumStatMisses(0), NumSLocEntriesRead(0),
5549 TotalNumSLocEntries(0), NumStatementsRead(0),
5550 TotalNumStatements(0), NumMacrosRead(0), TotalNumMacros(0),
5551 NumSelectorsRead(0), NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
Sebastian Redl8db9fae2010-09-22 20:19:08 +00005552 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
5553 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
Jonathan D. Turner1da90142011-07-21 21:15:19 +00005554 TotalVisibleDeclContexts(0), TotalModulesSizeInBits(0),
5555 NumCurrentElementsDeserializing(0), NumCXXBaseSpecifiersLoaded(0)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00005556{
5557 SourceMgr.setExternalSLocEntrySource(this);
Sebastian Redle1dde812010-08-24 00:50:04 +00005558}
5559
5560ASTReader::~ASTReader() {
Sebastian Redle1dde812010-08-24 00:50:04 +00005561 for (DeclContextVisibleUpdatesPending::iterator
5562 I = PendingVisibleUpdates.begin(),
5563 E = PendingVisibleUpdates.end();
5564 I != E; ++I) {
5565 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
5566 F = I->second.end();
5567 J != F; ++J)
Douglas Gregor496c7092011-08-03 15:48:04 +00005568 delete static_cast<ASTDeclContextNameLookupTable*>(J->first);
Sebastian Redle1dde812010-08-24 00:50:04 +00005569 }
5570}