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