blob: 8007c92afe0a355fc6e1eae2a3da8af600a879cc [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 Nicholasc18bb512020-07-28 14:46:53 -040014#include "src/sksl/ir/SkSLBinaryExpression.h"
15#include "src/sksl/ir/SkSLBreakStatement.h"
16#include "src/sksl/ir/SkSLContinueStatement.h"
17#include "src/sksl/ir/SkSLDiscardStatement.h"
18#include "src/sksl/ir/SkSLDoStatement.h"
19#include "src/sksl/ir/SkSLEnum.h"
20#include "src/sksl/ir/SkSLExpression.h"
21#include "src/sksl/ir/SkSLExpressionStatement.h"
22#include "src/sksl/ir/SkSLField.h"
23#include "src/sksl/ir/SkSLFieldAccess.h"
24#include "src/sksl/ir/SkSLFloatLiteral.h"
25#include "src/sksl/ir/SkSLForStatement.h"
26#include "src/sksl/ir/SkSLFunctionCall.h"
27#include "src/sksl/ir/SkSLFunctionDeclaration.h"
28#include "src/sksl/ir/SkSLFunctionDefinition.h"
29#include "src/sksl/ir/SkSLIfStatement.h"
30#include "src/sksl/ir/SkSLIndexExpression.h"
John Stiles98c1f822020-09-09 14:18:53 -040031#include "src/sksl/ir/SkSLInlineMarker.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040032#include "src/sksl/ir/SkSLIntLiteral.h"
33#include "src/sksl/ir/SkSLInterfaceBlock.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();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400105 int primitive = this->readS8();
106 int maxVertices = this->readS8();
107 int invocations = this->readS8();
108 StringFragment marker = this->readString();
109 StringFragment when = this->readString();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400110 int ctype = this->readS8();
111 return Layout(flags, location, offset, binding, index, set, builtin,
Brian Osman4717fbb2021-02-23 13:12:09 -0500112 inputAttachmentIndex, (Layout::Primitive)primitive, maxVertices,
Brian Osmanba7ef322021-02-23 13:37:22 -0500113 invocations, marker, when, (Layout::CType)ctype);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400114 }
115 default:
116 SkASSERT(false);
117 return Layout();
118 }
119}
120
121Modifiers Rehydrator::modifiers() {
122 switch (this->readU8()) {
123 case kDefaultModifiers_Command:
124 return Modifiers();
125 case kModifiers8Bit_Command: {
126 Layout l = this->layout();
127 int flags = this->readU8();
128 return Modifiers(l, flags);
129 }
130 case kModifiers_Command: {
131 Layout l = this->layout();
132 int flags = this->readS32();
133 return Modifiers(l, flags);
134 }
135 default:
136 SkASSERT(false);
137 return Modifiers();
138 }
139}
140
Brian Osman5bf3e202020-10-13 10:34:18 -0400141const Symbol* Rehydrator::symbol() {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400142 int kind = this->readU8();
143 switch (kind) {
144 case kArrayType_Command: {
145 uint16_t id = this->readU16();
146 const Type* componentType = this->type();
Brian Osmane8c26082020-10-01 17:22:45 -0400147 int8_t count = this->readS8();
148 String name = componentType->name();
149 if (count == Type::kUnsizedArray) {
150 name += "[]";
151 } else {
152 name += "[" + to_string(count) + "]";
153 }
Brian Osman5bf3e202020-10-13 10:34:18 -0400154 const Type* result = fSymbolTable->takeOwnershipOfSymbol(
John Stilesad2d4942020-12-11 16:55:58 -0500155 Type::MakeArrayType(name, *componentType, count));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400156 this->addSymbol(id, result);
157 return result;
158 }
159 case kEnumType_Command: {
160 uint16_t id = this->readU16();
161 StringFragment name = this->readString();
John Stilese6c67c52021-01-15 12:01:00 -0500162 const Type* result = fSymbolTable->takeOwnershipOfSymbol(Type::MakeEnumType(name));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400163 this->addSymbol(id, result);
164 return result;
165 }
166 case kFunctionDeclaration_Command: {
167 uint16_t id = this->readU16();
168 Modifiers modifiers = this->modifiers();
169 StringFragment name = this->readString();
170 int parameterCount = this->readU8();
Brian Osman5bf3e202020-10-13 10:34:18 -0400171 std::vector<const Variable*> parameters;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400172 parameters.reserve(parameterCount);
173 for (int i = 0; i < parameterCount; ++i) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400174 parameters.push_back(this->symbolRef<Variable>(Symbol::Kind::kVariable));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400175 }
176 const Type* returnType = this->type();
Brian Osman5bf3e202020-10-13 10:34:18 -0400177 const FunctionDeclaration* result =
John Stiles3ae071e2020-08-05 15:29:29 -0400178 fSymbolTable->takeOwnershipOfSymbol(std::make_unique<FunctionDeclaration>(
John Stiles586df952020-11-12 18:27:13 -0500179 /*offset=*/-1, fModifiers.addToPool(modifiers), name,
Ethan Nicholased84b732020-10-08 11:45:44 -0400180 std::move(parameters), returnType, /*builtin=*/true));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400181 this->addSymbol(id, result);
182 return result;
183 }
184 case kField_Command: {
Ethan Nicholase6592142020-09-08 10:22:09 -0400185 const Variable* owner = this->symbolRef<Variable>(Symbol::Kind::kVariable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400186 uint8_t index = this->readU8();
Brian Osman5bf3e202020-10-13 10:34:18 -0400187 const Field* result = fSymbolTable->takeOwnershipOfSymbol(
Ethan Nicholase2c49992020-10-05 11:49:11 -0400188 std::make_unique<Field>(/*offset=*/-1, owner, index));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400189 return result;
190 }
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400191 case kStructType_Command: {
192 uint16_t id = this->readU16();
193 StringFragment name = this->readString();
194 uint8_t fieldCount = this->readU8();
195 std::vector<Type::Field> fields;
196 fields.reserve(fieldCount);
197 for (int i = 0; i < fieldCount; ++i) {
198 Modifiers m = this->modifiers();
John Stilesf621e232020-08-25 13:33:02 -0400199 StringFragment fieldName = this->readString();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400200 const Type* type = this->type();
John Stilesf621e232020-08-25 13:33:02 -0400201 fields.emplace_back(m, fieldName, type);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400202 }
Brian Osman5bf3e202020-10-13 10:34:18 -0400203 const Type* result = fSymbolTable->takeOwnershipOfSymbol(
John Stilesad2d4942020-12-11 16:55:58 -0500204 Type::MakeStructType(/*offset=*/-1, name, std::move(fields)));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400205 this->addSymbol(id, result);
206 return result;
207 }
208 case kSymbolRef_Command: {
209 uint16_t id = this->readU16();
210 SkASSERT(fSymbols.size() > id);
211 return fSymbols[id];
212 }
John Stiles49a547f2020-10-06 16:14:37 -0400213 case kSymbolAlias_Command: {
214 uint16_t id = this->readU16();
215 StringFragment name = this->readString();
Brian Osman5bf3e202020-10-13 10:34:18 -0400216 const Symbol* origSymbol = this->symbol();
217 const SymbolAlias* symbolAlias = fSymbolTable->takeOwnershipOfSymbol(
John Stiles49a547f2020-10-06 16:14:37 -0400218 std::make_unique<SymbolAlias>(/*offset=*/-1, name, origSymbol));
219 this->addSymbol(id, symbolAlias);
220 return symbolAlias;
221 }
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400222 case kSystemType_Command: {
223 uint16_t id = this->readU16();
224 StringFragment name = this->readString();
Brian Osman5bf3e202020-10-13 10:34:18 -0400225 const Symbol* result = (*fSymbolTable)[name];
Ethan Nicholase6592142020-09-08 10:22:09 -0400226 SkASSERT(result && result->kind() == Symbol::Kind::kType);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400227 this->addSymbol(id, result);
228 return result;
229 }
230 case kUnresolvedFunction_Command: {
231 uint16_t id = this->readU16();
232 int length = this->readU8();
233 std::vector<const FunctionDeclaration*> functions;
234 functions.reserve(length);
235 for (int i = 0; i < length; ++i) {
236 const Symbol* f = this->symbol();
Ethan Nicholase6592142020-09-08 10:22:09 -0400237 SkASSERT(f && f->kind() == Symbol::Kind::kFunctionDeclaration);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400238 functions.push_back((const FunctionDeclaration*) f);
239 }
Brian Osman5bf3e202020-10-13 10:34:18 -0400240 const UnresolvedFunction* result = fSymbolTable->takeOwnershipOfSymbol(
John Stiles3ae071e2020-08-05 15:29:29 -0400241 std::make_unique<UnresolvedFunction>(std::move(functions)));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400242 this->addSymbol(id, result);
243 return result;
244 }
245 case kVariable_Command: {
246 uint16_t id = this->readU16();
John Stiles586df952020-11-12 18:27:13 -0500247 const Modifiers* m = fModifiers.addToPool(this->modifiers());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400248 StringFragment name = this->readString();
249 const Type* type = this->type();
250 Variable::Storage storage = (Variable::Storage) this->readU8();
Brian Osman5bf3e202020-10-13 10:34:18 -0400251 const Variable* result = fSymbolTable->takeOwnershipOfSymbol(std::make_unique<Variable>(
Brian Osman3887a012020-09-30 13:22:27 -0400252 /*offset=*/-1, m, name, type, /*builtin=*/true, storage));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400253 this->addSymbol(id, result);
254 return result;
255 }
256 default:
257 printf("unsupported symbol %d\n", kind);
258 SkASSERT(false);
259 return nullptr;
260 }
261}
262
263const Type* Rehydrator::type() {
264 const Symbol* result = this->symbol();
Ethan Nicholase6592142020-09-08 10:22:09 -0400265 SkASSERT(result->kind() == Symbol::Kind::kType);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400266 return (const Type*) result;
267}
268
269std::vector<std::unique_ptr<ProgramElement>> Rehydrator::elements() {
270 SkDEBUGCODE(uint8_t command = )this->readU8();
271 SkASSERT(command == kElements_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400272 std::vector<std::unique_ptr<ProgramElement>> result;
John Stiles1ea7f542020-11-02 13:07:23 -0500273 while (std::unique_ptr<ProgramElement> elem = this->element()) {
274 result.push_back(std::move(elem));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400275 }
276 return result;
277}
278
279std::unique_ptr<ProgramElement> Rehydrator::element() {
280 int kind = this->readU8();
281 switch (kind) {
282 case Rehydrator::kEnum_Command: {
283 StringFragment typeName = this->readString();
Brian Osman1313d1a2020-09-08 10:34:30 -0400284 std::shared_ptr<SymbolTable> symbols = this->symbolTable(/*inherit=*/false);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400285 for (auto& s : symbols->fOwnedSymbols) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400286 SkASSERT(s->kind() == Symbol::Kind::kVariable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400287 Variable& v = (Variable&) *s;
288 int value = this->readS32();
Ethan Nicholas5b9b0db2021-01-21 13:12:01 -0500289 // enum variables aren't really 'declared', but we have to create a declaration to
290 // store the value
291 auto valueLiteral = std::make_unique<IntLiteral>(fContext, /*offset=*/-1, value);
292 auto declaration = std::make_unique<VarDeclaration>(&v, &v.type(), /*arraySize=*/0,
293 std::move(valueLiteral));
294 v.setDeclaration(declaration.get());
295 symbols->takeOwnershipOfIRNode(std::move(declaration));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400296 }
John Stiles1c823672020-10-20 10:23:50 -0400297 return std::make_unique<Enum>(/*offset=*/-1, typeName, std::move(symbols),
298 /*isSharedWithCpp=*/true, /*isBuiltin=*/true);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400299 }
300 case Rehydrator::kFunctionDefinition_Command: {
301 const FunctionDeclaration* decl = this->symbolRef<FunctionDeclaration>(
Ethan Nicholase6592142020-09-08 10:22:09 -0400302 Symbol::Kind::kFunctionDeclaration);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400303 std::unique_ptr<Statement> body = this->statement();
John Stilesb8e010c2020-08-11 18:05:39 -0400304 std::unordered_set<const FunctionDeclaration*> refs;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400305 uint8_t refCount = this->readU8();
306 for (int i = 0; i < refCount; ++i) {
307 refs.insert(this->symbolRef<FunctionDeclaration>(
Ethan Nicholase6592142020-09-08 10:22:09 -0400308 Symbol::Kind::kFunctionDeclaration));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400309 }
John Stiles607d36b2020-10-19 15:00:01 -0400310 auto result = std::make_unique<FunctionDefinition>(/*offset=*/-1, decl,
311 /*builtin=*/true, std::move(body),
312 std::move(refs));
313 decl->setDefinition(result.get());
314 return std::move(result);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400315 }
316 case Rehydrator::kInterfaceBlock_Command: {
317 const Symbol* var = this->symbol();
John Stiles87ae34e2020-10-13 12:50:11 -0400318 SkASSERT(var && var->is<Variable>());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400319 StringFragment typeName = this->readString();
320 StringFragment instanceName = this->readString();
John Stilesd39aec02020-12-03 10:42:26 -0500321 int arraySize = this->readS8();
John Stiles87ae34e2020-10-13 12:50:11 -0400322 return std::make_unique<InterfaceBlock>(/*offset=*/-1, &var->as<Variable>(), typeName,
John Stilesd39aec02020-12-03 10:42:26 -0500323 instanceName, arraySize, nullptr);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400324 }
325 case Rehydrator::kVarDeclarations_Command: {
Brian Osmanc0213602020-10-06 14:43:32 -0400326 std::unique_ptr<Statement> decl = this->statement();
John Stiles1ea7f542020-11-02 13:07:23 -0500327 return std::make_unique<GlobalVarDeclaration>(/*offset=*/-1, std::move(decl));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400328 }
John Stilesdc75a972020-11-25 16:24:55 -0500329 case Rehydrator::kStructDefinition_Command: {
330 const Symbol* type = this->symbol();
331 SkASSERT(type && type->is<Type>());
332 return std::make_unique<StructDefinition>(/*offset=*/-1, type->as<Type>());
333 }
John Stiles1ea7f542020-11-02 13:07:23 -0500334 case Rehydrator::kElementsComplete_Command:
335 return nullptr;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400336 default:
John Stiles1ea7f542020-11-02 13:07:23 -0500337 SkDEBUGFAILF("unsupported element %d\n", kind);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400338 return nullptr;
339 }
340}
341
342std::unique_ptr<Statement> Rehydrator::statement() {
343 int kind = this->readU8();
344 switch (kind) {
345 case Rehydrator::kBlock_Command: {
346 AutoRehydratorSymbolTable symbols(this);
347 int count = this->readU8();
John Stiles8f2a0cf2020-10-13 12:48:21 -0400348 StatementArray statements;
John Stilesf4bda742020-10-14 16:57:41 -0400349 statements.reserve_back(count);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400350 for (int i = 0; i < count; ++i) {
351 statements.push_back(this->statement());
352 }
353 bool isScope = this->readU8();
John Stiles8f2a0cf2020-10-13 12:48:21 -0400354 return std::make_unique<Block>(/*offset=*/-1, std::move(statements), fSymbolTable,
355 isScope);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400356 }
357 case Rehydrator::kBreak_Command:
John Stiles23521a82021-03-02 17:02:51 -0500358 return std::make_unique<BreakStatement>(/*offset=*/-1);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400359 case Rehydrator::kContinue_Command:
John Stiles23521a82021-03-02 17:02:51 -0500360 return std::make_unique<ContinueStatement>(/*offset=*/-1);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400361 case Rehydrator::kDiscard_Command:
John Stiles23521a82021-03-02 17:02:51 -0500362 return std::make_unique<DiscardStatement>(/*offset=*/-1);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400363 case Rehydrator::kDo_Command: {
364 std::unique_ptr<Statement> stmt = this->statement();
365 std::unique_ptr<Expression> expr = this->expression();
John Stilesea5822e2021-02-26 11:18:20 -0500366 return DoStatement::Make(fContext, std::move(stmt), std::move(expr));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400367 }
368 case Rehydrator::kExpressionStatement_Command: {
369 std::unique_ptr<Expression> expr = this->expression();
John Stiles3e5871c2021-02-25 20:52:03 -0500370 return ExpressionStatement::Make(fContext, std::move(expr));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400371 }
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();
John Stiles23521a82021-03-02 17:02:51 -0500378 return ForStatement::Make(fContext, /*offset=*/-1, std::move(initializer),
379 std::move(test), std::move(next), std::move(body),
380 std::move(symbols));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400381 }
382 case Rehydrator::kIf_Command: {
383 bool isStatic = this->readU8();
384 std::unique_ptr<Expression> test = this->expression();
385 std::unique_ptr<Statement> ifTrue = this->statement();
386 std::unique_ptr<Statement> ifFalse = this->statement();
John Stilescf3059e2021-02-25 14:27:02 -0500387 return IfStatement::Make(fContext, /*offset=*/-1, isStatic, std::move(test),
388 std::move(ifTrue), std::move(ifFalse));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400389 }
John Stiles98c1f822020-09-09 14:18:53 -0400390 case Rehydrator::kInlineMarker_Command: {
391 const FunctionDeclaration* funcDecl = this->symbolRef<FunctionDeclaration>(
392 Symbol::Kind::kFunctionDeclaration);
Ethan Nicholasceb62142020-10-09 16:51:18 -0400393 return std::make_unique<InlineMarker>(funcDecl);
John Stiles98c1f822020-09-09 14:18:53 -0400394 }
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400395 case Rehydrator::kReturn_Command: {
396 std::unique_ptr<Expression> expr = this->expression();
397 if (expr) {
John Stiles23521a82021-03-02 17:02:51 -0500398 return std::make_unique<ReturnStatement>(std::move(expr));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400399 } else {
John Stiles23521a82021-03-02 17:02:51 -0500400 return std::make_unique<ReturnStatement>(/*offset=*/-1);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400401 }
402 }
403 case Rehydrator::kSwitch_Command: {
404 bool isStatic = this->readU8();
405 AutoRehydratorSymbolTable symbols(this);
406 std::unique_ptr<Expression> expr = this->expression();
407 int caseCount = this->readU8();
408 std::vector<std::unique_ptr<SwitchCase>> cases;
409 cases.reserve(caseCount);
410 for (int i = 0; i < caseCount; ++i) {
411 std::unique_ptr<Expression> value = this->expression();
412 int statementCount = this->readU8();
John Stiles8f2a0cf2020-10-13 12:48:21 -0400413 StatementArray statements;
John Stilesf4bda742020-10-14 16:57:41 -0400414 statements.reserve_back(statementCount);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400415 for (int j = 0; j < statementCount; ++j) {
416 statements.push_back(this->statement());
417 }
John Stiles8f2a0cf2020-10-13 12:48:21 -0400418 cases.push_back(std::make_unique<SwitchCase>(/*offset=*/-1, std::move(value),
419 std::move(statements)));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400420 }
John Stiles23521a82021-03-02 17:02:51 -0500421 return SwitchStatement::Make(fContext, /*offset=*/-1, isStatic, std::move(expr),
422 std::move(cases), fSymbolTable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400423 }
424 case Rehydrator::kVarDeclaration_Command: {
Ethan Nicholase6592142020-09-08 10:22:09 -0400425 Variable* var = this->symbolRef<Variable>(Symbol::Kind::kVariable);
Brian Osmanc0213602020-10-06 14:43:32 -0400426 const Type* baseType = this->type();
John Stiles62a56462020-12-03 10:41:58 -0500427 int arraySize = this->readS8();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400428 std::unique_ptr<Expression> value = this->expression();
Ethan Nicholas5b9b0db2021-01-21 13:12:01 -0500429 auto result = std::make_unique<VarDeclaration>(var, baseType, arraySize,
430 std::move(value));
431 var->setDeclaration(result.get());
432 return std::move(result);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400433 }
434 case Rehydrator::kVoid_Command:
435 return nullptr;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400436 default:
437 printf("unsupported statement %d\n", kind);
438 SkASSERT(false);
439 return nullptr;
440 }
441}
442
443std::unique_ptr<Expression> Rehydrator::expression() {
444 int kind = this->readU8();
445 switch (kind) {
446 case Rehydrator::kBinary_Command: {
447 std::unique_ptr<Expression> left = this->expression();
448 Token::Kind op = (Token::Kind) this->readU8();
449 std::unique_ptr<Expression> right = this->expression();
John Stilese2aec432021-03-01 09:27:48 -0500450 return BinaryExpression::Make(fContext, std::move(left), op, std::move(right));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400451 }
452 case Rehydrator::kBoolLiteral_Command: {
453 bool value = this->readU8();
Ethan Nicholas30d30222020-09-11 12:27:26 -0400454 return std::make_unique<BoolLiteral>(fContext, -1, value);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400455 }
456 case Rehydrator::kConstructor_Command: {
457 const Type* type = this->type();
458 uint8_t argCount = this->readU8();
John Stiles8e3b6be2020-10-13 11:14:08 -0400459 ExpressionArray args;
John Stilesf4bda742020-10-14 16:57:41 -0400460 args.reserve_back(argCount);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400461 for (int i = 0; i < argCount; ++i) {
462 args.push_back(this->expression());
463 }
John Stiles23521a82021-03-02 17:02:51 -0500464 auto ctor = Constructor::Convert(fContext, /*offset=*/-1, *type, std::move(args));
465 SkASSERT(ctor);
466 return ctor;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400467 }
468 case Rehydrator::kFieldAccess_Command: {
469 std::unique_ptr<Expression> base = this->expression();
470 int index = this->readU8();
471 FieldAccess::OwnerKind ownerKind = (FieldAccess::OwnerKind) this->readU8();
John Stiles06d600f2021-03-08 09:18:21 -0500472 return FieldAccess::Make(fContext, std::move(base), index, ownerKind);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400473 }
474 case Rehydrator::kFloatLiteral_Command: {
Brian Osmanfb964a42020-11-18 10:45:52 -0500475 const Type* type = this->type();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400476 FloatIntUnion u;
477 u.fInt = this->readS32();
Brian Osmanfb964a42020-11-18 10:45:52 -0500478 return std::make_unique<FloatLiteral>(-1, u.fFloat, type);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400479 }
480 case Rehydrator::kFunctionCall_Command: {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400481 const Type* type = this->type();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400482 const FunctionDeclaration* f = this->symbolRef<FunctionDeclaration>(
Ethan Nicholase6592142020-09-08 10:22:09 -0400483 Symbol::Kind::kFunctionDeclaration);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400484 uint8_t argCount = this->readU8();
John Stiles8e3b6be2020-10-13 11:14:08 -0400485 ExpressionArray args;
John Stilesf4bda742020-10-14 16:57:41 -0400486 args.reserve_back(argCount);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400487 for (int i = 0; i < argCount; ++i) {
488 args.push_back(this->expression());
489 }
John Stiles23521a82021-03-02 17:02:51 -0500490 return std::make_unique<FunctionCall>(/*offset=*/-1, type, f, std::move(args));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400491 }
492 case Rehydrator::kIndex_Command: {
493 std::unique_ptr<Expression> base = this->expression();
494 std::unique_ptr<Expression> index = this->expression();
John Stiles51d33982021-03-08 09:18:07 -0500495 return IndexExpression::Make(fContext, std::move(base), std::move(index));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400496 }
497 case Rehydrator::kIntLiteral_Command: {
Brian Osmanfb964a42020-11-18 10:45:52 -0500498 const Type* type = this->type();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400499 int value = this->readS32();
Brian Osmanfb964a42020-11-18 10:45:52 -0500500 return std::make_unique<IntLiteral>(-1, value, type);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400501 }
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400502 case Rehydrator::kPostfix_Command: {
503 Token::Kind op = (Token::Kind) this->readU8();
504 std::unique_ptr<Expression> operand = this->expression();
John Stiles52d3b012021-02-26 15:56:48 -0500505 return PostfixExpression::Make(fContext, std::move(operand), op);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400506 }
507 case Rehydrator::kPrefix_Command: {
508 Token::Kind op = (Token::Kind) this->readU8();
509 std::unique_ptr<Expression> operand = this->expression();
John Stilesb0eb20f2021-02-26 15:29:33 -0500510 return PrefixExpression::Make(fContext, op, std::move(operand));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400511 }
512 case Rehydrator::kSetting_Command: {
513 StringFragment name = this->readString();
John Stiles23521a82021-03-02 17:02:51 -0500514 return Setting::Convert(fContext, /*offset=*/-1, name);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400515 }
516 case Rehydrator::kSwizzle_Command: {
517 std::unique_ptr<Expression> base = this->expression();
518 int count = this->readU8();
John Stiles750109b2020-10-30 13:45:46 -0400519 ComponentArray components;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400520 for (int i = 0; i < count; ++i) {
521 components.push_back(this->readU8());
522 }
John Stiles23521a82021-03-02 17:02:51 -0500523 return Swizzle::Make(fContext, std::move(base), components);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400524 }
525 case Rehydrator::kTernary_Command: {
526 std::unique_ptr<Expression> test = this->expression();
527 std::unique_ptr<Expression> ifTrue = this->expression();
528 std::unique_ptr<Expression> ifFalse = this->expression();
John Stiles90518f72021-02-26 20:44:54 -0500529 return TernaryExpression::Make(fContext, std::move(test),
530 std::move(ifTrue), std::move(ifFalse));
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400531 }
532 case Rehydrator::kVariableReference_Command: {
Ethan Nicholase6592142020-09-08 10:22:09 -0400533 const Variable* var = this->symbolRef<Variable>(Symbol::Kind::kVariable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400534 VariableReference::RefKind refKind = (VariableReference::RefKind) this->readU8();
Brian Osman79457ef2020-09-24 15:01:27 -0400535 return std::make_unique<VariableReference>(-1, var, refKind);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400536 }
537 case Rehydrator::kVoid_Command:
538 return nullptr;
539 default:
540 printf("unsupported expression %d\n", kind);
541 SkASSERT(false);
542 return nullptr;
543 }
544}
545
Brian Osman1313d1a2020-09-08 10:34:30 -0400546std::shared_ptr<SymbolTable> Rehydrator::symbolTable(bool inherit) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400547 int command = this->readU8();
548 if (command == kVoid_Command) {
549 return nullptr;
550 }
551 SkASSERT(command == kSymbolTable_Command);
552 uint16_t ownedCount = this->readU16();
Brian Osman1313d1a2020-09-08 10:34:30 -0400553 std::shared_ptr<SymbolTable> oldTable = fSymbolTable;
John Stiles7c3515b2020-10-16 18:38:39 -0400554 std::shared_ptr<SymbolTable> result =
555 inherit ? std::make_shared<SymbolTable>(fSymbolTable, /*builtin=*/true)
556 : std::make_shared<SymbolTable>(fErrors, /*builtin=*/true);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400557 fSymbolTable = result;
Brian Osman5bf3e202020-10-13 10:34:18 -0400558 std::vector<const Symbol*> ownedSymbols;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400559 ownedSymbols.reserve(ownedCount);
560 for (int i = 0; i < ownedCount; ++i) {
561 ownedSymbols.push_back(this->symbol());
562 }
563 uint16_t symbolCount = this->readU16();
564 std::vector<std::pair<StringFragment, int>> symbols;
565 symbols.reserve(symbolCount);
566 for (int i = 0; i < symbolCount; ++i) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400567 int index = this->readU16();
John Stilesb8cc6652020-10-08 09:12:07 -0400568 fSymbolTable->addWithoutOwnership(ownedSymbols[index]);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400569 }
Brian Osman1313d1a2020-09-08 10:34:30 -0400570 fSymbolTable = oldTable;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400571 return result;
572}
573
John Stilesa6841be2020-08-06 14:11:56 -0400574} // namespace SkSL