blob: d47c839c2f30f1e972ebabf48e83de8b53aea12e [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"
44#include "src/sksl/ir/SkSLSymbolTable.h"
45#include "src/sksl/ir/SkSLTernaryExpression.h"
46#include "src/sksl/ir/SkSLType.h"
47#include "src/sksl/ir/SkSLUnresolvedFunction.h"
48#include "src/sksl/ir/SkSLVarDeclarations.h"
49#include "src/sksl/ir/SkSLVarDeclarationsStatement.h"
50#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
129const Symbol* Rehydrator::symbol() {
130 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 }
John Stiles3ae071e2020-08-05 15:29:29 -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();
John Stiles3ae071e2020-08-05 15:29:29 -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();
160 std::vector<const Variable*> parameters;
161 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();
John Stiles3ae071e2020-08-05 15:29:29 -0400166 const FunctionDeclaration* result =
167 fSymbolTable->takeOwnershipOfSymbol(std::make_unique<FunctionDeclaration>(
168 /*offset=*/-1, modifiers, name, std::move(parameters), *returnType,
169 /*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();
John Stiles3ae071e2020-08-05 15:29:29 -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();
John Stiles3ae071e2020-08-05 15:29:29 -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 }
John Stiles3ae071e2020-08-05 15:29:29 -0400200 const Type* result = fSymbolTable->takeOwnershipOfSymbol(
201 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 }
210 case kSystemType_Command: {
211 uint16_t id = this->readU16();
212 StringFragment name = this->readString();
213 const Symbol* result = (*fSymbolTable)[name];
Ethan Nicholase6592142020-09-08 10:22:09 -0400214 SkASSERT(result && result->kind() == Symbol::Kind::kType);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400215 this->addSymbol(id, result);
216 return result;
217 }
218 case kUnresolvedFunction_Command: {
219 uint16_t id = this->readU16();
220 int length = this->readU8();
221 std::vector<const FunctionDeclaration*> functions;
222 functions.reserve(length);
223 for (int i = 0; i < length; ++i) {
224 const Symbol* f = this->symbol();
Ethan Nicholase6592142020-09-08 10:22:09 -0400225 SkASSERT(f && f->kind() == Symbol::Kind::kFunctionDeclaration);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400226 functions.push_back((const FunctionDeclaration*) f);
227 }
John Stiles3ae071e2020-08-05 15:29:29 -0400228 const UnresolvedFunction* result = fSymbolTable->takeOwnershipOfSymbol(
229 std::make_unique<UnresolvedFunction>(std::move(functions)));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400230 this->addSymbol(id, result);
231 return result;
232 }
233 case kVariable_Command: {
234 uint16_t id = this->readU16();
235 Modifiers m = this->modifiers();
236 StringFragment name = this->readString();
237 const Type* type = this->type();
238 Variable::Storage storage = (Variable::Storage) this->readU8();
Brian Osman3887a012020-09-30 13:22:27 -0400239 const Variable* result = fSymbolTable->takeOwnershipOfSymbol(std::make_unique<Variable>(
240 /*offset=*/-1, m, name, type, /*builtin=*/true, storage));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400241 this->addSymbol(id, result);
242 return result;
243 }
244 default:
245 printf("unsupported symbol %d\n", kind);
246 SkASSERT(false);
247 return nullptr;
248 }
249}
250
251const Type* Rehydrator::type() {
252 const Symbol* result = this->symbol();
Ethan Nicholase6592142020-09-08 10:22:09 -0400253 SkASSERT(result->kind() == Symbol::Kind::kType);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400254 return (const Type*) result;
255}
256
257std::vector<std::unique_ptr<ProgramElement>> Rehydrator::elements() {
258 SkDEBUGCODE(uint8_t command = )this->readU8();
259 SkASSERT(command == kElements_Command);
260 uint8_t count = this->readU8();
261 std::vector<std::unique_ptr<ProgramElement>> result;
262 result.reserve(count);
263 for (int i = 0; i < count; ++i) {
264 result.push_back(this->element());
265 }
266 return result;
267}
268
269std::unique_ptr<ProgramElement> Rehydrator::element() {
270 int kind = this->readU8();
271 switch (kind) {
272 case Rehydrator::kEnum_Command: {
273 StringFragment typeName = this->readString();
Brian Osman1313d1a2020-09-08 10:34:30 -0400274 std::shared_ptr<SymbolTable> symbols = this->symbolTable(/*inherit=*/false);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400275 for (auto& s : symbols->fOwnedSymbols) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400276 SkASSERT(s->kind() == Symbol::Kind::kVariable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400277 Variable& v = (Variable&) *s;
278 int value = this->readS32();
John Stiles3ae071e2020-08-05 15:29:29 -0400279 v.fInitialValue = symbols->takeOwnershipOfIRNode(
280 std::make_unique<IntLiteral>(fContext, /*offset=*/-1, value));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400281 v.fWriteCount = 1;
282 }
283 return std::unique_ptr<ProgramElement>(new Enum(-1, typeName, std::move(symbols)));
284 }
285 case Rehydrator::kFunctionDefinition_Command: {
286 const FunctionDeclaration* decl = this->symbolRef<FunctionDeclaration>(
Ethan Nicholase6592142020-09-08 10:22:09 -0400287 Symbol::Kind::kFunctionDeclaration);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400288 std::unique_ptr<Statement> body = this->statement();
John Stilesb8e010c2020-08-11 18:05:39 -0400289 std::unordered_set<const FunctionDeclaration*> refs;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400290 uint8_t refCount = this->readU8();
291 for (int i = 0; i < refCount; ++i) {
292 refs.insert(this->symbolRef<FunctionDeclaration>(
Ethan Nicholase6592142020-09-08 10:22:09 -0400293 Symbol::Kind::kFunctionDeclaration));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400294 }
295 FunctionDefinition* result = new FunctionDefinition(-1, *decl, std::move(body),
296 std::move(refs));
297 decl->fDefinition = result;
298 return std::unique_ptr<ProgramElement>(result);
299 }
300 case Rehydrator::kInterfaceBlock_Command: {
301 const Symbol* var = this->symbol();
Ethan Nicholase6592142020-09-08 10:22:09 -0400302 SkASSERT(var && var->kind() == Symbol::Kind::kVariable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400303 StringFragment typeName = this->readString();
304 StringFragment instanceName = this->readString();
305 uint8_t sizeCount = this->readU8();
306 std::vector<std::unique_ptr<Expression>> sizes;
307 sizes.reserve(sizeCount);
308 for (int i = 0; i < sizeCount; ++i) {
309 sizes.push_back(this->expression());
310 }
311 return std::unique_ptr<ProgramElement>(new InterfaceBlock(-1, (Variable*) var, typeName,
312 instanceName,
313 std::move(sizes), nullptr));
314 }
315 case Rehydrator::kVarDeclarations_Command: {
316 const Type* baseType = this->type();
317 int count = this->readU8();
Brian Osman347e5dc2020-10-05 15:56:44 -0400318 std::vector<std::unique_ptr<Statement>> vars;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400319 vars.reserve(count);
320 for (int i = 0 ; i < count; ++i) {
Brian Osman347e5dc2020-10-05 15:56:44 -0400321 vars.push_back(this->statement());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400322 }
323 return std::unique_ptr<ProgramElement>(new VarDeclarations(-1, baseType,
324 std::move(vars)));
325 }
326 default:
327 printf("unsupported element %d\n", kind);
328 SkASSERT(false);
329 return nullptr;
330 }
331}
332
333std::unique_ptr<Statement> Rehydrator::statement() {
334 int kind = this->readU8();
335 switch (kind) {
336 case Rehydrator::kBlock_Command: {
337 AutoRehydratorSymbolTable symbols(this);
338 int count = this->readU8();
339 std::vector<std::unique_ptr<Statement>> statements;
340 statements.reserve(count);
341 for (int i = 0; i < count; ++i) {
342 statements.push_back(this->statement());
343 }
344 bool isScope = this->readU8();
345 return std::unique_ptr<Statement>(new Block(-1, std::move(statements), fSymbolTable,
346 isScope));
347 }
348 case Rehydrator::kBreak_Command:
349 return std::unique_ptr<Statement>(new BreakStatement(-1));
350 case Rehydrator::kContinue_Command:
351 return std::unique_ptr<Statement>(new ContinueStatement(-1));
352 case Rehydrator::kDiscard_Command:
353 return std::unique_ptr<Statement>(new DiscardStatement(-1));
354 case Rehydrator::kDo_Command: {
355 std::unique_ptr<Statement> stmt = this->statement();
356 std::unique_ptr<Expression> expr = this->expression();
357 return std::unique_ptr<Statement>(new DoStatement(-1, std::move(stmt),
358 std::move(expr)));
359 }
360 case Rehydrator::kExpressionStatement_Command: {
361 std::unique_ptr<Expression> expr = this->expression();
362 return std::unique_ptr<Statement>(new ExpressionStatement(std::move(expr)));
363 }
364 case Rehydrator::kFor_Command: {
365 std::unique_ptr<Statement> initializer = this->statement();
366 std::unique_ptr<Expression> test = this->expression();
367 std::unique_ptr<Expression> next = this->expression();
368 std::unique_ptr<Statement> body = this->statement();
369 std::shared_ptr<SymbolTable> symbols = this->symbolTable();
370 return std::unique_ptr<Statement>(new ForStatement(-1, std::move(initializer),
371 std::move(test), std::move(next),
372 std::move(body),
373 std::move(symbols)));
374 }
375 case Rehydrator::kIf_Command: {
376 bool isStatic = this->readU8();
377 std::unique_ptr<Expression> test = this->expression();
378 std::unique_ptr<Statement> ifTrue = this->statement();
379 std::unique_ptr<Statement> ifFalse = this->statement();
380 return std::unique_ptr<Statement>(new IfStatement(-1, isStatic, std::move(test),
381 std::move(ifTrue),
382 std::move(ifFalse)));
383 }
John Stiles98c1f822020-09-09 14:18:53 -0400384 case Rehydrator::kInlineMarker_Command: {
385 const FunctionDeclaration* funcDecl = this->symbolRef<FunctionDeclaration>(
386 Symbol::Kind::kFunctionDeclaration);
387 return std::make_unique<InlineMarker>(*funcDecl);
388 }
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400389 case Rehydrator::kReturn_Command: {
390 std::unique_ptr<Expression> expr = this->expression();
391 if (expr) {
392 return std::unique_ptr<Statement>(new ReturnStatement(std::move(expr)));
393 } else {
394 return std::unique_ptr<Statement>(new ReturnStatement(-1));
395 }
396 }
397 case Rehydrator::kSwitch_Command: {
398 bool isStatic = this->readU8();
399 AutoRehydratorSymbolTable symbols(this);
400 std::unique_ptr<Expression> expr = this->expression();
401 int caseCount = this->readU8();
402 std::vector<std::unique_ptr<SwitchCase>> cases;
403 cases.reserve(caseCount);
404 for (int i = 0; i < caseCount; ++i) {
405 std::unique_ptr<Expression> value = this->expression();
406 int statementCount = this->readU8();
407 std::vector<std::unique_ptr<Statement>> statements;
408 statements.reserve(statementCount);
409 for (int j = 0; j < statementCount; ++j) {
410 statements.push_back(this->statement());
411 }
412 cases.emplace_back(new SwitchCase(-1, std::move(value), std::move(statements)));
413 }
414 return std::unique_ptr<Statement>(new SwitchStatement(-1, isStatic, std::move(expr),
415 std::move(cases),
416 fSymbolTable));
417 }
418 case Rehydrator::kVarDeclaration_Command: {
Ethan Nicholase6592142020-09-08 10:22:09 -0400419 Variable* var = this->symbolRef<Variable>(Symbol::Kind::kVariable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400420 uint8_t sizeCount = this->readU8();
421 std::vector<std::unique_ptr<Expression>> sizes;
422 sizes.reserve(sizeCount);
423 for (int i = 0; i < sizeCount; ++i) {
424 sizes.push_back(this->expression());
425 }
426 std::unique_ptr<Expression> value = this->expression();
427 if (value) {
428 var->fInitialValue = value.get();
429 SkASSERT(var->fWriteCount == 0);
430 ++var->fWriteCount;
431 }
432 return std::unique_ptr<Statement>(new VarDeclaration(var,
433 std::move(sizes),
434 std::move(value)));
435 }
436 case Rehydrator::kVarDeclarations_Command: {
437 const Type* baseType = this->type();
438 int count = this->readU8();
Brian Osman347e5dc2020-10-05 15:56:44 -0400439 std::vector<std::unique_ptr<Statement>> vars;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400440 vars.reserve(count);
441 for (int i = 0 ; i < count; ++i) {
Brian Osman347e5dc2020-10-05 15:56:44 -0400442 vars.push_back(this->statement());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400443 }
John Stilesfbd050b2020-08-03 13:21:46 -0400444 return std::make_unique<VarDeclarationsStatement>(
445 std::make_unique<VarDeclarations>(-1, baseType, std::move(vars)));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400446 }
447 case Rehydrator::kVoid_Command:
448 return nullptr;
449 case Rehydrator::kWhile_Command: {
450 std::unique_ptr<Expression> expr = this->expression();
451 std::unique_ptr<Statement> stmt = this->statement();
452 return std::unique_ptr<Statement>(new WhileStatement(-1, std::move(expr),
453 std::move(stmt)));
454 }
455 default:
456 printf("unsupported statement %d\n", kind);
457 SkASSERT(false);
458 return nullptr;
459 }
460}
461
462std::unique_ptr<Expression> Rehydrator::expression() {
463 int kind = this->readU8();
464 switch (kind) {
465 case Rehydrator::kBinary_Command: {
466 std::unique_ptr<Expression> left = this->expression();
467 Token::Kind op = (Token::Kind) this->readU8();
468 std::unique_ptr<Expression> right = this->expression();
469 const Type* type = this->type();
Ethan Nicholas30d30222020-09-11 12:27:26 -0400470 return std::make_unique<BinaryExpression>(-1, std::move(left), op, std::move(right),
471 type);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400472 }
473 case Rehydrator::kBoolLiteral_Command: {
474 bool value = this->readU8();
Ethan Nicholas30d30222020-09-11 12:27:26 -0400475 return std::make_unique<BoolLiteral>(fContext, -1, value);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400476 }
477 case Rehydrator::kConstructor_Command: {
478 const Type* type = this->type();
479 uint8_t argCount = this->readU8();
480 std::vector<std::unique_ptr<Expression>> args;
481 args.reserve(argCount);
482 for (int i = 0; i < argCount; ++i) {
483 args.push_back(this->expression());
484 }
Ethan Nicholas30d30222020-09-11 12:27:26 -0400485 return std::make_unique<Constructor>(-1, type, std::move(args));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400486 }
487 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();
Ethan Nicholas30d30222020-09-11 12:27:26 -0400491 return std::make_unique<FieldAccess>(std::move(base), index, ownerKind);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400492 }
493 case Rehydrator::kFloatLiteral_Command: {
494 FloatIntUnion u;
495 u.fInt = this->readS32();
Ethan Nicholas30d30222020-09-11 12:27:26 -0400496 return std::make_unique<FloatLiteral>(fContext, -1, u.fFloat);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400497 }
498 case Rehydrator::kFunctionCall_Command: {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400499 const Type* type = this->type();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400500 const FunctionDeclaration* f = this->symbolRef<FunctionDeclaration>(
Ethan Nicholase6592142020-09-08 10:22:09 -0400501 Symbol::Kind::kFunctionDeclaration);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400502 uint8_t argCount = this->readU8();
503 std::vector<std::unique_ptr<Expression>> args;
504 args.reserve(argCount);
505 for (int i = 0; i < argCount; ++i) {
506 args.push_back(this->expression());
507 }
Ethan Nicholas0dec9922020-10-05 15:51:52 -0400508 return std::make_unique<FunctionCall>(-1, type, f, std::move(args));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400509 }
510 case Rehydrator::kIndex_Command: {
511 std::unique_ptr<Expression> base = this->expression();
512 std::unique_ptr<Expression> index = this->expression();
Ethan Nicholas30d30222020-09-11 12:27:26 -0400513 return std::make_unique<IndexExpression>(fContext, std::move(base), std::move(index));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400514 }
515 case Rehydrator::kIntLiteral_Command: {
516 int value = this->readS32();
Ethan Nicholas30d30222020-09-11 12:27:26 -0400517 return std::make_unique<IntLiteral>(fContext, -1, value);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400518 }
519 case Rehydrator::kNullLiteral_Command:
Ethan Nicholas30d30222020-09-11 12:27:26 -0400520 return std::make_unique<NullLiteral>(fContext, -1);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400521 case Rehydrator::kPostfix_Command: {
522 Token::Kind op = (Token::Kind) this->readU8();
523 std::unique_ptr<Expression> operand = this->expression();
Ethan Nicholas30d30222020-09-11 12:27:26 -0400524 return std::make_unique<PostfixExpression>(std::move(operand), op);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400525 }
526 case Rehydrator::kPrefix_Command: {
527 Token::Kind op = (Token::Kind) this->readU8();
528 std::unique_ptr<Expression> operand = this->expression();
Ethan Nicholas30d30222020-09-11 12:27:26 -0400529 return std::make_unique<PrefixExpression>(op, std::move(operand));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400530 }
531 case Rehydrator::kSetting_Command: {
532 StringFragment name = this->readString();
533 std::unique_ptr<Expression> value = this->expression();
Ethan Nicholas30d30222020-09-11 12:27:26 -0400534 return std::make_unique<Setting>(-1, name, std::move(value));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400535 }
536 case Rehydrator::kSwizzle_Command: {
537 std::unique_ptr<Expression> base = this->expression();
538 int count = this->readU8();
539 std::vector<int> components;
540 components.reserve(count);
541 for (int i = 0; i < count; ++i) {
542 components.push_back(this->readU8());
543 }
Ethan Nicholas30d30222020-09-11 12:27:26 -0400544 return std::make_unique<Swizzle>(fContext, std::move(base), std::move(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();
Ethan Nicholasa02ce0c2020-09-21 12:37:02 -0400550 return std::make_unique<TernaryExpression>(-1, std::move(test), std::move(ifTrue),
551 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;
575 std::shared_ptr<SymbolTable> result = inherit ? std::make_shared<SymbolTable>(fSymbolTable)
576 : std::make_shared<SymbolTable>(fErrors);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400577 fSymbolTable = result;
578 std::vector<const Symbol*> ownedSymbols;
579 ownedSymbols.reserve(ownedCount);
580 for (int i = 0; i < ownedCount; ++i) {
581 ownedSymbols.push_back(this->symbol());
582 }
583 uint16_t symbolCount = this->readU16();
584 std::vector<std::pair<StringFragment, int>> symbols;
585 symbols.reserve(symbolCount);
586 for (int i = 0; i < symbolCount; ++i) {
587 StringFragment name = this->readString();
588 int index = this->readU16();
589 fSymbolTable->addWithoutOwnership(name, ownedSymbols[index]);
590 }
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