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 | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 13 | #include "src/sksl/ir/SkSLBinaryExpression.h" |
| 14 | #include "src/sksl/ir/SkSLBreakStatement.h" |
| 15 | #include "src/sksl/ir/SkSLContinueStatement.h" |
| 16 | #include "src/sksl/ir/SkSLDiscardStatement.h" |
| 17 | #include "src/sksl/ir/SkSLDoStatement.h" |
| 18 | #include "src/sksl/ir/SkSLEnum.h" |
| 19 | #include "src/sksl/ir/SkSLExpression.h" |
| 20 | #include "src/sksl/ir/SkSLExpressionStatement.h" |
| 21 | #include "src/sksl/ir/SkSLField.h" |
| 22 | #include "src/sksl/ir/SkSLFieldAccess.h" |
| 23 | #include "src/sksl/ir/SkSLFloatLiteral.h" |
| 24 | #include "src/sksl/ir/SkSLForStatement.h" |
| 25 | #include "src/sksl/ir/SkSLFunctionCall.h" |
| 26 | #include "src/sksl/ir/SkSLFunctionDeclaration.h" |
| 27 | #include "src/sksl/ir/SkSLFunctionDefinition.h" |
| 28 | #include "src/sksl/ir/SkSLIfStatement.h" |
| 29 | #include "src/sksl/ir/SkSLIndexExpression.h" |
John Stiles | 98c1f82 | 2020-09-09 14:18:53 -0400 | [diff] [blame] | 30 | #include "src/sksl/ir/SkSLInlineMarker.h" |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 31 | #include "src/sksl/ir/SkSLIntLiteral.h" |
| 32 | #include "src/sksl/ir/SkSLInterfaceBlock.h" |
| 33 | #include "src/sksl/ir/SkSLModifiers.h" |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 34 | #include "src/sksl/ir/SkSLPostfixExpression.h" |
| 35 | #include "src/sksl/ir/SkSLPrefixExpression.h" |
| 36 | #include "src/sksl/ir/SkSLProgramElement.h" |
| 37 | #include "src/sksl/ir/SkSLReturnStatement.h" |
| 38 | #include "src/sksl/ir/SkSLSetting.h" |
| 39 | #include "src/sksl/ir/SkSLStatement.h" |
John Stiles | dc75a97 | 2020-11-25 16:24:55 -0500 | [diff] [blame] | 40 | #include "src/sksl/ir/SkSLStructDefinition.h" |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 41 | #include "src/sksl/ir/SkSLSwitchCase.h" |
| 42 | #include "src/sksl/ir/SkSLSwitchStatement.h" |
| 43 | #include "src/sksl/ir/SkSLSwizzle.h" |
John Stiles | 49a547f | 2020-10-06 16:14:37 -0400 | [diff] [blame] | 44 | #include "src/sksl/ir/SkSLSymbolAlias.h" |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 45 | #include "src/sksl/ir/SkSLSymbolTable.h" |
| 46 | #include "src/sksl/ir/SkSLTernaryExpression.h" |
| 47 | #include "src/sksl/ir/SkSLType.h" |
| 48 | #include "src/sksl/ir/SkSLUnresolvedFunction.h" |
| 49 | #include "src/sksl/ir/SkSLVarDeclarations.h" |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 50 | #include "src/sksl/ir/SkSLVariable.h" |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 51 | |
| 52 | namespace SkSL { |
| 53 | |
| 54 | class AutoRehydratorSymbolTable { |
| 55 | public: |
| 56 | AutoRehydratorSymbolTable(Rehydrator* rehydrator) |
| 57 | : fRehydrator(rehydrator) |
| 58 | , fOldSymbols(fRehydrator->fSymbolTable) { |
| 59 | fRehydrator->fSymbolTable = fRehydrator->symbolTable(); |
| 60 | } |
| 61 | |
| 62 | ~AutoRehydratorSymbolTable() { |
| 63 | fRehydrator->fSymbolTable = std::move(fOldSymbols); |
| 64 | } |
| 65 | |
| 66 | private: |
| 67 | Rehydrator* fRehydrator; |
| 68 | std::shared_ptr<SymbolTable> fOldSymbols; |
| 69 | }; |
| 70 | |
John Stiles | 7c3515b | 2020-10-16 18:38:39 -0400 | [diff] [blame] | 71 | Rehydrator::Rehydrator(const Context* context, ModifiersPool* modifiers, |
| 72 | std::shared_ptr<SymbolTable> symbolTable, ErrorReporter* errorReporter, |
| 73 | const uint8_t* src, size_t length) |
| 74 | : fContext(*context) |
| 75 | , fModifiers(*modifiers) |
| 76 | , fErrors(errorReporter) |
| 77 | , fSymbolTable(std::move(symbolTable)) |
| 78 | , fStart(src) |
| 79 | SkDEBUGCODE(, fEnd(fStart + length)) { |
| 80 | SkASSERT(fSymbolTable); |
| 81 | SkASSERT(fSymbolTable->isBuiltin()); |
| 82 | // skip past string data |
| 83 | fIP = fStart; |
| 84 | fIP += this->readU16(); |
| 85 | } |
| 86 | |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 87 | Layout Rehydrator::layout() { |
| 88 | switch (this->readU8()) { |
| 89 | case kBuiltinLayout_Command: { |
| 90 | Layout result; |
| 91 | result.fBuiltin = this->readS16(); |
| 92 | return result; |
| 93 | } |
| 94 | case kDefaultLayout_Command: |
| 95 | return Layout(); |
| 96 | case kLayout_Command: { |
| 97 | int flags = this->readU32(); |
| 98 | int location = this->readS8(); |
| 99 | int offset = this->readS8(); |
| 100 | int binding = this->readS8(); |
| 101 | int index = this->readS8(); |
| 102 | int set = this->readS8(); |
| 103 | int builtin = this->readS16(); |
| 104 | int inputAttachmentIndex = this->readS8(); |
| 105 | int format = this->readS8(); |
| 106 | int primitive = this->readS8(); |
| 107 | int maxVertices = this->readS8(); |
| 108 | int invocations = this->readS8(); |
| 109 | StringFragment marker = this->readString(); |
| 110 | StringFragment when = this->readString(); |
| 111 | int key = this->readS8(); |
| 112 | int ctype = this->readS8(); |
| 113 | return Layout(flags, location, offset, binding, index, set, builtin, |
| 114 | inputAttachmentIndex, (Layout::Format) format, |
| 115 | (Layout::Primitive) primitive, maxVertices, invocations, marker, when, |
| 116 | (Layout::Key) key, (Layout::CType) ctype); |
| 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 |
| 294 | auto valueLiteral = std::make_unique<IntLiteral>(fContext, /*offset=*/-1, value); |
| 295 | auto declaration = std::make_unique<VarDeclaration>(&v, &v.type(), /*arraySize=*/0, |
| 296 | std::move(valueLiteral)); |
| 297 | v.setDeclaration(declaration.get()); |
| 298 | symbols->takeOwnershipOfIRNode(std::move(declaration)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 299 | } |
John Stiles | 1c82367 | 2020-10-20 10:23:50 -0400 | [diff] [blame] | 300 | return std::make_unique<Enum>(/*offset=*/-1, typeName, std::move(symbols), |
| 301 | /*isSharedWithCpp=*/true, /*isBuiltin=*/true); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 302 | } |
| 303 | case Rehydrator::kFunctionDefinition_Command: { |
| 304 | const FunctionDeclaration* decl = this->symbolRef<FunctionDeclaration>( |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 305 | Symbol::Kind::kFunctionDeclaration); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 306 | std::unique_ptr<Statement> body = this->statement(); |
John Stiles | b8e010c | 2020-08-11 18:05:39 -0400 | [diff] [blame] | 307 | std::unordered_set<const FunctionDeclaration*> refs; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 308 | uint8_t refCount = this->readU8(); |
| 309 | for (int i = 0; i < refCount; ++i) { |
| 310 | refs.insert(this->symbolRef<FunctionDeclaration>( |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 311 | Symbol::Kind::kFunctionDeclaration)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 312 | } |
John Stiles | 607d36b | 2020-10-19 15:00:01 -0400 | [diff] [blame] | 313 | auto result = std::make_unique<FunctionDefinition>(/*offset=*/-1, decl, |
| 314 | /*builtin=*/true, std::move(body), |
| 315 | std::move(refs)); |
| 316 | decl->setDefinition(result.get()); |
| 317 | return std::move(result); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 318 | } |
| 319 | case Rehydrator::kInterfaceBlock_Command: { |
| 320 | const Symbol* var = this->symbol(); |
John Stiles | 87ae34e | 2020-10-13 12:50:11 -0400 | [diff] [blame] | 321 | SkASSERT(var && var->is<Variable>()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 322 | StringFragment typeName = this->readString(); |
| 323 | StringFragment instanceName = this->readString(); |
John Stiles | d39aec0 | 2020-12-03 10:42:26 -0500 | [diff] [blame] | 324 | int arraySize = this->readS8(); |
John Stiles | 87ae34e | 2020-10-13 12:50:11 -0400 | [diff] [blame] | 325 | return std::make_unique<InterfaceBlock>(/*offset=*/-1, &var->as<Variable>(), typeName, |
John Stiles | d39aec0 | 2020-12-03 10:42:26 -0500 | [diff] [blame] | 326 | instanceName, arraySize, nullptr); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 327 | } |
| 328 | case Rehydrator::kVarDeclarations_Command: { |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 329 | std::unique_ptr<Statement> decl = this->statement(); |
John Stiles | 1ea7f54 | 2020-11-02 13:07:23 -0500 | [diff] [blame] | 330 | return std::make_unique<GlobalVarDeclaration>(/*offset=*/-1, std::move(decl)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 331 | } |
John Stiles | dc75a97 | 2020-11-25 16:24:55 -0500 | [diff] [blame] | 332 | case Rehydrator::kStructDefinition_Command: { |
| 333 | const Symbol* type = this->symbol(); |
| 334 | SkASSERT(type && type->is<Type>()); |
| 335 | return std::make_unique<StructDefinition>(/*offset=*/-1, type->as<Type>()); |
| 336 | } |
John Stiles | 1ea7f54 | 2020-11-02 13:07:23 -0500 | [diff] [blame] | 337 | case Rehydrator::kElementsComplete_Command: |
| 338 | return nullptr; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 339 | default: |
John Stiles | 1ea7f54 | 2020-11-02 13:07:23 -0500 | [diff] [blame] | 340 | SkDEBUGFAILF("unsupported element %d\n", kind); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 341 | return nullptr; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | std::unique_ptr<Statement> Rehydrator::statement() { |
| 346 | int kind = this->readU8(); |
| 347 | switch (kind) { |
| 348 | case Rehydrator::kBlock_Command: { |
| 349 | AutoRehydratorSymbolTable symbols(this); |
| 350 | int count = this->readU8(); |
John Stiles | 8f2a0cf | 2020-10-13 12:48:21 -0400 | [diff] [blame] | 351 | StatementArray statements; |
John Stiles | f4bda74 | 2020-10-14 16:57:41 -0400 | [diff] [blame] | 352 | statements.reserve_back(count); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 353 | for (int i = 0; i < count; ++i) { |
| 354 | statements.push_back(this->statement()); |
| 355 | } |
| 356 | bool isScope = this->readU8(); |
John Stiles | 8f2a0cf | 2020-10-13 12:48:21 -0400 | [diff] [blame] | 357 | return std::make_unique<Block>(/*offset=*/-1, std::move(statements), fSymbolTable, |
| 358 | isScope); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 359 | } |
| 360 | case Rehydrator::kBreak_Command: |
| 361 | return std::unique_ptr<Statement>(new BreakStatement(-1)); |
| 362 | case Rehydrator::kContinue_Command: |
| 363 | return std::unique_ptr<Statement>(new ContinueStatement(-1)); |
| 364 | case Rehydrator::kDiscard_Command: |
| 365 | return std::unique_ptr<Statement>(new DiscardStatement(-1)); |
| 366 | case Rehydrator::kDo_Command: { |
| 367 | std::unique_ptr<Statement> stmt = this->statement(); |
| 368 | std::unique_ptr<Expression> expr = this->expression(); |
| 369 | return std::unique_ptr<Statement>(new DoStatement(-1, std::move(stmt), |
| 370 | std::move(expr))); |
| 371 | } |
| 372 | case Rehydrator::kExpressionStatement_Command: { |
| 373 | std::unique_ptr<Expression> expr = this->expression(); |
| 374 | return std::unique_ptr<Statement>(new ExpressionStatement(std::move(expr))); |
| 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(); |
| 382 | return std::unique_ptr<Statement>(new ForStatement(-1, std::move(initializer), |
| 383 | std::move(test), std::move(next), |
| 384 | std::move(body), |
| 385 | std::move(symbols))); |
| 386 | } |
| 387 | case Rehydrator::kIf_Command: { |
| 388 | bool isStatic = this->readU8(); |
| 389 | std::unique_ptr<Expression> test = this->expression(); |
| 390 | std::unique_ptr<Statement> ifTrue = this->statement(); |
| 391 | std::unique_ptr<Statement> ifFalse = this->statement(); |
| 392 | return std::unique_ptr<Statement>(new IfStatement(-1, isStatic, std::move(test), |
| 393 | std::move(ifTrue), |
| 394 | std::move(ifFalse))); |
| 395 | } |
John Stiles | 98c1f82 | 2020-09-09 14:18:53 -0400 | [diff] [blame] | 396 | case Rehydrator::kInlineMarker_Command: { |
| 397 | const FunctionDeclaration* funcDecl = this->symbolRef<FunctionDeclaration>( |
| 398 | Symbol::Kind::kFunctionDeclaration); |
Ethan Nicholas | ceb6214 | 2020-10-09 16:51:18 -0400 | [diff] [blame] | 399 | return std::make_unique<InlineMarker>(funcDecl); |
John Stiles | 98c1f82 | 2020-09-09 14:18:53 -0400 | [diff] [blame] | 400 | } |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 401 | case Rehydrator::kReturn_Command: { |
| 402 | std::unique_ptr<Expression> expr = this->expression(); |
| 403 | if (expr) { |
| 404 | return std::unique_ptr<Statement>(new ReturnStatement(std::move(expr))); |
| 405 | } else { |
| 406 | return std::unique_ptr<Statement>(new ReturnStatement(-1)); |
| 407 | } |
| 408 | } |
| 409 | case Rehydrator::kSwitch_Command: { |
| 410 | bool isStatic = this->readU8(); |
| 411 | AutoRehydratorSymbolTable symbols(this); |
| 412 | std::unique_ptr<Expression> expr = this->expression(); |
| 413 | int caseCount = this->readU8(); |
| 414 | std::vector<std::unique_ptr<SwitchCase>> cases; |
| 415 | cases.reserve(caseCount); |
| 416 | for (int i = 0; i < caseCount; ++i) { |
| 417 | std::unique_ptr<Expression> value = this->expression(); |
| 418 | int statementCount = this->readU8(); |
John Stiles | 8f2a0cf | 2020-10-13 12:48:21 -0400 | [diff] [blame] | 419 | StatementArray statements; |
John Stiles | f4bda74 | 2020-10-14 16:57:41 -0400 | [diff] [blame] | 420 | statements.reserve_back(statementCount); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 421 | for (int j = 0; j < statementCount; ++j) { |
| 422 | statements.push_back(this->statement()); |
| 423 | } |
John Stiles | 8f2a0cf | 2020-10-13 12:48:21 -0400 | [diff] [blame] | 424 | cases.push_back(std::make_unique<SwitchCase>(/*offset=*/-1, std::move(value), |
| 425 | std::move(statements))); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 426 | } |
John Stiles | 8f2a0cf | 2020-10-13 12:48:21 -0400 | [diff] [blame] | 427 | return std::make_unique<SwitchStatement>(-1, isStatic, std::move(expr), |
| 428 | std::move(cases), fSymbolTable); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 429 | } |
| 430 | case Rehydrator::kVarDeclaration_Command: { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 431 | Variable* var = this->symbolRef<Variable>(Symbol::Kind::kVariable); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 432 | const Type* baseType = this->type(); |
John Stiles | 62a5646 | 2020-12-03 10:41:58 -0500 | [diff] [blame] | 433 | int arraySize = this->readS8(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 434 | std::unique_ptr<Expression> value = this->expression(); |
Ethan Nicholas | 5b9b0db | 2021-01-21 13:12:01 -0500 | [diff] [blame^] | 435 | auto result = std::make_unique<VarDeclaration>(var, baseType, arraySize, |
| 436 | std::move(value)); |
| 437 | var->setDeclaration(result.get()); |
| 438 | return std::move(result); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 439 | } |
| 440 | case Rehydrator::kVoid_Command: |
| 441 | return nullptr; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 442 | default: |
| 443 | printf("unsupported statement %d\n", kind); |
| 444 | SkASSERT(false); |
| 445 | return nullptr; |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | std::unique_ptr<Expression> Rehydrator::expression() { |
| 450 | int kind = this->readU8(); |
| 451 | switch (kind) { |
| 452 | case Rehydrator::kBinary_Command: { |
| 453 | std::unique_ptr<Expression> left = this->expression(); |
| 454 | Token::Kind op = (Token::Kind) this->readU8(); |
| 455 | std::unique_ptr<Expression> right = this->expression(); |
| 456 | const Type* type = this->type(); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 457 | return std::make_unique<BinaryExpression>(-1, std::move(left), op, std::move(right), |
| 458 | type); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 459 | } |
| 460 | case Rehydrator::kBoolLiteral_Command: { |
| 461 | bool value = this->readU8(); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 462 | return std::make_unique<BoolLiteral>(fContext, -1, value); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 463 | } |
| 464 | case Rehydrator::kConstructor_Command: { |
| 465 | const Type* type = this->type(); |
| 466 | uint8_t argCount = this->readU8(); |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 467 | ExpressionArray args; |
John Stiles | f4bda74 | 2020-10-14 16:57:41 -0400 | [diff] [blame] | 468 | args.reserve_back(argCount); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 469 | for (int i = 0; i < argCount; ++i) { |
| 470 | args.push_back(this->expression()); |
| 471 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 472 | return std::make_unique<Constructor>(-1, type, std::move(args)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 473 | } |
| 474 | case Rehydrator::kFieldAccess_Command: { |
| 475 | std::unique_ptr<Expression> base = this->expression(); |
| 476 | int index = this->readU8(); |
| 477 | FieldAccess::OwnerKind ownerKind = (FieldAccess::OwnerKind) this->readU8(); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 478 | return std::make_unique<FieldAccess>(std::move(base), index, ownerKind); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 479 | } |
| 480 | case Rehydrator::kFloatLiteral_Command: { |
Brian Osman | fb964a4 | 2020-11-18 10:45:52 -0500 | [diff] [blame] | 481 | const Type* type = this->type(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 482 | FloatIntUnion u; |
| 483 | u.fInt = this->readS32(); |
Brian Osman | fb964a4 | 2020-11-18 10:45:52 -0500 | [diff] [blame] | 484 | return std::make_unique<FloatLiteral>(-1, u.fFloat, type); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 485 | } |
| 486 | case Rehydrator::kFunctionCall_Command: { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 487 | const Type* type = this->type(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 488 | const FunctionDeclaration* f = this->symbolRef<FunctionDeclaration>( |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 489 | Symbol::Kind::kFunctionDeclaration); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 490 | uint8_t argCount = this->readU8(); |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 491 | ExpressionArray args; |
John Stiles | f4bda74 | 2020-10-14 16:57:41 -0400 | [diff] [blame] | 492 | args.reserve_back(argCount); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 493 | for (int i = 0; i < argCount; ++i) { |
| 494 | args.push_back(this->expression()); |
| 495 | } |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 496 | return std::make_unique<FunctionCall>(-1, type, f, std::move(args)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 497 | } |
| 498 | case Rehydrator::kIndex_Command: { |
| 499 | std::unique_ptr<Expression> base = this->expression(); |
| 500 | std::unique_ptr<Expression> index = this->expression(); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 501 | return std::make_unique<IndexExpression>(fContext, std::move(base), std::move(index)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 502 | } |
| 503 | case Rehydrator::kIntLiteral_Command: { |
Brian Osman | fb964a4 | 2020-11-18 10:45:52 -0500 | [diff] [blame] | 504 | const Type* type = this->type(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 505 | int value = this->readS32(); |
Brian Osman | fb964a4 | 2020-11-18 10:45:52 -0500 | [diff] [blame] | 506 | return std::make_unique<IntLiteral>(-1, value, type); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 507 | } |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 508 | case Rehydrator::kPostfix_Command: { |
| 509 | Token::Kind op = (Token::Kind) this->readU8(); |
| 510 | std::unique_ptr<Expression> operand = this->expression(); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 511 | return std::make_unique<PostfixExpression>(std::move(operand), op); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 512 | } |
| 513 | case Rehydrator::kPrefix_Command: { |
| 514 | Token::Kind op = (Token::Kind) this->readU8(); |
| 515 | std::unique_ptr<Expression> operand = this->expression(); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 516 | return std::make_unique<PrefixExpression>(op, std::move(operand)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 517 | } |
| 518 | case Rehydrator::kSetting_Command: { |
| 519 | StringFragment name = this->readString(); |
Ethan Nicholas | 01ec7e8 | 2020-10-08 12:10:12 -0400 | [diff] [blame] | 520 | const Type* type = this->type(); |
| 521 | return std::make_unique<Setting>(-1, name, type); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 522 | } |
| 523 | case Rehydrator::kSwizzle_Command: { |
| 524 | std::unique_ptr<Expression> base = this->expression(); |
| 525 | int count = this->readU8(); |
John Stiles | 750109b | 2020-10-30 13:45:46 -0400 | [diff] [blame] | 526 | ComponentArray components; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 527 | for (int i = 0; i < count; ++i) { |
| 528 | components.push_back(this->readU8()); |
| 529 | } |
John Stiles | 750109b | 2020-10-30 13:45:46 -0400 | [diff] [blame] | 530 | return std::make_unique<Swizzle>(fContext, std::move(base), components); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 531 | } |
| 532 | case Rehydrator::kTernary_Command: { |
| 533 | std::unique_ptr<Expression> test = this->expression(); |
| 534 | std::unique_ptr<Expression> ifTrue = this->expression(); |
| 535 | std::unique_ptr<Expression> ifFalse = this->expression(); |
Ethan Nicholas | a02ce0c | 2020-09-21 12:37:02 -0400 | [diff] [blame] | 536 | return std::make_unique<TernaryExpression>(-1, std::move(test), std::move(ifTrue), |
| 537 | std::move(ifFalse)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 538 | } |
| 539 | case Rehydrator::kVariableReference_Command: { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 540 | const Variable* var = this->symbolRef<Variable>(Symbol::Kind::kVariable); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 541 | VariableReference::RefKind refKind = (VariableReference::RefKind) this->readU8(); |
Brian Osman | 79457ef | 2020-09-24 15:01:27 -0400 | [diff] [blame] | 542 | return std::make_unique<VariableReference>(-1, var, refKind); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 543 | } |
| 544 | case Rehydrator::kVoid_Command: |
| 545 | return nullptr; |
| 546 | default: |
| 547 | printf("unsupported expression %d\n", kind); |
| 548 | SkASSERT(false); |
| 549 | return nullptr; |
| 550 | } |
| 551 | } |
| 552 | |
Brian Osman | 1313d1a | 2020-09-08 10:34:30 -0400 | [diff] [blame] | 553 | std::shared_ptr<SymbolTable> Rehydrator::symbolTable(bool inherit) { |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 554 | int command = this->readU8(); |
| 555 | if (command == kVoid_Command) { |
| 556 | return nullptr; |
| 557 | } |
| 558 | SkASSERT(command == kSymbolTable_Command); |
| 559 | uint16_t ownedCount = this->readU16(); |
Brian Osman | 1313d1a | 2020-09-08 10:34:30 -0400 | [diff] [blame] | 560 | std::shared_ptr<SymbolTable> oldTable = fSymbolTable; |
John Stiles | 7c3515b | 2020-10-16 18:38:39 -0400 | [diff] [blame] | 561 | std::shared_ptr<SymbolTable> result = |
| 562 | inherit ? std::make_shared<SymbolTable>(fSymbolTable, /*builtin=*/true) |
| 563 | : std::make_shared<SymbolTable>(fErrors, /*builtin=*/true); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 564 | fSymbolTable = result; |
Brian Osman | 5bf3e20 | 2020-10-13 10:34:18 -0400 | [diff] [blame] | 565 | std::vector<const Symbol*> ownedSymbols; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 566 | ownedSymbols.reserve(ownedCount); |
| 567 | for (int i = 0; i < ownedCount; ++i) { |
| 568 | ownedSymbols.push_back(this->symbol()); |
| 569 | } |
| 570 | uint16_t symbolCount = this->readU16(); |
| 571 | std::vector<std::pair<StringFragment, int>> symbols; |
| 572 | symbols.reserve(symbolCount); |
| 573 | for (int i = 0; i < symbolCount; ++i) { |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 574 | int index = this->readU16(); |
John Stiles | b8cc665 | 2020-10-08 09:12:07 -0400 | [diff] [blame] | 575 | fSymbolTable->addWithoutOwnership(ownedSymbols[index]); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 576 | } |
Brian Osman | 1313d1a | 2020-09-08 10:34:30 -0400 | [diff] [blame] | 577 | fSymbolTable = oldTable; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 578 | return result; |
| 579 | } |
| 580 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 581 | } // namespace SkSL |