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  */
7 
8 #ifndef SKSL_FLOATLITERAL
9 #define SKSL_FLOATLITERAL
10 
11 #include "src/sksl/SkSLContext.h"
12 #include "src/sksl/ir/SkSLExpression.h"
13 
14 namespace SkSL {
15 
16 /**
17  * A literal floating point number. These are generally referred to as FloatLiteral, but
18  * Literal<SKSL_FLOAT> is also available for use with template code.
19  */
20 template <typename T> class Literal;
21 using FloatLiteral = Literal<SKSL_FLOAT>;
22 
23 template <>
24 class Literal<SKSL_FLOAT> final : public Expression {
25 public:
26     static constexpr Kind kExpressionKind = Kind::kFloatLiteral;
27 
Literal(const Context & context,int offset,float value)28     Literal(const Context& context, int offset, float value)
29         : INHERITED(offset, kExpressionKind, context.fFloatLiteral_Type.get())
30         , fValue(value) {}
31 
Literal(int offset,float value,const Type * type)32     Literal(int offset, float value, const Type* type)
33         : INHERITED(offset, kExpressionKind, type)
34         , fValue(value) {}
35 
value()36     float value() const {
37         return fValue;
38     }
39 
description()40     String description() const override {
41         return to_string(this->value());
42     }
43 
hasProperty(Property property)44     bool hasProperty(Property property) const override {
45         return false;
46     }
47 
isCompileTimeConstant()48     bool isCompileTimeConstant() const override {
49         return true;
50     }
51 
coercionCost(const Type & target)52     CoercionCost coercionCost(const Type& target) const override {
53         if (target.isFloat()) {
54             return CoercionCost::Free();
55         }
56         return INHERITED::coercionCost(target);
57     }
58 
compareConstant(const Context & context,const Expression & other)59     bool compareConstant(const Context& context, const Expression& other) const override {
60         return this->value() == other.as<FloatLiteral>().value();
61     }
62 
getConstantFloat()63     SKSL_FLOAT getConstantFloat() const override {
64         return this->value();
65     }
66 
clone()67     std::unique_ptr<Expression> clone() const override {
68         return std::make_unique<FloatLiteral>(fOffset, this->value(), &this->type());
69     }
70 
71 private:
72     float fValue;
73 
74     using INHERITED = Expression;
75 };
76 
77 }  // namespace SkSL
78 
79 #endif
80