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