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
11#include "src/sksl/ir/SkSLType.h"
12#include "src/sksl/ir/SkSLVariable.h"
13
14#include <unordered_map>
15
16namespace SkSL {
17
18struct Expression;
19class IRGenerator;
20
21typedef std::unordered_map<const Variable*, std::unique_ptr<Expression>*> DefinitionMap;
22
23/**
24 * Abstract supertype of all expressions.
25 */
26struct Expression : public IRNode {
27 enum Kind {
28 kBinary_Kind,
29 kBoolLiteral_Kind,
30 kConstructor_Kind,
31 kExternalFunctionCall_Kind,
32 kExternalValue_Kind,
33 kIntLiteral_Kind,
34 kFieldAccess_Kind,
35 kFloatLiteral_Kind,
36 kFunctionReference_Kind,
37 kFunctionCall_Kind,
38 kIndex_Kind,
39 kNullLiteral_Kind,
40 kPrefix_Kind,
41 kPostfix_Kind,
42 kSetting_Kind,
43 kSwizzle_Kind,
44 kVariableReference_Kind,
45 kTernary_Kind,
46 kTypeReference_Kind,
47 kDefined_Kind
48 };
49
50 enum class Property {
51 kSideEffects,
52 kContainsRTAdjust
53 };
54
55 Expression(int offset, Kind kind, const Type& type)
56 : INHERITED(offset)
57 , fKind(kind)
58 , fType(std::move(type)) {}
59
60 /**
61 * Returns true if this expression is constant. compareConstant must be implemented for all
62 * constants!
63 */
64 virtual bool isConstant() const {
65 return false;
66 }
67
68 /**
69 * Compares this constant expression against another constant expression of the same type. It is
70 * an error to call this on non-constant expressions, or if the types of the expressions do not
71 * match.
72 */
73 virtual bool compareConstant(const Context& context, const Expression& other) const {
74 ABORT("cannot call compareConstant on this type");
75 }
76
77 /**
78 * For an expression which evaluates to a constant int, returns the value. Otherwise calls
79 * ABORT.
80 */
81 virtual int64_t getConstantInt() const {
82 ABORT("not a constant int");
83 }
84
85 /**
86 * For an expression which evaluates to a constant float, returns the value. Otherwise calls
87 * ABORT.
88 */
89 virtual double getConstantFloat() const {
90 ABORT("not a constant float");
91 }
92
93 virtual bool hasProperty(Property property) const = 0;
94
95 bool hasSideEffects() const {
96 return this->hasProperty(Property::kSideEffects);
97 }
98
99 bool containsRTAdjust() const {
100 return this->hasProperty(Property::kContainsRTAdjust);
101 }
102
103 /**
104 * Given a map of known constant variable values, substitute them in for references to those
105 * variables occurring in this expression and its subexpressions. Similar simplifications, such
106 * as folding a constant binary expression down to a single value, may also be performed.
107 * Returns a new expression which replaces this expression, or null if no replacements were
108 * made. If a new expression is returned, this expression is no longer valid.
109 */
110 virtual std::unique_ptr<Expression> constantPropagate(const IRGenerator& irGenerator,
111 const DefinitionMap& definitions) {
112 return nullptr;
113 }
114
115 virtual int coercionCost(const Type& target) const {
116 return fType.coercionCost(target);
117 }
118
119 /**
120 * For a literal vector expression, return the floating point value of the n'th vector
121 * component. It is an error to call this method on an expression which is not a literal vector.
122 */
123 virtual SKSL_FLOAT getFVecComponent(int n) const {
124 SkASSERT(false);
125 return 0;
126 }
127
128 /**
129 * For a literal vector expression, return the integer value of the n'th vector component. It is
130 * an error to call this method on an expression which is not a literal vector.
131 */
132 virtual SKSL_INT getIVecComponent(int n) const {
133 SkASSERT(false);
134 return 0;
135 }
136
137 /**
138 * For a literal matrix expression, return the floating point value of the component at
139 * [col][row]. It is an error to call this method on an expression which is not a literal
140 * matrix.
141 */
142 virtual SKSL_FLOAT getMatComponent(int col, int row) const {
143 SkASSERT(false);
144 return 0;
145 }
146
147 virtual std::unique_ptr<Expression> clone() const = 0;
148
149 const Kind fKind;
150 const Type& fType;
151
152 typedef IRNode INHERITED;
153};
154
155} // namespace
156
157#endif
158