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
14namespace SkSL {
15
16/**
17 * A literal floating point number.
18 */
19struct FloatLiteral : public Expression {
20 FloatLiteral(const Context& context, int offset, double value)
21 : INHERITED(offset, kFloatLiteral_Kind, *context.fFloatLiteral_Type)
22 , fValue(value) {}
23
24 FloatLiteral(int offset, double value, const Type* type)
25 : INHERITED(offset, kFloatLiteral_Kind, *type)
26 , fValue(value) {}
27
28#ifdef SK_DEBUG
29 String description() const override {
30 return to_string(fValue);
31 }
32#endif
33
34 bool hasProperty(Property property) const override {
35 return false;
36 }
37
38 bool isConstant() const override {
39 return true;
40 }
41
42 int coercionCost(const Type& target) const override {
43 if (target.isFloat()) {
44 return 0;
45 }
46 return INHERITED::coercionCost(target);
47 }
48
49 bool compareConstant(const Context& context, const Expression& other) const override {
50 FloatLiteral& f = (FloatLiteral&) other;
51 return fValue == f.fValue;
52 }
53
54 double getConstantFloat() const override {
55 return fValue;
56 }
57
58 std::unique_ptr<Expression> clone() const override {
59 return std::unique_ptr<Expression>(new FloatLiteral(fOffset, fValue, &fType));
60 }
61
62 const double fValue;
63
64 typedef Expression INHERITED;
65};
66
67} // namespace
68
69#endif
70