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