blob: c4679ef9db3821f059728a88f9e8a19acdfdadc2 [file] [log] [blame]
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001/*
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 Stilesfbd050b2020-08-03 13:21:46 -04009
10#include <memory>
John Stilesb8e010c2020-08-11 18:05:39 -040011#include <unordered_set>
12
Ethan Nicholasdaed2592021-03-04 14:30:25 -050013#include "include/private/SkSLModifiers.h"
Ethan Nicholas24c17722021-03-09 13:10:59 -050014#include "include/private/SkSLProgramElement.h"
15#include "include/private/SkSLStatement.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040016#include "src/sksl/ir/SkSLBinaryExpression.h"
17#include "src/sksl/ir/SkSLBreakStatement.h"
John Stilese1182782021-03-30 22:09:37 -040018#include "src/sksl/ir/SkSLConstructor.h"
John Stiles4118f142021-04-01 16:42:35 -040019#include "src/sksl/ir/SkSLConstructorArray.h"
John Stiles8cad6372021-04-07 12:31:13 -040020#include "src/sksl/ir/SkSLConstructorCompound.h"
21#include "src/sksl/ir/SkSLConstructorCompoundCast.h"
John Stilese1182782021-03-30 22:09:37 -040022#include "src/sksl/ir/SkSLConstructorDiagonalMatrix.h"
John Stiles5abb9e12021-04-06 13:47:19 -040023#include "src/sksl/ir/SkSLConstructorMatrixResize.h"
John Stilesfd7252f2021-04-04 22:24:40 -040024#include "src/sksl/ir/SkSLConstructorScalarCast.h"
John Stiles2938eea2021-04-01 18:58:25 -040025#include "src/sksl/ir/SkSLConstructorSplat.h"
John Stilesd47330f2021-04-08 23:25:52 -040026#include "src/sksl/ir/SkSLConstructorStruct.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040027#include "src/sksl/ir/SkSLContinueStatement.h"
28#include "src/sksl/ir/SkSLDiscardStatement.h"
29#include "src/sksl/ir/SkSLDoStatement.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040030#include "src/sksl/ir/SkSLExpression.h"
31#include "src/sksl/ir/SkSLExpressionStatement.h"
32#include "src/sksl/ir/SkSLField.h"
33#include "src/sksl/ir/SkSLFieldAccess.h"
34#include "src/sksl/ir/SkSLFloatLiteral.h"
35#include "src/sksl/ir/SkSLForStatement.h"
36#include "src/sksl/ir/SkSLFunctionCall.h"
37#include "src/sksl/ir/SkSLFunctionDeclaration.h"
38#include "src/sksl/ir/SkSLFunctionDefinition.h"
39#include "src/sksl/ir/SkSLIfStatement.h"
40#include "src/sksl/ir/SkSLIndexExpression.h"
John Stiles98c1f822020-09-09 14:18:53 -040041#include "src/sksl/ir/SkSLInlineMarker.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040042#include "src/sksl/ir/SkSLIntLiteral.h"
43#include "src/sksl/ir/SkSLInterfaceBlock.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040044#include "src/sksl/ir/SkSLPostfixExpression.h"
45#include "src/sksl/ir/SkSLPrefixExpression.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040046#include "src/sksl/ir/SkSLReturnStatement.h"
47#include "src/sksl/ir/SkSLSetting.h"
John Stilesdc75a972020-11-25 16:24:55 -050048#include "src/sksl/ir/SkSLStructDefinition.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040049#include "src/sksl/ir/SkSLSwitchCase.h"
50#include "src/sksl/ir/SkSLSwitchStatement.h"
51#include "src/sksl/ir/SkSLSwizzle.h"
John Stiles49a547f2020-10-06 16:14:37 -040052#include "src/sksl/ir/SkSLSymbolAlias.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040053#include "src/sksl/ir/SkSLSymbolTable.h"
54#include "src/sksl/ir/SkSLTernaryExpression.h"
55#include "src/sksl/ir/SkSLType.h"
56#include "src/sksl/ir/SkSLUnresolvedFunction.h"
57#include "src/sksl/ir/SkSLVarDeclarations.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040058#include "src/sksl/ir/SkSLVariable.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040059
60namespace SkSL {
61
62class AutoRehydratorSymbolTable {
63public:
64 AutoRehydratorSymbolTable(Rehydrator* rehydrator)
65 : fRehydrator(rehydrator)
66 , fOldSymbols(fRehydrator->fSymbolTable) {
67 fRehydrator->fSymbolTable = fRehydrator->symbolTable();
68 }
69
70 ~AutoRehydratorSymbolTable() {
71 fRehydrator->fSymbolTable = std::move(fOldSymbols);
72 }
73
74private:
75 Rehydrator* fRehydrator;
76 std::shared_ptr<SymbolTable> fOldSymbols;
77};
78
John Stiles10d39d92021-05-04 16:13:14 -040079Rehydrator::Rehydrator(const Context* context, std::shared_ptr<SymbolTable> symbolTable,
John Stiles7c3515b2020-10-16 18:38:39 -040080 const uint8_t* src, size_t length)
81 : fContext(*context)
John Stiles7c3515b2020-10-16 18:38:39 -040082 , fSymbolTable(std::move(symbolTable))
83 , fStart(src)
84 SkDEBUGCODE(, fEnd(fStart + length)) {
85 SkASSERT(fSymbolTable);
86 SkASSERT(fSymbolTable->isBuiltin());
87 // skip past string data
88 fIP = fStart;
89 fIP += this->readU16();
90}
91
Ethan Nicholasc18bb512020-07-28 14:46:53 -040092Layout Rehydrator::layout() {
93 switch (this->readU8()) {
94 case kBuiltinLayout_Command: {
95 Layout result;
96 result.fBuiltin = this->readS16();
97 return result;
98 }
99 case kDefaultLayout_Command:
100 return Layout();
101 case kLayout_Command: {
102 int flags = this->readU32();
103 int location = this->readS8();
104 int offset = this->readS8();
105 int binding = this->readS8();
106 int index = this->readS8();
107 int set = this->readS8();
108 int builtin = this->readS16();
109 int inputAttachmentIndex = this->readS8();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400110 int primitive = this->readS8();
111 int maxVertices = this->readS8();
112 int invocations = this->readS8();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400113 return Layout(flags, location, offset, binding, index, set, builtin,
Brian Osman4717fbb2021-02-23 13:12:09 -0500114 inputAttachmentIndex, (Layout::Primitive)primitive, maxVertices,
Brian Osman8c264792021-07-01 16:41:27 -0400115 invocations);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400116 }
117 default:
118 SkASSERT(false);
119 return Layout();
120 }
121}
122
123Modifiers Rehydrator::modifiers() {
124 switch (this->readU8()) {
125 case kDefaultModifiers_Command:
126 return Modifiers();
127 case kModifiers8Bit_Command: {
128 Layout l = this->layout();
129 int flags = this->readU8();
130 return Modifiers(l, flags);
131 }
132 case kModifiers_Command: {
133 Layout l = this->layout();
134 int flags = this->readS32();
135 return Modifiers(l, flags);
136 }
137 default:
138 SkASSERT(false);
139 return Modifiers();
140 }
141}
142
Brian Osman5bf3e202020-10-13 10:34:18 -0400143const Symbol* Rehydrator::symbol() {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400144 int kind = this->readU8();
145 switch (kind) {
146 case kArrayType_Command: {
147 uint16_t id = this->readU16();
148 const Type* componentType = this->type();
Brian Osmane8c26082020-10-01 17:22:45 -0400149 int8_t count = this->readS8();
Ethan Nicholasd2e09602021-06-10 11:21:59 -0400150 String name(componentType->name());
Brian Osmane8c26082020-10-01 17:22:45 -0400151 if (count == Type::kUnsizedArray) {
152 name += "[]";
153 } else {
154 name += "[" + to_string(count) + "]";
155 }
Ethan Nicholas27f06eb2021-07-26 16:39:40 -0400156 skstd::string_view nameChars(*fSymbolTable->takeOwnershipOfString(std::move(name)));
Brian Osman5bf3e202020-10-13 10:34:18 -0400157 const Type* result = fSymbolTable->takeOwnershipOfSymbol(
Ethan Nicholas27f06eb2021-07-26 16:39:40 -0400158 Type::MakeArrayType(nameChars, *componentType, count));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400159 this->addSymbol(id, result);
160 return result;
161 }
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400162 case kFunctionDeclaration_Command: {
163 uint16_t id = this->readU16();
164 Modifiers modifiers = this->modifiers();
Ethan Nicholas962dec42021-06-10 13:06:39 -0400165 skstd::string_view name = this->readString();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400166 int parameterCount = this->readU8();
Brian Osman5bf3e202020-10-13 10:34:18 -0400167 std::vector<const Variable*> parameters;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400168 parameters.reserve(parameterCount);
169 for (int i = 0; i < parameterCount; ++i) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400170 parameters.push_back(this->symbolRef<Variable>(Symbol::Kind::kVariable));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400171 }
172 const Type* returnType = this->type();
Brian Osman5bf3e202020-10-13 10:34:18 -0400173 const FunctionDeclaration* result =
John Stiles3ae071e2020-08-05 15:29:29 -0400174 fSymbolTable->takeOwnershipOfSymbol(std::make_unique<FunctionDeclaration>(
John Stilesf2872e62021-05-04 11:38:43 -0400175 /*offset=*/-1,
176 this->modifiersPool().add(modifiers),
177 name,
178 std::move(parameters),
179 returnType,
180 /*builtin=*/true));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400181 this->addSymbol(id, result);
182 return result;
183 }
184 case kField_Command: {
Ethan Nicholase6592142020-09-08 10:22:09 -0400185 const Variable* owner = this->symbolRef<Variable>(Symbol::Kind::kVariable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400186 uint8_t index = this->readU8();
Brian Osman5bf3e202020-10-13 10:34:18 -0400187 const Field* result = fSymbolTable->takeOwnershipOfSymbol(
Ethan Nicholase2c49992020-10-05 11:49:11 -0400188 std::make_unique<Field>(/*offset=*/-1, owner, index));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400189 return result;
190 }
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400191 case kStructType_Command: {
192 uint16_t id = this->readU16();
Ethan Nicholasd2e09602021-06-10 11:21:59 -0400193 String name(this->readString());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400194 uint8_t fieldCount = this->readU8();
195 std::vector<Type::Field> fields;
196 fields.reserve(fieldCount);
197 for (int i = 0; i < fieldCount; ++i) {
198 Modifiers m = this->modifiers();
Ethan Nicholas962dec42021-06-10 13:06:39 -0400199 skstd::string_view fieldName = this->readString();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400200 const Type* type = this->type();
John Stilesf621e232020-08-25 13:33:02 -0400201 fields.emplace_back(m, fieldName, type);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400202 }
Ethan Nicholas27f06eb2021-07-26 16:39:40 -0400203 skstd::string_view nameChars(*fSymbolTable->takeOwnershipOfString(std::move(name)));
Brian Osman5bf3e202020-10-13 10:34:18 -0400204 const Type* result = fSymbolTable->takeOwnershipOfSymbol(
Ethan Nicholas27f06eb2021-07-26 16:39:40 -0400205 Type::MakeStructType(/*offset=*/-1, nameChars, std::move(fields)));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400206 this->addSymbol(id, result);
207 return result;
208 }
209 case kSymbolRef_Command: {
210 uint16_t id = this->readU16();
211 SkASSERT(fSymbols.size() > id);
212 return fSymbols[id];
213 }
John Stiles49a547f2020-10-06 16:14:37 -0400214 case kSymbolAlias_Command: {
215 uint16_t id = this->readU16();
Ethan Nicholas962dec42021-06-10 13:06:39 -0400216 skstd::string_view name = this->readString();
Brian Osman5bf3e202020-10-13 10:34:18 -0400217 const Symbol* origSymbol = this->symbol();
218 const SymbolAlias* symbolAlias = fSymbolTable->takeOwnershipOfSymbol(
John Stiles49a547f2020-10-06 16:14:37 -0400219 std::make_unique<SymbolAlias>(/*offset=*/-1, name, origSymbol));
220 this->addSymbol(id, symbolAlias);
221 return symbolAlias;
222 }
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400223 case kSystemType_Command: {
224 uint16_t id = this->readU16();
Ethan Nicholas962dec42021-06-10 13:06:39 -0400225 skstd::string_view name = this->readString();
Brian Osman5bf3e202020-10-13 10:34:18 -0400226 const Symbol* result = (*fSymbolTable)[name];
Ethan Nicholase6592142020-09-08 10:22:09 -0400227 SkASSERT(result && result->kind() == Symbol::Kind::kType);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400228 this->addSymbol(id, result);
229 return result;
230 }
231 case kUnresolvedFunction_Command: {
232 uint16_t id = this->readU16();
233 int length = this->readU8();
234 std::vector<const FunctionDeclaration*> functions;
235 functions.reserve(length);
236 for (int i = 0; i < length; ++i) {
237 const Symbol* f = this->symbol();
Ethan Nicholase6592142020-09-08 10:22:09 -0400238 SkASSERT(f && f->kind() == Symbol::Kind::kFunctionDeclaration);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400239 functions.push_back((const FunctionDeclaration*) f);
240 }
Brian Osman5bf3e202020-10-13 10:34:18 -0400241 const UnresolvedFunction* result = fSymbolTable->takeOwnershipOfSymbol(
John Stiles3ae071e2020-08-05 15:29:29 -0400242 std::make_unique<UnresolvedFunction>(std::move(functions)));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400243 this->addSymbol(id, result);
244 return result;
245 }
246 case kVariable_Command: {
247 uint16_t id = this->readU16();
John Stilesf2872e62021-05-04 11:38:43 -0400248 const Modifiers* m = this->modifiersPool().add(this->modifiers());
Ethan Nicholas962dec42021-06-10 13:06:39 -0400249 skstd::string_view name = this->readString();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400250 const Type* type = this->type();
251 Variable::Storage storage = (Variable::Storage) this->readU8();
Brian Osman5bf3e202020-10-13 10:34:18 -0400252 const Variable* result = fSymbolTable->takeOwnershipOfSymbol(std::make_unique<Variable>(
Brian Osman3887a012020-09-30 13:22:27 -0400253 /*offset=*/-1, m, name, type, /*builtin=*/true, storage));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400254 this->addSymbol(id, result);
255 return result;
256 }
257 default:
258 printf("unsupported symbol %d\n", kind);
259 SkASSERT(false);
260 return nullptr;
261 }
262}
263
264const Type* Rehydrator::type() {
265 const Symbol* result = this->symbol();
Ethan Nicholase6592142020-09-08 10:22:09 -0400266 SkASSERT(result->kind() == Symbol::Kind::kType);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400267 return (const Type*) result;
268}
269
270std::vector<std::unique_ptr<ProgramElement>> Rehydrator::elements() {
271 SkDEBUGCODE(uint8_t command = )this->readU8();
272 SkASSERT(command == kElements_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400273 std::vector<std::unique_ptr<ProgramElement>> result;
John Stiles1ea7f542020-11-02 13:07:23 -0500274 while (std::unique_ptr<ProgramElement> elem = this->element()) {
275 result.push_back(std::move(elem));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400276 }
277 return result;
278}
279
280std::unique_ptr<ProgramElement> Rehydrator::element() {
281 int kind = this->readU8();
282 switch (kind) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400283 case Rehydrator::kFunctionDefinition_Command: {
284 const FunctionDeclaration* decl = this->symbolRef<FunctionDeclaration>(
Ethan Nicholase6592142020-09-08 10:22:09 -0400285 Symbol::Kind::kFunctionDeclaration);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400286 std::unique_ptr<Statement> body = this->statement();
John Stilesb8e010c2020-08-11 18:05:39 -0400287 std::unordered_set<const FunctionDeclaration*> refs;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400288 uint8_t refCount = this->readU8();
289 for (int i = 0; i < refCount; ++i) {
290 refs.insert(this->symbolRef<FunctionDeclaration>(
Ethan Nicholase6592142020-09-08 10:22:09 -0400291 Symbol::Kind::kFunctionDeclaration));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400292 }
John Stiles607d36b2020-10-19 15:00:01 -0400293 auto result = std::make_unique<FunctionDefinition>(/*offset=*/-1, decl,
294 /*builtin=*/true, std::move(body),
295 std::move(refs));
296 decl->setDefinition(result.get());
297 return std::move(result);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400298 }
299 case Rehydrator::kInterfaceBlock_Command: {
300 const Symbol* var = this->symbol();
John Stiles87ae34e2020-10-13 12:50:11 -0400301 SkASSERT(var && var->is<Variable>());
Ethan Nicholasd2e09602021-06-10 11:21:59 -0400302 String typeName(this->readString());
303 String instanceName(this->readString());
John Stilesd39aec02020-12-03 10:42:26 -0500304 int arraySize = this->readS8();
Ethan Nicholasd2e09602021-06-10 11:21:59 -0400305 return std::make_unique<InterfaceBlock>(/*offset=*/-1, &var->as<Variable>(),
306 std::move(typeName), std::move(instanceName),
307 arraySize, nullptr);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400308 }
309 case Rehydrator::kVarDeclarations_Command: {
Brian Osmanc0213602020-10-06 14:43:32 -0400310 std::unique_ptr<Statement> decl = this->statement();
Ethan Nicholas624a5292021-04-16 14:54:43 -0400311 return std::make_unique<GlobalVarDeclaration>(std::move(decl));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400312 }
John Stilesdc75a972020-11-25 16:24:55 -0500313 case Rehydrator::kStructDefinition_Command: {
314 const Symbol* type = this->symbol();
315 SkASSERT(type && type->is<Type>());
316 return std::make_unique<StructDefinition>(/*offset=*/-1, type->as<Type>());
317 }
John Stiles1ea7f542020-11-02 13:07:23 -0500318 case Rehydrator::kElementsComplete_Command:
319 return nullptr;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400320 default:
John Stiles1ea7f542020-11-02 13:07:23 -0500321 SkDEBUGFAILF("unsupported element %d\n", kind);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400322 return nullptr;
323 }
324}
325
326std::unique_ptr<Statement> Rehydrator::statement() {
327 int kind = this->readU8();
328 switch (kind) {
329 case Rehydrator::kBlock_Command: {
330 AutoRehydratorSymbolTable symbols(this);
331 int count = this->readU8();
John Stiles8f2a0cf2020-10-13 12:48:21 -0400332 StatementArray statements;
John Stilesf4bda742020-10-14 16:57:41 -0400333 statements.reserve_back(count);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400334 for (int i = 0; i < count; ++i) {
335 statements.push_back(this->statement());
336 }
337 bool isScope = this->readU8();
John Stilesbf16b6c2021-03-12 19:24:31 -0500338 return Block::Make(/*offset=*/-1, std::move(statements), fSymbolTable, isScope);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400339 }
340 case Rehydrator::kBreak_Command:
John Stilesa0c04d62021-03-11 23:07:24 -0500341 return BreakStatement::Make(/*offset=*/-1);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400342 case Rehydrator::kContinue_Command:
John Stilesa0c04d62021-03-11 23:07:24 -0500343 return ContinueStatement::Make(/*offset=*/-1);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400344 case Rehydrator::kDiscard_Command:
John Stilesa0c04d62021-03-11 23:07:24 -0500345 return DiscardStatement::Make(/*offset=*/-1);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400346 case Rehydrator::kDo_Command: {
347 std::unique_ptr<Statement> stmt = this->statement();
348 std::unique_ptr<Expression> expr = this->expression();
John Stilesea5822e2021-02-26 11:18:20 -0500349 return DoStatement::Make(fContext, std::move(stmt), std::move(expr));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400350 }
351 case Rehydrator::kExpressionStatement_Command: {
352 std::unique_ptr<Expression> expr = this->expression();
John Stiles3e5871c2021-02-25 20:52:03 -0500353 return ExpressionStatement::Make(fContext, std::move(expr));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400354 }
355 case Rehydrator::kFor_Command: {
356 std::unique_ptr<Statement> initializer = this->statement();
357 std::unique_ptr<Expression> test = this->expression();
358 std::unique_ptr<Expression> next = this->expression();
359 std::unique_ptr<Statement> body = this->statement();
360 std::shared_ptr<SymbolTable> symbols = this->symbolTable();
John Stiles23521a82021-03-02 17:02:51 -0500361 return ForStatement::Make(fContext, /*offset=*/-1, std::move(initializer),
362 std::move(test), std::move(next), std::move(body),
363 std::move(symbols));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400364 }
365 case Rehydrator::kIf_Command: {
366 bool isStatic = this->readU8();
367 std::unique_ptr<Expression> test = this->expression();
368 std::unique_ptr<Statement> ifTrue = this->statement();
369 std::unique_ptr<Statement> ifFalse = this->statement();
John Stilescf3059e2021-02-25 14:27:02 -0500370 return IfStatement::Make(fContext, /*offset=*/-1, isStatic, std::move(test),
371 std::move(ifTrue), std::move(ifFalse));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400372 }
John Stiles98c1f822020-09-09 14:18:53 -0400373 case Rehydrator::kInlineMarker_Command: {
374 const FunctionDeclaration* funcDecl = this->symbolRef<FunctionDeclaration>(
375 Symbol::Kind::kFunctionDeclaration);
John Stilesa0c04d62021-03-11 23:07:24 -0500376 return InlineMarker::Make(funcDecl);
John Stiles98c1f822020-09-09 14:18:53 -0400377 }
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400378 case Rehydrator::kReturn_Command: {
379 std::unique_ptr<Expression> expr = this->expression();
John Stilesa0c04d62021-03-11 23:07:24 -0500380 return ReturnStatement::Make(/*offset=*/-1, std::move(expr));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400381 }
382 case Rehydrator::kSwitch_Command: {
383 bool isStatic = this->readU8();
384 AutoRehydratorSymbolTable symbols(this);
385 std::unique_ptr<Expression> expr = this->expression();
386 int caseCount = this->readU8();
John Stilesb23a64b2021-03-11 08:27:59 -0500387 StatementArray cases;
388 cases.reserve_back(caseCount);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400389 for (int i = 0; i < caseCount; ++i) {
390 std::unique_ptr<Expression> value = this->expression();
John Stilesc3ce43b2021-03-09 15:37:01 -0500391 std::unique_ptr<Statement> statement = this->statement();
John Stiles8f2a0cf2020-10-13 12:48:21 -0400392 cases.push_back(std::make_unique<SwitchCase>(/*offset=*/-1, std::move(value),
John Stilesc3ce43b2021-03-09 15:37:01 -0500393 std::move(statement)));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400394 }
John Stiles23521a82021-03-02 17:02:51 -0500395 return SwitchStatement::Make(fContext, /*offset=*/-1, isStatic, std::move(expr),
396 std::move(cases), fSymbolTable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400397 }
398 case Rehydrator::kVarDeclaration_Command: {
Ethan Nicholase6592142020-09-08 10:22:09 -0400399 Variable* var = this->symbolRef<Variable>(Symbol::Kind::kVariable);
Brian Osmanc0213602020-10-06 14:43:32 -0400400 const Type* baseType = this->type();
John Stiles62a56462020-12-03 10:41:58 -0500401 int arraySize = this->readS8();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400402 std::unique_ptr<Expression> value = this->expression();
John Stilese67bd132021-03-19 18:39:25 -0400403 return VarDeclaration::Make(fContext, var, baseType, arraySize, std::move(value));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400404 }
405 case Rehydrator::kVoid_Command:
406 return nullptr;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400407 default:
408 printf("unsupported statement %d\n", kind);
409 SkASSERT(false);
410 return nullptr;
411 }
412}
413
John Stilesd8eb8752021-04-01 11:49:10 -0400414ExpressionArray Rehydrator::expressionArray() {
415 uint8_t count = this->readU8();
416 ExpressionArray array;
417 array.reserve_back(count);
418 for (int i = 0; i < count; ++i) {
419 array.push_back(this->expression());
420 }
421 return array;
422}
423
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400424std::unique_ptr<Expression> Rehydrator::expression() {
425 int kind = this->readU8();
426 switch (kind) {
427 case Rehydrator::kBinary_Command: {
428 std::unique_ptr<Expression> left = this->expression();
429 Token::Kind op = (Token::Kind) this->readU8();
430 std::unique_ptr<Expression> right = this->expression();
John Stilese2aec432021-03-01 09:27:48 -0500431 return BinaryExpression::Make(fContext, std::move(left), op, std::move(right));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400432 }
433 case Rehydrator::kBoolLiteral_Command: {
434 bool value = this->readU8();
John Stiles9ce80f72021-03-11 22:35:19 -0500435 return BoolLiteral::Make(fContext, /*offset=*/-1, value);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400436 }
John Stiles4118f142021-04-01 16:42:35 -0400437 case Rehydrator::kConstructorArray_Command: {
438 const Type* type = this->type();
439 return ConstructorArray::Make(fContext, /*offset=*/-1, *type, this->expressionArray());
440 }
John Stiles8cad6372021-04-07 12:31:13 -0400441 case Rehydrator::kConstructorCompound_Command: {
John Stiles2bec8ab2021-04-06 18:40:04 -0400442 const Type* type = this->type();
John Stiles8cad6372021-04-07 12:31:13 -0400443 return ConstructorCompound::Make(fContext, /*offset=*/-1, *type,
John Stiles2bec8ab2021-04-06 18:40:04 -0400444 this->expressionArray());
445 }
John Stilese1182782021-03-30 22:09:37 -0400446 case Rehydrator::kConstructorDiagonalMatrix_Command: {
447 const Type* type = this->type();
John Stilesd8eb8752021-04-01 11:49:10 -0400448 ExpressionArray args = this->expressionArray();
449 SkASSERT(args.size() == 1);
John Stilese1182782021-03-30 22:09:37 -0400450 return ConstructorDiagonalMatrix::Make(fContext, /*offset=*/-1, *type,
John Stilesd8eb8752021-04-01 11:49:10 -0400451 std::move(args[0]));
John Stilese1182782021-03-30 22:09:37 -0400452 }
John Stiles5abb9e12021-04-06 13:47:19 -0400453 case Rehydrator::kConstructorMatrixResize_Command: {
454 const Type* type = this->type();
455 ExpressionArray args = this->expressionArray();
456 SkASSERT(args.size() == 1);
457 return ConstructorMatrixResize::Make(fContext, /*offset=*/-1, *type,
458 std::move(args[0]));
459 }
John Stilesfd7252f2021-04-04 22:24:40 -0400460 case Rehydrator::kConstructorScalarCast_Command: {
461 const Type* type = this->type();
462 ExpressionArray args = this->expressionArray();
463 SkASSERT(args.size() == 1);
464 return ConstructorScalarCast::Make(fContext, /*offset=*/-1, *type, std::move(args[0]));
465 }
John Stiles2938eea2021-04-01 18:58:25 -0400466 case Rehydrator::kConstructorSplat_Command: {
467 const Type* type = this->type();
468 ExpressionArray args = this->expressionArray();
469 SkASSERT(args.size() == 1);
470 return ConstructorSplat::Make(fContext, /*offset=*/-1, *type, std::move(args[0]));
471 }
John Stilesd47330f2021-04-08 23:25:52 -0400472 case Rehydrator::kConstructorStruct_Command: {
473 const Type* type = this->type();
474 return ConstructorStruct::Make(fContext, /*offset=*/-1, *type, this->expressionArray());
475 }
John Stiles8cad6372021-04-07 12:31:13 -0400476 case Rehydrator::kConstructorCompoundCast_Command: {
John Stilesb14a8192021-04-05 11:40:46 -0400477 const Type* type = this->type();
478 ExpressionArray args = this->expressionArray();
479 SkASSERT(args.size() == 1);
John Stiles8cad6372021-04-07 12:31:13 -0400480 return ConstructorCompoundCast::Make(fContext,/*offset=*/-1, *type, std::move(args[0]));
John Stilesb14a8192021-04-05 11:40:46 -0400481 }
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400482 case Rehydrator::kFieldAccess_Command: {
483 std::unique_ptr<Expression> base = this->expression();
484 int index = this->readU8();
485 FieldAccess::OwnerKind ownerKind = (FieldAccess::OwnerKind) this->readU8();
John Stiles06d600f2021-03-08 09:18:21 -0500486 return FieldAccess::Make(fContext, std::move(base), index, ownerKind);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400487 }
488 case Rehydrator::kFloatLiteral_Command: {
Brian Osmanfb964a42020-11-18 10:45:52 -0500489 const Type* type = this->type();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400490 FloatIntUnion u;
491 u.fInt = this->readS32();
John Stiles9ce80f72021-03-11 22:35:19 -0500492 return FloatLiteral::Make(/*offset=*/-1, u.fFloat, type);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400493 }
494 case Rehydrator::kFunctionCall_Command: {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400495 const Type* type = this->type();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400496 const FunctionDeclaration* f = this->symbolRef<FunctionDeclaration>(
Ethan Nicholase6592142020-09-08 10:22:09 -0400497 Symbol::Kind::kFunctionDeclaration);
John Stilesd8eb8752021-04-01 11:49:10 -0400498 ExpressionArray args = this->expressionArray();
John Stilescd7ba502021-03-19 10:54:59 -0400499 return FunctionCall::Make(fContext, /*offset=*/-1, type, *f, std::move(args));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400500 }
501 case Rehydrator::kIndex_Command: {
502 std::unique_ptr<Expression> base = this->expression();
503 std::unique_ptr<Expression> index = this->expression();
John Stiles51d33982021-03-08 09:18:07 -0500504 return IndexExpression::Make(fContext, std::move(base), std::move(index));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400505 }
506 case Rehydrator::kIntLiteral_Command: {
Brian Osmanfb964a42020-11-18 10:45:52 -0500507 const Type* type = this->type();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400508 int value = this->readS32();
John Stiles9ce80f72021-03-11 22:35:19 -0500509 return IntLiteral::Make(/*offset=*/-1, value, type);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400510 }
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400511 case Rehydrator::kPostfix_Command: {
512 Token::Kind op = (Token::Kind) this->readU8();
513 std::unique_ptr<Expression> operand = this->expression();
John Stiles52d3b012021-02-26 15:56:48 -0500514 return PostfixExpression::Make(fContext, std::move(operand), op);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400515 }
516 case Rehydrator::kPrefix_Command: {
517 Token::Kind op = (Token::Kind) this->readU8();
518 std::unique_ptr<Expression> operand = this->expression();
John Stilesb0eb20f2021-02-26 15:29:33 -0500519 return PrefixExpression::Make(fContext, op, std::move(operand));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400520 }
521 case Rehydrator::kSetting_Command: {
Ethan Nicholasd2e09602021-06-10 11:21:59 -0400522 String name(this->readString());
John Stiles23521a82021-03-02 17:02:51 -0500523 return Setting::Convert(fContext, /*offset=*/-1, name);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400524 }
525 case Rehydrator::kSwizzle_Command: {
526 std::unique_ptr<Expression> base = this->expression();
527 int count = this->readU8();
John Stiles750109b2020-10-30 13:45:46 -0400528 ComponentArray components;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400529 for (int i = 0; i < count; ++i) {
530 components.push_back(this->readU8());
531 }
John Stiles23521a82021-03-02 17:02:51 -0500532 return Swizzle::Make(fContext, std::move(base), components);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400533 }
534 case Rehydrator::kTernary_Command: {
535 std::unique_ptr<Expression> test = this->expression();
536 std::unique_ptr<Expression> ifTrue = this->expression();
537 std::unique_ptr<Expression> ifFalse = this->expression();
John Stiles90518f72021-02-26 20:44:54 -0500538 return TernaryExpression::Make(fContext, std::move(test),
539 std::move(ifTrue), std::move(ifFalse));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400540 }
541 case Rehydrator::kVariableReference_Command: {
Ethan Nicholase6592142020-09-08 10:22:09 -0400542 const Variable* var = this->symbolRef<Variable>(Symbol::Kind::kVariable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400543 VariableReference::RefKind refKind = (VariableReference::RefKind) this->readU8();
John Stilesa94e0262021-04-27 17:29:59 -0400544 return VariableReference::Make(/*offset=*/-1, var, refKind);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400545 }
546 case Rehydrator::kVoid_Command:
547 return nullptr;
548 default:
549 printf("unsupported expression %d\n", kind);
550 SkASSERT(false);
551 return nullptr;
552 }
553}
554
Brian Osman1313d1a2020-09-08 10:34:30 -0400555std::shared_ptr<SymbolTable> Rehydrator::symbolTable(bool inherit) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400556 int command = this->readU8();
557 if (command == kVoid_Command) {
558 return nullptr;
559 }
560 SkASSERT(command == kSymbolTable_Command);
561 uint16_t ownedCount = this->readU16();
Brian Osman1313d1a2020-09-08 10:34:30 -0400562 std::shared_ptr<SymbolTable> oldTable = fSymbolTable;
John Stiles7c3515b2020-10-16 18:38:39 -0400563 std::shared_ptr<SymbolTable> result =
564 inherit ? std::make_shared<SymbolTable>(fSymbolTable, /*builtin=*/true)
John Stilesf2872e62021-05-04 11:38:43 -0400565 : std::make_shared<SymbolTable>(this->errorReporter(), /*builtin=*/true);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400566 fSymbolTable = result;
Brian Osman5bf3e202020-10-13 10:34:18 -0400567 std::vector<const Symbol*> ownedSymbols;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400568 ownedSymbols.reserve(ownedCount);
569 for (int i = 0; i < ownedCount; ++i) {
570 ownedSymbols.push_back(this->symbol());
571 }
572 uint16_t symbolCount = this->readU16();
Ethan Nicholas962dec42021-06-10 13:06:39 -0400573 std::vector<std::pair<skstd::string_view, int>> symbols;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400574 symbols.reserve(symbolCount);
575 for (int i = 0; i < symbolCount; ++i) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400576 int index = this->readU16();
John Stilesb8cc6652020-10-08 09:12:07 -0400577 fSymbolTable->addWithoutOwnership(ownedSymbols[index]);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400578 }
Brian Osman1313d1a2020-09-08 10:34:30 -0400579 fSymbolTable = oldTable;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400580 return result;
581}
582
John Stilesa6841be2020-08-06 14:11:56 -0400583} // namespace SkSL