ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 7 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 8 | #ifndef SKSL_CONSTRUCTOR |
| 9 | #define SKSL_CONSTRUCTOR |
| 10 | |
| 11 | #include "SkSLExpression.h" |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame^] | 12 | #include "SkSLFloatLiteral.h" |
| 13 | #include "SkSLIntLiteral.h" |
| 14 | #include "SkSLIRGenerator.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 15 | |
| 16 | namespace SkSL { |
| 17 | |
| 18 | /** |
| 19 | * Represents the construction of a compound type, such as "vec2(x, y)". |
| 20 | */ |
| 21 | struct Constructor : public Expression { |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 22 | Constructor(Position position, const Type& type, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 23 | std::vector<std::unique_ptr<Expression>> arguments) |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 24 | : INHERITED(position, kConstructor_Kind, type) |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 25 | , fArguments(std::move(arguments)) {} |
| 26 | |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame^] | 27 | virtual std::unique_ptr<Expression> constantPropagate( |
| 28 | const IRGenerator& irGenerator, |
| 29 | const DefinitionMap& definitions) override { |
| 30 | if (fArguments.size() == 1 && fArguments[0]->fKind == Expression::kIntLiteral_Kind && |
| 31 | // promote float(1) to 1.0 |
| 32 | fType == *irGenerator.fContext.fFloat_Type) { |
| 33 | int64_t intValue = ((IntLiteral&) *fArguments[0]).fValue; |
| 34 | return std::unique_ptr<Expression>(new FloatLiteral(irGenerator.fContext, |
| 35 | fPosition, |
| 36 | intValue)); |
| 37 | } |
| 38 | return nullptr; |
| 39 | } |
| 40 | |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 41 | SkString description() const override { |
| 42 | SkString result = fType.description() + "("; |
| 43 | SkString separator; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 44 | for (size_t i = 0; i < fArguments.size(); i++) { |
| 45 | result += separator; |
| 46 | result += fArguments[i]->description(); |
| 47 | separator = ", "; |
| 48 | } |
| 49 | result += ")"; |
| 50 | return result; |
| 51 | } |
| 52 | |
| 53 | bool isConstant() const override { |
| 54 | for (size_t i = 0; i < fArguments.size(); i++) { |
| 55 | if (!fArguments[i]->isConstant()) { |
| 56 | return false; |
| 57 | } |
| 58 | } |
| 59 | return true; |
| 60 | } |
| 61 | |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame^] | 62 | std::vector<std::unique_ptr<Expression>> fArguments; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 63 | |
| 64 | typedef Expression INHERITED; |
| 65 | }; |
| 66 | |
| 67 | } // namespace |
| 68 | |
| 69 | #endif |