SkSLIntLiteral.h revision c576e93d174f3106e072a2f506bca3990b541265
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 "SkSLContext.h"
12#include "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, const Type* type = nullptr)
23    : INHERITED(offset, kIntLiteral_Kind, type ? *type : *context.fInt_Type)
24    , fValue(value) {}
25
26    String description() const override {
27        return to_string(fValue);
28    }
29
30    bool hasSideEffects() const override {
31        return false;
32    }
33
34    bool isConstant() const override {
35        return true;
36    }
37
38    bool compareConstant(const Context& context, const Expression& other) const override {
39        IntLiteral& i = (IntLiteral&) other;
40        return fValue == i.fValue;
41    }
42
43    int coercionCost(const Type& target) const override {
44        if (target.isUnsigned()) {
45            return 0;
46        }
47        return INHERITED::coercionCost(target);
48    }
49
50    const int64_t fValue;
51
52    typedef Expression INHERITED;
53};
54
55} // namespace
56
57#endif
58