Flutter Engine
The Flutter Engine
SkSLExpression.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_EXPRESSION
9#define SKSL_EXPRESSION
10
15
16#include <cstdint>
17#include <memory>
18#include <optional>
19#include <string>
20
21namespace SkSL {
22
23class AnyConstructor;
24class Context;
25enum class OperatorPrecedence : uint8_t;
26
27/**
28 * Abstract supertype of all expressions.
29 */
30class Expression : public IRNode {
31public:
33
35 : INHERITED(pos, (int) kind)
36 , fType(type) {
37 SkASSERT(kind >= Kind::kFirst && kind <= Kind::kLast);
38 }
39
40 Kind kind() const {
41 return (Kind)fKind;
42 }
43
44 const Type& type() const {
45 return *fType;
46 }
47
48 bool isAnyConstructor() const {
49 static_assert((int)Kind::kConstructorArray - 1 == (int)Kind::kChildCall);
50 static_assert((int)Kind::kConstructorStruct + 1 == (int)Kind::kEmpty);
51 return this->kind() >= Kind::kConstructorArray && this->kind() <= Kind::kConstructorStruct;
52 }
53
54 bool isIntLiteral() const {
55 return this->kind() == Kind::kLiteral && this->type().isInteger();
56 }
57
58 bool isFloatLiteral() const {
59 return this->kind() == Kind::kLiteral && this->type().isFloat();
60 }
61
62 bool isBoolLiteral() const {
63 return this->kind() == Kind::kLiteral && this->type().isBoolean();
64 }
65
67 const AnyConstructor& asAnyConstructor() const;
68
69 /**
70 * Returns true if this expression is incomplete. Specifically, dangling function/method-call
71 * references that were never invoked, or type references that were never constructed, are
72 * considered incomplete expressions and should result in an error.
73 */
74 bool isIncomplete(const Context& context) const;
75
76 /**
77 * Compares this constant expression against another constant expression. Returns kUnknown if
78 * we aren't able to deduce a result (an expression isn't actually constant, the types are
79 * mismatched, etc).
80 */
81 enum class ComparisonResult {
82 kUnknown = -1,
83 kNotEqual,
84 kEqual
85 };
86 virtual ComparisonResult compareConstant(const Expression& other) const {
88 }
89
91 return this->type().coercionCost(target);
92 }
93
94 /**
95 * Returns true if this expression type supports `getConstantValue`. (This particular expression
96 * may or may not actually contain a constant value.) It's harmless to call `getConstantValue`
97 * on expressions which don't support constant values or don't contain any constant values, but
98 * if `supportsConstantValues` returns false, you can assume that `getConstantValue` will return
99 * nullopt for every slot of this expression. This allows for early-out opportunities in some
100 * cases. (Some expressions have tons of slots but never hold a constant value; e.g. a variable
101 * holding a very large array.)
102 */
103 virtual bool supportsConstantValues() const {
104 return false;
105 }
106
107 /**
108 * Returns the n'th compile-time constant value within a literal or constructor.
109 * Use Type::slotCount to determine the number of slots within an expression.
110 * Slots which do not contain compile-time constant values will return nullopt.
111 * `vec4(1, vec2(2), 3)` contains four compile-time constants: (1, 2, 2, 3)
112 * `mat2(f)` contains four slots, and two are constant: (nullopt, 0,
113 * 0, nullopt)
114 * All classes which override this function must also implement `supportsConstantValues`.
115 */
116 virtual std::optional<double> getConstantValue(int n) const {
118 return std::nullopt;
119 }
120
121 virtual std::unique_ptr<Expression> clone(Position pos) const = 0;
122
123 /**
124 * Returns a clone at the same position.
125 */
126 std::unique_ptr<Expression> clone() const { return this->clone(fPosition); }
127
128 /**
129 * Returns a description of the expression.
130 */
131 std::string description() const final;
132 virtual std::string description(OperatorPrecedence parentPrecedence) const = 0;
133
134
135private:
136 const Type* fType;
137
138 using INHERITED = IRNode;
139};
140
141} // namespace SkSL
142
143#endif
SkPoint pos
#define SkASSERT(cond)
Definition: SkAssert.h:116
virtual ComparisonResult compareConstant(const Expression &other) const
Kind kind() const
std::unique_ptr< Expression > clone() const
virtual bool supportsConstantValues() const
bool isIntLiteral() const
bool isIncomplete(const Context &context) const
Expression(Position pos, Kind kind, const Type *type)
virtual std::unique_ptr< Expression > clone(Position pos) const =0
CoercionCost coercionCost(const Type &target) const
const Type & type() const
bool isBoolLiteral() const
virtual std::optional< double > getConstantValue(int n) const
bool isFloatLiteral() const
std::string description() const final
AnyConstructor & asAnyConstructor()
bool isAnyConstructor() const
Position fPosition
Definition: SkSLIRNode.h:109
CoercionCost coercionCost(const Type &other) const
bool isBoolean() const
Definition: SkSLType.h:297
bool isFloat() const
Definition: SkSLType.h:318
bool isInteger() const
Definition: SkSLType.h:339
uint32_t * target
OperatorPrecedence
Definition: SkSLOperator.h:57
ExpressionKind
Definition: SkSLIRNode.h:62
Definition: ref_ptr.h:256