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