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_TERNARYEXPRESSION 9 #define SKSL_TERNARYEXPRESSION 10 11 #include "src/sksl/SkSLPosition.h" 12 #include "src/sksl/ir/SkSLExpression.h" 13 14 namespace SkSL { 15 16 /** 17 * A ternary expression (test ? ifTrue : ifFalse). 18 */ 19 class TernaryExpression final : public Expression { 20 public: 21 static constexpr Kind kExpressionKind = Kind::kTernary; 22 TernaryExpression(int offset,std::unique_ptr<Expression> test,std::unique_ptr<Expression> ifTrue,std::unique_ptr<Expression> ifFalse)23 TernaryExpression(int offset, std::unique_ptr<Expression> test, 24 std::unique_ptr<Expression> ifTrue, std::unique_ptr<Expression> ifFalse) 25 : INHERITED(offset, kExpressionKind, &ifTrue->type()) 26 , fTest(std::move(test)) 27 , fIfTrue(std::move(ifTrue)) 28 , fIfFalse(std::move(ifFalse)) { 29 SkASSERT(this->ifTrue()->type() == this->ifFalse()->type()); 30 } 31 test()32 std::unique_ptr<Expression>& test() { 33 return fTest; 34 } 35 test()36 const std::unique_ptr<Expression>& test() const { 37 return fTest; 38 } 39 ifTrue()40 std::unique_ptr<Expression>& ifTrue() { 41 return fIfTrue; 42 } 43 ifTrue()44 const std::unique_ptr<Expression>& ifTrue() const { 45 return fIfTrue; 46 } 47 ifFalse()48 std::unique_ptr<Expression>& ifFalse() { 49 return fIfFalse; 50 } 51 ifFalse()52 const std::unique_ptr<Expression>& ifFalse() const { 53 return fIfFalse; 54 } 55 hasProperty(Property property)56 bool hasProperty(Property property) const override { 57 return this->test()->hasProperty(property) || this->ifTrue()->hasProperty(property) || 58 this->ifFalse()->hasProperty(property); 59 } 60 isConstantOrUniform()61 bool isConstantOrUniform() const override { 62 return this->test()->isConstantOrUniform() && this->ifTrue()->isConstantOrUniform() && 63 this->ifFalse()->isConstantOrUniform(); 64 } 65 clone()66 std::unique_ptr<Expression> clone() const override { 67 return std::unique_ptr<Expression>(new TernaryExpression(fOffset, this->test()->clone(), 68 this->ifTrue()->clone(), 69 this->ifFalse()->clone())); 70 } 71 description()72 String description() const override { 73 return "(" + this->test()->description() + " ? " + this->ifTrue()->description() + " : " + 74 this->ifFalse()->description() + ")"; 75 } 76 77 private: 78 std::unique_ptr<Expression> fTest; 79 std::unique_ptr<Expression> fIfTrue; 80 std::unique_ptr<Expression> fIfFalse; 81 82 using INHERITED = Expression; 83 }; 84 85 } // namespace SkSL 86 87 #endif 88