blob: 8ea9ae6e888a6df74213aa2535797fcb8ed0c0c6 [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"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040034#include "src/sksl/ir/SkSLPostfixExpression.h"
35#include "src/sksl/ir/SkSLPrefixExpression.h"
36#include "src/sksl/ir/SkSLProgramElement.h"
37#include "src/sksl/ir/SkSLReturnStatement.h"
38#include "src/sksl/ir/SkSLSetting.h"
39#include "src/sksl/ir/SkSLStatement.h"
John Stilesdc75a972020-11-25 16:24:55 -050040#include "src/sksl/ir/SkSLStructDefinition.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040041#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"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040051
52namespace SkSL {
53
54class AutoRehydratorSymbolTable {
55public:
56 AutoRehydratorSymbolTable(Rehydrator* rehydrator)
57 : fRehydrator(rehydrator)
58 , fOldSymbols(fRehydrator->fSymbolTable) {
59 fRehydrator->fSymbolTable = fRehydrator->symbolTable();
60 }
61
62 ~AutoRehydratorSymbolTable() {
63 fRehydrator->fSymbolTable = std::move(fOldSymbols);
64 }
65
66private:
67 Rehydrator* fRehydrator;
68 std::shared_ptr<SymbolTable> fOldSymbols;
69};
70
John Stiles7c3515b2020-10-16 18:38:39 -040071Rehydrator::Rehydrator(const Context* context, ModifiersPool* modifiers,
72 std::shared_ptr<SymbolTable> symbolTable, ErrorReporter* errorReporter,
73 const uint8_t* src, size_t length)
74 : fContext(*context)
75 , fModifiers(*modifiers)
76 , fErrors(errorReporter)
77 , fSymbolTable(std::move(symbolTable))
78 , fStart(src)
79 SkDEBUGCODE(, fEnd(fStart + length)) {
80 SkASSERT(fSymbolTable);
81 SkASSERT(fSymbolTable->isBuiltin());
82 // skip past string data
83 fIP = fStart;
84 fIP += this->readU16();
85}
86
Ethan Nicholasc18bb512020-07-28 14:46:53 -040087Layout Rehydrator::layout() {
88 switch (this->readU8()) {
89 case kBuiltinLayout_Command: {
90 Layout result;
91 result.fBuiltin = this->readS16();
92 return result;
93 }
94 case kDefaultLayout_Command:
95 return Layout();
96 case kLayout_Command: {
97 int flags = this->readU32();
98 int location = this->readS8();
99 int offset = this->readS8();
100 int binding = this->readS8();
101 int index = this->readS8();
102 int set = this->readS8();
103 int builtin = this->readS16();
104 int inputAttachmentIndex = this->readS8();
105 int format = this->readS8();
106 int primitive = this->readS8();
107 int maxVertices = this->readS8();
108 int invocations = this->readS8();
109 StringFragment marker = this->readString();
110 StringFragment when = this->readString();
111 int key = this->readS8();
112 int ctype = this->readS8();
113 return Layout(flags, location, offset, binding, index, set, builtin,
114 inputAttachmentIndex, (Layout::Format) format,
115 (Layout::Primitive) primitive, maxVertices, invocations, marker, when,
116 (Layout::Key) key, (Layout::CType) ctype);
117 }
118 default:
119 SkASSERT(false);
120 return Layout();
121 }
122}
123
124Modifiers Rehydrator::modifiers() {
125 switch (this->readU8()) {
126 case kDefaultModifiers_Command:
127 return Modifiers();
128 case kModifiers8Bit_Command: {
129 Layout l = this->layout();
130 int flags = this->readU8();
131 return Modifiers(l, flags);
132 }
133 case kModifiers_Command: {
134 Layout l = this->layout();
135 int flags = this->readS32();
136 return Modifiers(l, flags);
137 }
138 default:
139 SkASSERT(false);
140 return Modifiers();
141 }
142}
143
Brian Osman5bf3e202020-10-13 10:34:18 -0400144const Symbol* Rehydrator::symbol() {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400145 int kind = this->readU8();
146 switch (kind) {
147 case kArrayType_Command: {
148 uint16_t id = this->readU16();
149 const Type* componentType = this->type();
Brian Osmane8c26082020-10-01 17:22:45 -0400150 int8_t count = this->readS8();
151 String name = componentType->name();
152 if (count == Type::kUnsizedArray) {
153 name += "[]";
154 } else {
155 name += "[" + to_string(count) + "]";
156 }
Brian Osman5bf3e202020-10-13 10:34:18 -0400157 const Type* result = fSymbolTable->takeOwnershipOfSymbol(
John Stilesad2d4942020-12-11 16:55:58 -0500158 Type::MakeArrayType(name, *componentType, count));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400159 this->addSymbol(id, result);
160 return result;
161 }
162 case kEnumType_Command: {
163 uint16_t id = this->readU16();
164 StringFragment name = this->readString();
Brian Osman5bf3e202020-10-13 10:34:18 -0400165 const Type* result = fSymbolTable->takeOwnershipOfSymbol(
John Stilesad2d4942020-12-11 16:55:58 -0500166 Type::MakeSimpleType(name, Type::TypeKind::kEnum));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400167 this->addSymbol(id, result);
168 return result;
169 }
170 case kFunctionDeclaration_Command: {
171 uint16_t id = this->readU16();
172 Modifiers modifiers = this->modifiers();
173 StringFragment name = this->readString();
174 int parameterCount = this->readU8();
Brian Osman5bf3e202020-10-13 10:34:18 -0400175 std::vector<const Variable*> parameters;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400176 parameters.reserve(parameterCount);
177 for (int i = 0; i < parameterCount; ++i) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400178 parameters.push_back(this->symbolRef<Variable>(Symbol::Kind::kVariable));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400179 }
180 const Type* returnType = this->type();
Brian Osman5bf3e202020-10-13 10:34:18 -0400181 const FunctionDeclaration* result =
John Stiles3ae071e2020-08-05 15:29:29 -0400182 fSymbolTable->takeOwnershipOfSymbol(std::make_unique<FunctionDeclaration>(
John Stiles586df952020-11-12 18:27:13 -0500183 /*offset=*/-1, fModifiers.addToPool(modifiers), name,
Ethan Nicholased84b732020-10-08 11:45:44 -0400184 std::move(parameters), returnType, /*builtin=*/true));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400185 this->addSymbol(id, result);
186 return result;
187 }
188 case kField_Command: {
Ethan Nicholase6592142020-09-08 10:22:09 -0400189 const Variable* owner = this->symbolRef<Variable>(Symbol::Kind::kVariable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400190 uint8_t index = this->readU8();
Brian Osman5bf3e202020-10-13 10:34:18 -0400191 const Field* result = fSymbolTable->takeOwnershipOfSymbol(
Ethan Nicholase2c49992020-10-05 11:49:11 -0400192 std::make_unique<Field>(/*offset=*/-1, owner, index));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400193 return result;
194 }
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400195 case kStructType_Command: {
196 uint16_t id = this->readU16();
197 StringFragment name = this->readString();
198 uint8_t fieldCount = this->readU8();
199 std::vector<Type::Field> fields;
200 fields.reserve(fieldCount);
201 for (int i = 0; i < fieldCount; ++i) {
202 Modifiers m = this->modifiers();
John Stilesf621e232020-08-25 13:33:02 -0400203 StringFragment fieldName = this->readString();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400204 const Type* type = this->type();
John Stilesf621e232020-08-25 13:33:02 -0400205 fields.emplace_back(m, fieldName, type);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400206 }
Brian Osman5bf3e202020-10-13 10:34:18 -0400207 const Type* result = fSymbolTable->takeOwnershipOfSymbol(
John Stilesad2d4942020-12-11 16:55:58 -0500208 Type::MakeStructType(/*offset=*/-1, name, std::move(fields)));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400209 this->addSymbol(id, result);
210 return result;
211 }
212 case kSymbolRef_Command: {
213 uint16_t id = this->readU16();
214 SkASSERT(fSymbols.size() > id);
215 return fSymbols[id];
216 }
John Stiles49a547f2020-10-06 16:14:37 -0400217 case kSymbolAlias_Command: {
218 uint16_t id = this->readU16();
219 StringFragment name = this->readString();
Brian Osman5bf3e202020-10-13 10:34:18 -0400220 const Symbol* origSymbol = this->symbol();
221 const SymbolAlias* symbolAlias = fSymbolTable->takeOwnershipOfSymbol(
John Stiles49a547f2020-10-06 16:14:37 -0400222 std::make_unique<SymbolAlias>(/*offset=*/-1, name, origSymbol));
223 this->addSymbol(id, symbolAlias);
224 return symbolAlias;
225 }
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400226 case kSystemType_Command: {
227 uint16_t id = this->readU16();
228 StringFragment name = this->readString();
Brian Osman5bf3e202020-10-13 10:34:18 -0400229 const Symbol* result = (*fSymbolTable)[name];
Ethan Nicholase6592142020-09-08 10:22:09 -0400230 SkASSERT(result && result->kind() == Symbol::Kind::kType);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400231 this->addSymbol(id, result);
232 return result;
233 }
234 case kUnresolvedFunction_Command: {
235 uint16_t id = this->readU16();
236 int length = this->readU8();
237 std::vector<const FunctionDeclaration*> functions;
238 functions.reserve(length);
239 for (int i = 0; i < length; ++i) {
240 const Symbol* f = this->symbol();
Ethan Nicholase6592142020-09-08 10:22:09 -0400241 SkASSERT(f && f->kind() == Symbol::Kind::kFunctionDeclaration);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400242 functions.push_back((const FunctionDeclaration*) f);
243 }
Brian Osman5bf3e202020-10-13 10:34:18 -0400244 const UnresolvedFunction* result = fSymbolTable->takeOwnershipOfSymbol(
John Stiles3ae071e2020-08-05 15:29:29 -0400245 std::make_unique<UnresolvedFunction>(std::move(functions)));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400246 this->addSymbol(id, result);
247 return result;
248 }
249 case kVariable_Command: {
250 uint16_t id = this->readU16();
John Stiles586df952020-11-12 18:27:13 -0500251 const Modifiers* m = fModifiers.addToPool(this->modifiers());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400252 StringFragment name = this->readString();
253 const Type* type = this->type();
254 Variable::Storage storage = (Variable::Storage) this->readU8();
Brian Osman5bf3e202020-10-13 10:34:18 -0400255 const Variable* result = fSymbolTable->takeOwnershipOfSymbol(std::make_unique<Variable>(
Brian Osman3887a012020-09-30 13:22:27 -0400256 /*offset=*/-1, m, name, type, /*builtin=*/true, storage));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400257 this->addSymbol(id, result);
258 return result;
259 }
260 default:
261 printf("unsupported symbol %d\n", kind);
262 SkASSERT(false);
263 return nullptr;
264 }
265}
266
267const Type* Rehydrator::type() {
268 const Symbol* result = this->symbol();
Ethan Nicholase6592142020-09-08 10:22:09 -0400269 SkASSERT(result->kind() == Symbol::Kind::kType);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400270 return (const Type*) result;
271}
272
273std::vector<std::unique_ptr<ProgramElement>> Rehydrator::elements() {
274 SkDEBUGCODE(uint8_t command = )this->readU8();
275 SkASSERT(command == kElements_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400276 std::vector<std::unique_ptr<ProgramElement>> result;
John Stiles1ea7f542020-11-02 13:07:23 -0500277 while (std::unique_ptr<ProgramElement> elem = this->element()) {
278 result.push_back(std::move(elem));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400279 }
280 return result;
281}
282
283std::unique_ptr<ProgramElement> Rehydrator::element() {
284 int kind = this->readU8();
285 switch (kind) {
286 case Rehydrator::kEnum_Command: {
287 StringFragment typeName = this->readString();
Brian Osman1313d1a2020-09-08 10:34:30 -0400288 std::shared_ptr<SymbolTable> symbols = this->symbolTable(/*inherit=*/false);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400289 for (auto& s : symbols->fOwnedSymbols) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400290 SkASSERT(s->kind() == Symbol::Kind::kVariable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400291 Variable& v = (Variable&) *s;
292 int value = this->readS32();
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400293 v.setInitialValue(symbols->takeOwnershipOfIRNode(
294 std::make_unique<IntLiteral>(fContext, /*offset=*/-1, value)));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400295 }
John Stiles1c823672020-10-20 10:23:50 -0400296 return std::make_unique<Enum>(/*offset=*/-1, typeName, std::move(symbols),
297 /*isSharedWithCpp=*/true, /*isBuiltin=*/true);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400298 }
299 case Rehydrator::kFunctionDefinition_Command: {
300 const FunctionDeclaration* decl = this->symbolRef<FunctionDeclaration>(
Ethan Nicholase6592142020-09-08 10:22:09 -0400301 Symbol::Kind::kFunctionDeclaration);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400302 std::unique_ptr<Statement> body = this->statement();
John Stilesb8e010c2020-08-11 18:05:39 -0400303 std::unordered_set<const FunctionDeclaration*> refs;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400304 uint8_t refCount = this->readU8();
305 for (int i = 0; i < refCount; ++i) {
306 refs.insert(this->symbolRef<FunctionDeclaration>(
Ethan Nicholase6592142020-09-08 10:22:09 -0400307 Symbol::Kind::kFunctionDeclaration));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400308 }
John Stiles607d36b2020-10-19 15:00:01 -0400309 auto result = std::make_unique<FunctionDefinition>(/*offset=*/-1, decl,
310 /*builtin=*/true, std::move(body),
311 std::move(refs));
312 decl->setDefinition(result.get());
313 return std::move(result);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400314 }
315 case Rehydrator::kInterfaceBlock_Command: {
316 const Symbol* var = this->symbol();
John Stiles87ae34e2020-10-13 12:50:11 -0400317 SkASSERT(var && var->is<Variable>());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400318 StringFragment typeName = this->readString();
319 StringFragment instanceName = this->readString();
John Stilesd39aec02020-12-03 10:42:26 -0500320 int arraySize = this->readS8();
John Stiles87ae34e2020-10-13 12:50:11 -0400321 return std::make_unique<InterfaceBlock>(/*offset=*/-1, &var->as<Variable>(), typeName,
John Stilesd39aec02020-12-03 10:42:26 -0500322 instanceName, arraySize, nullptr);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400323 }
324 case Rehydrator::kVarDeclarations_Command: {
Brian Osmanc0213602020-10-06 14:43:32 -0400325 std::unique_ptr<Statement> decl = this->statement();
John Stiles1ea7f542020-11-02 13:07:23 -0500326 return std::make_unique<GlobalVarDeclaration>(/*offset=*/-1, std::move(decl));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400327 }
John Stilesdc75a972020-11-25 16:24:55 -0500328 case Rehydrator::kStructDefinition_Command: {
329 const Symbol* type = this->symbol();
330 SkASSERT(type && type->is<Type>());
331 return std::make_unique<StructDefinition>(/*offset=*/-1, type->as<Type>());
332 }
John Stiles1ea7f542020-11-02 13:07:23 -0500333 case Rehydrator::kElementsComplete_Command:
334 return nullptr;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400335 default:
John Stiles1ea7f542020-11-02 13:07:23 -0500336 SkDEBUGFAILF("unsupported element %d\n", kind);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400337 return nullptr;
338 }
339}
340
341std::unique_ptr<Statement> Rehydrator::statement() {
342 int kind = this->readU8();
343 switch (kind) {
344 case Rehydrator::kBlock_Command: {
345 AutoRehydratorSymbolTable symbols(this);
346 int count = this->readU8();
John Stiles8f2a0cf2020-10-13 12:48:21 -0400347 StatementArray statements;
John Stilesf4bda742020-10-14 16:57:41 -0400348 statements.reserve_back(count);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400349 for (int i = 0; i < count; ++i) {
350 statements.push_back(this->statement());
351 }
352 bool isScope = this->readU8();
John Stiles8f2a0cf2020-10-13 12:48:21 -0400353 return std::make_unique<Block>(/*offset=*/-1, std::move(statements), fSymbolTable,
354 isScope);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400355 }
356 case Rehydrator::kBreak_Command:
357 return std::unique_ptr<Statement>(new BreakStatement(-1));
358 case Rehydrator::kContinue_Command:
359 return std::unique_ptr<Statement>(new ContinueStatement(-1));
360 case Rehydrator::kDiscard_Command:
361 return std::unique_ptr<Statement>(new DiscardStatement(-1));
362 case Rehydrator::kDo_Command: {
363 std::unique_ptr<Statement> stmt = this->statement();
364 std::unique_ptr<Expression> expr = this->expression();
365 return std::unique_ptr<Statement>(new DoStatement(-1, std::move(stmt),
366 std::move(expr)));
367 }
368 case Rehydrator::kExpressionStatement_Command: {
369 std::unique_ptr<Expression> expr = this->expression();
370 return std::unique_ptr<Statement>(new ExpressionStatement(std::move(expr)));
371 }
372 case Rehydrator::kFor_Command: {
373 std::unique_ptr<Statement> initializer = this->statement();
374 std::unique_ptr<Expression> test = this->expression();
375 std::unique_ptr<Expression> next = this->expression();
376 std::unique_ptr<Statement> body = this->statement();
377 std::shared_ptr<SymbolTable> symbols = this->symbolTable();
378 return std::unique_ptr<Statement>(new ForStatement(-1, std::move(initializer),
379 std::move(test), std::move(next),
380 std::move(body),
381 std::move(symbols)));
382 }
383 case Rehydrator::kIf_Command: {
384 bool isStatic = this->readU8();
385 std::unique_ptr<Expression> test = this->expression();
386 std::unique_ptr<Statement> ifTrue = this->statement();
387 std::unique_ptr<Statement> ifFalse = this->statement();
388 return std::unique_ptr<Statement>(new IfStatement(-1, isStatic, std::move(test),
389 std::move(ifTrue),
390 std::move(ifFalse)));
391 }
John Stiles98c1f822020-09-09 14:18:53 -0400392 case Rehydrator::kInlineMarker_Command: {
393 const FunctionDeclaration* funcDecl = this->symbolRef<FunctionDeclaration>(
394 Symbol::Kind::kFunctionDeclaration);
Ethan Nicholasceb62142020-10-09 16:51:18 -0400395 return std::make_unique<InlineMarker>(funcDecl);
John Stiles98c1f822020-09-09 14:18:53 -0400396 }
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400397 case Rehydrator::kReturn_Command: {
398 std::unique_ptr<Expression> expr = this->expression();
399 if (expr) {
400 return std::unique_ptr<Statement>(new ReturnStatement(std::move(expr)));
401 } else {
402 return std::unique_ptr<Statement>(new ReturnStatement(-1));
403 }
404 }
405 case Rehydrator::kSwitch_Command: {
406 bool isStatic = this->readU8();
407 AutoRehydratorSymbolTable symbols(this);
408 std::unique_ptr<Expression> expr = this->expression();
409 int caseCount = this->readU8();
410 std::vector<std::unique_ptr<SwitchCase>> cases;
411 cases.reserve(caseCount);
412 for (int i = 0; i < caseCount; ++i) {
413 std::unique_ptr<Expression> value = this->expression();
414 int statementCount = this->readU8();
John Stiles8f2a0cf2020-10-13 12:48:21 -0400415 StatementArray statements;
John Stilesf4bda742020-10-14 16:57:41 -0400416 statements.reserve_back(statementCount);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400417 for (int j = 0; j < statementCount; ++j) {
418 statements.push_back(this->statement());
419 }
John Stiles8f2a0cf2020-10-13 12:48:21 -0400420 cases.push_back(std::make_unique<SwitchCase>(/*offset=*/-1, std::move(value),
421 std::move(statements)));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400422 }
John Stiles8f2a0cf2020-10-13 12:48:21 -0400423 return std::make_unique<SwitchStatement>(-1, isStatic, std::move(expr),
424 std::move(cases), fSymbolTable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400425 }
426 case Rehydrator::kVarDeclaration_Command: {
Ethan Nicholase6592142020-09-08 10:22:09 -0400427 Variable* var = this->symbolRef<Variable>(Symbol::Kind::kVariable);
Brian Osmanc0213602020-10-06 14:43:32 -0400428 const Type* baseType = this->type();
John Stiles62a56462020-12-03 10:41:58 -0500429 int arraySize = this->readS8();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400430 std::unique_ptr<Expression> value = this->expression();
431 if (value) {
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400432 var->setInitialValue(value.get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400433 }
John Stiles62a56462020-12-03 10:41:58 -0500434 return std::make_unique<VarDeclaration>(var, baseType, arraySize, std::move(value));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400435 }
436 case Rehydrator::kVoid_Command:
437 return nullptr;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400438 default:
439 printf("unsupported statement %d\n", kind);
440 SkASSERT(false);
441 return nullptr;
442 }
443}
444
445std::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();
452 const Type* type = this->type();
Ethan Nicholas30d30222020-09-11 12:27:26 -0400453 return std::make_unique<BinaryExpression>(-1, std::move(left), op, std::move(right),
454 type);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400455 }
456 case Rehydrator::kBoolLiteral_Command: {
457 bool value = this->readU8();
Ethan Nicholas30d30222020-09-11 12:27:26 -0400458 return std::make_unique<BoolLiteral>(fContext, -1, value);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400459 }
460 case Rehydrator::kConstructor_Command: {
461 const Type* type = this->type();
462 uint8_t argCount = this->readU8();
John Stiles8e3b6be2020-10-13 11:14:08 -0400463 ExpressionArray args;
John Stilesf4bda742020-10-14 16:57:41 -0400464 args.reserve_back(argCount);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400465 for (int i = 0; i < argCount; ++i) {
466 args.push_back(this->expression());
467 }
Ethan Nicholas30d30222020-09-11 12:27:26 -0400468 return std::make_unique<Constructor>(-1, type, std::move(args));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400469 }
470 case Rehydrator::kFieldAccess_Command: {
471 std::unique_ptr<Expression> base = this->expression();
472 int index = this->readU8();
473 FieldAccess::OwnerKind ownerKind = (FieldAccess::OwnerKind) this->readU8();
Ethan Nicholas30d30222020-09-11 12:27:26 -0400474 return std::make_unique<FieldAccess>(std::move(base), index, ownerKind);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400475 }
476 case Rehydrator::kFloatLiteral_Command: {
Brian Osmanfb964a42020-11-18 10:45:52 -0500477 const Type* type = this->type();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400478 FloatIntUnion u;
479 u.fInt = this->readS32();
Brian Osmanfb964a42020-11-18 10:45:52 -0500480 return std::make_unique<FloatLiteral>(-1, u.fFloat, type);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400481 }
482 case Rehydrator::kFunctionCall_Command: {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400483 const Type* type = this->type();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400484 const FunctionDeclaration* f = this->symbolRef<FunctionDeclaration>(
Ethan Nicholase6592142020-09-08 10:22:09 -0400485 Symbol::Kind::kFunctionDeclaration);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400486 uint8_t argCount = this->readU8();
John Stiles8e3b6be2020-10-13 11:14:08 -0400487 ExpressionArray args;
John Stilesf4bda742020-10-14 16:57:41 -0400488 args.reserve_back(argCount);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400489 for (int i = 0; i < argCount; ++i) {
490 args.push_back(this->expression());
491 }
Ethan Nicholas0dec9922020-10-05 15:51:52 -0400492 return std::make_unique<FunctionCall>(-1, type, f, std::move(args));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400493 }
494 case Rehydrator::kIndex_Command: {
495 std::unique_ptr<Expression> base = this->expression();
496 std::unique_ptr<Expression> index = this->expression();
Ethan Nicholas30d30222020-09-11 12:27:26 -0400497 return std::make_unique<IndexExpression>(fContext, std::move(base), std::move(index));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400498 }
499 case Rehydrator::kIntLiteral_Command: {
Brian Osmanfb964a42020-11-18 10:45:52 -0500500 const Type* type = this->type();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400501 int value = this->readS32();
Brian Osmanfb964a42020-11-18 10:45:52 -0500502 return std::make_unique<IntLiteral>(-1, value, type);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400503 }
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400504 case Rehydrator::kPostfix_Command: {
505 Token::Kind op = (Token::Kind) this->readU8();
506 std::unique_ptr<Expression> operand = this->expression();
Ethan Nicholas30d30222020-09-11 12:27:26 -0400507 return std::make_unique<PostfixExpression>(std::move(operand), op);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400508 }
509 case Rehydrator::kPrefix_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<PrefixExpression>(op, std::move(operand));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400513 }
514 case Rehydrator::kSetting_Command: {
515 StringFragment name = this->readString();
Ethan Nicholas01ec7e82020-10-08 12:10:12 -0400516 const Type* type = this->type();
517 return std::make_unique<Setting>(-1, name, type);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400518 }
519 case Rehydrator::kSwizzle_Command: {
520 std::unique_ptr<Expression> base = this->expression();
521 int count = this->readU8();
John Stiles750109b2020-10-30 13:45:46 -0400522 ComponentArray components;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400523 for (int i = 0; i < count; ++i) {
524 components.push_back(this->readU8());
525 }
John Stiles750109b2020-10-30 13:45:46 -0400526 return std::make_unique<Swizzle>(fContext, std::move(base), components);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400527 }
528 case Rehydrator::kTernary_Command: {
529 std::unique_ptr<Expression> test = this->expression();
530 std::unique_ptr<Expression> ifTrue = this->expression();
531 std::unique_ptr<Expression> ifFalse = this->expression();
Ethan Nicholasa02ce0c2020-09-21 12:37:02 -0400532 return std::make_unique<TernaryExpression>(-1, std::move(test), std::move(ifTrue),
533 std::move(ifFalse));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400534 }
535 case Rehydrator::kVariableReference_Command: {
Ethan Nicholase6592142020-09-08 10:22:09 -0400536 const Variable* var = this->symbolRef<Variable>(Symbol::Kind::kVariable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400537 VariableReference::RefKind refKind = (VariableReference::RefKind) this->readU8();
Brian Osman79457ef2020-09-24 15:01:27 -0400538 return std::make_unique<VariableReference>(-1, var, refKind);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400539 }
540 case Rehydrator::kVoid_Command:
541 return nullptr;
542 default:
543 printf("unsupported expression %d\n", kind);
544 SkASSERT(false);
545 return nullptr;
546 }
547}
548
Brian Osman1313d1a2020-09-08 10:34:30 -0400549std::shared_ptr<SymbolTable> Rehydrator::symbolTable(bool inherit) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400550 int command = this->readU8();
551 if (command == kVoid_Command) {
552 return nullptr;
553 }
554 SkASSERT(command == kSymbolTable_Command);
555 uint16_t ownedCount = this->readU16();
Brian Osman1313d1a2020-09-08 10:34:30 -0400556 std::shared_ptr<SymbolTable> oldTable = fSymbolTable;
John Stiles7c3515b2020-10-16 18:38:39 -0400557 std::shared_ptr<SymbolTable> result =
558 inherit ? std::make_shared<SymbolTable>(fSymbolTable, /*builtin=*/true)
559 : std::make_shared<SymbolTable>(fErrors, /*builtin=*/true);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400560 fSymbolTable = result;
Brian Osman5bf3e202020-10-13 10:34:18 -0400561 std::vector<const Symbol*> ownedSymbols;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400562 ownedSymbols.reserve(ownedCount);
563 for (int i = 0; i < ownedCount; ++i) {
564 ownedSymbols.push_back(this->symbol());
565 }
566 uint16_t symbolCount = this->readU16();
567 std::vector<std::pair<StringFragment, int>> symbols;
568 symbols.reserve(symbolCount);
569 for (int i = 0; i < symbolCount; ++i) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400570 int index = this->readU16();
John Stilesb8cc6652020-10-08 09:12:07 -0400571 fSymbolTable->addWithoutOwnership(ownedSymbols[index]);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400572 }
Brian Osman1313d1a2020-09-08 10:34:30 -0400573 fSymbolTable = oldTable;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400574 return result;
575}
576
John Stilesa6841be2020-08-06 14:11:56 -0400577} // namespace SkSL