blob: b208f3f05b91bdf790011f4969f6f42d9eb88bda [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 Stiles5abb9e12021-04-06 13:47:19 -040021#include "src/sksl/ir/SkSLConstructorMatrixResize.h"
John Stilesfd7252f2021-04-04 22:24:40 -040022#include "src/sksl/ir/SkSLConstructorScalarCast.h"
John Stiles2938eea2021-04-01 18:58:25 -040023#include "src/sksl/ir/SkSLConstructorSplat.h"
John Stilesb14a8192021-04-05 11:40:46 -040024#include "src/sksl/ir/SkSLConstructorVectorCast.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040025#include "src/sksl/ir/SkSLContinueStatement.h"
26#include "src/sksl/ir/SkSLDiscardStatement.h"
27#include "src/sksl/ir/SkSLDoStatement.h"
28#include "src/sksl/ir/SkSLEnum.h"
29#include "src/sksl/ir/SkSLExpression.h"
30#include "src/sksl/ir/SkSLExpressionStatement.h"
31#include "src/sksl/ir/SkSLField.h"
32#include "src/sksl/ir/SkSLFieldAccess.h"
33#include "src/sksl/ir/SkSLFloatLiteral.h"
34#include "src/sksl/ir/SkSLForStatement.h"
35#include "src/sksl/ir/SkSLFunctionCall.h"
36#include "src/sksl/ir/SkSLFunctionDeclaration.h"
37#include "src/sksl/ir/SkSLFunctionDefinition.h"
38#include "src/sksl/ir/SkSLIfStatement.h"
39#include "src/sksl/ir/SkSLIndexExpression.h"
John Stiles98c1f822020-09-09 14:18:53 -040040#include "src/sksl/ir/SkSLInlineMarker.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040041#include "src/sksl/ir/SkSLIntLiteral.h"
42#include "src/sksl/ir/SkSLInterfaceBlock.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040043#include "src/sksl/ir/SkSLPostfixExpression.h"
44#include "src/sksl/ir/SkSLPrefixExpression.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040045#include "src/sksl/ir/SkSLReturnStatement.h"
46#include "src/sksl/ir/SkSLSetting.h"
John Stilesdc75a972020-11-25 16:24:55 -050047#include "src/sksl/ir/SkSLStructDefinition.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040048#include "src/sksl/ir/SkSLSwitchCase.h"
49#include "src/sksl/ir/SkSLSwitchStatement.h"
50#include "src/sksl/ir/SkSLSwizzle.h"
John Stiles49a547f2020-10-06 16:14:37 -040051#include "src/sksl/ir/SkSLSymbolAlias.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040052#include "src/sksl/ir/SkSLSymbolTable.h"
53#include "src/sksl/ir/SkSLTernaryExpression.h"
54#include "src/sksl/ir/SkSLType.h"
55#include "src/sksl/ir/SkSLUnresolvedFunction.h"
56#include "src/sksl/ir/SkSLVarDeclarations.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040057#include "src/sksl/ir/SkSLVariable.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040058
59namespace SkSL {
60
61class AutoRehydratorSymbolTable {
62public:
63 AutoRehydratorSymbolTable(Rehydrator* rehydrator)
64 : fRehydrator(rehydrator)
65 , fOldSymbols(fRehydrator->fSymbolTable) {
66 fRehydrator->fSymbolTable = fRehydrator->symbolTable();
67 }
68
69 ~AutoRehydratorSymbolTable() {
70 fRehydrator->fSymbolTable = std::move(fOldSymbols);
71 }
72
73private:
74 Rehydrator* fRehydrator;
75 std::shared_ptr<SymbolTable> fOldSymbols;
76};
77
John Stiles7c3515b2020-10-16 18:38:39 -040078Rehydrator::Rehydrator(const Context* context, ModifiersPool* modifiers,
79 std::shared_ptr<SymbolTable> symbolTable, ErrorReporter* errorReporter,
80 const uint8_t* src, size_t length)
81 : fContext(*context)
82 , fModifiers(*modifiers)
83 , fErrors(errorReporter)
84 , fSymbolTable(std::move(symbolTable))
85 , fStart(src)
86 SkDEBUGCODE(, fEnd(fStart + length)) {
87 SkASSERT(fSymbolTable);
88 SkASSERT(fSymbolTable->isBuiltin());
89 // skip past string data
90 fIP = fStart;
91 fIP += this->readU16();
92}
93
Ethan Nicholasc18bb512020-07-28 14:46:53 -040094Layout Rehydrator::layout() {
95 switch (this->readU8()) {
96 case kBuiltinLayout_Command: {
97 Layout result;
98 result.fBuiltin = this->readS16();
99 return result;
100 }
101 case kDefaultLayout_Command:
102 return Layout();
103 case kLayout_Command: {
104 int flags = this->readU32();
105 int location = this->readS8();
106 int offset = this->readS8();
107 int binding = this->readS8();
108 int index = this->readS8();
109 int set = this->readS8();
110 int builtin = this->readS16();
111 int inputAttachmentIndex = this->readS8();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400112 int primitive = this->readS8();
113 int maxVertices = this->readS8();
114 int invocations = this->readS8();
115 StringFragment marker = this->readString();
116 StringFragment when = this->readString();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400117 int ctype = this->readS8();
118 return Layout(flags, location, offset, binding, index, set, builtin,
Brian Osman4717fbb2021-02-23 13:12:09 -0500119 inputAttachmentIndex, (Layout::Primitive)primitive, maxVertices,
Brian Osmanba7ef322021-02-23 13:37:22 -0500120 invocations, marker, when, (Layout::CType)ctype);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400121 }
122 default:
123 SkASSERT(false);
124 return Layout();
125 }
126}
127
128Modifiers Rehydrator::modifiers() {
129 switch (this->readU8()) {
130 case kDefaultModifiers_Command:
131 return Modifiers();
132 case kModifiers8Bit_Command: {
133 Layout l = this->layout();
134 int flags = this->readU8();
135 return Modifiers(l, flags);
136 }
137 case kModifiers_Command: {
138 Layout l = this->layout();
139 int flags = this->readS32();
140 return Modifiers(l, flags);
141 }
142 default:
143 SkASSERT(false);
144 return Modifiers();
145 }
146}
147
Brian Osman5bf3e202020-10-13 10:34:18 -0400148const Symbol* Rehydrator::symbol() {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400149 int kind = this->readU8();
150 switch (kind) {
151 case kArrayType_Command: {
152 uint16_t id = this->readU16();
153 const Type* componentType = this->type();
Brian Osmane8c26082020-10-01 17:22:45 -0400154 int8_t count = this->readS8();
155 String name = componentType->name();
156 if (count == Type::kUnsizedArray) {
157 name += "[]";
158 } else {
159 name += "[" + to_string(count) + "]";
160 }
Brian Osman5bf3e202020-10-13 10:34:18 -0400161 const Type* result = fSymbolTable->takeOwnershipOfSymbol(
John Stilesad2d4942020-12-11 16:55:58 -0500162 Type::MakeArrayType(name, *componentType, count));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400163 this->addSymbol(id, result);
164 return result;
165 }
166 case kEnumType_Command: {
167 uint16_t id = this->readU16();
168 StringFragment name = this->readString();
John Stilese6c67c52021-01-15 12:01:00 -0500169 const Type* result = fSymbolTable->takeOwnershipOfSymbol(Type::MakeEnumType(name));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400170 this->addSymbol(id, result);
171 return result;
172 }
173 case kFunctionDeclaration_Command: {
174 uint16_t id = this->readU16();
175 Modifiers modifiers = this->modifiers();
176 StringFragment name = this->readString();
177 int parameterCount = this->readU8();
Brian Osman5bf3e202020-10-13 10:34:18 -0400178 std::vector<const Variable*> parameters;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400179 parameters.reserve(parameterCount);
180 for (int i = 0; i < parameterCount; ++i) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400181 parameters.push_back(this->symbolRef<Variable>(Symbol::Kind::kVariable));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400182 }
183 const Type* returnType = this->type();
Brian Osman5bf3e202020-10-13 10:34:18 -0400184 const FunctionDeclaration* result =
John Stiles3ae071e2020-08-05 15:29:29 -0400185 fSymbolTable->takeOwnershipOfSymbol(std::make_unique<FunctionDeclaration>(
John Stiles586df952020-11-12 18:27:13 -0500186 /*offset=*/-1, fModifiers.addToPool(modifiers), name,
Ethan Nicholased84b732020-10-08 11:45:44 -0400187 std::move(parameters), returnType, /*builtin=*/true));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400188 this->addSymbol(id, result);
189 return result;
190 }
191 case kField_Command: {
Ethan Nicholase6592142020-09-08 10:22:09 -0400192 const Variable* owner = this->symbolRef<Variable>(Symbol::Kind::kVariable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400193 uint8_t index = this->readU8();
Brian Osman5bf3e202020-10-13 10:34:18 -0400194 const Field* result = fSymbolTable->takeOwnershipOfSymbol(
Ethan Nicholase2c49992020-10-05 11:49:11 -0400195 std::make_unique<Field>(/*offset=*/-1, owner, index));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400196 return result;
197 }
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400198 case kStructType_Command: {
199 uint16_t id = this->readU16();
200 StringFragment name = this->readString();
201 uint8_t fieldCount = this->readU8();
202 std::vector<Type::Field> fields;
203 fields.reserve(fieldCount);
204 for (int i = 0; i < fieldCount; ++i) {
205 Modifiers m = this->modifiers();
John Stilesf621e232020-08-25 13:33:02 -0400206 StringFragment fieldName = this->readString();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400207 const Type* type = this->type();
John Stilesf621e232020-08-25 13:33:02 -0400208 fields.emplace_back(m, fieldName, type);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400209 }
Brian Osman5bf3e202020-10-13 10:34:18 -0400210 const Type* result = fSymbolTable->takeOwnershipOfSymbol(
John Stilesad2d4942020-12-11 16:55:58 -0500211 Type::MakeStructType(/*offset=*/-1, name, std::move(fields)));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400212 this->addSymbol(id, result);
213 return result;
214 }
215 case kSymbolRef_Command: {
216 uint16_t id = this->readU16();
217 SkASSERT(fSymbols.size() > id);
218 return fSymbols[id];
219 }
John Stiles49a547f2020-10-06 16:14:37 -0400220 case kSymbolAlias_Command: {
221 uint16_t id = this->readU16();
222 StringFragment name = this->readString();
Brian Osman5bf3e202020-10-13 10:34:18 -0400223 const Symbol* origSymbol = this->symbol();
224 const SymbolAlias* symbolAlias = fSymbolTable->takeOwnershipOfSymbol(
John Stiles49a547f2020-10-06 16:14:37 -0400225 std::make_unique<SymbolAlias>(/*offset=*/-1, name, origSymbol));
226 this->addSymbol(id, symbolAlias);
227 return symbolAlias;
228 }
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400229 case kSystemType_Command: {
230 uint16_t id = this->readU16();
231 StringFragment name = this->readString();
Brian Osman5bf3e202020-10-13 10:34:18 -0400232 const Symbol* result = (*fSymbolTable)[name];
Ethan Nicholase6592142020-09-08 10:22:09 -0400233 SkASSERT(result && result->kind() == Symbol::Kind::kType);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400234 this->addSymbol(id, result);
235 return result;
236 }
237 case kUnresolvedFunction_Command: {
238 uint16_t id = this->readU16();
239 int length = this->readU8();
240 std::vector<const FunctionDeclaration*> functions;
241 functions.reserve(length);
242 for (int i = 0; i < length; ++i) {
243 const Symbol* f = this->symbol();
Ethan Nicholase6592142020-09-08 10:22:09 -0400244 SkASSERT(f && f->kind() == Symbol::Kind::kFunctionDeclaration);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400245 functions.push_back((const FunctionDeclaration*) f);
246 }
Brian Osman5bf3e202020-10-13 10:34:18 -0400247 const UnresolvedFunction* result = fSymbolTable->takeOwnershipOfSymbol(
John Stiles3ae071e2020-08-05 15:29:29 -0400248 std::make_unique<UnresolvedFunction>(std::move(functions)));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400249 this->addSymbol(id, result);
250 return result;
251 }
252 case kVariable_Command: {
253 uint16_t id = this->readU16();
John Stiles586df952020-11-12 18:27:13 -0500254 const Modifiers* m = fModifiers.addToPool(this->modifiers());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400255 StringFragment name = this->readString();
256 const Type* type = this->type();
257 Variable::Storage storage = (Variable::Storage) this->readU8();
Brian Osman5bf3e202020-10-13 10:34:18 -0400258 const Variable* result = fSymbolTable->takeOwnershipOfSymbol(std::make_unique<Variable>(
Brian Osman3887a012020-09-30 13:22:27 -0400259 /*offset=*/-1, m, name, type, /*builtin=*/true, storage));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400260 this->addSymbol(id, result);
261 return result;
262 }
263 default:
264 printf("unsupported symbol %d\n", kind);
265 SkASSERT(false);
266 return nullptr;
267 }
268}
269
270const Type* Rehydrator::type() {
271 const Symbol* result = this->symbol();
Ethan Nicholase6592142020-09-08 10:22:09 -0400272 SkASSERT(result->kind() == Symbol::Kind::kType);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400273 return (const Type*) result;
274}
275
276std::vector<std::unique_ptr<ProgramElement>> Rehydrator::elements() {
277 SkDEBUGCODE(uint8_t command = )this->readU8();
278 SkASSERT(command == kElements_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400279 std::vector<std::unique_ptr<ProgramElement>> result;
John Stiles1ea7f542020-11-02 13:07:23 -0500280 while (std::unique_ptr<ProgramElement> elem = this->element()) {
281 result.push_back(std::move(elem));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400282 }
283 return result;
284}
285
286std::unique_ptr<ProgramElement> Rehydrator::element() {
287 int kind = this->readU8();
288 switch (kind) {
289 case Rehydrator::kEnum_Command: {
290 StringFragment typeName = this->readString();
Brian Osman1313d1a2020-09-08 10:34:30 -0400291 std::shared_ptr<SymbolTable> symbols = this->symbolTable(/*inherit=*/false);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400292 for (auto& s : symbols->fOwnedSymbols) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400293 SkASSERT(s->kind() == Symbol::Kind::kVariable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400294 Variable& v = (Variable&) *s;
295 int value = this->readS32();
Ethan Nicholas5b9b0db2021-01-21 13:12:01 -0500296 // enum variables aren't really 'declared', but we have to create a declaration to
297 // store the value
John Stiles9ce80f72021-03-11 22:35:19 -0500298 auto valueLiteral = IntLiteral::Make(fContext, /*offset=*/-1, value);
John Stilese67bd132021-03-19 18:39:25 -0400299 auto declaration = VarDeclaration::Make(fContext, &v, &v.type(), /*arraySize=*/0,
300 std::move(valueLiteral));
Ethan Nicholas5b9b0db2021-01-21 13:12:01 -0500301 symbols->takeOwnershipOfIRNode(std::move(declaration));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400302 }
John Stiles1c823672020-10-20 10:23:50 -0400303 return std::make_unique<Enum>(/*offset=*/-1, typeName, std::move(symbols),
304 /*isSharedWithCpp=*/true, /*isBuiltin=*/true);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400305 }
306 case Rehydrator::kFunctionDefinition_Command: {
307 const FunctionDeclaration* decl = this->symbolRef<FunctionDeclaration>(
Ethan Nicholase6592142020-09-08 10:22:09 -0400308 Symbol::Kind::kFunctionDeclaration);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400309 std::unique_ptr<Statement> body = this->statement();
John Stilesb8e010c2020-08-11 18:05:39 -0400310 std::unordered_set<const FunctionDeclaration*> refs;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400311 uint8_t refCount = this->readU8();
312 for (int i = 0; i < refCount; ++i) {
313 refs.insert(this->symbolRef<FunctionDeclaration>(
Ethan Nicholase6592142020-09-08 10:22:09 -0400314 Symbol::Kind::kFunctionDeclaration));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400315 }
John Stiles607d36b2020-10-19 15:00:01 -0400316 auto result = std::make_unique<FunctionDefinition>(/*offset=*/-1, decl,
317 /*builtin=*/true, std::move(body),
318 std::move(refs));
319 decl->setDefinition(result.get());
320 return std::move(result);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400321 }
322 case Rehydrator::kInterfaceBlock_Command: {
323 const Symbol* var = this->symbol();
John Stiles87ae34e2020-10-13 12:50:11 -0400324 SkASSERT(var && var->is<Variable>());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400325 StringFragment typeName = this->readString();
326 StringFragment instanceName = this->readString();
John Stilesd39aec02020-12-03 10:42:26 -0500327 int arraySize = this->readS8();
John Stiles87ae34e2020-10-13 12:50:11 -0400328 return std::make_unique<InterfaceBlock>(/*offset=*/-1, &var->as<Variable>(), typeName,
John Stilesd39aec02020-12-03 10:42:26 -0500329 instanceName, arraySize, nullptr);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400330 }
331 case Rehydrator::kVarDeclarations_Command: {
Brian Osmanc0213602020-10-06 14:43:32 -0400332 std::unique_ptr<Statement> decl = this->statement();
John Stiles1ea7f542020-11-02 13:07:23 -0500333 return std::make_unique<GlobalVarDeclaration>(/*offset=*/-1, std::move(decl));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400334 }
John Stilesdc75a972020-11-25 16:24:55 -0500335 case Rehydrator::kStructDefinition_Command: {
336 const Symbol* type = this->symbol();
337 SkASSERT(type && type->is<Type>());
338 return std::make_unique<StructDefinition>(/*offset=*/-1, type->as<Type>());
339 }
John Stiles1ea7f542020-11-02 13:07:23 -0500340 case Rehydrator::kElementsComplete_Command:
341 return nullptr;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400342 default:
John Stiles1ea7f542020-11-02 13:07:23 -0500343 SkDEBUGFAILF("unsupported element %d\n", kind);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400344 return nullptr;
345 }
346}
347
348std::unique_ptr<Statement> Rehydrator::statement() {
349 int kind = this->readU8();
350 switch (kind) {
351 case Rehydrator::kBlock_Command: {
352 AutoRehydratorSymbolTable symbols(this);
353 int count = this->readU8();
John Stiles8f2a0cf2020-10-13 12:48:21 -0400354 StatementArray statements;
John Stilesf4bda742020-10-14 16:57:41 -0400355 statements.reserve_back(count);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400356 for (int i = 0; i < count; ++i) {
357 statements.push_back(this->statement());
358 }
359 bool isScope = this->readU8();
John Stilesbf16b6c2021-03-12 19:24:31 -0500360 return Block::Make(/*offset=*/-1, std::move(statements), fSymbolTable, isScope);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400361 }
362 case Rehydrator::kBreak_Command:
John Stilesa0c04d62021-03-11 23:07:24 -0500363 return BreakStatement::Make(/*offset=*/-1);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400364 case Rehydrator::kContinue_Command:
John Stilesa0c04d62021-03-11 23:07:24 -0500365 return ContinueStatement::Make(/*offset=*/-1);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400366 case Rehydrator::kDiscard_Command:
John Stilesa0c04d62021-03-11 23:07:24 -0500367 return DiscardStatement::Make(/*offset=*/-1);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400368 case Rehydrator::kDo_Command: {
369 std::unique_ptr<Statement> stmt = this->statement();
370 std::unique_ptr<Expression> expr = this->expression();
John Stilesea5822e2021-02-26 11:18:20 -0500371 return DoStatement::Make(fContext, std::move(stmt), std::move(expr));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400372 }
373 case Rehydrator::kExpressionStatement_Command: {
374 std::unique_ptr<Expression> expr = this->expression();
John Stiles3e5871c2021-02-25 20:52:03 -0500375 return ExpressionStatement::Make(fContext, std::move(expr));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400376 }
377 case Rehydrator::kFor_Command: {
378 std::unique_ptr<Statement> initializer = this->statement();
379 std::unique_ptr<Expression> test = this->expression();
380 std::unique_ptr<Expression> next = this->expression();
381 std::unique_ptr<Statement> body = this->statement();
382 std::shared_ptr<SymbolTable> symbols = this->symbolTable();
John Stiles23521a82021-03-02 17:02:51 -0500383 return ForStatement::Make(fContext, /*offset=*/-1, std::move(initializer),
384 std::move(test), std::move(next), std::move(body),
385 std::move(symbols));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400386 }
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();
John Stilescf3059e2021-02-25 14:27:02 -0500392 return IfStatement::Make(fContext, /*offset=*/-1, isStatic, std::move(test),
393 std::move(ifTrue), std::move(ifFalse));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400394 }
John Stiles98c1f822020-09-09 14:18:53 -0400395 case Rehydrator::kInlineMarker_Command: {
396 const FunctionDeclaration* funcDecl = this->symbolRef<FunctionDeclaration>(
397 Symbol::Kind::kFunctionDeclaration);
John Stilesa0c04d62021-03-11 23:07:24 -0500398 return InlineMarker::Make(funcDecl);
John Stiles98c1f822020-09-09 14:18:53 -0400399 }
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400400 case Rehydrator::kReturn_Command: {
401 std::unique_ptr<Expression> expr = this->expression();
John Stilesa0c04d62021-03-11 23:07:24 -0500402 return ReturnStatement::Make(/*offset=*/-1, std::move(expr));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400403 }
404 case Rehydrator::kSwitch_Command: {
405 bool isStatic = this->readU8();
406 AutoRehydratorSymbolTable symbols(this);
407 std::unique_ptr<Expression> expr = this->expression();
408 int caseCount = this->readU8();
John Stilesb23a64b2021-03-11 08:27:59 -0500409 StatementArray cases;
410 cases.reserve_back(caseCount);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400411 for (int i = 0; i < caseCount; ++i) {
412 std::unique_ptr<Expression> value = this->expression();
John Stilesc3ce43b2021-03-09 15:37:01 -0500413 std::unique_ptr<Statement> statement = this->statement();
John Stiles8f2a0cf2020-10-13 12:48:21 -0400414 cases.push_back(std::make_unique<SwitchCase>(/*offset=*/-1, std::move(value),
John Stilesc3ce43b2021-03-09 15:37:01 -0500415 std::move(statement)));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400416 }
John Stiles23521a82021-03-02 17:02:51 -0500417 return SwitchStatement::Make(fContext, /*offset=*/-1, isStatic, std::move(expr),
418 std::move(cases), fSymbolTable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400419 }
420 case Rehydrator::kVarDeclaration_Command: {
Ethan Nicholase6592142020-09-08 10:22:09 -0400421 Variable* var = this->symbolRef<Variable>(Symbol::Kind::kVariable);
Brian Osmanc0213602020-10-06 14:43:32 -0400422 const Type* baseType = this->type();
John Stiles62a56462020-12-03 10:41:58 -0500423 int arraySize = this->readS8();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400424 std::unique_ptr<Expression> value = this->expression();
John Stilese67bd132021-03-19 18:39:25 -0400425 return VarDeclaration::Make(fContext, var, baseType, arraySize, std::move(value));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400426 }
427 case Rehydrator::kVoid_Command:
428 return nullptr;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400429 default:
430 printf("unsupported statement %d\n", kind);
431 SkASSERT(false);
432 return nullptr;
433 }
434}
435
John Stilesd8eb8752021-04-01 11:49:10 -0400436ExpressionArray Rehydrator::expressionArray() {
437 uint8_t count = this->readU8();
438 ExpressionArray array;
439 array.reserve_back(count);
440 for (int i = 0; i < count; ++i) {
441 array.push_back(this->expression());
442 }
443 return array;
444}
445
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400446std::unique_ptr<Expression> Rehydrator::expression() {
447 int kind = this->readU8();
448 switch (kind) {
449 case Rehydrator::kBinary_Command: {
450 std::unique_ptr<Expression> left = this->expression();
451 Token::Kind op = (Token::Kind) this->readU8();
452 std::unique_ptr<Expression> right = this->expression();
John Stilese2aec432021-03-01 09:27:48 -0500453 return BinaryExpression::Make(fContext, std::move(left), op, std::move(right));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400454 }
455 case Rehydrator::kBoolLiteral_Command: {
456 bool value = this->readU8();
John Stiles9ce80f72021-03-11 22:35:19 -0500457 return BoolLiteral::Make(fContext, /*offset=*/-1, value);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400458 }
459 case Rehydrator::kConstructor_Command: {
460 const Type* type = this->type();
John Stilesd8eb8752021-04-01 11:49:10 -0400461 ExpressionArray args = this->expressionArray();
John Stiles23521a82021-03-02 17:02:51 -0500462 auto ctor = Constructor::Convert(fContext, /*offset=*/-1, *type, std::move(args));
463 SkASSERT(ctor);
464 return ctor;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400465 }
John Stiles4118f142021-04-01 16:42:35 -0400466 case Rehydrator::kConstructorArray_Command: {
467 const Type* type = this->type();
468 return ConstructorArray::Make(fContext, /*offset=*/-1, *type, this->expressionArray());
469 }
John Stilese1182782021-03-30 22:09:37 -0400470 case Rehydrator::kConstructorDiagonalMatrix_Command: {
471 const Type* type = this->type();
John Stilesd8eb8752021-04-01 11:49:10 -0400472 ExpressionArray args = this->expressionArray();
473 SkASSERT(args.size() == 1);
John Stilese1182782021-03-30 22:09:37 -0400474 return ConstructorDiagonalMatrix::Make(fContext, /*offset=*/-1, *type,
John Stilesd8eb8752021-04-01 11:49:10 -0400475 std::move(args[0]));
John Stilese1182782021-03-30 22:09:37 -0400476 }
John Stiles5abb9e12021-04-06 13:47:19 -0400477 case Rehydrator::kConstructorMatrixResize_Command: {
478 const Type* type = this->type();
479 ExpressionArray args = this->expressionArray();
480 SkASSERT(args.size() == 1);
481 return ConstructorMatrixResize::Make(fContext, /*offset=*/-1, *type,
482 std::move(args[0]));
483 }
John Stilesfd7252f2021-04-04 22:24:40 -0400484 case Rehydrator::kConstructorScalarCast_Command: {
485 const Type* type = this->type();
486 ExpressionArray args = this->expressionArray();
487 SkASSERT(args.size() == 1);
488 return ConstructorScalarCast::Make(fContext, /*offset=*/-1, *type, std::move(args[0]));
489 }
John Stiles2938eea2021-04-01 18:58:25 -0400490 case Rehydrator::kConstructorSplat_Command: {
491 const Type* type = this->type();
492 ExpressionArray args = this->expressionArray();
493 SkASSERT(args.size() == 1);
494 return ConstructorSplat::Make(fContext, /*offset=*/-1, *type, std::move(args[0]));
495 }
John Stilesb14a8192021-04-05 11:40:46 -0400496 case Rehydrator::kConstructorVectorCast_Command: {
497 const Type* type = this->type();
498 ExpressionArray args = this->expressionArray();
499 SkASSERT(args.size() == 1);
500 return ConstructorVectorCast::Make(fContext, /*offset=*/-1, *type, std::move(args[0]));
501 }
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400502 case Rehydrator::kFieldAccess_Command: {
503 std::unique_ptr<Expression> base = this->expression();
504 int index = this->readU8();
505 FieldAccess::OwnerKind ownerKind = (FieldAccess::OwnerKind) this->readU8();
John Stiles06d600f2021-03-08 09:18:21 -0500506 return FieldAccess::Make(fContext, std::move(base), index, ownerKind);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400507 }
508 case Rehydrator::kFloatLiteral_Command: {
Brian Osmanfb964a42020-11-18 10:45:52 -0500509 const Type* type = this->type();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400510 FloatIntUnion u;
511 u.fInt = this->readS32();
John Stiles9ce80f72021-03-11 22:35:19 -0500512 return FloatLiteral::Make(/*offset=*/-1, u.fFloat, type);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400513 }
514 case Rehydrator::kFunctionCall_Command: {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400515 const Type* type = this->type();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400516 const FunctionDeclaration* f = this->symbolRef<FunctionDeclaration>(
Ethan Nicholase6592142020-09-08 10:22:09 -0400517 Symbol::Kind::kFunctionDeclaration);
John Stilesd8eb8752021-04-01 11:49:10 -0400518 ExpressionArray args = this->expressionArray();
John Stilescd7ba502021-03-19 10:54:59 -0400519 return FunctionCall::Make(fContext, /*offset=*/-1, type, *f, std::move(args));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400520 }
521 case Rehydrator::kIndex_Command: {
522 std::unique_ptr<Expression> base = this->expression();
523 std::unique_ptr<Expression> index = this->expression();
John Stiles51d33982021-03-08 09:18:07 -0500524 return IndexExpression::Make(fContext, std::move(base), std::move(index));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400525 }
526 case Rehydrator::kIntLiteral_Command: {
Brian Osmanfb964a42020-11-18 10:45:52 -0500527 const Type* type = this->type();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400528 int value = this->readS32();
John Stiles9ce80f72021-03-11 22:35:19 -0500529 return IntLiteral::Make(/*offset=*/-1, value, type);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400530 }
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400531 case Rehydrator::kPostfix_Command: {
532 Token::Kind op = (Token::Kind) this->readU8();
533 std::unique_ptr<Expression> operand = this->expression();
John Stiles52d3b012021-02-26 15:56:48 -0500534 return PostfixExpression::Make(fContext, std::move(operand), op);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400535 }
536 case Rehydrator::kPrefix_Command: {
537 Token::Kind op = (Token::Kind) this->readU8();
538 std::unique_ptr<Expression> operand = this->expression();
John Stilesb0eb20f2021-02-26 15:29:33 -0500539 return PrefixExpression::Make(fContext, op, std::move(operand));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400540 }
541 case Rehydrator::kSetting_Command: {
542 StringFragment name = this->readString();
John Stiles23521a82021-03-02 17:02:51 -0500543 return Setting::Convert(fContext, /*offset=*/-1, name);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400544 }
545 case Rehydrator::kSwizzle_Command: {
546 std::unique_ptr<Expression> base = this->expression();
547 int count = this->readU8();
John Stiles750109b2020-10-30 13:45:46 -0400548 ComponentArray components;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400549 for (int i = 0; i < count; ++i) {
550 components.push_back(this->readU8());
551 }
John Stiles23521a82021-03-02 17:02:51 -0500552 return Swizzle::Make(fContext, std::move(base), components);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400553 }
554 case Rehydrator::kTernary_Command: {
555 std::unique_ptr<Expression> test = this->expression();
556 std::unique_ptr<Expression> ifTrue = this->expression();
557 std::unique_ptr<Expression> ifFalse = this->expression();
John Stiles90518f72021-02-26 20:44:54 -0500558 return TernaryExpression::Make(fContext, std::move(test),
559 std::move(ifTrue), std::move(ifFalse));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400560 }
561 case Rehydrator::kVariableReference_Command: {
Ethan Nicholase6592142020-09-08 10:22:09 -0400562 const Variable* var = this->symbolRef<Variable>(Symbol::Kind::kVariable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400563 VariableReference::RefKind refKind = (VariableReference::RefKind) this->readU8();
Brian Osman79457ef2020-09-24 15:01:27 -0400564 return std::make_unique<VariableReference>(-1, var, refKind);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400565 }
566 case Rehydrator::kVoid_Command:
567 return nullptr;
568 default:
569 printf("unsupported expression %d\n", kind);
570 SkASSERT(false);
571 return nullptr;
572 }
573}
574
Brian Osman1313d1a2020-09-08 10:34:30 -0400575std::shared_ptr<SymbolTable> Rehydrator::symbolTable(bool inherit) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400576 int command = this->readU8();
577 if (command == kVoid_Command) {
578 return nullptr;
579 }
580 SkASSERT(command == kSymbolTable_Command);
581 uint16_t ownedCount = this->readU16();
Brian Osman1313d1a2020-09-08 10:34:30 -0400582 std::shared_ptr<SymbolTable> oldTable = fSymbolTable;
John Stiles7c3515b2020-10-16 18:38:39 -0400583 std::shared_ptr<SymbolTable> result =
584 inherit ? std::make_shared<SymbolTable>(fSymbolTable, /*builtin=*/true)
585 : std::make_shared<SymbolTable>(fErrors, /*builtin=*/true);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400586 fSymbolTable = result;
Brian Osman5bf3e202020-10-13 10:34:18 -0400587 std::vector<const Symbol*> ownedSymbols;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400588 ownedSymbols.reserve(ownedCount);
589 for (int i = 0; i < ownedCount; ++i) {
590 ownedSymbols.push_back(this->symbol());
591 }
592 uint16_t symbolCount = this->readU16();
593 std::vector<std::pair<StringFragment, int>> symbols;
594 symbols.reserve(symbolCount);
595 for (int i = 0; i < symbolCount; ++i) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400596 int index = this->readU16();
John Stilesb8cc6652020-10-08 09:12:07 -0400597 fSymbolTable->addWithoutOwnership(ownedSymbols[index]);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400598 }
Brian Osman1313d1a2020-09-08 10:34:30 -0400599 fSymbolTable = oldTable;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400600 return result;
601}
602
John Stilesa6841be2020-08-06 14:11:56 -0400603} // namespace SkSL