1 /* 2 * Copyright 2020 Google LLC 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_DSL_EXPRESSION 9 #define SKSL_DSL_EXPRESSION 10 11 #include "include/core/SkTypes.h" 12 #include "include/private/SkTArray.h" 13 #include "include/sksl/DSLErrorHandling.h" 14 #include "include/sksl/DSLWrapper.h" 15 16 #include <cstdint> 17 #include <memory> 18 19 namespace SkSL { 20 21 class Expression; 22 class Type; 23 24 namespace dsl { 25 26 class DSLPossibleExpression; 27 class DSLStatement; 28 class DSLType; 29 class DSLVar; 30 31 /** 32 * Represents an expression such as 'cos(x)' or 'a + b'. 33 */ 34 class DSLExpression { 35 public: 36 DSLExpression(const DSLExpression&) = delete; 37 38 DSLExpression(DSLExpression&&); 39 40 DSLExpression(); 41 42 /** 43 * Creates an expression representing a literal float. 44 */ 45 DSLExpression(float value); 46 47 /** 48 * Creates an expression representing a literal float. 49 */ DSLExpression(double value)50 DSLExpression(double value) 51 : DSLExpression((float) value) {} 52 53 /** 54 * Creates an expression representing a literal int. 55 */ 56 DSLExpression(int value); 57 58 /** 59 * Creates an expression representing a literal uint. 60 */ 61 DSLExpression(unsigned int value); 62 63 /** 64 * Creates an expression representing a literal bool. 65 */ 66 DSLExpression(bool value); 67 68 /** 69 * Creates an expression representing a variable reference. 70 */ 71 DSLExpression(DSLVar& var); 72 73 DSLExpression(DSLVar&& var); 74 75 DSLExpression(DSLPossibleExpression expr, PositionInfo pos = PositionInfo()); 76 77 DSLExpression(std::unique_ptr<SkSL::Expression> expression); 78 79 ~DSLExpression(); 80 81 DSLType type(); 82 83 /** 84 * Overloads the '=' operator to create an SkSL assignment statement. 85 */ 86 DSLPossibleExpression operator=(DSLExpression other); 87 88 DSLExpression x(PositionInfo pos = PositionInfo()); 89 90 DSLExpression y(PositionInfo pos = PositionInfo()); 91 92 DSLExpression z(PositionInfo pos = PositionInfo()); 93 94 DSLExpression w(PositionInfo pos = PositionInfo()); 95 96 DSLExpression r(PositionInfo pos = PositionInfo()); 97 98 DSLExpression g(PositionInfo pos = PositionInfo()); 99 100 DSLExpression b(PositionInfo pos = PositionInfo()); 101 102 DSLExpression a(PositionInfo pos = PositionInfo()); 103 104 /** 105 * Creates an SkSL struct field access expression. 106 */ 107 DSLExpression field(const char* name, PositionInfo pos = PositionInfo()); 108 109 /** 110 * Creates an SkSL array index expression. 111 */ 112 DSLPossibleExpression operator[](DSLExpression index); 113 114 DSLPossibleExpression operator()(SkTArray<DSLWrapper<DSLExpression>> args); 115 116 /** 117 * Invalidates this object and returns the SkSL expression it represents. 118 */ 119 std::unique_ptr<SkSL::Expression> release(); 120 121 private: 122 void swap(DSLExpression& other); 123 124 /** 125 * Invalidates this object and returns the SkSL expression it represents coerced to the 126 * specified type. If the expression cannot be coerced, reports an error and returns null. 127 */ 128 std::unique_ptr<SkSL::Expression> coerceAndRelease(const SkSL::Type& type); 129 130 std::unique_ptr<SkSL::Expression> fExpression; 131 132 friend DSLExpression SampleChild(int index, DSLExpression coords); 133 134 friend class DSLCore; 135 friend class DSLFunction; 136 friend class DSLPossibleExpression; 137 friend class DSLVar; 138 friend class DSLWriter; 139 template<typename T> friend class DSLWrapper; 140 }; 141 142 DSLPossibleExpression operator+(DSLExpression left, DSLExpression right); 143 DSLPossibleExpression operator+(DSLExpression expr); 144 DSLPossibleExpression operator+=(DSLExpression left, DSLExpression right); 145 DSLPossibleExpression operator-(DSLExpression left, DSLExpression right); 146 DSLPossibleExpression operator-(DSLExpression expr); 147 DSLPossibleExpression operator-=(DSLExpression left, DSLExpression right); 148 DSLPossibleExpression operator*(DSLExpression left, DSLExpression right); 149 DSLPossibleExpression operator*=(DSLExpression left, DSLExpression right); 150 DSLPossibleExpression operator/(DSLExpression left, DSLExpression right); 151 DSLPossibleExpression operator/=(DSLExpression left, DSLExpression right); 152 DSLPossibleExpression operator%(DSLExpression left, DSLExpression right); 153 DSLPossibleExpression operator%=(DSLExpression left, DSLExpression right); 154 DSLPossibleExpression operator<<(DSLExpression left, DSLExpression right); 155 DSLPossibleExpression operator<<=(DSLExpression left, DSLExpression right); 156 DSLPossibleExpression operator>>(DSLExpression left, DSLExpression right); 157 DSLPossibleExpression operator>>=(DSLExpression left, DSLExpression right); 158 DSLPossibleExpression operator&&(DSLExpression left, DSLExpression right); 159 DSLPossibleExpression operator||(DSLExpression left, DSLExpression right); 160 DSLPossibleExpression operator&(DSLExpression left, DSLExpression right); 161 DSLPossibleExpression operator&=(DSLExpression left, DSLExpression right); 162 DSLPossibleExpression operator|(DSLExpression left, DSLExpression right); 163 DSLPossibleExpression operator|=(DSLExpression left, DSLExpression right); 164 DSLPossibleExpression operator^(DSLExpression left, DSLExpression right); 165 DSLPossibleExpression operator^=(DSLExpression left, DSLExpression right); 166 DSLPossibleExpression operator,(DSLExpression left, DSLExpression right); 167 DSLPossibleExpression operator,(DSLPossibleExpression left, DSLExpression right); 168 DSLPossibleExpression operator,(DSLExpression left, DSLPossibleExpression right); 169 DSLPossibleExpression operator,(DSLPossibleExpression left, DSLPossibleExpression right); 170 DSLPossibleExpression operator==(DSLExpression left, DSLExpression right); 171 DSLPossibleExpression operator!=(DSLExpression left, DSLExpression right); 172 DSLPossibleExpression operator>(DSLExpression left, DSLExpression right); 173 DSLPossibleExpression operator<(DSLExpression left, DSLExpression right); 174 DSLPossibleExpression operator>=(DSLExpression left, DSLExpression right); 175 DSLPossibleExpression operator<=(DSLExpression left, DSLExpression right); 176 DSLPossibleExpression operator!(DSLExpression expr); 177 DSLPossibleExpression operator~(DSLExpression expr); 178 DSLPossibleExpression operator++(DSLExpression expr); 179 DSLPossibleExpression operator++(DSLExpression expr, int); 180 DSLPossibleExpression operator--(DSLExpression expr); 181 DSLPossibleExpression operator--(DSLExpression expr, int); 182 183 /** 184 * Represents an Expression which may have failed and/or have pending errors to report. Converting a 185 * PossibleExpression into an Expression requires PositionInfo so that any pending errors can be 186 * reported at the correct position. 187 * 188 * PossibleExpression is used instead of Expression in situations where it is not possible to 189 * capture the PositionInfo at the time of Expression construction (notably in operator overloads, 190 * where we cannot add default parameters). 191 */ 192 class DSLPossibleExpression { 193 public: 194 DSLPossibleExpression(std::unique_ptr<SkSL::Expression> expression); 195 196 DSLPossibleExpression(DSLPossibleExpression&& other); 197 198 ~DSLPossibleExpression(); 199 200 DSLType type(); 201 202 DSLExpression x(PositionInfo pos = PositionInfo()); 203 204 DSLExpression y(PositionInfo pos = PositionInfo()); 205 206 DSLExpression z(PositionInfo pos = PositionInfo()); 207 208 DSLExpression w(PositionInfo pos = PositionInfo()); 209 210 DSLExpression r(PositionInfo pos = PositionInfo()); 211 212 DSLExpression g(PositionInfo pos = PositionInfo()); 213 214 DSLExpression b(PositionInfo pos = PositionInfo()); 215 216 DSLExpression a(PositionInfo pos = PositionInfo()); 217 218 DSLExpression field(const char* name, PositionInfo pos = PositionInfo()); 219 220 DSLPossibleExpression operator=(DSLExpression expr); 221 222 DSLPossibleExpression operator=(int expr); 223 224 DSLPossibleExpression operator=(float expr); 225 226 DSLPossibleExpression operator=(double expr); 227 228 DSLPossibleExpression operator[](DSLExpression index); 229 230 DSLPossibleExpression operator()(SkTArray<DSLWrapper<DSLExpression>> args); 231 232 DSLPossibleExpression operator++(); 233 234 DSLPossibleExpression operator++(int); 235 236 DSLPossibleExpression operator--(); 237 238 DSLPossibleExpression operator--(int); 239 240 std::unique_ptr<SkSL::Expression> release(); 241 242 private: 243 std::unique_ptr<SkSL::Expression> fExpression; 244 245 friend class DSLExpression; 246 }; 247 248 } // namespace dsl 249 250 } // namespace SkSL 251 252 #endif 253