blob: e3eb78487d990679577b636195f28d38fd215c6b [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 Stilese1182782021-03-30 22:09:37 -040020#include "src/sksl/ir/SkSLConstructorDiagonalMatrix.h"
John Stiles2938eea2021-04-01 18:58:25 -040021#include "src/sksl/ir/SkSLConstructorSplat.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040022#include "src/sksl/ir/SkSLContinueStatement.h"
23#include "src/sksl/ir/SkSLDiscardStatement.h"
24#include "src/sksl/ir/SkSLDoStatement.h"
25#include "src/sksl/ir/SkSLEnum.h"
26#include "src/sksl/ir/SkSLExpression.h"
27#include "src/sksl/ir/SkSLExpressionStatement.h"
28#include "src/sksl/ir/SkSLField.h"
29#include "src/sksl/ir/SkSLFieldAccess.h"
30#include "src/sksl/ir/SkSLFloatLiteral.h"
31#include "src/sksl/ir/SkSLForStatement.h"
32#include "src/sksl/ir/SkSLFunctionCall.h"
33#include "src/sksl/ir/SkSLFunctionDeclaration.h"
34#include "src/sksl/ir/SkSLFunctionDefinition.h"
35#include "src/sksl/ir/SkSLIfStatement.h"
36#include "src/sksl/ir/SkSLIndexExpression.h"
John Stiles98c1f822020-09-09 14:18:53 -040037#include "src/sksl/ir/SkSLInlineMarker.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040038#include "src/sksl/ir/SkSLIntLiteral.h"
39#include "src/sksl/ir/SkSLInterfaceBlock.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040040#include "src/sksl/ir/SkSLPostfixExpression.h"
41#include "src/sksl/ir/SkSLPrefixExpression.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040042#include "src/sksl/ir/SkSLReturnStatement.h"
43#include "src/sksl/ir/SkSLSetting.h"
John Stilesdc75a972020-11-25 16:24:55 -050044#include "src/sksl/ir/SkSLStructDefinition.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040045#include "src/sksl/ir/SkSLSwitchCase.h"
46#include "src/sksl/ir/SkSLSwitchStatement.h"
47#include "src/sksl/ir/SkSLSwizzle.h"
John Stiles49a547f2020-10-06 16:14:37 -040048#include "src/sksl/ir/SkSLSymbolAlias.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040049#include "src/sksl/ir/SkSLSymbolTable.h"
50#include "src/sksl/ir/SkSLTernaryExpression.h"
51#include "src/sksl/ir/SkSLType.h"
52#include "src/sksl/ir/SkSLUnresolvedFunction.h"
53#include "src/sksl/ir/SkSLVarDeclarations.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040054#include "src/sksl/ir/SkSLVariable.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040055
56namespace SkSL {
57
58class AutoRehydratorSymbolTable {
59public:
60 AutoRehydratorSymbolTable(Rehydrator* rehydrator)
61 : fRehydrator(rehydrator)
62 , fOldSymbols(fRehydrator->fSymbolTable) {
63 fRehydrator->fSymbolTable = fRehydrator->symbolTable();
64 }
65
66 ~AutoRehydratorSymbolTable() {
67 fRehydrator->fSymbolTable = std::move(fOldSymbols);
68 }
69
70private:
71 Rehydrator* fRehydrator;
72 std::shared_ptr<SymbolTable> fOldSymbols;
73};
74
John Stiles7c3515b2020-10-16 18:38:39 -040075Rehydrator::Rehydrator(const Context* context, ModifiersPool* modifiers,
76 std::shared_ptr<SymbolTable> symbolTable, ErrorReporter* errorReporter,
77 const uint8_t* src, size_t length)
78 : fContext(*context)
79 , fModifiers(*modifiers)
80 , fErrors(errorReporter)
81 , fSymbolTable(std::move(symbolTable))
82 , fStart(src)
83 SkDEBUGCODE(, fEnd(fStart + length)) {
84 SkASSERT(fSymbolTable);
85 SkASSERT(fSymbolTable->isBuiltin());
86 // skip past string data
87 fIP = fStart;
88 fIP += this->readU16();
89}
90
Ethan Nicholasc18bb512020-07-28 14:46:53 -040091Layout Rehydrator::layout() {
92 switch (this->readU8()) {
93 case kBuiltinLayout_Command: {
94 Layout result;
95 result.fBuiltin = this->readS16();
96 return result;
97 }
98 case kDefaultLayout_Command:
99 return Layout();
100 case kLayout_Command: {
101 int flags = this->readU32();
102 int location = this->readS8();
103 int offset = this->readS8();
104 int binding = this->readS8();
105 int index = this->readS8();
106 int set = this->readS8();
107 int builtin = this->readS16();
108 int inputAttachmentIndex = this->readS8();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400109 int primitive = this->readS8();
110 int maxVertices = this->readS8();
111 int invocations = this->readS8();
112 StringFragment marker = this->readString();
113 StringFragment when = this->readString();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400114 int ctype = this->readS8();
115 return Layout(flags, location, offset, binding, index, set, builtin,
Brian Osman4717fbb2021-02-23 13:12:09 -0500116 inputAttachmentIndex, (Layout::Primitive)primitive, maxVertices,
Brian Osmanba7ef322021-02-23 13:37:22 -0500117 invocations, marker, when, (Layout::CType)ctype);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400118 }
119 default:
120 SkASSERT(false);
121 return Layout();
122 }
123}
124
125Modifiers Rehydrator::modifiers() {
126 switch (this->readU8()) {
127 case kDefaultModifiers_Command:
128 return Modifiers();
129 case kModifiers8Bit_Command: {
130 Layout l = this->layout();
131 int flags = this->readU8();
132 return Modifiers(l, flags);
133 }
134 case kModifiers_Command: {
135 Layout l = this->layout();
136 int flags = this->readS32();
137 return Modifiers(l, flags);
138 }
139 default:
140 SkASSERT(false);
141 return Modifiers();
142 }
143}
144
Brian Osman5bf3e202020-10-13 10:34:18 -0400145const Symbol* Rehydrator::symbol() {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400146 int kind = this->readU8();
147 switch (kind) {
148 case kArrayType_Command: {
149 uint16_t id = this->readU16();
150 const Type* componentType = this->type();
Brian Osmane8c26082020-10-01 17:22:45 -0400151 int8_t count = this->readS8();
152 String name = componentType->name();
153 if (count == Type::kUnsizedArray) {
154 name += "[]";
155 } else {
156 name += "[" + to_string(count) + "]";
157 }
Brian Osman5bf3e202020-10-13 10:34:18 -0400158 const Type* result = fSymbolTable->takeOwnershipOfSymbol(
John Stilesad2d4942020-12-11 16:55:58 -0500159 Type::MakeArrayType(name, *componentType, count));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400160 this->addSymbol(id, result);
161 return result;
162 }
163 case kEnumType_Command: {
164 uint16_t id = this->readU16();
165 StringFragment name = this->readString();
John Stilese6c67c52021-01-15 12:01:00 -0500166 const Type* result = fSymbolTable->takeOwnershipOfSymbol(Type::MakeEnumType(name));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400167 this->addSymbol(id, result);
168 return result;
169 }
170 case kFunctionDeclaration_Command: {
171 uint16_t id = this->readU16();
172 Modifiers modifiers = this->modifiers();
173 StringFragment name = this->readString();
174 int parameterCount = this->readU8();
Brian Osman5bf3e202020-10-13 10:34:18 -0400175 std::vector<const Variable*> parameters;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400176 parameters.reserve(parameterCount);
177 for (int i = 0; i < parameterCount; ++i) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400178 parameters.push_back(this->symbolRef<Variable>(Symbol::Kind::kVariable));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400179 }
180 const Type* returnType = this->type();
Brian Osman5bf3e202020-10-13 10:34:18 -0400181 const FunctionDeclaration* result =
John Stiles3ae071e2020-08-05 15:29:29 -0400182 fSymbolTable->takeOwnershipOfSymbol(std::make_unique<FunctionDeclaration>(
John Stiles586df952020-11-12 18:27:13 -0500183 /*offset=*/-1, fModifiers.addToPool(modifiers), name,
Ethan Nicholased84b732020-10-08 11:45:44 -0400184 std::move(parameters), returnType, /*builtin=*/true));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400185 this->addSymbol(id, result);
186 return result;
187 }
188 case kField_Command: {
Ethan Nicholase6592142020-09-08 10:22:09 -0400189 const Variable* owner = this->symbolRef<Variable>(Symbol::Kind::kVariable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400190 uint8_t index = this->readU8();
Brian Osman5bf3e202020-10-13 10:34:18 -0400191 const Field* result = fSymbolTable->takeOwnershipOfSymbol(
Ethan Nicholase2c49992020-10-05 11:49:11 -0400192 std::make_unique<Field>(/*offset=*/-1, owner, index));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400193 return result;
194 }
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400195 case kStructType_Command: {
196 uint16_t id = this->readU16();
197 StringFragment name = this->readString();
198 uint8_t fieldCount = this->readU8();
199 std::vector<Type::Field> fields;
200 fields.reserve(fieldCount);
201 for (int i = 0; i < fieldCount; ++i) {
202 Modifiers m = this->modifiers();
John Stilesf621e232020-08-25 13:33:02 -0400203 StringFragment fieldName = this->readString();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400204 const Type* type = this->type();
John Stilesf621e232020-08-25 13:33:02 -0400205 fields.emplace_back(m, fieldName, type);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400206 }
Brian Osman5bf3e202020-10-13 10:34:18 -0400207 const Type* result = fSymbolTable->takeOwnershipOfSymbol(
John Stilesad2d4942020-12-11 16:55:58 -0500208 Type::MakeStructType(/*offset=*/-1, name, std::move(fields)));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400209 this->addSymbol(id, result);
210 return result;
211 }
212 case kSymbolRef_Command: {
213 uint16_t id = this->readU16();
214 SkASSERT(fSymbols.size() > id);
215 return fSymbols[id];
216 }
John Stiles49a547f2020-10-06 16:14:37 -0400217 case kSymbolAlias_Command: {
218 uint16_t id = this->readU16();
219 StringFragment name = this->readString();
Brian Osman5bf3e202020-10-13 10:34:18 -0400220 const Symbol* origSymbol = this->symbol();
221 const SymbolAlias* symbolAlias = fSymbolTable->takeOwnershipOfSymbol(
John Stiles49a547f2020-10-06 16:14:37 -0400222 std::make_unique<SymbolAlias>(/*offset=*/-1, name, origSymbol));
223 this->addSymbol(id, symbolAlias);
224 return symbolAlias;
225 }
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400226 case kSystemType_Command: {
227 uint16_t id = this->readU16();
228 StringFragment name = this->readString();
Brian Osman5bf3e202020-10-13 10:34:18 -0400229 const Symbol* result = (*fSymbolTable)[name];
Ethan Nicholase6592142020-09-08 10:22:09 -0400230 SkASSERT(result && result->kind() == Symbol::Kind::kType);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400231 this->addSymbol(id, result);
232 return result;
233 }
234 case kUnresolvedFunction_Command: {
235 uint16_t id = this->readU16();
236 int length = this->readU8();
237 std::vector<const FunctionDeclaration*> functions;
238 functions.reserve(length);
239 for (int i = 0; i < length; ++i) {
240 const Symbol* f = this->symbol();
Ethan Nicholase6592142020-09-08 10:22:09 -0400241 SkASSERT(f && f->kind() == Symbol::Kind::kFunctionDeclaration);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400242 functions.push_back((const FunctionDeclaration*) f);
243 }
Brian Osman5bf3e202020-10-13 10:34:18 -0400244 const UnresolvedFunction* result = fSymbolTable->takeOwnershipOfSymbol(
John Stiles3ae071e2020-08-05 15:29:29 -0400245 std::make_unique<UnresolvedFunction>(std::move(functions)));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400246 this->addSymbol(id, result);
247 return result;
248 }
249 case kVariable_Command: {
250 uint16_t id = this->readU16();
John Stiles586df952020-11-12 18:27:13 -0500251 const Modifiers* m = fModifiers.addToPool(this->modifiers());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400252 StringFragment name = this->readString();
253 const Type* type = this->type();
254 Variable::Storage storage = (Variable::Storage) this->readU8();
Brian Osman5bf3e202020-10-13 10:34:18 -0400255 const Variable* result = fSymbolTable->takeOwnershipOfSymbol(std::make_unique<Variable>(
Brian Osman3887a012020-09-30 13:22:27 -0400256 /*offset=*/-1, m, name, type, /*builtin=*/true, storage));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400257 this->addSymbol(id, result);
258 return result;
259 }
260 default:
261 printf("unsupported symbol %d\n", kind);
262 SkASSERT(false);
263 return nullptr;
264 }
265}
266
267const Type* Rehydrator::type() {
268 const Symbol* result = this->symbol();
Ethan Nicholase6592142020-09-08 10:22:09 -0400269 SkASSERT(result->kind() == Symbol::Kind::kType);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400270 return (const Type*) result;
271}
272
273std::vector<std::unique_ptr<ProgramElement>> Rehydrator::elements() {
274 SkDEBUGCODE(uint8_t command = )this->readU8();
275 SkASSERT(command == kElements_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400276 std::vector<std::unique_ptr<ProgramElement>> result;
John Stiles1ea7f542020-11-02 13:07:23 -0500277 while (std::unique_ptr<ProgramElement> elem = this->element()) {
278 result.push_back(std::move(elem));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400279 }
280 return result;
281}
282
283std::unique_ptr<ProgramElement> Rehydrator::element() {
284 int kind = this->readU8();
285 switch (kind) {
286 case Rehydrator::kEnum_Command: {
287 StringFragment typeName = this->readString();
Brian Osman1313d1a2020-09-08 10:34:30 -0400288 std::shared_ptr<SymbolTable> symbols = this->symbolTable(/*inherit=*/false);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400289 for (auto& s : symbols->fOwnedSymbols) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400290 SkASSERT(s->kind() == Symbol::Kind::kVariable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400291 Variable& v = (Variable&) *s;
292 int value = this->readS32();
Ethan Nicholas5b9b0db2021-01-21 13:12:01 -0500293 // enum variables aren't really 'declared', but we have to create a declaration to
294 // store the value
John Stiles9ce80f72021-03-11 22:35:19 -0500295 auto valueLiteral = IntLiteral::Make(fContext, /*offset=*/-1, value);
John Stilese67bd132021-03-19 18:39:25 -0400296 auto declaration = VarDeclaration::Make(fContext, &v, &v.type(), /*arraySize=*/0,
297 std::move(valueLiteral));
Ethan Nicholas5b9b0db2021-01-21 13:12:01 -0500298 symbols->takeOwnershipOfIRNode(std::move(declaration));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400299 }
John Stiles1c823672020-10-20 10:23:50 -0400300 return std::make_unique<Enum>(/*offset=*/-1, typeName, std::move(symbols),
301 /*isSharedWithCpp=*/true, /*isBuiltin=*/true);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400302 }
303 case Rehydrator::kFunctionDefinition_Command: {
304 const FunctionDeclaration* decl = this->symbolRef<FunctionDeclaration>(
Ethan Nicholase6592142020-09-08 10:22:09 -0400305 Symbol::Kind::kFunctionDeclaration);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400306 std::unique_ptr<Statement> body = this->statement();
John Stilesb8e010c2020-08-11 18:05:39 -0400307 std::unordered_set<const FunctionDeclaration*> refs;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400308 uint8_t refCount = this->readU8();
309 for (int i = 0; i < refCount; ++i) {
310 refs.insert(this->symbolRef<FunctionDeclaration>(
Ethan Nicholase6592142020-09-08 10:22:09 -0400311 Symbol::Kind::kFunctionDeclaration));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400312 }
John Stiles607d36b2020-10-19 15:00:01 -0400313 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 Nicholasc18bb512020-07-28 14:46:53 -0400318 }
319 case Rehydrator::kInterfaceBlock_Command: {
320 const Symbol* var = this->symbol();
John Stiles87ae34e2020-10-13 12:50:11 -0400321 SkASSERT(var && var->is<Variable>());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400322 StringFragment typeName = this->readString();
323 StringFragment instanceName = this->readString();
John Stilesd39aec02020-12-03 10:42:26 -0500324 int arraySize = this->readS8();
John Stiles87ae34e2020-10-13 12:50:11 -0400325 return std::make_unique<InterfaceBlock>(/*offset=*/-1, &var->as<Variable>(), typeName,
John Stilesd39aec02020-12-03 10:42:26 -0500326 instanceName, arraySize, nullptr);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400327 }
328 case Rehydrator::kVarDeclarations_Command: {
Brian Osmanc0213602020-10-06 14:43:32 -0400329 std::unique_ptr<Statement> decl = this->statement();
John Stiles1ea7f542020-11-02 13:07:23 -0500330 return std::make_unique<GlobalVarDeclaration>(/*offset=*/-1, std::move(decl));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400331 }
John Stilesdc75a972020-11-25 16:24:55 -0500332 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 Stiles1ea7f542020-11-02 13:07:23 -0500337 case Rehydrator::kElementsComplete_Command:
338 return nullptr;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400339 default:
John Stiles1ea7f542020-11-02 13:07:23 -0500340 SkDEBUGFAILF("unsupported element %d\n", kind);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400341 return nullptr;
342 }
343}
344
345std::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 Stiles8f2a0cf2020-10-13 12:48:21 -0400351 StatementArray statements;
John Stilesf4bda742020-10-14 16:57:41 -0400352 statements.reserve_back(count);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400353 for (int i = 0; i < count; ++i) {
354 statements.push_back(this->statement());
355 }
356 bool isScope = this->readU8();
John Stilesbf16b6c2021-03-12 19:24:31 -0500357 return Block::Make(/*offset=*/-1, std::move(statements), fSymbolTable, isScope);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400358 }
359 case Rehydrator::kBreak_Command:
John Stilesa0c04d62021-03-11 23:07:24 -0500360 return BreakStatement::Make(/*offset=*/-1);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400361 case Rehydrator::kContinue_Command:
John Stilesa0c04d62021-03-11 23:07:24 -0500362 return ContinueStatement::Make(/*offset=*/-1);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400363 case Rehydrator::kDiscard_Command:
John Stilesa0c04d62021-03-11 23:07:24 -0500364 return DiscardStatement::Make(/*offset=*/-1);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400365 case Rehydrator::kDo_Command: {
366 std::unique_ptr<Statement> stmt = this->statement();
367 std::unique_ptr<Expression> expr = this->expression();
John Stilesea5822e2021-02-26 11:18:20 -0500368 return DoStatement::Make(fContext, std::move(stmt), std::move(expr));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400369 }
370 case Rehydrator::kExpressionStatement_Command: {
371 std::unique_ptr<Expression> expr = this->expression();
John Stiles3e5871c2021-02-25 20:52:03 -0500372 return ExpressionStatement::Make(fContext, std::move(expr));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400373 }
374 case Rehydrator::kFor_Command: {
375 std::unique_ptr<Statement> initializer = this->statement();
376 std::unique_ptr<Expression> test = this->expression();
377 std::unique_ptr<Expression> next = this->expression();
378 std::unique_ptr<Statement> body = this->statement();
379 std::shared_ptr<SymbolTable> symbols = this->symbolTable();
John Stiles23521a82021-03-02 17:02:51 -0500380 return ForStatement::Make(fContext, /*offset=*/-1, std::move(initializer),
381 std::move(test), std::move(next), std::move(body),
382 std::move(symbols));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400383 }
384 case Rehydrator::kIf_Command: {
385 bool isStatic = this->readU8();
386 std::unique_ptr<Expression> test = this->expression();
387 std::unique_ptr<Statement> ifTrue = this->statement();
388 std::unique_ptr<Statement> ifFalse = this->statement();
John Stilescf3059e2021-02-25 14:27:02 -0500389 return IfStatement::Make(fContext, /*offset=*/-1, isStatic, std::move(test),
390 std::move(ifTrue), std::move(ifFalse));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400391 }
John Stiles98c1f822020-09-09 14:18:53 -0400392 case Rehydrator::kInlineMarker_Command: {
393 const FunctionDeclaration* funcDecl = this->symbolRef<FunctionDeclaration>(
394 Symbol::Kind::kFunctionDeclaration);
John Stilesa0c04d62021-03-11 23:07:24 -0500395 return InlineMarker::Make(funcDecl);
John Stiles98c1f822020-09-09 14:18:53 -0400396 }
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400397 case Rehydrator::kReturn_Command: {
398 std::unique_ptr<Expression> expr = this->expression();
John Stilesa0c04d62021-03-11 23:07:24 -0500399 return ReturnStatement::Make(/*offset=*/-1, std::move(expr));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400400 }
401 case Rehydrator::kSwitch_Command: {
402 bool isStatic = this->readU8();
403 AutoRehydratorSymbolTable symbols(this);
404 std::unique_ptr<Expression> expr = this->expression();
405 int caseCount = this->readU8();
John Stilesb23a64b2021-03-11 08:27:59 -0500406 StatementArray cases;
407 cases.reserve_back(caseCount);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400408 for (int i = 0; i < caseCount; ++i) {
409 std::unique_ptr<Expression> value = this->expression();
John Stilesc3ce43b2021-03-09 15:37:01 -0500410 std::unique_ptr<Statement> statement = this->statement();
John Stiles8f2a0cf2020-10-13 12:48:21 -0400411 cases.push_back(std::make_unique<SwitchCase>(/*offset=*/-1, std::move(value),
John Stilesc3ce43b2021-03-09 15:37:01 -0500412 std::move(statement)));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400413 }
John Stiles23521a82021-03-02 17:02:51 -0500414 return SwitchStatement::Make(fContext, /*offset=*/-1, isStatic, std::move(expr),
415 std::move(cases), fSymbolTable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400416 }
417 case Rehydrator::kVarDeclaration_Command: {
Ethan Nicholase6592142020-09-08 10:22:09 -0400418 Variable* var = this->symbolRef<Variable>(Symbol::Kind::kVariable);
Brian Osmanc0213602020-10-06 14:43:32 -0400419 const Type* baseType = this->type();
John Stiles62a56462020-12-03 10:41:58 -0500420 int arraySize = this->readS8();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400421 std::unique_ptr<Expression> value = this->expression();
John Stilese67bd132021-03-19 18:39:25 -0400422 return VarDeclaration::Make(fContext, var, baseType, arraySize, std::move(value));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400423 }
424 case Rehydrator::kVoid_Command:
425 return nullptr;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400426 default:
427 printf("unsupported statement %d\n", kind);
428 SkASSERT(false);
429 return nullptr;
430 }
431}
432
John Stilesd8eb8752021-04-01 11:49:10 -0400433ExpressionArray Rehydrator::expressionArray() {
434 uint8_t count = this->readU8();
435 ExpressionArray array;
436 array.reserve_back(count);
437 for (int i = 0; i < count; ++i) {
438 array.push_back(this->expression());
439 }
440 return array;
441}
442
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400443std::unique_ptr<Expression> Rehydrator::expression() {
444 int kind = this->readU8();
445 switch (kind) {
446 case Rehydrator::kBinary_Command: {
447 std::unique_ptr<Expression> left = this->expression();
448 Token::Kind op = (Token::Kind) this->readU8();
449 std::unique_ptr<Expression> right = this->expression();
John Stilese2aec432021-03-01 09:27:48 -0500450 return BinaryExpression::Make(fContext, std::move(left), op, std::move(right));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400451 }
452 case Rehydrator::kBoolLiteral_Command: {
453 bool value = this->readU8();
John Stiles9ce80f72021-03-11 22:35:19 -0500454 return BoolLiteral::Make(fContext, /*offset=*/-1, value);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400455 }
456 case Rehydrator::kConstructor_Command: {
457 const Type* type = this->type();
John Stilesd8eb8752021-04-01 11:49:10 -0400458 ExpressionArray args = this->expressionArray();
John Stiles23521a82021-03-02 17:02:51 -0500459 auto ctor = Constructor::Convert(fContext, /*offset=*/-1, *type, std::move(args));
460 SkASSERT(ctor);
461 return ctor;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400462 }
John Stiles4118f142021-04-01 16:42:35 -0400463 case Rehydrator::kConstructorArray_Command: {
464 const Type* type = this->type();
465 return ConstructorArray::Make(fContext, /*offset=*/-1, *type, this->expressionArray());
466 }
John Stilese1182782021-03-30 22:09:37 -0400467 case Rehydrator::kConstructorDiagonalMatrix_Command: {
468 const Type* type = this->type();
John Stilesd8eb8752021-04-01 11:49:10 -0400469 ExpressionArray args = this->expressionArray();
470 SkASSERT(args.size() == 1);
John Stilese1182782021-03-30 22:09:37 -0400471 return ConstructorDiagonalMatrix::Make(fContext, /*offset=*/-1, *type,
John Stilesd8eb8752021-04-01 11:49:10 -0400472 std::move(args[0]));
John Stilese1182782021-03-30 22:09:37 -0400473 }
John Stiles2938eea2021-04-01 18:58:25 -0400474 case Rehydrator::kConstructorSplat_Command: {
475 const Type* type = this->type();
476 ExpressionArray args = this->expressionArray();
477 SkASSERT(args.size() == 1);
478 return ConstructorSplat::Make(fContext, /*offset=*/-1, *type, std::move(args[0]));
479 }
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400480 case Rehydrator::kFieldAccess_Command: {
481 std::unique_ptr<Expression> base = this->expression();
482 int index = this->readU8();
483 FieldAccess::OwnerKind ownerKind = (FieldAccess::OwnerKind) this->readU8();
John Stiles06d600f2021-03-08 09:18:21 -0500484 return FieldAccess::Make(fContext, std::move(base), index, ownerKind);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400485 }
486 case Rehydrator::kFloatLiteral_Command: {
Brian Osmanfb964a42020-11-18 10:45:52 -0500487 const Type* type = this->type();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400488 FloatIntUnion u;
489 u.fInt = this->readS32();
John Stiles9ce80f72021-03-11 22:35:19 -0500490 return FloatLiteral::Make(/*offset=*/-1, u.fFloat, type);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400491 }
492 case Rehydrator::kFunctionCall_Command: {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400493 const Type* type = this->type();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400494 const FunctionDeclaration* f = this->symbolRef<FunctionDeclaration>(
Ethan Nicholase6592142020-09-08 10:22:09 -0400495 Symbol::Kind::kFunctionDeclaration);
John Stilesd8eb8752021-04-01 11:49:10 -0400496 ExpressionArray args = this->expressionArray();
John Stilescd7ba502021-03-19 10:54:59 -0400497 return FunctionCall::Make(fContext, /*offset=*/-1, type, *f, std::move(args));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400498 }
499 case Rehydrator::kIndex_Command: {
500 std::unique_ptr<Expression> base = this->expression();
501 std::unique_ptr<Expression> index = this->expression();
John Stiles51d33982021-03-08 09:18:07 -0500502 return IndexExpression::Make(fContext, std::move(base), std::move(index));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400503 }
504 case Rehydrator::kIntLiteral_Command: {
Brian Osmanfb964a42020-11-18 10:45:52 -0500505 const Type* type = this->type();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400506 int value = this->readS32();
John Stiles9ce80f72021-03-11 22:35:19 -0500507 return IntLiteral::Make(/*offset=*/-1, value, type);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400508 }
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400509 case Rehydrator::kPostfix_Command: {
510 Token::Kind op = (Token::Kind) this->readU8();
511 std::unique_ptr<Expression> operand = this->expression();
John Stiles52d3b012021-02-26 15:56:48 -0500512 return PostfixExpression::Make(fContext, std::move(operand), op);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400513 }
514 case Rehydrator::kPrefix_Command: {
515 Token::Kind op = (Token::Kind) this->readU8();
516 std::unique_ptr<Expression> operand = this->expression();
John Stilesb0eb20f2021-02-26 15:29:33 -0500517 return PrefixExpression::Make(fContext, op, std::move(operand));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400518 }
519 case Rehydrator::kSetting_Command: {
520 StringFragment name = this->readString();
John Stiles23521a82021-03-02 17:02:51 -0500521 return Setting::Convert(fContext, /*offset=*/-1, name);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400522 }
523 case Rehydrator::kSwizzle_Command: {
524 std::unique_ptr<Expression> base = this->expression();
525 int count = this->readU8();
John Stiles750109b2020-10-30 13:45:46 -0400526 ComponentArray components;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400527 for (int i = 0; i < count; ++i) {
528 components.push_back(this->readU8());
529 }
John Stiles23521a82021-03-02 17:02:51 -0500530 return Swizzle::Make(fContext, std::move(base), components);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400531 }
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();
John Stiles90518f72021-02-26 20:44:54 -0500536 return TernaryExpression::Make(fContext, std::move(test),
537 std::move(ifTrue), std::move(ifFalse));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400538 }
539 case Rehydrator::kVariableReference_Command: {
Ethan Nicholase6592142020-09-08 10:22:09 -0400540 const Variable* var = this->symbolRef<Variable>(Symbol::Kind::kVariable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400541 VariableReference::RefKind refKind = (VariableReference::RefKind) this->readU8();
Brian Osman79457ef2020-09-24 15:01:27 -0400542 return std::make_unique<VariableReference>(-1, var, refKind);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400543 }
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 Osman1313d1a2020-09-08 10:34:30 -0400553std::shared_ptr<SymbolTable> Rehydrator::symbolTable(bool inherit) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400554 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 Osman1313d1a2020-09-08 10:34:30 -0400560 std::shared_ptr<SymbolTable> oldTable = fSymbolTable;
John Stiles7c3515b2020-10-16 18:38:39 -0400561 std::shared_ptr<SymbolTable> result =
562 inherit ? std::make_shared<SymbolTable>(fSymbolTable, /*builtin=*/true)
563 : std::make_shared<SymbolTable>(fErrors, /*builtin=*/true);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400564 fSymbolTable = result;
Brian Osman5bf3e202020-10-13 10:34:18 -0400565 std::vector<const Symbol*> ownedSymbols;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400566 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 Nicholasc18bb512020-07-28 14:46:53 -0400574 int index = this->readU16();
John Stilesb8cc6652020-10-08 09:12:07 -0400575 fSymbolTable->addWithoutOwnership(ownedSymbols[index]);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400576 }
Brian Osman1313d1a2020-09-08 10:34:30 -0400577 fSymbolTable = oldTable;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400578 return result;
579}
580
John Stilesa6841be2020-08-06 14:11:56 -0400581} // namespace SkSL