blob: d296deedb2264073c059d98aff61cf6af4c53cf5 [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 Nicholasc18bb512020-07-28 14:46:53 -040013#include "src/sksl/ir/SkSLBinaryExpression.h"
14#include "src/sksl/ir/SkSLBreakStatement.h"
15#include "src/sksl/ir/SkSLContinueStatement.h"
16#include "src/sksl/ir/SkSLDiscardStatement.h"
17#include "src/sksl/ir/SkSLDoStatement.h"
18#include "src/sksl/ir/SkSLEnum.h"
19#include "src/sksl/ir/SkSLExpression.h"
20#include "src/sksl/ir/SkSLExpressionStatement.h"
21#include "src/sksl/ir/SkSLField.h"
22#include "src/sksl/ir/SkSLFieldAccess.h"
23#include "src/sksl/ir/SkSLFloatLiteral.h"
24#include "src/sksl/ir/SkSLForStatement.h"
25#include "src/sksl/ir/SkSLFunctionCall.h"
26#include "src/sksl/ir/SkSLFunctionDeclaration.h"
27#include "src/sksl/ir/SkSLFunctionDefinition.h"
28#include "src/sksl/ir/SkSLIfStatement.h"
29#include "src/sksl/ir/SkSLIndexExpression.h"
John Stiles98c1f822020-09-09 14:18:53 -040030#include "src/sksl/ir/SkSLInlineMarker.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040031#include "src/sksl/ir/SkSLIntLiteral.h"
32#include "src/sksl/ir/SkSLInterfaceBlock.h"
33#include "src/sksl/ir/SkSLModifiers.h"
34#include "src/sksl/ir/SkSLNullLiteral.h"
35#include "src/sksl/ir/SkSLPostfixExpression.h"
36#include "src/sksl/ir/SkSLPrefixExpression.h"
37#include "src/sksl/ir/SkSLProgramElement.h"
38#include "src/sksl/ir/SkSLReturnStatement.h"
39#include "src/sksl/ir/SkSLSetting.h"
40#include "src/sksl/ir/SkSLStatement.h"
41#include "src/sksl/ir/SkSLSwitchCase.h"
42#include "src/sksl/ir/SkSLSwitchStatement.h"
43#include "src/sksl/ir/SkSLSwizzle.h"
John Stiles49a547f2020-10-06 16:14:37 -040044#include "src/sksl/ir/SkSLSymbolAlias.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040045#include "src/sksl/ir/SkSLSymbolTable.h"
46#include "src/sksl/ir/SkSLTernaryExpression.h"
47#include "src/sksl/ir/SkSLType.h"
48#include "src/sksl/ir/SkSLUnresolvedFunction.h"
49#include "src/sksl/ir/SkSLVarDeclarations.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040050#include "src/sksl/ir/SkSLVariable.h"
51#include "src/sksl/ir/SkSLWhileStatement.h"
52
53namespace SkSL {
54
55class AutoRehydratorSymbolTable {
56public:
57 AutoRehydratorSymbolTable(Rehydrator* rehydrator)
58 : fRehydrator(rehydrator)
59 , fOldSymbols(fRehydrator->fSymbolTable) {
60 fRehydrator->fSymbolTable = fRehydrator->symbolTable();
61 }
62
63 ~AutoRehydratorSymbolTable() {
64 fRehydrator->fSymbolTable = std::move(fOldSymbols);
65 }
66
67private:
68 Rehydrator* fRehydrator;
69 std::shared_ptr<SymbolTable> fOldSymbols;
70};
71
72Layout Rehydrator::layout() {
73 switch (this->readU8()) {
74 case kBuiltinLayout_Command: {
75 Layout result;
76 result.fBuiltin = this->readS16();
77 return result;
78 }
79 case kDefaultLayout_Command:
80 return Layout();
81 case kLayout_Command: {
82 int flags = this->readU32();
83 int location = this->readS8();
84 int offset = this->readS8();
85 int binding = this->readS8();
86 int index = this->readS8();
87 int set = this->readS8();
88 int builtin = this->readS16();
89 int inputAttachmentIndex = this->readS8();
90 int format = this->readS8();
91 int primitive = this->readS8();
92 int maxVertices = this->readS8();
93 int invocations = this->readS8();
94 StringFragment marker = this->readString();
95 StringFragment when = this->readString();
96 int key = this->readS8();
97 int ctype = this->readS8();
98 return Layout(flags, location, offset, binding, index, set, builtin,
99 inputAttachmentIndex, (Layout::Format) format,
100 (Layout::Primitive) primitive, maxVertices, invocations, marker, when,
101 (Layout::Key) key, (Layout::CType) ctype);
102 }
103 default:
104 SkASSERT(false);
105 return Layout();
106 }
107}
108
109Modifiers Rehydrator::modifiers() {
110 switch (this->readU8()) {
111 case kDefaultModifiers_Command:
112 return Modifiers();
113 case kModifiers8Bit_Command: {
114 Layout l = this->layout();
115 int flags = this->readU8();
116 return Modifiers(l, flags);
117 }
118 case kModifiers_Command: {
119 Layout l = this->layout();
120 int flags = this->readS32();
121 return Modifiers(l, flags);
122 }
123 default:
124 SkASSERT(false);
125 return Modifiers();
126 }
127}
128
Brian Osman5bf3e202020-10-13 10:34:18 -0400129const Symbol* Rehydrator::symbol() {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400130 int kind = this->readU8();
131 switch (kind) {
132 case kArrayType_Command: {
133 uint16_t id = this->readU16();
134 const Type* componentType = this->type();
Brian Osmane8c26082020-10-01 17:22:45 -0400135 int8_t count = this->readS8();
136 String name = componentType->name();
137 if (count == Type::kUnsizedArray) {
138 name += "[]";
139 } else {
140 name += "[" + to_string(count) + "]";
141 }
Brian Osman5bf3e202020-10-13 10:34:18 -0400142 const Type* result = fSymbolTable->takeOwnershipOfSymbol(
Brian Osmane8c26082020-10-01 17:22:45 -0400143 std::make_unique<Type>(name, Type::TypeKind::kArray, *componentType, count));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400144 this->addSymbol(id, result);
145 return result;
146 }
147 case kEnumType_Command: {
148 uint16_t id = this->readU16();
149 StringFragment name = this->readString();
Brian Osman5bf3e202020-10-13 10:34:18 -0400150 const Type* result = fSymbolTable->takeOwnershipOfSymbol(
Ethan Nicholase6592142020-09-08 10:22:09 -0400151 std::make_unique<Type>(name, Type::TypeKind::kEnum));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400152 this->addSymbol(id, result);
153 return result;
154 }
155 case kFunctionDeclaration_Command: {
156 uint16_t id = this->readU16();
157 Modifiers modifiers = this->modifiers();
158 StringFragment name = this->readString();
159 int parameterCount = this->readU8();
Brian Osman5bf3e202020-10-13 10:34:18 -0400160 std::vector<const Variable*> parameters;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400161 parameters.reserve(parameterCount);
162 for (int i = 0; i < parameterCount; ++i) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400163 parameters.push_back(this->symbolRef<Variable>(Symbol::Kind::kVariable));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400164 }
165 const Type* returnType = this->type();
Brian Osman5bf3e202020-10-13 10:34:18 -0400166 const FunctionDeclaration* result =
John Stiles3ae071e2020-08-05 15:29:29 -0400167 fSymbolTable->takeOwnershipOfSymbol(std::make_unique<FunctionDeclaration>(
Ethan Nicholased84b732020-10-08 11:45:44 -0400168 /*offset=*/-1, fModifiers.handle(modifiers), name,
169 std::move(parameters), returnType, /*builtin=*/true));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400170 this->addSymbol(id, result);
171 return result;
172 }
173 case kField_Command: {
Ethan Nicholase6592142020-09-08 10:22:09 -0400174 const Variable* owner = this->symbolRef<Variable>(Symbol::Kind::kVariable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400175 uint8_t index = this->readU8();
Brian Osman5bf3e202020-10-13 10:34:18 -0400176 const Field* result = fSymbolTable->takeOwnershipOfSymbol(
Ethan Nicholase2c49992020-10-05 11:49:11 -0400177 std::make_unique<Field>(/*offset=*/-1, owner, index));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400178 return result;
179 }
180 case kNullableType_Command: {
181 uint16_t id = this->readU16();
182 const Type* base = this->type();
Brian Osman5bf3e202020-10-13 10:34:18 -0400183 const Type* result = fSymbolTable->takeOwnershipOfSymbol(
Ethan Nicholase6592142020-09-08 10:22:09 -0400184 std::make_unique<Type>(base->name() + "?", Type::TypeKind::kNullable, *base));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400185 this->addSymbol(id, result);
186 return result;
187 }
188 case kStructType_Command: {
189 uint16_t id = this->readU16();
190 StringFragment name = this->readString();
191 uint8_t fieldCount = this->readU8();
192 std::vector<Type::Field> fields;
193 fields.reserve(fieldCount);
194 for (int i = 0; i < fieldCount; ++i) {
195 Modifiers m = this->modifiers();
John Stilesf621e232020-08-25 13:33:02 -0400196 StringFragment fieldName = this->readString();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400197 const Type* type = this->type();
John Stilesf621e232020-08-25 13:33:02 -0400198 fields.emplace_back(m, fieldName, type);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400199 }
Brian Osman5bf3e202020-10-13 10:34:18 -0400200 const Type* result = fSymbolTable->takeOwnershipOfSymbol(
John Stiles3ae071e2020-08-05 15:29:29 -0400201 std::make_unique<Type>(/*offset=*/-1, name, std::move(fields)));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400202 this->addSymbol(id, result);
203 return result;
204 }
205 case kSymbolRef_Command: {
206 uint16_t id = this->readU16();
207 SkASSERT(fSymbols.size() > id);
208 return fSymbols[id];
209 }
John Stiles49a547f2020-10-06 16:14:37 -0400210 case kSymbolAlias_Command: {
211 uint16_t id = this->readU16();
212 StringFragment name = this->readString();
Brian Osman5bf3e202020-10-13 10:34:18 -0400213 const Symbol* origSymbol = this->symbol();
214 const SymbolAlias* symbolAlias = fSymbolTable->takeOwnershipOfSymbol(
John Stiles49a547f2020-10-06 16:14:37 -0400215 std::make_unique<SymbolAlias>(/*offset=*/-1, name, origSymbol));
216 this->addSymbol(id, symbolAlias);
217 return symbolAlias;
218 }
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400219 case kSystemType_Command: {
220 uint16_t id = this->readU16();
221 StringFragment name = this->readString();
Brian Osman5bf3e202020-10-13 10:34:18 -0400222 const Symbol* result = (*fSymbolTable)[name];
Ethan Nicholase6592142020-09-08 10:22:09 -0400223 SkASSERT(result && result->kind() == Symbol::Kind::kType);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400224 this->addSymbol(id, result);
225 return result;
226 }
227 case kUnresolvedFunction_Command: {
228 uint16_t id = this->readU16();
229 int length = this->readU8();
230 std::vector<const FunctionDeclaration*> functions;
231 functions.reserve(length);
232 for (int i = 0; i < length; ++i) {
233 const Symbol* f = this->symbol();
Ethan Nicholase6592142020-09-08 10:22:09 -0400234 SkASSERT(f && f->kind() == Symbol::Kind::kFunctionDeclaration);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400235 functions.push_back((const FunctionDeclaration*) f);
236 }
Brian Osman5bf3e202020-10-13 10:34:18 -0400237 const UnresolvedFunction* result = fSymbolTable->takeOwnershipOfSymbol(
John Stiles3ae071e2020-08-05 15:29:29 -0400238 std::make_unique<UnresolvedFunction>(std::move(functions)));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400239 this->addSymbol(id, result);
240 return result;
241 }
242 case kVariable_Command: {
243 uint16_t id = this->readU16();
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400244 ModifiersPool::Handle m = fModifiers.handle(this->modifiers());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400245 StringFragment name = this->readString();
246 const Type* type = this->type();
247 Variable::Storage storage = (Variable::Storage) this->readU8();
Brian Osman5bf3e202020-10-13 10:34:18 -0400248 const Variable* result = fSymbolTable->takeOwnershipOfSymbol(std::make_unique<Variable>(
Brian Osman3887a012020-09-30 13:22:27 -0400249 /*offset=*/-1, m, name, type, /*builtin=*/true, storage));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400250 this->addSymbol(id, result);
251 return result;
252 }
253 default:
254 printf("unsupported symbol %d\n", kind);
255 SkASSERT(false);
256 return nullptr;
257 }
258}
259
260const Type* Rehydrator::type() {
261 const Symbol* result = this->symbol();
Ethan Nicholase6592142020-09-08 10:22:09 -0400262 SkASSERT(result->kind() == Symbol::Kind::kType);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400263 return (const Type*) result;
264}
265
266std::vector<std::unique_ptr<ProgramElement>> Rehydrator::elements() {
267 SkDEBUGCODE(uint8_t command = )this->readU8();
268 SkASSERT(command == kElements_Command);
269 uint8_t count = this->readU8();
270 std::vector<std::unique_ptr<ProgramElement>> result;
271 result.reserve(count);
272 for (int i = 0; i < count; ++i) {
273 result.push_back(this->element());
274 }
275 return result;
276}
277
278std::unique_ptr<ProgramElement> Rehydrator::element() {
279 int kind = this->readU8();
280 switch (kind) {
281 case Rehydrator::kEnum_Command: {
282 StringFragment typeName = this->readString();
Brian Osman1313d1a2020-09-08 10:34:30 -0400283 std::shared_ptr<SymbolTable> symbols = this->symbolTable(/*inherit=*/false);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400284 for (auto& s : symbols->fOwnedSymbols) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400285 SkASSERT(s->kind() == Symbol::Kind::kVariable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400286 Variable& v = (Variable&) *s;
287 int value = this->readS32();
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400288 v.setInitialValue(symbols->takeOwnershipOfIRNode(
289 std::make_unique<IntLiteral>(fContext, /*offset=*/-1, value)));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400290 }
291 return std::unique_ptr<ProgramElement>(new Enum(-1, typeName, std::move(symbols)));
292 }
293 case Rehydrator::kFunctionDefinition_Command: {
294 const FunctionDeclaration* decl = this->symbolRef<FunctionDeclaration>(
Ethan Nicholase6592142020-09-08 10:22:09 -0400295 Symbol::Kind::kFunctionDeclaration);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400296 std::unique_ptr<Statement> body = this->statement();
John Stilesb8e010c2020-08-11 18:05:39 -0400297 std::unordered_set<const FunctionDeclaration*> refs;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400298 uint8_t refCount = this->readU8();
299 for (int i = 0; i < refCount; ++i) {
300 refs.insert(this->symbolRef<FunctionDeclaration>(
Ethan Nicholase6592142020-09-08 10:22:09 -0400301 Symbol::Kind::kFunctionDeclaration));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400302 }
Ethan Nicholas0a5d0962020-10-14 13:33:18 -0400303 FunctionDefinition* result = new FunctionDefinition(-1, decl, std::move(body),
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400304 std::move(refs));
Ethan Nicholased84b732020-10-08 11:45:44 -0400305 decl->setDefinition(result);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400306 return std::unique_ptr<ProgramElement>(result);
307 }
308 case Rehydrator::kInterfaceBlock_Command: {
309 const Symbol* var = this->symbol();
John Stiles87ae34e2020-10-13 12:50:11 -0400310 SkASSERT(var && var->is<Variable>());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400311 StringFragment typeName = this->readString();
312 StringFragment instanceName = this->readString();
313 uint8_t sizeCount = this->readU8();
John Stiles87ae34e2020-10-13 12:50:11 -0400314 ExpressionArray sizes;
John Stilesf4bda742020-10-14 16:57:41 -0400315 sizes.reserve_back(sizeCount);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400316 for (int i = 0; i < sizeCount; ++i) {
317 sizes.push_back(this->expression());
318 }
John Stiles87ae34e2020-10-13 12:50:11 -0400319 return std::make_unique<InterfaceBlock>(/*offset=*/-1, &var->as<Variable>(), typeName,
320 instanceName, std::move(sizes), nullptr);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400321 }
322 case Rehydrator::kVarDeclarations_Command: {
Brian Osmanc0213602020-10-06 14:43:32 -0400323 std::unique_ptr<Statement> decl = this->statement();
324 return std::unique_ptr<ProgramElement>(
325 new GlobalVarDeclaration(/*offset=*/-1, std::move(decl)));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400326 }
327 default:
328 printf("unsupported element %d\n", kind);
329 SkASSERT(false);
330 return nullptr;
331 }
332}
333
334std::unique_ptr<Statement> Rehydrator::statement() {
335 int kind = this->readU8();
336 switch (kind) {
337 case Rehydrator::kBlock_Command: {
338 AutoRehydratorSymbolTable symbols(this);
339 int count = this->readU8();
John Stiles8f2a0cf2020-10-13 12:48:21 -0400340 StatementArray statements;
John Stilesf4bda742020-10-14 16:57:41 -0400341 statements.reserve_back(count);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400342 for (int i = 0; i < count; ++i) {
343 statements.push_back(this->statement());
344 }
345 bool isScope = this->readU8();
John Stiles8f2a0cf2020-10-13 12:48:21 -0400346 return std::make_unique<Block>(/*offset=*/-1, std::move(statements), fSymbolTable,
347 isScope);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400348 }
349 case Rehydrator::kBreak_Command:
350 return std::unique_ptr<Statement>(new BreakStatement(-1));
351 case Rehydrator::kContinue_Command:
352 return std::unique_ptr<Statement>(new ContinueStatement(-1));
353 case Rehydrator::kDiscard_Command:
354 return std::unique_ptr<Statement>(new DiscardStatement(-1));
355 case Rehydrator::kDo_Command: {
356 std::unique_ptr<Statement> stmt = this->statement();
357 std::unique_ptr<Expression> expr = this->expression();
358 return std::unique_ptr<Statement>(new DoStatement(-1, std::move(stmt),
359 std::move(expr)));
360 }
361 case Rehydrator::kExpressionStatement_Command: {
362 std::unique_ptr<Expression> expr = this->expression();
363 return std::unique_ptr<Statement>(new ExpressionStatement(std::move(expr)));
364 }
365 case Rehydrator::kFor_Command: {
366 std::unique_ptr<Statement> initializer = this->statement();
367 std::unique_ptr<Expression> test = this->expression();
368 std::unique_ptr<Expression> next = this->expression();
369 std::unique_ptr<Statement> body = this->statement();
370 std::shared_ptr<SymbolTable> symbols = this->symbolTable();
371 return std::unique_ptr<Statement>(new ForStatement(-1, std::move(initializer),
372 std::move(test), std::move(next),
373 std::move(body),
374 std::move(symbols)));
375 }
376 case Rehydrator::kIf_Command: {
377 bool isStatic = this->readU8();
378 std::unique_ptr<Expression> test = this->expression();
379 std::unique_ptr<Statement> ifTrue = this->statement();
380 std::unique_ptr<Statement> ifFalse = this->statement();
381 return std::unique_ptr<Statement>(new IfStatement(-1, isStatic, std::move(test),
382 std::move(ifTrue),
383 std::move(ifFalse)));
384 }
John Stiles98c1f822020-09-09 14:18:53 -0400385 case Rehydrator::kInlineMarker_Command: {
386 const FunctionDeclaration* funcDecl = this->symbolRef<FunctionDeclaration>(
387 Symbol::Kind::kFunctionDeclaration);
Ethan Nicholasceb62142020-10-09 16:51:18 -0400388 return std::make_unique<InlineMarker>(funcDecl);
John Stiles98c1f822020-09-09 14:18:53 -0400389 }
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400390 case Rehydrator::kReturn_Command: {
391 std::unique_ptr<Expression> expr = this->expression();
392 if (expr) {
393 return std::unique_ptr<Statement>(new ReturnStatement(std::move(expr)));
394 } else {
395 return std::unique_ptr<Statement>(new ReturnStatement(-1));
396 }
397 }
398 case Rehydrator::kSwitch_Command: {
399 bool isStatic = this->readU8();
400 AutoRehydratorSymbolTable symbols(this);
401 std::unique_ptr<Expression> expr = this->expression();
402 int caseCount = this->readU8();
403 std::vector<std::unique_ptr<SwitchCase>> cases;
404 cases.reserve(caseCount);
405 for (int i = 0; i < caseCount; ++i) {
406 std::unique_ptr<Expression> value = this->expression();
407 int statementCount = this->readU8();
John Stiles8f2a0cf2020-10-13 12:48:21 -0400408 StatementArray statements;
John Stilesf4bda742020-10-14 16:57:41 -0400409 statements.reserve_back(statementCount);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400410 for (int j = 0; j < statementCount; ++j) {
411 statements.push_back(this->statement());
412 }
John Stiles8f2a0cf2020-10-13 12:48:21 -0400413 cases.push_back(std::make_unique<SwitchCase>(/*offset=*/-1, std::move(value),
414 std::move(statements)));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400415 }
John Stiles8f2a0cf2020-10-13 12:48:21 -0400416 return std::make_unique<SwitchStatement>(-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();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400422 uint8_t sizeCount = this->readU8();
John Stiles87ae34e2020-10-13 12:50:11 -0400423 ExpressionArray sizes;
John Stilesf4bda742020-10-14 16:57:41 -0400424 sizes.reserve_back(sizeCount);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400425 for (int i = 0; i < sizeCount; ++i) {
426 sizes.push_back(this->expression());
427 }
428 std::unique_ptr<Expression> value = this->expression();
429 if (value) {
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400430 var->setInitialValue(value.get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400431 }
John Stiles87ae34e2020-10-13 12:50:11 -0400432 return std::make_unique<VarDeclaration>(var, baseType, std::move(sizes),
433 std::move(value));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400434 }
435 case Rehydrator::kVoid_Command:
436 return nullptr;
437 case Rehydrator::kWhile_Command: {
438 std::unique_ptr<Expression> expr = this->expression();
439 std::unique_ptr<Statement> stmt = this->statement();
440 return std::unique_ptr<Statement>(new WhileStatement(-1, std::move(expr),
441 std::move(stmt)));
442 }
443 default:
444 printf("unsupported statement %d\n", kind);
445 SkASSERT(false);
446 return nullptr;
447 }
448}
449
450std::unique_ptr<Expression> Rehydrator::expression() {
451 int kind = this->readU8();
452 switch (kind) {
453 case Rehydrator::kBinary_Command: {
454 std::unique_ptr<Expression> left = this->expression();
455 Token::Kind op = (Token::Kind) this->readU8();
456 std::unique_ptr<Expression> right = this->expression();
457 const Type* type = this->type();
Ethan Nicholas30d30222020-09-11 12:27:26 -0400458 return std::make_unique<BinaryExpression>(-1, std::move(left), op, std::move(right),
459 type);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400460 }
461 case Rehydrator::kBoolLiteral_Command: {
462 bool value = this->readU8();
Ethan Nicholas30d30222020-09-11 12:27:26 -0400463 return std::make_unique<BoolLiteral>(fContext, -1, value);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400464 }
465 case Rehydrator::kConstructor_Command: {
466 const Type* type = this->type();
467 uint8_t argCount = this->readU8();
John Stiles8e3b6be2020-10-13 11:14:08 -0400468 ExpressionArray args;
John Stilesf4bda742020-10-14 16:57:41 -0400469 args.reserve_back(argCount);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400470 for (int i = 0; i < argCount; ++i) {
471 args.push_back(this->expression());
472 }
Ethan Nicholas30d30222020-09-11 12:27:26 -0400473 return std::make_unique<Constructor>(-1, type, std::move(args));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400474 }
475 case Rehydrator::kFieldAccess_Command: {
476 std::unique_ptr<Expression> base = this->expression();
477 int index = this->readU8();
478 FieldAccess::OwnerKind ownerKind = (FieldAccess::OwnerKind) this->readU8();
Ethan Nicholas30d30222020-09-11 12:27:26 -0400479 return std::make_unique<FieldAccess>(std::move(base), index, ownerKind);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400480 }
481 case Rehydrator::kFloatLiteral_Command: {
482 FloatIntUnion u;
483 u.fInt = this->readS32();
Ethan Nicholas30d30222020-09-11 12:27:26 -0400484 return std::make_unique<FloatLiteral>(fContext, -1, u.fFloat);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400485 }
486 case Rehydrator::kFunctionCall_Command: {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400487 const Type* type = this->type();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400488 const FunctionDeclaration* f = this->symbolRef<FunctionDeclaration>(
Ethan Nicholase6592142020-09-08 10:22:09 -0400489 Symbol::Kind::kFunctionDeclaration);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400490 uint8_t argCount = this->readU8();
John Stiles8e3b6be2020-10-13 11:14:08 -0400491 ExpressionArray args;
John Stilesf4bda742020-10-14 16:57:41 -0400492 args.reserve_back(argCount);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400493 for (int i = 0; i < argCount; ++i) {
494 args.push_back(this->expression());
495 }
Ethan Nicholas0dec9922020-10-05 15:51:52 -0400496 return std::make_unique<FunctionCall>(-1, type, f, std::move(args));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400497 }
498 case Rehydrator::kIndex_Command: {
499 std::unique_ptr<Expression> base = this->expression();
500 std::unique_ptr<Expression> index = this->expression();
Ethan Nicholas30d30222020-09-11 12:27:26 -0400501 return std::make_unique<IndexExpression>(fContext, std::move(base), std::move(index));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400502 }
503 case Rehydrator::kIntLiteral_Command: {
504 int value = this->readS32();
Ethan Nicholas30d30222020-09-11 12:27:26 -0400505 return std::make_unique<IntLiteral>(fContext, -1, value);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400506 }
507 case Rehydrator::kNullLiteral_Command:
Ethan Nicholas30d30222020-09-11 12:27:26 -0400508 return std::make_unique<NullLiteral>(fContext, -1);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400509 case Rehydrator::kPostfix_Command: {
510 Token::Kind op = (Token::Kind) this->readU8();
511 std::unique_ptr<Expression> operand = this->expression();
Ethan Nicholas30d30222020-09-11 12:27:26 -0400512 return std::make_unique<PostfixExpression>(std::move(operand), op);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400513 }
514 case Rehydrator::kPrefix_Command: {
515 Token::Kind op = (Token::Kind) this->readU8();
516 std::unique_ptr<Expression> operand = this->expression();
Ethan Nicholas30d30222020-09-11 12:27:26 -0400517 return std::make_unique<PrefixExpression>(op, std::move(operand));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400518 }
519 case Rehydrator::kSetting_Command: {
520 StringFragment name = this->readString();
Ethan Nicholas01ec7e82020-10-08 12:10:12 -0400521 const Type* type = this->type();
522 return std::make_unique<Setting>(-1, name, type);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400523 }
524 case Rehydrator::kSwizzle_Command: {
525 std::unique_ptr<Expression> base = this->expression();
526 int count = this->readU8();
527 std::vector<int> components;
528 components.reserve(count);
529 for (int i = 0; i < count; ++i) {
530 components.push_back(this->readU8());
531 }
Ethan Nicholas30d30222020-09-11 12:27:26 -0400532 return std::make_unique<Swizzle>(fContext, std::move(base), std::move(components));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400533 }
534 case Rehydrator::kTernary_Command: {
535 std::unique_ptr<Expression> test = this->expression();
536 std::unique_ptr<Expression> ifTrue = this->expression();
537 std::unique_ptr<Expression> ifFalse = this->expression();
Ethan Nicholasa02ce0c2020-09-21 12:37:02 -0400538 return std::make_unique<TernaryExpression>(-1, std::move(test), std::move(ifTrue),
539 std::move(ifFalse));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400540 }
541 case Rehydrator::kVariableReference_Command: {
Ethan Nicholase6592142020-09-08 10:22:09 -0400542 const Variable* var = this->symbolRef<Variable>(Symbol::Kind::kVariable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400543 VariableReference::RefKind refKind = (VariableReference::RefKind) this->readU8();
Brian Osman79457ef2020-09-24 15:01:27 -0400544 return std::make_unique<VariableReference>(-1, var, refKind);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400545 }
546 case Rehydrator::kVoid_Command:
547 return nullptr;
548 default:
549 printf("unsupported expression %d\n", kind);
550 SkASSERT(false);
551 return nullptr;
552 }
553}
554
Brian Osman1313d1a2020-09-08 10:34:30 -0400555std::shared_ptr<SymbolTable> Rehydrator::symbolTable(bool inherit) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400556 int command = this->readU8();
557 if (command == kVoid_Command) {
558 return nullptr;
559 }
560 SkASSERT(command == kSymbolTable_Command);
561 uint16_t ownedCount = this->readU16();
Brian Osman1313d1a2020-09-08 10:34:30 -0400562 std::shared_ptr<SymbolTable> oldTable = fSymbolTable;
563 std::shared_ptr<SymbolTable> result = inherit ? std::make_shared<SymbolTable>(fSymbolTable)
564 : std::make_shared<SymbolTable>(fErrors);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400565 fSymbolTable = result;
Brian Osman5bf3e202020-10-13 10:34:18 -0400566 std::vector<const Symbol*> ownedSymbols;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400567 ownedSymbols.reserve(ownedCount);
568 for (int i = 0; i < ownedCount; ++i) {
569 ownedSymbols.push_back(this->symbol());
570 }
571 uint16_t symbolCount = this->readU16();
572 std::vector<std::pair<StringFragment, int>> symbols;
573 symbols.reserve(symbolCount);
574 for (int i = 0; i < symbolCount; ++i) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400575 int index = this->readU16();
John Stilesb8cc6652020-10-08 09:12:07 -0400576 fSymbolTable->addWithoutOwnership(ownedSymbols[index]);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400577 }
Brian Osman1313d1a2020-09-08 10:34:30 -0400578 fSymbolTable = oldTable;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400579 return result;
580}
581
John Stilesa6841be2020-08-06 14:11:56 -0400582} // namespace SkSL