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