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 
14 namespace SkSL {
15 
16 /**
17  * A literal integer.
18  */
19 struct 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
IntLiteralIntLiteral22     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 
descriptionIntLiteral30     String description() const override {
31         return to_string(fValue);
32     }
33 
hasSideEffectsIntLiteral34     bool hasSideEffects() const override {
35         return false;
36     }
37 
isConstantIntLiteral38     bool isConstant() const override {
39         return true;
40     }
41 
compareConstantIntLiteral42     bool compareConstant(const Context& context, const Expression& other) const override {
43         IntLiteral& i = (IntLiteral&) other;
44         return fValue == i.fValue;
45     }
46 
coercionCostIntLiteral47     int coercionCost(const Type& target) const override {
48         if (target.isSigned() || target.isUnsigned() || target.isFloat()) {
49             return 0;
50         }
51         return INHERITED::coercionCost(target);
52     }
53 
getConstantIntIntLiteral54     int64_t getConstantInt() const override {
55         return fValue;
56     }
57 
cloneIntLiteral58     std::unique_ptr<Expression> clone() const override {
59         return std::unique_ptr<Expression>(new IntLiteral(fOffset, fValue, &fType));
60     }
61 
62     const int64_t fValue;
63 
64     typedef Expression INHERITED;
65 };
66 
67 } // namespace
68 
69 #endif
70