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