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