blob: 335a7c3e759f74616c31fa2d4604d515d47784d9 [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"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040023#include "src/sksl/ir/SkSLContinueStatement.h"
24#include "src/sksl/ir/SkSLDiscardStatement.h"
25#include "src/sksl/ir/SkSLDoStatement.h"
26#include "src/sksl/ir/SkSLEnum.h"
27#include "src/sksl/ir/SkSLExpression.h"
28#include "src/sksl/ir/SkSLExpressionStatement.h"
29#include "src/sksl/ir/SkSLField.h"
30#include "src/sksl/ir/SkSLFieldAccess.h"
31#include "src/sksl/ir/SkSLFloatLiteral.h"
32#include "src/sksl/ir/SkSLForStatement.h"
33#include "src/sksl/ir/SkSLFunctionCall.h"
34#include "src/sksl/ir/SkSLFunctionDeclaration.h"
35#include "src/sksl/ir/SkSLFunctionDefinition.h"
36#include "src/sksl/ir/SkSLIfStatement.h"
37#include "src/sksl/ir/SkSLIndexExpression.h"
John Stiles98c1f822020-09-09 14:18:53 -040038#include "src/sksl/ir/SkSLInlineMarker.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040039#include "src/sksl/ir/SkSLIntLiteral.h"
40#include "src/sksl/ir/SkSLInterfaceBlock.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040041#include "src/sksl/ir/SkSLPostfixExpression.h"
42#include "src/sksl/ir/SkSLPrefixExpression.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040043#include "src/sksl/ir/SkSLReturnStatement.h"
44#include "src/sksl/ir/SkSLSetting.h"
John Stilesdc75a972020-11-25 16:24:55 -050045#include "src/sksl/ir/SkSLStructDefinition.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040046#include "src/sksl/ir/SkSLSwitchCase.h"
47#include "src/sksl/ir/SkSLSwitchStatement.h"
48#include "src/sksl/ir/SkSLSwizzle.h"
John Stiles49a547f2020-10-06 16:14:37 -040049#include "src/sksl/ir/SkSLSymbolAlias.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040050#include "src/sksl/ir/SkSLSymbolTable.h"
51#include "src/sksl/ir/SkSLTernaryExpression.h"
52#include "src/sksl/ir/SkSLType.h"
53#include "src/sksl/ir/SkSLUnresolvedFunction.h"
54#include "src/sksl/ir/SkSLVarDeclarations.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040055#include "src/sksl/ir/SkSLVariable.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040056
57namespace SkSL {
58
59class AutoRehydratorSymbolTable {
60public:
61 AutoRehydratorSymbolTable(Rehydrator* rehydrator)
62 : fRehydrator(rehydrator)
63 , fOldSymbols(fRehydrator->fSymbolTable) {
64 fRehydrator->fSymbolTable = fRehydrator->symbolTable();
65 }
66
67 ~AutoRehydratorSymbolTable() {
68 fRehydrator->fSymbolTable = std::move(fOldSymbols);
69 }
70
71private:
72 Rehydrator* fRehydrator;
73 std::shared_ptr<SymbolTable> fOldSymbols;
74};
75
John Stiles7c3515b2020-10-16 18:38:39 -040076Rehydrator::Rehydrator(const Context* context, ModifiersPool* modifiers,
77 std::shared_ptr<SymbolTable> symbolTable, ErrorReporter* errorReporter,
78 const uint8_t* src, size_t length)
79 : fContext(*context)
80 , fModifiers(*modifiers)
81 , fErrors(errorReporter)
82 , fSymbolTable(std::move(symbolTable))
83 , fStart(src)
84 SkDEBUGCODE(, fEnd(fStart + length)) {
85 SkASSERT(fSymbolTable);
86 SkASSERT(fSymbolTable->isBuiltin());
87 // skip past string data
88 fIP = fStart;
89 fIP += this->readU16();
90}
91
Ethan Nicholasc18bb512020-07-28 14:46:53 -040092Layout Rehydrator::layout() {
93 switch (this->readU8()) {
94 case kBuiltinLayout_Command: {
95 Layout result;
96 result.fBuiltin = this->readS16();
97 return result;
98 }
99 case kDefaultLayout_Command:
100 return Layout();
101 case kLayout_Command: {
102 int flags = this->readU32();
103 int location = this->readS8();
104 int offset = this->readS8();
105 int binding = this->readS8();
106 int index = this->readS8();
107 int set = this->readS8();
108 int builtin = this->readS16();
109 int inputAttachmentIndex = this->readS8();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400110 int primitive = this->readS8();
111 int maxVertices = this->readS8();
112 int invocations = this->readS8();
113 StringFragment marker = this->readString();
114 StringFragment when = this->readString();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400115 int ctype = this->readS8();
116 return Layout(flags, location, offset, binding, index, set, builtin,
Brian Osman4717fbb2021-02-23 13:12:09 -0500117 inputAttachmentIndex, (Layout::Primitive)primitive, maxVertices,
Brian Osmanba7ef322021-02-23 13:37:22 -0500118 invocations, marker, when, (Layout::CType)ctype);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400119 }
120 default:
121 SkASSERT(false);
122 return Layout();
123 }
124}
125
126Modifiers Rehydrator::modifiers() {
127 switch (this->readU8()) {
128 case kDefaultModifiers_Command:
129 return Modifiers();
130 case kModifiers8Bit_Command: {
131 Layout l = this->layout();
132 int flags = this->readU8();
133 return Modifiers(l, flags);
134 }
135 case kModifiers_Command: {
136 Layout l = this->layout();
137 int flags = this->readS32();
138 return Modifiers(l, flags);
139 }
140 default:
141 SkASSERT(false);
142 return Modifiers();
143 }
144}
145
Brian Osman5bf3e202020-10-13 10:34:18 -0400146const Symbol* Rehydrator::symbol() {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400147 int kind = this->readU8();
148 switch (kind) {
149 case kArrayType_Command: {
150 uint16_t id = this->readU16();
151 const Type* componentType = this->type();
Brian Osmane8c26082020-10-01 17:22:45 -0400152 int8_t count = this->readS8();
153 String name = componentType->name();
154 if (count == Type::kUnsizedArray) {
155 name += "[]";
156 } else {
157 name += "[" + to_string(count) + "]";
158 }
Brian Osman5bf3e202020-10-13 10:34:18 -0400159 const Type* result = fSymbolTable->takeOwnershipOfSymbol(
John Stilesad2d4942020-12-11 16:55:58 -0500160 Type::MakeArrayType(name, *componentType, count));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400161 this->addSymbol(id, result);
162 return result;
163 }
164 case kEnumType_Command: {
165 uint16_t id = this->readU16();
166 StringFragment name = this->readString();
John Stilese6c67c52021-01-15 12:01:00 -0500167 const Type* result = fSymbolTable->takeOwnershipOfSymbol(Type::MakeEnumType(name));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400168 this->addSymbol(id, result);
169 return result;
170 }
171 case kFunctionDeclaration_Command: {
172 uint16_t id = this->readU16();
173 Modifiers modifiers = this->modifiers();
174 StringFragment name = this->readString();
175 int parameterCount = this->readU8();
Brian Osman5bf3e202020-10-13 10:34:18 -0400176 std::vector<const Variable*> parameters;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400177 parameters.reserve(parameterCount);
178 for (int i = 0; i < parameterCount; ++i) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400179 parameters.push_back(this->symbolRef<Variable>(Symbol::Kind::kVariable));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400180 }
181 const Type* returnType = this->type();
Brian Osman5bf3e202020-10-13 10:34:18 -0400182 const FunctionDeclaration* result =
John Stiles3ae071e2020-08-05 15:29:29 -0400183 fSymbolTable->takeOwnershipOfSymbol(std::make_unique<FunctionDeclaration>(
John Stiles586df952020-11-12 18:27:13 -0500184 /*offset=*/-1, fModifiers.addToPool(modifiers), name,
Ethan Nicholased84b732020-10-08 11:45:44 -0400185 std::move(parameters), returnType, /*builtin=*/true));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400186 this->addSymbol(id, result);
187 return result;
188 }
189 case kField_Command: {
Ethan Nicholase6592142020-09-08 10:22:09 -0400190 const Variable* owner = this->symbolRef<Variable>(Symbol::Kind::kVariable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400191 uint8_t index = this->readU8();
Brian Osman5bf3e202020-10-13 10:34:18 -0400192 const Field* result = fSymbolTable->takeOwnershipOfSymbol(
Ethan Nicholase2c49992020-10-05 11:49:11 -0400193 std::make_unique<Field>(/*offset=*/-1, owner, index));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400194 return result;
195 }
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400196 case kStructType_Command: {
197 uint16_t id = this->readU16();
198 StringFragment name = this->readString();
199 uint8_t fieldCount = this->readU8();
200 std::vector<Type::Field> fields;
201 fields.reserve(fieldCount);
202 for (int i = 0; i < fieldCount; ++i) {
203 Modifiers m = this->modifiers();
John Stilesf621e232020-08-25 13:33:02 -0400204 StringFragment fieldName = this->readString();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400205 const Type* type = this->type();
John Stilesf621e232020-08-25 13:33:02 -0400206 fields.emplace_back(m, fieldName, type);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400207 }
Brian Osman5bf3e202020-10-13 10:34:18 -0400208 const Type* result = fSymbolTable->takeOwnershipOfSymbol(
John Stilesad2d4942020-12-11 16:55:58 -0500209 Type::MakeStructType(/*offset=*/-1, name, std::move(fields)));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400210 this->addSymbol(id, result);
211 return result;
212 }
213 case kSymbolRef_Command: {
214 uint16_t id = this->readU16();
215 SkASSERT(fSymbols.size() > id);
216 return fSymbols[id];
217 }
John Stiles49a547f2020-10-06 16:14:37 -0400218 case kSymbolAlias_Command: {
219 uint16_t id = this->readU16();
220 StringFragment name = this->readString();
Brian Osman5bf3e202020-10-13 10:34:18 -0400221 const Symbol* origSymbol = this->symbol();
222 const SymbolAlias* symbolAlias = fSymbolTable->takeOwnershipOfSymbol(
John Stiles49a547f2020-10-06 16:14:37 -0400223 std::make_unique<SymbolAlias>(/*offset=*/-1, name, origSymbol));
224 this->addSymbol(id, symbolAlias);
225 return symbolAlias;
226 }
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400227 case kSystemType_Command: {
228 uint16_t id = this->readU16();
229 StringFragment name = this->readString();
Brian Osman5bf3e202020-10-13 10:34:18 -0400230 const Symbol* result = (*fSymbolTable)[name];
Ethan Nicholase6592142020-09-08 10:22:09 -0400231 SkASSERT(result && result->kind() == Symbol::Kind::kType);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400232 this->addSymbol(id, result);
233 return result;
234 }
235 case kUnresolvedFunction_Command: {
236 uint16_t id = this->readU16();
237 int length = this->readU8();
238 std::vector<const FunctionDeclaration*> functions;
239 functions.reserve(length);
240 for (int i = 0; i < length; ++i) {
241 const Symbol* f = this->symbol();
Ethan Nicholase6592142020-09-08 10:22:09 -0400242 SkASSERT(f && f->kind() == Symbol::Kind::kFunctionDeclaration);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400243 functions.push_back((const FunctionDeclaration*) f);
244 }
Brian Osman5bf3e202020-10-13 10:34:18 -0400245 const UnresolvedFunction* result = fSymbolTable->takeOwnershipOfSymbol(
John Stiles3ae071e2020-08-05 15:29:29 -0400246 std::make_unique<UnresolvedFunction>(std::move(functions)));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400247 this->addSymbol(id, result);
248 return result;
249 }
250 case kVariable_Command: {
251 uint16_t id = this->readU16();
John Stiles586df952020-11-12 18:27:13 -0500252 const Modifiers* m = fModifiers.addToPool(this->modifiers());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400253 StringFragment name = this->readString();
254 const Type* type = this->type();
255 Variable::Storage storage = (Variable::Storage) this->readU8();
Brian Osman5bf3e202020-10-13 10:34:18 -0400256 const Variable* result = fSymbolTable->takeOwnershipOfSymbol(std::make_unique<Variable>(
Brian Osman3887a012020-09-30 13:22:27 -0400257 /*offset=*/-1, m, name, type, /*builtin=*/true, storage));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400258 this->addSymbol(id, result);
259 return result;
260 }
261 default:
262 printf("unsupported symbol %d\n", kind);
263 SkASSERT(false);
264 return nullptr;
265 }
266}
267
268const Type* Rehydrator::type() {
269 const Symbol* result = this->symbol();
Ethan Nicholase6592142020-09-08 10:22:09 -0400270 SkASSERT(result->kind() == Symbol::Kind::kType);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400271 return (const Type*) result;
272}
273
274std::vector<std::unique_ptr<ProgramElement>> Rehydrator::elements() {
275 SkDEBUGCODE(uint8_t command = )this->readU8();
276 SkASSERT(command == kElements_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400277 std::vector<std::unique_ptr<ProgramElement>> result;
John Stiles1ea7f542020-11-02 13:07:23 -0500278 while (std::unique_ptr<ProgramElement> elem = this->element()) {
279 result.push_back(std::move(elem));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400280 }
281 return result;
282}
283
284std::unique_ptr<ProgramElement> Rehydrator::element() {
285 int kind = this->readU8();
286 switch (kind) {
287 case Rehydrator::kEnum_Command: {
288 StringFragment typeName = this->readString();
Brian Osman1313d1a2020-09-08 10:34:30 -0400289 std::shared_ptr<SymbolTable> symbols = this->symbolTable(/*inherit=*/false);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400290 for (auto& s : symbols->fOwnedSymbols) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400291 SkASSERT(s->kind() == Symbol::Kind::kVariable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400292 Variable& v = (Variable&) *s;
293 int value = this->readS32();
Ethan Nicholas5b9b0db2021-01-21 13:12:01 -0500294 // enum variables aren't really 'declared', but we have to create a declaration to
295 // store the value
John Stiles9ce80f72021-03-11 22:35:19 -0500296 auto valueLiteral = IntLiteral::Make(fContext, /*offset=*/-1, value);
John Stilese67bd132021-03-19 18:39:25 -0400297 auto declaration = VarDeclaration::Make(fContext, &v, &v.type(), /*arraySize=*/0,
298 std::move(valueLiteral));
Ethan Nicholas5b9b0db2021-01-21 13:12:01 -0500299 symbols->takeOwnershipOfIRNode(std::move(declaration));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400300 }
John Stiles1c823672020-10-20 10:23:50 -0400301 return std::make_unique<Enum>(/*offset=*/-1, typeName, std::move(symbols),
302 /*isSharedWithCpp=*/true, /*isBuiltin=*/true);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400303 }
304 case Rehydrator::kFunctionDefinition_Command: {
305 const FunctionDeclaration* decl = this->symbolRef<FunctionDeclaration>(
Ethan Nicholase6592142020-09-08 10:22:09 -0400306 Symbol::Kind::kFunctionDeclaration);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400307 std::unique_ptr<Statement> body = this->statement();
John Stilesb8e010c2020-08-11 18:05:39 -0400308 std::unordered_set<const FunctionDeclaration*> refs;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400309 uint8_t refCount = this->readU8();
310 for (int i = 0; i < refCount; ++i) {
311 refs.insert(this->symbolRef<FunctionDeclaration>(
Ethan Nicholase6592142020-09-08 10:22:09 -0400312 Symbol::Kind::kFunctionDeclaration));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400313 }
John Stiles607d36b2020-10-19 15:00:01 -0400314 auto result = std::make_unique<FunctionDefinition>(/*offset=*/-1, decl,
315 /*builtin=*/true, std::move(body),
316 std::move(refs));
317 decl->setDefinition(result.get());
318 return std::move(result);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400319 }
320 case Rehydrator::kInterfaceBlock_Command: {
321 const Symbol* var = this->symbol();
John Stiles87ae34e2020-10-13 12:50:11 -0400322 SkASSERT(var && var->is<Variable>());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400323 StringFragment typeName = this->readString();
324 StringFragment instanceName = this->readString();
John Stilesd39aec02020-12-03 10:42:26 -0500325 int arraySize = this->readS8();
John Stiles87ae34e2020-10-13 12:50:11 -0400326 return std::make_unique<InterfaceBlock>(/*offset=*/-1, &var->as<Variable>(), typeName,
John Stilesd39aec02020-12-03 10:42:26 -0500327 instanceName, arraySize, nullptr);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400328 }
329 case Rehydrator::kVarDeclarations_Command: {
Brian Osmanc0213602020-10-06 14:43:32 -0400330 std::unique_ptr<Statement> decl = this->statement();
John Stiles1ea7f542020-11-02 13:07:23 -0500331 return std::make_unique<GlobalVarDeclaration>(/*offset=*/-1, std::move(decl));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400332 }
John Stilesdc75a972020-11-25 16:24:55 -0500333 case Rehydrator::kStructDefinition_Command: {
334 const Symbol* type = this->symbol();
335 SkASSERT(type && type->is<Type>());
336 return std::make_unique<StructDefinition>(/*offset=*/-1, type->as<Type>());
337 }
John Stiles1ea7f542020-11-02 13:07:23 -0500338 case Rehydrator::kElementsComplete_Command:
339 return nullptr;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400340 default:
John Stiles1ea7f542020-11-02 13:07:23 -0500341 SkDEBUGFAILF("unsupported element %d\n", kind);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400342 return nullptr;
343 }
344}
345
346std::unique_ptr<Statement> Rehydrator::statement() {
347 int kind = this->readU8();
348 switch (kind) {
349 case Rehydrator::kBlock_Command: {
350 AutoRehydratorSymbolTable symbols(this);
351 int count = this->readU8();
John Stiles8f2a0cf2020-10-13 12:48:21 -0400352 StatementArray statements;
John Stilesf4bda742020-10-14 16:57:41 -0400353 statements.reserve_back(count);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400354 for (int i = 0; i < count; ++i) {
355 statements.push_back(this->statement());
356 }
357 bool isScope = this->readU8();
John Stilesbf16b6c2021-03-12 19:24:31 -0500358 return Block::Make(/*offset=*/-1, std::move(statements), fSymbolTable, isScope);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400359 }
360 case Rehydrator::kBreak_Command:
John Stilesa0c04d62021-03-11 23:07:24 -0500361 return BreakStatement::Make(/*offset=*/-1);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400362 case Rehydrator::kContinue_Command:
John Stilesa0c04d62021-03-11 23:07:24 -0500363 return ContinueStatement::Make(/*offset=*/-1);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400364 case Rehydrator::kDiscard_Command:
John Stilesa0c04d62021-03-11 23:07:24 -0500365 return DiscardStatement::Make(/*offset=*/-1);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400366 case Rehydrator::kDo_Command: {
367 std::unique_ptr<Statement> stmt = this->statement();
368 std::unique_ptr<Expression> expr = this->expression();
John Stilesea5822e2021-02-26 11:18:20 -0500369 return DoStatement::Make(fContext, std::move(stmt), std::move(expr));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400370 }
371 case Rehydrator::kExpressionStatement_Command: {
372 std::unique_ptr<Expression> expr = this->expression();
John Stiles3e5871c2021-02-25 20:52:03 -0500373 return ExpressionStatement::Make(fContext, std::move(expr));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400374 }
375 case Rehydrator::kFor_Command: {
376 std::unique_ptr<Statement> initializer = this->statement();
377 std::unique_ptr<Expression> test = this->expression();
378 std::unique_ptr<Expression> next = this->expression();
379 std::unique_ptr<Statement> body = this->statement();
380 std::shared_ptr<SymbolTable> symbols = this->symbolTable();
John Stiles23521a82021-03-02 17:02:51 -0500381 return ForStatement::Make(fContext, /*offset=*/-1, std::move(initializer),
382 std::move(test), std::move(next), std::move(body),
383 std::move(symbols));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400384 }
385 case Rehydrator::kIf_Command: {
386 bool isStatic = this->readU8();
387 std::unique_ptr<Expression> test = this->expression();
388 std::unique_ptr<Statement> ifTrue = this->statement();
389 std::unique_ptr<Statement> ifFalse = this->statement();
John Stilescf3059e2021-02-25 14:27:02 -0500390 return IfStatement::Make(fContext, /*offset=*/-1, isStatic, std::move(test),
391 std::move(ifTrue), std::move(ifFalse));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400392 }
John Stiles98c1f822020-09-09 14:18:53 -0400393 case Rehydrator::kInlineMarker_Command: {
394 const FunctionDeclaration* funcDecl = this->symbolRef<FunctionDeclaration>(
395 Symbol::Kind::kFunctionDeclaration);
John Stilesa0c04d62021-03-11 23:07:24 -0500396 return InlineMarker::Make(funcDecl);
John Stiles98c1f822020-09-09 14:18:53 -0400397 }
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400398 case Rehydrator::kReturn_Command: {
399 std::unique_ptr<Expression> expr = this->expression();
John Stilesa0c04d62021-03-11 23:07:24 -0500400 return ReturnStatement::Make(/*offset=*/-1, std::move(expr));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400401 }
402 case Rehydrator::kSwitch_Command: {
403 bool isStatic = this->readU8();
404 AutoRehydratorSymbolTable symbols(this);
405 std::unique_ptr<Expression> expr = this->expression();
406 int caseCount = this->readU8();
John Stilesb23a64b2021-03-11 08:27:59 -0500407 StatementArray cases;
408 cases.reserve_back(caseCount);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400409 for (int i = 0; i < caseCount; ++i) {
410 std::unique_ptr<Expression> value = this->expression();
John Stilesc3ce43b2021-03-09 15:37:01 -0500411 std::unique_ptr<Statement> statement = this->statement();
John Stiles8f2a0cf2020-10-13 12:48:21 -0400412 cases.push_back(std::make_unique<SwitchCase>(/*offset=*/-1, std::move(value),
John Stilesc3ce43b2021-03-09 15:37:01 -0500413 std::move(statement)));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400414 }
John Stiles23521a82021-03-02 17:02:51 -0500415 return SwitchStatement::Make(fContext, /*offset=*/-1, isStatic, std::move(expr),
416 std::move(cases), fSymbolTable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400417 }
418 case Rehydrator::kVarDeclaration_Command: {
Ethan Nicholase6592142020-09-08 10:22:09 -0400419 Variable* var = this->symbolRef<Variable>(Symbol::Kind::kVariable);
Brian Osmanc0213602020-10-06 14:43:32 -0400420 const Type* baseType = this->type();
John Stiles62a56462020-12-03 10:41:58 -0500421 int arraySize = this->readS8();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400422 std::unique_ptr<Expression> value = this->expression();
John Stilese67bd132021-03-19 18:39:25 -0400423 return VarDeclaration::Make(fContext, var, baseType, arraySize, std::move(value));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400424 }
425 case Rehydrator::kVoid_Command:
426 return nullptr;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400427 default:
428 printf("unsupported statement %d\n", kind);
429 SkASSERT(false);
430 return nullptr;
431 }
432}
433
John Stilesd8eb8752021-04-01 11:49:10 -0400434ExpressionArray Rehydrator::expressionArray() {
435 uint8_t count = this->readU8();
436 ExpressionArray array;
437 array.reserve_back(count);
438 for (int i = 0; i < count; ++i) {
439 array.push_back(this->expression());
440 }
441 return array;
442}
443
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400444std::unique_ptr<Expression> Rehydrator::expression() {
445 int kind = this->readU8();
446 switch (kind) {
447 case Rehydrator::kBinary_Command: {
448 std::unique_ptr<Expression> left = this->expression();
449 Token::Kind op = (Token::Kind) this->readU8();
450 std::unique_ptr<Expression> right = this->expression();
John Stilese2aec432021-03-01 09:27:48 -0500451 return BinaryExpression::Make(fContext, std::move(left), op, std::move(right));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400452 }
453 case Rehydrator::kBoolLiteral_Command: {
454 bool value = this->readU8();
John Stiles9ce80f72021-03-11 22:35:19 -0500455 return BoolLiteral::Make(fContext, /*offset=*/-1, value);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400456 }
457 case Rehydrator::kConstructor_Command: {
458 const Type* type = this->type();
John Stilesd8eb8752021-04-01 11:49:10 -0400459 ExpressionArray args = this->expressionArray();
John Stiles23521a82021-03-02 17:02:51 -0500460 auto ctor = Constructor::Convert(fContext, /*offset=*/-1, *type, std::move(args));
461 SkASSERT(ctor);
462 return ctor;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400463 }
John Stiles4118f142021-04-01 16:42:35 -0400464 case Rehydrator::kConstructorArray_Command: {
465 const Type* type = this->type();
466 return ConstructorArray::Make(fContext, /*offset=*/-1, *type, this->expressionArray());
467 }
John Stilese1182782021-03-30 22:09:37 -0400468 case Rehydrator::kConstructorDiagonalMatrix_Command: {
469 const Type* type = this->type();
John Stilesd8eb8752021-04-01 11:49:10 -0400470 ExpressionArray args = this->expressionArray();
471 SkASSERT(args.size() == 1);
John Stilese1182782021-03-30 22:09:37 -0400472 return ConstructorDiagonalMatrix::Make(fContext, /*offset=*/-1, *type,
John Stilesd8eb8752021-04-01 11:49:10 -0400473 std::move(args[0]));
John Stilese1182782021-03-30 22:09:37 -0400474 }
John Stilesfd7252f2021-04-04 22:24:40 -0400475 case Rehydrator::kConstructorScalarCast_Command: {
476 const Type* type = this->type();
477 ExpressionArray args = this->expressionArray();
478 SkASSERT(args.size() == 1);
479 return ConstructorScalarCast::Make(fContext, /*offset=*/-1, *type, std::move(args[0]));
480 }
John Stiles2938eea2021-04-01 18:58:25 -0400481 case Rehydrator::kConstructorSplat_Command: {
482 const Type* type = this->type();
483 ExpressionArray args = this->expressionArray();
484 SkASSERT(args.size() == 1);
485 return ConstructorSplat::Make(fContext, /*offset=*/-1, *type, std::move(args[0]));
486 }
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400487 case Rehydrator::kFieldAccess_Command: {
488 std::unique_ptr<Expression> base = this->expression();
489 int index = this->readU8();
490 FieldAccess::OwnerKind ownerKind = (FieldAccess::OwnerKind) this->readU8();
John Stiles06d600f2021-03-08 09:18:21 -0500491 return FieldAccess::Make(fContext, std::move(base), index, ownerKind);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400492 }
493 case Rehydrator::kFloatLiteral_Command: {
Brian Osmanfb964a42020-11-18 10:45:52 -0500494 const Type* type = this->type();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400495 FloatIntUnion u;
496 u.fInt = this->readS32();
John Stiles9ce80f72021-03-11 22:35:19 -0500497 return FloatLiteral::Make(/*offset=*/-1, u.fFloat, type);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400498 }
499 case Rehydrator::kFunctionCall_Command: {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400500 const Type* type = this->type();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400501 const FunctionDeclaration* f = this->symbolRef<FunctionDeclaration>(
Ethan Nicholase6592142020-09-08 10:22:09 -0400502 Symbol::Kind::kFunctionDeclaration);
John Stilesd8eb8752021-04-01 11:49:10 -0400503 ExpressionArray args = this->expressionArray();
John Stilescd7ba502021-03-19 10:54:59 -0400504 return FunctionCall::Make(fContext, /*offset=*/-1, type, *f, std::move(args));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400505 }
506 case Rehydrator::kIndex_Command: {
507 std::unique_ptr<Expression> base = this->expression();
508 std::unique_ptr<Expression> index = this->expression();
John Stiles51d33982021-03-08 09:18:07 -0500509 return IndexExpression::Make(fContext, std::move(base), std::move(index));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400510 }
511 case Rehydrator::kIntLiteral_Command: {
Brian Osmanfb964a42020-11-18 10:45:52 -0500512 const Type* type = this->type();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400513 int value = this->readS32();
John Stiles9ce80f72021-03-11 22:35:19 -0500514 return IntLiteral::Make(/*offset=*/-1, value, type);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400515 }
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400516 case Rehydrator::kPostfix_Command: {
517 Token::Kind op = (Token::Kind) this->readU8();
518 std::unique_ptr<Expression> operand = this->expression();
John Stiles52d3b012021-02-26 15:56:48 -0500519 return PostfixExpression::Make(fContext, std::move(operand), op);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400520 }
521 case Rehydrator::kPrefix_Command: {
522 Token::Kind op = (Token::Kind) this->readU8();
523 std::unique_ptr<Expression> operand = this->expression();
John Stilesb0eb20f2021-02-26 15:29:33 -0500524 return PrefixExpression::Make(fContext, op, std::move(operand));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400525 }
526 case Rehydrator::kSetting_Command: {
527 StringFragment name = this->readString();
John Stiles23521a82021-03-02 17:02:51 -0500528 return Setting::Convert(fContext, /*offset=*/-1, name);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400529 }
530 case Rehydrator::kSwizzle_Command: {
531 std::unique_ptr<Expression> base = this->expression();
532 int count = this->readU8();
John Stiles750109b2020-10-30 13:45:46 -0400533 ComponentArray components;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400534 for (int i = 0; i < count; ++i) {
535 components.push_back(this->readU8());
536 }
John Stiles23521a82021-03-02 17:02:51 -0500537 return Swizzle::Make(fContext, std::move(base), components);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400538 }
539 case Rehydrator::kTernary_Command: {
540 std::unique_ptr<Expression> test = this->expression();
541 std::unique_ptr<Expression> ifTrue = this->expression();
542 std::unique_ptr<Expression> ifFalse = this->expression();
John Stiles90518f72021-02-26 20:44:54 -0500543 return TernaryExpression::Make(fContext, std::move(test),
544 std::move(ifTrue), std::move(ifFalse));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400545 }
546 case Rehydrator::kVariableReference_Command: {
Ethan Nicholase6592142020-09-08 10:22:09 -0400547 const Variable* var = this->symbolRef<Variable>(Symbol::Kind::kVariable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400548 VariableReference::RefKind refKind = (VariableReference::RefKind) this->readU8();
Brian Osman79457ef2020-09-24 15:01:27 -0400549 return std::make_unique<VariableReference>(-1, var, refKind);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400550 }
551 case Rehydrator::kVoid_Command:
552 return nullptr;
553 default:
554 printf("unsupported expression %d\n", kind);
555 SkASSERT(false);
556 return nullptr;
557 }
558}
559
Brian Osman1313d1a2020-09-08 10:34:30 -0400560std::shared_ptr<SymbolTable> Rehydrator::symbolTable(bool inherit) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400561 int command = this->readU8();
562 if (command == kVoid_Command) {
563 return nullptr;
564 }
565 SkASSERT(command == kSymbolTable_Command);
566 uint16_t ownedCount = this->readU16();
Brian Osman1313d1a2020-09-08 10:34:30 -0400567 std::shared_ptr<SymbolTable> oldTable = fSymbolTable;
John Stiles7c3515b2020-10-16 18:38:39 -0400568 std::shared_ptr<SymbolTable> result =
569 inherit ? std::make_shared<SymbolTable>(fSymbolTable, /*builtin=*/true)
570 : std::make_shared<SymbolTable>(fErrors, /*builtin=*/true);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400571 fSymbolTable = result;
Brian Osman5bf3e202020-10-13 10:34:18 -0400572 std::vector<const Symbol*> ownedSymbols;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400573 ownedSymbols.reserve(ownedCount);
574 for (int i = 0; i < ownedCount; ++i) {
575 ownedSymbols.push_back(this->symbol());
576 }
577 uint16_t symbolCount = this->readU16();
578 std::vector<std::pair<StringFragment, int>> symbols;
579 symbols.reserve(symbolCount);
580 for (int i = 0; i < symbolCount; ++i) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400581 int index = this->readU16();
John Stilesb8cc6652020-10-08 09:12:07 -0400582 fSymbolTable->addWithoutOwnership(ownedSymbols[index]);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400583 }
Brian Osman1313d1a2020-09-08 10:34:30 -0400584 fSymbolTable = oldTable;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400585 return result;
586}
587
John Stilesa6841be2020-08-06 14:11:56 -0400588} // namespace SkSL