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