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 | |
John Stiles | 7c3515b | 2020-10-16 18:38:39 -0400 | [diff] [blame] | 72 | Rehydrator::Rehydrator(const Context* context, ModifiersPool* modifiers, |
| 73 | std::shared_ptr<SymbolTable> symbolTable, ErrorReporter* errorReporter, |
| 74 | const uint8_t* src, size_t length) |
| 75 | : fContext(*context) |
| 76 | , fModifiers(*modifiers) |
| 77 | , fErrors(errorReporter) |
| 78 | , fSymbolTable(std::move(symbolTable)) |
| 79 | , fStart(src) |
| 80 | SkDEBUGCODE(, fEnd(fStart + length)) { |
| 81 | SkASSERT(fSymbolTable); |
| 82 | SkASSERT(fSymbolTable->isBuiltin()); |
| 83 | // skip past string data |
| 84 | fIP = fStart; |
| 85 | fIP += this->readU16(); |
| 86 | } |
| 87 | |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 88 | Layout Rehydrator::layout() { |
| 89 | switch (this->readU8()) { |
| 90 | case kBuiltinLayout_Command: { |
| 91 | Layout result; |
| 92 | result.fBuiltin = this->readS16(); |
| 93 | return result; |
| 94 | } |
| 95 | case kDefaultLayout_Command: |
| 96 | return Layout(); |
| 97 | case kLayout_Command: { |
| 98 | int flags = this->readU32(); |
| 99 | int location = this->readS8(); |
| 100 | int offset = this->readS8(); |
| 101 | int binding = this->readS8(); |
| 102 | int index = this->readS8(); |
| 103 | int set = this->readS8(); |
| 104 | int builtin = this->readS16(); |
| 105 | int inputAttachmentIndex = this->readS8(); |
| 106 | int format = this->readS8(); |
| 107 | int primitive = this->readS8(); |
| 108 | int maxVertices = this->readS8(); |
| 109 | int invocations = this->readS8(); |
| 110 | StringFragment marker = this->readString(); |
| 111 | StringFragment when = this->readString(); |
| 112 | int key = this->readS8(); |
| 113 | int ctype = this->readS8(); |
| 114 | return Layout(flags, location, offset, binding, index, set, builtin, |
| 115 | inputAttachmentIndex, (Layout::Format) format, |
| 116 | (Layout::Primitive) primitive, maxVertices, invocations, marker, when, |
| 117 | (Layout::Key) key, (Layout::CType) ctype); |
| 118 | } |
| 119 | default: |
| 120 | SkASSERT(false); |
| 121 | return Layout(); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | Modifiers Rehydrator::modifiers() { |
| 126 | switch (this->readU8()) { |
| 127 | case kDefaultModifiers_Command: |
| 128 | return Modifiers(); |
| 129 | case kModifiers8Bit_Command: { |
| 130 | Layout l = this->layout(); |
| 131 | int flags = this->readU8(); |
| 132 | return Modifiers(l, flags); |
| 133 | } |
| 134 | case kModifiers_Command: { |
| 135 | Layout l = this->layout(); |
| 136 | int flags = this->readS32(); |
| 137 | return Modifiers(l, flags); |
| 138 | } |
| 139 | default: |
| 140 | SkASSERT(false); |
| 141 | return Modifiers(); |
| 142 | } |
| 143 | } |
| 144 | |
Brian Osman | 5bf3e20 | 2020-10-13 10:34:18 -0400 | [diff] [blame] | 145 | const Symbol* Rehydrator::symbol() { |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 146 | int kind = this->readU8(); |
| 147 | switch (kind) { |
| 148 | case kArrayType_Command: { |
| 149 | uint16_t id = this->readU16(); |
| 150 | const Type* componentType = this->type(); |
Brian Osman | e8c2608 | 2020-10-01 17:22:45 -0400 | [diff] [blame] | 151 | int8_t count = this->readS8(); |
| 152 | String name = componentType->name(); |
| 153 | if (count == Type::kUnsizedArray) { |
| 154 | name += "[]"; |
| 155 | } else { |
| 156 | name += "[" + to_string(count) + "]"; |
| 157 | } |
Brian Osman | 5bf3e20 | 2020-10-13 10:34:18 -0400 | [diff] [blame] | 158 | const Type* result = fSymbolTable->takeOwnershipOfSymbol( |
Brian Osman | e8c2608 | 2020-10-01 17:22:45 -0400 | [diff] [blame] | 159 | std::make_unique<Type>(name, Type::TypeKind::kArray, *componentType, count)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 160 | this->addSymbol(id, result); |
| 161 | return result; |
| 162 | } |
| 163 | case kEnumType_Command: { |
| 164 | uint16_t id = this->readU16(); |
| 165 | StringFragment name = this->readString(); |
Brian Osman | 5bf3e20 | 2020-10-13 10:34:18 -0400 | [diff] [blame] | 166 | const Type* result = fSymbolTable->takeOwnershipOfSymbol( |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 167 | std::make_unique<Type>(name, Type::TypeKind::kEnum)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 168 | this->addSymbol(id, result); |
| 169 | return result; |
| 170 | } |
| 171 | case kFunctionDeclaration_Command: { |
| 172 | uint16_t id = this->readU16(); |
| 173 | Modifiers modifiers = this->modifiers(); |
| 174 | StringFragment name = this->readString(); |
| 175 | int parameterCount = this->readU8(); |
Brian Osman | 5bf3e20 | 2020-10-13 10:34:18 -0400 | [diff] [blame] | 176 | std::vector<const Variable*> parameters; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 177 | parameters.reserve(parameterCount); |
| 178 | for (int i = 0; i < parameterCount; ++i) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 179 | parameters.push_back(this->symbolRef<Variable>(Symbol::Kind::kVariable)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 180 | } |
| 181 | const Type* returnType = this->type(); |
Brian Osman | 5bf3e20 | 2020-10-13 10:34:18 -0400 | [diff] [blame] | 182 | const FunctionDeclaration* result = |
John Stiles | 3ae071e | 2020-08-05 15:29:29 -0400 | [diff] [blame] | 183 | fSymbolTable->takeOwnershipOfSymbol(std::make_unique<FunctionDeclaration>( |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 184 | /*offset=*/-1, fModifiers.handle(modifiers), name, |
| 185 | std::move(parameters), returnType, /*builtin=*/true)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 186 | this->addSymbol(id, result); |
| 187 | return result; |
| 188 | } |
| 189 | case kField_Command: { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 190 | const Variable* owner = this->symbolRef<Variable>(Symbol::Kind::kVariable); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 191 | uint8_t index = this->readU8(); |
Brian Osman | 5bf3e20 | 2020-10-13 10:34:18 -0400 | [diff] [blame] | 192 | const Field* result = fSymbolTable->takeOwnershipOfSymbol( |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 193 | std::make_unique<Field>(/*offset=*/-1, owner, index)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 194 | return result; |
| 195 | } |
| 196 | case kNullableType_Command: { |
| 197 | uint16_t id = this->readU16(); |
| 198 | const Type* base = this->type(); |
Brian Osman | 5bf3e20 | 2020-10-13 10:34:18 -0400 | [diff] [blame] | 199 | const Type* result = fSymbolTable->takeOwnershipOfSymbol( |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 200 | std::make_unique<Type>(base->name() + "?", Type::TypeKind::kNullable, *base)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 201 | this->addSymbol(id, result); |
| 202 | return result; |
| 203 | } |
| 204 | case kStructType_Command: { |
| 205 | uint16_t id = this->readU16(); |
| 206 | StringFragment name = this->readString(); |
| 207 | uint8_t fieldCount = this->readU8(); |
| 208 | std::vector<Type::Field> fields; |
| 209 | fields.reserve(fieldCount); |
| 210 | for (int i = 0; i < fieldCount; ++i) { |
| 211 | Modifiers m = this->modifiers(); |
John Stiles | f621e23 | 2020-08-25 13:33:02 -0400 | [diff] [blame] | 212 | StringFragment fieldName = this->readString(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 213 | const Type* type = this->type(); |
John Stiles | f621e23 | 2020-08-25 13:33:02 -0400 | [diff] [blame] | 214 | fields.emplace_back(m, fieldName, type); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 215 | } |
Brian Osman | 5bf3e20 | 2020-10-13 10:34:18 -0400 | [diff] [blame] | 216 | const Type* result = fSymbolTable->takeOwnershipOfSymbol( |
John Stiles | 3ae071e | 2020-08-05 15:29:29 -0400 | [diff] [blame] | 217 | std::make_unique<Type>(/*offset=*/-1, name, std::move(fields))); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 218 | this->addSymbol(id, result); |
| 219 | return result; |
| 220 | } |
| 221 | case kSymbolRef_Command: { |
| 222 | uint16_t id = this->readU16(); |
| 223 | SkASSERT(fSymbols.size() > id); |
| 224 | return fSymbols[id]; |
| 225 | } |
John Stiles | 49a547f | 2020-10-06 16:14:37 -0400 | [diff] [blame] | 226 | case kSymbolAlias_Command: { |
| 227 | uint16_t id = this->readU16(); |
| 228 | StringFragment name = this->readString(); |
Brian Osman | 5bf3e20 | 2020-10-13 10:34:18 -0400 | [diff] [blame] | 229 | const Symbol* origSymbol = this->symbol(); |
| 230 | const SymbolAlias* symbolAlias = fSymbolTable->takeOwnershipOfSymbol( |
John Stiles | 49a547f | 2020-10-06 16:14:37 -0400 | [diff] [blame] | 231 | std::make_unique<SymbolAlias>(/*offset=*/-1, name, origSymbol)); |
| 232 | this->addSymbol(id, symbolAlias); |
| 233 | return symbolAlias; |
| 234 | } |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 235 | case kSystemType_Command: { |
| 236 | uint16_t id = this->readU16(); |
| 237 | StringFragment name = this->readString(); |
Brian Osman | 5bf3e20 | 2020-10-13 10:34:18 -0400 | [diff] [blame] | 238 | const Symbol* result = (*fSymbolTable)[name]; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 239 | SkASSERT(result && result->kind() == Symbol::Kind::kType); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 240 | this->addSymbol(id, result); |
| 241 | return result; |
| 242 | } |
| 243 | case kUnresolvedFunction_Command: { |
| 244 | uint16_t id = this->readU16(); |
| 245 | int length = this->readU8(); |
| 246 | std::vector<const FunctionDeclaration*> functions; |
| 247 | functions.reserve(length); |
| 248 | for (int i = 0; i < length; ++i) { |
| 249 | const Symbol* f = this->symbol(); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 250 | SkASSERT(f && f->kind() == Symbol::Kind::kFunctionDeclaration); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 251 | functions.push_back((const FunctionDeclaration*) f); |
| 252 | } |
Brian Osman | 5bf3e20 | 2020-10-13 10:34:18 -0400 | [diff] [blame] | 253 | const UnresolvedFunction* result = fSymbolTable->takeOwnershipOfSymbol( |
John Stiles | 3ae071e | 2020-08-05 15:29:29 -0400 | [diff] [blame] | 254 | std::make_unique<UnresolvedFunction>(std::move(functions))); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 255 | this->addSymbol(id, result); |
| 256 | return result; |
| 257 | } |
| 258 | case kVariable_Command: { |
| 259 | uint16_t id = this->readU16(); |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 260 | ModifiersPool::Handle m = fModifiers.handle(this->modifiers()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 261 | StringFragment name = this->readString(); |
| 262 | const Type* type = this->type(); |
| 263 | Variable::Storage storage = (Variable::Storage) this->readU8(); |
Brian Osman | 5bf3e20 | 2020-10-13 10:34:18 -0400 | [diff] [blame] | 264 | const Variable* result = fSymbolTable->takeOwnershipOfSymbol(std::make_unique<Variable>( |
Brian Osman | 3887a01 | 2020-09-30 13:22:27 -0400 | [diff] [blame] | 265 | /*offset=*/-1, m, name, type, /*builtin=*/true, storage)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 266 | this->addSymbol(id, result); |
| 267 | return result; |
| 268 | } |
| 269 | default: |
| 270 | printf("unsupported symbol %d\n", kind); |
| 271 | SkASSERT(false); |
| 272 | return nullptr; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | const Type* Rehydrator::type() { |
| 277 | const Symbol* result = this->symbol(); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 278 | SkASSERT(result->kind() == Symbol::Kind::kType); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 279 | return (const Type*) result; |
| 280 | } |
| 281 | |
| 282 | std::vector<std::unique_ptr<ProgramElement>> Rehydrator::elements() { |
| 283 | SkDEBUGCODE(uint8_t command = )this->readU8(); |
| 284 | SkASSERT(command == kElements_Command); |
| 285 | uint8_t count = this->readU8(); |
| 286 | std::vector<std::unique_ptr<ProgramElement>> result; |
| 287 | result.reserve(count); |
| 288 | for (int i = 0; i < count; ++i) { |
| 289 | result.push_back(this->element()); |
| 290 | } |
| 291 | return result; |
| 292 | } |
| 293 | |
| 294 | std::unique_ptr<ProgramElement> Rehydrator::element() { |
| 295 | int kind = this->readU8(); |
| 296 | switch (kind) { |
| 297 | case Rehydrator::kEnum_Command: { |
| 298 | StringFragment typeName = this->readString(); |
Brian Osman | 1313d1a | 2020-09-08 10:34:30 -0400 | [diff] [blame] | 299 | std::shared_ptr<SymbolTable> symbols = this->symbolTable(/*inherit=*/false); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 300 | for (auto& s : symbols->fOwnedSymbols) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 301 | SkASSERT(s->kind() == Symbol::Kind::kVariable); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 302 | Variable& v = (Variable&) *s; |
| 303 | int value = this->readS32(); |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 304 | v.setInitialValue(symbols->takeOwnershipOfIRNode( |
| 305 | std::make_unique<IntLiteral>(fContext, /*offset=*/-1, value))); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 306 | } |
| 307 | return std::unique_ptr<ProgramElement>(new Enum(-1, typeName, std::move(symbols))); |
| 308 | } |
| 309 | case Rehydrator::kFunctionDefinition_Command: { |
| 310 | const FunctionDeclaration* decl = this->symbolRef<FunctionDeclaration>( |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 311 | Symbol::Kind::kFunctionDeclaration); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 312 | std::unique_ptr<Statement> body = this->statement(); |
John Stiles | b8e010c | 2020-08-11 18:05:39 -0400 | [diff] [blame] | 313 | std::unordered_set<const FunctionDeclaration*> refs; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 314 | uint8_t refCount = this->readU8(); |
| 315 | for (int i = 0; i < refCount; ++i) { |
| 316 | refs.insert(this->symbolRef<FunctionDeclaration>( |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 317 | Symbol::Kind::kFunctionDeclaration)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 318 | } |
John Stiles | 607d36b | 2020-10-19 15:00:01 -0400 | [diff] [blame^] | 319 | auto result = std::make_unique<FunctionDefinition>(/*offset=*/-1, decl, |
| 320 | /*builtin=*/true, std::move(body), |
| 321 | std::move(refs)); |
| 322 | decl->setDefinition(result.get()); |
| 323 | return std::move(result); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 324 | } |
| 325 | case Rehydrator::kInterfaceBlock_Command: { |
| 326 | const Symbol* var = this->symbol(); |
John Stiles | 87ae34e | 2020-10-13 12:50:11 -0400 | [diff] [blame] | 327 | SkASSERT(var && var->is<Variable>()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 328 | StringFragment typeName = this->readString(); |
| 329 | StringFragment instanceName = this->readString(); |
| 330 | uint8_t sizeCount = this->readU8(); |
John Stiles | 87ae34e | 2020-10-13 12:50:11 -0400 | [diff] [blame] | 331 | ExpressionArray sizes; |
John Stiles | f4bda74 | 2020-10-14 16:57:41 -0400 | [diff] [blame] | 332 | sizes.reserve_back(sizeCount); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 333 | for (int i = 0; i < sizeCount; ++i) { |
| 334 | sizes.push_back(this->expression()); |
| 335 | } |
John Stiles | 87ae34e | 2020-10-13 12:50:11 -0400 | [diff] [blame] | 336 | return std::make_unique<InterfaceBlock>(/*offset=*/-1, &var->as<Variable>(), typeName, |
| 337 | instanceName, std::move(sizes), nullptr); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 338 | } |
| 339 | case Rehydrator::kVarDeclarations_Command: { |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 340 | std::unique_ptr<Statement> decl = this->statement(); |
| 341 | return std::unique_ptr<ProgramElement>( |
| 342 | new GlobalVarDeclaration(/*offset=*/-1, std::move(decl))); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 343 | } |
| 344 | default: |
| 345 | printf("unsupported element %d\n", kind); |
| 346 | SkASSERT(false); |
| 347 | return nullptr; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | std::unique_ptr<Statement> Rehydrator::statement() { |
| 352 | int kind = this->readU8(); |
| 353 | switch (kind) { |
| 354 | case Rehydrator::kBlock_Command: { |
| 355 | AutoRehydratorSymbolTable symbols(this); |
| 356 | int count = this->readU8(); |
John Stiles | 8f2a0cf | 2020-10-13 12:48:21 -0400 | [diff] [blame] | 357 | StatementArray statements; |
John Stiles | f4bda74 | 2020-10-14 16:57:41 -0400 | [diff] [blame] | 358 | statements.reserve_back(count); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 359 | for (int i = 0; i < count; ++i) { |
| 360 | statements.push_back(this->statement()); |
| 361 | } |
| 362 | bool isScope = this->readU8(); |
John Stiles | 8f2a0cf | 2020-10-13 12:48:21 -0400 | [diff] [blame] | 363 | return std::make_unique<Block>(/*offset=*/-1, std::move(statements), fSymbolTable, |
| 364 | isScope); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 365 | } |
| 366 | case Rehydrator::kBreak_Command: |
| 367 | return std::unique_ptr<Statement>(new BreakStatement(-1)); |
| 368 | case Rehydrator::kContinue_Command: |
| 369 | return std::unique_ptr<Statement>(new ContinueStatement(-1)); |
| 370 | case Rehydrator::kDiscard_Command: |
| 371 | return std::unique_ptr<Statement>(new DiscardStatement(-1)); |
| 372 | case Rehydrator::kDo_Command: { |
| 373 | std::unique_ptr<Statement> stmt = this->statement(); |
| 374 | std::unique_ptr<Expression> expr = this->expression(); |
| 375 | return std::unique_ptr<Statement>(new DoStatement(-1, std::move(stmt), |
| 376 | std::move(expr))); |
| 377 | } |
| 378 | case Rehydrator::kExpressionStatement_Command: { |
| 379 | std::unique_ptr<Expression> expr = this->expression(); |
| 380 | return std::unique_ptr<Statement>(new ExpressionStatement(std::move(expr))); |
| 381 | } |
| 382 | case Rehydrator::kFor_Command: { |
| 383 | std::unique_ptr<Statement> initializer = this->statement(); |
| 384 | std::unique_ptr<Expression> test = this->expression(); |
| 385 | std::unique_ptr<Expression> next = this->expression(); |
| 386 | std::unique_ptr<Statement> body = this->statement(); |
| 387 | std::shared_ptr<SymbolTable> symbols = this->symbolTable(); |
| 388 | return std::unique_ptr<Statement>(new ForStatement(-1, std::move(initializer), |
| 389 | std::move(test), std::move(next), |
| 390 | std::move(body), |
| 391 | std::move(symbols))); |
| 392 | } |
| 393 | case Rehydrator::kIf_Command: { |
| 394 | bool isStatic = this->readU8(); |
| 395 | std::unique_ptr<Expression> test = this->expression(); |
| 396 | std::unique_ptr<Statement> ifTrue = this->statement(); |
| 397 | std::unique_ptr<Statement> ifFalse = this->statement(); |
| 398 | return std::unique_ptr<Statement>(new IfStatement(-1, isStatic, std::move(test), |
| 399 | std::move(ifTrue), |
| 400 | std::move(ifFalse))); |
| 401 | } |
John Stiles | 98c1f82 | 2020-09-09 14:18:53 -0400 | [diff] [blame] | 402 | case Rehydrator::kInlineMarker_Command: { |
| 403 | const FunctionDeclaration* funcDecl = this->symbolRef<FunctionDeclaration>( |
| 404 | Symbol::Kind::kFunctionDeclaration); |
Ethan Nicholas | ceb6214 | 2020-10-09 16:51:18 -0400 | [diff] [blame] | 405 | return std::make_unique<InlineMarker>(funcDecl); |
John Stiles | 98c1f82 | 2020-09-09 14:18:53 -0400 | [diff] [blame] | 406 | } |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 407 | case Rehydrator::kReturn_Command: { |
| 408 | std::unique_ptr<Expression> expr = this->expression(); |
| 409 | if (expr) { |
| 410 | return std::unique_ptr<Statement>(new ReturnStatement(std::move(expr))); |
| 411 | } else { |
| 412 | return std::unique_ptr<Statement>(new ReturnStatement(-1)); |
| 413 | } |
| 414 | } |
| 415 | case Rehydrator::kSwitch_Command: { |
| 416 | bool isStatic = this->readU8(); |
| 417 | AutoRehydratorSymbolTable symbols(this); |
| 418 | std::unique_ptr<Expression> expr = this->expression(); |
| 419 | int caseCount = this->readU8(); |
| 420 | std::vector<std::unique_ptr<SwitchCase>> cases; |
| 421 | cases.reserve(caseCount); |
| 422 | for (int i = 0; i < caseCount; ++i) { |
| 423 | std::unique_ptr<Expression> value = this->expression(); |
| 424 | int statementCount = this->readU8(); |
John Stiles | 8f2a0cf | 2020-10-13 12:48:21 -0400 | [diff] [blame] | 425 | StatementArray statements; |
John Stiles | f4bda74 | 2020-10-14 16:57:41 -0400 | [diff] [blame] | 426 | statements.reserve_back(statementCount); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 427 | for (int j = 0; j < statementCount; ++j) { |
| 428 | statements.push_back(this->statement()); |
| 429 | } |
John Stiles | 8f2a0cf | 2020-10-13 12:48:21 -0400 | [diff] [blame] | 430 | cases.push_back(std::make_unique<SwitchCase>(/*offset=*/-1, std::move(value), |
| 431 | std::move(statements))); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 432 | } |
John Stiles | 8f2a0cf | 2020-10-13 12:48:21 -0400 | [diff] [blame] | 433 | return std::make_unique<SwitchStatement>(-1, isStatic, std::move(expr), |
| 434 | std::move(cases), fSymbolTable); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 435 | } |
| 436 | case Rehydrator::kVarDeclaration_Command: { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 437 | Variable* var = this->symbolRef<Variable>(Symbol::Kind::kVariable); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 438 | const Type* baseType = this->type(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 439 | uint8_t sizeCount = this->readU8(); |
John Stiles | 87ae34e | 2020-10-13 12:50:11 -0400 | [diff] [blame] | 440 | ExpressionArray sizes; |
John Stiles | f4bda74 | 2020-10-14 16:57:41 -0400 | [diff] [blame] | 441 | sizes.reserve_back(sizeCount); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 442 | for (int i = 0; i < sizeCount; ++i) { |
| 443 | sizes.push_back(this->expression()); |
| 444 | } |
| 445 | std::unique_ptr<Expression> value = this->expression(); |
| 446 | if (value) { |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 447 | var->setInitialValue(value.get()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 448 | } |
John Stiles | 87ae34e | 2020-10-13 12:50:11 -0400 | [diff] [blame] | 449 | return std::make_unique<VarDeclaration>(var, baseType, std::move(sizes), |
| 450 | std::move(value)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 451 | } |
| 452 | case Rehydrator::kVoid_Command: |
| 453 | return nullptr; |
| 454 | case Rehydrator::kWhile_Command: { |
| 455 | std::unique_ptr<Expression> expr = this->expression(); |
| 456 | std::unique_ptr<Statement> stmt = this->statement(); |
| 457 | return std::unique_ptr<Statement>(new WhileStatement(-1, std::move(expr), |
| 458 | std::move(stmt))); |
| 459 | } |
| 460 | default: |
| 461 | printf("unsupported statement %d\n", kind); |
| 462 | SkASSERT(false); |
| 463 | return nullptr; |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | std::unique_ptr<Expression> Rehydrator::expression() { |
| 468 | int kind = this->readU8(); |
| 469 | switch (kind) { |
| 470 | case Rehydrator::kBinary_Command: { |
| 471 | std::unique_ptr<Expression> left = this->expression(); |
| 472 | Token::Kind op = (Token::Kind) this->readU8(); |
| 473 | std::unique_ptr<Expression> right = this->expression(); |
| 474 | const Type* type = this->type(); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 475 | return std::make_unique<BinaryExpression>(-1, std::move(left), op, std::move(right), |
| 476 | type); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 477 | } |
| 478 | case Rehydrator::kBoolLiteral_Command: { |
| 479 | bool value = this->readU8(); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 480 | return std::make_unique<BoolLiteral>(fContext, -1, value); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 481 | } |
| 482 | case Rehydrator::kConstructor_Command: { |
| 483 | const Type* type = this->type(); |
| 484 | uint8_t argCount = this->readU8(); |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 485 | ExpressionArray args; |
John Stiles | f4bda74 | 2020-10-14 16:57:41 -0400 | [diff] [blame] | 486 | args.reserve_back(argCount); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 487 | for (int i = 0; i < argCount; ++i) { |
| 488 | args.push_back(this->expression()); |
| 489 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 490 | return std::make_unique<Constructor>(-1, type, std::move(args)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 491 | } |
| 492 | case Rehydrator::kFieldAccess_Command: { |
| 493 | std::unique_ptr<Expression> base = this->expression(); |
| 494 | int index = this->readU8(); |
| 495 | FieldAccess::OwnerKind ownerKind = (FieldAccess::OwnerKind) this->readU8(); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 496 | return std::make_unique<FieldAccess>(std::move(base), index, ownerKind); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 497 | } |
| 498 | case Rehydrator::kFloatLiteral_Command: { |
| 499 | FloatIntUnion u; |
| 500 | u.fInt = this->readS32(); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 501 | return std::make_unique<FloatLiteral>(fContext, -1, u.fFloat); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 502 | } |
| 503 | case Rehydrator::kFunctionCall_Command: { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 504 | const Type* type = this->type(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 505 | const FunctionDeclaration* f = this->symbolRef<FunctionDeclaration>( |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 506 | Symbol::Kind::kFunctionDeclaration); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 507 | uint8_t argCount = this->readU8(); |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 508 | ExpressionArray args; |
John Stiles | f4bda74 | 2020-10-14 16:57:41 -0400 | [diff] [blame] | 509 | args.reserve_back(argCount); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 510 | for (int i = 0; i < argCount; ++i) { |
| 511 | args.push_back(this->expression()); |
| 512 | } |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 513 | return std::make_unique<FunctionCall>(-1, type, f, std::move(args)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 514 | } |
| 515 | case Rehydrator::kIndex_Command: { |
| 516 | std::unique_ptr<Expression> base = this->expression(); |
| 517 | std::unique_ptr<Expression> index = this->expression(); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 518 | return std::make_unique<IndexExpression>(fContext, std::move(base), std::move(index)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 519 | } |
| 520 | case Rehydrator::kIntLiteral_Command: { |
| 521 | int value = this->readS32(); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 522 | return std::make_unique<IntLiteral>(fContext, -1, value); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 523 | } |
| 524 | case Rehydrator::kNullLiteral_Command: |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 525 | return std::make_unique<NullLiteral>(fContext, -1); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 526 | case Rehydrator::kPostfix_Command: { |
| 527 | Token::Kind op = (Token::Kind) this->readU8(); |
| 528 | std::unique_ptr<Expression> operand = this->expression(); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 529 | return std::make_unique<PostfixExpression>(std::move(operand), op); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 530 | } |
| 531 | case Rehydrator::kPrefix_Command: { |
| 532 | Token::Kind op = (Token::Kind) this->readU8(); |
| 533 | std::unique_ptr<Expression> operand = this->expression(); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 534 | return std::make_unique<PrefixExpression>(op, std::move(operand)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 535 | } |
| 536 | case Rehydrator::kSetting_Command: { |
| 537 | StringFragment name = this->readString(); |
Ethan Nicholas | 01ec7e8 | 2020-10-08 12:10:12 -0400 | [diff] [blame] | 538 | const Type* type = this->type(); |
| 539 | return std::make_unique<Setting>(-1, name, type); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 540 | } |
| 541 | case Rehydrator::kSwizzle_Command: { |
| 542 | std::unique_ptr<Expression> base = this->expression(); |
| 543 | int count = this->readU8(); |
| 544 | std::vector<int> components; |
| 545 | components.reserve(count); |
| 546 | for (int i = 0; i < count; ++i) { |
| 547 | components.push_back(this->readU8()); |
| 548 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 549 | return std::make_unique<Swizzle>(fContext, std::move(base), std::move(components)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 550 | } |
| 551 | case Rehydrator::kTernary_Command: { |
| 552 | std::unique_ptr<Expression> test = this->expression(); |
| 553 | std::unique_ptr<Expression> ifTrue = this->expression(); |
| 554 | std::unique_ptr<Expression> ifFalse = this->expression(); |
Ethan Nicholas | a02ce0c | 2020-09-21 12:37:02 -0400 | [diff] [blame] | 555 | return std::make_unique<TernaryExpression>(-1, std::move(test), std::move(ifTrue), |
| 556 | std::move(ifFalse)); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 557 | } |
| 558 | case Rehydrator::kVariableReference_Command: { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 559 | const Variable* var = this->symbolRef<Variable>(Symbol::Kind::kVariable); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 560 | VariableReference::RefKind refKind = (VariableReference::RefKind) this->readU8(); |
Brian Osman | 79457ef | 2020-09-24 15:01:27 -0400 | [diff] [blame] | 561 | return std::make_unique<VariableReference>(-1, var, refKind); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 562 | } |
| 563 | case Rehydrator::kVoid_Command: |
| 564 | return nullptr; |
| 565 | default: |
| 566 | printf("unsupported expression %d\n", kind); |
| 567 | SkASSERT(false); |
| 568 | return nullptr; |
| 569 | } |
| 570 | } |
| 571 | |
Brian Osman | 1313d1a | 2020-09-08 10:34:30 -0400 | [diff] [blame] | 572 | std::shared_ptr<SymbolTable> Rehydrator::symbolTable(bool inherit) { |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 573 | int command = this->readU8(); |
| 574 | if (command == kVoid_Command) { |
| 575 | return nullptr; |
| 576 | } |
| 577 | SkASSERT(command == kSymbolTable_Command); |
| 578 | uint16_t ownedCount = this->readU16(); |
Brian Osman | 1313d1a | 2020-09-08 10:34:30 -0400 | [diff] [blame] | 579 | std::shared_ptr<SymbolTable> oldTable = fSymbolTable; |
John Stiles | 7c3515b | 2020-10-16 18:38:39 -0400 | [diff] [blame] | 580 | std::shared_ptr<SymbolTable> result = |
| 581 | inherit ? std::make_shared<SymbolTable>(fSymbolTable, /*builtin=*/true) |
| 582 | : std::make_shared<SymbolTable>(fErrors, /*builtin=*/true); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 583 | fSymbolTable = result; |
Brian Osman | 5bf3e20 | 2020-10-13 10:34:18 -0400 | [diff] [blame] | 584 | std::vector<const Symbol*> ownedSymbols; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 585 | ownedSymbols.reserve(ownedCount); |
| 586 | for (int i = 0; i < ownedCount; ++i) { |
| 587 | ownedSymbols.push_back(this->symbol()); |
| 588 | } |
| 589 | uint16_t symbolCount = this->readU16(); |
| 590 | std::vector<std::pair<StringFragment, int>> symbols; |
| 591 | symbols.reserve(symbolCount); |
| 592 | for (int i = 0; i < symbolCount; ++i) { |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 593 | int index = this->readU16(); |
John Stiles | b8cc665 | 2020-10-08 09:12:07 -0400 | [diff] [blame] | 594 | fSymbolTable->addWithoutOwnership(ownedSymbols[index]); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 595 | } |
Brian Osman | 1313d1a | 2020-09-08 10:34:30 -0400 | [diff] [blame] | 596 | fSymbolTable = oldTable; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 597 | return result; |
| 598 | } |
| 599 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 600 | } // namespace SkSL |