Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkSLLiteral.h
Go to the documentation of this file.
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
19
20#include <cstdint>
21#include <cinttypes>
22#include <memory>
23#include <optional>
24#include <string>
25
26namespace SkSL {
27
28enum class OperatorPrecedence : uint8_t;
29
30/**
31 * A literal value. These can contain ints, floats, or booleans.
32 */
33
34class Literal : public Expression {
35public:
36 inline static constexpr Kind kIRNodeKind = Kind::kLiteral;
37
38 Literal(Position pos, double value, const Type* type)
40 , fValue(value) {}
41
42 // Makes a literal of $floatLiteral type.
43 static std::unique_ptr<Literal> MakeFloat(const Context& context, Position pos, float value) {
44 return std::make_unique<Literal>(pos, value, context.fTypes.fFloatLiteral.get());
45 }
46
47 // Makes a float literal of the specified type.
48 static std::unique_ptr<Literal> MakeFloat(Position pos, float value, const Type* type) {
50 return std::make_unique<Literal>(pos, value, type);
51 }
52
53 // Makes a literal of $intLiteral type.
54 static std::unique_ptr<Literal> MakeInt(const Context& context, Position pos, SKSL_INT value) {
55 return std::make_unique<Literal>(pos, value, context.fTypes.fIntLiteral.get());
56 }
57
58 // Makes an int literal of the specified type.
59 static std::unique_ptr<Literal> MakeInt(Position pos, SKSL_INT value, const Type* type) {
61 SkASSERTF(value >= type->minimumValue(), "Value %" PRId64 " does not fit in type %s",
62 value, type->description().c_str());
63 SkASSERTF(value <= type->maximumValue(), "Value %" PRId64 " does not fit in type %s",
64 value, type->description().c_str());
65 return std::make_unique<Literal>(pos, value, type);
66 }
67
68 // Makes a literal of boolean type.
69 static std::unique_ptr<Literal> MakeBool(const Context& context, Position pos, bool value) {
70 return std::make_unique<Literal>(pos, value, context.fTypes.fBool.get());
71 }
72
73 // Makes a literal of boolean type. (Functionally identical to the above, but useful if you
74 // don't have access to the Context.)
75 static std::unique_ptr<Literal> MakeBool(Position pos, bool value, const Type* type) {
77 return std::make_unique<Literal>(pos, value, type);
78 }
79
80 // Makes a literal of the specified type, rounding as needed.
81 static std::unique_ptr<Literal> Make(Position pos, double value, const Type* type) {
82 if (type->isFloat()) {
83 return MakeFloat(pos, value, type);
84 }
85 if (type->isInteger()) {
86 return MakeInt(pos, value, type);
87 }
89 return MakeBool(pos, value, type);
90 }
91
92 float floatValue() const {
93 SkASSERT(this->type().isFloat());
94 return (SKSL_FLOAT)fValue;
95 }
96
98 SkASSERT(this->type().isInteger());
99 return (SKSL_INT)fValue;
100 }
101
103 SkASSERT(this->type().isBoolean());
104 return (bool)fValue;
105 }
106
107 double value() const {
108 return fValue;
109 }
110
111 std::string description(OperatorPrecedence) const override;
112
113 ComparisonResult compareConstant(const Expression& other) const override {
114 if (!other.is<Literal>() || this->type().numberKind() != other.type().numberKind()) {
116 }
117 return this->value() == other.as<Literal>().value()
120 }
121
122 std::unique_ptr<Expression> clone(Position pos) const override {
123 return std::make_unique<Literal>(pos, this->value(), &this->type());
124 }
125
126 bool supportsConstantValues() const override {
127 return true;
128 }
129
130 std::optional<double> getConstantValue(int n) const override {
131 SkASSERT(n == 0);
132 return fValue;
133 }
134
135private:
136 double fValue;
137
138 using INHERITED = Expression;
139};
140
141} // namespace SkSL
142
143#endif
SkPoint pos
#define SkASSERT(cond)
Definition SkAssert.h:116
#define SkASSERTF(cond, fmt,...)
Definition SkAssert.h:117
int64_t SKSL_INT
Definition SkSLDefines.h:16
float SKSL_FLOAT
Definition SkSLDefines.h:17
const std::unique_ptr< Type > fFloatLiteral
const std::unique_ptr< Type > fIntLiteral
const std::unique_ptr< Type > fBool
const BuiltinTypes & fTypes
Definition SkSLContext.h:30
virtual const Type & type() const
std::string description() const final
bool is() const
Definition SkSLIRNode.h:124
const T & as() const
Definition SkSLIRNode.h:133
static std::unique_ptr< Literal > MakeInt(const Context &context, Position pos, SKSL_INT value)
Definition SkSLLiteral.h:54
bool supportsConstantValues() const override
ComparisonResult compareConstant(const Expression &other) const override
static std::unique_ptr< Literal > MakeBool(const Context &context, Position pos, bool value)
Definition SkSLLiteral.h:69
Literal(Position pos, double value, const Type *type)
Definition SkSLLiteral.h:38
SKSL_INT boolValue() const
static std::unique_ptr< Literal > Make(Position pos, double value, const Type *type)
Definition SkSLLiteral.h:81
static std::unique_ptr< Literal > MakeFloat(const Context &context, Position pos, float value)
Definition SkSLLiteral.h:43
std::unique_ptr< Expression > clone(Position pos) const override
float floatValue() const
Definition SkSLLiteral.h:92
static std::unique_ptr< Literal > MakeInt(Position pos, SKSL_INT value, const Type *type)
Definition SkSLLiteral.h:59
SKSL_INT intValue() const
Definition SkSLLiteral.h:97
double value() const
static constexpr Kind kIRNodeKind
Definition SkSLLiteral.h:36
static std::unique_ptr< Literal > MakeFloat(Position pos, float value, const Type *type)
Definition SkSLLiteral.h:48
static std::unique_ptr< Literal > MakeBool(Position pos, bool value, const Type *type)
Definition SkSLLiteral.h:75
std::optional< double > getConstantValue(int n) const override
bool isBoolean() const
Definition SkSLType.h:297
virtual NumberKind numberKind() const
Definition SkSLType.h:290
std::string description() const override
Definition SkSLType.h:238
bool isFloat() const
Definition SkSLType.h:318
virtual double minimumValue() const
Definition SkSLType.h:444
bool isInteger() const
Definition SkSLType.h:339
OperatorPrecedence
ExpressionKind
Definition SkSLIRNode.h:62