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_INTLITERAL
9#define SKSL_INTLITERAL
10
11#include "src/sksl/SkSLContext.h"
12#include "src/sksl/ir/SkSLExpression.h"
13
14namespace SkSL {
15
16/**
17 * A literal integer.
18 */
19struct IntLiteral : public Expression {
20 // FIXME: we will need to revisit this if/when we add full support for both signed and unsigned
21 // 64-bit integers, but for right now an int64_t will hold every value we care about
22 IntLiteral(const Context& context, int offset, int64_t value)
23 : INHERITED(offset, kIntLiteral_Kind, *context.fInt_Type)
24 , fValue(value) {}
25
26 IntLiteral(int offset, int64_t value, const Type* type = nullptr)
27 : INHERITED(offset, kIntLiteral_Kind, *type)
28 , fValue(value) {}
29
30 String description() const override {
31 return to_string(fValue);
32 }
33
34 bool hasProperty(Property property) const override {
35 return false;
36 }
37
38 bool isCompileTimeConstant() const override {
39 return true;
40 }
41
42 bool compareConstant(const Context& context, const Expression& other) const override {
43 IntLiteral& i = (IntLiteral&) other;
44 return fValue == i.fValue;
45 }
46
47 int coercionCost(const Type& target) const override {
48 if (target.isSigned() || target.isUnsigned() || target.isFloat() ||
49 target.kind() == Type::kEnum_Kind) {
50 return 0;
51 }
52 return INHERITED::coercionCost(target);
53 }
54
55 int64_t getConstantInt() const override {
56 return fValue;
57 }
58
59 int nodeCount() const override {
60 return 1;
61 }
62
63 std::unique_ptr<Expression> clone() const override {
64 return std::unique_ptr<Expression>(new IntLiteral(fOffset, fValue, &fType));
65 }
66
67 const int64_t fValue;
68
69 typedef Expression INHERITED;
70};
71
72} // namespace SkSL
73
74#endif
75