blob: 4f2d953eaa64da2753b8cb39e29b0991a25268bc [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 Stiles2bec8ab2021-04-06 18:40:04 -040020#include "src/sksl/ir/SkSLConstructorComposite.h"
John Stilese1182782021-03-30 22:09:37 -040021#include "src/sksl/ir/SkSLConstructorDiagonalMatrix.h"
John Stiles5abb9e12021-04-06 13:47:19 -040022#include "src/sksl/ir/SkSLConstructorMatrixResize.h"
John Stilesfd7252f2021-04-04 22:24:40 -040023#include "src/sksl/ir/SkSLConstructorScalarCast.h"
John Stiles2938eea2021-04-01 18:58:25 -040024#include "src/sksl/ir/SkSLConstructorSplat.h"
John Stilesb14a8192021-04-05 11:40:46 -040025#include "src/sksl/ir/SkSLConstructorVectorCast.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040026#include "src/sksl/ir/SkSLContinueStatement.h"
27#include "src/sksl/ir/SkSLDiscardStatement.h"
28#include "src/sksl/ir/SkSLDoStatement.h"
29#include "src/sksl/ir/SkSLEnum.h"
30#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 Stiles7c3515b2020-10-16 18:38:39 -040079Rehydrator::Rehydrator(const Context* context, ModifiersPool* modifiers,
80 std::shared_ptr<SymbolTable> symbolTable, ErrorReporter* errorReporter,
81 const uint8_t* src, size_t length)
82 : fContext(*context)
83 , fModifiers(*modifiers)
84 , fErrors(errorReporter)
85 , fSymbolTable(std::move(symbolTable))
86 , fStart(src)
87 SkDEBUGCODE(, fEnd(fStart + length)) {
88 SkASSERT(fSymbolTable);
89 SkASSERT(fSymbolTable->isBuiltin());
90 // skip past string data
91 fIP = fStart;
92 fIP += this->readU16();
93}
94
Ethan Nicholasc18bb512020-07-28 14:46:53 -040095Layout Rehydrator::layout() {
96 switch (this->readU8()) {
97 case kBuiltinLayout_Command: {
98 Layout result;
99 result.fBuiltin = this->readS16();
100 return result;
101 }
102 case kDefaultLayout_Command:
103 return Layout();
104 case kLayout_Command: {
105 int flags = this->readU32();
106 int location = this->readS8();
107 int offset = this->readS8();
108 int binding = this->readS8();
109 int index = this->readS8();
110 int set = this->readS8();
111 int builtin = this->readS16();
112 int inputAttachmentIndex = this->readS8();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400113 int primitive = this->readS8();
114 int maxVertices = this->readS8();
115 int invocations = this->readS8();
116 StringFragment marker = this->readString();
117 StringFragment when = this->readString();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400118 int ctype = this->readS8();
119 return Layout(flags, location, offset, binding, index, set, builtin,
Brian Osman4717fbb2021-02-23 13:12:09 -0500120 inputAttachmentIndex, (Layout::Primitive)primitive, maxVertices,
Brian Osmanba7ef322021-02-23 13:37:22 -0500121 invocations, marker, when, (Layout::CType)ctype);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400122 }
123 default:
124 SkASSERT(false);
125 return Layout();
126 }
127}
128
129Modifiers Rehydrator::modifiers() {
130 switch (this->readU8()) {
131 case kDefaultModifiers_Command:
132 return Modifiers();
133 case kModifiers8Bit_Command: {
134 Layout l = this->layout();
135 int flags = this->readU8();
136 return Modifiers(l, flags);
137 }
138 case kModifiers_Command: {
139 Layout l = this->layout();
140 int flags = this->readS32();
141 return Modifiers(l, flags);
142 }
143 default:
144 SkASSERT(false);
145 return Modifiers();
146 }
147}
148
Brian Osman5bf3e202020-10-13 10:34:18 -0400149const Symbol* Rehydrator::symbol() {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400150 int kind = this->readU8();
151 switch (kind) {
152 case kArrayType_Command: {
153 uint16_t id = this->readU16();
154 const Type* componentType = this->type();
Brian Osmane8c26082020-10-01 17:22:45 -0400155 int8_t count = this->readS8();
156 String name = componentType->name();
157 if (count == Type::kUnsizedArray) {
158 name += "[]";
159 } else {
160 name += "[" + to_string(count) + "]";
161 }
Brian Osman5bf3e202020-10-13 10:34:18 -0400162 const Type* result = fSymbolTable->takeOwnershipOfSymbol(
John Stilesad2d4942020-12-11 16:55:58 -0500163 Type::MakeArrayType(name, *componentType, count));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400164 this->addSymbol(id, result);
165 return result;
166 }
167 case kEnumType_Command: {
168 uint16_t id = this->readU16();
169 StringFragment name = this->readString();
John Stilese6c67c52021-01-15 12:01:00 -0500170 const Type* result = fSymbolTable->takeOwnershipOfSymbol(Type::MakeEnumType(name));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400171 this->addSymbol(id, result);
172 return result;
173 }
174 case kFunctionDeclaration_Command: {
175 uint16_t id = this->readU16();
176 Modifiers modifiers = this->modifiers();
177 StringFragment name = this->readString();
178 int parameterCount = this->readU8();
Brian Osman5bf3e202020-10-13 10:34:18 -0400179 std::vector<const Variable*> parameters;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400180 parameters.reserve(parameterCount);
181 for (int i = 0; i < parameterCount; ++i) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400182 parameters.push_back(this->symbolRef<Variable>(Symbol::Kind::kVariable));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400183 }
184 const Type* returnType = this->type();
Brian Osman5bf3e202020-10-13 10:34:18 -0400185 const FunctionDeclaration* result =
John Stiles3ae071e2020-08-05 15:29:29 -0400186 fSymbolTable->takeOwnershipOfSymbol(std::make_unique<FunctionDeclaration>(
John Stiles586df952020-11-12 18:27:13 -0500187 /*offset=*/-1, fModifiers.addToPool(modifiers), name,
Ethan Nicholased84b732020-10-08 11:45:44 -0400188 std::move(parameters), returnType, /*builtin=*/true));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400189 this->addSymbol(id, result);
190 return result;
191 }
192 case kField_Command: {
Ethan Nicholase6592142020-09-08 10:22:09 -0400193 const Variable* owner = this->symbolRef<Variable>(Symbol::Kind::kVariable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400194 uint8_t index = this->readU8();
Brian Osman5bf3e202020-10-13 10:34:18 -0400195 const Field* result = fSymbolTable->takeOwnershipOfSymbol(
Ethan Nicholase2c49992020-10-05 11:49:11 -0400196 std::make_unique<Field>(/*offset=*/-1, owner, index));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400197 return result;
198 }
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400199 case kStructType_Command: {
200 uint16_t id = this->readU16();
201 StringFragment name = this->readString();
202 uint8_t fieldCount = this->readU8();
203 std::vector<Type::Field> fields;
204 fields.reserve(fieldCount);
205 for (int i = 0; i < fieldCount; ++i) {
206 Modifiers m = this->modifiers();
John Stilesf621e232020-08-25 13:33:02 -0400207 StringFragment fieldName = this->readString();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400208 const Type* type = this->type();
John Stilesf621e232020-08-25 13:33:02 -0400209 fields.emplace_back(m, fieldName, type);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400210 }
Brian Osman5bf3e202020-10-13 10:34:18 -0400211 const Type* result = fSymbolTable->takeOwnershipOfSymbol(
John Stilesad2d4942020-12-11 16:55:58 -0500212 Type::MakeStructType(/*offset=*/-1, name, std::move(fields)));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400213 this->addSymbol(id, result);
214 return result;
215 }
216 case kSymbolRef_Command: {
217 uint16_t id = this->readU16();
218 SkASSERT(fSymbols.size() > id);
219 return fSymbols[id];
220 }
John Stiles49a547f2020-10-06 16:14:37 -0400221 case kSymbolAlias_Command: {
222 uint16_t id = this->readU16();
223 StringFragment name = this->readString();
Brian Osman5bf3e202020-10-13 10:34:18 -0400224 const Symbol* origSymbol = this->symbol();
225 const SymbolAlias* symbolAlias = fSymbolTable->takeOwnershipOfSymbol(
John Stiles49a547f2020-10-06 16:14:37 -0400226 std::make_unique<SymbolAlias>(/*offset=*/-1, name, origSymbol));
227 this->addSymbol(id, symbolAlias);
228 return symbolAlias;
229 }
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400230 case kSystemType_Command: {
231 uint16_t id = this->readU16();
232 StringFragment name = this->readString();
Brian Osman5bf3e202020-10-13 10:34:18 -0400233 const Symbol* result = (*fSymbolTable)[name];
Ethan Nicholase6592142020-09-08 10:22:09 -0400234 SkASSERT(result && result->kind() == Symbol::Kind::kType);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400235 this->addSymbol(id, result);
236 return result;
237 }
238 case kUnresolvedFunction_Command: {
239 uint16_t id = this->readU16();
240 int length = this->readU8();
241 std::vector<const FunctionDeclaration*> functions;
242 functions.reserve(length);
243 for (int i = 0; i < length; ++i) {
244 const Symbol* f = this->symbol();
Ethan Nicholase6592142020-09-08 10:22:09 -0400245 SkASSERT(f && f->kind() == Symbol::Kind::kFunctionDeclaration);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400246 functions.push_back((const FunctionDeclaration*) f);
247 }
Brian Osman5bf3e202020-10-13 10:34:18 -0400248 const UnresolvedFunction* result = fSymbolTable->takeOwnershipOfSymbol(
John Stiles3ae071e2020-08-05 15:29:29 -0400249 std::make_unique<UnresolvedFunction>(std::move(functions)));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400250 this->addSymbol(id, result);
251 return result;
252 }
253 case kVariable_Command: {
254 uint16_t id = this->readU16();
John Stiles586df952020-11-12 18:27:13 -0500255 const Modifiers* m = fModifiers.addToPool(this->modifiers());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400256 StringFragment name = this->readString();
257 const Type* type = this->type();
258 Variable::Storage storage = (Variable::Storage) this->readU8();
Brian Osman5bf3e202020-10-13 10:34:18 -0400259 const Variable* result = fSymbolTable->takeOwnershipOfSymbol(std::make_unique<Variable>(
Brian Osman3887a012020-09-30 13:22:27 -0400260 /*offset=*/-1, m, name, type, /*builtin=*/true, storage));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400261 this->addSymbol(id, result);
262 return result;
263 }
264 default:
265 printf("unsupported symbol %d\n", kind);
266 SkASSERT(false);
267 return nullptr;
268 }
269}
270
271const Type* Rehydrator::type() {
272 const Symbol* result = this->symbol();
Ethan Nicholase6592142020-09-08 10:22:09 -0400273 SkASSERT(result->kind() == Symbol::Kind::kType);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400274 return (const Type*) result;
275}
276
277std::vector<std::unique_ptr<ProgramElement>> Rehydrator::elements() {
278 SkDEBUGCODE(uint8_t command = )this->readU8();
279 SkASSERT(command == kElements_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400280 std::vector<std::unique_ptr<ProgramElement>> result;
John Stiles1ea7f542020-11-02 13:07:23 -0500281 while (std::unique_ptr<ProgramElement> elem = this->element()) {
282 result.push_back(std::move(elem));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400283 }
284 return result;
285}
286
287std::unique_ptr<ProgramElement> Rehydrator::element() {
288 int kind = this->readU8();
289 switch (kind) {
290 case Rehydrator::kEnum_Command: {
291 StringFragment typeName = this->readString();
Brian Osman1313d1a2020-09-08 10:34:30 -0400292 std::shared_ptr<SymbolTable> symbols = this->symbolTable(/*inherit=*/false);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400293 for (auto& s : symbols->fOwnedSymbols) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400294 SkASSERT(s->kind() == Symbol::Kind::kVariable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400295 Variable& v = (Variable&) *s;
296 int value = this->readS32();
Ethan Nicholas5b9b0db2021-01-21 13:12:01 -0500297 // enum variables aren't really 'declared', but we have to create a declaration to
298 // store the value
John Stiles9ce80f72021-03-11 22:35:19 -0500299 auto valueLiteral = IntLiteral::Make(fContext, /*offset=*/-1, value);
John Stilese67bd132021-03-19 18:39:25 -0400300 auto declaration = VarDeclaration::Make(fContext, &v, &v.type(), /*arraySize=*/0,
301 std::move(valueLiteral));
Ethan Nicholas5b9b0db2021-01-21 13:12:01 -0500302 symbols->takeOwnershipOfIRNode(std::move(declaration));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400303 }
John Stiles1c823672020-10-20 10:23:50 -0400304 return std::make_unique<Enum>(/*offset=*/-1, typeName, std::move(symbols),
305 /*isSharedWithCpp=*/true, /*isBuiltin=*/true);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400306 }
307 case Rehydrator::kFunctionDefinition_Command: {
308 const FunctionDeclaration* decl = this->symbolRef<FunctionDeclaration>(
Ethan Nicholase6592142020-09-08 10:22:09 -0400309 Symbol::Kind::kFunctionDeclaration);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400310 std::unique_ptr<Statement> body = this->statement();
John Stilesb8e010c2020-08-11 18:05:39 -0400311 std::unordered_set<const FunctionDeclaration*> refs;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400312 uint8_t refCount = this->readU8();
313 for (int i = 0; i < refCount; ++i) {
314 refs.insert(this->symbolRef<FunctionDeclaration>(
Ethan Nicholase6592142020-09-08 10:22:09 -0400315 Symbol::Kind::kFunctionDeclaration));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400316 }
John Stiles607d36b2020-10-19 15:00:01 -0400317 auto result = std::make_unique<FunctionDefinition>(/*offset=*/-1, decl,
318 /*builtin=*/true, std::move(body),
319 std::move(refs));
320 decl->setDefinition(result.get());
321 return std::move(result);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400322 }
323 case Rehydrator::kInterfaceBlock_Command: {
324 const Symbol* var = this->symbol();
John Stiles87ae34e2020-10-13 12:50:11 -0400325 SkASSERT(var && var->is<Variable>());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400326 StringFragment typeName = this->readString();
327 StringFragment instanceName = this->readString();
John Stilesd39aec02020-12-03 10:42:26 -0500328 int arraySize = this->readS8();
John Stiles87ae34e2020-10-13 12:50:11 -0400329 return std::make_unique<InterfaceBlock>(/*offset=*/-1, &var->as<Variable>(), typeName,
John Stilesd39aec02020-12-03 10:42:26 -0500330 instanceName, arraySize, nullptr);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400331 }
332 case Rehydrator::kVarDeclarations_Command: {
Brian Osmanc0213602020-10-06 14:43:32 -0400333 std::unique_ptr<Statement> decl = this->statement();
John Stiles1ea7f542020-11-02 13:07:23 -0500334 return std::make_unique<GlobalVarDeclaration>(/*offset=*/-1, std::move(decl));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400335 }
John Stilesdc75a972020-11-25 16:24:55 -0500336 case Rehydrator::kStructDefinition_Command: {
337 const Symbol* type = this->symbol();
338 SkASSERT(type && type->is<Type>());
339 return std::make_unique<StructDefinition>(/*offset=*/-1, type->as<Type>());
340 }
John Stiles1ea7f542020-11-02 13:07:23 -0500341 case Rehydrator::kElementsComplete_Command:
342 return nullptr;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400343 default:
John Stiles1ea7f542020-11-02 13:07:23 -0500344 SkDEBUGFAILF("unsupported element %d\n", kind);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400345 return nullptr;
346 }
347}
348
349std::unique_ptr<Statement> Rehydrator::statement() {
350 int kind = this->readU8();
351 switch (kind) {
352 case Rehydrator::kBlock_Command: {
353 AutoRehydratorSymbolTable symbols(this);
354 int count = this->readU8();
John Stiles8f2a0cf2020-10-13 12:48:21 -0400355 StatementArray statements;
John Stilesf4bda742020-10-14 16:57:41 -0400356 statements.reserve_back(count);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400357 for (int i = 0; i < count; ++i) {
358 statements.push_back(this->statement());
359 }
360 bool isScope = this->readU8();
John Stilesbf16b6c2021-03-12 19:24:31 -0500361 return Block::Make(/*offset=*/-1, std::move(statements), fSymbolTable, isScope);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400362 }
363 case Rehydrator::kBreak_Command:
John Stilesa0c04d62021-03-11 23:07:24 -0500364 return BreakStatement::Make(/*offset=*/-1);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400365 case Rehydrator::kContinue_Command:
John Stilesa0c04d62021-03-11 23:07:24 -0500366 return ContinueStatement::Make(/*offset=*/-1);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400367 case Rehydrator::kDiscard_Command:
John Stilesa0c04d62021-03-11 23:07:24 -0500368 return DiscardStatement::Make(/*offset=*/-1);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400369 case Rehydrator::kDo_Command: {
370 std::unique_ptr<Statement> stmt = this->statement();
371 std::unique_ptr<Expression> expr = this->expression();
John Stilesea5822e2021-02-26 11:18:20 -0500372 return DoStatement::Make(fContext, std::move(stmt), std::move(expr));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400373 }
374 case Rehydrator::kExpressionStatement_Command: {
375 std::unique_ptr<Expression> expr = this->expression();
John Stiles3e5871c2021-02-25 20:52:03 -0500376 return ExpressionStatement::Make(fContext, std::move(expr));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400377 }
378 case Rehydrator::kFor_Command: {
379 std::unique_ptr<Statement> initializer = this->statement();
380 std::unique_ptr<Expression> test = this->expression();
381 std::unique_ptr<Expression> next = this->expression();
382 std::unique_ptr<Statement> body = this->statement();
383 std::shared_ptr<SymbolTable> symbols = this->symbolTable();
John Stiles23521a82021-03-02 17:02:51 -0500384 return ForStatement::Make(fContext, /*offset=*/-1, std::move(initializer),
385 std::move(test), std::move(next), std::move(body),
386 std::move(symbols));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400387 }
388 case Rehydrator::kIf_Command: {
389 bool isStatic = this->readU8();
390 std::unique_ptr<Expression> test = this->expression();
391 std::unique_ptr<Statement> ifTrue = this->statement();
392 std::unique_ptr<Statement> ifFalse = this->statement();
John Stilescf3059e2021-02-25 14:27:02 -0500393 return IfStatement::Make(fContext, /*offset=*/-1, isStatic, std::move(test),
394 std::move(ifTrue), std::move(ifFalse));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400395 }
John Stiles98c1f822020-09-09 14:18:53 -0400396 case Rehydrator::kInlineMarker_Command: {
397 const FunctionDeclaration* funcDecl = this->symbolRef<FunctionDeclaration>(
398 Symbol::Kind::kFunctionDeclaration);
John Stilesa0c04d62021-03-11 23:07:24 -0500399 return InlineMarker::Make(funcDecl);
John Stiles98c1f822020-09-09 14:18:53 -0400400 }
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400401 case Rehydrator::kReturn_Command: {
402 std::unique_ptr<Expression> expr = this->expression();
John Stilesa0c04d62021-03-11 23:07:24 -0500403 return ReturnStatement::Make(/*offset=*/-1, std::move(expr));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400404 }
405 case Rehydrator::kSwitch_Command: {
406 bool isStatic = this->readU8();
407 AutoRehydratorSymbolTable symbols(this);
408 std::unique_ptr<Expression> expr = this->expression();
409 int caseCount = this->readU8();
John Stilesb23a64b2021-03-11 08:27:59 -0500410 StatementArray cases;
411 cases.reserve_back(caseCount);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400412 for (int i = 0; i < caseCount; ++i) {
413 std::unique_ptr<Expression> value = this->expression();
John Stilesc3ce43b2021-03-09 15:37:01 -0500414 std::unique_ptr<Statement> statement = this->statement();
John Stiles8f2a0cf2020-10-13 12:48:21 -0400415 cases.push_back(std::make_unique<SwitchCase>(/*offset=*/-1, std::move(value),
John Stilesc3ce43b2021-03-09 15:37:01 -0500416 std::move(statement)));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400417 }
John Stiles23521a82021-03-02 17:02:51 -0500418 return SwitchStatement::Make(fContext, /*offset=*/-1, isStatic, std::move(expr),
419 std::move(cases), fSymbolTable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400420 }
421 case Rehydrator::kVarDeclaration_Command: {
Ethan Nicholase6592142020-09-08 10:22:09 -0400422 Variable* var = this->symbolRef<Variable>(Symbol::Kind::kVariable);
Brian Osmanc0213602020-10-06 14:43:32 -0400423 const Type* baseType = this->type();
John Stiles62a56462020-12-03 10:41:58 -0500424 int arraySize = this->readS8();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400425 std::unique_ptr<Expression> value = this->expression();
John Stilese67bd132021-03-19 18:39:25 -0400426 return VarDeclaration::Make(fContext, var, baseType, arraySize, std::move(value));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400427 }
428 case Rehydrator::kVoid_Command:
429 return nullptr;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400430 default:
431 printf("unsupported statement %d\n", kind);
432 SkASSERT(false);
433 return nullptr;
434 }
435}
436
John Stilesd8eb8752021-04-01 11:49:10 -0400437ExpressionArray Rehydrator::expressionArray() {
438 uint8_t count = this->readU8();
439 ExpressionArray array;
440 array.reserve_back(count);
441 for (int i = 0; i < count; ++i) {
442 array.push_back(this->expression());
443 }
444 return array;
445}
446
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400447std::unique_ptr<Expression> Rehydrator::expression() {
448 int kind = this->readU8();
449 switch (kind) {
450 case Rehydrator::kBinary_Command: {
451 std::unique_ptr<Expression> left = this->expression();
452 Token::Kind op = (Token::Kind) this->readU8();
453 std::unique_ptr<Expression> right = this->expression();
John Stilese2aec432021-03-01 09:27:48 -0500454 return BinaryExpression::Make(fContext, std::move(left), op, std::move(right));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400455 }
456 case Rehydrator::kBoolLiteral_Command: {
457 bool value = this->readU8();
John Stiles9ce80f72021-03-11 22:35:19 -0500458 return BoolLiteral::Make(fContext, /*offset=*/-1, value);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400459 }
460 case Rehydrator::kConstructor_Command: {
461 const Type* type = this->type();
John Stilesd8eb8752021-04-01 11:49:10 -0400462 ExpressionArray args = this->expressionArray();
John Stiles23521a82021-03-02 17:02:51 -0500463 auto ctor = Constructor::Convert(fContext, /*offset=*/-1, *type, std::move(args));
464 SkASSERT(ctor);
465 return ctor;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400466 }
John Stiles4118f142021-04-01 16:42:35 -0400467 case Rehydrator::kConstructorArray_Command: {
468 const Type* type = this->type();
469 return ConstructorArray::Make(fContext, /*offset=*/-1, *type, this->expressionArray());
470 }
John Stiles2bec8ab2021-04-06 18:40:04 -0400471 case Rehydrator::kConstructorComposite_Command: {
472 const Type* type = this->type();
473 return ConstructorComposite::Make(fContext, /*offset=*/-1, *type,
474 this->expressionArray());
475 }
John Stilese1182782021-03-30 22:09:37 -0400476 case Rehydrator::kConstructorDiagonalMatrix_Command: {
477 const Type* type = this->type();
John Stilesd8eb8752021-04-01 11:49:10 -0400478 ExpressionArray args = this->expressionArray();
479 SkASSERT(args.size() == 1);
John Stilese1182782021-03-30 22:09:37 -0400480 return ConstructorDiagonalMatrix::Make(fContext, /*offset=*/-1, *type,
John Stilesd8eb8752021-04-01 11:49:10 -0400481 std::move(args[0]));
John Stilese1182782021-03-30 22:09:37 -0400482 }
John Stiles5abb9e12021-04-06 13:47:19 -0400483 case Rehydrator::kConstructorMatrixResize_Command: {
484 const Type* type = this->type();
485 ExpressionArray args = this->expressionArray();
486 SkASSERT(args.size() == 1);
487 return ConstructorMatrixResize::Make(fContext, /*offset=*/-1, *type,
488 std::move(args[0]));
489 }
John Stilesfd7252f2021-04-04 22:24:40 -0400490 case Rehydrator::kConstructorScalarCast_Command: {
491 const Type* type = this->type();
492 ExpressionArray args = this->expressionArray();
493 SkASSERT(args.size() == 1);
494 return ConstructorScalarCast::Make(fContext, /*offset=*/-1, *type, std::move(args[0]));
495 }
John Stiles2938eea2021-04-01 18:58:25 -0400496 case Rehydrator::kConstructorSplat_Command: {
497 const Type* type = this->type();
498 ExpressionArray args = this->expressionArray();
499 SkASSERT(args.size() == 1);
500 return ConstructorSplat::Make(fContext, /*offset=*/-1, *type, std::move(args[0]));
501 }
John Stilesb14a8192021-04-05 11:40:46 -0400502 case Rehydrator::kConstructorVectorCast_Command: {
503 const Type* type = this->type();
504 ExpressionArray args = this->expressionArray();
505 SkASSERT(args.size() == 1);
506 return ConstructorVectorCast::Make(fContext, /*offset=*/-1, *type, std::move(args[0]));
507 }
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400508 case Rehydrator::kFieldAccess_Command: {
509 std::unique_ptr<Expression> base = this->expression();
510 int index = this->readU8();
511 FieldAccess::OwnerKind ownerKind = (FieldAccess::OwnerKind) this->readU8();
John Stiles06d600f2021-03-08 09:18:21 -0500512 return FieldAccess::Make(fContext, std::move(base), index, ownerKind);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400513 }
514 case Rehydrator::kFloatLiteral_Command: {
Brian Osmanfb964a42020-11-18 10:45:52 -0500515 const Type* type = this->type();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400516 FloatIntUnion u;
517 u.fInt = this->readS32();
John Stiles9ce80f72021-03-11 22:35:19 -0500518 return FloatLiteral::Make(/*offset=*/-1, u.fFloat, type);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400519 }
520 case Rehydrator::kFunctionCall_Command: {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400521 const Type* type = this->type();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400522 const FunctionDeclaration* f = this->symbolRef<FunctionDeclaration>(
Ethan Nicholase6592142020-09-08 10:22:09 -0400523 Symbol::Kind::kFunctionDeclaration);
John Stilesd8eb8752021-04-01 11:49:10 -0400524 ExpressionArray args = this->expressionArray();
John Stilescd7ba502021-03-19 10:54:59 -0400525 return FunctionCall::Make(fContext, /*offset=*/-1, type, *f, std::move(args));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400526 }
527 case Rehydrator::kIndex_Command: {
528 std::unique_ptr<Expression> base = this->expression();
529 std::unique_ptr<Expression> index = this->expression();
John Stiles51d33982021-03-08 09:18:07 -0500530 return IndexExpression::Make(fContext, std::move(base), std::move(index));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400531 }
532 case Rehydrator::kIntLiteral_Command: {
Brian Osmanfb964a42020-11-18 10:45:52 -0500533 const Type* type = this->type();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400534 int value = this->readS32();
John Stiles9ce80f72021-03-11 22:35:19 -0500535 return IntLiteral::Make(/*offset=*/-1, value, type);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400536 }
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400537 case Rehydrator::kPostfix_Command: {
538 Token::Kind op = (Token::Kind) this->readU8();
539 std::unique_ptr<Expression> operand = this->expression();
John Stiles52d3b012021-02-26 15:56:48 -0500540 return PostfixExpression::Make(fContext, std::move(operand), op);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400541 }
542 case Rehydrator::kPrefix_Command: {
543 Token::Kind op = (Token::Kind) this->readU8();
544 std::unique_ptr<Expression> operand = this->expression();
John Stilesb0eb20f2021-02-26 15:29:33 -0500545 return PrefixExpression::Make(fContext, op, std::move(operand));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400546 }
547 case Rehydrator::kSetting_Command: {
548 StringFragment name = this->readString();
John Stiles23521a82021-03-02 17:02:51 -0500549 return Setting::Convert(fContext, /*offset=*/-1, name);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400550 }
551 case Rehydrator::kSwizzle_Command: {
552 std::unique_ptr<Expression> base = this->expression();
553 int count = this->readU8();
John Stiles750109b2020-10-30 13:45:46 -0400554 ComponentArray components;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400555 for (int i = 0; i < count; ++i) {
556 components.push_back(this->readU8());
557 }
John Stiles23521a82021-03-02 17:02:51 -0500558 return Swizzle::Make(fContext, std::move(base), components);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400559 }
560 case Rehydrator::kTernary_Command: {
561 std::unique_ptr<Expression> test = this->expression();
562 std::unique_ptr<Expression> ifTrue = this->expression();
563 std::unique_ptr<Expression> ifFalse = this->expression();
John Stiles90518f72021-02-26 20:44:54 -0500564 return TernaryExpression::Make(fContext, std::move(test),
565 std::move(ifTrue), std::move(ifFalse));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400566 }
567 case Rehydrator::kVariableReference_Command: {
Ethan Nicholase6592142020-09-08 10:22:09 -0400568 const Variable* var = this->symbolRef<Variable>(Symbol::Kind::kVariable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400569 VariableReference::RefKind refKind = (VariableReference::RefKind) this->readU8();
Brian Osman79457ef2020-09-24 15:01:27 -0400570 return std::make_unique<VariableReference>(-1, var, refKind);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400571 }
572 case Rehydrator::kVoid_Command:
573 return nullptr;
574 default:
575 printf("unsupported expression %d\n", kind);
576 SkASSERT(false);
577 return nullptr;
578 }
579}
580
Brian Osman1313d1a2020-09-08 10:34:30 -0400581std::shared_ptr<SymbolTable> Rehydrator::symbolTable(bool inherit) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400582 int command = this->readU8();
583 if (command == kVoid_Command) {
584 return nullptr;
585 }
586 SkASSERT(command == kSymbolTable_Command);
587 uint16_t ownedCount = this->readU16();
Brian Osman1313d1a2020-09-08 10:34:30 -0400588 std::shared_ptr<SymbolTable> oldTable = fSymbolTable;
John Stiles7c3515b2020-10-16 18:38:39 -0400589 std::shared_ptr<SymbolTable> result =
590 inherit ? std::make_shared<SymbolTable>(fSymbolTable, /*builtin=*/true)
591 : std::make_shared<SymbolTable>(fErrors, /*builtin=*/true);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400592 fSymbolTable = result;
Brian Osman5bf3e202020-10-13 10:34:18 -0400593 std::vector<const Symbol*> ownedSymbols;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400594 ownedSymbols.reserve(ownedCount);
595 for (int i = 0; i < ownedCount; ++i) {
596 ownedSymbols.push_back(this->symbol());
597 }
598 uint16_t symbolCount = this->readU16();
599 std::vector<std::pair<StringFragment, int>> symbols;
600 symbols.reserve(symbolCount);
601 for (int i = 0; i < symbolCount; ++i) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400602 int index = this->readU16();
John Stilesb8cc6652020-10-08 09:12:07 -0400603 fSymbolTable->addWithoutOwnership(ownedSymbols[index]);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400604 }
Brian Osman1313d1a2020-09-08 10:34:30 -0400605 fSymbolTable = oldTable;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400606 return result;
607}
608
John Stilesa6841be2020-08-06 14:11:56 -0400609} // namespace SkSL