Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 Google LLC. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "src/sksl/SkSLRehydrator.h" |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 9 | |
| 10 | #include <memory> |
John Stiles | b8e010c | 2020-08-11 18:05:39 -0400 | [diff] [blame] | 11 | #include <unordered_set> |
| 12 | |
Ethan Nicholas | daed259 | 2021-03-04 14:30:25 -0500 | [diff] [blame] | 13 | #include "include/private/SkSLModifiers.h" |
Ethan Nicholas | 24c1772 | 2021-03-09 13:10:59 -0500 | [diff] [blame] | 14 | #include "include/private/SkSLProgramElement.h" |
| 15 | #include "include/private/SkSLStatement.h" |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 16 | #include "src/sksl/ir/SkSLBinaryExpression.h" |
| 17 | #include "src/sksl/ir/SkSLBreakStatement.h" |
John Stiles | e118278 | 2021-03-30 22:09:37 -0400 | [diff] [blame] | 18 | #include "src/sksl/ir/SkSLConstructor.h" |
John Stiles | 4118f14 | 2021-04-01 16:42:35 -0400 | [diff] [blame] | 19 | #include "src/sksl/ir/SkSLConstructorArray.h" |
John Stiles | 8cad637 | 2021-04-07 12:31:13 -0400 | [diff] [blame] | 20 | #include "src/sksl/ir/SkSLConstructorCompound.h" |
| 21 | #include "src/sksl/ir/SkSLConstructorCompoundCast.h" |
John Stiles | e118278 | 2021-03-30 22:09:37 -0400 | [diff] [blame] | 22 | #include "src/sksl/ir/SkSLConstructorDiagonalMatrix.h" |
John Stiles | 5abb9e1 | 2021-04-06 13:47:19 -0400 | [diff] [blame] | 23 | #include "src/sksl/ir/SkSLConstructorMatrixResize.h" |
John Stiles | fd7252f | 2021-04-04 22:24:40 -0400 | [diff] [blame] | 24 | #include "src/sksl/ir/SkSLConstructorScalarCast.h" |
John Stiles | 2938eea | 2021-04-01 18:58:25 -0400 | [diff] [blame] | 25 | #include "src/sksl/ir/SkSLConstructorSplat.h" |
John Stiles | d47330f | 2021-04-08 23:25:52 -0400 | [diff] [blame] | 26 | #include "src/sksl/ir/SkSLConstructorStruct.h" |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 27 | #include "src/sksl/ir/SkSLContinueStatement.h" |
| 28 | #include "src/sksl/ir/SkSLDiscardStatement.h" |
| 29 | #include "src/sksl/ir/SkSLDoStatement.h" |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 30 | #include "src/sksl/ir/SkSLExpression.h" |
| 31 | #include "src/sksl/ir/SkSLExpressionStatement.h" |
| 32 | #include "src/sksl/ir/SkSLField.h" |
| 33 | #include "src/sksl/ir/SkSLFieldAccess.h" |
| 34 | #include "src/sksl/ir/SkSLFloatLiteral.h" |
| 35 | #include "src/sksl/ir/SkSLForStatement.h" |
| 36 | #include "src/sksl/ir/SkSLFunctionCall.h" |
| 37 | #include "src/sksl/ir/SkSLFunctionDeclaration.h" |
| 38 | #include "src/sksl/ir/SkSLFunctionDefinition.h" |
| 39 | #include "src/sksl/ir/SkSLIfStatement.h" |
| 40 | #include "src/sksl/ir/SkSLIndexExpression.h" |
John Stiles | 98c1f82 | 2020-09-09 14:18:53 -0400 | [diff] [blame] | 41 | #include "src/sksl/ir/SkSLInlineMarker.h" |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 42 | #include "src/sksl/ir/SkSLIntLiteral.h" |
| 43 | #include "src/sksl/ir/SkSLInterfaceBlock.h" |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 44 | #include "src/sksl/ir/SkSLPostfixExpression.h" |
| 45 | #include "src/sksl/ir/SkSLPrefixExpression.h" |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 46 | #include "src/sksl/ir/SkSLReturnStatement.h" |
| 47 | #include "src/sksl/ir/SkSLSetting.h" |
John Stiles | dc75a97 | 2020-11-25 16:24:55 -0500 | [diff] [blame] | 48 | #include "src/sksl/ir/SkSLStructDefinition.h" |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 49 | #include "src/sksl/ir/SkSLSwitchCase.h" |
| 50 | #include "src/sksl/ir/SkSLSwitchStatement.h" |
| 51 | #include "src/sksl/ir/SkSLSwizzle.h" |
John Stiles | 49a547f | 2020-10-06 16:14:37 -0400 | [diff] [blame] | 52 | #include "src/sksl/ir/SkSLSymbolAlias.h" |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 53 | #include "src/sksl/ir/SkSLSymbolTable.h" |
| 54 | #include "src/sksl/ir/SkSLTernaryExpression.h" |
| 55 | #include "src/sksl/ir/SkSLType.h" |
| 56 | #include "src/sksl/ir/SkSLUnresolvedFunction.h" |
| 57 | #include "src/sksl/ir/SkSLVarDeclarations.h" |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 58 | #include "src/sksl/ir/SkSLVariable.h" |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 59 | |
| 60 | namespace SkSL { |
| 61 | |
| 62 | class AutoRehydratorSymbolTable { |
| 63 | public: |
| 64 | AutoRehydratorSymbolTable(Rehydrator* rehydrator) |
| 65 | : fRehydrator(rehydrator) |
| 66 | , fOldSymbols(fRehydrator->fSymbolTable) { |
| 67 | fRehydrator->fSymbolTable = fRehydrator->symbolTable(); |
| 68 | } |
| 69 | |
| 70 | ~AutoRehydratorSymbolTable() { |
| 71 | fRehydrator->fSymbolTable = std::move(fOldSymbols); |
| 72 | } |
| 73 | |
| 74 | private: |
| 75 | Rehydrator* fRehydrator; |
| 76 | std::shared_ptr<SymbolTable> fOldSymbols; |
| 77 | }; |
| 78 | |
John Stiles | 10d39d9 | 2021-05-04 16:13:14 -0400 | [diff] [blame] | 79 | Rehydrator::Rehydrator(const Context* context, std::shared_ptr<SymbolTable> symbolTable, |
John Stiles | 7c3515b | 2020-10-16 18:38:39 -0400 | [diff] [blame] | 80 | const uint8_t* src, size_t length) |
| 81 | : fContext(*context) |
John Stiles | 7c3515b | 2020-10-16 18:38:39 -0400 | [diff] [blame] | 82 | , fSymbolTable(std::move(symbolTable)) |
| 83 | , fStart(src) |
| 84 | SkDEBUGCODE(, fEnd(fStart + length)) { |
| 85 | SkASSERT(fSymbolTable); |
| 86 | SkASSERT(fSymbolTable->isBuiltin()); |
| 87 | // skip past string data |
| 88 | fIP = fStart; |
| 89 | fIP += this->readU16(); |
| 90 | } |
| 91 | |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 92 | Layout Rehydrator::layout() { |
| 93 | switch (this->readU8()) { |
| 94 | case kBuiltinLayout_Command: { |
| 95 | Layout result; |
| 96 | result.fBuiltin = this->readS16(); |
| 97 | return result; |
| 98 | } |
| 99 | case kDefaultLayout_Command: |
| 100 | return Layout(); |
| 101 | case kLayout_Command: { |
| 102 | int flags = this->readU32(); |
| 103 | int location = this->readS8(); |
| 104 | int offset = this->readS8(); |
| 105 | int binding = this->readS8(); |
| 106 | int index = this->readS8(); |
| 107 | int set = this->readS8(); |
| 108 | int builtin = this->readS16(); |
| 109 | int inputAttachmentIndex = this->readS8(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 110 | int primitive = this->readS8(); |
| 111 | int maxVertices = this->readS8(); |
| 112 | int invocations = this->readS8(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 113 | return Layout(flags, location, offset, binding, index, set, builtin, |
Brian Osman | 4717fbb | 2021-02-23 13:12:09 -0500 | [diff] [blame] | 114 | inputAttachmentIndex, (Layout::Primitive)primitive, maxVertices, |
Brian Osman | 8c26479 | 2021-07-01 16:41:27 -0400 | [diff] [blame] | 115 | invocations); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 116 | } |
| 117 | default: |
| 118 | SkASSERT(false); |
| 119 | return Layout(); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | Modifiers Rehydrator::modifiers() { |
| 124 | switch (this->readU8()) { |
| 125 | case kDefaultModifiers_Command: |
| 126 | return Modifiers(); |
| 127 | case kModifiers8Bit_Command: { |
| 128 | Layout l = this->layout(); |
| 129 | int flags = this->readU8(); |
| 130 | return Modifiers(l, flags); |
| 131 | } |
| 132 | case kModifiers_Command: { |
| 133 | Layout l = this->layout(); |
| 134 | int flags = this->readS32(); |
| 135 | return Modifiers(l, flags); |
| 136 | } |
| 137 | default: |
| 138 | SkASSERT(false); |
| 139 | return Modifiers(); |
| 140 | } |
| 141 | } |
| 142 | |
Brian Osman | 5bf3e20 | 2020-10-13 10:34:18 -0400 | [diff] [blame] | 143 | const Symbol* Rehydrator::symbol() { |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 144 | int kind = this->readU8(); |
| 145 | switch (kind) { |
| 146 | case kArrayType_Command: { |
| 147 | uint16_t id = this->readU16(); |
| 148 | const Type* componentType = this->type(); |
Brian Osman | e8c2608 | 2020-10-01 17:22:45 -0400 | [diff] [blame] | 149 | int8_t count = this->readS8(); |
John Stiles | 9506c34 | 2021-08-09 14:19:01 -0400 | [diff] [blame^] | 150 | const String* arrayName = |
| 151 | fSymbolTable->takeOwnershipOfString(componentType->getArrayName(count)); |
Brian Osman | 5bf3e20 | 2020-10-13 10:34:18 -0400 | [diff] [blame] | 152 | const Type* result = fSymbolTable->takeOwnershipOfSymbol( |
John Stiles | 9506c34 | 2021-08-09 14:19:01 -0400 | [diff] [blame^] | 153 | Type::MakeArrayType(*arrayName, *componentType, count)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 154 | this->addSymbol(id, result); |
| 155 | return result; |
| 156 | } |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 157 | case kFunctionDeclaration_Command: { |
| 158 | uint16_t id = this->readU16(); |
| 159 | Modifiers modifiers = this->modifiers(); |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 160 | skstd::string_view name = this->readString(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 161 | int parameterCount = this->readU8(); |
Brian Osman | 5bf3e20 | 2020-10-13 10:34:18 -0400 | [diff] [blame] | 162 | std::vector<const Variable*> parameters; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 163 | parameters.reserve(parameterCount); |
| 164 | for (int i = 0; i < parameterCount; ++i) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 165 | parameters.push_back(this->symbolRef<Variable>(Symbol::Kind::kVariable)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 166 | } |
| 167 | const Type* returnType = this->type(); |
Brian Osman | 5bf3e20 | 2020-10-13 10:34:18 -0400 | [diff] [blame] | 168 | const FunctionDeclaration* result = |
John Stiles | 3ae071e | 2020-08-05 15:29:29 -0400 | [diff] [blame] | 169 | fSymbolTable->takeOwnershipOfSymbol(std::make_unique<FunctionDeclaration>( |
John Stiles | f2872e6 | 2021-05-04 11:38:43 -0400 | [diff] [blame] | 170 | /*offset=*/-1, |
| 171 | this->modifiersPool().add(modifiers), |
| 172 | name, |
| 173 | std::move(parameters), |
| 174 | returnType, |
| 175 | /*builtin=*/true)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 176 | this->addSymbol(id, result); |
| 177 | return result; |
| 178 | } |
| 179 | case kField_Command: { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 180 | const Variable* owner = this->symbolRef<Variable>(Symbol::Kind::kVariable); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 181 | uint8_t index = this->readU8(); |
Brian Osman | 5bf3e20 | 2020-10-13 10:34:18 -0400 | [diff] [blame] | 182 | const Field* result = fSymbolTable->takeOwnershipOfSymbol( |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 183 | std::make_unique<Field>(/*offset=*/-1, owner, index)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 184 | return result; |
| 185 | } |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 186 | case kStructType_Command: { |
| 187 | uint16_t id = this->readU16(); |
Ethan Nicholas | d2e0960 | 2021-06-10 11:21:59 -0400 | [diff] [blame] | 188 | String name(this->readString()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 189 | uint8_t fieldCount = this->readU8(); |
| 190 | std::vector<Type::Field> fields; |
| 191 | fields.reserve(fieldCount); |
| 192 | for (int i = 0; i < fieldCount; ++i) { |
| 193 | Modifiers m = this->modifiers(); |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 194 | skstd::string_view fieldName = this->readString(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 195 | const Type* type = this->type(); |
John Stiles | f621e23 | 2020-08-25 13:33:02 -0400 | [diff] [blame] | 196 | fields.emplace_back(m, fieldName, type); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 197 | } |
Ethan Nicholas | 27f06eb | 2021-07-26 16:39:40 -0400 | [diff] [blame] | 198 | skstd::string_view nameChars(*fSymbolTable->takeOwnershipOfString(std::move(name))); |
Brian Osman | 5bf3e20 | 2020-10-13 10:34:18 -0400 | [diff] [blame] | 199 | const Type* result = fSymbolTable->takeOwnershipOfSymbol( |
Ethan Nicholas | 27f06eb | 2021-07-26 16:39:40 -0400 | [diff] [blame] | 200 | Type::MakeStructType(/*offset=*/-1, nameChars, std::move(fields))); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 201 | this->addSymbol(id, result); |
| 202 | return result; |
| 203 | } |
| 204 | case kSymbolRef_Command: { |
| 205 | uint16_t id = this->readU16(); |
| 206 | SkASSERT(fSymbols.size() > id); |
| 207 | return fSymbols[id]; |
| 208 | } |
John Stiles | 49a547f | 2020-10-06 16:14:37 -0400 | [diff] [blame] | 209 | case kSymbolAlias_Command: { |
| 210 | uint16_t id = this->readU16(); |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 211 | skstd::string_view name = this->readString(); |
Brian Osman | 5bf3e20 | 2020-10-13 10:34:18 -0400 | [diff] [blame] | 212 | const Symbol* origSymbol = this->symbol(); |
| 213 | const SymbolAlias* symbolAlias = fSymbolTable->takeOwnershipOfSymbol( |
John Stiles | 49a547f | 2020-10-06 16:14:37 -0400 | [diff] [blame] | 214 | std::make_unique<SymbolAlias>(/*offset=*/-1, name, origSymbol)); |
| 215 | this->addSymbol(id, symbolAlias); |
| 216 | return symbolAlias; |
| 217 | } |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 218 | case kSystemType_Command: { |
| 219 | uint16_t id = this->readU16(); |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 220 | skstd::string_view name = this->readString(); |
Brian Osman | 5bf3e20 | 2020-10-13 10:34:18 -0400 | [diff] [blame] | 221 | const Symbol* result = (*fSymbolTable)[name]; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 222 | SkASSERT(result && result->kind() == Symbol::Kind::kType); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 223 | this->addSymbol(id, result); |
| 224 | return result; |
| 225 | } |
| 226 | case kUnresolvedFunction_Command: { |
| 227 | uint16_t id = this->readU16(); |
| 228 | int length = this->readU8(); |
| 229 | std::vector<const FunctionDeclaration*> functions; |
| 230 | functions.reserve(length); |
| 231 | for (int i = 0; i < length; ++i) { |
| 232 | const Symbol* f = this->symbol(); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 233 | SkASSERT(f && f->kind() == Symbol::Kind::kFunctionDeclaration); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 234 | functions.push_back((const FunctionDeclaration*) f); |
| 235 | } |
Brian Osman | 5bf3e20 | 2020-10-13 10:34:18 -0400 | [diff] [blame] | 236 | const UnresolvedFunction* result = fSymbolTable->takeOwnershipOfSymbol( |
John Stiles | 3ae071e | 2020-08-05 15:29:29 -0400 | [diff] [blame] | 237 | std::make_unique<UnresolvedFunction>(std::move(functions))); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 238 | this->addSymbol(id, result); |
| 239 | return result; |
| 240 | } |
| 241 | case kVariable_Command: { |
| 242 | uint16_t id = this->readU16(); |
John Stiles | f2872e6 | 2021-05-04 11:38:43 -0400 | [diff] [blame] | 243 | const Modifiers* m = this->modifiersPool().add(this->modifiers()); |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 244 | skstd::string_view name = this->readString(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 245 | const Type* type = this->type(); |
| 246 | Variable::Storage storage = (Variable::Storage) this->readU8(); |
Brian Osman | 5bf3e20 | 2020-10-13 10:34:18 -0400 | [diff] [blame] | 247 | const Variable* result = fSymbolTable->takeOwnershipOfSymbol(std::make_unique<Variable>( |
Brian Osman | 3887a01 | 2020-09-30 13:22:27 -0400 | [diff] [blame] | 248 | /*offset=*/-1, m, name, type, /*builtin=*/true, storage)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 249 | this->addSymbol(id, result); |
| 250 | return result; |
| 251 | } |
| 252 | default: |
| 253 | printf("unsupported symbol %d\n", kind); |
| 254 | SkASSERT(false); |
| 255 | return nullptr; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | const Type* Rehydrator::type() { |
| 260 | const Symbol* result = this->symbol(); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 261 | SkASSERT(result->kind() == Symbol::Kind::kType); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 262 | return (const Type*) result; |
| 263 | } |
| 264 | |
| 265 | std::vector<std::unique_ptr<ProgramElement>> Rehydrator::elements() { |
| 266 | SkDEBUGCODE(uint8_t command = )this->readU8(); |
| 267 | SkASSERT(command == kElements_Command); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 268 | std::vector<std::unique_ptr<ProgramElement>> result; |
John Stiles | 1ea7f54 | 2020-11-02 13:07:23 -0500 | [diff] [blame] | 269 | while (std::unique_ptr<ProgramElement> elem = this->element()) { |
| 270 | result.push_back(std::move(elem)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 271 | } |
| 272 | return result; |
| 273 | } |
| 274 | |
| 275 | std::unique_ptr<ProgramElement> Rehydrator::element() { |
| 276 | int kind = this->readU8(); |
| 277 | switch (kind) { |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 278 | case Rehydrator::kFunctionDefinition_Command: { |
| 279 | const FunctionDeclaration* decl = this->symbolRef<FunctionDeclaration>( |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 280 | Symbol::Kind::kFunctionDeclaration); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 281 | std::unique_ptr<Statement> body = this->statement(); |
John Stiles | b8e010c | 2020-08-11 18:05:39 -0400 | [diff] [blame] | 282 | std::unordered_set<const FunctionDeclaration*> refs; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 283 | uint8_t refCount = this->readU8(); |
| 284 | for (int i = 0; i < refCount; ++i) { |
| 285 | refs.insert(this->symbolRef<FunctionDeclaration>( |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 286 | Symbol::Kind::kFunctionDeclaration)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 287 | } |
John Stiles | 607d36b | 2020-10-19 15:00:01 -0400 | [diff] [blame] | 288 | auto result = std::make_unique<FunctionDefinition>(/*offset=*/-1, decl, |
| 289 | /*builtin=*/true, std::move(body), |
| 290 | std::move(refs)); |
| 291 | decl->setDefinition(result.get()); |
| 292 | return std::move(result); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 293 | } |
| 294 | case Rehydrator::kInterfaceBlock_Command: { |
| 295 | const Symbol* var = this->symbol(); |
John Stiles | 87ae34e | 2020-10-13 12:50:11 -0400 | [diff] [blame] | 296 | SkASSERT(var && var->is<Variable>()); |
Ethan Nicholas | 3533ff1 | 2021-08-02 12:53:29 -0400 | [diff] [blame] | 297 | skstd::string_view typeName = this->readString(); |
| 298 | skstd::string_view instanceName = this->readString(); |
John Stiles | d39aec0 | 2020-12-03 10:42:26 -0500 | [diff] [blame] | 299 | int arraySize = this->readS8(); |
Ethan Nicholas | 3533ff1 | 2021-08-02 12:53:29 -0400 | [diff] [blame] | 300 | return std::make_unique<InterfaceBlock>(/*offset=*/-1, &var->as<Variable>(), typeName, |
| 301 | instanceName, arraySize, nullptr); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 302 | } |
| 303 | case Rehydrator::kVarDeclarations_Command: { |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 304 | std::unique_ptr<Statement> decl = this->statement(); |
Ethan Nicholas | 624a529 | 2021-04-16 14:54:43 -0400 | [diff] [blame] | 305 | return std::make_unique<GlobalVarDeclaration>(std::move(decl)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 306 | } |
John Stiles | dc75a97 | 2020-11-25 16:24:55 -0500 | [diff] [blame] | 307 | case Rehydrator::kStructDefinition_Command: { |
| 308 | const Symbol* type = this->symbol(); |
| 309 | SkASSERT(type && type->is<Type>()); |
| 310 | return std::make_unique<StructDefinition>(/*offset=*/-1, type->as<Type>()); |
| 311 | } |
John Stiles | 1ea7f54 | 2020-11-02 13:07:23 -0500 | [diff] [blame] | 312 | case Rehydrator::kElementsComplete_Command: |
| 313 | return nullptr; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 314 | default: |
John Stiles | 1ea7f54 | 2020-11-02 13:07:23 -0500 | [diff] [blame] | 315 | SkDEBUGFAILF("unsupported element %d\n", kind); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 316 | return nullptr; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | std::unique_ptr<Statement> Rehydrator::statement() { |
| 321 | int kind = this->readU8(); |
| 322 | switch (kind) { |
| 323 | case Rehydrator::kBlock_Command: { |
| 324 | AutoRehydratorSymbolTable symbols(this); |
| 325 | int count = this->readU8(); |
John Stiles | 8f2a0cf | 2020-10-13 12:48:21 -0400 | [diff] [blame] | 326 | StatementArray statements; |
John Stiles | f4bda74 | 2020-10-14 16:57:41 -0400 | [diff] [blame] | 327 | statements.reserve_back(count); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 328 | for (int i = 0; i < count; ++i) { |
| 329 | statements.push_back(this->statement()); |
| 330 | } |
| 331 | bool isScope = this->readU8(); |
John Stiles | bf16b6c | 2021-03-12 19:24:31 -0500 | [diff] [blame] | 332 | return Block::Make(/*offset=*/-1, std::move(statements), fSymbolTable, isScope); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 333 | } |
| 334 | case Rehydrator::kBreak_Command: |
John Stiles | a0c04d6 | 2021-03-11 23:07:24 -0500 | [diff] [blame] | 335 | return BreakStatement::Make(/*offset=*/-1); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 336 | case Rehydrator::kContinue_Command: |
John Stiles | a0c04d6 | 2021-03-11 23:07:24 -0500 | [diff] [blame] | 337 | return ContinueStatement::Make(/*offset=*/-1); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 338 | case Rehydrator::kDiscard_Command: |
John Stiles | a0c04d6 | 2021-03-11 23:07:24 -0500 | [diff] [blame] | 339 | return DiscardStatement::Make(/*offset=*/-1); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 340 | case Rehydrator::kDo_Command: { |
| 341 | std::unique_ptr<Statement> stmt = this->statement(); |
| 342 | std::unique_ptr<Expression> expr = this->expression(); |
John Stiles | ea5822e | 2021-02-26 11:18:20 -0500 | [diff] [blame] | 343 | return DoStatement::Make(fContext, std::move(stmt), std::move(expr)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 344 | } |
| 345 | case Rehydrator::kExpressionStatement_Command: { |
| 346 | std::unique_ptr<Expression> expr = this->expression(); |
John Stiles | 3e5871c | 2021-02-25 20:52:03 -0500 | [diff] [blame] | 347 | return ExpressionStatement::Make(fContext, std::move(expr)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 348 | } |
| 349 | case Rehydrator::kFor_Command: { |
| 350 | std::unique_ptr<Statement> initializer = this->statement(); |
| 351 | std::unique_ptr<Expression> test = this->expression(); |
| 352 | std::unique_ptr<Expression> next = this->expression(); |
| 353 | std::unique_ptr<Statement> body = this->statement(); |
| 354 | std::shared_ptr<SymbolTable> symbols = this->symbolTable(); |
John Stiles | 23521a8 | 2021-03-02 17:02:51 -0500 | [diff] [blame] | 355 | return ForStatement::Make(fContext, /*offset=*/-1, std::move(initializer), |
| 356 | std::move(test), std::move(next), std::move(body), |
| 357 | std::move(symbols)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 358 | } |
| 359 | case Rehydrator::kIf_Command: { |
| 360 | bool isStatic = this->readU8(); |
| 361 | std::unique_ptr<Expression> test = this->expression(); |
| 362 | std::unique_ptr<Statement> ifTrue = this->statement(); |
| 363 | std::unique_ptr<Statement> ifFalse = this->statement(); |
John Stiles | cf3059e | 2021-02-25 14:27:02 -0500 | [diff] [blame] | 364 | return IfStatement::Make(fContext, /*offset=*/-1, isStatic, std::move(test), |
| 365 | std::move(ifTrue), std::move(ifFalse)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 366 | } |
John Stiles | 98c1f82 | 2020-09-09 14:18:53 -0400 | [diff] [blame] | 367 | case Rehydrator::kInlineMarker_Command: { |
| 368 | const FunctionDeclaration* funcDecl = this->symbolRef<FunctionDeclaration>( |
| 369 | Symbol::Kind::kFunctionDeclaration); |
John Stiles | a0c04d6 | 2021-03-11 23:07:24 -0500 | [diff] [blame] | 370 | return InlineMarker::Make(funcDecl); |
John Stiles | 98c1f82 | 2020-09-09 14:18:53 -0400 | [diff] [blame] | 371 | } |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 372 | case Rehydrator::kReturn_Command: { |
| 373 | std::unique_ptr<Expression> expr = this->expression(); |
John Stiles | a0c04d6 | 2021-03-11 23:07:24 -0500 | [diff] [blame] | 374 | return ReturnStatement::Make(/*offset=*/-1, std::move(expr)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 375 | } |
| 376 | case Rehydrator::kSwitch_Command: { |
| 377 | bool isStatic = this->readU8(); |
| 378 | AutoRehydratorSymbolTable symbols(this); |
| 379 | std::unique_ptr<Expression> expr = this->expression(); |
| 380 | int caseCount = this->readU8(); |
John Stiles | b23a64b | 2021-03-11 08:27:59 -0500 | [diff] [blame] | 381 | StatementArray cases; |
| 382 | cases.reserve_back(caseCount); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 383 | for (int i = 0; i < caseCount; ++i) { |
| 384 | std::unique_ptr<Expression> value = this->expression(); |
John Stiles | c3ce43b | 2021-03-09 15:37:01 -0500 | [diff] [blame] | 385 | std::unique_ptr<Statement> statement = this->statement(); |
John Stiles | 8f2a0cf | 2020-10-13 12:48:21 -0400 | [diff] [blame] | 386 | cases.push_back(std::make_unique<SwitchCase>(/*offset=*/-1, std::move(value), |
John Stiles | c3ce43b | 2021-03-09 15:37:01 -0500 | [diff] [blame] | 387 | std::move(statement))); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 388 | } |
John Stiles | 23521a8 | 2021-03-02 17:02:51 -0500 | [diff] [blame] | 389 | return SwitchStatement::Make(fContext, /*offset=*/-1, isStatic, std::move(expr), |
| 390 | std::move(cases), fSymbolTable); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 391 | } |
| 392 | case Rehydrator::kVarDeclaration_Command: { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 393 | Variable* var = this->symbolRef<Variable>(Symbol::Kind::kVariable); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 394 | const Type* baseType = this->type(); |
John Stiles | 62a5646 | 2020-12-03 10:41:58 -0500 | [diff] [blame] | 395 | int arraySize = this->readS8(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 396 | std::unique_ptr<Expression> value = this->expression(); |
John Stiles | e67bd13 | 2021-03-19 18:39:25 -0400 | [diff] [blame] | 397 | return VarDeclaration::Make(fContext, var, baseType, arraySize, std::move(value)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 398 | } |
| 399 | case Rehydrator::kVoid_Command: |
| 400 | return nullptr; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 401 | default: |
| 402 | printf("unsupported statement %d\n", kind); |
| 403 | SkASSERT(false); |
| 404 | return nullptr; |
| 405 | } |
| 406 | } |
| 407 | |
John Stiles | d8eb875 | 2021-04-01 11:49:10 -0400 | [diff] [blame] | 408 | ExpressionArray Rehydrator::expressionArray() { |
| 409 | uint8_t count = this->readU8(); |
| 410 | ExpressionArray array; |
| 411 | array.reserve_back(count); |
| 412 | for (int i = 0; i < count; ++i) { |
| 413 | array.push_back(this->expression()); |
| 414 | } |
| 415 | return array; |
| 416 | } |
| 417 | |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 418 | std::unique_ptr<Expression> Rehydrator::expression() { |
| 419 | int kind = this->readU8(); |
| 420 | switch (kind) { |
| 421 | case Rehydrator::kBinary_Command: { |
| 422 | std::unique_ptr<Expression> left = this->expression(); |
| 423 | Token::Kind op = (Token::Kind) this->readU8(); |
| 424 | std::unique_ptr<Expression> right = this->expression(); |
John Stiles | e2aec43 | 2021-03-01 09:27:48 -0500 | [diff] [blame] | 425 | return BinaryExpression::Make(fContext, std::move(left), op, std::move(right)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 426 | } |
| 427 | case Rehydrator::kBoolLiteral_Command: { |
| 428 | bool value = this->readU8(); |
John Stiles | 9ce80f7 | 2021-03-11 22:35:19 -0500 | [diff] [blame] | 429 | return BoolLiteral::Make(fContext, /*offset=*/-1, value); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 430 | } |
John Stiles | 4118f14 | 2021-04-01 16:42:35 -0400 | [diff] [blame] | 431 | case Rehydrator::kConstructorArray_Command: { |
| 432 | const Type* type = this->type(); |
| 433 | return ConstructorArray::Make(fContext, /*offset=*/-1, *type, this->expressionArray()); |
| 434 | } |
John Stiles | 8cad637 | 2021-04-07 12:31:13 -0400 | [diff] [blame] | 435 | case Rehydrator::kConstructorCompound_Command: { |
John Stiles | 2bec8ab | 2021-04-06 18:40:04 -0400 | [diff] [blame] | 436 | const Type* type = this->type(); |
John Stiles | 8cad637 | 2021-04-07 12:31:13 -0400 | [diff] [blame] | 437 | return ConstructorCompound::Make(fContext, /*offset=*/-1, *type, |
John Stiles | 2bec8ab | 2021-04-06 18:40:04 -0400 | [diff] [blame] | 438 | this->expressionArray()); |
| 439 | } |
John Stiles | e118278 | 2021-03-30 22:09:37 -0400 | [diff] [blame] | 440 | case Rehydrator::kConstructorDiagonalMatrix_Command: { |
| 441 | const Type* type = this->type(); |
John Stiles | d8eb875 | 2021-04-01 11:49:10 -0400 | [diff] [blame] | 442 | ExpressionArray args = this->expressionArray(); |
| 443 | SkASSERT(args.size() == 1); |
John Stiles | e118278 | 2021-03-30 22:09:37 -0400 | [diff] [blame] | 444 | return ConstructorDiagonalMatrix::Make(fContext, /*offset=*/-1, *type, |
John Stiles | d8eb875 | 2021-04-01 11:49:10 -0400 | [diff] [blame] | 445 | std::move(args[0])); |
John Stiles | e118278 | 2021-03-30 22:09:37 -0400 | [diff] [blame] | 446 | } |
John Stiles | 5abb9e1 | 2021-04-06 13:47:19 -0400 | [diff] [blame] | 447 | case Rehydrator::kConstructorMatrixResize_Command: { |
| 448 | const Type* type = this->type(); |
| 449 | ExpressionArray args = this->expressionArray(); |
| 450 | SkASSERT(args.size() == 1); |
| 451 | return ConstructorMatrixResize::Make(fContext, /*offset=*/-1, *type, |
| 452 | std::move(args[0])); |
| 453 | } |
John Stiles | fd7252f | 2021-04-04 22:24:40 -0400 | [diff] [blame] | 454 | case Rehydrator::kConstructorScalarCast_Command: { |
| 455 | const Type* type = this->type(); |
| 456 | ExpressionArray args = this->expressionArray(); |
| 457 | SkASSERT(args.size() == 1); |
| 458 | return ConstructorScalarCast::Make(fContext, /*offset=*/-1, *type, std::move(args[0])); |
| 459 | } |
John Stiles | 2938eea | 2021-04-01 18:58:25 -0400 | [diff] [blame] | 460 | case Rehydrator::kConstructorSplat_Command: { |
| 461 | const Type* type = this->type(); |
| 462 | ExpressionArray args = this->expressionArray(); |
| 463 | SkASSERT(args.size() == 1); |
| 464 | return ConstructorSplat::Make(fContext, /*offset=*/-1, *type, std::move(args[0])); |
| 465 | } |
John Stiles | d47330f | 2021-04-08 23:25:52 -0400 | [diff] [blame] | 466 | case Rehydrator::kConstructorStruct_Command: { |
| 467 | const Type* type = this->type(); |
| 468 | return ConstructorStruct::Make(fContext, /*offset=*/-1, *type, this->expressionArray()); |
| 469 | } |
John Stiles | 8cad637 | 2021-04-07 12:31:13 -0400 | [diff] [blame] | 470 | case Rehydrator::kConstructorCompoundCast_Command: { |
John Stiles | b14a819 | 2021-04-05 11:40:46 -0400 | [diff] [blame] | 471 | const Type* type = this->type(); |
| 472 | ExpressionArray args = this->expressionArray(); |
| 473 | SkASSERT(args.size() == 1); |
John Stiles | 8cad637 | 2021-04-07 12:31:13 -0400 | [diff] [blame] | 474 | return ConstructorCompoundCast::Make(fContext,/*offset=*/-1, *type, std::move(args[0])); |
John Stiles | b14a819 | 2021-04-05 11:40:46 -0400 | [diff] [blame] | 475 | } |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 476 | case Rehydrator::kFieldAccess_Command: { |
| 477 | std::unique_ptr<Expression> base = this->expression(); |
| 478 | int index = this->readU8(); |
| 479 | FieldAccess::OwnerKind ownerKind = (FieldAccess::OwnerKind) this->readU8(); |
John Stiles | 06d600f | 2021-03-08 09:18:21 -0500 | [diff] [blame] | 480 | return FieldAccess::Make(fContext, std::move(base), index, ownerKind); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 481 | } |
| 482 | case Rehydrator::kFloatLiteral_Command: { |
Brian Osman | fb964a4 | 2020-11-18 10:45:52 -0500 | [diff] [blame] | 483 | const Type* type = this->type(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 484 | FloatIntUnion u; |
| 485 | u.fInt = this->readS32(); |
John Stiles | 9ce80f7 | 2021-03-11 22:35:19 -0500 | [diff] [blame] | 486 | return FloatLiteral::Make(/*offset=*/-1, u.fFloat, type); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 487 | } |
| 488 | case Rehydrator::kFunctionCall_Command: { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 489 | const Type* type = this->type(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 490 | const FunctionDeclaration* f = this->symbolRef<FunctionDeclaration>( |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 491 | Symbol::Kind::kFunctionDeclaration); |
John Stiles | d8eb875 | 2021-04-01 11:49:10 -0400 | [diff] [blame] | 492 | ExpressionArray args = this->expressionArray(); |
John Stiles | cd7ba50 | 2021-03-19 10:54:59 -0400 | [diff] [blame] | 493 | return FunctionCall::Make(fContext, /*offset=*/-1, type, *f, std::move(args)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 494 | } |
| 495 | case Rehydrator::kIndex_Command: { |
| 496 | std::unique_ptr<Expression> base = this->expression(); |
| 497 | std::unique_ptr<Expression> index = this->expression(); |
John Stiles | 51d3398 | 2021-03-08 09:18:07 -0500 | [diff] [blame] | 498 | return IndexExpression::Make(fContext, std::move(base), std::move(index)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 499 | } |
| 500 | case Rehydrator::kIntLiteral_Command: { |
Brian Osman | fb964a4 | 2020-11-18 10:45:52 -0500 | [diff] [blame] | 501 | const Type* type = this->type(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 502 | int value = this->readS32(); |
John Stiles | 9ce80f7 | 2021-03-11 22:35:19 -0500 | [diff] [blame] | 503 | return IntLiteral::Make(/*offset=*/-1, value, type); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 504 | } |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 505 | case Rehydrator::kPostfix_Command: { |
| 506 | Token::Kind op = (Token::Kind) this->readU8(); |
| 507 | std::unique_ptr<Expression> operand = this->expression(); |
John Stiles | 52d3b01 | 2021-02-26 15:56:48 -0500 | [diff] [blame] | 508 | return PostfixExpression::Make(fContext, std::move(operand), op); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 509 | } |
| 510 | case Rehydrator::kPrefix_Command: { |
| 511 | Token::Kind op = (Token::Kind) this->readU8(); |
| 512 | std::unique_ptr<Expression> operand = this->expression(); |
John Stiles | b0eb20f | 2021-02-26 15:29:33 -0500 | [diff] [blame] | 513 | return PrefixExpression::Make(fContext, op, std::move(operand)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 514 | } |
| 515 | case Rehydrator::kSetting_Command: { |
Ethan Nicholas | d2e0960 | 2021-06-10 11:21:59 -0400 | [diff] [blame] | 516 | String name(this->readString()); |
John Stiles | 23521a8 | 2021-03-02 17:02:51 -0500 | [diff] [blame] | 517 | return Setting::Convert(fContext, /*offset=*/-1, name); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 518 | } |
| 519 | case Rehydrator::kSwizzle_Command: { |
| 520 | std::unique_ptr<Expression> base = this->expression(); |
| 521 | int count = this->readU8(); |
John Stiles | 750109b | 2020-10-30 13:45:46 -0400 | [diff] [blame] | 522 | ComponentArray components; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 523 | for (int i = 0; i < count; ++i) { |
| 524 | components.push_back(this->readU8()); |
| 525 | } |
John Stiles | 23521a8 | 2021-03-02 17:02:51 -0500 | [diff] [blame] | 526 | return Swizzle::Make(fContext, std::move(base), components); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 527 | } |
| 528 | case Rehydrator::kTernary_Command: { |
| 529 | std::unique_ptr<Expression> test = this->expression(); |
| 530 | std::unique_ptr<Expression> ifTrue = this->expression(); |
| 531 | std::unique_ptr<Expression> ifFalse = this->expression(); |
John Stiles | 90518f7 | 2021-02-26 20:44:54 -0500 | [diff] [blame] | 532 | return TernaryExpression::Make(fContext, std::move(test), |
| 533 | std::move(ifTrue), std::move(ifFalse)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 534 | } |
| 535 | case Rehydrator::kVariableReference_Command: { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 536 | const Variable* var = this->symbolRef<Variable>(Symbol::Kind::kVariable); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 537 | VariableReference::RefKind refKind = (VariableReference::RefKind) this->readU8(); |
John Stiles | a94e026 | 2021-04-27 17:29:59 -0400 | [diff] [blame] | 538 | return VariableReference::Make(/*offset=*/-1, var, refKind); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 539 | } |
| 540 | case Rehydrator::kVoid_Command: |
| 541 | return nullptr; |
| 542 | default: |
| 543 | printf("unsupported expression %d\n", kind); |
| 544 | SkASSERT(false); |
| 545 | return nullptr; |
| 546 | } |
| 547 | } |
| 548 | |
Brian Osman | 1313d1a | 2020-09-08 10:34:30 -0400 | [diff] [blame] | 549 | std::shared_ptr<SymbolTable> Rehydrator::symbolTable(bool inherit) { |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 550 | int command = this->readU8(); |
| 551 | if (command == kVoid_Command) { |
| 552 | return nullptr; |
| 553 | } |
| 554 | SkASSERT(command == kSymbolTable_Command); |
| 555 | uint16_t ownedCount = this->readU16(); |
Brian Osman | 1313d1a | 2020-09-08 10:34:30 -0400 | [diff] [blame] | 556 | std::shared_ptr<SymbolTable> oldTable = fSymbolTable; |
John Stiles | 7c3515b | 2020-10-16 18:38:39 -0400 | [diff] [blame] | 557 | std::shared_ptr<SymbolTable> result = |
| 558 | inherit ? std::make_shared<SymbolTable>(fSymbolTable, /*builtin=*/true) |
John Stiles | f2872e6 | 2021-05-04 11:38:43 -0400 | [diff] [blame] | 559 | : std::make_shared<SymbolTable>(this->errorReporter(), /*builtin=*/true); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 560 | fSymbolTable = result; |
Brian Osman | 5bf3e20 | 2020-10-13 10:34:18 -0400 | [diff] [blame] | 561 | std::vector<const Symbol*> ownedSymbols; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 562 | ownedSymbols.reserve(ownedCount); |
| 563 | for (int i = 0; i < ownedCount; ++i) { |
| 564 | ownedSymbols.push_back(this->symbol()); |
| 565 | } |
| 566 | uint16_t symbolCount = this->readU16(); |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 567 | std::vector<std::pair<skstd::string_view, int>> symbols; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 568 | symbols.reserve(symbolCount); |
| 569 | for (int i = 0; i < symbolCount; ++i) { |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 570 | int index = this->readU16(); |
John Stiles | b8cc665 | 2020-10-08 09:12:07 -0400 | [diff] [blame] | 571 | fSymbolTable->addWithoutOwnership(ownedSymbols[index]); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 572 | } |
Brian Osman | 1313d1a | 2020-09-08 10:34:30 -0400 | [diff] [blame] | 573 | fSymbolTable = oldTable; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 574 | return result; |
| 575 | } |
| 576 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 577 | } // namespace SkSL |