blob: 99e90643a08038377a73a31e06c6406db0310ad6 [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 Stilesfd7252f2021-04-04 22:24:40 -040021#include "src/sksl/ir/SkSLConstructorScalarCast.h"
John Stiles2938eea2021-04-01 18:58:25 -040022#include "src/sksl/ir/SkSLConstructorSplat.h"
John Stilesb14a8192021-04-05 11:40:46 -040023#include "src/sksl/ir/SkSLConstructorVectorCast.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040024#include "src/sksl/ir/SkSLContinueStatement.h"
25#include "src/sksl/ir/SkSLDiscardStatement.h"
26#include "src/sksl/ir/SkSLDoStatement.h"
27#include "src/sksl/ir/SkSLEnum.h"
28#include "src/sksl/ir/SkSLExpression.h"
29#include "src/sksl/ir/SkSLExpressionStatement.h"
30#include "src/sksl/ir/SkSLField.h"
31#include "src/sksl/ir/SkSLFieldAccess.h"
32#include "src/sksl/ir/SkSLFloatLiteral.h"
33#include "src/sksl/ir/SkSLForStatement.h"
34#include "src/sksl/ir/SkSLFunctionCall.h"
35#include "src/sksl/ir/SkSLFunctionDeclaration.h"
36#include "src/sksl/ir/SkSLFunctionDefinition.h"
37#include "src/sksl/ir/SkSLIfStatement.h"
38#include "src/sksl/ir/SkSLIndexExpression.h"
John Stiles98c1f822020-09-09 14:18:53 -040039#include "src/sksl/ir/SkSLInlineMarker.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040040#include "src/sksl/ir/SkSLIntLiteral.h"
41#include "src/sksl/ir/SkSLInterfaceBlock.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040042#include "src/sksl/ir/SkSLPostfixExpression.h"
43#include "src/sksl/ir/SkSLPrefixExpression.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040044#include "src/sksl/ir/SkSLReturnStatement.h"
45#include "src/sksl/ir/SkSLSetting.h"
John Stilesdc75a972020-11-25 16:24:55 -050046#include "src/sksl/ir/SkSLStructDefinition.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040047#include "src/sksl/ir/SkSLSwitchCase.h"
48#include "src/sksl/ir/SkSLSwitchStatement.h"
49#include "src/sksl/ir/SkSLSwizzle.h"
John Stiles49a547f2020-10-06 16:14:37 -040050#include "src/sksl/ir/SkSLSymbolAlias.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040051#include "src/sksl/ir/SkSLSymbolTable.h"
52#include "src/sksl/ir/SkSLTernaryExpression.h"
53#include "src/sksl/ir/SkSLType.h"
54#include "src/sksl/ir/SkSLUnresolvedFunction.h"
55#include "src/sksl/ir/SkSLVarDeclarations.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040056#include "src/sksl/ir/SkSLVariable.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040057
58namespace SkSL {
59
60class AutoRehydratorSymbolTable {
61public:
62 AutoRehydratorSymbolTable(Rehydrator* rehydrator)
63 : fRehydrator(rehydrator)
64 , fOldSymbols(fRehydrator->fSymbolTable) {
65 fRehydrator->fSymbolTable = fRehydrator->symbolTable();
66 }
67
68 ~AutoRehydratorSymbolTable() {
69 fRehydrator->fSymbolTable = std::move(fOldSymbols);
70 }
71
72private:
73 Rehydrator* fRehydrator;
74 std::shared_ptr<SymbolTable> fOldSymbols;
75};
76
John Stiles7c3515b2020-10-16 18:38:39 -040077Rehydrator::Rehydrator(const Context* context, ModifiersPool* modifiers,
78 std::shared_ptr<SymbolTable> symbolTable, ErrorReporter* errorReporter,
79 const uint8_t* src, size_t length)
80 : fContext(*context)
81 , fModifiers(*modifiers)
82 , fErrors(errorReporter)
83 , fSymbolTable(std::move(symbolTable))
84 , fStart(src)
85 SkDEBUGCODE(, fEnd(fStart + length)) {
86 SkASSERT(fSymbolTable);
87 SkASSERT(fSymbolTable->isBuiltin());
88 // skip past string data
89 fIP = fStart;
90 fIP += this->readU16();
91}
92
Ethan Nicholasc18bb512020-07-28 14:46:53 -040093Layout Rehydrator::layout() {
94 switch (this->readU8()) {
95 case kBuiltinLayout_Command: {
96 Layout result;
97 result.fBuiltin = this->readS16();
98 return result;
99 }
100 case kDefaultLayout_Command:
101 return Layout();
102 case kLayout_Command: {
103 int flags = this->readU32();
104 int location = this->readS8();
105 int offset = this->readS8();
106 int binding = this->readS8();
107 int index = this->readS8();
108 int set = this->readS8();
109 int builtin = this->readS16();
110 int inputAttachmentIndex = this->readS8();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400111 int primitive = this->readS8();
112 int maxVertices = this->readS8();
113 int invocations = this->readS8();
114 StringFragment marker = this->readString();
115 StringFragment when = this->readString();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400116 int ctype = this->readS8();
117 return Layout(flags, location, offset, binding, index, set, builtin,
Brian Osman4717fbb2021-02-23 13:12:09 -0500118 inputAttachmentIndex, (Layout::Primitive)primitive, maxVertices,
Brian Osmanba7ef322021-02-23 13:37:22 -0500119 invocations, marker, when, (Layout::CType)ctype);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400120 }
121 default:
122 SkASSERT(false);
123 return Layout();
124 }
125}
126
127Modifiers Rehydrator::modifiers() {
128 switch (this->readU8()) {
129 case kDefaultModifiers_Command:
130 return Modifiers();
131 case kModifiers8Bit_Command: {
132 Layout l = this->layout();
133 int flags = this->readU8();
134 return Modifiers(l, flags);
135 }
136 case kModifiers_Command: {
137 Layout l = this->layout();
138 int flags = this->readS32();
139 return Modifiers(l, flags);
140 }
141 default:
142 SkASSERT(false);
143 return Modifiers();
144 }
145}
146
Brian Osman5bf3e202020-10-13 10:34:18 -0400147const Symbol* Rehydrator::symbol() {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400148 int kind = this->readU8();
149 switch (kind) {
150 case kArrayType_Command: {
151 uint16_t id = this->readU16();
152 const Type* componentType = this->type();
Brian Osmane8c26082020-10-01 17:22:45 -0400153 int8_t count = this->readS8();
154 String name = componentType->name();
155 if (count == Type::kUnsizedArray) {
156 name += "[]";
157 } else {
158 name += "[" + to_string(count) + "]";
159 }
Brian Osman5bf3e202020-10-13 10:34:18 -0400160 const Type* result = fSymbolTable->takeOwnershipOfSymbol(
John Stilesad2d4942020-12-11 16:55:58 -0500161 Type::MakeArrayType(name, *componentType, count));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400162 this->addSymbol(id, result);
163 return result;
164 }
165 case kEnumType_Command: {
166 uint16_t id = this->readU16();
167 StringFragment name = this->readString();
John Stilese6c67c52021-01-15 12:01:00 -0500168 const Type* result = fSymbolTable->takeOwnershipOfSymbol(Type::MakeEnumType(name));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400169 this->addSymbol(id, result);
170 return result;
171 }
172 case kFunctionDeclaration_Command: {
173 uint16_t id = this->readU16();
174 Modifiers modifiers = this->modifiers();
175 StringFragment name = this->readString();
176 int parameterCount = this->readU8();
Brian Osman5bf3e202020-10-13 10:34:18 -0400177 std::vector<const Variable*> parameters;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400178 parameters.reserve(parameterCount);
179 for (int i = 0; i < parameterCount; ++i) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400180 parameters.push_back(this->symbolRef<Variable>(Symbol::Kind::kVariable));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400181 }
182 const Type* returnType = this->type();
Brian Osman5bf3e202020-10-13 10:34:18 -0400183 const FunctionDeclaration* result =
John Stiles3ae071e2020-08-05 15:29:29 -0400184 fSymbolTable->takeOwnershipOfSymbol(std::make_unique<FunctionDeclaration>(
John Stiles586df952020-11-12 18:27:13 -0500185 /*offset=*/-1, fModifiers.addToPool(modifiers), name,
Ethan Nicholased84b732020-10-08 11:45:44 -0400186 std::move(parameters), returnType, /*builtin=*/true));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400187 this->addSymbol(id, result);
188 return result;
189 }
190 case kField_Command: {
Ethan Nicholase6592142020-09-08 10:22:09 -0400191 const Variable* owner = this->symbolRef<Variable>(Symbol::Kind::kVariable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400192 uint8_t index = this->readU8();
Brian Osman5bf3e202020-10-13 10:34:18 -0400193 const Field* result = fSymbolTable->takeOwnershipOfSymbol(
Ethan Nicholase2c49992020-10-05 11:49:11 -0400194 std::make_unique<Field>(/*offset=*/-1, owner, index));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400195 return result;
196 }
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400197 case kStructType_Command: {
198 uint16_t id = this->readU16();
199 StringFragment name = this->readString();
200 uint8_t fieldCount = this->readU8();
201 std::vector<Type::Field> fields;
202 fields.reserve(fieldCount);
203 for (int i = 0; i < fieldCount; ++i) {
204 Modifiers m = this->modifiers();
John Stilesf621e232020-08-25 13:33:02 -0400205 StringFragment fieldName = this->readString();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400206 const Type* type = this->type();
John Stilesf621e232020-08-25 13:33:02 -0400207 fields.emplace_back(m, fieldName, type);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400208 }
Brian Osman5bf3e202020-10-13 10:34:18 -0400209 const Type* result = fSymbolTable->takeOwnershipOfSymbol(
John Stilesad2d4942020-12-11 16:55:58 -0500210 Type::MakeStructType(/*offset=*/-1, name, std::move(fields)));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400211 this->addSymbol(id, result);
212 return result;
213 }
214 case kSymbolRef_Command: {
215 uint16_t id = this->readU16();
216 SkASSERT(fSymbols.size() > id);
217 return fSymbols[id];
218 }
John Stiles49a547f2020-10-06 16:14:37 -0400219 case kSymbolAlias_Command: {
220 uint16_t id = this->readU16();
221 StringFragment name = this->readString();
Brian Osman5bf3e202020-10-13 10:34:18 -0400222 const Symbol* origSymbol = this->symbol();
223 const SymbolAlias* symbolAlias = fSymbolTable->takeOwnershipOfSymbol(
John Stiles49a547f2020-10-06 16:14:37 -0400224 std::make_unique<SymbolAlias>(/*offset=*/-1, name, origSymbol));
225 this->addSymbol(id, symbolAlias);
226 return symbolAlias;
227 }
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400228 case kSystemType_Command: {
229 uint16_t id = this->readU16();
230 StringFragment name = this->readString();
Brian Osman5bf3e202020-10-13 10:34:18 -0400231 const Symbol* result = (*fSymbolTable)[name];
Ethan Nicholase6592142020-09-08 10:22:09 -0400232 SkASSERT(result && result->kind() == Symbol::Kind::kType);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400233 this->addSymbol(id, result);
234 return result;
235 }
236 case kUnresolvedFunction_Command: {
237 uint16_t id = this->readU16();
238 int length = this->readU8();
239 std::vector<const FunctionDeclaration*> functions;
240 functions.reserve(length);
241 for (int i = 0; i < length; ++i) {
242 const Symbol* f = this->symbol();
Ethan Nicholase6592142020-09-08 10:22:09 -0400243 SkASSERT(f && f->kind() == Symbol::Kind::kFunctionDeclaration);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400244 functions.push_back((const FunctionDeclaration*) f);
245 }
Brian Osman5bf3e202020-10-13 10:34:18 -0400246 const UnresolvedFunction* result = fSymbolTable->takeOwnershipOfSymbol(
John Stiles3ae071e2020-08-05 15:29:29 -0400247 std::make_unique<UnresolvedFunction>(std::move(functions)));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400248 this->addSymbol(id, result);
249 return result;
250 }
251 case kVariable_Command: {
252 uint16_t id = this->readU16();
John Stiles586df952020-11-12 18:27:13 -0500253 const Modifiers* m = fModifiers.addToPool(this->modifiers());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400254 StringFragment name = this->readString();
255 const Type* type = this->type();
256 Variable::Storage storage = (Variable::Storage) this->readU8();
Brian Osman5bf3e202020-10-13 10:34:18 -0400257 const Variable* result = fSymbolTable->takeOwnershipOfSymbol(std::make_unique<Variable>(
Brian Osman3887a012020-09-30 13:22:27 -0400258 /*offset=*/-1, m, name, type, /*builtin=*/true, storage));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400259 this->addSymbol(id, result);
260 return result;
261 }
262 default:
263 printf("unsupported symbol %d\n", kind);
264 SkASSERT(false);
265 return nullptr;
266 }
267}
268
269const Type* Rehydrator::type() {
270 const Symbol* result = this->symbol();
Ethan Nicholase6592142020-09-08 10:22:09 -0400271 SkASSERT(result->kind() == Symbol::Kind::kType);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400272 return (const Type*) result;
273}
274
275std::vector<std::unique_ptr<ProgramElement>> Rehydrator::elements() {
276 SkDEBUGCODE(uint8_t command = )this->readU8();
277 SkASSERT(command == kElements_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400278 std::vector<std::unique_ptr<ProgramElement>> result;
John Stiles1ea7f542020-11-02 13:07:23 -0500279 while (std::unique_ptr<ProgramElement> elem = this->element()) {
280 result.push_back(std::move(elem));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400281 }
282 return result;
283}
284
285std::unique_ptr<ProgramElement> Rehydrator::element() {
286 int kind = this->readU8();
287 switch (kind) {
288 case Rehydrator::kEnum_Command: {
289 StringFragment typeName = this->readString();
Brian Osman1313d1a2020-09-08 10:34:30 -0400290 std::shared_ptr<SymbolTable> symbols = this->symbolTable(/*inherit=*/false);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400291 for (auto& s : symbols->fOwnedSymbols) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400292 SkASSERT(s->kind() == Symbol::Kind::kVariable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400293 Variable& v = (Variable&) *s;
294 int value = this->readS32();
Ethan Nicholas5b9b0db2021-01-21 13:12:01 -0500295 // enum variables aren't really 'declared', but we have to create a declaration to
296 // store the value
John Stiles9ce80f72021-03-11 22:35:19 -0500297 auto valueLiteral = IntLiteral::Make(fContext, /*offset=*/-1, value);
John Stilese67bd132021-03-19 18:39:25 -0400298 auto declaration = VarDeclaration::Make(fContext, &v, &v.type(), /*arraySize=*/0,
299 std::move(valueLiteral));
Ethan Nicholas5b9b0db2021-01-21 13:12:01 -0500300 symbols->takeOwnershipOfIRNode(std::move(declaration));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400301 }
John Stiles1c823672020-10-20 10:23:50 -0400302 return std::make_unique<Enum>(/*offset=*/-1, typeName, std::move(symbols),
303 /*isSharedWithCpp=*/true, /*isBuiltin=*/true);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400304 }
305 case Rehydrator::kFunctionDefinition_Command: {
306 const FunctionDeclaration* decl = this->symbolRef<FunctionDeclaration>(
Ethan Nicholase6592142020-09-08 10:22:09 -0400307 Symbol::Kind::kFunctionDeclaration);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400308 std::unique_ptr<Statement> body = this->statement();
John Stilesb8e010c2020-08-11 18:05:39 -0400309 std::unordered_set<const FunctionDeclaration*> refs;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400310 uint8_t refCount = this->readU8();
311 for (int i = 0; i < refCount; ++i) {
312 refs.insert(this->symbolRef<FunctionDeclaration>(
Ethan Nicholase6592142020-09-08 10:22:09 -0400313 Symbol::Kind::kFunctionDeclaration));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400314 }
John Stiles607d36b2020-10-19 15:00:01 -0400315 auto result = std::make_unique<FunctionDefinition>(/*offset=*/-1, decl,
316 /*builtin=*/true, std::move(body),
317 std::move(refs));
318 decl->setDefinition(result.get());
319 return std::move(result);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400320 }
321 case Rehydrator::kInterfaceBlock_Command: {
322 const Symbol* var = this->symbol();
John Stiles87ae34e2020-10-13 12:50:11 -0400323 SkASSERT(var && var->is<Variable>());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400324 StringFragment typeName = this->readString();
325 StringFragment instanceName = this->readString();
John Stilesd39aec02020-12-03 10:42:26 -0500326 int arraySize = this->readS8();
John Stiles87ae34e2020-10-13 12:50:11 -0400327 return std::make_unique<InterfaceBlock>(/*offset=*/-1, &var->as<Variable>(), typeName,
John Stilesd39aec02020-12-03 10:42:26 -0500328 instanceName, arraySize, nullptr);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400329 }
330 case Rehydrator::kVarDeclarations_Command: {
Brian Osmanc0213602020-10-06 14:43:32 -0400331 std::unique_ptr<Statement> decl = this->statement();
John Stiles1ea7f542020-11-02 13:07:23 -0500332 return std::make_unique<GlobalVarDeclaration>(/*offset=*/-1, std::move(decl));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400333 }
John Stilesdc75a972020-11-25 16:24:55 -0500334 case Rehydrator::kStructDefinition_Command: {
335 const Symbol* type = this->symbol();
336 SkASSERT(type && type->is<Type>());
337 return std::make_unique<StructDefinition>(/*offset=*/-1, type->as<Type>());
338 }
John Stiles1ea7f542020-11-02 13:07:23 -0500339 case Rehydrator::kElementsComplete_Command:
340 return nullptr;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400341 default:
John Stiles1ea7f542020-11-02 13:07:23 -0500342 SkDEBUGFAILF("unsupported element %d\n", kind);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400343 return nullptr;
344 }
345}
346
347std::unique_ptr<Statement> Rehydrator::statement() {
348 int kind = this->readU8();
349 switch (kind) {
350 case Rehydrator::kBlock_Command: {
351 AutoRehydratorSymbolTable symbols(this);
352 int count = this->readU8();
John Stiles8f2a0cf2020-10-13 12:48:21 -0400353 StatementArray statements;
John Stilesf4bda742020-10-14 16:57:41 -0400354 statements.reserve_back(count);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400355 for (int i = 0; i < count; ++i) {
356 statements.push_back(this->statement());
357 }
358 bool isScope = this->readU8();
John Stilesbf16b6c2021-03-12 19:24:31 -0500359 return Block::Make(/*offset=*/-1, std::move(statements), fSymbolTable, isScope);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400360 }
361 case Rehydrator::kBreak_Command:
John Stilesa0c04d62021-03-11 23:07:24 -0500362 return BreakStatement::Make(/*offset=*/-1);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400363 case Rehydrator::kContinue_Command:
John Stilesa0c04d62021-03-11 23:07:24 -0500364 return ContinueStatement::Make(/*offset=*/-1);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400365 case Rehydrator::kDiscard_Command:
John Stilesa0c04d62021-03-11 23:07:24 -0500366 return DiscardStatement::Make(/*offset=*/-1);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400367 case Rehydrator::kDo_Command: {
368 std::unique_ptr<Statement> stmt = this->statement();
369 std::unique_ptr<Expression> expr = this->expression();
John Stilesea5822e2021-02-26 11:18:20 -0500370 return DoStatement::Make(fContext, std::move(stmt), std::move(expr));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400371 }
372 case Rehydrator::kExpressionStatement_Command: {
373 std::unique_ptr<Expression> expr = this->expression();
John Stiles3e5871c2021-02-25 20:52:03 -0500374 return ExpressionStatement::Make(fContext, std::move(expr));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400375 }
376 case Rehydrator::kFor_Command: {
377 std::unique_ptr<Statement> initializer = this->statement();
378 std::unique_ptr<Expression> test = this->expression();
379 std::unique_ptr<Expression> next = this->expression();
380 std::unique_ptr<Statement> body = this->statement();
381 std::shared_ptr<SymbolTable> symbols = this->symbolTable();
John Stiles23521a82021-03-02 17:02:51 -0500382 return ForStatement::Make(fContext, /*offset=*/-1, std::move(initializer),
383 std::move(test), std::move(next), std::move(body),
384 std::move(symbols));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400385 }
386 case Rehydrator::kIf_Command: {
387 bool isStatic = this->readU8();
388 std::unique_ptr<Expression> test = this->expression();
389 std::unique_ptr<Statement> ifTrue = this->statement();
390 std::unique_ptr<Statement> ifFalse = this->statement();
John Stilescf3059e2021-02-25 14:27:02 -0500391 return IfStatement::Make(fContext, /*offset=*/-1, isStatic, std::move(test),
392 std::move(ifTrue), std::move(ifFalse));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400393 }
John Stiles98c1f822020-09-09 14:18:53 -0400394 case Rehydrator::kInlineMarker_Command: {
395 const FunctionDeclaration* funcDecl = this->symbolRef<FunctionDeclaration>(
396 Symbol::Kind::kFunctionDeclaration);
John Stilesa0c04d62021-03-11 23:07:24 -0500397 return InlineMarker::Make(funcDecl);
John Stiles98c1f822020-09-09 14:18:53 -0400398 }
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400399 case Rehydrator::kReturn_Command: {
400 std::unique_ptr<Expression> expr = this->expression();
John Stilesa0c04d62021-03-11 23:07:24 -0500401 return ReturnStatement::Make(/*offset=*/-1, std::move(expr));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400402 }
403 case Rehydrator::kSwitch_Command: {
404 bool isStatic = this->readU8();
405 AutoRehydratorSymbolTable symbols(this);
406 std::unique_ptr<Expression> expr = this->expression();
407 int caseCount = this->readU8();
John Stilesb23a64b2021-03-11 08:27:59 -0500408 StatementArray cases;
409 cases.reserve_back(caseCount);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400410 for (int i = 0; i < caseCount; ++i) {
411 std::unique_ptr<Expression> value = this->expression();
John Stilesc3ce43b2021-03-09 15:37:01 -0500412 std::unique_ptr<Statement> statement = this->statement();
John Stiles8f2a0cf2020-10-13 12:48:21 -0400413 cases.push_back(std::make_unique<SwitchCase>(/*offset=*/-1, std::move(value),
John Stilesc3ce43b2021-03-09 15:37:01 -0500414 std::move(statement)));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400415 }
John Stiles23521a82021-03-02 17:02:51 -0500416 return SwitchStatement::Make(fContext, /*offset=*/-1, isStatic, std::move(expr),
417 std::move(cases), fSymbolTable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400418 }
419 case Rehydrator::kVarDeclaration_Command: {
Ethan Nicholase6592142020-09-08 10:22:09 -0400420 Variable* var = this->symbolRef<Variable>(Symbol::Kind::kVariable);
Brian Osmanc0213602020-10-06 14:43:32 -0400421 const Type* baseType = this->type();
John Stiles62a56462020-12-03 10:41:58 -0500422 int arraySize = this->readS8();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400423 std::unique_ptr<Expression> value = this->expression();
John Stilese67bd132021-03-19 18:39:25 -0400424 return VarDeclaration::Make(fContext, var, baseType, arraySize, std::move(value));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400425 }
426 case Rehydrator::kVoid_Command:
427 return nullptr;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400428 default:
429 printf("unsupported statement %d\n", kind);
430 SkASSERT(false);
431 return nullptr;
432 }
433}
434
John Stilesd8eb8752021-04-01 11:49:10 -0400435ExpressionArray Rehydrator::expressionArray() {
436 uint8_t count = this->readU8();
437 ExpressionArray array;
438 array.reserve_back(count);
439 for (int i = 0; i < count; ++i) {
440 array.push_back(this->expression());
441 }
442 return array;
443}
444
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400445std::unique_ptr<Expression> Rehydrator::expression() {
446 int kind = this->readU8();
447 switch (kind) {
448 case Rehydrator::kBinary_Command: {
449 std::unique_ptr<Expression> left = this->expression();
450 Token::Kind op = (Token::Kind) this->readU8();
451 std::unique_ptr<Expression> right = this->expression();
John Stilese2aec432021-03-01 09:27:48 -0500452 return BinaryExpression::Make(fContext, std::move(left), op, std::move(right));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400453 }
454 case Rehydrator::kBoolLiteral_Command: {
455 bool value = this->readU8();
John Stiles9ce80f72021-03-11 22:35:19 -0500456 return BoolLiteral::Make(fContext, /*offset=*/-1, value);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400457 }
458 case Rehydrator::kConstructor_Command: {
459 const Type* type = this->type();
John Stilesd8eb8752021-04-01 11:49:10 -0400460 ExpressionArray args = this->expressionArray();
John Stiles23521a82021-03-02 17:02:51 -0500461 auto ctor = Constructor::Convert(fContext, /*offset=*/-1, *type, std::move(args));
462 SkASSERT(ctor);
463 return ctor;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400464 }
John Stiles4118f142021-04-01 16:42:35 -0400465 case Rehydrator::kConstructorArray_Command: {
466 const Type* type = this->type();
467 return ConstructorArray::Make(fContext, /*offset=*/-1, *type, this->expressionArray());
468 }
John Stilese1182782021-03-30 22:09:37 -0400469 case Rehydrator::kConstructorDiagonalMatrix_Command: {
470 const Type* type = this->type();
John Stilesd8eb8752021-04-01 11:49:10 -0400471 ExpressionArray args = this->expressionArray();
472 SkASSERT(args.size() == 1);
John Stilese1182782021-03-30 22:09:37 -0400473 return ConstructorDiagonalMatrix::Make(fContext, /*offset=*/-1, *type,
John Stilesd8eb8752021-04-01 11:49:10 -0400474 std::move(args[0]));
John Stilese1182782021-03-30 22:09:37 -0400475 }
John Stilesfd7252f2021-04-04 22:24:40 -0400476 case Rehydrator::kConstructorScalarCast_Command: {
477 const Type* type = this->type();
478 ExpressionArray args = this->expressionArray();
479 SkASSERT(args.size() == 1);
480 return ConstructorScalarCast::Make(fContext, /*offset=*/-1, *type, std::move(args[0]));
481 }
John Stiles2938eea2021-04-01 18:58:25 -0400482 case Rehydrator::kConstructorSplat_Command: {
483 const Type* type = this->type();
484 ExpressionArray args = this->expressionArray();
485 SkASSERT(args.size() == 1);
486 return ConstructorSplat::Make(fContext, /*offset=*/-1, *type, std::move(args[0]));
487 }
John Stilesb14a8192021-04-05 11:40:46 -0400488 case Rehydrator::kConstructorVectorCast_Command: {
489 const Type* type = this->type();
490 ExpressionArray args = this->expressionArray();
491 SkASSERT(args.size() == 1);
492 return ConstructorVectorCast::Make(fContext, /*offset=*/-1, *type, std::move(args[0]));
493 }
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400494 case Rehydrator::kFieldAccess_Command: {
495 std::unique_ptr<Expression> base = this->expression();
496 int index = this->readU8();
497 FieldAccess::OwnerKind ownerKind = (FieldAccess::OwnerKind) this->readU8();
John Stiles06d600f2021-03-08 09:18:21 -0500498 return FieldAccess::Make(fContext, std::move(base), index, ownerKind);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400499 }
500 case Rehydrator::kFloatLiteral_Command: {
Brian Osmanfb964a42020-11-18 10:45:52 -0500501 const Type* type = this->type();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400502 FloatIntUnion u;
503 u.fInt = this->readS32();
John Stiles9ce80f72021-03-11 22:35:19 -0500504 return FloatLiteral::Make(/*offset=*/-1, u.fFloat, type);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400505 }
506 case Rehydrator::kFunctionCall_Command: {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400507 const Type* type = this->type();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400508 const FunctionDeclaration* f = this->symbolRef<FunctionDeclaration>(
Ethan Nicholase6592142020-09-08 10:22:09 -0400509 Symbol::Kind::kFunctionDeclaration);
John Stilesd8eb8752021-04-01 11:49:10 -0400510 ExpressionArray args = this->expressionArray();
John Stilescd7ba502021-03-19 10:54:59 -0400511 return FunctionCall::Make(fContext, /*offset=*/-1, type, *f, std::move(args));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400512 }
513 case Rehydrator::kIndex_Command: {
514 std::unique_ptr<Expression> base = this->expression();
515 std::unique_ptr<Expression> index = this->expression();
John Stiles51d33982021-03-08 09:18:07 -0500516 return IndexExpression::Make(fContext, std::move(base), std::move(index));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400517 }
518 case Rehydrator::kIntLiteral_Command: {
Brian Osmanfb964a42020-11-18 10:45:52 -0500519 const Type* type = this->type();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400520 int value = this->readS32();
John Stiles9ce80f72021-03-11 22:35:19 -0500521 return IntLiteral::Make(/*offset=*/-1, value, type);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400522 }
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400523 case Rehydrator::kPostfix_Command: {
524 Token::Kind op = (Token::Kind) this->readU8();
525 std::unique_ptr<Expression> operand = this->expression();
John Stiles52d3b012021-02-26 15:56:48 -0500526 return PostfixExpression::Make(fContext, std::move(operand), op);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400527 }
528 case Rehydrator::kPrefix_Command: {
529 Token::Kind op = (Token::Kind) this->readU8();
530 std::unique_ptr<Expression> operand = this->expression();
John Stilesb0eb20f2021-02-26 15:29:33 -0500531 return PrefixExpression::Make(fContext, op, std::move(operand));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400532 }
533 case Rehydrator::kSetting_Command: {
534 StringFragment name = this->readString();
John Stiles23521a82021-03-02 17:02:51 -0500535 return Setting::Convert(fContext, /*offset=*/-1, name);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400536 }
537 case Rehydrator::kSwizzle_Command: {
538 std::unique_ptr<Expression> base = this->expression();
539 int count = this->readU8();
John Stiles750109b2020-10-30 13:45:46 -0400540 ComponentArray components;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400541 for (int i = 0; i < count; ++i) {
542 components.push_back(this->readU8());
543 }
John Stiles23521a82021-03-02 17:02:51 -0500544 return Swizzle::Make(fContext, std::move(base), components);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400545 }
546 case Rehydrator::kTernary_Command: {
547 std::unique_ptr<Expression> test = this->expression();
548 std::unique_ptr<Expression> ifTrue = this->expression();
549 std::unique_ptr<Expression> ifFalse = this->expression();
John Stiles90518f72021-02-26 20:44:54 -0500550 return TernaryExpression::Make(fContext, std::move(test),
551 std::move(ifTrue), std::move(ifFalse));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400552 }
553 case Rehydrator::kVariableReference_Command: {
Ethan Nicholase6592142020-09-08 10:22:09 -0400554 const Variable* var = this->symbolRef<Variable>(Symbol::Kind::kVariable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400555 VariableReference::RefKind refKind = (VariableReference::RefKind) this->readU8();
Brian Osman79457ef2020-09-24 15:01:27 -0400556 return std::make_unique<VariableReference>(-1, var, refKind);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400557 }
558 case Rehydrator::kVoid_Command:
559 return nullptr;
560 default:
561 printf("unsupported expression %d\n", kind);
562 SkASSERT(false);
563 return nullptr;
564 }
565}
566
Brian Osman1313d1a2020-09-08 10:34:30 -0400567std::shared_ptr<SymbolTable> Rehydrator::symbolTable(bool inherit) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400568 int command = this->readU8();
569 if (command == kVoid_Command) {
570 return nullptr;
571 }
572 SkASSERT(command == kSymbolTable_Command);
573 uint16_t ownedCount = this->readU16();
Brian Osman1313d1a2020-09-08 10:34:30 -0400574 std::shared_ptr<SymbolTable> oldTable = fSymbolTable;
John Stiles7c3515b2020-10-16 18:38:39 -0400575 std::shared_ptr<SymbolTable> result =
576 inherit ? std::make_shared<SymbolTable>(fSymbolTable, /*builtin=*/true)
577 : std::make_shared<SymbolTable>(fErrors, /*builtin=*/true);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400578 fSymbolTable = result;
Brian Osman5bf3e202020-10-13 10:34:18 -0400579 std::vector<const Symbol*> ownedSymbols;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400580 ownedSymbols.reserve(ownedCount);
581 for (int i = 0; i < ownedCount; ++i) {
582 ownedSymbols.push_back(this->symbol());
583 }
584 uint16_t symbolCount = this->readU16();
585 std::vector<std::pair<StringFragment, int>> symbols;
586 symbols.reserve(symbolCount);
587 for (int i = 0; i < symbolCount; ++i) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400588 int index = this->readU16();
John Stilesb8cc6652020-10-08 09:12:07 -0400589 fSymbolTable->addWithoutOwnership(ownedSymbols[index]);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400590 }
Brian Osman1313d1a2020-09-08 10:34:30 -0400591 fSymbolTable = oldTable;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400592 return result;
593}
594
John Stilesa6841be2020-08-06 14:11:56 -0400595} // namespace SkSL