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