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