blob: 2ed2b796b131d8ab5be80edceb08a40fac32d7e3 [file] [log] [blame]
ethannicholasb3058bd2016-07-01 08:22:01 -07001/*
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 Nicholas0df1b042017-03-31 13:56:23 -04007
ethannicholasb3058bd2016-07-01 08:22:01 -07008#ifndef SKSL_FLOATLITERAL
9#define SKSL_FLOATLITERAL
10
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/sksl/SkSLContext.h"
12#include "src/sksl/ir/SkSLExpression.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070013
14namespace SkSL {
15
16/**
17 * A literal floating point number.
18 */
19struct FloatLiteral : public Expression {
Ethan Nicholas00543112018-07-31 09:44:36 -040020 FloatLiteral(const Context& context, int offset, double value)
Ethan Nicholase1f55022019-02-05 17:17:40 -050021 : INHERITED(offset, kFloatLiteral_Kind, *context.fFloatLiteral_Type)
Ethan Nicholas00543112018-07-31 09:44:36 -040022 , fValue(value) {}
23
24 FloatLiteral(int offset, double value, const Type* type)
25 : INHERITED(offset, kFloatLiteral_Kind, *type)
ethannicholasb3058bd2016-07-01 08:22:01 -070026 , fValue(value) {}
27
Ethan Nicholascb670962017-04-20 19:31:52 -040028 String description() const override {
ethannicholasb3058bd2016-07-01 08:22:01 -070029 return to_string(fValue);
30 }
31
Ethan Nicholascb670962017-04-20 19:31:52 -040032 bool hasSideEffects() const override {
33 return false;
34 }
35
ethannicholasb3058bd2016-07-01 08:22:01 -070036 bool isConstant() const override {
ethannicholasf789b382016-08-03 12:43:36 -070037 return true;
ethannicholasb3058bd2016-07-01 08:22:01 -070038 }
39
Ethan Nicholase1f55022019-02-05 17:17:40 -050040 int coercionCost(const Type& target) const override {
41 if (target.isFloat()) {
42 return 0;
43 }
44 return INHERITED::coercionCost(target);
45 }
46
Ethan Nicholas3deaeb22017-04-25 14:42:11 -040047 bool compareConstant(const Context& context, const Expression& other) const override {
48 FloatLiteral& f = (FloatLiteral&) other;
49 return fValue == f.fValue;
50 }
51
Ethan Nicholas8f6c2ab2018-01-17 13:51:52 -050052 double getConstantFloat() const override {
53 return fValue;
54 }
55
Ethan Nicholas00543112018-07-31 09:44:36 -040056 std::unique_ptr<Expression> clone() const override {
57 return std::unique_ptr<Expression>(new FloatLiteral(fOffset, fValue, &fType));
58 }
59
ethannicholasb3058bd2016-07-01 08:22:01 -070060 const double fValue;
61
62 typedef Expression INHERITED;
63};
64
65} // namespace
66
67#endif