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_PREFIXEXPRESSION 9 #define SKSL_PREFIXEXPRESSION 10 11 #ifdef SKSL_EXT 12 #include "src/sksl/SkSLLexerExt.h" 13 #else 14 #include "src/sksl/SkSLLexer.h" 15 #endif 16 #include "src/sksl/SkSLOperators.h" 17 #include "src/sksl/ir/SkSLExpression.h" 18 19 #include <memory> 20 21 namespace SkSL { 22 23 /** 24 * An expression modified by a unary operator appearing before it, such as '!flag'. 25 */ 26 class PrefixExpression final : public Expression { 27 public: 28 inline static constexpr Kind kExpressionKind = Kind::kPrefix; 29 30 // Use PrefixExpression::Make to automatically simplify various prefix expression types. PrefixExpression(Operator op,std::unique_ptr<Expression> operand)31 PrefixExpression(Operator op, std::unique_ptr<Expression> operand) 32 : INHERITED(operand->fLine, kExpressionKind, &operand->type()) 33 , fOperator(op) 34 , fOperand(std::move(operand)) {} 35 36 // Creates an SkSL prefix expression; uses the ErrorReporter to report errors. 37 static std::unique_ptr<Expression> Convert(const Context& context, Operator op, 38 std::unique_ptr<Expression> base); 39 40 // Creates an SkSL prefix expression; reports errors via ASSERT. 41 static std::unique_ptr<Expression> Make(const Context& context, Operator op, 42 std::unique_ptr<Expression> base); 43 getOperator()44 Operator getOperator() const { 45 return fOperator; 46 } 47 operand()48 std::unique_ptr<Expression>& operand() { 49 return fOperand; 50 } 51 operand()52 const std::unique_ptr<Expression>& operand() const { 53 return fOperand; 54 } 55 hasProperty(Property property)56 bool hasProperty(Property property) const override { 57 if (property == Property::kSideEffects && 58 (this->getOperator().kind() == Token::Kind::TK_PLUSPLUS || 59 this->getOperator().kind() == Token::Kind::TK_MINUSMINUS)) { 60 return true; 61 } 62 return this->operand()->hasProperty(property); 63 } 64 clone()65 std::unique_ptr<Expression> clone() const override { 66 return std::make_unique<PrefixExpression>(this->getOperator(), this->operand()->clone()); 67 } 68 description()69 String description() const override { 70 return this->getOperator().operatorName() + this->operand()->description(); 71 } 72 73 private: 74 Operator fOperator; 75 std::unique_ptr<Expression> fOperand; 76 77 using INHERITED = Expression; 78 }; 79 80 } // namespace SkSL 81 82 #endif 83