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