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 String description() const override {
29 return to_string(fValue);
30 }
31
32 bool hasProperty(Property property) const override {
33 return false;
34 }
35
36 bool isCompileTimeConstant() const override {
37 return true;
38 }
39
40 int coercionCost(const Type& target) const override {
41 if (target.isFloat()) {
42 return 0;
43 }
44 return INHERITED::coercionCost(target);
45 }
46
47 bool compareConstant(const Context& context, const Expression& other) const override {
48 FloatLiteral& f = (FloatLiteral&) other;
49 return fValue == f.fValue;
50 }
51
52 double getConstantFloat() const override {
53 return fValue;
54 }
55
56 int nodeCount() const override {
57 return 1;
58 }
59
60 std::unique_ptr<Expression> clone() const override {
61 return std::unique_ptr<Expression>(new FloatLiteral(fOffset, fValue, &fType));
62 }
63
64 const double fValue;
65
66 typedef Expression INHERITED;
67};
68
69} // namespace SkSL
70
71#endif
72