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 | */ |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 7 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 8 | #ifndef SKSL_FLOATLITERAL |
| 9 | #define SKSL_FLOATLITERAL |
| 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "src/sksl/SkSLContext.h" |
| 12 | #include "src/sksl/ir/SkSLExpression.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 13 | |
| 14 | namespace SkSL { |
| 15 | |
| 16 | /** |
| 17 | * A literal floating point number. |
| 18 | */ |
| 19 | struct FloatLiteral : public Expression { |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 20 | FloatLiteral(const Context& context, int offset, double value) |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 21 | : INHERITED(offset, kFloatLiteral_Kind, *context.fFloatLiteral_Type) |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 22 | , fValue(value) {} |
| 23 | |
| 24 | FloatLiteral(int offset, double value, const Type* type) |
| 25 | : INHERITED(offset, kFloatLiteral_Kind, *type) |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 26 | , fValue(value) {} |
| 27 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 28 | String description() const override { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 29 | return to_string(fValue); |
| 30 | } |
| 31 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 32 | bool hasSideEffects() const override { |
| 33 | return false; |
| 34 | } |
| 35 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 36 | bool isConstant() const override { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 37 | return true; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 40 | int coercionCost(const Type& target) const override { |
| 41 | if (target.isFloat()) { |
| 42 | return 0; |
| 43 | } |
| 44 | return INHERITED::coercionCost(target); |
| 45 | } |
| 46 | |
Ethan Nicholas | 3deaeb2 | 2017-04-25 14:42:11 -0400 | [diff] [blame] | 47 | bool compareConstant(const Context& context, const Expression& other) const override { |
| 48 | FloatLiteral& f = (FloatLiteral&) other; |
| 49 | return fValue == f.fValue; |
| 50 | } |
| 51 | |
Ethan Nicholas | 8f6c2ab | 2018-01-17 13:51:52 -0500 | [diff] [blame] | 52 | double getConstantFloat() const override { |
| 53 | return fValue; |
| 54 | } |
| 55 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 56 | std::unique_ptr<Expression> clone() const override { |
| 57 | return std::unique_ptr<Expression>(new FloatLiteral(fOffset, fValue, &fType)); |
| 58 | } |
| 59 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 60 | const double fValue; |
| 61 | |
| 62 | typedef Expression INHERITED; |
| 63 | }; |
| 64 | |
| 65 | } // namespace |
| 66 | |
| 67 | #endif |