Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1 | //===--- PCHReader.cpp - Precompiled Headers Reader -------------*- C++ -*-===// |
| 2 | // |
| 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 | // |
| 10 | // This file defines the PCHReader class, which reads a precompiled header. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
Chris Lattner | 92ba5ff | 2009-04-27 05:14:47 +0000 | [diff] [blame] | 13 | |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/PCHReader.h" |
Douglas Gregor | 55abb23 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 15 | #include "clang/Frontend/FrontendDiagnostic.h" |
Sebastian Redl | 85b2a6a | 2010-07-14 23:45:08 +0000 | [diff] [blame] | 16 | #include "clang/Frontend/PCHDeserializationListener.h" |
Daniel Dunbar | 732ef8a | 2009-11-11 23:58:53 +0000 | [diff] [blame] | 17 | #include "clang/Frontend/Utils.h" |
Douglas Gregor | a868bbd | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 18 | #include "../Sema/Sema.h" // FIXME: move Sema headers elsewhere |
Douglas Gregor | 1a0d0b9 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTConsumer.h" |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 20 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | feb84b0 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 21 | #include "clang/AST/Expr.h" |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 22 | #include "clang/AST/Type.h" |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 23 | #include "clang/AST/TypeLocVisitor.h" |
Chris Lattner | 34321bc | 2009-04-10 21:41:48 +0000 | [diff] [blame] | 24 | #include "clang/Lex/MacroInfo.h" |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 25 | #include "clang/Lex/PreprocessingRecord.h" |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 26 | #include "clang/Lex/Preprocessor.h" |
Steve Naroff | 3fa455a | 2009-04-24 20:03:17 +0000 | [diff] [blame] | 27 | #include "clang/Lex/HeaderSearch.h" |
Douglas Gregor | a868bbd | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 28 | #include "clang/Basic/OnDiskHashTable.h" |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 29 | #include "clang/Basic/SourceManager.h" |
Douglas Gregor | 4c7626e | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 30 | #include "clang/Basic/SourceManagerInternals.h" |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 31 | #include "clang/Basic/FileManager.h" |
Douglas Gregor | bfbde53 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 32 | #include "clang/Basic/TargetInfo.h" |
Douglas Gregor | d54f3a1 | 2009-10-05 21:07:28 +0000 | [diff] [blame] | 33 | #include "clang/Basic/Version.h" |
Daniel Dunbar | f8502d5 | 2009-10-17 23:52:28 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/StringExtras.h" |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 35 | #include "llvm/Bitcode/BitstreamReader.h" |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 36 | #include "llvm/Support/MemoryBuffer.h" |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 37 | #include "llvm/Support/ErrorHandling.h" |
Daniel Dunbar | f2ce9a2 | 2009-11-18 19:50:41 +0000 | [diff] [blame] | 38 | #include "llvm/System/Path.h" |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 39 | #include <algorithm> |
Douglas Gregor | c379c07 | 2009-04-28 18:58:38 +0000 | [diff] [blame] | 40 | #include <iterator> |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 41 | #include <cstdio> |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 42 | #include <sys/stat.h> |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 43 | using namespace clang; |
| 44 | |
| 45 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 46 | // PCH reader validator implementation |
| 47 | //===----------------------------------------------------------------------===// |
| 48 | |
| 49 | PCHReaderListener::~PCHReaderListener() {} |
| 50 | |
| 51 | bool |
| 52 | PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts) { |
| 53 | const LangOptions &PPLangOpts = PP.getLangOptions(); |
| 54 | #define PARSE_LANGOPT_BENIGN(Option) |
| 55 | #define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \ |
| 56 | if (PPLangOpts.Option != LangOpts.Option) { \ |
| 57 | Reader.Diag(DiagID) << LangOpts.Option << PPLangOpts.Option; \ |
| 58 | return true; \ |
| 59 | } |
| 60 | |
| 61 | PARSE_LANGOPT_BENIGN(Trigraphs); |
| 62 | PARSE_LANGOPT_BENIGN(BCPLComment); |
| 63 | PARSE_LANGOPT_BENIGN(DollarIdents); |
| 64 | PARSE_LANGOPT_BENIGN(AsmPreprocessor); |
| 65 | PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions); |
Chandler Carruth | e03aa55 | 2010-04-17 20:17:31 +0000 | [diff] [blame] | 66 | PARSE_LANGOPT_IMPORTANT(GNUKeywords, diag::warn_pch_gnu_keywords); |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 67 | PARSE_LANGOPT_BENIGN(ImplicitInt); |
| 68 | PARSE_LANGOPT_BENIGN(Digraphs); |
| 69 | PARSE_LANGOPT_BENIGN(HexFloats); |
| 70 | PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99); |
| 71 | PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions); |
| 72 | PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus); |
| 73 | PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x); |
| 74 | PARSE_LANGOPT_BENIGN(CXXOperatorName); |
| 75 | PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c); |
| 76 | PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2); |
| 77 | PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi); |
Fariborz Jahanian | 4587803 | 2010-02-09 19:31:38 +0000 | [diff] [blame] | 78 | PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI2, diag::warn_pch_nonfragile_abi2); |
Fariborz Jahanian | 62c5602 | 2010-04-22 21:01:59 +0000 | [diff] [blame] | 79 | PARSE_LANGOPT_IMPORTANT(NoConstantCFStrings, |
| 80 | diag::warn_pch_no_constant_cfstrings); |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 81 | PARSE_LANGOPT_BENIGN(PascalStrings); |
| 82 | PARSE_LANGOPT_BENIGN(WritableStrings); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 83 | PARSE_LANGOPT_IMPORTANT(LaxVectorConversions, |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 84 | diag::warn_pch_lax_vector_conversions); |
Nate Begeman | 9d90579 | 2009-06-25 22:57:40 +0000 | [diff] [blame] | 85 | PARSE_LANGOPT_IMPORTANT(AltiVec, diag::warn_pch_altivec); |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 86 | PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions); |
Daniel Dunbar | 925152c | 2010-02-10 18:48:44 +0000 | [diff] [blame] | 87 | PARSE_LANGOPT_IMPORTANT(SjLjExceptions, diag::warn_pch_sjlj_exceptions); |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 88 | PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime); |
| 89 | PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding); |
| 90 | PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 91 | PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics, |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 92 | diag::warn_pch_thread_safe_statics); |
Daniel Dunbar | a77eaeb | 2009-09-03 04:54:28 +0000 | [diff] [blame] | 93 | PARSE_LANGOPT_IMPORTANT(POSIXThreads, diag::warn_pch_posix_threads); |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 94 | PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks); |
| 95 | PARSE_LANGOPT_BENIGN(EmitAllDecls); |
| 96 | PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno); |
Chris Lattner | 51924e51 | 2010-06-26 21:25:03 +0000 | [diff] [blame] | 97 | PARSE_LANGOPT_BENIGN(getSignedOverflowBehavior()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 98 | PARSE_LANGOPT_IMPORTANT(HeinousExtensions, |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 99 | diag::warn_pch_heinous_extensions); |
| 100 | // FIXME: Most of the options below are benign if the macro wasn't |
| 101 | // used. Unfortunately, this means that a PCH compiled without |
| 102 | // optimization can't be used with optimization turned on, even |
| 103 | // though the only thing that changes is whether __OPTIMIZE__ was |
| 104 | // defined... but if __OPTIMIZE__ never showed up in the header, it |
| 105 | // doesn't matter. We could consider making this some special kind |
| 106 | // of check. |
| 107 | PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize); |
| 108 | PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size); |
| 109 | PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static); |
| 110 | PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level); |
| 111 | PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline); |
| 112 | PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline); |
| 113 | PARSE_LANGOPT_IMPORTANT(AccessControl, diag::warn_pch_access_control); |
| 114 | PARSE_LANGOPT_IMPORTANT(CharIsSigned, diag::warn_pch_char_signed); |
John Thompson | ed4e295 | 2009-11-05 20:14:16 +0000 | [diff] [blame] | 115 | PARSE_LANGOPT_IMPORTANT(ShortWChar, diag::warn_pch_short_wchar); |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 116 | if ((PPLangOpts.getGCMode() != 0) != (LangOpts.getGCMode() != 0)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 117 | Reader.Diag(diag::warn_pch_gc_mode) |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 118 | << LangOpts.getGCMode() << PPLangOpts.getGCMode(); |
| 119 | return true; |
| 120 | } |
| 121 | PARSE_LANGOPT_BENIGN(getVisibilityMode()); |
Daniel Dunbar | 143021e | 2009-09-21 04:16:19 +0000 | [diff] [blame] | 122 | PARSE_LANGOPT_IMPORTANT(getStackProtectorMode(), |
| 123 | diag::warn_pch_stack_protector); |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 124 | PARSE_LANGOPT_BENIGN(InstantiationDepth); |
Nate Begeman | 9d90579 | 2009-06-25 22:57:40 +0000 | [diff] [blame] | 125 | PARSE_LANGOPT_IMPORTANT(OpenCL, diag::warn_pch_opencl); |
Mike Stump | d954638 | 2009-12-12 01:27:46 +0000 | [diff] [blame] | 126 | PARSE_LANGOPT_BENIGN(CatchUndefined); |
Daniel Dunbar | 143021e | 2009-09-21 04:16:19 +0000 | [diff] [blame] | 127 | PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors); |
Douglas Gregor | 8ed0c0b | 2010-07-09 17:35:33 +0000 | [diff] [blame] | 128 | PARSE_LANGOPT_BENIGN(SpellChecking); |
Kovarththanan Rajaratnam | 39f2fbd1 | 2010-03-07 19:10:13 +0000 | [diff] [blame] | 129 | #undef PARSE_LANGOPT_IMPORTANT |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 130 | #undef PARSE_LANGOPT_BENIGN |
| 131 | |
| 132 | return false; |
| 133 | } |
| 134 | |
Daniel Dunbar | 20a682d | 2009-11-11 00:52:11 +0000 | [diff] [blame] | 135 | bool PCHValidator::ReadTargetTriple(llvm::StringRef Triple) { |
| 136 | if (Triple == PP.getTargetInfo().getTriple().str()) |
| 137 | return false; |
| 138 | |
| 139 | Reader.Diag(diag::warn_pch_target_triple) |
| 140 | << Triple << PP.getTargetInfo().getTriple().str(); |
| 141 | return true; |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Sebastian Redl | 75fbb3b | 2010-07-14 17:49:11 +0000 | [diff] [blame] | 144 | struct EmptyStringRef { |
Benjamin Kramer | 8d5609b | 2010-07-14 23:19:41 +0000 | [diff] [blame] | 145 | bool operator ()(llvm::StringRef r) const { return r.empty(); } |
Sebastian Redl | 75fbb3b | 2010-07-14 17:49:11 +0000 | [diff] [blame] | 146 | }; |
| 147 | struct EmptyBlock { |
| 148 | bool operator ()(const PCHPredefinesBlock &r) const { return r.Data.empty(); } |
| 149 | }; |
| 150 | |
| 151 | static bool EqualConcatenations(llvm::SmallVector<llvm::StringRef, 2> L, |
| 152 | PCHPredefinesBlocks R) { |
| 153 | // First, sum up the lengths. |
| 154 | unsigned LL = 0, RL = 0; |
| 155 | for (unsigned I = 0, N = L.size(); I != N; ++I) { |
| 156 | LL += L[I].size(); |
| 157 | } |
| 158 | for (unsigned I = 0, N = R.size(); I != N; ++I) { |
| 159 | RL += R[I].Data.size(); |
| 160 | } |
| 161 | if (LL != RL) |
| 162 | return false; |
| 163 | if (LL == 0 && RL == 0) |
| 164 | return true; |
| 165 | |
| 166 | // Kick out empty parts, they confuse the algorithm below. |
| 167 | L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end()); |
| 168 | R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end()); |
| 169 | |
| 170 | // Do it the hard way. At this point, both vectors must be non-empty. |
| 171 | llvm::StringRef LR = L[0], RR = R[0].Data; |
| 172 | unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size(); |
Daniel Dunbar | 01ad0a7 | 2010-07-16 00:00:11 +0000 | [diff] [blame] | 173 | (void) RN; |
Sebastian Redl | 75fbb3b | 2010-07-14 17:49:11 +0000 | [diff] [blame] | 174 | for (;;) { |
| 175 | // Compare the current pieces. |
| 176 | if (LR.size() == RR.size()) { |
| 177 | // If they're the same length, it's pretty easy. |
| 178 | if (LR != RR) |
| 179 | return false; |
| 180 | // Both pieces are done, advance. |
| 181 | ++LI; |
| 182 | ++RI; |
| 183 | // If either string is done, they're both done, since they're the same |
| 184 | // length. |
| 185 | if (LI == LN) { |
| 186 | assert(RI == RN && "Strings not the same length after all?"); |
| 187 | return true; |
| 188 | } |
| 189 | LR = L[LI]; |
| 190 | RR = R[RI].Data; |
| 191 | } else if (LR.size() < RR.size()) { |
| 192 | // Right piece is longer. |
| 193 | if (!RR.startswith(LR)) |
| 194 | return false; |
| 195 | ++LI; |
| 196 | assert(LI != LN && "Strings not the same length after all?"); |
| 197 | RR = RR.substr(LR.size()); |
| 198 | LR = L[LI]; |
| 199 | } else { |
| 200 | // Left piece is longer. |
| 201 | if (!LR.startswith(RR)) |
| 202 | return false; |
| 203 | ++RI; |
| 204 | assert(RI != RN && "Strings not the same length after all?"); |
| 205 | LR = LR.substr(RR.size()); |
| 206 | RR = R[RI].Data; |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | static std::pair<FileID, llvm::StringRef::size_type> |
| 212 | FindMacro(const PCHPredefinesBlocks &Buffers, llvm::StringRef MacroDef) { |
| 213 | std::pair<FileID, llvm::StringRef::size_type> Res; |
| 214 | for (unsigned I = 0, N = Buffers.size(); I != N; ++I) { |
| 215 | Res.second = Buffers[I].Data.find(MacroDef); |
| 216 | if (Res.second != llvm::StringRef::npos) { |
| 217 | Res.first = Buffers[I].BufferID; |
| 218 | break; |
| 219 | } |
| 220 | } |
| 221 | return Res; |
| 222 | } |
| 223 | |
| 224 | bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers, |
Daniel Dunbar | 000c4ff | 2009-11-11 05:29:04 +0000 | [diff] [blame] | 225 | llvm::StringRef OriginalFileName, |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 226 | std::string &SuggestedPredefines) { |
Daniel Dunbar | 732ef8a | 2009-11-11 23:58:53 +0000 | [diff] [blame] | 227 | // We are in the context of an implicit include, so the predefines buffer will |
| 228 | // have a #include entry for the PCH file itself (as normalized by the |
| 229 | // preprocessor initialization). Find it and skip over it in the checking |
| 230 | // below. |
Daniel Dunbar | 000c4ff | 2009-11-11 05:29:04 +0000 | [diff] [blame] | 231 | llvm::SmallString<256> PCHInclude; |
| 232 | PCHInclude += "#include \""; |
Daniel Dunbar | 732ef8a | 2009-11-11 23:58:53 +0000 | [diff] [blame] | 233 | PCHInclude += NormalizeDashIncludePath(OriginalFileName); |
Daniel Dunbar | 000c4ff | 2009-11-11 05:29:04 +0000 | [diff] [blame] | 234 | PCHInclude += "\"\n"; |
| 235 | std::pair<llvm::StringRef,llvm::StringRef> Split = |
| 236 | llvm::StringRef(PP.getPredefines()).split(PCHInclude.str()); |
| 237 | llvm::StringRef Left = Split.first, Right = Split.second; |
Ted Kremenek | 1ff615c | 2010-03-18 00:56:54 +0000 | [diff] [blame] | 238 | if (Left == PP.getPredefines()) { |
| 239 | Error("Missing PCH include entry!"); |
| 240 | return true; |
| 241 | } |
Daniel Dunbar | 000c4ff | 2009-11-11 05:29:04 +0000 | [diff] [blame] | 242 | |
Sebastian Redl | 75fbb3b | 2010-07-14 17:49:11 +0000 | [diff] [blame] | 243 | // If the concatenation of all the PCH buffers is equal to the adjusted |
| 244 | // command line, we're done. |
| 245 | // We build a SmallVector of the command line here, because we'll eventually |
| 246 | // need to support an arbitrary amount of pieces anyway (when we have chained |
| 247 | // PCH reading). |
| 248 | llvm::SmallVector<llvm::StringRef, 2> CommandLine; |
| 249 | CommandLine.push_back(Left); |
| 250 | CommandLine.push_back(Right); |
| 251 | if (EqualConcatenations(CommandLine, Buffers)) |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 252 | return false; |
| 253 | |
| 254 | SourceManager &SourceMgr = PP.getSourceManager(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 255 | |
Daniel Dunbar | 8665c7e | 2009-11-11 03:45:59 +0000 | [diff] [blame] | 256 | // The predefines buffers are different. Determine what the differences are, |
| 257 | // and whether they require us to reject the PCH file. |
Daniel Dunbar | 045f917e | 2009-11-13 16:46:11 +0000 | [diff] [blame] | 258 | llvm::SmallVector<llvm::StringRef, 8> PCHLines; |
Sebastian Redl | 75fbb3b | 2010-07-14 17:49:11 +0000 | [diff] [blame] | 259 | for (unsigned I = 0, N = Buffers.size(); I != N; ++I) |
| 260 | Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false); |
Daniel Dunbar | 045f917e | 2009-11-13 16:46:11 +0000 | [diff] [blame] | 261 | |
| 262 | llvm::SmallVector<llvm::StringRef, 8> CmdLineLines; |
| 263 | Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false); |
| 264 | Right.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false); |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 265 | |
Daniel Dunbar | 499baed | 2009-11-11 05:26:28 +0000 | [diff] [blame] | 266 | // Sort both sets of predefined buffer lines, since we allow some extra |
| 267 | // definitions and they may appear at any point in the output. |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 268 | std::sort(CmdLineLines.begin(), CmdLineLines.end()); |
| 269 | std::sort(PCHLines.begin(), PCHLines.end()); |
| 270 | |
Daniel Dunbar | 499baed | 2009-11-11 05:26:28 +0000 | [diff] [blame] | 271 | // Determine which predefines that were used to build the PCH file are missing |
| 272 | // from the command line. |
| 273 | std::vector<llvm::StringRef> MissingPredefines; |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 274 | std::set_difference(PCHLines.begin(), PCHLines.end(), |
| 275 | CmdLineLines.begin(), CmdLineLines.end(), |
| 276 | std::back_inserter(MissingPredefines)); |
| 277 | |
| 278 | bool MissingDefines = false; |
| 279 | bool ConflictingDefines = false; |
| 280 | for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) { |
Daniel Dunbar | 499baed | 2009-11-11 05:26:28 +0000 | [diff] [blame] | 281 | llvm::StringRef Missing = MissingPredefines[I]; |
| 282 | if (!Missing.startswith("#define ")) { |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 283 | Reader.Diag(diag::warn_pch_compiler_options_mismatch); |
| 284 | return true; |
| 285 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 286 | |
Daniel Dunbar | 8665c7e | 2009-11-11 03:45:59 +0000 | [diff] [blame] | 287 | // This is a macro definition. Determine the name of the macro we're |
| 288 | // defining. |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 289 | std::string::size_type StartOfMacroName = strlen("#define "); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 290 | std::string::size_type EndOfMacroName |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 291 | = Missing.find_first_of("( \n\r", StartOfMacroName); |
| 292 | assert(EndOfMacroName != std::string::npos && |
| 293 | "Couldn't find the end of the macro name"); |
Daniel Dunbar | 499baed | 2009-11-11 05:26:28 +0000 | [diff] [blame] | 294 | llvm::StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName); |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 295 | |
Daniel Dunbar | 8665c7e | 2009-11-11 03:45:59 +0000 | [diff] [blame] | 296 | // Determine whether this macro was given a different definition on the |
| 297 | // command line. |
Daniel Dunbar | 499baed | 2009-11-11 05:26:28 +0000 | [diff] [blame] | 298 | std::string MacroDefStart = "#define " + MacroName.str(); |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 299 | std::string::size_type MacroDefLen = MacroDefStart.size(); |
Daniel Dunbar | 045f917e | 2009-11-13 16:46:11 +0000 | [diff] [blame] | 300 | llvm::SmallVector<llvm::StringRef, 8>::iterator ConflictPos |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 301 | = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(), |
| 302 | MacroDefStart); |
| 303 | for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) { |
Daniel Dunbar | 8665c7e | 2009-11-11 03:45:59 +0000 | [diff] [blame] | 304 | if (!ConflictPos->startswith(MacroDefStart)) { |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 305 | // Different macro; we're done. |
| 306 | ConflictPos = CmdLineLines.end(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 307 | break; |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 308 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 309 | |
| 310 | assert(ConflictPos->size() > MacroDefLen && |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 311 | "Invalid #define in predefines buffer?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 312 | if ((*ConflictPos)[MacroDefLen] != ' ' && |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 313 | (*ConflictPos)[MacroDefLen] != '(') |
| 314 | continue; // Longer macro name; keep trying. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 315 | |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 316 | // We found a conflicting macro definition. |
| 317 | break; |
| 318 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 319 | |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 320 | if (ConflictPos != CmdLineLines.end()) { |
| 321 | Reader.Diag(diag::warn_cmdline_conflicting_macro_def) |
| 322 | << MacroName; |
| 323 | |
| 324 | // Show the definition of this macro within the PCH file. |
Sebastian Redl | 75fbb3b | 2010-07-14 17:49:11 +0000 | [diff] [blame] | 325 | std::pair<FileID, llvm::StringRef::size_type> MacroLoc = |
| 326 | FindMacro(Buffers, Missing); |
| 327 | assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!"); |
| 328 | SourceLocation PCHMissingLoc = |
| 329 | SourceMgr.getLocForStartOfFile(MacroLoc.first) |
| 330 | .getFileLocWithOffset(MacroLoc.second); |
Daniel Dunbar | 499baed | 2009-11-11 05:26:28 +0000 | [diff] [blame] | 331 | Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName; |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 332 | |
| 333 | ConflictingDefines = true; |
| 334 | continue; |
| 335 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 336 | |
Daniel Dunbar | 8665c7e | 2009-11-11 03:45:59 +0000 | [diff] [blame] | 337 | // If the macro doesn't conflict, then we'll just pick up the macro |
| 338 | // definition from the PCH file. Warn the user that they made a mistake. |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 339 | if (ConflictingDefines) |
| 340 | continue; // Don't complain if there are already conflicting defs |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 341 | |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 342 | if (!MissingDefines) { |
| 343 | Reader.Diag(diag::warn_cmdline_missing_macro_defs); |
| 344 | MissingDefines = true; |
| 345 | } |
| 346 | |
| 347 | // Show the definition of this macro within the PCH file. |
Sebastian Redl | 75fbb3b | 2010-07-14 17:49:11 +0000 | [diff] [blame] | 348 | std::pair<FileID, llvm::StringRef::size_type> MacroLoc = |
| 349 | FindMacro(Buffers, Missing); |
| 350 | assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!"); |
| 351 | SourceLocation PCHMissingLoc = |
| 352 | SourceMgr.getLocForStartOfFile(MacroLoc.first) |
| 353 | .getFileLocWithOffset(MacroLoc.second); |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 354 | Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch); |
| 355 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 356 | |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 357 | if (ConflictingDefines) |
| 358 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 359 | |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 360 | // Determine what predefines were introduced based on command-line |
| 361 | // parameters that were not present when building the PCH |
| 362 | // file. Extra #defines are okay, so long as the identifiers being |
| 363 | // defined were not used within the precompiled header. |
Daniel Dunbar | 499baed | 2009-11-11 05:26:28 +0000 | [diff] [blame] | 364 | std::vector<llvm::StringRef> ExtraPredefines; |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 365 | std::set_difference(CmdLineLines.begin(), CmdLineLines.end(), |
| 366 | PCHLines.begin(), PCHLines.end(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 367 | std::back_inserter(ExtraPredefines)); |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 368 | for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) { |
Daniel Dunbar | 499baed | 2009-11-11 05:26:28 +0000 | [diff] [blame] | 369 | llvm::StringRef &Extra = ExtraPredefines[I]; |
| 370 | if (!Extra.startswith("#define ")) { |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 371 | Reader.Diag(diag::warn_pch_compiler_options_mismatch); |
| 372 | return true; |
| 373 | } |
| 374 | |
| 375 | // This is an extra macro definition. Determine the name of the |
| 376 | // macro we're defining. |
| 377 | std::string::size_type StartOfMacroName = strlen("#define "); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 378 | std::string::size_type EndOfMacroName |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 379 | = Extra.find_first_of("( \n\r", StartOfMacroName); |
| 380 | assert(EndOfMacroName != std::string::npos && |
| 381 | "Couldn't find the end of the macro name"); |
Daniel Dunbar | 499baed | 2009-11-11 05:26:28 +0000 | [diff] [blame] | 382 | llvm::StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName); |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 383 | |
| 384 | // Check whether this name was used somewhere in the PCH file. If |
| 385 | // so, defining it as a macro could change behavior, so we reject |
| 386 | // the PCH file. |
Daniel Dunbar | 499baed | 2009-11-11 05:26:28 +0000 | [diff] [blame] | 387 | if (IdentifierInfo *II = Reader.get(MacroName)) { |
Daniel Dunbar | 045c92f | 2009-11-11 00:52:00 +0000 | [diff] [blame] | 388 | Reader.Diag(diag::warn_macro_name_used_in_pch) << II; |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 389 | return true; |
| 390 | } |
| 391 | |
| 392 | // Add this definition to the suggested predefines buffer. |
| 393 | SuggestedPredefines += Extra; |
| 394 | SuggestedPredefines += '\n'; |
| 395 | } |
| 396 | |
| 397 | // If we get here, it's because the predefines buffer had compatible |
| 398 | // contents. Accept the PCH file. |
| 399 | return false; |
| 400 | } |
| 401 | |
Douglas Gregor | 5712ebc | 2010-03-16 16:35:32 +0000 | [diff] [blame] | 402 | void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI, |
| 403 | unsigned ID) { |
| 404 | PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID); |
| 405 | ++NumHeaderInfos; |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | void PCHValidator::ReadCounter(unsigned Value) { |
| 409 | PP.setCounterValue(Value); |
| 410 | } |
| 411 | |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 412 | //===----------------------------------------------------------------------===// |
Douglas Gregor | a868bbd | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 413 | // PCH reader implementation |
| 414 | //===----------------------------------------------------------------------===// |
| 415 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 416 | PCHReader::PCHReader(Preprocessor &PP, ASTContext *Context, |
Douglas Gregor | ce3a829 | 2010-07-27 00:27:13 +0000 | [diff] [blame] | 417 | const char *isysroot, bool DisableValidation) |
Sebastian Redl | 85b2a6a | 2010-07-14 23:45:08 +0000 | [diff] [blame] | 418 | : Listener(new PCHValidator(PP, *this)), DeserializationListener(0), |
| 419 | SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()), |
| 420 | Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context), |
Sebastian Redl | bd1b5be | 2010-07-19 22:28:42 +0000 | [diff] [blame] | 421 | Consumer(0), MethodPoolLookupTable(0), MethodPoolLookupTableData(0), |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 422 | TotalSelectorsInMethodPool(0), SelectorOffsets(0), |
Douglas Gregor | ce3a829 | 2010-07-27 00:27:13 +0000 | [diff] [blame] | 423 | TotalNumSelectors(0), isysroot(isysroot), |
| 424 | DisableValidation(DisableValidation), NumStatHits(0), NumStatMisses(0), |
Sebastian Redl | b293a45 | 2010-07-20 21:20:32 +0000 | [diff] [blame] | 425 | NumSLocEntriesRead(0), TotalNumSLocEntries(0), NumStatementsRead(0), |
| 426 | TotalNumStatements(0), NumMacrosRead(0), NumMethodPoolSelectorsRead(0), |
| 427 | NumMethodPoolMisses(0), TotalNumMacros(0), NumLexicalDeclContextsRead(0), |
| 428 | TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0), |
| 429 | TotalVisibleDeclContexts(0), CurrentlyLoadingTypeOrDecl(0) { |
Douglas Gregor | 0086a5a | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 430 | RelocatablePCH = false; |
| 431 | } |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 432 | |
| 433 | PCHReader::PCHReader(SourceManager &SourceMgr, FileManager &FileMgr, |
Douglas Gregor | ce3a829 | 2010-07-27 00:27:13 +0000 | [diff] [blame] | 434 | Diagnostic &Diags, const char *isysroot, |
| 435 | bool DisableValidation) |
Sebastian Redl | 85b2a6a | 2010-07-14 23:45:08 +0000 | [diff] [blame] | 436 | : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr), |
Sebastian Redl | 3452281 | 2010-07-16 17:50:48 +0000 | [diff] [blame] | 437 | Diags(Diags), SemaObj(0), PP(0), Context(0), Consumer(0), |
Chris Lattner | 92ba5ff | 2009-04-27 05:14:47 +0000 | [diff] [blame] | 438 | MethodPoolLookupTable(0), MethodPoolLookupTableData(0), |
| 439 | TotalSelectorsInMethodPool(0), SelectorOffsets(0), |
Douglas Gregor | ce3a829 | 2010-07-27 00:27:13 +0000 | [diff] [blame] | 440 | TotalNumSelectors(0), isysroot(isysroot), |
| 441 | DisableValidation(DisableValidation), NumStatHits(0), NumStatMisses(0), |
Sebastian Redl | b293a45 | 2010-07-20 21:20:32 +0000 | [diff] [blame] | 442 | NumSLocEntriesRead(0), TotalNumSLocEntries(0), NumStatementsRead(0), |
| 443 | TotalNumStatements(0), NumMacrosRead(0), NumMethodPoolSelectorsRead(0), |
| 444 | NumMethodPoolMisses(0), TotalNumMacros(0), NumLexicalDeclContextsRead(0), |
| 445 | TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0), |
| 446 | TotalVisibleDeclContexts(0), CurrentlyLoadingTypeOrDecl(0) { |
Douglas Gregor | 0086a5a | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 447 | RelocatablePCH = false; |
| 448 | } |
Chris Lattner | 92ba5ff | 2009-04-27 05:14:47 +0000 | [diff] [blame] | 449 | |
Sebastian Redl | 3452281 | 2010-07-16 17:50:48 +0000 | [diff] [blame] | 450 | PCHReader::~PCHReader() { |
| 451 | for (unsigned i = 0, e = Chain.size(); i != e; ++i) |
| 452 | delete Chain[e - i - 1]; |
| 453 | } |
| 454 | |
| 455 | PCHReader::PerFileData::PerFileData() |
Sebastian Redl | 9e68799 | 2010-07-19 22:06:55 +0000 | [diff] [blame] | 456 | : StatCache(0), LocalNumSLocEntries(0), LocalNumTypes(0), TypeOffsets(0), |
Sebastian Redl | bd1b5be | 2010-07-19 22:28:42 +0000 | [diff] [blame] | 457 | LocalNumDecls(0), DeclOffsets(0), LocalNumIdentifiers(0), |
Sebastian Redl | fa06144 | 2010-07-21 20:07:32 +0000 | [diff] [blame] | 458 | IdentifierOffsets(0), IdentifierTableData(0), IdentifierLookupTable(0), |
| 459 | LocalNumMacroDefinitions(0), MacroDefinitionOffsets(0), |
| 460 | NumPreallocatedPreprocessingEntities(0) |
Sebastian Redl | 3452281 | 2010-07-16 17:50:48 +0000 | [diff] [blame] | 461 | {} |
Chris Lattner | 92ba5ff | 2009-04-27 05:14:47 +0000 | [diff] [blame] | 462 | |
Chris Lattner | 92ba5ff | 2009-04-27 05:14:47 +0000 | [diff] [blame] | 463 | |
Douglas Gregor | a868bbd | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 464 | namespace { |
Benjamin Kramer | 16634c2 | 2009-11-28 10:07:24 +0000 | [diff] [blame] | 465 | class PCHMethodPoolLookupTrait { |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 466 | PCHReader &Reader; |
| 467 | |
| 468 | public: |
| 469 | typedef std::pair<ObjCMethodList, ObjCMethodList> data_type; |
| 470 | |
| 471 | typedef Selector external_key_type; |
| 472 | typedef external_key_type internal_key_type; |
| 473 | |
| 474 | explicit PCHMethodPoolLookupTrait(PCHReader &Reader) : Reader(Reader) { } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 475 | |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 476 | static bool EqualKey(const internal_key_type& a, |
| 477 | const internal_key_type& b) { |
| 478 | return a == b; |
| 479 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 480 | |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 481 | static unsigned ComputeHash(Selector Sel) { |
| 482 | unsigned N = Sel.getNumArgs(); |
| 483 | if (N == 0) |
| 484 | ++N; |
| 485 | unsigned R = 5381; |
| 486 | for (unsigned I = 0; I != N; ++I) |
| 487 | if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I)) |
Daniel Dunbar | f8502d5 | 2009-10-17 23:52:28 +0000 | [diff] [blame] | 488 | R = llvm::HashString(II->getName(), R); |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 489 | return R; |
| 490 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 491 | |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 492 | // This hopefully will just get inlined and removed by the optimizer. |
| 493 | static const internal_key_type& |
| 494 | GetInternalKey(const external_key_type& x) { return x; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 495 | |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 496 | static std::pair<unsigned, unsigned> |
| 497 | ReadKeyDataLength(const unsigned char*& d) { |
| 498 | using namespace clang::io; |
| 499 | unsigned KeyLen = ReadUnalignedLE16(d); |
| 500 | unsigned DataLen = ReadUnalignedLE16(d); |
| 501 | return std::make_pair(KeyLen, DataLen); |
| 502 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 503 | |
Douglas Gregor | 95c13f5 | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 504 | internal_key_type ReadKey(const unsigned char* d, unsigned) { |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 505 | using namespace clang::io; |
Chris Lattner | 8575daa | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 506 | SelectorTable &SelTable = Reader.getContext()->Selectors; |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 507 | unsigned N = ReadUnalignedLE16(d); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 508 | IdentifierInfo *FirstII |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 509 | = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)); |
| 510 | if (N == 0) |
| 511 | return SelTable.getNullarySelector(FirstII); |
| 512 | else if (N == 1) |
| 513 | return SelTable.getUnarySelector(FirstII); |
| 514 | |
| 515 | llvm::SmallVector<IdentifierInfo *, 16> Args; |
| 516 | Args.push_back(FirstII); |
| 517 | for (unsigned I = 1; I != N; ++I) |
| 518 | Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d))); |
| 519 | |
Douglas Gregor | 038c338 | 2009-05-22 22:45:36 +0000 | [diff] [blame] | 520 | return SelTable.getSelector(N, Args.data()); |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 521 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 522 | |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 523 | data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) { |
| 524 | using namespace clang::io; |
| 525 | unsigned NumInstanceMethods = ReadUnalignedLE16(d); |
| 526 | unsigned NumFactoryMethods = ReadUnalignedLE16(d); |
| 527 | |
| 528 | data_type Result; |
| 529 | |
| 530 | // Load instance methods |
| 531 | ObjCMethodList *Prev = 0; |
| 532 | for (unsigned I = 0; I != NumInstanceMethods; ++I) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 533 | ObjCMethodDecl *Method |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 534 | = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d))); |
| 535 | if (!Result.first.Method) { |
| 536 | // This is the first method, which is the easy case. |
| 537 | Result.first.Method = Method; |
| 538 | Prev = &Result.first; |
| 539 | continue; |
| 540 | } |
| 541 | |
Ted Kremenek | da4abf1 | 2010-02-11 00:53:01 +0000 | [diff] [blame] | 542 | ObjCMethodList *Mem = |
| 543 | Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>(); |
| 544 | Prev->Next = new (Mem) ObjCMethodList(Method, 0); |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 545 | Prev = Prev->Next; |
| 546 | } |
| 547 | |
| 548 | // Load factory methods |
| 549 | Prev = 0; |
| 550 | for (unsigned I = 0; I != NumFactoryMethods; ++I) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 551 | ObjCMethodDecl *Method |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 552 | = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d))); |
| 553 | if (!Result.second.Method) { |
| 554 | // This is the first method, which is the easy case. |
| 555 | Result.second.Method = Method; |
| 556 | Prev = &Result.second; |
| 557 | continue; |
| 558 | } |
| 559 | |
Ted Kremenek | da4abf1 | 2010-02-11 00:53:01 +0000 | [diff] [blame] | 560 | ObjCMethodList *Mem = |
| 561 | Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>(); |
| 562 | Prev->Next = new (Mem) ObjCMethodList(Method, 0); |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 563 | Prev = Prev->Next; |
| 564 | } |
| 565 | |
| 566 | return Result; |
| 567 | } |
| 568 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 569 | |
| 570 | } // end anonymous namespace |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 571 | |
| 572 | /// \brief The on-disk hash table used for the global method pool. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 573 | typedef OnDiskChainedHashTable<PCHMethodPoolLookupTrait> |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 574 | PCHMethodPoolLookupTable; |
| 575 | |
| 576 | namespace { |
Benjamin Kramer | 16634c2 | 2009-11-28 10:07:24 +0000 | [diff] [blame] | 577 | class PCHIdentifierLookupTrait { |
Douglas Gregor | a868bbd | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 578 | PCHReader &Reader; |
Sebastian Redl | 4e6c567 | 2010-07-21 22:31:37 +0000 | [diff] [blame] | 579 | llvm::BitstreamCursor &Stream; |
Douglas Gregor | a868bbd | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 580 | |
| 581 | // If we know the IdentifierInfo in advance, it is here and we will |
| 582 | // not build a new one. Used when deserializing information about an |
| 583 | // identifier that was constructed before the PCH file was read. |
| 584 | IdentifierInfo *KnownII; |
| 585 | |
| 586 | public: |
| 587 | typedef IdentifierInfo * data_type; |
| 588 | |
| 589 | typedef const std::pair<const char*, unsigned> external_key_type; |
| 590 | |
| 591 | typedef external_key_type internal_key_type; |
| 592 | |
Sebastian Redl | 4e6c567 | 2010-07-21 22:31:37 +0000 | [diff] [blame] | 593 | PCHIdentifierLookupTrait(PCHReader &Reader, llvm::BitstreamCursor &Stream, |
| 594 | IdentifierInfo *II = 0) |
| 595 | : Reader(Reader), Stream(Stream), KnownII(II) { } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 596 | |
Douglas Gregor | a868bbd | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 597 | static bool EqualKey(const internal_key_type& a, |
| 598 | const internal_key_type& b) { |
| 599 | return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0 |
| 600 | : false; |
| 601 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 602 | |
Douglas Gregor | a868bbd | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 603 | static unsigned ComputeHash(const internal_key_type& a) { |
Daniel Dunbar | f8502d5 | 2009-10-17 23:52:28 +0000 | [diff] [blame] | 604 | return llvm::HashString(llvm::StringRef(a.first, a.second)); |
Douglas Gregor | a868bbd | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 605 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 606 | |
Douglas Gregor | a868bbd | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 607 | // This hopefully will just get inlined and removed by the optimizer. |
| 608 | static const internal_key_type& |
| 609 | GetInternalKey(const external_key_type& x) { return x; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 610 | |
Douglas Gregor | a868bbd | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 611 | static std::pair<unsigned, unsigned> |
| 612 | ReadKeyDataLength(const unsigned char*& d) { |
| 613 | using namespace clang::io; |
Douglas Gregor | 6b7bf5a | 2009-04-25 20:26:24 +0000 | [diff] [blame] | 614 | unsigned DataLen = ReadUnalignedLE16(d); |
Douglas Gregor | 5287b4e | 2009-04-25 21:04:17 +0000 | [diff] [blame] | 615 | unsigned KeyLen = ReadUnalignedLE16(d); |
Douglas Gregor | a868bbd | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 616 | return std::make_pair(KeyLen, DataLen); |
| 617 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 618 | |
Douglas Gregor | a868bbd | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 619 | static std::pair<const char*, unsigned> |
| 620 | ReadKey(const unsigned char* d, unsigned n) { |
| 621 | assert(n >= 2 && d[n-1] == '\0'); |
| 622 | return std::make_pair((const char*) d, n-1); |
| 623 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 624 | |
| 625 | IdentifierInfo *ReadData(const internal_key_type& k, |
Douglas Gregor | a868bbd | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 626 | const unsigned char* d, |
| 627 | unsigned DataLen) { |
| 628 | using namespace clang::io; |
Douglas Gregor | 1d583f2 | 2009-04-28 21:18:29 +0000 | [diff] [blame] | 629 | pch::IdentID ID = ReadUnalignedLE32(d); |
| 630 | bool IsInteresting = ID & 0x01; |
| 631 | |
| 632 | // Wipe out the "is interesting" bit. |
| 633 | ID = ID >> 1; |
| 634 | |
| 635 | if (!IsInteresting) { |
| 636 | // For unintersting identifiers, just build the IdentifierInfo |
| 637 | // and associate it with the persistent ID. |
| 638 | IdentifierInfo *II = KnownII; |
| 639 | if (!II) |
| 640 | II = &Reader.getIdentifierTable().CreateIdentifierInfo( |
| 641 | k.first, k.first + k.second); |
| 642 | Reader.SetIdentifierInfo(ID, II); |
| 643 | return II; |
| 644 | } |
| 645 | |
Douglas Gregor | b925652 | 2009-04-28 21:32:13 +0000 | [diff] [blame] | 646 | unsigned Bits = ReadUnalignedLE16(d); |
Douglas Gregor | 4621c6a | 2009-04-22 18:49:13 +0000 | [diff] [blame] | 647 | bool CPlusPlusOperatorKeyword = Bits & 0x01; |
| 648 | Bits >>= 1; |
| 649 | bool Poisoned = Bits & 0x01; |
| 650 | Bits >>= 1; |
| 651 | bool ExtensionToken = Bits & 0x01; |
| 652 | Bits >>= 1; |
| 653 | bool hasMacroDefinition = Bits & 0x01; |
| 654 | Bits >>= 1; |
| 655 | unsigned ObjCOrBuiltinID = Bits & 0x3FF; |
| 656 | Bits >>= 10; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 657 | |
Douglas Gregor | 4621c6a | 2009-04-22 18:49:13 +0000 | [diff] [blame] | 658 | assert(Bits == 0 && "Extra bits in the identifier?"); |
Douglas Gregor | b925652 | 2009-04-28 21:32:13 +0000 | [diff] [blame] | 659 | DataLen -= 6; |
Douglas Gregor | a868bbd | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 660 | |
| 661 | // Build the IdentifierInfo itself and link the identifier ID with |
| 662 | // the new IdentifierInfo. |
| 663 | IdentifierInfo *II = KnownII; |
| 664 | if (!II) |
Douglas Gregor | 6b7bf5a | 2009-04-25 20:26:24 +0000 | [diff] [blame] | 665 | II = &Reader.getIdentifierTable().CreateIdentifierInfo( |
| 666 | k.first, k.first + k.second); |
Douglas Gregor | a868bbd | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 667 | Reader.SetIdentifierInfo(ID, II); |
| 668 | |
Douglas Gregor | 4621c6a | 2009-04-22 18:49:13 +0000 | [diff] [blame] | 669 | // Set or check the various bits in the IdentifierInfo structure. |
| 670 | // FIXME: Load token IDs lazily, too? |
Douglas Gregor | 4621c6a | 2009-04-22 18:49:13 +0000 | [diff] [blame] | 671 | II->setObjCOrBuiltinID(ObjCOrBuiltinID); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 672 | assert(II->isExtensionToken() == ExtensionToken && |
Douglas Gregor | 4621c6a | 2009-04-22 18:49:13 +0000 | [diff] [blame] | 673 | "Incorrect extension token flag"); |
| 674 | (void)ExtensionToken; |
| 675 | II->setIsPoisoned(Poisoned); |
| 676 | assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword && |
| 677 | "Incorrect C++ operator keyword flag"); |
| 678 | (void)CPlusPlusOperatorKeyword; |
| 679 | |
Douglas Gregor | c3366a5 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 680 | // If this identifier is a macro, deserialize the macro |
| 681 | // definition. |
| 682 | if (hasMacroDefinition) { |
Douglas Gregor | b925652 | 2009-04-28 21:32:13 +0000 | [diff] [blame] | 683 | uint32_t Offset = ReadUnalignedLE32(d); |
Sebastian Redl | 4e6c567 | 2010-07-21 22:31:37 +0000 | [diff] [blame] | 684 | Reader.ReadMacroRecord(Stream, Offset); |
Douglas Gregor | b925652 | 2009-04-28 21:32:13 +0000 | [diff] [blame] | 685 | DataLen -= 4; |
Douglas Gregor | c3366a5 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 686 | } |
Douglas Gregor | a868bbd | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 687 | |
| 688 | // Read all of the declarations visible at global scope with this |
| 689 | // name. |
Chris Lattner | 1d72888 | 2009-04-27 22:17:41 +0000 | [diff] [blame] | 690 | if (Reader.getContext() == 0) return II; |
Douglas Gregor | 1342e84 | 2009-07-06 18:54:52 +0000 | [diff] [blame] | 691 | if (DataLen > 0) { |
| 692 | llvm::SmallVector<uint32_t, 4> DeclIDs; |
| 693 | for (; DataLen > 0; DataLen -= 4) |
| 694 | DeclIDs.push_back(ReadUnalignedLE32(d)); |
| 695 | Reader.SetGloballyVisibleDecls(II, DeclIDs); |
Douglas Gregor | a868bbd | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 696 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 697 | |
Douglas Gregor | a868bbd | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 698 | return II; |
| 699 | } |
| 700 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 701 | |
| 702 | } // end anonymous namespace |
Douglas Gregor | a868bbd | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 703 | |
| 704 | /// \brief The on-disk hash table used to contain information about |
| 705 | /// all of the identifiers in the program. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 706 | typedef OnDiskChainedHashTable<PCHIdentifierLookupTrait> |
Douglas Gregor | a868bbd | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 707 | PCHIdentifierLookupTable; |
| 708 | |
Ted Kremenek | 1ff615c | 2010-03-18 00:56:54 +0000 | [diff] [blame] | 709 | void PCHReader::Error(const char *Msg) { |
| 710 | Diag(diag::err_fe_pch_malformed) << Msg; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Sebastian Redl | 75fbb3b | 2010-07-14 17:49:11 +0000 | [diff] [blame] | 713 | /// \brief Check the contents of the concatenation of all predefines buffers in |
| 714 | /// the PCH chain against the contents of the predefines buffer of the current |
| 715 | /// compiler invocation. |
Douglas Gregor | 92863e4 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 716 | /// |
Sebastian Redl | 75fbb3b | 2010-07-14 17:49:11 +0000 | [diff] [blame] | 717 | /// The contents should be the same. If not, then some command-line option |
| 718 | /// changed the preprocessor state and we must probably reject the PCH file. |
Douglas Gregor | 92863e4 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 719 | /// |
| 720 | /// \returns true if there was a mismatch (in which case the PCH file |
| 721 | /// should be ignored), or false otherwise. |
Sebastian Redl | 75fbb3b | 2010-07-14 17:49:11 +0000 | [diff] [blame] | 722 | bool PCHReader::CheckPredefinesBuffers() { |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 723 | if (Listener) |
Sebastian Redl | 75fbb3b | 2010-07-14 17:49:11 +0000 | [diff] [blame] | 724 | return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers, |
Daniel Dunbar | 000c4ff | 2009-11-11 05:29:04 +0000 | [diff] [blame] | 725 | ActualOriginalFileName, |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 726 | SuggestedPredefines); |
Douglas Gregor | c379c07 | 2009-04-28 18:58:38 +0000 | [diff] [blame] | 727 | return false; |
Douglas Gregor | 92863e4 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 728 | } |
| 729 | |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 730 | //===----------------------------------------------------------------------===// |
| 731 | // Source Manager Deserialization |
| 732 | //===----------------------------------------------------------------------===// |
| 733 | |
Douglas Gregor | 4c7626e | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 734 | /// \brief Read the line table in the source manager block. |
| 735 | /// \returns true if ther was an error. |
Sebastian Redl | b293a45 | 2010-07-20 21:20:32 +0000 | [diff] [blame] | 736 | bool PCHReader::ParseLineTable(llvm::SmallVectorImpl<uint64_t> &Record) { |
Douglas Gregor | 4c7626e | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 737 | unsigned Idx = 0; |
| 738 | LineTableInfo &LineTable = SourceMgr.getLineTable(); |
| 739 | |
| 740 | // Parse the file names |
Douglas Gregor | a885465 | 2009-04-13 17:12:42 +0000 | [diff] [blame] | 741 | std::map<int, int> FileIDs; |
| 742 | for (int I = 0, N = Record[Idx++]; I != N; ++I) { |
Douglas Gregor | 4c7626e | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 743 | // Extract the file name |
| 744 | unsigned FilenameLen = Record[Idx++]; |
| 745 | std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen); |
| 746 | Idx += FilenameLen; |
Douglas Gregor | 0086a5a | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 747 | MaybeAddSystemRootToFilename(Filename); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 748 | FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(), |
Douglas Gregor | a885465 | 2009-04-13 17:12:42 +0000 | [diff] [blame] | 749 | Filename.size()); |
Douglas Gregor | 4c7626e | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | // Parse the line entries |
| 753 | std::vector<LineEntry> Entries; |
| 754 | while (Idx < Record.size()) { |
Argyrios Kyrtzidis | e3029a7 | 2010-07-02 11:55:05 +0000 | [diff] [blame] | 755 | int FID = Record[Idx++]; |
Douglas Gregor | 4c7626e | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 756 | |
| 757 | // Extract the line entries |
| 758 | unsigned NumEntries = Record[Idx++]; |
Argyrios Kyrtzidis | e3029a7 | 2010-07-02 11:55:05 +0000 | [diff] [blame] | 759 | assert(NumEntries && "Numentries is 00000"); |
Douglas Gregor | 4c7626e | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 760 | Entries.clear(); |
| 761 | Entries.reserve(NumEntries); |
| 762 | for (unsigned I = 0; I != NumEntries; ++I) { |
| 763 | unsigned FileOffset = Record[Idx++]; |
| 764 | unsigned LineNo = Record[Idx++]; |
Argyrios Kyrtzidis | e3029a7 | 2010-07-02 11:55:05 +0000 | [diff] [blame] | 765 | int FilenameID = FileIDs[Record[Idx++]]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 766 | SrcMgr::CharacteristicKind FileKind |
Douglas Gregor | 4c7626e | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 767 | = (SrcMgr::CharacteristicKind)Record[Idx++]; |
| 768 | unsigned IncludeOffset = Record[Idx++]; |
| 769 | Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID, |
| 770 | FileKind, IncludeOffset)); |
| 771 | } |
| 772 | LineTable.AddEntry(FID, Entries); |
| 773 | } |
| 774 | |
| 775 | return false; |
| 776 | } |
| 777 | |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 778 | namespace { |
| 779 | |
Benjamin Kramer | 16634c2 | 2009-11-28 10:07:24 +0000 | [diff] [blame] | 780 | class PCHStatData { |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 781 | public: |
| 782 | const bool hasStat; |
| 783 | const ino_t ino; |
| 784 | const dev_t dev; |
| 785 | const mode_t mode; |
| 786 | const time_t mtime; |
| 787 | const off_t size; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 788 | |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 789 | PCHStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 790 | : hasStat(true), ino(i), dev(d), mode(mo), mtime(m), size(s) {} |
| 791 | |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 792 | PCHStatData() |
| 793 | : hasStat(false), ino(0), dev(0), mode(0), mtime(0), size(0) {} |
| 794 | }; |
| 795 | |
Benjamin Kramer | 16634c2 | 2009-11-28 10:07:24 +0000 | [diff] [blame] | 796 | class PCHStatLookupTrait { |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 797 | public: |
| 798 | typedef const char *external_key_type; |
| 799 | typedef const char *internal_key_type; |
| 800 | |
| 801 | typedef PCHStatData data_type; |
| 802 | |
| 803 | static unsigned ComputeHash(const char *path) { |
Daniel Dunbar | f8502d5 | 2009-10-17 23:52:28 +0000 | [diff] [blame] | 804 | return llvm::HashString(path); |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | static internal_key_type GetInternalKey(const char *path) { return path; } |
| 808 | |
| 809 | static bool EqualKey(internal_key_type a, internal_key_type b) { |
| 810 | return strcmp(a, b) == 0; |
| 811 | } |
| 812 | |
| 813 | static std::pair<unsigned, unsigned> |
| 814 | ReadKeyDataLength(const unsigned char*& d) { |
| 815 | unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d); |
| 816 | unsigned DataLen = (unsigned) *d++; |
| 817 | return std::make_pair(KeyLen + 1, DataLen); |
| 818 | } |
| 819 | |
| 820 | static internal_key_type ReadKey(const unsigned char *d, unsigned) { |
| 821 | return (const char *)d; |
| 822 | } |
| 823 | |
| 824 | static data_type ReadData(const internal_key_type, const unsigned char *d, |
| 825 | unsigned /*DataLen*/) { |
| 826 | using namespace clang::io; |
| 827 | |
| 828 | if (*d++ == 1) |
| 829 | return data_type(); |
| 830 | |
| 831 | ino_t ino = (ino_t) ReadUnalignedLE32(d); |
| 832 | dev_t dev = (dev_t) ReadUnalignedLE32(d); |
| 833 | mode_t mode = (mode_t) ReadUnalignedLE16(d); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 834 | time_t mtime = (time_t) ReadUnalignedLE64(d); |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 835 | off_t size = (off_t) ReadUnalignedLE64(d); |
| 836 | return data_type(ino, dev, mode, mtime, size); |
| 837 | } |
| 838 | }; |
| 839 | |
| 840 | /// \brief stat() cache for precompiled headers. |
| 841 | /// |
| 842 | /// This cache is very similar to the stat cache used by pretokenized |
| 843 | /// headers. |
Benjamin Kramer | 16634c2 | 2009-11-28 10:07:24 +0000 | [diff] [blame] | 844 | class PCHStatCache : public StatSysCallCache { |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 845 | typedef OnDiskChainedHashTable<PCHStatLookupTrait> CacheTy; |
| 846 | CacheTy *Cache; |
| 847 | |
| 848 | unsigned &NumStatHits, &NumStatMisses; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 849 | public: |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 850 | PCHStatCache(const unsigned char *Buckets, |
| 851 | const unsigned char *Base, |
| 852 | unsigned &NumStatHits, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 853 | unsigned &NumStatMisses) |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 854 | : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) { |
| 855 | Cache = CacheTy::Create(Buckets, Base); |
| 856 | } |
| 857 | |
| 858 | ~PCHStatCache() { delete Cache; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 859 | |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 860 | int stat(const char *path, struct stat *buf) { |
| 861 | // Do the lookup for the file's data in the PCH file. |
| 862 | CacheTy::iterator I = Cache->find(path); |
| 863 | |
| 864 | // If we don't get a hit in the PCH file just forward to 'stat'. |
| 865 | if (I == Cache->end()) { |
| 866 | ++NumStatMisses; |
Douglas Gregor | d2eb58a | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 867 | return StatSysCallCache::stat(path, buf); |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 868 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 869 | |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 870 | ++NumStatHits; |
| 871 | PCHStatData Data = *I; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 872 | |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 873 | if (!Data.hasStat) |
| 874 | return 1; |
| 875 | |
| 876 | buf->st_ino = Data.ino; |
| 877 | buf->st_dev = Data.dev; |
| 878 | buf->st_mtime = Data.mtime; |
| 879 | buf->st_mode = Data.mode; |
| 880 | buf->st_size = Data.size; |
| 881 | return 0; |
| 882 | } |
| 883 | }; |
| 884 | } // end anonymous namespace |
| 885 | |
| 886 | |
Sebastian Redl | 393f8b7 | 2010-07-19 20:52:06 +0000 | [diff] [blame] | 887 | /// \brief Read a source manager block |
| 888 | PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock(PerFileData &F) { |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 889 | using namespace SrcMgr; |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 890 | |
Sebastian Redl | 393f8b7 | 2010-07-19 20:52:06 +0000 | [diff] [blame] | 891 | llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor; |
Sebastian Redl | 3452281 | 2010-07-16 17:50:48 +0000 | [diff] [blame] | 892 | |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 893 | // Set the source-location entry cursor to the current position in |
| 894 | // the stream. This cursor will be used to read the contents of the |
| 895 | // source manager block initially, and then lazily read |
| 896 | // source-location entries as needed. |
Sebastian Redl | 393f8b7 | 2010-07-19 20:52:06 +0000 | [diff] [blame] | 897 | SLocEntryCursor = F.Stream; |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 898 | |
| 899 | // The stream itself is going to skip over the source manager block. |
Sebastian Redl | 393f8b7 | 2010-07-19 20:52:06 +0000 | [diff] [blame] | 900 | if (F.Stream.SkipBlock()) { |
Douglas Gregor | 6f00bf8 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 901 | Error("malformed block record in PCH file"); |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 902 | return Failure; |
| 903 | } |
| 904 | |
| 905 | // Enter the source manager block. |
| 906 | if (SLocEntryCursor.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) { |
Douglas Gregor | 6f00bf8 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 907 | Error("malformed source manager block record in PCH file"); |
Douglas Gregor | 92863e4 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 908 | return Failure; |
| 909 | } |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 910 | |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 911 | RecordData Record; |
| 912 | while (true) { |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 913 | unsigned Code = SLocEntryCursor.ReadCode(); |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 914 | if (Code == llvm::bitc::END_BLOCK) { |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 915 | if (SLocEntryCursor.ReadBlockEnd()) { |
Douglas Gregor | 6f00bf8 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 916 | Error("error at end of Source Manager block in PCH file"); |
Douglas Gregor | 92863e4 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 917 | return Failure; |
| 918 | } |
Douglas Gregor | 92863e4 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 919 | return Success; |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 920 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 921 | |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 922 | if (Code == llvm::bitc::ENTER_SUBBLOCK) { |
| 923 | // No known subblocks, always skip them. |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 924 | SLocEntryCursor.ReadSubBlockID(); |
| 925 | if (SLocEntryCursor.SkipBlock()) { |
Douglas Gregor | 6f00bf8 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 926 | Error("malformed block record in PCH file"); |
Douglas Gregor | 92863e4 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 927 | return Failure; |
| 928 | } |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 929 | continue; |
| 930 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 931 | |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 932 | if (Code == llvm::bitc::DEFINE_ABBREV) { |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 933 | SLocEntryCursor.ReadAbbrevRecord(); |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 934 | continue; |
| 935 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 936 | |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 937 | // Read a record. |
| 938 | const char *BlobStart; |
| 939 | unsigned BlobLen; |
| 940 | Record.clear(); |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 941 | switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) { |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 942 | default: // Default behavior: ignore. |
| 943 | break; |
| 944 | |
Chris Lattner | 184e65d | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 945 | case pch::SM_LINE_TABLE: |
Sebastian Redl | b293a45 | 2010-07-20 21:20:32 +0000 | [diff] [blame] | 946 | if (ParseLineTable(Record)) |
Douglas Gregor | 4c7626e | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 947 | return Failure; |
Chris Lattner | 184e65d | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 948 | break; |
Douglas Gregor | eda6a89 | 2009-04-26 00:07:37 +0000 | [diff] [blame] | 949 | |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 950 | case pch::SM_SLOC_FILE_ENTRY: |
| 951 | case pch::SM_SLOC_BUFFER_ENTRY: |
| 952 | case pch::SM_SLOC_INSTANTIATION_ENTRY: |
| 953 | // Once we hit one of the source location entries, we're done. |
| 954 | return Success; |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 955 | } |
| 956 | } |
| 957 | } |
| 958 | |
Sebastian Redl | 0675030 | 2010-07-20 21:50:20 +0000 | [diff] [blame] | 959 | /// \brief Get a cursor that's correctly positioned for reading the source |
| 960 | /// location entry with the given ID. |
| 961 | llvm::BitstreamCursor &PCHReader::SLocCursorForID(unsigned ID) { |
| 962 | assert(ID != 0 && ID <= TotalNumSLocEntries && |
| 963 | "SLocCursorForID should only be called for real IDs."); |
| 964 | |
| 965 | ID -= 1; |
| 966 | PerFileData *F = 0; |
| 967 | for (unsigned I = 0, N = Chain.size(); I != N; ++I) { |
| 968 | F = Chain[N - I - 1]; |
| 969 | if (ID < F->LocalNumSLocEntries) |
| 970 | break; |
| 971 | ID -= F->LocalNumSLocEntries; |
| 972 | } |
| 973 | assert(F && F->LocalNumSLocEntries > ID && "Chain corrupted"); |
| 974 | |
| 975 | F->SLocEntryCursor.JumpToBit(F->SLocOffsets[ID]); |
| 976 | return F->SLocEntryCursor; |
| 977 | } |
| 978 | |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 979 | /// \brief Read in the source location entry with the given ID. |
| 980 | PCHReader::PCHReadResult PCHReader::ReadSLocEntryRecord(unsigned ID) { |
| 981 | if (ID == 0) |
| 982 | return Success; |
| 983 | |
| 984 | if (ID > TotalNumSLocEntries) { |
| 985 | Error("source location entry ID out-of-range for PCH file"); |
| 986 | return Failure; |
| 987 | } |
| 988 | |
Sebastian Redl | 0675030 | 2010-07-20 21:50:20 +0000 | [diff] [blame] | 989 | llvm::BitstreamCursor &SLocEntryCursor = SLocCursorForID(ID); |
Sebastian Redl | 3452281 | 2010-07-16 17:50:48 +0000 | [diff] [blame] | 990 | |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 991 | ++NumSLocEntriesRead; |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 992 | unsigned Code = SLocEntryCursor.ReadCode(); |
| 993 | if (Code == llvm::bitc::END_BLOCK || |
| 994 | Code == llvm::bitc::ENTER_SUBBLOCK || |
| 995 | Code == llvm::bitc::DEFINE_ABBREV) { |
| 996 | Error("incorrectly-formatted source location entry in PCH file"); |
| 997 | return Failure; |
| 998 | } |
| 999 | |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1000 | RecordData Record; |
| 1001 | const char *BlobStart; |
| 1002 | unsigned BlobLen; |
| 1003 | switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) { |
| 1004 | default: |
| 1005 | Error("incorrectly-formatted source location entry in PCH file"); |
| 1006 | return Failure; |
| 1007 | |
| 1008 | case pch::SM_SLOC_FILE_ENTRY: { |
Douglas Gregor | 0086a5a | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 1009 | std::string Filename(BlobStart, BlobStart + BlobLen); |
| 1010 | MaybeAddSystemRootToFilename(Filename); |
| 1011 | const FileEntry *File = FileMgr.getFile(Filename); |
Chris Lattner | d20dc87 | 2009-06-15 04:35:16 +0000 | [diff] [blame] | 1012 | if (File == 0) { |
| 1013 | std::string ErrorStr = "could not find file '"; |
Douglas Gregor | 0086a5a | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 1014 | ErrorStr += Filename; |
Chris Lattner | d20dc87 | 2009-06-15 04:35:16 +0000 | [diff] [blame] | 1015 | ErrorStr += "' referenced by PCH file"; |
| 1016 | Error(ErrorStr.c_str()); |
| 1017 | return Failure; |
| 1018 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1019 | |
Douglas Gregor | b41ca8f | 2010-03-21 22:49:54 +0000 | [diff] [blame] | 1020 | if (Record.size() < 10) { |
Ted Kremenek | abb1ddd | 2010-03-18 21:23:05 +0000 | [diff] [blame] | 1021 | Error("source location entry is incorrect"); |
| 1022 | return Failure; |
| 1023 | } |
| 1024 | |
Douglas Gregor | ce3a829 | 2010-07-27 00:27:13 +0000 | [diff] [blame] | 1025 | if (!DisableValidation && |
| 1026 | ((off_t)Record[4] != File->getSize() |
Douglas Gregor | 08288f2 | 2010-04-09 15:54:22 +0000 | [diff] [blame] | 1027 | #if !defined(LLVM_ON_WIN32) |
| 1028 | // In our regression testing, the Windows file system seems to |
| 1029 | // have inconsistent modification times that sometimes |
| 1030 | // erroneously trigger this error-handling path. |
Douglas Gregor | ce3a829 | 2010-07-27 00:27:13 +0000 | [diff] [blame] | 1031 | || (time_t)Record[5] != File->getModificationTime() |
Douglas Gregor | 08288f2 | 2010-04-09 15:54:22 +0000 | [diff] [blame] | 1032 | #endif |
Douglas Gregor | ce3a829 | 2010-07-27 00:27:13 +0000 | [diff] [blame] | 1033 | )) { |
Douglas Gregor | b41ca8f | 2010-03-21 22:49:54 +0000 | [diff] [blame] | 1034 | Diag(diag::err_fe_pch_file_modified) |
| 1035 | << Filename; |
| 1036 | return Failure; |
| 1037 | } |
| 1038 | |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1039 | FileID FID = SourceMgr.createFileID(File, |
| 1040 | SourceLocation::getFromRawEncoding(Record[1]), |
| 1041 | (SrcMgr::CharacteristicKind)Record[2], |
| 1042 | ID, Record[0]); |
| 1043 | if (Record[3]) |
| 1044 | const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile()) |
| 1045 | .setHasLineDirectives(); |
| 1046 | |
Douglas Gregor | 5712ebc | 2010-03-16 16:35:32 +0000 | [diff] [blame] | 1047 | // Reconstruct header-search information for this file. |
| 1048 | HeaderFileInfo HFI; |
Douglas Gregor | b41ca8f | 2010-03-21 22:49:54 +0000 | [diff] [blame] | 1049 | HFI.isImport = Record[6]; |
| 1050 | HFI.DirInfo = Record[7]; |
| 1051 | HFI.NumIncludes = Record[8]; |
| 1052 | HFI.ControllingMacroID = Record[9]; |
Douglas Gregor | 5712ebc | 2010-03-16 16:35:32 +0000 | [diff] [blame] | 1053 | if (Listener) |
| 1054 | Listener->ReadHeaderFileInfo(HFI, File->getUID()); |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1055 | break; |
| 1056 | } |
| 1057 | |
| 1058 | case pch::SM_SLOC_BUFFER_ENTRY: { |
| 1059 | const char *Name = BlobStart; |
| 1060 | unsigned Offset = Record[0]; |
| 1061 | unsigned Code = SLocEntryCursor.ReadCode(); |
| 1062 | Record.clear(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1063 | unsigned RecCode |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1064 | = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen); |
Ted Kremenek | 1ff615c | 2010-03-18 00:56:54 +0000 | [diff] [blame] | 1065 | |
| 1066 | if (RecCode != pch::SM_SLOC_BUFFER_BLOB) { |
| 1067 | Error("PCH record has invalid code"); |
| 1068 | return Failure; |
| 1069 | } |
| 1070 | |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1071 | llvm::MemoryBuffer *Buffer |
Chris Lattner | 58c7934 | 2010-04-05 22:42:27 +0000 | [diff] [blame] | 1072 | = llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(BlobStart, BlobLen - 1), |
| 1073 | Name); |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1074 | FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1075 | |
Douglas Gregor | e6648fb | 2009-04-28 20:33:11 +0000 | [diff] [blame] | 1076 | if (strcmp(Name, "<built-in>") == 0) { |
Sebastian Redl | 75fbb3b | 2010-07-14 17:49:11 +0000 | [diff] [blame] | 1077 | PCHPredefinesBlock Block = { |
| 1078 | BufferID, |
| 1079 | llvm::StringRef(BlobStart, BlobLen - 1) |
| 1080 | }; |
| 1081 | PCHPredefinesBuffers.push_back(Block); |
Douglas Gregor | e6648fb | 2009-04-28 20:33:11 +0000 | [diff] [blame] | 1082 | } |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1083 | |
| 1084 | break; |
| 1085 | } |
| 1086 | |
| 1087 | case pch::SM_SLOC_INSTANTIATION_ENTRY: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1088 | SourceLocation SpellingLoc |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1089 | = SourceLocation::getFromRawEncoding(Record[1]); |
| 1090 | SourceMgr.createInstantiationLoc(SpellingLoc, |
| 1091 | SourceLocation::getFromRawEncoding(Record[2]), |
| 1092 | SourceLocation::getFromRawEncoding(Record[3]), |
| 1093 | Record[4], |
| 1094 | ID, |
| 1095 | Record[0]); |
| 1096 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1097 | } |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1098 | } |
| 1099 | |
| 1100 | return Success; |
| 1101 | } |
| 1102 | |
Chris Lattner | e78a6be | 2009-04-27 01:05:14 +0000 | [diff] [blame] | 1103 | /// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the |
| 1104 | /// specified cursor. Read the abbreviations that are at the top of the block |
| 1105 | /// and then leave the cursor pointing into the block. |
| 1106 | bool PCHReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor, |
| 1107 | unsigned BlockID) { |
| 1108 | if (Cursor.EnterSubBlock(BlockID)) { |
Douglas Gregor | 6f00bf8 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1109 | Error("malformed block record in PCH file"); |
Chris Lattner | e78a6be | 2009-04-27 01:05:14 +0000 | [diff] [blame] | 1110 | return Failure; |
| 1111 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1112 | |
Chris Lattner | e78a6be | 2009-04-27 01:05:14 +0000 | [diff] [blame] | 1113 | while (true) { |
| 1114 | unsigned Code = Cursor.ReadCode(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1115 | |
Chris Lattner | e78a6be | 2009-04-27 01:05:14 +0000 | [diff] [blame] | 1116 | // We expect all abbrevs to be at the start of the block. |
| 1117 | if (Code != llvm::bitc::DEFINE_ABBREV) |
| 1118 | return false; |
| 1119 | Cursor.ReadAbbrevRecord(); |
| 1120 | } |
| 1121 | } |
| 1122 | |
Sebastian Redl | 4e6c567 | 2010-07-21 22:31:37 +0000 | [diff] [blame] | 1123 | void PCHReader::ReadMacroRecord(llvm::BitstreamCursor &Stream, uint64_t Offset){ |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1124 | assert(PP && "Forgot to set Preprocessor ?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1125 | |
Douglas Gregor | c3366a5 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1126 | // Keep track of where we are in the stream, then jump back there |
| 1127 | // after reading this macro. |
| 1128 | SavedStreamPosition SavedPosition(Stream); |
| 1129 | |
| 1130 | Stream.JumpToBit(Offset); |
| 1131 | RecordData Record; |
| 1132 | llvm::SmallVector<IdentifierInfo*, 16> MacroArgs; |
| 1133 | MacroInfo *Macro = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1134 | |
Douglas Gregor | c3366a5 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1135 | while (true) { |
| 1136 | unsigned Code = Stream.ReadCode(); |
| 1137 | switch (Code) { |
| 1138 | case llvm::bitc::END_BLOCK: |
| 1139 | return; |
| 1140 | |
| 1141 | case llvm::bitc::ENTER_SUBBLOCK: |
| 1142 | // No known subblocks, always skip them. |
| 1143 | Stream.ReadSubBlockID(); |
| 1144 | if (Stream.SkipBlock()) { |
Douglas Gregor | 6f00bf8 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1145 | Error("malformed block record in PCH file"); |
Douglas Gregor | c3366a5 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1146 | return; |
| 1147 | } |
| 1148 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1149 | |
Douglas Gregor | c3366a5 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1150 | case llvm::bitc::DEFINE_ABBREV: |
| 1151 | Stream.ReadAbbrevRecord(); |
| 1152 | continue; |
| 1153 | default: break; |
| 1154 | } |
| 1155 | |
| 1156 | // Read a record. |
| 1157 | Record.clear(); |
| 1158 | pch::PreprocessorRecordTypes RecType = |
| 1159 | (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record); |
| 1160 | switch (RecType) { |
Douglas Gregor | c3366a5 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1161 | case pch::PP_MACRO_OBJECT_LIKE: |
| 1162 | case pch::PP_MACRO_FUNCTION_LIKE: { |
| 1163 | // If we already have a macro, that means that we've hit the end |
| 1164 | // of the definition of the macro we were looking for. We're |
| 1165 | // done. |
| 1166 | if (Macro) |
| 1167 | return; |
| 1168 | |
| 1169 | IdentifierInfo *II = DecodeIdentifierInfo(Record[0]); |
| 1170 | if (II == 0) { |
Douglas Gregor | 6f00bf8 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1171 | Error("macro must have a name in PCH file"); |
Douglas Gregor | c3366a5 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1172 | return; |
| 1173 | } |
| 1174 | SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]); |
| 1175 | bool isUsed = Record[2]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1176 | |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1177 | MacroInfo *MI = PP->AllocateMacroInfo(Loc); |
Douglas Gregor | c3366a5 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1178 | MI->setIsUsed(isUsed); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1179 | |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 1180 | unsigned NextIndex = 3; |
Douglas Gregor | c3366a5 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1181 | if (RecType == pch::PP_MACRO_FUNCTION_LIKE) { |
| 1182 | // Decode function-like macro info. |
| 1183 | bool isC99VarArgs = Record[3]; |
| 1184 | bool isGNUVarArgs = Record[4]; |
| 1185 | MacroArgs.clear(); |
| 1186 | unsigned NumArgs = Record[5]; |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 1187 | NextIndex = 6 + NumArgs; |
Douglas Gregor | c3366a5 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1188 | for (unsigned i = 0; i != NumArgs; ++i) |
| 1189 | MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i])); |
| 1190 | |
| 1191 | // Install function-like macro info. |
| 1192 | MI->setIsFunctionLike(); |
| 1193 | if (isC99VarArgs) MI->setIsC99Varargs(); |
| 1194 | if (isGNUVarArgs) MI->setIsGNUVarargs(); |
Douglas Gregor | 038c338 | 2009-05-22 22:45:36 +0000 | [diff] [blame] | 1195 | MI->setArgumentList(MacroArgs.data(), MacroArgs.size(), |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1196 | PP->getPreprocessorAllocator()); |
Douglas Gregor | c3366a5 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | // Finally, install the macro. |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1200 | PP->setMacroInfo(II, MI); |
Douglas Gregor | c3366a5 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1201 | |
| 1202 | // Remember that we saw this macro last so that we add the tokens that |
| 1203 | // form its body to it. |
| 1204 | Macro = MI; |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 1205 | |
| 1206 | if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) { |
| 1207 | // We have a macro definition. Load it now. |
| 1208 | PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro, |
| 1209 | getMacroDefinition(Record[NextIndex])); |
| 1210 | } |
| 1211 | |
Douglas Gregor | c3366a5 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1212 | ++NumMacrosRead; |
| 1213 | break; |
| 1214 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1215 | |
Douglas Gregor | c3366a5 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1216 | case pch::PP_TOKEN: { |
| 1217 | // If we see a TOKEN before a PP_MACRO_*, then the file is |
| 1218 | // erroneous, just pretend we didn't see this. |
| 1219 | if (Macro == 0) break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1220 | |
Douglas Gregor | c3366a5 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1221 | Token Tok; |
| 1222 | Tok.startToken(); |
| 1223 | Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0])); |
| 1224 | Tok.setLength(Record[1]); |
| 1225 | if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2])) |
| 1226 | Tok.setIdentifierInfo(II); |
| 1227 | Tok.setKind((tok::TokenKind)Record[3]); |
| 1228 | Tok.setFlag((Token::TokenFlags)Record[4]); |
| 1229 | Macro->AddTokenToBody(Tok); |
| 1230 | break; |
| 1231 | } |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 1232 | |
| 1233 | case pch::PP_MACRO_INSTANTIATION: { |
| 1234 | // If we already have a macro, that means that we've hit the end |
| 1235 | // of the definition of the macro we were looking for. We're |
| 1236 | // done. |
| 1237 | if (Macro) |
| 1238 | return; |
| 1239 | |
| 1240 | if (!PP->getPreprocessingRecord()) { |
| 1241 | Error("missing preprocessing record in PCH file"); |
| 1242 | return; |
| 1243 | } |
| 1244 | |
| 1245 | PreprocessingRecord &PPRec = *PP->getPreprocessingRecord(); |
| 1246 | if (PPRec.getPreprocessedEntity(Record[0])) |
| 1247 | return; |
| 1248 | |
| 1249 | MacroInstantiation *MI |
| 1250 | = new (PPRec) MacroInstantiation(DecodeIdentifierInfo(Record[3]), |
| 1251 | SourceRange( |
| 1252 | SourceLocation::getFromRawEncoding(Record[1]), |
| 1253 | SourceLocation::getFromRawEncoding(Record[2])), |
| 1254 | getMacroDefinition(Record[4])); |
| 1255 | PPRec.SetPreallocatedEntity(Record[0], MI); |
| 1256 | return; |
| 1257 | } |
| 1258 | |
| 1259 | case pch::PP_MACRO_DEFINITION: { |
| 1260 | // If we already have a macro, that means that we've hit the end |
| 1261 | // of the definition of the macro we were looking for. We're |
| 1262 | // done. |
| 1263 | if (Macro) |
| 1264 | return; |
| 1265 | |
| 1266 | if (!PP->getPreprocessingRecord()) { |
| 1267 | Error("missing preprocessing record in PCH file"); |
| 1268 | return; |
| 1269 | } |
| 1270 | |
| 1271 | PreprocessingRecord &PPRec = *PP->getPreprocessingRecord(); |
| 1272 | if (PPRec.getPreprocessedEntity(Record[0])) |
| 1273 | return; |
| 1274 | |
| 1275 | if (Record[1] >= MacroDefinitionsLoaded.size()) { |
| 1276 | Error("out-of-bounds macro definition record"); |
| 1277 | return; |
| 1278 | } |
| 1279 | |
| 1280 | MacroDefinition *MD |
| 1281 | = new (PPRec) MacroDefinition(DecodeIdentifierInfo(Record[4]), |
| 1282 | SourceLocation::getFromRawEncoding(Record[5]), |
| 1283 | SourceRange( |
| 1284 | SourceLocation::getFromRawEncoding(Record[2]), |
| 1285 | SourceLocation::getFromRawEncoding(Record[3]))); |
| 1286 | PPRec.SetPreallocatedEntity(Record[0], MD); |
| 1287 | MacroDefinitionsLoaded[Record[1]] = MD; |
| 1288 | return; |
| 1289 | } |
Steve Naroff | 3fa455a | 2009-04-24 20:03:17 +0000 | [diff] [blame] | 1290 | } |
Douglas Gregor | c3366a5 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1291 | } |
| 1292 | } |
| 1293 | |
Douglas Gregor | 9882a5a | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 1294 | void PCHReader::ReadDefinedMacros() { |
Sebastian Redl | 4e6c567 | 2010-07-21 22:31:37 +0000 | [diff] [blame] | 1295 | for (unsigned I = 0, N = Chain.size(); I != N; ++I) { |
| 1296 | llvm::BitstreamCursor &MacroCursor = Chain[N - I - 1]->MacroCursor; |
Sebastian Redl | 3452281 | 2010-07-16 17:50:48 +0000 | [diff] [blame] | 1297 | |
Sebastian Redl | 4e6c567 | 2010-07-21 22:31:37 +0000 | [diff] [blame] | 1298 | // If there was no preprocessor block, skip this file. |
| 1299 | if (!MacroCursor.getBitStreamReader()) |
| 1300 | continue; |
Kovarththanan Rajaratnam | 39f2fbd1 | 2010-03-07 19:10:13 +0000 | [diff] [blame] | 1301 | |
Sebastian Redl | 4e6c567 | 2010-07-21 22:31:37 +0000 | [diff] [blame] | 1302 | llvm::BitstreamCursor Cursor = MacroCursor; |
| 1303 | if (Cursor.EnterSubBlock(pch::PREPROCESSOR_BLOCK_ID)) { |
| 1304 | Error("malformed preprocessor block record in PCH file"); |
Douglas Gregor | 9882a5a | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 1305 | return; |
| 1306 | } |
Kovarththanan Rajaratnam | 39f2fbd1 | 2010-03-07 19:10:13 +0000 | [diff] [blame] | 1307 | |
Sebastian Redl | 4e6c567 | 2010-07-21 22:31:37 +0000 | [diff] [blame] | 1308 | RecordData Record; |
| 1309 | while (true) { |
| 1310 | unsigned Code = Cursor.ReadCode(); |
| 1311 | if (Code == llvm::bitc::END_BLOCK) { |
| 1312 | if (Cursor.ReadBlockEnd()) { |
| 1313 | Error("error at end of preprocessor block in PCH file"); |
| 1314 | return; |
| 1315 | } |
| 1316 | break; |
Douglas Gregor | 9882a5a | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 1317 | } |
Kovarththanan Rajaratnam | 39f2fbd1 | 2010-03-07 19:10:13 +0000 | [diff] [blame] | 1318 | |
Sebastian Redl | 4e6c567 | 2010-07-21 22:31:37 +0000 | [diff] [blame] | 1319 | if (Code == llvm::bitc::ENTER_SUBBLOCK) { |
| 1320 | // No known subblocks, always skip them. |
| 1321 | Cursor.ReadSubBlockID(); |
| 1322 | if (Cursor.SkipBlock()) { |
| 1323 | Error("malformed block record in PCH file"); |
| 1324 | return; |
| 1325 | } |
| 1326 | continue; |
| 1327 | } |
Kovarththanan Rajaratnam | 39f2fbd1 | 2010-03-07 19:10:13 +0000 | [diff] [blame] | 1328 | |
Sebastian Redl | 4e6c567 | 2010-07-21 22:31:37 +0000 | [diff] [blame] | 1329 | if (Code == llvm::bitc::DEFINE_ABBREV) { |
| 1330 | Cursor.ReadAbbrevRecord(); |
| 1331 | continue; |
| 1332 | } |
Kovarththanan Rajaratnam | 39f2fbd1 | 2010-03-07 19:10:13 +0000 | [diff] [blame] | 1333 | |
Sebastian Redl | 4e6c567 | 2010-07-21 22:31:37 +0000 | [diff] [blame] | 1334 | // Read a record. |
| 1335 | const char *BlobStart; |
| 1336 | unsigned BlobLen; |
| 1337 | Record.clear(); |
| 1338 | switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) { |
| 1339 | default: // Default behavior: ignore. |
| 1340 | break; |
Douglas Gregor | 9882a5a | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 1341 | |
Sebastian Redl | 4e6c567 | 2010-07-21 22:31:37 +0000 | [diff] [blame] | 1342 | case pch::PP_MACRO_OBJECT_LIKE: |
| 1343 | case pch::PP_MACRO_FUNCTION_LIKE: |
| 1344 | DecodeIdentifierInfo(Record[0]); |
| 1345 | break; |
| 1346 | |
| 1347 | case pch::PP_TOKEN: |
| 1348 | // Ignore tokens. |
| 1349 | break; |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 1350 | |
Sebastian Redl | 4e6c567 | 2010-07-21 22:31:37 +0000 | [diff] [blame] | 1351 | case pch::PP_MACRO_INSTANTIATION: |
| 1352 | case pch::PP_MACRO_DEFINITION: |
| 1353 | // Read the macro record. |
| 1354 | ReadMacroRecord(Chain[N - I - 1]->Stream, Cursor.GetCurrentBitNo()); |
| 1355 | break; |
| 1356 | } |
Douglas Gregor | 9882a5a | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 1357 | } |
| 1358 | } |
| 1359 | } |
| 1360 | |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 1361 | MacroDefinition *PCHReader::getMacroDefinition(pch::IdentID ID) { |
| 1362 | if (ID == 0 || ID >= MacroDefinitionsLoaded.size()) |
| 1363 | return 0; |
Sebastian Redl | 4e6c567 | 2010-07-21 22:31:37 +0000 | [diff] [blame] | 1364 | |
| 1365 | if (!MacroDefinitionsLoaded[ID]) { |
| 1366 | unsigned Index = ID; |
| 1367 | for (unsigned I = 0, N = Chain.size(); I != N; ++I) { |
| 1368 | PerFileData &F = *Chain[N - I - 1]; |
| 1369 | if (Index < F.LocalNumMacroDefinitions) { |
| 1370 | ReadMacroRecord(F.Stream, F.MacroDefinitionOffsets[Index]); |
| 1371 | break; |
| 1372 | } |
| 1373 | Index -= F.LocalNumMacroDefinitions; |
| 1374 | } |
| 1375 | assert(MacroDefinitionsLoaded[ID] && "Broken chain"); |
| 1376 | } |
| 1377 | |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 1378 | return MacroDefinitionsLoaded[ID]; |
| 1379 | } |
| 1380 | |
Douglas Gregor | 0086a5a | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 1381 | /// \brief If we are loading a relocatable PCH file, and the filename is |
| 1382 | /// not an absolute path, add the system root to the beginning of the file |
| 1383 | /// name. |
| 1384 | void PCHReader::MaybeAddSystemRootToFilename(std::string &Filename) { |
| 1385 | // If this is not a relocatable PCH file, there's nothing to do. |
| 1386 | if (!RelocatablePCH) |
| 1387 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1388 | |
Daniel Dunbar | f2ce9a2 | 2009-11-18 19:50:41 +0000 | [diff] [blame] | 1389 | if (Filename.empty() || llvm::sys::Path(Filename).isAbsolute()) |
Douglas Gregor | 0086a5a | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 1390 | return; |
| 1391 | |
Douglas Gregor | 0086a5a | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 1392 | if (isysroot == 0) { |
| 1393 | // If no system root was given, default to '/' |
| 1394 | Filename.insert(Filename.begin(), '/'); |
| 1395 | return; |
| 1396 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1397 | |
Douglas Gregor | 0086a5a | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 1398 | unsigned Length = strlen(isysroot); |
| 1399 | if (isysroot[Length - 1] != '/') |
| 1400 | Filename.insert(Filename.begin(), '/'); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1401 | |
Douglas Gregor | 0086a5a | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 1402 | Filename.insert(Filename.begin(), isysroot, isysroot + Length); |
| 1403 | } |
| 1404 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1405 | PCHReader::PCHReadResult |
Sebastian Redl | 2abc038 | 2010-07-16 20:41:52 +0000 | [diff] [blame] | 1406 | PCHReader::ReadPCHBlock(PerFileData &F) { |
Sebastian Redl | 3452281 | 2010-07-16 17:50:48 +0000 | [diff] [blame] | 1407 | llvm::BitstreamCursor &Stream = F.Stream; |
| 1408 | |
Douglas Gregor | 55abb23 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1409 | if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) { |
Douglas Gregor | 6f00bf8 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1410 | Error("malformed block record in PCH file"); |
Douglas Gregor | 55abb23 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1411 | return Failure; |
| 1412 | } |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1413 | |
| 1414 | // Read all of the records and blocks for the PCH file. |
Douglas Gregor | 1e9bf3b | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 1415 | RecordData Record; |
Sebastian Redl | 393f8b7 | 2010-07-19 20:52:06 +0000 | [diff] [blame] | 1416 | bool First = true; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1417 | while (!Stream.AtEndOfStream()) { |
| 1418 | unsigned Code = Stream.ReadCode(); |
| 1419 | if (Code == llvm::bitc::END_BLOCK) { |
Douglas Gregor | 55abb23 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1420 | if (Stream.ReadBlockEnd()) { |
Douglas Gregor | 6f00bf8 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1421 | Error("error at end of module block in PCH file"); |
Douglas Gregor | 55abb23 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1422 | return Failure; |
| 1423 | } |
Chris Lattner | c523d8e | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 1424 | |
Douglas Gregor | 55abb23 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1425 | return Success; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1426 | } |
| 1427 | |
| 1428 | if (Code == llvm::bitc::ENTER_SUBBLOCK) { |
| 1429 | switch (Stream.ReadSubBlockID()) { |
Douglas Gregor | 12bfa38 | 2009-10-17 00:13:19 +0000 | [diff] [blame] | 1430 | case pch::DECLTYPES_BLOCK_ID: |
Chris Lattner | e78a6be | 2009-04-27 01:05:14 +0000 | [diff] [blame] | 1431 | // We lazily load the decls block, but we want to set up the |
| 1432 | // DeclsCursor cursor to point into it. Clone our current bitcode |
| 1433 | // cursor to it, enter the block and read the abbrevs in that block. |
| 1434 | // With the main cursor, we just skip over it. |
Sebastian Redl | 3452281 | 2010-07-16 17:50:48 +0000 | [diff] [blame] | 1435 | F.DeclsCursor = Stream; |
Chris Lattner | e78a6be | 2009-04-27 01:05:14 +0000 | [diff] [blame] | 1436 | if (Stream.SkipBlock() || // Skip with the main cursor. |
| 1437 | // Read the abbrevs. |
Sebastian Redl | 3452281 | 2010-07-16 17:50:48 +0000 | [diff] [blame] | 1438 | ReadBlockAbbrevs(F.DeclsCursor, pch::DECLTYPES_BLOCK_ID)) { |
Douglas Gregor | 6f00bf8 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1439 | Error("malformed block record in PCH file"); |
Chris Lattner | e78a6be | 2009-04-27 01:05:14 +0000 | [diff] [blame] | 1440 | return Failure; |
| 1441 | } |
| 1442 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1443 | |
Chris Lattner | c523d8e | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 1444 | case pch::PREPROCESSOR_BLOCK_ID: |
Sebastian Redl | 3452281 | 2010-07-16 17:50:48 +0000 | [diff] [blame] | 1445 | F.MacroCursor = Stream; |
Douglas Gregor | 9882a5a | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 1446 | if (PP) |
| 1447 | PP->setExternalSource(this); |
| 1448 | |
Chris Lattner | c523d8e | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 1449 | if (Stream.SkipBlock()) { |
Douglas Gregor | 6f00bf8 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1450 | Error("malformed block record in PCH file"); |
Chris Lattner | c523d8e | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 1451 | return Failure; |
| 1452 | } |
| 1453 | break; |
Steve Naroff | 2ddea05 | 2009-04-23 10:39:46 +0000 | [diff] [blame] | 1454 | |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1455 | case pch::SOURCE_MANAGER_BLOCK_ID: |
Sebastian Redl | 393f8b7 | 2010-07-19 20:52:06 +0000 | [diff] [blame] | 1456 | switch (ReadSourceManagerBlock(F)) { |
Douglas Gregor | 92863e4 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1457 | case Success: |
| 1458 | break; |
| 1459 | |
| 1460 | case Failure: |
Douglas Gregor | 6f00bf8 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1461 | Error("malformed source manager block in PCH file"); |
Douglas Gregor | 55abb23 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1462 | return Failure; |
Douglas Gregor | 92863e4 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1463 | |
| 1464 | case IgnorePCH: |
| 1465 | return IgnorePCH; |
Douglas Gregor | 55abb23 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1466 | } |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1467 | break; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1468 | } |
Sebastian Redl | 393f8b7 | 2010-07-19 20:52:06 +0000 | [diff] [blame] | 1469 | First = false; |
Douglas Gregor | 1e9bf3b | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 1470 | continue; |
| 1471 | } |
| 1472 | |
| 1473 | if (Code == llvm::bitc::DEFINE_ABBREV) { |
| 1474 | Stream.ReadAbbrevRecord(); |
| 1475 | continue; |
| 1476 | } |
| 1477 | |
| 1478 | // Read and process a record. |
| 1479 | Record.clear(); |
Douglas Gregor | bfbde53 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 1480 | const char *BlobStart = 0; |
| 1481 | unsigned BlobLen = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1482 | switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record, |
Douglas Gregor | bfbde53 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 1483 | &BlobStart, &BlobLen)) { |
Douglas Gregor | 1e9bf3b | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 1484 | default: // Default behavior: ignore. |
| 1485 | break; |
| 1486 | |
Sebastian Redl | c2e6dbf | 2010-07-17 00:12:06 +0000 | [diff] [blame] | 1487 | case pch::METADATA: { |
Douglas Gregor | ce3a829 | 2010-07-27 00:27:13 +0000 | [diff] [blame] | 1488 | if (Record[0] != pch::VERSION_MAJOR && !DisableValidation) { |
Sebastian Redl | c2e6dbf | 2010-07-17 00:12:06 +0000 | [diff] [blame] | 1489 | Diag(Record[0] < pch::VERSION_MAJOR? diag::warn_pch_version_too_old |
| 1490 | : diag::warn_pch_version_too_new); |
| 1491 | return IgnorePCH; |
| 1492 | } |
| 1493 | |
| 1494 | RelocatablePCH = Record[4]; |
| 1495 | if (Listener) { |
| 1496 | std::string TargetTriple(BlobStart, BlobLen); |
| 1497 | if (Listener->ReadTargetTriple(TargetTriple)) |
| 1498 | return IgnorePCH; |
| 1499 | } |
| 1500 | break; |
| 1501 | } |
| 1502 | |
| 1503 | case pch::CHAINED_METADATA: { |
Sebastian Redl | 393f8b7 | 2010-07-19 20:52:06 +0000 | [diff] [blame] | 1504 | if (!First) { |
| 1505 | Error("CHAINED_METADATA is not first record in block"); |
| 1506 | return Failure; |
| 1507 | } |
Douglas Gregor | ce3a829 | 2010-07-27 00:27:13 +0000 | [diff] [blame] | 1508 | if (Record[0] != pch::VERSION_MAJOR && !DisableValidation) { |
Sebastian Redl | c2e6dbf | 2010-07-17 00:12:06 +0000 | [diff] [blame] | 1509 | Diag(Record[0] < pch::VERSION_MAJOR? diag::warn_pch_version_too_old |
| 1510 | : diag::warn_pch_version_too_new); |
| 1511 | return IgnorePCH; |
| 1512 | } |
| 1513 | |
| 1514 | // Load the chained file. |
| 1515 | switch(ReadPCHCore(llvm::StringRef(BlobStart, BlobLen))) { |
| 1516 | case Failure: return Failure; |
| 1517 | // If we have to ignore the dependency, we'll have to ignore this too. |
| 1518 | case IgnorePCH: return IgnorePCH; |
| 1519 | case Success: break; |
| 1520 | } |
| 1521 | break; |
| 1522 | } |
| 1523 | |
Douglas Gregor | 1e9bf3b | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 1524 | case pch::TYPE_OFFSET: |
Sebastian Redl | 9e68799 | 2010-07-19 22:06:55 +0000 | [diff] [blame] | 1525 | if (F.LocalNumTypes != 0) { |
Douglas Gregor | 6f00bf8 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1526 | Error("duplicate TYPE_OFFSET record in PCH file"); |
Douglas Gregor | 55abb23 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1527 | return Failure; |
| 1528 | } |
Sebastian Redl | 9e68799 | 2010-07-19 22:06:55 +0000 | [diff] [blame] | 1529 | F.TypeOffsets = (const uint32_t *)BlobStart; |
| 1530 | F.LocalNumTypes = Record[0]; |
Douglas Gregor | 1e9bf3b | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 1531 | break; |
| 1532 | |
| 1533 | case pch::DECL_OFFSET: |
Sebastian Redl | 9e68799 | 2010-07-19 22:06:55 +0000 | [diff] [blame] | 1534 | if (F.LocalNumDecls != 0) { |
Douglas Gregor | 6f00bf8 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1535 | Error("duplicate DECL_OFFSET record in PCH file"); |
Douglas Gregor | 55abb23 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1536 | return Failure; |
| 1537 | } |
Sebastian Redl | 9e68799 | 2010-07-19 22:06:55 +0000 | [diff] [blame] | 1538 | F.DeclOffsets = (const uint32_t *)BlobStart; |
| 1539 | F.LocalNumDecls = Record[0]; |
Douglas Gregor | 1e9bf3b | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 1540 | break; |
Douglas Gregor | 55abb23 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1541 | |
| 1542 | case pch::LANGUAGE_OPTIONS: |
Douglas Gregor | ce3a829 | 2010-07-27 00:27:13 +0000 | [diff] [blame] | 1543 | if (ParseLanguageOptions(Record) && !DisableValidation) |
Douglas Gregor | 55abb23 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1544 | return IgnorePCH; |
| 1545 | break; |
Douglas Gregor | bfbde53 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 1546 | |
Douglas Gregor | 3ed42cb | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 1547 | case pch::IDENTIFIER_TABLE: |
Sebastian Redl | 393f8b7 | 2010-07-19 20:52:06 +0000 | [diff] [blame] | 1548 | F.IdentifierTableData = BlobStart; |
Douglas Gregor | 0e14997 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 1549 | if (Record[0]) { |
Sebastian Redl | 393f8b7 | 2010-07-19 20:52:06 +0000 | [diff] [blame] | 1550 | F.IdentifierLookupTable |
Douglas Gregor | 0e14997 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 1551 | = PCHIdentifierLookupTable::Create( |
Sebastian Redl | 393f8b7 | 2010-07-19 20:52:06 +0000 | [diff] [blame] | 1552 | (const unsigned char *)F.IdentifierTableData + Record[0], |
| 1553 | (const unsigned char *)F.IdentifierTableData, |
Sebastian Redl | 4e6c567 | 2010-07-21 22:31:37 +0000 | [diff] [blame] | 1554 | PCHIdentifierLookupTrait(*this, F.Stream)); |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1555 | if (PP) |
| 1556 | PP->getIdentifierTable().setExternalIdentifierLookup(this); |
Douglas Gregor | 0e14997 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 1557 | } |
Douglas Gregor | 3ed42cb | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 1558 | break; |
| 1559 | |
| 1560 | case pch::IDENTIFIER_OFFSET: |
Sebastian Redl | bd1b5be | 2010-07-19 22:28:42 +0000 | [diff] [blame] | 1561 | if (F.LocalNumIdentifiers != 0) { |
Douglas Gregor | 6f00bf8 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1562 | Error("duplicate IDENTIFIER_OFFSET record in PCH file"); |
Douglas Gregor | 3ed42cb | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 1563 | return Failure; |
| 1564 | } |
Sebastian Redl | bd1b5be | 2010-07-19 22:28:42 +0000 | [diff] [blame] | 1565 | F.IdentifierOffsets = (const uint32_t *)BlobStart; |
| 1566 | F.LocalNumIdentifiers = Record[0]; |
Douglas Gregor | 3ed42cb | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 1567 | break; |
Douglas Gregor | 1a0d0b9 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 1568 | |
| 1569 | case pch::EXTERNAL_DEFINITIONS: |
Sebastian Redl | b293a45 | 2010-07-20 21:20:32 +0000 | [diff] [blame] | 1570 | // Optimization for the first block. |
| 1571 | if (ExternalDefinitions.empty()) |
| 1572 | ExternalDefinitions.swap(Record); |
| 1573 | else |
| 1574 | ExternalDefinitions.insert(ExternalDefinitions.end(), |
| 1575 | Record.begin(), Record.end()); |
Douglas Gregor | 1a0d0b9 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 1576 | break; |
Douglas Gregor | 08f0129 | 2009-04-17 22:13:46 +0000 | [diff] [blame] | 1577 | |
Douglas Gregor | 652d82a | 2009-04-18 05:55:16 +0000 | [diff] [blame] | 1578 | case pch::SPECIAL_TYPES: |
Sebastian Redl | b293a45 | 2010-07-20 21:20:32 +0000 | [diff] [blame] | 1579 | // Optimization for the first block |
| 1580 | if (SpecialTypes.empty()) |
| 1581 | SpecialTypes.swap(Record); |
| 1582 | else |
| 1583 | SpecialTypes.insert(SpecialTypes.end(), Record.begin(), Record.end()); |
Douglas Gregor | 652d82a | 2009-04-18 05:55:16 +0000 | [diff] [blame] | 1584 | break; |
| 1585 | |
Douglas Gregor | 08f0129 | 2009-04-17 22:13:46 +0000 | [diff] [blame] | 1586 | case pch::STATISTICS: |
Sebastian Redl | b293a45 | 2010-07-20 21:20:32 +0000 | [diff] [blame] | 1587 | TotalNumStatements += Record[0]; |
| 1588 | TotalNumMacros += Record[1]; |
| 1589 | TotalLexicalDeclContexts += Record[2]; |
| 1590 | TotalVisibleDeclContexts += Record[3]; |
Douglas Gregor | 08f0129 | 2009-04-17 22:13:46 +0000 | [diff] [blame] | 1591 | break; |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1592 | |
Douglas Gregor | d4df865 | 2009-04-22 22:02:47 +0000 | [diff] [blame] | 1593 | case pch::TENTATIVE_DEFINITIONS: |
Sebastian Redl | b293a45 | 2010-07-20 21:20:32 +0000 | [diff] [blame] | 1594 | // Optimization for the first block. |
| 1595 | if (TentativeDefinitions.empty()) |
| 1596 | TentativeDefinitions.swap(Record); |
| 1597 | else |
| 1598 | TentativeDefinitions.insert(TentativeDefinitions.end(), |
| 1599 | Record.begin(), Record.end()); |
Douglas Gregor | d4df865 | 2009-04-22 22:02:47 +0000 | [diff] [blame] | 1600 | break; |
Douglas Gregor | acfc76c | 2009-04-22 22:18:58 +0000 | [diff] [blame] | 1601 | |
Tanya Lattner | 9007380 | 2010-02-12 00:07:30 +0000 | [diff] [blame] | 1602 | case pch::UNUSED_STATIC_FUNCS: |
Sebastian Redl | b293a45 | 2010-07-20 21:20:32 +0000 | [diff] [blame] | 1603 | // Optimization for the first block. |
| 1604 | if (UnusedStaticFuncs.empty()) |
| 1605 | UnusedStaticFuncs.swap(Record); |
| 1606 | else |
| 1607 | UnusedStaticFuncs.insert(UnusedStaticFuncs.end(), |
| 1608 | Record.begin(), Record.end()); |
Tanya Lattner | 9007380 | 2010-02-12 00:07:30 +0000 | [diff] [blame] | 1609 | break; |
Kovarththanan Rajaratnam | 39f2fbd1 | 2010-03-07 19:10:13 +0000 | [diff] [blame] | 1610 | |
Douglas Gregor | acfc76c | 2009-04-22 22:18:58 +0000 | [diff] [blame] | 1611 | case pch::LOCALLY_SCOPED_EXTERNAL_DECLS: |
Sebastian Redl | b293a45 | 2010-07-20 21:20:32 +0000 | [diff] [blame] | 1612 | // Optimization for the first block. |
| 1613 | if (LocallyScopedExternalDecls.empty()) |
| 1614 | LocallyScopedExternalDecls.swap(Record); |
| 1615 | else |
| 1616 | LocallyScopedExternalDecls.insert(LocallyScopedExternalDecls.end(), |
| 1617 | Record.begin(), Record.end()); |
Douglas Gregor | acfc76c | 2009-04-22 22:18:58 +0000 | [diff] [blame] | 1618 | break; |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1619 | |
Douglas Gregor | 95c13f5 | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1620 | case pch::SELECTOR_OFFSETS: |
| 1621 | SelectorOffsets = (const uint32_t *)BlobStart; |
| 1622 | TotalNumSelectors = Record[0]; |
| 1623 | SelectorsLoaded.resize(TotalNumSelectors); |
| 1624 | break; |
| 1625 | |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1626 | case pch::METHOD_POOL: |
Douglas Gregor | 95c13f5 | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1627 | MethodPoolLookupTableData = (const unsigned char *)BlobStart; |
| 1628 | if (Record[0]) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1629 | MethodPoolLookupTable |
Douglas Gregor | 95c13f5 | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1630 | = PCHMethodPoolLookupTable::Create( |
| 1631 | MethodPoolLookupTableData + Record[0], |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1632 | MethodPoolLookupTableData, |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1633 | PCHMethodPoolLookupTrait(*this)); |
Douglas Gregor | 95c13f5 | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1634 | TotalSelectorsInMethodPool = Record[1]; |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1635 | break; |
Douglas Gregor | eda6a89 | 2009-04-26 00:07:37 +0000 | [diff] [blame] | 1636 | |
Fariborz Jahanian | c51609a | 2010-07-23 19:11:11 +0000 | [diff] [blame] | 1637 | case pch::REFERENCED_SELECTOR_POOL: { |
| 1638 | unsigned int numEl = Record[0]*2; |
| 1639 | for (unsigned int i = 1; i <= numEl; i++) |
| 1640 | F.ReferencedSelectorsData.push_back(Record[i]); |
Fariborz Jahanian | c51609a | 2010-07-23 19:11:11 +0000 | [diff] [blame] | 1641 | break; |
Sebastian Redl | 66c5eef | 2010-07-27 00:17:23 +0000 | [diff] [blame] | 1642 | } |
Fariborz Jahanian | c51609a | 2010-07-23 19:11:11 +0000 | [diff] [blame] | 1643 | |
Douglas Gregor | eda6a89 | 2009-04-26 00:07:37 +0000 | [diff] [blame] | 1644 | case pch::PP_COUNTER_VALUE: |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1645 | if (!Record.empty() && Listener) |
| 1646 | Listener->ReadCounter(Record[0]); |
Douglas Gregor | eda6a89 | 2009-04-26 00:07:37 +0000 | [diff] [blame] | 1647 | break; |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1648 | |
| 1649 | case pch::SOURCE_LOCATION_OFFSETS: |
Sebastian Redl | b293a45 | 2010-07-20 21:20:32 +0000 | [diff] [blame] | 1650 | F.SLocOffsets = (const uint32_t *)BlobStart; |
| 1651 | F.LocalNumSLocEntries = Record[0]; |
| 1652 | // We cannot delay this until all PCHs are loaded, because then source |
| 1653 | // location preloads would also have to be delayed. |
| 1654 | TotalNumSLocEntries += F.LocalNumSLocEntries; |
Douglas Gregor | d54f3a1 | 2009-10-05 21:07:28 +0000 | [diff] [blame] | 1655 | SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, Record[1]); |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1656 | break; |
| 1657 | |
| 1658 | case pch::SOURCE_LOCATION_PRELOADS: |
| 1659 | for (unsigned I = 0, N = Record.size(); I != N; ++I) { |
| 1660 | PCHReadResult Result = ReadSLocEntryRecord(Record[I]); |
| 1661 | if (Result != Success) |
| 1662 | return Result; |
| 1663 | } |
| 1664 | break; |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1665 | |
Douglas Gregor | d2eb58a | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 1666 | case pch::STAT_CACHE: { |
Kovarththanan Rajaratnam | 39f2fbd1 | 2010-03-07 19:10:13 +0000 | [diff] [blame] | 1667 | PCHStatCache *MyStatCache = |
Douglas Gregor | d2eb58a | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 1668 | new PCHStatCache((const unsigned char *)BlobStart + Record[0], |
| 1669 | (const unsigned char *)BlobStart, |
| 1670 | NumStatHits, NumStatMisses); |
| 1671 | FileMgr.addStatCache(MyStatCache); |
Sebastian Redl | 3452281 | 2010-07-16 17:50:48 +0000 | [diff] [blame] | 1672 | F.StatCache = MyStatCache; |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1673 | break; |
Douglas Gregor | d2eb58a | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 1674 | } |
Kovarththanan Rajaratnam | 39f2fbd1 | 2010-03-07 19:10:13 +0000 | [diff] [blame] | 1675 | |
Douglas Gregor | 61cac2b | 2009-04-27 20:06:05 +0000 | [diff] [blame] | 1676 | case pch::EXT_VECTOR_DECLS: |
| 1677 | if (!ExtVectorDecls.empty()) { |
Douglas Gregor | 6f00bf8 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1678 | Error("duplicate EXT_VECTOR_DECLS record in PCH file"); |
Douglas Gregor | 61cac2b | 2009-04-27 20:06:05 +0000 | [diff] [blame] | 1679 | return Failure; |
| 1680 | } |
| 1681 | ExtVectorDecls.swap(Record); |
| 1682 | break; |
| 1683 | |
Argyrios Kyrtzidis | af2eac2 | 2010-07-06 15:37:04 +0000 | [diff] [blame] | 1684 | case pch::VTABLE_USES: |
| 1685 | if (!VTableUses.empty()) { |
| 1686 | Error("duplicate VTABLE_USES record in PCH file"); |
| 1687 | return Failure; |
| 1688 | } |
| 1689 | VTableUses.swap(Record); |
| 1690 | break; |
| 1691 | |
| 1692 | case pch::DYNAMIC_CLASSES: |
| 1693 | if (!DynamicClasses.empty()) { |
| 1694 | Error("duplicate DYNAMIC_CLASSES record in PCH file"); |
| 1695 | return Failure; |
| 1696 | } |
| 1697 | DynamicClasses.swap(Record); |
| 1698 | break; |
| 1699 | |
Douglas Gregor | 45fe036 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 1700 | case pch::ORIGINAL_FILE_NAME: |
Sebastian Redl | b293a45 | 2010-07-20 21:20:32 +0000 | [diff] [blame] | 1701 | // The primary PCH will be the last to get here, so it will be the one |
| 1702 | // that's used. |
Daniel Dunbar | 000c4ff | 2009-11-11 05:29:04 +0000 | [diff] [blame] | 1703 | ActualOriginalFileName.assign(BlobStart, BlobLen); |
| 1704 | OriginalFileName = ActualOriginalFileName; |
Douglas Gregor | 0086a5a | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 1705 | MaybeAddSystemRootToFilename(OriginalFileName); |
Douglas Gregor | 45fe036 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 1706 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1707 | |
Ted Kremenek | 1743713 | 2010-01-22 20:59:36 +0000 | [diff] [blame] | 1708 | case pch::VERSION_CONTROL_BRANCH_REVISION: { |
Ted Kremenek | 8bd0929 | 2010-02-12 23:31:14 +0000 | [diff] [blame] | 1709 | const std::string &CurBranch = getClangFullRepositoryVersion(); |
Ted Kremenek | 2377a0e | 2010-01-22 20:55:35 +0000 | [diff] [blame] | 1710 | llvm::StringRef PCHBranch(BlobStart, BlobLen); |
Douglas Gregor | ce3a829 | 2010-07-27 00:27:13 +0000 | [diff] [blame] | 1711 | if (llvm::StringRef(CurBranch) != PCHBranch && !DisableValidation) { |
Douglas Gregor | d54f3a1 | 2009-10-05 21:07:28 +0000 | [diff] [blame] | 1712 | Diag(diag::warn_pch_different_branch) << PCHBranch << CurBranch; |
| 1713 | return IgnorePCH; |
| 1714 | } |
| 1715 | break; |
| 1716 | } |
Sebastian Redl | fa06144 | 2010-07-21 20:07:32 +0000 | [diff] [blame] | 1717 | |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 1718 | case pch::MACRO_DEFINITION_OFFSETS: |
Sebastian Redl | fa06144 | 2010-07-21 20:07:32 +0000 | [diff] [blame] | 1719 | F.MacroDefinitionOffsets = (const uint32_t *)BlobStart; |
| 1720 | F.NumPreallocatedPreprocessingEntities = Record[0]; |
| 1721 | F.LocalNumMacroDefinitions = Record[1]; |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 1722 | break; |
Douglas Gregor | 3ed42cb | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 1723 | } |
Sebastian Redl | 393f8b7 | 2010-07-19 20:52:06 +0000 | [diff] [blame] | 1724 | First = false; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1725 | } |
Douglas Gregor | 6f00bf8 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1726 | Error("premature end of bitstream in PCH file"); |
Douglas Gregor | 55abb23 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1727 | return Failure; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1728 | } |
| 1729 | |
Douglas Gregor | 92863e4 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1730 | PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) { |
Sebastian Redl | c2e6dbf | 2010-07-17 00:12:06 +0000 | [diff] [blame] | 1731 | switch(ReadPCHCore(FileName)) { |
Sebastian Redl | 2abc038 | 2010-07-16 20:41:52 +0000 | [diff] [blame] | 1732 | case Failure: return Failure; |
| 1733 | case IgnorePCH: return IgnorePCH; |
| 1734 | case Success: break; |
| 1735 | } |
Sebastian Redl | c2e6dbf | 2010-07-17 00:12:06 +0000 | [diff] [blame] | 1736 | |
| 1737 | // Here comes stuff that we only do once the entire chain is loaded. |
| 1738 | |
Sebastian Redl | b293a45 | 2010-07-20 21:20:32 +0000 | [diff] [blame] | 1739 | // Allocate space for loaded identifiers, decls and types. |
Sebastian Redl | fa06144 | 2010-07-21 20:07:32 +0000 | [diff] [blame] | 1740 | unsigned TotalNumIdentifiers = 0, TotalNumTypes = 0, TotalNumDecls = 0, |
| 1741 | TotalNumPreallocatedPreprocessingEntities = 0, TotalNumMacroDefs = 0; |
Sebastian Redl | 9e68799 | 2010-07-19 22:06:55 +0000 | [diff] [blame] | 1742 | for (unsigned I = 0, N = Chain.size(); I != N; ++I) { |
Sebastian Redl | bd1b5be | 2010-07-19 22:28:42 +0000 | [diff] [blame] | 1743 | TotalNumIdentifiers += Chain[I]->LocalNumIdentifiers; |
Sebastian Redl | 9e68799 | 2010-07-19 22:06:55 +0000 | [diff] [blame] | 1744 | TotalNumTypes += Chain[I]->LocalNumTypes; |
| 1745 | TotalNumDecls += Chain[I]->LocalNumDecls; |
Sebastian Redl | fa06144 | 2010-07-21 20:07:32 +0000 | [diff] [blame] | 1746 | TotalNumPreallocatedPreprocessingEntities += |
| 1747 | Chain[I]->NumPreallocatedPreprocessingEntities; |
| 1748 | TotalNumMacroDefs += Chain[I]->LocalNumMacroDefinitions; |
Sebastian Redl | 9e68799 | 2010-07-19 22:06:55 +0000 | [diff] [blame] | 1749 | } |
Sebastian Redl | bd1b5be | 2010-07-19 22:28:42 +0000 | [diff] [blame] | 1750 | IdentifiersLoaded.resize(TotalNumIdentifiers); |
Sebastian Redl | 9e68799 | 2010-07-19 22:06:55 +0000 | [diff] [blame] | 1751 | TypesLoaded.resize(TotalNumTypes); |
| 1752 | DeclsLoaded.resize(TotalNumDecls); |
Sebastian Redl | fa06144 | 2010-07-21 20:07:32 +0000 | [diff] [blame] | 1753 | MacroDefinitionsLoaded.resize(TotalNumMacroDefs); |
| 1754 | if (PP) { |
| 1755 | if (TotalNumIdentifiers > 0) |
| 1756 | PP->getHeaderSearchInfo().SetExternalLookup(this); |
| 1757 | if (TotalNumPreallocatedPreprocessingEntities > 0) { |
| 1758 | if (!PP->getPreprocessingRecord()) |
| 1759 | PP->createPreprocessingRecord(); |
| 1760 | PP->getPreprocessingRecord()->SetExternalSource(*this, |
| 1761 | TotalNumPreallocatedPreprocessingEntities); |
| 1762 | } |
| 1763 | } |
Sebastian Redl | 9e68799 | 2010-07-19 22:06:55 +0000 | [diff] [blame] | 1764 | |
Sebastian Redl | c2e6dbf | 2010-07-17 00:12:06 +0000 | [diff] [blame] | 1765 | // Check the predefines buffers. |
Douglas Gregor | ce3a829 | 2010-07-27 00:27:13 +0000 | [diff] [blame] | 1766 | if (!DisableValidation && CheckPredefinesBuffers()) |
Sebastian Redl | c2e6dbf | 2010-07-17 00:12:06 +0000 | [diff] [blame] | 1767 | return IgnorePCH; |
| 1768 | |
| 1769 | if (PP) { |
| 1770 | // Initialization of keywords and pragmas occurs before the |
| 1771 | // PCH file is read, so there may be some identifiers that were |
| 1772 | // loaded into the IdentifierTable before we intercepted the |
| 1773 | // creation of identifiers. Iterate through the list of known |
| 1774 | // identifiers and determine whether we have to establish |
| 1775 | // preprocessor definitions or top-level identifier declaration |
| 1776 | // chains for those identifiers. |
| 1777 | // |
| 1778 | // We copy the IdentifierInfo pointers to a small vector first, |
| 1779 | // since de-serializing declarations or macro definitions can add |
| 1780 | // new entries into the identifier table, invalidating the |
| 1781 | // iterators. |
| 1782 | llvm::SmallVector<IdentifierInfo *, 128> Identifiers; |
| 1783 | for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(), |
| 1784 | IdEnd = PP->getIdentifierTable().end(); |
| 1785 | Id != IdEnd; ++Id) |
| 1786 | Identifiers.push_back(Id->second); |
Sebastian Redl | fa06144 | 2010-07-21 20:07:32 +0000 | [diff] [blame] | 1787 | // We need to search the tables in all files. |
| 1788 | // FIXME: What happens if this stuff changes between files, e.g. the |
| 1789 | // dependent PCH undefs a macro from the core file? |
| 1790 | for (unsigned J = 0, M = Chain.size(); J != M; ++J) { |
| 1791 | PCHIdentifierLookupTable *IdTable |
| 1792 | = (PCHIdentifierLookupTable *)Chain[J]->IdentifierLookupTable; |
Sebastian Redl | 5c415f3 | 2010-07-22 17:01:13 +0000 | [diff] [blame] | 1793 | // Not all PCH files necessarily have identifier tables, only the useful |
| 1794 | // ones. |
| 1795 | if (!IdTable) |
| 1796 | continue; |
Sebastian Redl | fa06144 | 2010-07-21 20:07:32 +0000 | [diff] [blame] | 1797 | for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) { |
| 1798 | IdentifierInfo *II = Identifiers[I]; |
| 1799 | // Look in the on-disk hash tables for an entry for this identifier |
Sebastian Redl | 4e6c567 | 2010-07-21 22:31:37 +0000 | [diff] [blame] | 1800 | PCHIdentifierLookupTrait Info(*this, Chain[J]->Stream, II); |
Sebastian Redl | fa06144 | 2010-07-21 20:07:32 +0000 | [diff] [blame] | 1801 | std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength()); |
Sebastian Redl | b293a45 | 2010-07-20 21:20:32 +0000 | [diff] [blame] | 1802 | PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info); |
| 1803 | if (Pos == IdTable->end()) |
| 1804 | continue; |
Sebastian Redl | c2e6dbf | 2010-07-17 00:12:06 +0000 | [diff] [blame] | 1805 | |
Sebastian Redl | b293a45 | 2010-07-20 21:20:32 +0000 | [diff] [blame] | 1806 | // Dereferencing the iterator has the effect of populating the |
| 1807 | // IdentifierInfo node with the various declarations it needs. |
| 1808 | (void)*Pos; |
| 1809 | } |
Sebastian Redl | c2e6dbf | 2010-07-17 00:12:06 +0000 | [diff] [blame] | 1810 | } |
| 1811 | } |
| 1812 | |
| 1813 | if (Context) |
| 1814 | InitializeContext(*Context); |
| 1815 | |
| 1816 | return Success; |
| 1817 | } |
| 1818 | |
| 1819 | PCHReader::PCHReadResult PCHReader::ReadPCHCore(llvm::StringRef FileName) { |
| 1820 | Chain.push_back(new PerFileData()); |
Sebastian Redl | 3452281 | 2010-07-16 17:50:48 +0000 | [diff] [blame] | 1821 | PerFileData &F = *Chain.back(); |
Sebastian Redl | c2e6dbf | 2010-07-17 00:12:06 +0000 | [diff] [blame] | 1822 | |
| 1823 | // Set the PCH file name. |
| 1824 | F.FileName = FileName; |
| 1825 | |
| 1826 | // Open the PCH file. |
| 1827 | // |
| 1828 | // FIXME: This shouldn't be here, we should just take a raw_ostream. |
| 1829 | std::string ErrStr; |
| 1830 | F.Buffer.reset(llvm::MemoryBuffer::getFileOrSTDIN(FileName, &ErrStr)); |
| 1831 | if (!F.Buffer) { |
| 1832 | Error(ErrStr.c_str()); |
| 1833 | return IgnorePCH; |
| 1834 | } |
| 1835 | |
| 1836 | // Initialize the stream |
| 1837 | F.StreamFile.init((const unsigned char *)F.Buffer->getBufferStart(), |
| 1838 | (const unsigned char *)F.Buffer->getBufferEnd()); |
Sebastian Redl | 3452281 | 2010-07-16 17:50:48 +0000 | [diff] [blame] | 1839 | llvm::BitstreamCursor &Stream = F.Stream; |
Sebastian Redl | c2e6dbf | 2010-07-17 00:12:06 +0000 | [diff] [blame] | 1840 | Stream.init(F.StreamFile); |
Sebastian Redl | fa06144 | 2010-07-21 20:07:32 +0000 | [diff] [blame] | 1841 | F.SizeInBits = F.Buffer->getBufferSize() * 8; |
Sebastian Redl | c2e6dbf | 2010-07-17 00:12:06 +0000 | [diff] [blame] | 1842 | |
| 1843 | // Sniff for the signature. |
| 1844 | if (Stream.Read(8) != 'C' || |
| 1845 | Stream.Read(8) != 'P' || |
| 1846 | Stream.Read(8) != 'C' || |
| 1847 | Stream.Read(8) != 'H') { |
| 1848 | Diag(diag::err_not_a_pch_file) << FileName; |
| 1849 | return Failure; |
| 1850 | } |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1851 | |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1852 | while (!Stream.AtEndOfStream()) { |
| 1853 | unsigned Code = Stream.ReadCode(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1854 | |
Douglas Gregor | 92863e4 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1855 | if (Code != llvm::bitc::ENTER_SUBBLOCK) { |
Douglas Gregor | 6f00bf8 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1856 | Error("invalid record at top-level of PCH file"); |
Douglas Gregor | 92863e4 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1857 | return Failure; |
| 1858 | } |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1859 | |
| 1860 | unsigned BlockID = Stream.ReadSubBlockID(); |
Douglas Gregor | a868bbd | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 1861 | |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1862 | // We only know the PCH subblock ID. |
| 1863 | switch (BlockID) { |
| 1864 | case llvm::bitc::BLOCKINFO_BLOCK_ID: |
Douglas Gregor | 92863e4 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1865 | if (Stream.ReadBlockInfoBlock()) { |
Douglas Gregor | 6f00bf8 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1866 | Error("malformed BlockInfoBlock in PCH file"); |
Douglas Gregor | 92863e4 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1867 | return Failure; |
| 1868 | } |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1869 | break; |
| 1870 | case pch::PCH_BLOCK_ID: |
Sebastian Redl | 2abc038 | 2010-07-16 20:41:52 +0000 | [diff] [blame] | 1871 | switch (ReadPCHBlock(F)) { |
Douglas Gregor | 55abb23 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1872 | case Success: |
| 1873 | break; |
| 1874 | |
| 1875 | case Failure: |
Douglas Gregor | 92863e4 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1876 | return Failure; |
Douglas Gregor | 55abb23 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1877 | |
| 1878 | case IgnorePCH: |
Douglas Gregor | bfbde53 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 1879 | // FIXME: We could consider reading through to the end of this |
| 1880 | // PCH block, skipping subblocks, to see if there are other |
| 1881 | // PCH blocks elsewhere. |
Douglas Gregor | 0bc1293 | 2009-04-27 21:28:04 +0000 | [diff] [blame] | 1882 | |
| 1883 | // Clear out any preallocated source location entries, so that |
| 1884 | // the source manager does not try to resolve them later. |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1885 | SourceMgr.ClearPreallocatedSLocEntries(); |
Douglas Gregor | 0bc1293 | 2009-04-27 21:28:04 +0000 | [diff] [blame] | 1886 | |
| 1887 | // Remove the stat cache. |
Sebastian Redl | 3452281 | 2010-07-16 17:50:48 +0000 | [diff] [blame] | 1888 | if (F.StatCache) |
| 1889 | FileMgr.removeStatCache((PCHStatCache*)F.StatCache); |
Douglas Gregor | 0bc1293 | 2009-04-27 21:28:04 +0000 | [diff] [blame] | 1890 | |
Douglas Gregor | 92863e4 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1891 | return IgnorePCH; |
Douglas Gregor | 55abb23 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1892 | } |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1893 | break; |
| 1894 | default: |
Douglas Gregor | 92863e4 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1895 | if (Stream.SkipBlock()) { |
Douglas Gregor | 6f00bf8 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 1896 | Error("malformed block record in PCH file"); |
Douglas Gregor | 92863e4 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 1897 | return Failure; |
| 1898 | } |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1899 | break; |
| 1900 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1901 | } |
| 1902 | |
Sebastian Redl | 2abc038 | 2010-07-16 20:41:52 +0000 | [diff] [blame] | 1903 | return Success; |
| 1904 | } |
| 1905 | |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 1906 | void PCHReader::setPreprocessor(Preprocessor &pp) { |
| 1907 | PP = &pp; |
Sebastian Redl | fa06144 | 2010-07-21 20:07:32 +0000 | [diff] [blame] | 1908 | |
| 1909 | unsigned TotalNum = 0; |
| 1910 | for (unsigned I = 0, N = Chain.size(); I != N; ++I) |
| 1911 | TotalNum += Chain[I]->NumPreallocatedPreprocessingEntities; |
| 1912 | if (TotalNum) { |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 1913 | if (!PP->getPreprocessingRecord()) |
| 1914 | PP->createPreprocessingRecord(); |
Sebastian Redl | fa06144 | 2010-07-21 20:07:32 +0000 | [diff] [blame] | 1915 | PP->getPreprocessingRecord()->SetExternalSource(*this, TotalNum); |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 1916 | } |
| 1917 | } |
| 1918 | |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1919 | void PCHReader::InitializeContext(ASTContext &Ctx) { |
| 1920 | Context = &Ctx; |
| 1921 | assert(Context && "Passed null context!"); |
| 1922 | |
| 1923 | assert(PP && "Forgot to set Preprocessor ?"); |
| 1924 | PP->getIdentifierTable().setExternalIdentifierLookup(this); |
| 1925 | PP->getHeaderSearchInfo().SetExternalLookup(this); |
Douglas Gregor | 9882a5a | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 1926 | PP->setExternalSource(this); |
Kovarththanan Rajaratnam | 39f2fbd1 | 2010-03-07 19:10:13 +0000 | [diff] [blame] | 1927 | |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1928 | // Load the translation unit declaration |
Argyrios Kyrtzidis | 7e8996c | 2010-07-08 17:13:02 +0000 | [diff] [blame] | 1929 | GetTranslationUnitDecl(); |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1930 | |
| 1931 | // Load the special types. |
| 1932 | Context->setBuiltinVaListType( |
| 1933 | GetType(SpecialTypes[pch::SPECIAL_TYPE_BUILTIN_VA_LIST])); |
| 1934 | if (unsigned Id = SpecialTypes[pch::SPECIAL_TYPE_OBJC_ID]) |
| 1935 | Context->setObjCIdType(GetType(Id)); |
| 1936 | if (unsigned Sel = SpecialTypes[pch::SPECIAL_TYPE_OBJC_SELECTOR]) |
| 1937 | Context->setObjCSelType(GetType(Sel)); |
| 1938 | if (unsigned Proto = SpecialTypes[pch::SPECIAL_TYPE_OBJC_PROTOCOL]) |
| 1939 | Context->setObjCProtoType(GetType(Proto)); |
| 1940 | if (unsigned Class = SpecialTypes[pch::SPECIAL_TYPE_OBJC_CLASS]) |
| 1941 | Context->setObjCClassType(GetType(Class)); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1942 | |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1943 | if (unsigned String = SpecialTypes[pch::SPECIAL_TYPE_CF_CONSTANT_STRING]) |
| 1944 | Context->setCFConstantStringType(GetType(String)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1945 | if (unsigned FastEnum |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 1946 | = SpecialTypes[pch::SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE]) |
| 1947 | Context->setObjCFastEnumerationStateType(GetType(FastEnum)); |
Douglas Gregor | 27821ce | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 1948 | if (unsigned File = SpecialTypes[pch::SPECIAL_TYPE_FILE]) { |
| 1949 | QualType FileType = GetType(File); |
Ted Kremenek | 1ff615c | 2010-03-18 00:56:54 +0000 | [diff] [blame] | 1950 | if (FileType.isNull()) { |
| 1951 | Error("FILE type is NULL"); |
| 1952 | return; |
| 1953 | } |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1954 | if (const TypedefType *Typedef = FileType->getAs<TypedefType>()) |
Douglas Gregor | 27821ce | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 1955 | Context->setFILEDecl(Typedef->getDecl()); |
| 1956 | else { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1957 | const TagType *Tag = FileType->getAs<TagType>(); |
Ted Kremenek | 1ff615c | 2010-03-18 00:56:54 +0000 | [diff] [blame] | 1958 | if (!Tag) { |
| 1959 | Error("Invalid FILE type in PCH file"); |
| 1960 | return; |
| 1961 | } |
Douglas Gregor | 27821ce | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 1962 | Context->setFILEDecl(Tag->getDecl()); |
| 1963 | } |
| 1964 | } |
Mike Stump | a4de80b | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 1965 | if (unsigned Jmp_buf = SpecialTypes[pch::SPECIAL_TYPE_jmp_buf]) { |
| 1966 | QualType Jmp_bufType = GetType(Jmp_buf); |
Ted Kremenek | 1ff615c | 2010-03-18 00:56:54 +0000 | [diff] [blame] | 1967 | if (Jmp_bufType.isNull()) { |
| 1968 | Error("jmp_bug type is NULL"); |
| 1969 | return; |
| 1970 | } |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1971 | if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>()) |
Mike Stump | a4de80b | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 1972 | Context->setjmp_bufDecl(Typedef->getDecl()); |
| 1973 | else { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1974 | const TagType *Tag = Jmp_bufType->getAs<TagType>(); |
Ted Kremenek | 1ff615c | 2010-03-18 00:56:54 +0000 | [diff] [blame] | 1975 | if (!Tag) { |
| 1976 | Error("Invalid jmp_bug type in PCH file"); |
| 1977 | return; |
| 1978 | } |
Mike Stump | a4de80b | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 1979 | Context->setjmp_bufDecl(Tag->getDecl()); |
| 1980 | } |
| 1981 | } |
| 1982 | if (unsigned Sigjmp_buf = SpecialTypes[pch::SPECIAL_TYPE_sigjmp_buf]) { |
| 1983 | QualType Sigjmp_bufType = GetType(Sigjmp_buf); |
Ted Kremenek | 1ff615c | 2010-03-18 00:56:54 +0000 | [diff] [blame] | 1984 | if (Sigjmp_bufType.isNull()) { |
| 1985 | Error("sigjmp_buf type is NULL"); |
| 1986 | return; |
| 1987 | } |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1988 | if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>()) |
Mike Stump | a4de80b | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 1989 | Context->setsigjmp_bufDecl(Typedef->getDecl()); |
| 1990 | else { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1991 | const TagType *Tag = Sigjmp_bufType->getAs<TagType>(); |
Mike Stump | a4de80b | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 1992 | assert(Tag && "Invalid sigjmp_buf type in PCH file"); |
| 1993 | Context->setsigjmp_bufDecl(Tag->getDecl()); |
| 1994 | } |
| 1995 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1996 | if (unsigned ObjCIdRedef |
Douglas Gregor | a8eed7d | 2009-08-21 00:27:50 +0000 | [diff] [blame] | 1997 | = SpecialTypes[pch::SPECIAL_TYPE_OBJC_ID_REDEFINITION]) |
| 1998 | Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1999 | if (unsigned ObjCClassRedef |
Douglas Gregor | a8eed7d | 2009-08-21 00:27:50 +0000 | [diff] [blame] | 2000 | = SpecialTypes[pch::SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) |
| 2001 | Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef); |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 2002 | if (unsigned String = SpecialTypes[pch::SPECIAL_TYPE_BLOCK_DESCRIPTOR]) |
| 2003 | Context->setBlockDescriptorType(GetType(String)); |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 2004 | if (unsigned String |
| 2005 | = SpecialTypes[pch::SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR]) |
| 2006 | Context->setBlockDescriptorExtendedType(GetType(String)); |
Fariborz Jahanian | e804c28 | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 2007 | if (unsigned ObjCSelRedef |
| 2008 | = SpecialTypes[pch::SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) |
| 2009 | Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef); |
| 2010 | if (unsigned String = SpecialTypes[pch::SPECIAL_TYPE_NS_CONSTANT_STRING]) |
| 2011 | Context->setNSConstantStringType(GetType(String)); |
Argyrios Kyrtzidis | e862cbc | 2010-07-04 21:44:19 +0000 | [diff] [blame] | 2012 | |
| 2013 | if (SpecialTypes[pch::SPECIAL_TYPE_INT128_INSTALLED]) |
| 2014 | Context->setInt128Installed(); |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 2015 | } |
| 2016 | |
Douglas Gregor | 45fe036 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 2017 | /// \brief Retrieve the name of the original source file name |
| 2018 | /// directly from the PCH file, without actually loading the PCH |
| 2019 | /// file. |
Daniel Dunbar | 3b95148 | 2009-12-03 09:13:06 +0000 | [diff] [blame] | 2020 | std::string PCHReader::getOriginalSourceFile(const std::string &PCHFileName, |
| 2021 | Diagnostic &Diags) { |
Douglas Gregor | 45fe036 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 2022 | // Open the PCH file. |
| 2023 | std::string ErrStr; |
| 2024 | llvm::OwningPtr<llvm::MemoryBuffer> Buffer; |
| 2025 | Buffer.reset(llvm::MemoryBuffer::getFile(PCHFileName.c_str(), &ErrStr)); |
| 2026 | if (!Buffer) { |
Daniel Dunbar | 3b95148 | 2009-12-03 09:13:06 +0000 | [diff] [blame] | 2027 | Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr; |
Douglas Gregor | 45fe036 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 2028 | return std::string(); |
| 2029 | } |
| 2030 | |
| 2031 | // Initialize the stream |
| 2032 | llvm::BitstreamReader StreamFile; |
| 2033 | llvm::BitstreamCursor Stream; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2034 | StreamFile.init((const unsigned char *)Buffer->getBufferStart(), |
Douglas Gregor | 45fe036 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 2035 | (const unsigned char *)Buffer->getBufferEnd()); |
| 2036 | Stream.init(StreamFile); |
| 2037 | |
| 2038 | // Sniff for the signature. |
| 2039 | if (Stream.Read(8) != 'C' || |
| 2040 | Stream.Read(8) != 'P' || |
| 2041 | Stream.Read(8) != 'C' || |
| 2042 | Stream.Read(8) != 'H') { |
Daniel Dunbar | 3b95148 | 2009-12-03 09:13:06 +0000 | [diff] [blame] | 2043 | Diags.Report(diag::err_fe_not_a_pch_file) << PCHFileName; |
Douglas Gregor | 45fe036 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 2044 | return std::string(); |
| 2045 | } |
| 2046 | |
| 2047 | RecordData Record; |
| 2048 | while (!Stream.AtEndOfStream()) { |
| 2049 | unsigned Code = Stream.ReadCode(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2050 | |
Douglas Gregor | 45fe036 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 2051 | if (Code == llvm::bitc::ENTER_SUBBLOCK) { |
| 2052 | unsigned BlockID = Stream.ReadSubBlockID(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2053 | |
Douglas Gregor | 45fe036 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 2054 | // We only know the PCH subblock ID. |
| 2055 | switch (BlockID) { |
| 2056 | case pch::PCH_BLOCK_ID: |
| 2057 | if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) { |
Daniel Dunbar | 3b95148 | 2009-12-03 09:13:06 +0000 | [diff] [blame] | 2058 | Diags.Report(diag::err_fe_pch_malformed_block) << PCHFileName; |
Douglas Gregor | 45fe036 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 2059 | return std::string(); |
| 2060 | } |
| 2061 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2062 | |
Douglas Gregor | 45fe036 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 2063 | default: |
| 2064 | if (Stream.SkipBlock()) { |
Daniel Dunbar | 3b95148 | 2009-12-03 09:13:06 +0000 | [diff] [blame] | 2065 | Diags.Report(diag::err_fe_pch_malformed_block) << PCHFileName; |
Douglas Gregor | 45fe036 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 2066 | return std::string(); |
| 2067 | } |
| 2068 | break; |
| 2069 | } |
| 2070 | continue; |
| 2071 | } |
| 2072 | |
| 2073 | if (Code == llvm::bitc::END_BLOCK) { |
| 2074 | if (Stream.ReadBlockEnd()) { |
Daniel Dunbar | 3b95148 | 2009-12-03 09:13:06 +0000 | [diff] [blame] | 2075 | Diags.Report(diag::err_fe_pch_error_at_end_block) << PCHFileName; |
Douglas Gregor | 45fe036 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 2076 | return std::string(); |
| 2077 | } |
| 2078 | continue; |
| 2079 | } |
| 2080 | |
| 2081 | if (Code == llvm::bitc::DEFINE_ABBREV) { |
| 2082 | Stream.ReadAbbrevRecord(); |
| 2083 | continue; |
| 2084 | } |
| 2085 | |
| 2086 | Record.clear(); |
| 2087 | const char *BlobStart = 0; |
| 2088 | unsigned BlobLen = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2089 | if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen) |
Douglas Gregor | 45fe036 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 2090 | == pch::ORIGINAL_FILE_NAME) |
| 2091 | return std::string(BlobStart, BlobLen); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2092 | } |
Douglas Gregor | 45fe036 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 2093 | |
| 2094 | return std::string(); |
| 2095 | } |
| 2096 | |
Douglas Gregor | 55abb23 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 2097 | /// \brief Parse the record that corresponds to a LangOptions data |
| 2098 | /// structure. |
| 2099 | /// |
| 2100 | /// This routine compares the language options used to generate the |
| 2101 | /// PCH file against the language options set for the current |
| 2102 | /// compilation. For each option, we classify differences between the |
| 2103 | /// two compiler states as either "benign" or "important". Benign |
| 2104 | /// differences don't matter, and we accept them without complaint |
| 2105 | /// (and without modifying the language options). Differences between |
| 2106 | /// the states for important options cause the PCH file to be |
| 2107 | /// unusable, so we emit a warning and return true to indicate that |
| 2108 | /// there was an error. |
| 2109 | /// |
| 2110 | /// \returns true if the PCH file is unacceptable, false otherwise. |
| 2111 | bool PCHReader::ParseLanguageOptions( |
| 2112 | const llvm::SmallVectorImpl<uint64_t> &Record) { |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 2113 | if (Listener) { |
| 2114 | LangOptions LangOpts; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2115 | |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 2116 | #define PARSE_LANGOPT(Option) \ |
| 2117 | LangOpts.Option = Record[Idx]; \ |
| 2118 | ++Idx |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2119 | |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 2120 | unsigned Idx = 0; |
| 2121 | PARSE_LANGOPT(Trigraphs); |
| 2122 | PARSE_LANGOPT(BCPLComment); |
| 2123 | PARSE_LANGOPT(DollarIdents); |
| 2124 | PARSE_LANGOPT(AsmPreprocessor); |
| 2125 | PARSE_LANGOPT(GNUMode); |
Chandler Carruth | e03aa55 | 2010-04-17 20:17:31 +0000 | [diff] [blame] | 2126 | PARSE_LANGOPT(GNUKeywords); |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 2127 | PARSE_LANGOPT(ImplicitInt); |
| 2128 | PARSE_LANGOPT(Digraphs); |
| 2129 | PARSE_LANGOPT(HexFloats); |
| 2130 | PARSE_LANGOPT(C99); |
| 2131 | PARSE_LANGOPT(Microsoft); |
| 2132 | PARSE_LANGOPT(CPlusPlus); |
| 2133 | PARSE_LANGOPT(CPlusPlus0x); |
| 2134 | PARSE_LANGOPT(CXXOperatorNames); |
| 2135 | PARSE_LANGOPT(ObjC1); |
| 2136 | PARSE_LANGOPT(ObjC2); |
| 2137 | PARSE_LANGOPT(ObjCNonFragileABI); |
Fariborz Jahanian | 4587803 | 2010-02-09 19:31:38 +0000 | [diff] [blame] | 2138 | PARSE_LANGOPT(ObjCNonFragileABI2); |
Fariborz Jahanian | 62c5602 | 2010-04-22 21:01:59 +0000 | [diff] [blame] | 2139 | PARSE_LANGOPT(NoConstantCFStrings); |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 2140 | PARSE_LANGOPT(PascalStrings); |
| 2141 | PARSE_LANGOPT(WritableStrings); |
| 2142 | PARSE_LANGOPT(LaxVectorConversions); |
Nate Begeman | f291166 | 2009-06-25 23:01:11 +0000 | [diff] [blame] | 2143 | PARSE_LANGOPT(AltiVec); |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 2144 | PARSE_LANGOPT(Exceptions); |
Daniel Dunbar | 925152c | 2010-02-10 18:48:44 +0000 | [diff] [blame] | 2145 | PARSE_LANGOPT(SjLjExceptions); |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 2146 | PARSE_LANGOPT(NeXTRuntime); |
| 2147 | PARSE_LANGOPT(Freestanding); |
| 2148 | PARSE_LANGOPT(NoBuiltin); |
| 2149 | PARSE_LANGOPT(ThreadsafeStatics); |
Douglas Gregor | b3286fe | 2009-09-03 14:36:33 +0000 | [diff] [blame] | 2150 | PARSE_LANGOPT(POSIXThreads); |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 2151 | PARSE_LANGOPT(Blocks); |
| 2152 | PARSE_LANGOPT(EmitAllDecls); |
| 2153 | PARSE_LANGOPT(MathErrno); |
Chris Lattner | 51924e51 | 2010-06-26 21:25:03 +0000 | [diff] [blame] | 2154 | LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy) |
| 2155 | Record[Idx++]); |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 2156 | PARSE_LANGOPT(HeinousExtensions); |
| 2157 | PARSE_LANGOPT(Optimize); |
| 2158 | PARSE_LANGOPT(OptimizeSize); |
| 2159 | PARSE_LANGOPT(Static); |
| 2160 | PARSE_LANGOPT(PICLevel); |
| 2161 | PARSE_LANGOPT(GNUInline); |
| 2162 | PARSE_LANGOPT(NoInline); |
| 2163 | PARSE_LANGOPT(AccessControl); |
| 2164 | PARSE_LANGOPT(CharIsSigned); |
John Thompson | ed4e295 | 2009-11-05 20:14:16 +0000 | [diff] [blame] | 2165 | PARSE_LANGOPT(ShortWChar); |
Chris Lattner | 51924e51 | 2010-06-26 21:25:03 +0000 | [diff] [blame] | 2166 | LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]); |
| 2167 | LangOpts.setVisibilityMode((LangOptions::VisibilityMode)Record[Idx++]); |
Daniel Dunbar | 143021e | 2009-09-21 04:16:19 +0000 | [diff] [blame] | 2168 | LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode) |
Chris Lattner | 51924e51 | 2010-06-26 21:25:03 +0000 | [diff] [blame] | 2169 | Record[Idx++]); |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 2170 | PARSE_LANGOPT(InstantiationDepth); |
Nate Begeman | f291166 | 2009-06-25 23:01:11 +0000 | [diff] [blame] | 2171 | PARSE_LANGOPT(OpenCL); |
Mike Stump | d954638 | 2009-12-12 01:27:46 +0000 | [diff] [blame] | 2172 | PARSE_LANGOPT(CatchUndefined); |
| 2173 | // FIXME: Missing ElideConstructors?! |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 2174 | #undef PARSE_LANGOPT |
Douglas Gregor | 55abb23 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 2175 | |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 2176 | return Listener->ReadLanguageOptions(LangOpts); |
Douglas Gregor | 55abb23 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 2177 | } |
Douglas Gregor | 55abb23 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 2178 | |
| 2179 | return false; |
| 2180 | } |
| 2181 | |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 2182 | void PCHReader::ReadPreprocessedEntities() { |
| 2183 | ReadDefinedMacros(); |
| 2184 | } |
| 2185 | |
Sebastian Redl | 837a6cb | 2010-07-20 22:37:49 +0000 | [diff] [blame] | 2186 | /// \brief Get the correct cursor and offset for loading a type. |
| 2187 | PCHReader::RecordLocation PCHReader::TypeCursorForIndex(unsigned Index) { |
| 2188 | PerFileData *F = 0; |
| 2189 | for (unsigned I = 0, N = Chain.size(); I != N; ++I) { |
| 2190 | F = Chain[N - I - 1]; |
| 2191 | if (Index < F->LocalNumTypes) |
| 2192 | break; |
| 2193 | Index -= F->LocalNumTypes; |
| 2194 | } |
| 2195 | assert(F && F->LocalNumTypes > Index && "Broken chain"); |
Sebastian Redl | b2831db | 2010-07-20 22:55:31 +0000 | [diff] [blame] | 2196 | return RecordLocation(&F->DeclsCursor, F->TypeOffsets[Index]); |
Sebastian Redl | 837a6cb | 2010-07-20 22:37:49 +0000 | [diff] [blame] | 2197 | } |
| 2198 | |
| 2199 | /// \brief Read and return the type with the given index.. |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2200 | /// |
Sebastian Redl | 837a6cb | 2010-07-20 22:37:49 +0000 | [diff] [blame] | 2201 | /// The index is the type ID, shifted and minus the number of predefs. This |
| 2202 | /// routine actually reads the record corresponding to the type at the given |
| 2203 | /// location. It is a helper routine for GetType, which deals with reading type |
| 2204 | /// IDs. |
| 2205 | QualType PCHReader::ReadTypeRecord(unsigned Index) { |
| 2206 | RecordLocation Loc = TypeCursorForIndex(Index); |
Sebastian Redl | b2831db | 2010-07-20 22:55:31 +0000 | [diff] [blame] | 2207 | llvm::BitstreamCursor &DeclsCursor = *Loc.first; |
Sebastian Redl | 3452281 | 2010-07-16 17:50:48 +0000 | [diff] [blame] | 2208 | |
Douglas Gregor | feb84b0 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 2209 | // Keep track of where we are in the stream, then jump back there |
| 2210 | // after reading this type. |
Douglas Gregor | 12bfa38 | 2009-10-17 00:13:19 +0000 | [diff] [blame] | 2211 | SavedStreamPosition SavedPosition(DeclsCursor); |
Douglas Gregor | feb84b0 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 2212 | |
Argyrios Kyrtzidis | d0795b2 | 2010-06-28 22:28:35 +0000 | [diff] [blame] | 2213 | ReadingKindTracker ReadingKind(Read_Type, *this); |
| 2214 | |
Douglas Gregor | 1342e84 | 2009-07-06 18:54:52 +0000 | [diff] [blame] | 2215 | // Note that we are loading a type record. |
| 2216 | LoadingTypeOrDecl Loading(*this); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2217 | |
Sebastian Redl | 837a6cb | 2010-07-20 22:37:49 +0000 | [diff] [blame] | 2218 | DeclsCursor.JumpToBit(Loc.second); |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2219 | RecordData Record; |
Douglas Gregor | 12bfa38 | 2009-10-17 00:13:19 +0000 | [diff] [blame] | 2220 | unsigned Code = DeclsCursor.ReadCode(); |
| 2221 | switch ((pch::TypeCode)DeclsCursor.ReadRecord(Code, Record)) { |
Douglas Gregor | 455b8f4 | 2009-04-15 22:00:08 +0000 | [diff] [blame] | 2222 | case pch::TYPE_EXT_QUAL: { |
Ted Kremenek | 1ff615c | 2010-03-18 00:56:54 +0000 | [diff] [blame] | 2223 | if (Record.size() != 2) { |
| 2224 | Error("Incorrect encoding of extended qualifier type"); |
| 2225 | return QualType(); |
| 2226 | } |
Douglas Gregor | 455b8f4 | 2009-04-15 22:00:08 +0000 | [diff] [blame] | 2227 | QualType Base = GetType(Record[0]); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2228 | Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]); |
| 2229 | return Context->getQualifiedType(Base, Quals); |
Douglas Gregor | 455b8f4 | 2009-04-15 22:00:08 +0000 | [diff] [blame] | 2230 | } |
Douglas Gregor | 85c0fcd | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 2231 | |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2232 | case pch::TYPE_COMPLEX: { |
Ted Kremenek | 1ff615c | 2010-03-18 00:56:54 +0000 | [diff] [blame] | 2233 | if (Record.size() != 1) { |
| 2234 | Error("Incorrect encoding of complex type"); |
| 2235 | return QualType(); |
| 2236 | } |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2237 | QualType ElemType = GetType(Record[0]); |
Chris Lattner | 8575daa | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 2238 | return Context->getComplexType(ElemType); |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2239 | } |
| 2240 | |
| 2241 | case pch::TYPE_POINTER: { |
Ted Kremenek | 1ff615c | 2010-03-18 00:56:54 +0000 | [diff] [blame] | 2242 | if (Record.size() != 1) { |
| 2243 | Error("Incorrect encoding of pointer type"); |
| 2244 | return QualType(); |
| 2245 | } |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2246 | QualType PointeeType = GetType(Record[0]); |
Chris Lattner | 8575daa | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 2247 | return Context->getPointerType(PointeeType); |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2248 | } |
| 2249 | |
| 2250 | case pch::TYPE_BLOCK_POINTER: { |
Ted Kremenek | 1ff615c | 2010-03-18 00:56:54 +0000 | [diff] [blame] | 2251 | if (Record.size() != 1) { |
| 2252 | Error("Incorrect encoding of block pointer type"); |
| 2253 | return QualType(); |
| 2254 | } |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2255 | QualType PointeeType = GetType(Record[0]); |
Chris Lattner | 8575daa | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 2256 | return Context->getBlockPointerType(PointeeType); |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2257 | } |
| 2258 | |
| 2259 | case pch::TYPE_LVALUE_REFERENCE: { |
Ted Kremenek | 1ff615c | 2010-03-18 00:56:54 +0000 | [diff] [blame] | 2260 | if (Record.size() != 1) { |
| 2261 | Error("Incorrect encoding of lvalue reference type"); |
| 2262 | return QualType(); |
| 2263 | } |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2264 | QualType PointeeType = GetType(Record[0]); |
Chris Lattner | 8575daa | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 2265 | return Context->getLValueReferenceType(PointeeType); |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2266 | } |
| 2267 | |
| 2268 | case pch::TYPE_RVALUE_REFERENCE: { |
Ted Kremenek | 1ff615c | 2010-03-18 00:56:54 +0000 | [diff] [blame] | 2269 | if (Record.size() != 1) { |
| 2270 | Error("Incorrect encoding of rvalue reference type"); |
| 2271 | return QualType(); |
| 2272 | } |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2273 | QualType PointeeType = GetType(Record[0]); |
Chris Lattner | 8575daa | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 2274 | return Context->getRValueReferenceType(PointeeType); |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2275 | } |
| 2276 | |
| 2277 | case pch::TYPE_MEMBER_POINTER: { |
Argyrios Kyrtzidis | ee776bc | 2010-07-02 11:55:15 +0000 | [diff] [blame] | 2278 | if (Record.size() != 2) { |
Ted Kremenek | 1ff615c | 2010-03-18 00:56:54 +0000 | [diff] [blame] | 2279 | Error("Incorrect encoding of member pointer type"); |
| 2280 | return QualType(); |
| 2281 | } |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2282 | QualType PointeeType = GetType(Record[0]); |
| 2283 | QualType ClassType = GetType(Record[1]); |
Chris Lattner | 8575daa | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 2284 | return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr()); |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2285 | } |
| 2286 | |
Douglas Gregor | 85c0fcd | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 2287 | case pch::TYPE_CONSTANT_ARRAY: { |
| 2288 | QualType ElementType = GetType(Record[0]); |
| 2289 | ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; |
| 2290 | unsigned IndexTypeQuals = Record[2]; |
| 2291 | unsigned Idx = 3; |
| 2292 | llvm::APInt Size = ReadAPInt(Record, Idx); |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2293 | return Context->getConstantArrayType(ElementType, Size, |
| 2294 | ASM, IndexTypeQuals); |
| 2295 | } |
| 2296 | |
Douglas Gregor | 85c0fcd | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 2297 | case pch::TYPE_INCOMPLETE_ARRAY: { |
| 2298 | QualType ElementType = GetType(Record[0]); |
| 2299 | ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; |
| 2300 | unsigned IndexTypeQuals = Record[2]; |
Chris Lattner | 8575daa | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 2301 | return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals); |
Douglas Gregor | 85c0fcd | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 2302 | } |
| 2303 | |
| 2304 | case pch::TYPE_VARIABLE_ARRAY: { |
Douglas Gregor | feb84b0 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 2305 | QualType ElementType = GetType(Record[0]); |
| 2306 | ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; |
| 2307 | unsigned IndexTypeQuals = Record[2]; |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2308 | SourceLocation LBLoc = SourceLocation::getFromRawEncoding(Record[3]); |
| 2309 | SourceLocation RBLoc = SourceLocation::getFromRawEncoding(Record[4]); |
Sebastian Redl | c67764e | 2010-07-22 22:43:28 +0000 | [diff] [blame] | 2310 | return Context->getVariableArrayType(ElementType, ReadExpr(DeclsCursor), |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2311 | ASM, IndexTypeQuals, |
| 2312 | SourceRange(LBLoc, RBLoc)); |
Douglas Gregor | 85c0fcd | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 2313 | } |
| 2314 | |
| 2315 | case pch::TYPE_VECTOR: { |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 2316 | if (Record.size() != 3) { |
Douglas Gregor | 6f00bf8 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 2317 | Error("incorrect encoding of vector type in PCH file"); |
Douglas Gregor | 85c0fcd | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 2318 | return QualType(); |
| 2319 | } |
| 2320 | |
| 2321 | QualType ElementType = GetType(Record[0]); |
| 2322 | unsigned NumElements = Record[1]; |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 2323 | unsigned AltiVecSpec = Record[2]; |
| 2324 | return Context->getVectorType(ElementType, NumElements, |
| 2325 | (VectorType::AltiVecSpecific)AltiVecSpec); |
Douglas Gregor | 85c0fcd | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 2326 | } |
| 2327 | |
| 2328 | case pch::TYPE_EXT_VECTOR: { |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 2329 | if (Record.size() != 3) { |
Douglas Gregor | 6f00bf8 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 2330 | Error("incorrect encoding of extended vector type in PCH file"); |
Douglas Gregor | 85c0fcd | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 2331 | return QualType(); |
| 2332 | } |
| 2333 | |
| 2334 | QualType ElementType = GetType(Record[0]); |
| 2335 | unsigned NumElements = Record[1]; |
Chris Lattner | 8575daa | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 2336 | return Context->getExtVectorType(ElementType, NumElements); |
Douglas Gregor | 85c0fcd | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 2337 | } |
| 2338 | |
| 2339 | case pch::TYPE_FUNCTION_NO_PROTO: { |
Rafael Espindola | 49b85ab | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 2340 | if (Record.size() != 4) { |
Douglas Gregor | 6f00bf8 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 2341 | Error("incorrect encoding of no-proto function type"); |
Douglas Gregor | 85c0fcd | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 2342 | return QualType(); |
| 2343 | } |
| 2344 | QualType ResultType = GetType(Record[0]); |
Rafael Espindola | 49b85ab | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 2345 | FunctionType::ExtInfo Info(Record[1], Record[2], (CallingConv)Record[3]); |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2346 | return Context->getFunctionNoProtoType(ResultType, Info); |
Douglas Gregor | 85c0fcd | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 2347 | } |
| 2348 | |
| 2349 | case pch::TYPE_FUNCTION_PROTO: { |
| 2350 | QualType ResultType = GetType(Record[0]); |
Douglas Gregor | dc72875 | 2009-12-22 18:11:50 +0000 | [diff] [blame] | 2351 | bool NoReturn = Record[1]; |
Rafael Espindola | 49b85ab | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 2352 | unsigned RegParm = Record[2]; |
| 2353 | CallingConv CallConv = (CallingConv)Record[3]; |
| 2354 | unsigned Idx = 4; |
Douglas Gregor | 85c0fcd | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 2355 | unsigned NumParams = Record[Idx++]; |
| 2356 | llvm::SmallVector<QualType, 16> ParamTypes; |
| 2357 | for (unsigned I = 0; I != NumParams; ++I) |
| 2358 | ParamTypes.push_back(GetType(Record[Idx++])); |
| 2359 | bool isVariadic = Record[Idx++]; |
| 2360 | unsigned Quals = Record[Idx++]; |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2361 | bool hasExceptionSpec = Record[Idx++]; |
| 2362 | bool hasAnyExceptionSpec = Record[Idx++]; |
| 2363 | unsigned NumExceptions = Record[Idx++]; |
| 2364 | llvm::SmallVector<QualType, 2> Exceptions; |
| 2365 | for (unsigned I = 0; I != NumExceptions; ++I) |
| 2366 | Exceptions.push_back(GetType(Record[Idx++])); |
Jay Foad | 7d0479f | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 2367 | return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams, |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2368 | isVariadic, Quals, hasExceptionSpec, |
| 2369 | hasAnyExceptionSpec, NumExceptions, |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2370 | Exceptions.data(), |
Rafael Espindola | 49b85ab | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 2371 | FunctionType::ExtInfo(NoReturn, RegParm, |
| 2372 | CallConv)); |
Douglas Gregor | 85c0fcd | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 2373 | } |
| 2374 | |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2375 | case pch::TYPE_UNRESOLVED_USING: |
| 2376 | return Context->getTypeDeclType( |
| 2377 | cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0]))); |
| 2378 | |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2379 | case pch::TYPE_TYPEDEF: { |
| 2380 | if (Record.size() != 2) { |
Ted Kremenek | 1ff615c | 2010-03-18 00:56:54 +0000 | [diff] [blame] | 2381 | Error("incorrect encoding of typedef type"); |
| 2382 | return QualType(); |
| 2383 | } |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2384 | TypedefDecl *Decl = cast<TypedefDecl>(GetDecl(Record[0])); |
| 2385 | QualType Canonical = GetType(Record[1]); |
| 2386 | return Context->getTypedefType(Decl, Canonical); |
| 2387 | } |
Douglas Gregor | 85c0fcd | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 2388 | |
| 2389 | case pch::TYPE_TYPEOF_EXPR: |
Sebastian Redl | c67764e | 2010-07-22 22:43:28 +0000 | [diff] [blame] | 2390 | return Context->getTypeOfExprType(ReadExpr(DeclsCursor)); |
Douglas Gregor | 85c0fcd | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 2391 | |
| 2392 | case pch::TYPE_TYPEOF: { |
| 2393 | if (Record.size() != 1) { |
Douglas Gregor | 6f00bf8 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 2394 | Error("incorrect encoding of typeof(type) in PCH file"); |
Douglas Gregor | 85c0fcd | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 2395 | return QualType(); |
| 2396 | } |
| 2397 | QualType UnderlyingType = GetType(Record[0]); |
Chris Lattner | 8575daa | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 2398 | return Context->getTypeOfType(UnderlyingType); |
Douglas Gregor | 85c0fcd | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 2399 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2400 | |
Anders Carlsson | 81df7b8 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2401 | case pch::TYPE_DECLTYPE: |
Sebastian Redl | c67764e | 2010-07-22 22:43:28 +0000 | [diff] [blame] | 2402 | return Context->getDecltypeType(ReadExpr(DeclsCursor)); |
Anders Carlsson | 81df7b8 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2403 | |
Argyrios Kyrtzidis | a4ed181 | 2010-07-08 13:09:53 +0000 | [diff] [blame] | 2404 | case pch::TYPE_RECORD: { |
| 2405 | if (Record.size() != 2) { |
Ted Kremenek | 1ff615c | 2010-03-18 00:56:54 +0000 | [diff] [blame] | 2406 | Error("incorrect encoding of record type"); |
| 2407 | return QualType(); |
| 2408 | } |
Argyrios Kyrtzidis | a4ed181 | 2010-07-08 13:09:53 +0000 | [diff] [blame] | 2409 | bool IsDependent = Record[0]; |
| 2410 | QualType T = Context->getRecordType(cast<RecordDecl>(GetDecl(Record[1]))); |
| 2411 | T->Dependent = IsDependent; |
| 2412 | return T; |
| 2413 | } |
Douglas Gregor | 85c0fcd | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 2414 | |
Argyrios Kyrtzidis | a4ed181 | 2010-07-08 13:09:53 +0000 | [diff] [blame] | 2415 | case pch::TYPE_ENUM: { |
| 2416 | if (Record.size() != 2) { |
Ted Kremenek | 1ff615c | 2010-03-18 00:56:54 +0000 | [diff] [blame] | 2417 | Error("incorrect encoding of enum type"); |
| 2418 | return QualType(); |
| 2419 | } |
Argyrios Kyrtzidis | a4ed181 | 2010-07-08 13:09:53 +0000 | [diff] [blame] | 2420 | bool IsDependent = Record[0]; |
| 2421 | QualType T = Context->getEnumType(cast<EnumDecl>(GetDecl(Record[1]))); |
| 2422 | T->Dependent = IsDependent; |
| 2423 | return T; |
| 2424 | } |
Douglas Gregor | 1daeb69 | 2009-04-13 18:14:40 +0000 | [diff] [blame] | 2425 | |
John McCall | fcc33b0 | 2009-09-05 00:15:47 +0000 | [diff] [blame] | 2426 | case pch::TYPE_ELABORATED: { |
Argyrios Kyrtzidis | f0f7a79 | 2010-06-25 16:24:58 +0000 | [diff] [blame] | 2427 | unsigned Idx = 0; |
| 2428 | ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++]; |
| 2429 | NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx); |
| 2430 | QualType NamedType = GetType(Record[Idx++]); |
| 2431 | return Context->getElaboratedType(Keyword, NNS, NamedType); |
John McCall | fcc33b0 | 2009-09-05 00:15:47 +0000 | [diff] [blame] | 2432 | } |
| 2433 | |
Steve Naroff | c277ad1 | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 2434 | case pch::TYPE_OBJC_INTERFACE: { |
Chris Lattner | 587cbe1 | 2009-04-22 06:45:28 +0000 | [diff] [blame] | 2435 | unsigned Idx = 0; |
| 2436 | ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++])); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2437 | return Context->getObjCInterfaceType(ItfD); |
| 2438 | } |
| 2439 | |
| 2440 | case pch::TYPE_OBJC_OBJECT: { |
| 2441 | unsigned Idx = 0; |
| 2442 | QualType Base = GetType(Record[Idx++]); |
Chris Lattner | 587cbe1 | 2009-04-22 06:45:28 +0000 | [diff] [blame] | 2443 | unsigned NumProtos = Record[Idx++]; |
| 2444 | llvm::SmallVector<ObjCProtocolDecl*, 4> Protos; |
| 2445 | for (unsigned I = 0; I != NumProtos; ++I) |
| 2446 | Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++]))); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2447 | return Context->getObjCObjectType(Base, Protos.data(), NumProtos); |
Chris Lattner | 587cbe1 | 2009-04-22 06:45:28 +0000 | [diff] [blame] | 2448 | } |
Douglas Gregor | 85c0fcd | 2009-04-13 20:46:52 +0000 | [diff] [blame] | 2449 | |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2450 | case pch::TYPE_OBJC_OBJECT_POINTER: { |
Chris Lattner | 6e054af | 2009-04-22 06:40:03 +0000 | [diff] [blame] | 2451 | unsigned Idx = 0; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2452 | QualType Pointee = GetType(Record[Idx++]); |
| 2453 | return Context->getObjCObjectPointerType(Pointee); |
Chris Lattner | 6e054af | 2009-04-22 06:40:03 +0000 | [diff] [blame] | 2454 | } |
Argyrios Kyrtzidis | a7a36df | 2009-09-29 19:42:55 +0000 | [diff] [blame] | 2455 | |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 2456 | case pch::TYPE_SUBST_TEMPLATE_TYPE_PARM: { |
| 2457 | unsigned Idx = 0; |
| 2458 | QualType Parm = GetType(Record[Idx++]); |
| 2459 | QualType Replacement = GetType(Record[Idx++]); |
| 2460 | return |
| 2461 | Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm), |
| 2462 | Replacement); |
| 2463 | } |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2464 | |
| 2465 | case pch::TYPE_INJECTED_CLASS_NAME: { |
| 2466 | CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0])); |
| 2467 | QualType TST = GetType(Record[1]); // probably derivable |
Argyrios Kyrtzidis | dab33c5 | 2010-07-02 11:55:20 +0000 | [diff] [blame] | 2468 | // FIXME: ASTContext::getInjectedClassNameType is not currently suitable |
| 2469 | // for PCH reading, too much interdependencies. |
| 2470 | return |
| 2471 | QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2472 | } |
Argyrios Kyrtzidis | 106caf92 | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 2473 | |
Argyrios Kyrtzidis | 95c04ca | 2010-06-19 19:29:09 +0000 | [diff] [blame] | 2474 | case pch::TYPE_TEMPLATE_TYPE_PARM: { |
| 2475 | unsigned Idx = 0; |
| 2476 | unsigned Depth = Record[Idx++]; |
| 2477 | unsigned Index = Record[Idx++]; |
| 2478 | bool Pack = Record[Idx++]; |
| 2479 | IdentifierInfo *Name = GetIdentifierInfo(Record, Idx); |
| 2480 | return Context->getTemplateTypeParmType(Depth, Index, Pack, Name); |
| 2481 | } |
Argyrios Kyrtzidis | bfcacee | 2010-06-24 08:57:31 +0000 | [diff] [blame] | 2482 | |
| 2483 | case pch::TYPE_DEPENDENT_NAME: { |
| 2484 | unsigned Idx = 0; |
| 2485 | ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++]; |
| 2486 | NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx); |
| 2487 | const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx); |
Argyrios Kyrtzidis | e929095 | 2010-07-02 11:55:24 +0000 | [diff] [blame] | 2488 | QualType Canon = GetType(Record[Idx++]); |
| 2489 | return Context->getDependentNameType(Keyword, NNS, Name, Canon); |
Argyrios Kyrtzidis | bfcacee | 2010-06-24 08:57:31 +0000 | [diff] [blame] | 2490 | } |
Argyrios Kyrtzidis | f0f7a79 | 2010-06-25 16:24:58 +0000 | [diff] [blame] | 2491 | |
| 2492 | case pch::TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: { |
| 2493 | unsigned Idx = 0; |
| 2494 | ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++]; |
| 2495 | NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx); |
| 2496 | const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx); |
| 2497 | unsigned NumArgs = Record[Idx++]; |
| 2498 | llvm::SmallVector<TemplateArgument, 8> Args; |
| 2499 | Args.reserve(NumArgs); |
| 2500 | while (NumArgs--) |
Sebastian Redl | c67764e | 2010-07-22 22:43:28 +0000 | [diff] [blame] | 2501 | Args.push_back(ReadTemplateArgument(DeclsCursor, Record, Idx)); |
Argyrios Kyrtzidis | f0f7a79 | 2010-06-25 16:24:58 +0000 | [diff] [blame] | 2502 | return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name, |
| 2503 | Args.size(), Args.data()); |
| 2504 | } |
Argyrios Kyrtzidis | 4a57bd0 | 2010-06-30 08:49:25 +0000 | [diff] [blame] | 2505 | |
| 2506 | case pch::TYPE_DEPENDENT_SIZED_ARRAY: { |
| 2507 | unsigned Idx = 0; |
| 2508 | |
| 2509 | // ArrayType |
| 2510 | QualType ElementType = GetType(Record[Idx++]); |
| 2511 | ArrayType::ArraySizeModifier ASM |
| 2512 | = (ArrayType::ArraySizeModifier)Record[Idx++]; |
| 2513 | unsigned IndexTypeQuals = Record[Idx++]; |
| 2514 | |
| 2515 | // DependentSizedArrayType |
Sebastian Redl | c67764e | 2010-07-22 22:43:28 +0000 | [diff] [blame] | 2516 | Expr *NumElts = ReadExpr(DeclsCursor); |
Argyrios Kyrtzidis | 4a57bd0 | 2010-06-30 08:49:25 +0000 | [diff] [blame] | 2517 | SourceRange Brackets = ReadSourceRange(Record, Idx); |
| 2518 | |
| 2519 | return Context->getDependentSizedArrayType(ElementType, NumElts, ASM, |
| 2520 | IndexTypeQuals, Brackets); |
| 2521 | } |
Argyrios Kyrtzidis | 106caf92 | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 2522 | |
Argyrios Kyrtzidis | 95c04ca | 2010-06-19 19:29:09 +0000 | [diff] [blame] | 2523 | case pch::TYPE_TEMPLATE_SPECIALIZATION: { |
| 2524 | unsigned Idx = 0; |
Argyrios Kyrtzidis | a4ed181 | 2010-07-08 13:09:53 +0000 | [diff] [blame] | 2525 | bool IsDependent = Record[Idx++]; |
Argyrios Kyrtzidis | 95c04ca | 2010-06-19 19:29:09 +0000 | [diff] [blame] | 2526 | TemplateName Name = ReadTemplateName(Record, Idx); |
Argyrios Kyrtzidis | 95c04ca | 2010-06-19 19:29:09 +0000 | [diff] [blame] | 2527 | llvm::SmallVector<TemplateArgument, 8> Args; |
Sebastian Redl | c67764e | 2010-07-22 22:43:28 +0000 | [diff] [blame] | 2528 | ReadTemplateArgumentList(Args, DeclsCursor, Record, Idx); |
Argyrios Kyrtzidis | 818c5db | 2010-06-23 13:48:30 +0000 | [diff] [blame] | 2529 | QualType Canon = GetType(Record[Idx++]); |
Argyrios Kyrtzidis | a4ed181 | 2010-07-08 13:09:53 +0000 | [diff] [blame] | 2530 | QualType T; |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2531 | if (Canon.isNull()) |
Argyrios Kyrtzidis | a4ed181 | 2010-07-08 13:09:53 +0000 | [diff] [blame] | 2532 | T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(), |
| 2533 | Args.size()); |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2534 | else |
Argyrios Kyrtzidis | a4ed181 | 2010-07-08 13:09:53 +0000 | [diff] [blame] | 2535 | T = Context->getTemplateSpecializationType(Name, Args.data(), |
| 2536 | Args.size(), Canon); |
| 2537 | T->Dependent = IsDependent; |
| 2538 | return T; |
Argyrios Kyrtzidis | 95c04ca | 2010-06-19 19:29:09 +0000 | [diff] [blame] | 2539 | } |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2540 | } |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2541 | // Suppress a GCC warning |
| 2542 | return QualType(); |
| 2543 | } |
| 2544 | |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2545 | namespace { |
| 2546 | |
| 2547 | class TypeLocReader : public TypeLocVisitor<TypeLocReader> { |
| 2548 | PCHReader &Reader; |
Sebastian Redl | c67764e | 2010-07-22 22:43:28 +0000 | [diff] [blame] | 2549 | llvm::BitstreamCursor &DeclsCursor; |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2550 | const PCHReader::RecordData &Record; |
| 2551 | unsigned &Idx; |
| 2552 | |
| 2553 | public: |
Sebastian Redl | c67764e | 2010-07-22 22:43:28 +0000 | [diff] [blame] | 2554 | TypeLocReader(PCHReader &Reader, llvm::BitstreamCursor &Cursor, |
| 2555 | const PCHReader::RecordData &Record, unsigned &Idx) |
| 2556 | : Reader(Reader), DeclsCursor(Cursor), Record(Record), Idx(Idx) { } |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2557 | |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2558 | // We want compile-time assurance that we've enumerated all of |
| 2559 | // these, so unfortunately we have to declare them first, then |
| 2560 | // define them out-of-line. |
| 2561 | #define ABSTRACT_TYPELOC(CLASS, PARENT) |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2562 | #define TYPELOC(CLASS, PARENT) \ |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2563 | void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc); |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2564 | #include "clang/AST/TypeLocNodes.def" |
| 2565 | |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2566 | void VisitFunctionTypeLoc(FunctionTypeLoc); |
| 2567 | void VisitArrayTypeLoc(ArrayTypeLoc); |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2568 | }; |
| 2569 | |
| 2570 | } |
| 2571 | |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2572 | void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2573 | // nothing to do |
| 2574 | } |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2575 | void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { |
Douglas Gregor | c9b7a59 | 2010-01-18 18:04:31 +0000 | [diff] [blame] | 2576 | TL.setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2577 | if (TL.needsExtraLocalData()) { |
| 2578 | TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++])); |
| 2579 | TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++])); |
| 2580 | TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++])); |
| 2581 | TL.setModeAttr(Record[Idx++]); |
| 2582 | } |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2583 | } |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2584 | void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) { |
| 2585 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2586 | } |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2587 | void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) { |
| 2588 | TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2589 | } |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2590 | void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { |
| 2591 | TL.setCaretLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2592 | } |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2593 | void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { |
| 2594 | TL.setAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2595 | } |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2596 | void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { |
| 2597 | TL.setAmpAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2598 | } |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2599 | void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { |
| 2600 | TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2601 | } |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2602 | void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) { |
| 2603 | TL.setLBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2604 | TL.setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2605 | if (Record[Idx++]) |
Sebastian Redl | c67764e | 2010-07-22 22:43:28 +0000 | [diff] [blame] | 2606 | TL.setSizeExpr(Reader.ReadExpr(DeclsCursor)); |
Douglas Gregor | 12bfa38 | 2009-10-17 00:13:19 +0000 | [diff] [blame] | 2607 | else |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2608 | TL.setSizeExpr(0); |
| 2609 | } |
| 2610 | void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) { |
| 2611 | VisitArrayTypeLoc(TL); |
| 2612 | } |
| 2613 | void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) { |
| 2614 | VisitArrayTypeLoc(TL); |
| 2615 | } |
| 2616 | void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) { |
| 2617 | VisitArrayTypeLoc(TL); |
| 2618 | } |
| 2619 | void TypeLocReader::VisitDependentSizedArrayTypeLoc( |
| 2620 | DependentSizedArrayTypeLoc TL) { |
| 2621 | VisitArrayTypeLoc(TL); |
| 2622 | } |
| 2623 | void TypeLocReader::VisitDependentSizedExtVectorTypeLoc( |
| 2624 | DependentSizedExtVectorTypeLoc TL) { |
| 2625 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2626 | } |
| 2627 | void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) { |
| 2628 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2629 | } |
| 2630 | void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) { |
| 2631 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2632 | } |
| 2633 | void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) { |
| 2634 | TL.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2635 | TL.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2636 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) { |
John McCall | e634700 | 2009-10-23 01:28:53 +0000 | [diff] [blame] | 2637 | TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++]))); |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2638 | } |
| 2639 | } |
| 2640 | void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) { |
| 2641 | VisitFunctionTypeLoc(TL); |
| 2642 | } |
| 2643 | void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) { |
| 2644 | VisitFunctionTypeLoc(TL); |
| 2645 | } |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2646 | void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { |
| 2647 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2648 | } |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2649 | void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) { |
| 2650 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2651 | } |
| 2652 | void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { |
John McCall | e859503 | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 2653 | TL.setTypeofLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2654 | TL.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2655 | TL.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2656 | } |
| 2657 | void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { |
John McCall | e859503 | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 2658 | TL.setTypeofLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2659 | TL.setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2660 | TL.setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
Sebastian Redl | c67764e | 2010-07-22 22:43:28 +0000 | [diff] [blame] | 2661 | TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(DeclsCursor, Record, Idx)); |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2662 | } |
| 2663 | void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { |
| 2664 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2665 | } |
| 2666 | void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) { |
| 2667 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2668 | } |
| 2669 | void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) { |
| 2670 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2671 | } |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2672 | void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { |
| 2673 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2674 | } |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 2675 | void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc( |
| 2676 | SubstTemplateTypeParmTypeLoc TL) { |
| 2677 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2678 | } |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2679 | void TypeLocReader::VisitTemplateSpecializationTypeLoc( |
| 2680 | TemplateSpecializationTypeLoc TL) { |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2681 | TL.setTemplateNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2682 | TL.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2683 | TL.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2684 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
| 2685 | TL.setArgLocInfo(i, |
| 2686 | Reader.GetTemplateArgumentLocInfo(TL.getTypePtr()->getArg(i).getKind(), |
Sebastian Redl | c67764e | 2010-07-22 22:43:28 +0000 | [diff] [blame] | 2687 | DeclsCursor, Record, Idx)); |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2688 | } |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2689 | void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 2690 | TL.setKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2691 | TL.setQualifierRange(Reader.ReadSourceRange(Record, Idx)); |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2692 | } |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2693 | void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { |
| 2694 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2695 | } |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 2696 | void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 2697 | TL.setKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2698 | TL.setQualifierRange(Reader.ReadSourceRange(Record, Idx)); |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2699 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2700 | } |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2701 | void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc( |
| 2702 | DependentTemplateSpecializationTypeLoc TL) { |
| 2703 | TL.setKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2704 | TL.setQualifierRange(Reader.ReadSourceRange(Record, Idx)); |
| 2705 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2706 | TL.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2707 | TL.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2708 | for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) |
| 2709 | TL.setArgLocInfo(I, |
| 2710 | Reader.GetTemplateArgumentLocInfo(TL.getTypePtr()->getArg(I).getKind(), |
Sebastian Redl | c67764e | 2010-07-22 22:43:28 +0000 | [diff] [blame] | 2711 | DeclsCursor, Record, Idx)); |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2712 | } |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2713 | void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { |
| 2714 | TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2715 | } |
| 2716 | void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { |
| 2717 | TL.setHasBaseTypeAsWritten(Record[Idx++]); |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2718 | TL.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2719 | TL.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
| 2720 | for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) |
| 2721 | TL.setProtocolLoc(i, SourceLocation::getFromRawEncoding(Record[Idx++])); |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2722 | } |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2723 | void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { |
| 2724 | TL.setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2725 | } |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2726 | |
Sebastian Redl | c67764e | 2010-07-22 22:43:28 +0000 | [diff] [blame] | 2727 | TypeSourceInfo *PCHReader::GetTypeSourceInfo(llvm::BitstreamCursor &DeclsCursor, |
| 2728 | const RecordData &Record, |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2729 | unsigned &Idx) { |
| 2730 | QualType InfoTy = GetType(Record[Idx++]); |
| 2731 | if (InfoTy.isNull()) |
| 2732 | return 0; |
| 2733 | |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2734 | TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy); |
Sebastian Redl | c67764e | 2010-07-22 22:43:28 +0000 | [diff] [blame] | 2735 | TypeLocReader TLR(*this, DeclsCursor, Record, Idx); |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2736 | for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc()) |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2737 | TLR.Visit(TL); |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2738 | return TInfo; |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 2739 | } |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2740 | |
Douglas Gregor | 1e9bf3b | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 2741 | QualType PCHReader::GetType(pch::TypeID ID) { |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2742 | unsigned FastQuals = ID & Qualifiers::FastMask; |
| 2743 | unsigned Index = ID >> Qualifiers::FastWidth; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2744 | |
| 2745 | if (Index < pch::NUM_PREDEF_TYPE_IDS) { |
| 2746 | QualType T; |
| 2747 | switch ((pch::PredefinedTypeIDs)Index) { |
| 2748 | case pch::PREDEF_TYPE_NULL_ID: return QualType(); |
Chris Lattner | 8575daa | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 2749 | case pch::PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break; |
| 2750 | case pch::PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2751 | |
| 2752 | case pch::PREDEF_TYPE_CHAR_U_ID: |
| 2753 | case pch::PREDEF_TYPE_CHAR_S_ID: |
| 2754 | // FIXME: Check that the signedness of CharTy is correct! |
Chris Lattner | 8575daa | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 2755 | T = Context->CharTy; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2756 | break; |
| 2757 | |
Chris Lattner | 8575daa | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 2758 | case pch::PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break; |
| 2759 | case pch::PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break; |
| 2760 | case pch::PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break; |
| 2761 | case pch::PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break; |
| 2762 | case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break; |
Chris Lattner | f122cef | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 2763 | case pch::PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break; |
Chris Lattner | 8575daa | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 2764 | case pch::PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break; |
| 2765 | case pch::PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break; |
| 2766 | case pch::PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break; |
| 2767 | case pch::PREDEF_TYPE_INT_ID: T = Context->IntTy; break; |
| 2768 | case pch::PREDEF_TYPE_LONG_ID: T = Context->LongTy; break; |
| 2769 | case pch::PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break; |
Chris Lattner | f122cef | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 2770 | case pch::PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break; |
Chris Lattner | 8575daa | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 2771 | case pch::PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break; |
| 2772 | case pch::PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break; |
| 2773 | case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break; |
| 2774 | case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break; |
| 2775 | case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break; |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2776 | case pch::PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break; |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2777 | case pch::PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break; |
| 2778 | case pch::PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break; |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2779 | case pch::PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break; |
| 2780 | case pch::PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break; |
Fariborz Jahanian | 252ba5f | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 2781 | case pch::PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2782 | } |
| 2783 | |
| 2784 | assert(!T.isNull() && "Unknown predefined type"); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2785 | return T.withFastQualifiers(FastQuals); |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2786 | } |
| 2787 | |
| 2788 | Index -= pch::NUM_PREDEF_TYPE_IDS; |
Sebastian Redl | 837a6cb | 2010-07-20 22:37:49 +0000 | [diff] [blame] | 2789 | assert(Index < TypesLoaded.size() && "Type index out-of-range"); |
Sebastian Redl | 409183f | 2010-07-14 20:26:45 +0000 | [diff] [blame] | 2790 | if (TypesLoaded[Index].isNull()) { |
Sebastian Redl | 837a6cb | 2010-07-20 22:37:49 +0000 | [diff] [blame] | 2791 | TypesLoaded[Index] = ReadTypeRecord(Index); |
Sebastian Redl | 409183f | 2010-07-14 20:26:45 +0000 | [diff] [blame] | 2792 | TypesLoaded[Index]->setFromPCH(); |
Sebastian Redl | 85b2a6a | 2010-07-14 23:45:08 +0000 | [diff] [blame] | 2793 | if (DeserializationListener) |
Sebastian Redl | 1ea025b | 2010-07-16 16:36:56 +0000 | [diff] [blame] | 2794 | DeserializationListener->TypeRead(ID >> Qualifiers::FastWidth, |
| 2795 | TypesLoaded[Index]); |
Sebastian Redl | 409183f | 2010-07-14 20:26:45 +0000 | [diff] [blame] | 2796 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2797 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2798 | return TypesLoaded[Index].withFastQualifiers(FastQuals); |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2799 | } |
| 2800 | |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2801 | TemplateArgumentLocInfo |
| 2802 | PCHReader::GetTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind, |
Sebastian Redl | c67764e | 2010-07-22 22:43:28 +0000 | [diff] [blame] | 2803 | llvm::BitstreamCursor &DeclsCursor, |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2804 | const RecordData &Record, |
Argyrios Kyrtzidis | d0795b2 | 2010-06-28 22:28:35 +0000 | [diff] [blame] | 2805 | unsigned &Index) { |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2806 | switch (Kind) { |
| 2807 | case TemplateArgument::Expression: |
Sebastian Redl | c67764e | 2010-07-22 22:43:28 +0000 | [diff] [blame] | 2808 | return ReadExpr(DeclsCursor); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2809 | case TemplateArgument::Type: |
Sebastian Redl | c67764e | 2010-07-22 22:43:28 +0000 | [diff] [blame] | 2810 | return GetTypeSourceInfo(DeclsCursor, Record, Index); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2811 | case TemplateArgument::Template: { |
Argyrios Kyrtzidis | ddf5f21 | 2010-06-28 09:31:42 +0000 | [diff] [blame] | 2812 | SourceRange QualifierRange = ReadSourceRange(Record, Index); |
| 2813 | SourceLocation TemplateNameLoc = ReadSourceLocation(Record, Index); |
| 2814 | return TemplateArgumentLocInfo(QualifierRange, TemplateNameLoc); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2815 | } |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2816 | case TemplateArgument::Null: |
| 2817 | case TemplateArgument::Integral: |
| 2818 | case TemplateArgument::Declaration: |
| 2819 | case TemplateArgument::Pack: |
| 2820 | return TemplateArgumentLocInfo(); |
| 2821 | } |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2822 | llvm_unreachable("unexpected template argument loc"); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2823 | return TemplateArgumentLocInfo(); |
| 2824 | } |
| 2825 | |
Argyrios Kyrtzidis | ddf5f21 | 2010-06-28 09:31:42 +0000 | [diff] [blame] | 2826 | TemplateArgumentLoc |
Sebastian Redl | c67764e | 2010-07-22 22:43:28 +0000 | [diff] [blame] | 2827 | PCHReader::ReadTemplateArgumentLoc(llvm::BitstreamCursor &DeclsCursor, |
| 2828 | const RecordData &Record, unsigned &Index) { |
| 2829 | TemplateArgument Arg = ReadTemplateArgument(DeclsCursor, Record, Index); |
Argyrios Kyrtzidis | ddf5f21 | 2010-06-28 09:31:42 +0000 | [diff] [blame] | 2830 | |
| 2831 | if (Arg.getKind() == TemplateArgument::Expression) { |
| 2832 | if (Record[Index++]) // bool InfoHasSameExpr. |
| 2833 | return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr())); |
| 2834 | } |
| 2835 | return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(Arg.getKind(), |
Sebastian Redl | c67764e | 2010-07-22 22:43:28 +0000 | [diff] [blame] | 2836 | DeclsCursor, |
Argyrios Kyrtzidis | d0795b2 | 2010-06-28 22:28:35 +0000 | [diff] [blame] | 2837 | Record, Index)); |
Argyrios Kyrtzidis | ae85e24 | 2010-06-22 09:54:59 +0000 | [diff] [blame] | 2838 | } |
| 2839 | |
John McCall | 75b960e | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 2840 | Decl *PCHReader::GetExternalDecl(uint32_t ID) { |
| 2841 | return GetDecl(ID); |
| 2842 | } |
| 2843 | |
Argyrios Kyrtzidis | 7e8996c | 2010-07-08 17:13:02 +0000 | [diff] [blame] | 2844 | TranslationUnitDecl *PCHReader::GetTranslationUnitDecl() { |
Sebastian Redl | 85b2a6a | 2010-07-14 23:45:08 +0000 | [diff] [blame] | 2845 | if (!DeclsLoaded[0]) { |
Sebastian Redl | 3462779 | 2010-07-20 22:46:15 +0000 | [diff] [blame] | 2846 | ReadDeclRecord(0); |
Sebastian Redl | 85b2a6a | 2010-07-14 23:45:08 +0000 | [diff] [blame] | 2847 | if (DeserializationListener) |
Sebastian Redl | 1ea025b | 2010-07-16 16:36:56 +0000 | [diff] [blame] | 2848 | DeserializationListener->DeclRead(1, DeclsLoaded[0]); |
Sebastian Redl | 85b2a6a | 2010-07-14 23:45:08 +0000 | [diff] [blame] | 2849 | } |
Argyrios Kyrtzidis | 7e8996c | 2010-07-08 17:13:02 +0000 | [diff] [blame] | 2850 | |
| 2851 | return cast<TranslationUnitDecl>(DeclsLoaded[0]); |
| 2852 | } |
| 2853 | |
Douglas Gregor | 1e9bf3b | 2009-04-10 17:25:41 +0000 | [diff] [blame] | 2854 | Decl *PCHReader::GetDecl(pch::DeclID ID) { |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2855 | if (ID == 0) |
| 2856 | return 0; |
| 2857 | |
Douglas Gregor | 745ed14 | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 2858 | if (ID > DeclsLoaded.size()) { |
Douglas Gregor | 6f00bf8 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 2859 | Error("declaration ID out-of-range for PCH file"); |
Douglas Gregor | 745ed14 | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 2860 | return 0; |
| 2861 | } |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2862 | |
Douglas Gregor | 745ed14 | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 2863 | unsigned Index = ID - 1; |
Sebastian Redl | 85b2a6a | 2010-07-14 23:45:08 +0000 | [diff] [blame] | 2864 | if (!DeclsLoaded[Index]) { |
Sebastian Redl | 3462779 | 2010-07-20 22:46:15 +0000 | [diff] [blame] | 2865 | ReadDeclRecord(Index); |
Sebastian Redl | 85b2a6a | 2010-07-14 23:45:08 +0000 | [diff] [blame] | 2866 | if (DeserializationListener) |
| 2867 | DeserializationListener->DeclRead(ID, DeclsLoaded[Index]); |
| 2868 | } |
Douglas Gregor | 745ed14 | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 2869 | |
| 2870 | return DeclsLoaded[Index]; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2871 | } |
| 2872 | |
Chris Lattner | 9c28af0 | 2009-04-27 05:46:25 +0000 | [diff] [blame] | 2873 | /// \brief Resolve the offset of a statement into a statement. |
| 2874 | /// |
| 2875 | /// This operation will read a new statement from the external |
| 2876 | /// source each time it is called, and is meant to be used via a |
| 2877 | /// LazyOffsetPtr (which is used by Decls for the body of functions, etc). |
John McCall | 75b960e | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 2878 | Stmt *PCHReader::GetExternalDeclStmt(uint64_t Offset) { |
Sebastian Redl | 5c415f3 | 2010-07-22 17:01:13 +0000 | [diff] [blame] | 2879 | // Offset here is a global offset across the entire chain. |
| 2880 | for (unsigned I = 0, N = Chain.size(); I != N; ++I) { |
| 2881 | PerFileData &F = *Chain[N - I - 1]; |
| 2882 | if (Offset < F.SizeInBits) { |
| 2883 | // Since we know that this statement is part of a decl, make sure to use |
| 2884 | // the decl cursor to read it. |
| 2885 | F.DeclsCursor.JumpToBit(Offset); |
| 2886 | return ReadStmtFromStream(F.DeclsCursor); |
| 2887 | } |
| 2888 | Offset -= F.SizeInBits; |
| 2889 | } |
| 2890 | llvm_unreachable("Broken chain"); |
Douglas Gregor | 3c3aa61 | 2009-04-18 00:07:54 +0000 | [diff] [blame] | 2891 | } |
| 2892 | |
John McCall | 75b960e | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 2893 | bool PCHReader::FindExternalLexicalDecls(const DeclContext *DC, |
| 2894 | llvm::SmallVectorImpl<Decl*> &Decls) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2895 | assert(DC->hasExternalLexicalStorage() && |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2896 | "DeclContext has no lexical decls in storage"); |
Ted Kremenek | 1ff615c | 2010-03-18 00:56:54 +0000 | [diff] [blame] | 2897 | |
Sebastian Redl | 5c415f3 | 2010-07-22 17:01:13 +0000 | [diff] [blame] | 2898 | // There might be lexical decls in multiple parts of the chain, for the TU |
| 2899 | // at least. |
| 2900 | DeclContextInfos &Infos = DeclContextOffsets[DC]; |
| 2901 | for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end(); |
| 2902 | I != E; ++I) { |
Sebastian Redl | 66c5eef | 2010-07-27 00:17:23 +0000 | [diff] [blame] | 2903 | // IDs can be 0 if this context doesn't contain declarations. |
| 2904 | if (!I->LexicalDecls) |
Sebastian Redl | 5c415f3 | 2010-07-22 17:01:13 +0000 | [diff] [blame] | 2905 | continue; |
Sebastian Redl | 5c415f3 | 2010-07-22 17:01:13 +0000 | [diff] [blame] | 2906 | |
| 2907 | // Load all of the declaration IDs |
Sebastian Redl | 66c5eef | 2010-07-27 00:17:23 +0000 | [diff] [blame] | 2908 | for (const pch::DeclID *ID = I->LexicalDecls, |
| 2909 | *IDE = ID + I->NumLexicalDecls; |
| 2910 | ID != IDE; ++ID) |
| 2911 | Decls.push_back(GetDecl(*ID)); |
Ted Kremenek | 1ff615c | 2010-03-18 00:56:54 +0000 | [diff] [blame] | 2912 | } |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2913 | |
Douglas Gregor | a57c3ab | 2009-04-22 22:34:57 +0000 | [diff] [blame] | 2914 | ++NumLexicalDeclContextsRead; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2915 | return false; |
| 2916 | } |
| 2917 | |
John McCall | 75b960e | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 2918 | DeclContext::lookup_result |
| 2919 | PCHReader::FindExternalVisibleDeclsByName(const DeclContext *DC, |
| 2920 | DeclarationName Name) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2921 | assert(DC->hasExternalVisibleStorage() && |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2922 | "DeclContext has no visible decls in storage"); |
Ted Kremenek | 1ff615c | 2010-03-18 00:56:54 +0000 | [diff] [blame] | 2923 | |
John McCall | 75b960e | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 2924 | llvm::SmallVector<VisibleDeclaration, 64> Decls; |
Sebastian Redl | 5c415f3 | 2010-07-22 17:01:13 +0000 | [diff] [blame] | 2925 | // There might be lexical decls in multiple parts of the chain, for the TU |
| 2926 | // and namespaces. |
| 2927 | DeclContextInfos &Infos = DeclContextOffsets[DC]; |
| 2928 | for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end(); |
| 2929 | I != E; ++I) { |
| 2930 | uint64_t Offset = I->OffsetToVisibleDecls; |
| 2931 | if (Offset == 0) |
| 2932 | continue; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2933 | |
Sebastian Redl | 5c415f3 | 2010-07-22 17:01:13 +0000 | [diff] [blame] | 2934 | llvm::BitstreamCursor &DeclsCursor = *I->Stream; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2935 | |
Sebastian Redl | 5c415f3 | 2010-07-22 17:01:13 +0000 | [diff] [blame] | 2936 | // Keep track of where we are in the stream, then jump back there |
| 2937 | // after reading this context. |
| 2938 | SavedStreamPosition SavedPosition(DeclsCursor); |
| 2939 | |
| 2940 | // Load the record containing all of the declarations visible in |
| 2941 | // this context. |
| 2942 | DeclsCursor.JumpToBit(Offset); |
| 2943 | RecordData Record; |
| 2944 | unsigned Code = DeclsCursor.ReadCode(); |
| 2945 | unsigned RecCode = DeclsCursor.ReadRecord(Code, Record); |
| 2946 | if (RecCode != pch::DECL_CONTEXT_VISIBLE) { |
| 2947 | Error("Expected visible block"); |
| 2948 | return DeclContext::lookup_result(DeclContext::lookup_iterator(), |
| 2949 | DeclContext::lookup_iterator()); |
| 2950 | } |
| 2951 | |
| 2952 | if (Record.empty()) |
| 2953 | continue; |
| 2954 | |
| 2955 | unsigned Idx = 0; |
| 2956 | while (Idx < Record.size()) { |
| 2957 | Decls.push_back(VisibleDeclaration()); |
| 2958 | Decls.back().Name = ReadDeclarationName(Record, Idx); |
| 2959 | |
| 2960 | unsigned Size = Record[Idx++]; |
| 2961 | llvm::SmallVector<unsigned, 4> &LoadedDecls = Decls.back().Declarations; |
| 2962 | LoadedDecls.reserve(Size); |
| 2963 | for (unsigned J = 0; J < Size; ++J) |
| 2964 | LoadedDecls.push_back(Record[Idx++]); |
| 2965 | } |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2966 | } |
| 2967 | |
Douglas Gregor | a57c3ab | 2009-04-22 22:34:57 +0000 | [diff] [blame] | 2968 | ++NumVisibleDeclContextsRead; |
John McCall | 75b960e | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 2969 | |
| 2970 | SetExternalVisibleDecls(DC, Decls); |
| 2971 | return const_cast<DeclContext*>(DC)->lookup(Name); |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2972 | } |
| 2973 | |
Argyrios Kyrtzidis | 903ccd6 | 2010-07-07 15:46:26 +0000 | [diff] [blame] | 2974 | void PCHReader::PassInterestingDeclsToConsumer() { |
| 2975 | assert(Consumer); |
| 2976 | while (!InterestingDecls.empty()) { |
| 2977 | DeclGroupRef DG(InterestingDecls.front()); |
| 2978 | InterestingDecls.pop_front(); |
| 2979 | Consumer->HandleTopLevelDecl(DG); |
| 2980 | } |
| 2981 | } |
| 2982 | |
Douglas Gregor | 1a0d0b9 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 2983 | void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) { |
Douglas Gregor | b985eeb | 2009-04-22 19:09:20 +0000 | [diff] [blame] | 2984 | this->Consumer = Consumer; |
| 2985 | |
Douglas Gregor | 1a0d0b9 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 2986 | if (!Consumer) |
| 2987 | return; |
| 2988 | |
| 2989 | for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) { |
Argyrios Kyrtzidis | 903ccd6 | 2010-07-07 15:46:26 +0000 | [diff] [blame] | 2990 | // Force deserialization of this decl, which will cause it to be queued for |
| 2991 | // passing to the consumer. |
Daniel Dunbar | 865c2a7 | 2009-09-17 03:06:44 +0000 | [diff] [blame] | 2992 | GetDecl(ExternalDefinitions[I]); |
Douglas Gregor | 1a0d0b9 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 2993 | } |
Douglas Gregor | f005eac | 2009-04-25 00:41:30 +0000 | [diff] [blame] | 2994 | |
Argyrios Kyrtzidis | 903ccd6 | 2010-07-07 15:46:26 +0000 | [diff] [blame] | 2995 | PassInterestingDeclsToConsumer(); |
Douglas Gregor | 1a0d0b9 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 2996 | } |
| 2997 | |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2998 | void PCHReader::PrintStats() { |
| 2999 | std::fprintf(stderr, "*** PCH Statistics:\n"); |
| 3000 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3001 | unsigned NumTypesLoaded |
Douglas Gregor | 0e14997 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 3002 | = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3003 | QualType()); |
Douglas Gregor | 0e14997 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 3004 | unsigned NumDeclsLoaded |
| 3005 | = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(), |
| 3006 | (Decl *)0); |
| 3007 | unsigned NumIdentifiersLoaded |
| 3008 | = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(), |
| 3009 | IdentifiersLoaded.end(), |
| 3010 | (IdentifierInfo *)0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3011 | unsigned NumSelectorsLoaded |
Douglas Gregor | 0e14997 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 3012 | = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(), |
| 3013 | SelectorsLoaded.end(), |
| 3014 | Selector()); |
Douglas Gregor | c3b1dd1 | 2009-04-13 20:50:16 +0000 | [diff] [blame] | 3015 | |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 3016 | std::fprintf(stderr, " %u stat cache hits\n", NumStatHits); |
| 3017 | std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses); |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 3018 | if (TotalNumSLocEntries) |
| 3019 | std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n", |
| 3020 | NumSLocEntriesRead, TotalNumSLocEntries, |
| 3021 | ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100)); |
Douglas Gregor | 745ed14 | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 3022 | if (!TypesLoaded.empty()) |
Douglas Gregor | 95c13f5 | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 3023 | std::fprintf(stderr, " %u/%u types read (%f%%)\n", |
Douglas Gregor | 745ed14 | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 3024 | NumTypesLoaded, (unsigned)TypesLoaded.size(), |
| 3025 | ((float)NumTypesLoaded/TypesLoaded.size() * 100)); |
| 3026 | if (!DeclsLoaded.empty()) |
Douglas Gregor | 95c13f5 | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 3027 | std::fprintf(stderr, " %u/%u declarations read (%f%%)\n", |
Douglas Gregor | 745ed14 | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 3028 | NumDeclsLoaded, (unsigned)DeclsLoaded.size(), |
| 3029 | ((float)NumDeclsLoaded/DeclsLoaded.size() * 100)); |
Douglas Gregor | 0e14997 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 3030 | if (!IdentifiersLoaded.empty()) |
Douglas Gregor | 95c13f5 | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 3031 | std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n", |
Douglas Gregor | 0e14997 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 3032 | NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(), |
| 3033 | ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100)); |
Douglas Gregor | 95c13f5 | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 3034 | if (TotalNumSelectors) |
| 3035 | std::fprintf(stderr, " %u/%u selectors read (%f%%)\n", |
| 3036 | NumSelectorsLoaded, TotalNumSelectors, |
| 3037 | ((float)NumSelectorsLoaded/TotalNumSelectors * 100)); |
| 3038 | if (TotalNumStatements) |
| 3039 | std::fprintf(stderr, " %u/%u statements read (%f%%)\n", |
| 3040 | NumStatementsRead, TotalNumStatements, |
| 3041 | ((float)NumStatementsRead/TotalNumStatements * 100)); |
| 3042 | if (TotalNumMacros) |
| 3043 | std::fprintf(stderr, " %u/%u macros read (%f%%)\n", |
| 3044 | NumMacrosRead, TotalNumMacros, |
| 3045 | ((float)NumMacrosRead/TotalNumMacros * 100)); |
| 3046 | if (TotalLexicalDeclContexts) |
| 3047 | std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n", |
| 3048 | NumLexicalDeclContextsRead, TotalLexicalDeclContexts, |
| 3049 | ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts |
| 3050 | * 100)); |
| 3051 | if (TotalVisibleDeclContexts) |
| 3052 | std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n", |
| 3053 | NumVisibleDeclContextsRead, TotalVisibleDeclContexts, |
| 3054 | ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts |
| 3055 | * 100)); |
| 3056 | if (TotalSelectorsInMethodPool) { |
| 3057 | std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n", |
| 3058 | NumMethodPoolSelectorsRead, TotalSelectorsInMethodPool, |
| 3059 | ((float)NumMethodPoolSelectorsRead/TotalSelectorsInMethodPool |
| 3060 | * 100)); |
| 3061 | std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses); |
| 3062 | } |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3063 | std::fprintf(stderr, "\n"); |
| 3064 | } |
| 3065 | |
Douglas Gregor | a868bbd | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 3066 | void PCHReader::InitializeSema(Sema &S) { |
| 3067 | SemaObj = &S; |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 3068 | S.ExternalSource = this; |
| 3069 | |
Douglas Gregor | 7cd60f7 | 2009-04-22 21:15:06 +0000 | [diff] [blame] | 3070 | // Makes sure any declarations that were deserialized "too early" |
| 3071 | // still get added to the identifier's declaration chains. |
| 3072 | for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) { |
| 3073 | SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(PreloadedDecls[I])); |
| 3074 | SemaObj->IdResolver.AddDecl(PreloadedDecls[I]); |
Douglas Gregor | a868bbd | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 3075 | } |
Douglas Gregor | 7cd60f7 | 2009-04-22 21:15:06 +0000 | [diff] [blame] | 3076 | PreloadedDecls.clear(); |
Douglas Gregor | d4df865 | 2009-04-22 22:02:47 +0000 | [diff] [blame] | 3077 | |
| 3078 | // If there were any tentative definitions, deserialize them and add |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 3079 | // them to Sema's list of tentative definitions. |
Douglas Gregor | d4df865 | 2009-04-22 22:02:47 +0000 | [diff] [blame] | 3080 | for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) { |
| 3081 | VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I])); |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 3082 | SemaObj->TentativeDefinitions.push_back(Var); |
Douglas Gregor | d4df865 | 2009-04-22 22:02:47 +0000 | [diff] [blame] | 3083 | } |
Kovarththanan Rajaratnam | 39f2fbd1 | 2010-03-07 19:10:13 +0000 | [diff] [blame] | 3084 | |
Tanya Lattner | 9007380 | 2010-02-12 00:07:30 +0000 | [diff] [blame] | 3085 | // If there were any unused static functions, deserialize them and add to |
| 3086 | // Sema's list of unused static functions. |
| 3087 | for (unsigned I = 0, N = UnusedStaticFuncs.size(); I != N; ++I) { |
| 3088 | FunctionDecl *FD = cast<FunctionDecl>(GetDecl(UnusedStaticFuncs[I])); |
| 3089 | SemaObj->UnusedStaticFuncs.push_back(FD); |
| 3090 | } |
Douglas Gregor | acfc76c | 2009-04-22 22:18:58 +0000 | [diff] [blame] | 3091 | |
| 3092 | // If there were any locally-scoped external declarations, |
| 3093 | // deserialize them and add them to Sema's table of locally-scoped |
| 3094 | // external declarations. |
| 3095 | for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) { |
| 3096 | NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I])); |
| 3097 | SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D; |
| 3098 | } |
Douglas Gregor | 61cac2b | 2009-04-27 20:06:05 +0000 | [diff] [blame] | 3099 | |
| 3100 | // If there were any ext_vector type declarations, deserialize them |
| 3101 | // and add them to Sema's vector of such declarations. |
| 3102 | for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) |
| 3103 | SemaObj->ExtVectorDecls.push_back( |
| 3104 | cast<TypedefDecl>(GetDecl(ExtVectorDecls[I]))); |
Argyrios Kyrtzidis | af2eac2 | 2010-07-06 15:37:04 +0000 | [diff] [blame] | 3105 | |
| 3106 | // FIXME: Do VTable uses and dynamic classes deserialize too much ? |
| 3107 | // Can we cut them down before writing them ? |
| 3108 | |
| 3109 | // If there were any VTable uses, deserialize the information and add it |
| 3110 | // to Sema's vector and map of VTable uses. |
| 3111 | unsigned Idx = 0; |
| 3112 | for (unsigned I = 0, N = VTableUses[Idx++]; I != N; ++I) { |
| 3113 | CXXRecordDecl *Class = cast<CXXRecordDecl>(GetDecl(VTableUses[Idx++])); |
| 3114 | SourceLocation Loc = ReadSourceLocation(VTableUses, Idx); |
| 3115 | bool DefinitionRequired = VTableUses[Idx++]; |
| 3116 | SemaObj->VTableUses.push_back(std::make_pair(Class, Loc)); |
| 3117 | SemaObj->VTablesUsed[Class] = DefinitionRequired; |
| 3118 | } |
| 3119 | |
| 3120 | // If there were any dynamic classes declarations, deserialize them |
| 3121 | // and add them to Sema's vector of such declarations. |
| 3122 | for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) |
| 3123 | SemaObj->DynamicClasses.push_back( |
| 3124 | cast<CXXRecordDecl>(GetDecl(DynamicClasses[I]))); |
Fariborz Jahanian | c51609a | 2010-07-23 19:11:11 +0000 | [diff] [blame] | 3125 | |
| 3126 | // If there are @selector references added them to its pool. This is for |
| 3127 | // implementation of -Wselector. |
| 3128 | PerFileData &F = *Chain[0]; |
| 3129 | if (!F.ReferencedSelectorsData.empty()) { |
| 3130 | unsigned int DataSize = F.ReferencedSelectorsData.size()-1; |
| 3131 | unsigned I = 0; |
| 3132 | while (I < DataSize) { |
| 3133 | Selector Sel = DecodeSelector(F.ReferencedSelectorsData[I++]); |
| 3134 | SourceLocation SelLoc = |
| 3135 | SourceLocation::getFromRawEncoding(F.ReferencedSelectorsData[I++]); |
| 3136 | SemaObj->ReferencedSelectors.insert(std::make_pair(Sel, SelLoc)); |
| 3137 | } |
| 3138 | } |
Douglas Gregor | a868bbd | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 3139 | } |
| 3140 | |
| 3141 | IdentifierInfo* PCHReader::get(const char *NameStart, const char *NameEnd) { |
Sebastian Redl | 4e6c567 | 2010-07-21 22:31:37 +0000 | [diff] [blame] | 3142 | // Try to find this name within our on-disk hash tables. We need to aggregate |
| 3143 | // the info from all of them. |
| 3144 | IdentifierInfo *II = 0; |
| 3145 | for (unsigned I = 0, N = Chain.size(); I != N; ++I) { |
| 3146 | PCHIdentifierLookupTable *IdTable |
| 3147 | = (PCHIdentifierLookupTable *)Chain[N - I - 1]->IdentifierLookupTable; |
Sebastian Redl | 5c415f3 | 2010-07-22 17:01:13 +0000 | [diff] [blame] | 3148 | if (!IdTable) |
| 3149 | continue; |
Sebastian Redl | 4e6c567 | 2010-07-21 22:31:37 +0000 | [diff] [blame] | 3150 | std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart); |
| 3151 | PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key); |
| 3152 | if (Pos == IdTable->end()) |
| 3153 | continue; |
Douglas Gregor | a868bbd | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 3154 | |
Sebastian Redl | 4e6c567 | 2010-07-21 22:31:37 +0000 | [diff] [blame] | 3155 | // Dereferencing the iterator has the effect of building the |
| 3156 | // IdentifierInfo node and populating it with the various |
| 3157 | // declarations it needs. |
| 3158 | II = *Pos; |
| 3159 | } |
| 3160 | return II; |
Douglas Gregor | a868bbd | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 3161 | } |
| 3162 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3163 | std::pair<ObjCMethodList, ObjCMethodList> |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 3164 | PCHReader::ReadMethodPool(Selector Sel) { |
| 3165 | if (!MethodPoolLookupTable) |
| 3166 | return std::pair<ObjCMethodList, ObjCMethodList>(); |
| 3167 | |
| 3168 | // Try to find this selector within our on-disk hash table. |
| 3169 | PCHMethodPoolLookupTable *PoolTable |
| 3170 | = (PCHMethodPoolLookupTable*)MethodPoolLookupTable; |
| 3171 | PCHMethodPoolLookupTable::iterator Pos = PoolTable->find(Sel); |
Douglas Gregor | 95c13f5 | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 3172 | if (Pos == PoolTable->end()) { |
| 3173 | ++NumMethodPoolMisses; |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 3174 | return std::pair<ObjCMethodList, ObjCMethodList>();; |
Douglas Gregor | 95c13f5 | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 3175 | } |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 3176 | |
Douglas Gregor | 95c13f5 | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 3177 | ++NumMethodPoolSelectorsRead; |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 3178 | return *Pos; |
| 3179 | } |
| 3180 | |
Douglas Gregor | 0e14997 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 3181 | void PCHReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) { |
Douglas Gregor | a868bbd | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 3182 | assert(ID && "Non-zero identifier ID required"); |
Douglas Gregor | 6f00bf8 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 3183 | assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range"); |
Douglas Gregor | 0e14997 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 3184 | IdentifiersLoaded[ID - 1] = II; |
Sebastian Redl | ff4a295 | 2010-07-23 23:49:55 +0000 | [diff] [blame] | 3185 | if (DeserializationListener) |
| 3186 | DeserializationListener->IdentifierRead(ID, II); |
Douglas Gregor | a868bbd | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 3187 | } |
| 3188 | |
Douglas Gregor | 1342e84 | 2009-07-06 18:54:52 +0000 | [diff] [blame] | 3189 | /// \brief Set the globally-visible declarations associated with the given |
| 3190 | /// identifier. |
| 3191 | /// |
| 3192 | /// If the PCH reader is currently in a state where the given declaration IDs |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3193 | /// cannot safely be resolved, they are queued until it is safe to resolve |
Douglas Gregor | 1342e84 | 2009-07-06 18:54:52 +0000 | [diff] [blame] | 3194 | /// them. |
| 3195 | /// |
| 3196 | /// \param II an IdentifierInfo that refers to one or more globally-visible |
| 3197 | /// declarations. |
| 3198 | /// |
| 3199 | /// \param DeclIDs the set of declaration IDs with the name @p II that are |
| 3200 | /// visible at global scope. |
| 3201 | /// |
| 3202 | /// \param Nonrecursive should be true to indicate that the caller knows that |
| 3203 | /// this call is non-recursive, and therefore the globally-visible declarations |
| 3204 | /// will not be placed onto the pending queue. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3205 | void |
| 3206 | PCHReader::SetGloballyVisibleDecls(IdentifierInfo *II, |
Douglas Gregor | 1342e84 | 2009-07-06 18:54:52 +0000 | [diff] [blame] | 3207 | const llvm::SmallVectorImpl<uint32_t> &DeclIDs, |
| 3208 | bool Nonrecursive) { |
| 3209 | if (CurrentlyLoadingTypeOrDecl && !Nonrecursive) { |
| 3210 | PendingIdentifierInfos.push_back(PendingIdentifierInfo()); |
| 3211 | PendingIdentifierInfo &PII = PendingIdentifierInfos.back(); |
| 3212 | PII.II = II; |
| 3213 | for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) |
| 3214 | PII.DeclIDs.push_back(DeclIDs[I]); |
| 3215 | return; |
| 3216 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3217 | |
Douglas Gregor | 1342e84 | 2009-07-06 18:54:52 +0000 | [diff] [blame] | 3218 | for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) { |
| 3219 | NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I])); |
| 3220 | if (SemaObj) { |
| 3221 | // Introduce this declaration into the translation-unit scope |
| 3222 | // and add it to the declaration chain for this identifier, so |
| 3223 | // that (unqualified) name lookup will find it. |
| 3224 | SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(D)); |
| 3225 | SemaObj->IdResolver.AddDeclToIdentifierChain(II, D); |
| 3226 | } else { |
| 3227 | // Queue this declaration so that it will be added to the |
| 3228 | // translation unit scope and identifier's declaration chain |
| 3229 | // once a Sema object is known. |
| 3230 | PreloadedDecls.push_back(D); |
| 3231 | } |
| 3232 | } |
| 3233 | } |
| 3234 | |
Chris Lattner | c523d8e | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 3235 | IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) { |
Douglas Gregor | 3ed42cb | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 3236 | if (ID == 0) |
| 3237 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3238 | |
Sebastian Redl | c713b96 | 2010-07-21 00:46:22 +0000 | [diff] [blame] | 3239 | if (IdentifiersLoaded.empty()) { |
Douglas Gregor | 6f00bf8 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 3240 | Error("no identifier table in PCH file"); |
Douglas Gregor | 3ed42cb | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 3241 | return 0; |
| 3242 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3243 | |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 3244 | assert(PP && "Forgot to set Preprocessor ?"); |
Sebastian Redl | c713b96 | 2010-07-21 00:46:22 +0000 | [diff] [blame] | 3245 | ID -= 1; |
| 3246 | if (!IdentifiersLoaded[ID]) { |
| 3247 | unsigned Index = ID; |
| 3248 | const char *Str = 0; |
| 3249 | for (unsigned I = 0, N = Chain.size(); I != N; ++I) { |
| 3250 | PerFileData *F = Chain[N - I - 1]; |
| 3251 | if (Index < F->LocalNumIdentifiers) { |
| 3252 | uint32_t Offset = F->IdentifierOffsets[Index]; |
| 3253 | Str = F->IdentifierTableData + Offset; |
| 3254 | break; |
| 3255 | } |
| 3256 | Index -= F->LocalNumIdentifiers; |
| 3257 | } |
| 3258 | assert(Str && "Broken Chain"); |
Douglas Gregor | 5287b4e | 2009-04-25 21:04:17 +0000 | [diff] [blame] | 3259 | |
Douglas Gregor | ab4df58 | 2009-04-28 20:01:51 +0000 | [diff] [blame] | 3260 | // All of the strings in the PCH file are preceded by a 16-bit |
| 3261 | // length. Extract that 16-bit length to avoid having to execute |
| 3262 | // strlen(). |
Ted Kremenek | ca42a51 | 2009-10-23 04:45:31 +0000 | [diff] [blame] | 3263 | // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as |
| 3264 | // unsigned integers. This is important to avoid integer overflow when |
| 3265 | // we cast them to 'unsigned'. |
Ted Kremenek | 49c5232 | 2009-10-23 03:57:22 +0000 | [diff] [blame] | 3266 | const unsigned char *StrLenPtr = (const unsigned char*) Str - 2; |
Douglas Gregor | ab4df58 | 2009-04-28 20:01:51 +0000 | [diff] [blame] | 3267 | unsigned StrLen = (((unsigned) StrLenPtr[0]) |
| 3268 | | (((unsigned) StrLenPtr[1]) << 8)) - 1; |
Sebastian Redl | c713b96 | 2010-07-21 00:46:22 +0000 | [diff] [blame] | 3269 | IdentifiersLoaded[ID] |
Kovarththanan Rajaratnam | a3b0959 | 2010-03-12 10:32:27 +0000 | [diff] [blame] | 3270 | = &PP->getIdentifierTable().get(Str, StrLen); |
Sebastian Redl | ff4a295 | 2010-07-23 23:49:55 +0000 | [diff] [blame] | 3271 | if (DeserializationListener) |
| 3272 | DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]); |
Douglas Gregor | 3ed42cb | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 3273 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3274 | |
Sebastian Redl | c713b96 | 2010-07-21 00:46:22 +0000 | [diff] [blame] | 3275 | return IdentifiersLoaded[ID]; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3276 | } |
| 3277 | |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 3278 | void PCHReader::ReadSLocEntry(unsigned ID) { |
| 3279 | ReadSLocEntryRecord(ID); |
| 3280 | } |
| 3281 | |
Steve Naroff | 2ddea05 | 2009-04-23 10:39:46 +0000 | [diff] [blame] | 3282 | Selector PCHReader::DecodeSelector(unsigned ID) { |
| 3283 | if (ID == 0) |
| 3284 | return Selector(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3285 | |
Douglas Gregor | 6f00bf8 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 3286 | if (!MethodPoolLookupTableData) |
Steve Naroff | 2ddea05 | 2009-04-23 10:39:46 +0000 | [diff] [blame] | 3287 | return Selector(); |
Douglas Gregor | 95c13f5 | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 3288 | |
| 3289 | if (ID > TotalNumSelectors) { |
Douglas Gregor | 6f00bf8 | 2009-04-28 21:53:25 +0000 | [diff] [blame] | 3290 | Error("selector ID out of range in PCH file"); |
Steve Naroff | 2ddea05 | 2009-04-23 10:39:46 +0000 | [diff] [blame] | 3291 | return Selector(); |
| 3292 | } |
Douglas Gregor | 95c13f5 | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 3293 | |
| 3294 | unsigned Index = ID - 1; |
| 3295 | if (SelectorsLoaded[Index].getAsOpaquePtr() == 0) { |
| 3296 | // Load this selector from the selector table. |
| 3297 | // FIXME: endianness portability issues with SelectorOffsets table |
| 3298 | PCHMethodPoolLookupTrait Trait(*this); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3299 | SelectorsLoaded[Index] |
Douglas Gregor | 95c13f5 | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 3300 | = Trait.ReadKey(MethodPoolLookupTableData + SelectorOffsets[Index], 0); |
| 3301 | } |
| 3302 | |
| 3303 | return SelectorsLoaded[Index]; |
Steve Naroff | 2ddea05 | 2009-04-23 10:39:46 +0000 | [diff] [blame] | 3304 | } |
| 3305 | |
John McCall | 75b960e | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 3306 | Selector PCHReader::GetExternalSelector(uint32_t ID) { |
Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 3307 | return DecodeSelector(ID); |
| 3308 | } |
| 3309 | |
John McCall | 75b960e | 2010-06-01 09:23:16 +0000 | [diff] [blame] | 3310 | uint32_t PCHReader::GetNumExternalSelectors() { |
Douglas Gregor | d720daf | 2010-04-06 17:30:22 +0000 | [diff] [blame] | 3311 | return TotalNumSelectors + 1; |
| 3312 | } |
| 3313 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3314 | DeclarationName |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3315 | PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) { |
| 3316 | DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++]; |
| 3317 | switch (Kind) { |
| 3318 | case DeclarationName::Identifier: |
| 3319 | return DeclarationName(GetIdentifierInfo(Record, Idx)); |
| 3320 | |
| 3321 | case DeclarationName::ObjCZeroArgSelector: |
| 3322 | case DeclarationName::ObjCOneArgSelector: |
| 3323 | case DeclarationName::ObjCMultiArgSelector: |
Steve Naroff | 3c301dc | 2009-04-23 15:15:40 +0000 | [diff] [blame] | 3324 | return DeclarationName(GetSelector(Record, Idx)); |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3325 | |
| 3326 | case DeclarationName::CXXConstructorName: |
Chris Lattner | 8575daa | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 3327 | return Context->DeclarationNames.getCXXConstructorName( |
Douglas Gregor | 2211d34 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 3328 | Context->getCanonicalType(GetType(Record[Idx++]))); |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3329 | |
| 3330 | case DeclarationName::CXXDestructorName: |
Chris Lattner | 8575daa | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 3331 | return Context->DeclarationNames.getCXXDestructorName( |
Douglas Gregor | 2211d34 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 3332 | Context->getCanonicalType(GetType(Record[Idx++]))); |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3333 | |
| 3334 | case DeclarationName::CXXConversionFunctionName: |
Chris Lattner | 8575daa | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 3335 | return Context->DeclarationNames.getCXXConversionFunctionName( |
Douglas Gregor | 2211d34 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 3336 | Context->getCanonicalType(GetType(Record[Idx++]))); |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3337 | |
| 3338 | case DeclarationName::CXXOperatorName: |
Chris Lattner | 8575daa | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 3339 | return Context->DeclarationNames.getCXXOperatorName( |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3340 | (OverloadedOperatorKind)Record[Idx++]); |
| 3341 | |
Alexis Hunt | 3d221f2 | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 3342 | case DeclarationName::CXXLiteralOperatorName: |
| 3343 | return Context->DeclarationNames.getCXXLiteralOperatorName( |
| 3344 | GetIdentifierInfo(Record, Idx)); |
| 3345 | |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3346 | case DeclarationName::CXXUsingDirective: |
| 3347 | return DeclarationName::getUsingDirectiveName(); |
| 3348 | } |
| 3349 | |
| 3350 | // Required to silence GCC warning |
| 3351 | return DeclarationName(); |
| 3352 | } |
Douglas Gregor | 55abb23 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 3353 | |
Argyrios Kyrtzidis | 95c04ca | 2010-06-19 19:29:09 +0000 | [diff] [blame] | 3354 | TemplateName |
| 3355 | PCHReader::ReadTemplateName(const RecordData &Record, unsigned &Idx) { |
| 3356 | TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++]; |
| 3357 | switch (Kind) { |
| 3358 | case TemplateName::Template: |
| 3359 | return TemplateName(cast_or_null<TemplateDecl>(GetDecl(Record[Idx++]))); |
| 3360 | |
| 3361 | case TemplateName::OverloadedTemplate: { |
| 3362 | unsigned size = Record[Idx++]; |
| 3363 | UnresolvedSet<8> Decls; |
| 3364 | while (size--) |
| 3365 | Decls.addDecl(cast<NamedDecl>(GetDecl(Record[Idx++]))); |
| 3366 | |
| 3367 | return Context->getOverloadedTemplateName(Decls.begin(), Decls.end()); |
| 3368 | } |
| 3369 | |
| 3370 | case TemplateName::QualifiedTemplate: { |
| 3371 | NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx); |
| 3372 | bool hasTemplKeyword = Record[Idx++]; |
| 3373 | TemplateDecl *Template = cast<TemplateDecl>(GetDecl(Record[Idx++])); |
| 3374 | return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template); |
| 3375 | } |
| 3376 | |
| 3377 | case TemplateName::DependentTemplate: { |
| 3378 | NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx); |
| 3379 | if (Record[Idx++]) // isIdentifier |
| 3380 | return Context->getDependentTemplateName(NNS, |
| 3381 | GetIdentifierInfo(Record, Idx)); |
| 3382 | return Context->getDependentTemplateName(NNS, |
Argyrios Kyrtzidis | ddf5f21 | 2010-06-28 09:31:42 +0000 | [diff] [blame] | 3383 | (OverloadedOperatorKind)Record[Idx++]); |
Argyrios Kyrtzidis | 95c04ca | 2010-06-19 19:29:09 +0000 | [diff] [blame] | 3384 | } |
| 3385 | } |
| 3386 | |
| 3387 | assert(0 && "Unhandled template name kind!"); |
| 3388 | return TemplateName(); |
| 3389 | } |
| 3390 | |
| 3391 | TemplateArgument |
Sebastian Redl | c67764e | 2010-07-22 22:43:28 +0000 | [diff] [blame] | 3392 | PCHReader::ReadTemplateArgument(llvm::BitstreamCursor &DeclsCursor, |
| 3393 | const RecordData &Record, unsigned &Idx) { |
Argyrios Kyrtzidis | 95c04ca | 2010-06-19 19:29:09 +0000 | [diff] [blame] | 3394 | switch ((TemplateArgument::ArgKind)Record[Idx++]) { |
| 3395 | case TemplateArgument::Null: |
| 3396 | return TemplateArgument(); |
| 3397 | case TemplateArgument::Type: |
| 3398 | return TemplateArgument(GetType(Record[Idx++])); |
| 3399 | case TemplateArgument::Declaration: |
| 3400 | return TemplateArgument(GetDecl(Record[Idx++])); |
Argyrios Kyrtzidis | 0b0369a | 2010-06-28 09:31:34 +0000 | [diff] [blame] | 3401 | case TemplateArgument::Integral: { |
| 3402 | llvm::APSInt Value = ReadAPSInt(Record, Idx); |
| 3403 | QualType T = GetType(Record[Idx++]); |
| 3404 | return TemplateArgument(Value, T); |
| 3405 | } |
Argyrios Kyrtzidis | 95c04ca | 2010-06-19 19:29:09 +0000 | [diff] [blame] | 3406 | case TemplateArgument::Template: |
| 3407 | return TemplateArgument(ReadTemplateName(Record, Idx)); |
| 3408 | case TemplateArgument::Expression: |
Sebastian Redl | c67764e | 2010-07-22 22:43:28 +0000 | [diff] [blame] | 3409 | return TemplateArgument(ReadExpr(DeclsCursor)); |
Argyrios Kyrtzidis | 95c04ca | 2010-06-19 19:29:09 +0000 | [diff] [blame] | 3410 | case TemplateArgument::Pack: { |
| 3411 | unsigned NumArgs = Record[Idx++]; |
| 3412 | llvm::SmallVector<TemplateArgument, 8> Args; |
| 3413 | Args.reserve(NumArgs); |
| 3414 | while (NumArgs--) |
Sebastian Redl | c67764e | 2010-07-22 22:43:28 +0000 | [diff] [blame] | 3415 | Args.push_back(ReadTemplateArgument(DeclsCursor, Record, Idx)); |
Argyrios Kyrtzidis | 95c04ca | 2010-06-19 19:29:09 +0000 | [diff] [blame] | 3416 | TemplateArgument TemplArg; |
| 3417 | TemplArg.setArgumentPack(Args.data(), Args.size(), /*CopyArgs=*/true); |
| 3418 | return TemplArg; |
| 3419 | } |
| 3420 | } |
| 3421 | |
| 3422 | assert(0 && "Unhandled template argument kind!"); |
| 3423 | return TemplateArgument(); |
| 3424 | } |
| 3425 | |
Argyrios Kyrtzidis | 818c5db | 2010-06-23 13:48:30 +0000 | [diff] [blame] | 3426 | TemplateParameterList * |
| 3427 | PCHReader::ReadTemplateParameterList(const RecordData &Record, unsigned &Idx) { |
| 3428 | SourceLocation TemplateLoc = ReadSourceLocation(Record, Idx); |
| 3429 | SourceLocation LAngleLoc = ReadSourceLocation(Record, Idx); |
| 3430 | SourceLocation RAngleLoc = ReadSourceLocation(Record, Idx); |
| 3431 | |
| 3432 | unsigned NumParams = Record[Idx++]; |
| 3433 | llvm::SmallVector<NamedDecl *, 16> Params; |
| 3434 | Params.reserve(NumParams); |
| 3435 | while (NumParams--) |
| 3436 | Params.push_back(cast<NamedDecl>(GetDecl(Record[Idx++]))); |
| 3437 | |
| 3438 | TemplateParameterList* TemplateParams = |
| 3439 | TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc, |
| 3440 | Params.data(), Params.size(), RAngleLoc); |
| 3441 | return TemplateParams; |
| 3442 | } |
| 3443 | |
| 3444 | void |
| 3445 | PCHReader:: |
| 3446 | ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs, |
Sebastian Redl | c67764e | 2010-07-22 22:43:28 +0000 | [diff] [blame] | 3447 | llvm::BitstreamCursor &DeclsCursor, |
Argyrios Kyrtzidis | 818c5db | 2010-06-23 13:48:30 +0000 | [diff] [blame] | 3448 | const RecordData &Record, unsigned &Idx) { |
| 3449 | unsigned NumTemplateArgs = Record[Idx++]; |
| 3450 | TemplArgs.reserve(NumTemplateArgs); |
| 3451 | while (NumTemplateArgs--) |
Sebastian Redl | c67764e | 2010-07-22 22:43:28 +0000 | [diff] [blame] | 3452 | TemplArgs.push_back(ReadTemplateArgument(DeclsCursor, Record, Idx)); |
Argyrios Kyrtzidis | 818c5db | 2010-06-23 13:48:30 +0000 | [diff] [blame] | 3453 | } |
| 3454 | |
Argyrios Kyrtzidis | 2c2167a | 2010-07-02 11:55:32 +0000 | [diff] [blame] | 3455 | /// \brief Read a UnresolvedSet structure. |
| 3456 | void PCHReader::ReadUnresolvedSet(UnresolvedSetImpl &Set, |
| 3457 | const RecordData &Record, unsigned &Idx) { |
| 3458 | unsigned NumDecls = Record[Idx++]; |
| 3459 | while (NumDecls--) { |
| 3460 | NamedDecl *D = cast<NamedDecl>(GetDecl(Record[Idx++])); |
| 3461 | AccessSpecifier AS = (AccessSpecifier)Record[Idx++]; |
| 3462 | Set.addDecl(D, AS); |
| 3463 | } |
| 3464 | } |
| 3465 | |
Argyrios Kyrtzidis | 3701fcd | 2010-07-02 23:30:27 +0000 | [diff] [blame] | 3466 | CXXBaseSpecifier |
Nick Lewycky | 19b9f95 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 3467 | PCHReader::ReadCXXBaseSpecifier(llvm::BitstreamCursor &DeclsCursor, |
| 3468 | const RecordData &Record, unsigned &Idx) { |
Argyrios Kyrtzidis | 3701fcd | 2010-07-02 23:30:27 +0000 | [diff] [blame] | 3469 | bool isVirtual = static_cast<bool>(Record[Idx++]); |
| 3470 | bool isBaseOfClass = static_cast<bool>(Record[Idx++]); |
| 3471 | AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]); |
Nick Lewycky | 19b9f95 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 3472 | TypeSourceInfo *TInfo = GetTypeSourceInfo(DeclsCursor, Record, Idx); |
Argyrios Kyrtzidis | 3701fcd | 2010-07-02 23:30:27 +0000 | [diff] [blame] | 3473 | SourceRange Range = ReadSourceRange(Record, Idx); |
Nick Lewycky | 19b9f95 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 3474 | return CXXBaseSpecifier(Range, isVirtual, isBaseOfClass, AS, TInfo); |
Argyrios Kyrtzidis | 3701fcd | 2010-07-02 23:30:27 +0000 | [diff] [blame] | 3475 | } |
| 3476 | |
Chris Lattner | ca025db | 2010-05-07 21:43:38 +0000 | [diff] [blame] | 3477 | NestedNameSpecifier * |
| 3478 | PCHReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) { |
| 3479 | unsigned N = Record[Idx++]; |
| 3480 | NestedNameSpecifier *NNS = 0, *Prev = 0; |
| 3481 | for (unsigned I = 0; I != N; ++I) { |
| 3482 | NestedNameSpecifier::SpecifierKind Kind |
| 3483 | = (NestedNameSpecifier::SpecifierKind)Record[Idx++]; |
| 3484 | switch (Kind) { |
| 3485 | case NestedNameSpecifier::Identifier: { |
| 3486 | IdentifierInfo *II = GetIdentifierInfo(Record, Idx); |
| 3487 | NNS = NestedNameSpecifier::Create(*Context, Prev, II); |
| 3488 | break; |
| 3489 | } |
| 3490 | |
| 3491 | case NestedNameSpecifier::Namespace: { |
| 3492 | NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++])); |
| 3493 | NNS = NestedNameSpecifier::Create(*Context, Prev, NS); |
| 3494 | break; |
| 3495 | } |
| 3496 | |
| 3497 | case NestedNameSpecifier::TypeSpec: |
| 3498 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 3499 | Type *T = GetType(Record[Idx++]).getTypePtr(); |
| 3500 | bool Template = Record[Idx++]; |
| 3501 | NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T); |
| 3502 | break; |
| 3503 | } |
| 3504 | |
| 3505 | case NestedNameSpecifier::Global: { |
| 3506 | NNS = NestedNameSpecifier::GlobalSpecifier(*Context); |
| 3507 | // No associated value, and there can't be a prefix. |
| 3508 | break; |
| 3509 | } |
Chris Lattner | ca025db | 2010-05-07 21:43:38 +0000 | [diff] [blame] | 3510 | } |
Argyrios Kyrtzidis | ad65c69 | 2010-07-07 15:46:30 +0000 | [diff] [blame] | 3511 | Prev = NNS; |
Chris Lattner | ca025db | 2010-05-07 21:43:38 +0000 | [diff] [blame] | 3512 | } |
| 3513 | return NNS; |
| 3514 | } |
| 3515 | |
| 3516 | SourceRange |
| 3517 | PCHReader::ReadSourceRange(const RecordData &Record, unsigned &Idx) { |
Daniel Dunbar | 6d3bc08 | 2010-06-02 15:47:10 +0000 | [diff] [blame] | 3518 | SourceLocation beg = SourceLocation::getFromRawEncoding(Record[Idx++]); |
| 3519 | SourceLocation end = SourceLocation::getFromRawEncoding(Record[Idx++]); |
| 3520 | return SourceRange(beg, end); |
Chris Lattner | ca025db | 2010-05-07 21:43:38 +0000 | [diff] [blame] | 3521 | } |
| 3522 | |
Douglas Gregor | 1daeb69 | 2009-04-13 18:14:40 +0000 | [diff] [blame] | 3523 | /// \brief Read an integral value |
| 3524 | llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) { |
| 3525 | unsigned BitWidth = Record[Idx++]; |
| 3526 | unsigned NumWords = llvm::APInt::getNumWords(BitWidth); |
| 3527 | llvm::APInt Result(BitWidth, NumWords, &Record[Idx]); |
| 3528 | Idx += NumWords; |
| 3529 | return Result; |
| 3530 | } |
| 3531 | |
| 3532 | /// \brief Read a signed integral value |
| 3533 | llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) { |
| 3534 | bool isUnsigned = Record[Idx++]; |
| 3535 | return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned); |
| 3536 | } |
| 3537 | |
Douglas Gregor | e0a3a51 | 2009-04-14 21:55:33 +0000 | [diff] [blame] | 3538 | /// \brief Read a floating-point value |
| 3539 | llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) { |
Douglas Gregor | e0a3a51 | 2009-04-14 21:55:33 +0000 | [diff] [blame] | 3540 | return llvm::APFloat(ReadAPInt(Record, Idx)); |
| 3541 | } |
| 3542 | |
Douglas Gregor | bc8a78d5 | 2009-04-15 21:30:51 +0000 | [diff] [blame] | 3543 | // \brief Read a string |
| 3544 | std::string PCHReader::ReadString(const RecordData &Record, unsigned &Idx) { |
| 3545 | unsigned Len = Record[Idx++]; |
Jay Foad | 7d0479f | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 3546 | std::string Result(Record.data() + Idx, Record.data() + Idx + Len); |
Douglas Gregor | bc8a78d5 | 2009-04-15 21:30:51 +0000 | [diff] [blame] | 3547 | Idx += Len; |
| 3548 | return Result; |
| 3549 | } |
| 3550 | |
Chris Lattner | cba8614 | 2010-05-10 00:25:06 +0000 | [diff] [blame] | 3551 | CXXTemporary *PCHReader::ReadCXXTemporary(const RecordData &Record, |
| 3552 | unsigned &Idx) { |
| 3553 | CXXDestructorDecl *Decl = cast<CXXDestructorDecl>(GetDecl(Record[Idx++])); |
| 3554 | return CXXTemporary::Create(*Context, Decl); |
| 3555 | } |
| 3556 | |
Douglas Gregor | 55abb23 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 3557 | DiagnosticBuilder PCHReader::Diag(unsigned DiagID) { |
Douglas Gregor | 92863e4 | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 3558 | return Diag(SourceLocation(), DiagID); |
| 3559 | } |
| 3560 | |
| 3561 | DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) { |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 3562 | return Diags.Report(FullSourceLoc(Loc, SourceMgr), DiagID); |
Douglas Gregor | 55abb23 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 3563 | } |
Douglas Gregor | a9af1d1 | 2009-04-17 00:04:06 +0000 | [diff] [blame] | 3564 | |
Douglas Gregor | a868bbd | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 3565 | /// \brief Retrieve the identifier table associated with the |
| 3566 | /// preprocessor. |
| 3567 | IdentifierTable &PCHReader::getIdentifierTable() { |
Argyrios Kyrtzidis | 366985d | 2009-06-19 00:03:23 +0000 | [diff] [blame] | 3568 | assert(PP && "Forgot to set Preprocessor ?"); |
| 3569 | return PP->getIdentifierTable(); |
Douglas Gregor | a868bbd | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 3570 | } |
| 3571 | |
Douglas Gregor | a9af1d1 | 2009-04-17 00:04:06 +0000 | [diff] [blame] | 3572 | /// \brief Record that the given ID maps to the given switch-case |
| 3573 | /// statement. |
| 3574 | void PCHReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) { |
| 3575 | assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID"); |
| 3576 | SwitchCaseStmts[ID] = SC; |
| 3577 | } |
| 3578 | |
| 3579 | /// \brief Retrieve the switch-case statement with the given ID. |
| 3580 | SwitchCase *PCHReader::getSwitchCaseWithID(unsigned ID) { |
| 3581 | assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID"); |
| 3582 | return SwitchCaseStmts[ID]; |
| 3583 | } |
Douglas Gregor | 6cc68a4 | 2009-04-17 18:18:49 +0000 | [diff] [blame] | 3584 | |
| 3585 | /// \brief Record that the given label statement has been |
| 3586 | /// deserialized and has the given ID. |
| 3587 | void PCHReader::RecordLabelStmt(LabelStmt *S, unsigned ID) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3588 | assert(LabelStmts.find(ID) == LabelStmts.end() && |
Douglas Gregor | 6cc68a4 | 2009-04-17 18:18:49 +0000 | [diff] [blame] | 3589 | "Deserialized label twice"); |
| 3590 | LabelStmts[ID] = S; |
| 3591 | |
| 3592 | // If we've already seen any goto statements that point to this |
| 3593 | // label, resolve them now. |
| 3594 | typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter; |
| 3595 | std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID); |
| 3596 | for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto) |
| 3597 | Goto->second->setLabel(S); |
| 3598 | UnresolvedGotoStmts.erase(Gotos.first, Gotos.second); |
Douglas Gregor | 779d865 | 2009-04-17 18:58:21 +0000 | [diff] [blame] | 3599 | |
| 3600 | // If we've already seen any address-label statements that point to |
| 3601 | // this label, resolve them now. |
| 3602 | typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3603 | std::pair<AddrLabelIter, AddrLabelIter> AddrLabels |
Douglas Gregor | 779d865 | 2009-04-17 18:58:21 +0000 | [diff] [blame] | 3604 | = UnresolvedAddrLabelExprs.equal_range(ID); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3605 | for (AddrLabelIter AddrLabel = AddrLabels.first; |
Douglas Gregor | 779d865 | 2009-04-17 18:58:21 +0000 | [diff] [blame] | 3606 | AddrLabel != AddrLabels.second; ++AddrLabel) |
| 3607 | AddrLabel->second->setLabel(S); |
| 3608 | UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second); |
Douglas Gregor | 6cc68a4 | 2009-04-17 18:18:49 +0000 | [diff] [blame] | 3609 | } |
| 3610 | |
| 3611 | /// \brief Set the label of the given statement to the label |
| 3612 | /// identified by ID. |
| 3613 | /// |
| 3614 | /// Depending on the order in which the label and other statements |
| 3615 | /// referencing that label occur, this operation may complete |
| 3616 | /// immediately (updating the statement) or it may queue the |
| 3617 | /// statement to be back-patched later. |
| 3618 | void PCHReader::SetLabelOf(GotoStmt *S, unsigned ID) { |
| 3619 | std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID); |
| 3620 | if (Label != LabelStmts.end()) { |
| 3621 | // We've already seen this label, so set the label of the goto and |
| 3622 | // we're done. |
| 3623 | S->setLabel(Label->second); |
| 3624 | } else { |
| 3625 | // We haven't seen this label yet, so add this goto to the set of |
| 3626 | // unresolved goto statements. |
| 3627 | UnresolvedGotoStmts.insert(std::make_pair(ID, S)); |
| 3628 | } |
| 3629 | } |
Douglas Gregor | 779d865 | 2009-04-17 18:58:21 +0000 | [diff] [blame] | 3630 | |
| 3631 | /// \brief Set the label of the given expression to the label |
| 3632 | /// identified by ID. |
| 3633 | /// |
| 3634 | /// Depending on the order in which the label and other statements |
| 3635 | /// referencing that label occur, this operation may complete |
| 3636 | /// immediately (updating the statement) or it may queue the |
| 3637 | /// statement to be back-patched later. |
| 3638 | void PCHReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) { |
| 3639 | std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID); |
| 3640 | if (Label != LabelStmts.end()) { |
| 3641 | // We've already seen this label, so set the label of the |
| 3642 | // label-address expression and we're done. |
| 3643 | S->setLabel(Label->second); |
| 3644 | } else { |
| 3645 | // We haven't seen this label yet, so add this label-address |
| 3646 | // expression to the set of unresolved label-address expressions. |
| 3647 | UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S)); |
| 3648 | } |
| 3649 | } |
Douglas Gregor | 1342e84 | 2009-07-06 18:54:52 +0000 | [diff] [blame] | 3650 | |
| 3651 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3652 | PCHReader::LoadingTypeOrDecl::LoadingTypeOrDecl(PCHReader &Reader) |
Douglas Gregor | 1342e84 | 2009-07-06 18:54:52 +0000 | [diff] [blame] | 3653 | : Reader(Reader), Parent(Reader.CurrentlyLoadingTypeOrDecl) { |
| 3654 | Reader.CurrentlyLoadingTypeOrDecl = this; |
| 3655 | } |
| 3656 | |
| 3657 | PCHReader::LoadingTypeOrDecl::~LoadingTypeOrDecl() { |
| 3658 | if (!Parent) { |
| 3659 | // If any identifiers with corresponding top-level declarations have |
| 3660 | // been loaded, load those declarations now. |
| 3661 | while (!Reader.PendingIdentifierInfos.empty()) { |
| 3662 | Reader.SetGloballyVisibleDecls(Reader.PendingIdentifierInfos.front().II, |
| 3663 | Reader.PendingIdentifierInfos.front().DeclIDs, |
| 3664 | true); |
| 3665 | Reader.PendingIdentifierInfos.pop_front(); |
| 3666 | } |
Argyrios Kyrtzidis | 903ccd6 | 2010-07-07 15:46:26 +0000 | [diff] [blame] | 3667 | |
| 3668 | // We are not in recursive loading, so it's safe to pass the "interesting" |
| 3669 | // decls to the consumer. |
| 3670 | if (Reader.Consumer) |
| 3671 | Reader.PassInterestingDeclsToConsumer(); |
Douglas Gregor | 1342e84 | 2009-07-06 18:54:52 +0000 | [diff] [blame] | 3672 | } |
| 3673 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3674 | Reader.CurrentlyLoadingTypeOrDecl = Parent; |
Douglas Gregor | 1342e84 | 2009-07-06 18:54:52 +0000 | [diff] [blame] | 3675 | } |