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