Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 1 | /* |
| 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 Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 9 | |
| 10 | #include <memory> |
John Stiles | b8e010c | 2020-08-11 18:05:39 -0400 | [diff] [blame] | 11 | #include <unordered_set> |
| 12 | |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 13 | #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 Stiles | 98c1f82 | 2020-09-09 14:18:53 -0400 | [diff] [blame] | 30 | #include "src/sksl/ir/SkSLInlineMarker.h" |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 31 | #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 Stiles | 49a547f | 2020-10-06 16:14:37 -0400 | [diff] [blame] | 44 | #include "src/sksl/ir/SkSLSymbolAlias.h" |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 45 | #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 Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 50 | #include "src/sksl/ir/SkSLVariable.h" |
| 51 | #include "src/sksl/ir/SkSLWhileStatement.h" |
| 52 | |
| 53 | namespace SkSL { |
| 54 | |
| 55 | class AutoRehydratorSymbolTable { |
| 56 | public: |
| 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 | |
| 67 | private: |
| 68 | Rehydrator* fRehydrator; |
| 69 | std::shared_ptr<SymbolTable> fOldSymbols; |
| 70 | }; |
| 71 | |
| 72 | Layout 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 | |
| 109 | Modifiers 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 | |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 129 | Symbol* Rehydrator::symbol() { |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 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 Osman | e8c2608 | 2020-10-01 17:22:45 -0400 | [diff] [blame] | 135 | 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 | } |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 142 | Type* result = fSymbolTable->takeOwnershipOfSymbol( |
Brian Osman | e8c2608 | 2020-10-01 17:22:45 -0400 | [diff] [blame] | 143 | std::make_unique<Type>(name, Type::TypeKind::kArray, *componentType, count)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 144 | this->addSymbol(id, result); |
| 145 | return result; |
| 146 | } |
| 147 | case kEnumType_Command: { |
| 148 | uint16_t id = this->readU16(); |
| 149 | StringFragment name = this->readString(); |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 150 | Type* result = fSymbolTable->takeOwnershipOfSymbol( |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 151 | std::make_unique<Type>(name, Type::TypeKind::kEnum)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 152 | 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(); |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 160 | std::vector<Variable*> parameters; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 161 | parameters.reserve(parameterCount); |
| 162 | for (int i = 0; i < parameterCount; ++i) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 163 | parameters.push_back(this->symbolRef<Variable>(Symbol::Kind::kVariable)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 164 | } |
| 165 | const Type* returnType = this->type(); |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 166 | FunctionDeclaration* result = |
John Stiles | 3ae071e | 2020-08-05 15:29:29 -0400 | [diff] [blame] | 167 | fSymbolTable->takeOwnershipOfSymbol(std::make_unique<FunctionDeclaration>( |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame^] | 168 | /*offset=*/-1, fModifiers.handle(modifiers), name, |
| 169 | std::move(parameters), returnType, /*builtin=*/true)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 170 | this->addSymbol(id, result); |
| 171 | return result; |
| 172 | } |
| 173 | case kField_Command: { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 174 | const Variable* owner = this->symbolRef<Variable>(Symbol::Kind::kVariable); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 175 | uint8_t index = this->readU8(); |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 176 | Field* result = fSymbolTable->takeOwnershipOfSymbol( |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 177 | std::make_unique<Field>(/*offset=*/-1, owner, index)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 178 | return result; |
| 179 | } |
| 180 | case kNullableType_Command: { |
| 181 | uint16_t id = this->readU16(); |
| 182 | const Type* base = this->type(); |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 183 | Type* result = fSymbolTable->takeOwnershipOfSymbol( |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 184 | std::make_unique<Type>(base->name() + "?", Type::TypeKind::kNullable, *base)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 185 | 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 Stiles | f621e23 | 2020-08-25 13:33:02 -0400 | [diff] [blame] | 196 | StringFragment fieldName = this->readString(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 197 | const Type* type = this->type(); |
John Stiles | f621e23 | 2020-08-25 13:33:02 -0400 | [diff] [blame] | 198 | fields.emplace_back(m, fieldName, type); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 199 | } |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 200 | Type* result = fSymbolTable->takeOwnershipOfSymbol( |
John Stiles | 3ae071e | 2020-08-05 15:29:29 -0400 | [diff] [blame] | 201 | std::make_unique<Type>(/*offset=*/-1, name, std::move(fields))); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 202 | 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 Stiles | 49a547f | 2020-10-06 16:14:37 -0400 | [diff] [blame] | 210 | case kSymbolAlias_Command: { |
| 211 | uint16_t id = this->readU16(); |
| 212 | StringFragment name = this->readString(); |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 213 | Symbol* origSymbol = this->symbol(); |
| 214 | SymbolAlias* symbolAlias = fSymbolTable->takeOwnershipOfSymbol( |
John Stiles | 49a547f | 2020-10-06 16:14:37 -0400 | [diff] [blame] | 215 | std::make_unique<SymbolAlias>(/*offset=*/-1, name, origSymbol)); |
| 216 | this->addSymbol(id, symbolAlias); |
| 217 | return symbolAlias; |
| 218 | } |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 219 | case kSystemType_Command: { |
| 220 | uint16_t id = this->readU16(); |
| 221 | StringFragment name = this->readString(); |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 222 | Symbol* result = (*fSymbolTable)[name]; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 223 | SkASSERT(result && result->kind() == Symbol::Kind::kType); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 224 | 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 Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 234 | SkASSERT(f && f->kind() == Symbol::Kind::kFunctionDeclaration); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 235 | functions.push_back((const FunctionDeclaration*) f); |
| 236 | } |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 237 | UnresolvedFunction* result = fSymbolTable->takeOwnershipOfSymbol( |
John Stiles | 3ae071e | 2020-08-05 15:29:29 -0400 | [diff] [blame] | 238 | std::make_unique<UnresolvedFunction>(std::move(functions))); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 239 | this->addSymbol(id, result); |
| 240 | return result; |
| 241 | } |
| 242 | case kVariable_Command: { |
| 243 | uint16_t id = this->readU16(); |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 244 | ModifiersPool::Handle m = fModifiers.handle(this->modifiers()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 245 | StringFragment name = this->readString(); |
| 246 | const Type* type = this->type(); |
| 247 | Variable::Storage storage = (Variable::Storage) this->readU8(); |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 248 | Variable* result = fSymbolTable->takeOwnershipOfSymbol(std::make_unique<Variable>( |
Brian Osman | 3887a01 | 2020-09-30 13:22:27 -0400 | [diff] [blame] | 249 | /*offset=*/-1, m, name, type, /*builtin=*/true, storage)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 250 | 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 | |
| 260 | const Type* Rehydrator::type() { |
| 261 | const Symbol* result = this->symbol(); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 262 | SkASSERT(result->kind() == Symbol::Kind::kType); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 263 | return (const Type*) result; |
| 264 | } |
| 265 | |
| 266 | std::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 | |
| 278 | std::unique_ptr<ProgramElement> Rehydrator::element() { |
| 279 | int kind = this->readU8(); |
| 280 | switch (kind) { |
| 281 | case Rehydrator::kEnum_Command: { |
| 282 | StringFragment typeName = this->readString(); |
Brian Osman | 1313d1a | 2020-09-08 10:34:30 -0400 | [diff] [blame] | 283 | std::shared_ptr<SymbolTable> symbols = this->symbolTable(/*inherit=*/false); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 284 | for (auto& s : symbols->fOwnedSymbols) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 285 | SkASSERT(s->kind() == Symbol::Kind::kVariable); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 286 | Variable& v = (Variable&) *s; |
| 287 | int value = this->readS32(); |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 288 | v.setInitialValue(symbols->takeOwnershipOfIRNode( |
| 289 | std::make_unique<IntLiteral>(fContext, /*offset=*/-1, value))); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 290 | } |
| 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 Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 295 | Symbol::Kind::kFunctionDeclaration); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 296 | std::unique_ptr<Statement> body = this->statement(); |
John Stiles | b8e010c | 2020-08-11 18:05:39 -0400 | [diff] [blame] | 297 | std::unordered_set<const FunctionDeclaration*> refs; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 298 | uint8_t refCount = this->readU8(); |
| 299 | for (int i = 0; i < refCount; ++i) { |
| 300 | refs.insert(this->symbolRef<FunctionDeclaration>( |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 301 | Symbol::Kind::kFunctionDeclaration)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 302 | } |
| 303 | FunctionDefinition* result = new FunctionDefinition(-1, *decl, std::move(body), |
| 304 | std::move(refs)); |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame^] | 305 | decl->setDefinition(result); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 306 | return std::unique_ptr<ProgramElement>(result); |
| 307 | } |
| 308 | case Rehydrator::kInterfaceBlock_Command: { |
| 309 | const Symbol* var = this->symbol(); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 310 | SkASSERT(var && var->kind() == Symbol::Kind::kVariable); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 311 | StringFragment typeName = this->readString(); |
| 312 | StringFragment instanceName = this->readString(); |
| 313 | uint8_t sizeCount = this->readU8(); |
| 314 | std::vector<std::unique_ptr<Expression>> sizes; |
| 315 | sizes.reserve(sizeCount); |
| 316 | for (int i = 0; i < sizeCount; ++i) { |
| 317 | sizes.push_back(this->expression()); |
| 318 | } |
| 319 | return std::unique_ptr<ProgramElement>(new InterfaceBlock(-1, (Variable*) var, typeName, |
| 320 | instanceName, |
| 321 | std::move(sizes), nullptr)); |
| 322 | } |
| 323 | case Rehydrator::kVarDeclarations_Command: { |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 324 | std::unique_ptr<Statement> decl = this->statement(); |
| 325 | return std::unique_ptr<ProgramElement>( |
| 326 | new GlobalVarDeclaration(/*offset=*/-1, std::move(decl))); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 327 | } |
| 328 | default: |
| 329 | printf("unsupported element %d\n", kind); |
| 330 | SkASSERT(false); |
| 331 | return nullptr; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | std::unique_ptr<Statement> Rehydrator::statement() { |
| 336 | int kind = this->readU8(); |
| 337 | switch (kind) { |
| 338 | case Rehydrator::kBlock_Command: { |
| 339 | AutoRehydratorSymbolTable symbols(this); |
| 340 | int count = this->readU8(); |
| 341 | std::vector<std::unique_ptr<Statement>> statements; |
| 342 | statements.reserve(count); |
| 343 | for (int i = 0; i < count; ++i) { |
| 344 | statements.push_back(this->statement()); |
| 345 | } |
| 346 | bool isScope = this->readU8(); |
| 347 | return std::unique_ptr<Statement>(new Block(-1, std::move(statements), fSymbolTable, |
| 348 | isScope)); |
| 349 | } |
| 350 | case Rehydrator::kBreak_Command: |
| 351 | return std::unique_ptr<Statement>(new BreakStatement(-1)); |
| 352 | case Rehydrator::kContinue_Command: |
| 353 | return std::unique_ptr<Statement>(new ContinueStatement(-1)); |
| 354 | case Rehydrator::kDiscard_Command: |
| 355 | return std::unique_ptr<Statement>(new DiscardStatement(-1)); |
| 356 | case Rehydrator::kDo_Command: { |
| 357 | std::unique_ptr<Statement> stmt = this->statement(); |
| 358 | std::unique_ptr<Expression> expr = this->expression(); |
| 359 | return std::unique_ptr<Statement>(new DoStatement(-1, std::move(stmt), |
| 360 | std::move(expr))); |
| 361 | } |
| 362 | case Rehydrator::kExpressionStatement_Command: { |
| 363 | std::unique_ptr<Expression> expr = this->expression(); |
| 364 | return std::unique_ptr<Statement>(new ExpressionStatement(std::move(expr))); |
| 365 | } |
| 366 | case Rehydrator::kFor_Command: { |
| 367 | std::unique_ptr<Statement> initializer = this->statement(); |
| 368 | std::unique_ptr<Expression> test = this->expression(); |
| 369 | std::unique_ptr<Expression> next = this->expression(); |
| 370 | std::unique_ptr<Statement> body = this->statement(); |
| 371 | std::shared_ptr<SymbolTable> symbols = this->symbolTable(); |
| 372 | return std::unique_ptr<Statement>(new ForStatement(-1, std::move(initializer), |
| 373 | std::move(test), std::move(next), |
| 374 | std::move(body), |
| 375 | std::move(symbols))); |
| 376 | } |
| 377 | case Rehydrator::kIf_Command: { |
| 378 | bool isStatic = this->readU8(); |
| 379 | std::unique_ptr<Expression> test = this->expression(); |
| 380 | std::unique_ptr<Statement> ifTrue = this->statement(); |
| 381 | std::unique_ptr<Statement> ifFalse = this->statement(); |
| 382 | return std::unique_ptr<Statement>(new IfStatement(-1, isStatic, std::move(test), |
| 383 | std::move(ifTrue), |
| 384 | std::move(ifFalse))); |
| 385 | } |
John Stiles | 98c1f82 | 2020-09-09 14:18:53 -0400 | [diff] [blame] | 386 | case Rehydrator::kInlineMarker_Command: { |
| 387 | const FunctionDeclaration* funcDecl = this->symbolRef<FunctionDeclaration>( |
| 388 | Symbol::Kind::kFunctionDeclaration); |
| 389 | return std::make_unique<InlineMarker>(*funcDecl); |
| 390 | } |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 391 | case Rehydrator::kReturn_Command: { |
| 392 | std::unique_ptr<Expression> expr = this->expression(); |
| 393 | if (expr) { |
| 394 | return std::unique_ptr<Statement>(new ReturnStatement(std::move(expr))); |
| 395 | } else { |
| 396 | return std::unique_ptr<Statement>(new ReturnStatement(-1)); |
| 397 | } |
| 398 | } |
| 399 | case Rehydrator::kSwitch_Command: { |
| 400 | bool isStatic = this->readU8(); |
| 401 | AutoRehydratorSymbolTable symbols(this); |
| 402 | std::unique_ptr<Expression> expr = this->expression(); |
| 403 | int caseCount = this->readU8(); |
| 404 | std::vector<std::unique_ptr<SwitchCase>> cases; |
| 405 | cases.reserve(caseCount); |
| 406 | for (int i = 0; i < caseCount; ++i) { |
| 407 | std::unique_ptr<Expression> value = this->expression(); |
| 408 | int statementCount = this->readU8(); |
| 409 | std::vector<std::unique_ptr<Statement>> statements; |
| 410 | statements.reserve(statementCount); |
| 411 | for (int j = 0; j < statementCount; ++j) { |
| 412 | statements.push_back(this->statement()); |
| 413 | } |
| 414 | cases.emplace_back(new SwitchCase(-1, std::move(value), std::move(statements))); |
| 415 | } |
| 416 | return std::unique_ptr<Statement>(new SwitchStatement(-1, isStatic, std::move(expr), |
| 417 | std::move(cases), |
| 418 | fSymbolTable)); |
| 419 | } |
| 420 | case Rehydrator::kVarDeclaration_Command: { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 421 | Variable* var = this->symbolRef<Variable>(Symbol::Kind::kVariable); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 422 | const Type* baseType = this->type(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 423 | uint8_t sizeCount = this->readU8(); |
| 424 | std::vector<std::unique_ptr<Expression>> sizes; |
| 425 | sizes.reserve(sizeCount); |
| 426 | for (int i = 0; i < sizeCount; ++i) { |
| 427 | sizes.push_back(this->expression()); |
| 428 | } |
| 429 | std::unique_ptr<Expression> value = this->expression(); |
| 430 | if (value) { |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 431 | var->setInitialValue(value.get()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 432 | } |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 433 | return std::unique_ptr<Statement>( |
| 434 | new VarDeclaration(var, baseType, std::move(sizes), std::move(value))); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 435 | } |
| 436 | case Rehydrator::kVoid_Command: |
| 437 | return nullptr; |
| 438 | case Rehydrator::kWhile_Command: { |
| 439 | std::unique_ptr<Expression> expr = this->expression(); |
| 440 | std::unique_ptr<Statement> stmt = this->statement(); |
| 441 | return std::unique_ptr<Statement>(new WhileStatement(-1, std::move(expr), |
| 442 | std::move(stmt))); |
| 443 | } |
| 444 | default: |
| 445 | printf("unsupported statement %d\n", kind); |
| 446 | SkASSERT(false); |
| 447 | return nullptr; |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | std::unique_ptr<Expression> Rehydrator::expression() { |
| 452 | int kind = this->readU8(); |
| 453 | switch (kind) { |
| 454 | case Rehydrator::kBinary_Command: { |
| 455 | std::unique_ptr<Expression> left = this->expression(); |
| 456 | Token::Kind op = (Token::Kind) this->readU8(); |
| 457 | std::unique_ptr<Expression> right = this->expression(); |
| 458 | const Type* type = this->type(); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 459 | return std::make_unique<BinaryExpression>(-1, std::move(left), op, std::move(right), |
| 460 | type); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 461 | } |
| 462 | case Rehydrator::kBoolLiteral_Command: { |
| 463 | bool value = this->readU8(); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 464 | return std::make_unique<BoolLiteral>(fContext, -1, value); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 465 | } |
| 466 | case Rehydrator::kConstructor_Command: { |
| 467 | const Type* type = this->type(); |
| 468 | uint8_t argCount = this->readU8(); |
| 469 | std::vector<std::unique_ptr<Expression>> args; |
| 470 | args.reserve(argCount); |
| 471 | for (int i = 0; i < argCount; ++i) { |
| 472 | args.push_back(this->expression()); |
| 473 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 474 | return std::make_unique<Constructor>(-1, type, std::move(args)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 475 | } |
| 476 | case Rehydrator::kFieldAccess_Command: { |
| 477 | std::unique_ptr<Expression> base = this->expression(); |
| 478 | int index = this->readU8(); |
| 479 | FieldAccess::OwnerKind ownerKind = (FieldAccess::OwnerKind) this->readU8(); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 480 | return std::make_unique<FieldAccess>(std::move(base), index, ownerKind); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 481 | } |
| 482 | case Rehydrator::kFloatLiteral_Command: { |
| 483 | FloatIntUnion u; |
| 484 | u.fInt = this->readS32(); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 485 | return std::make_unique<FloatLiteral>(fContext, -1, u.fFloat); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 486 | } |
| 487 | case Rehydrator::kFunctionCall_Command: { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 488 | const Type* type = this->type(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 489 | const FunctionDeclaration* f = this->symbolRef<FunctionDeclaration>( |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 490 | Symbol::Kind::kFunctionDeclaration); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 491 | uint8_t argCount = this->readU8(); |
| 492 | std::vector<std::unique_ptr<Expression>> args; |
| 493 | args.reserve(argCount); |
| 494 | for (int i = 0; i < argCount; ++i) { |
| 495 | args.push_back(this->expression()); |
| 496 | } |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 497 | return std::make_unique<FunctionCall>(-1, type, f, std::move(args)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 498 | } |
| 499 | case Rehydrator::kIndex_Command: { |
| 500 | std::unique_ptr<Expression> base = this->expression(); |
| 501 | std::unique_ptr<Expression> index = this->expression(); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 502 | return std::make_unique<IndexExpression>(fContext, std::move(base), std::move(index)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 503 | } |
| 504 | case Rehydrator::kIntLiteral_Command: { |
| 505 | int value = this->readS32(); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 506 | return std::make_unique<IntLiteral>(fContext, -1, value); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 507 | } |
| 508 | case Rehydrator::kNullLiteral_Command: |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 509 | return std::make_unique<NullLiteral>(fContext, -1); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 510 | case Rehydrator::kPostfix_Command: { |
| 511 | Token::Kind op = (Token::Kind) this->readU8(); |
| 512 | std::unique_ptr<Expression> operand = this->expression(); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 513 | return std::make_unique<PostfixExpression>(std::move(operand), op); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 514 | } |
| 515 | case Rehydrator::kPrefix_Command: { |
| 516 | Token::Kind op = (Token::Kind) this->readU8(); |
| 517 | std::unique_ptr<Expression> operand = this->expression(); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 518 | return std::make_unique<PrefixExpression>(op, std::move(operand)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 519 | } |
| 520 | case Rehydrator::kSetting_Command: { |
| 521 | StringFragment name = this->readString(); |
| 522 | std::unique_ptr<Expression> value = this->expression(); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 523 | return std::make_unique<Setting>(-1, name, std::move(value)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 524 | } |
| 525 | case Rehydrator::kSwizzle_Command: { |
| 526 | std::unique_ptr<Expression> base = this->expression(); |
| 527 | int count = this->readU8(); |
| 528 | std::vector<int> components; |
| 529 | components.reserve(count); |
| 530 | for (int i = 0; i < count; ++i) { |
| 531 | components.push_back(this->readU8()); |
| 532 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 533 | return std::make_unique<Swizzle>(fContext, std::move(base), std::move(components)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 534 | } |
| 535 | case Rehydrator::kTernary_Command: { |
| 536 | std::unique_ptr<Expression> test = this->expression(); |
| 537 | std::unique_ptr<Expression> ifTrue = this->expression(); |
| 538 | std::unique_ptr<Expression> ifFalse = this->expression(); |
Ethan Nicholas | a02ce0c | 2020-09-21 12:37:02 -0400 | [diff] [blame] | 539 | return std::make_unique<TernaryExpression>(-1, std::move(test), std::move(ifTrue), |
| 540 | std::move(ifFalse)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 541 | } |
| 542 | case Rehydrator::kVariableReference_Command: { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 543 | const Variable* var = this->symbolRef<Variable>(Symbol::Kind::kVariable); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 544 | VariableReference::RefKind refKind = (VariableReference::RefKind) this->readU8(); |
Brian Osman | 79457ef | 2020-09-24 15:01:27 -0400 | [diff] [blame] | 545 | return std::make_unique<VariableReference>(-1, var, refKind); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 546 | } |
| 547 | case Rehydrator::kVoid_Command: |
| 548 | return nullptr; |
| 549 | default: |
| 550 | printf("unsupported expression %d\n", kind); |
| 551 | SkASSERT(false); |
| 552 | return nullptr; |
| 553 | } |
| 554 | } |
| 555 | |
Brian Osman | 1313d1a | 2020-09-08 10:34:30 -0400 | [diff] [blame] | 556 | std::shared_ptr<SymbolTable> Rehydrator::symbolTable(bool inherit) { |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 557 | int command = this->readU8(); |
| 558 | if (command == kVoid_Command) { |
| 559 | return nullptr; |
| 560 | } |
| 561 | SkASSERT(command == kSymbolTable_Command); |
| 562 | uint16_t ownedCount = this->readU16(); |
Brian Osman | 1313d1a | 2020-09-08 10:34:30 -0400 | [diff] [blame] | 563 | std::shared_ptr<SymbolTable> oldTable = fSymbolTable; |
| 564 | std::shared_ptr<SymbolTable> result = inherit ? std::make_shared<SymbolTable>(fSymbolTable) |
| 565 | : std::make_shared<SymbolTable>(fErrors); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 566 | fSymbolTable = result; |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 567 | std::vector<Symbol*> ownedSymbols; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 568 | ownedSymbols.reserve(ownedCount); |
| 569 | for (int i = 0; i < ownedCount; ++i) { |
| 570 | ownedSymbols.push_back(this->symbol()); |
| 571 | } |
| 572 | uint16_t symbolCount = this->readU16(); |
| 573 | std::vector<std::pair<StringFragment, int>> symbols; |
| 574 | symbols.reserve(symbolCount); |
| 575 | for (int i = 0; i < symbolCount; ++i) { |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 576 | int index = this->readU16(); |
John Stiles | b8cc665 | 2020-10-08 09:12:07 -0400 | [diff] [blame] | 577 | fSymbolTable->addWithoutOwnership(ownedSymbols[index]); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 578 | } |
Brian Osman | 1313d1a | 2020-09-08 10:34:30 -0400 | [diff] [blame] | 579 | fSymbolTable = oldTable; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 580 | return result; |
| 581 | } |
| 582 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 583 | } // namespace SkSL |