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