ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 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 | */ |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 7 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 8 | #ifndef SKSL_PREFIXEXPRESSION |
| 9 | #define SKSL_PREFIXEXPRESSION |
| 10 | |
| 11 | #include "SkSLExpression.h" |
Hal Canary | ce78bad | 2017-05-04 14:15:40 -0400 | [diff] [blame] | 12 | #include "SkSLFloatLiteral.h" |
| 13 | #include "SkSLIRGenerator.h" |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 14 | #include "SkSLLexer.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 15 | |
| 16 | namespace SkSL { |
| 17 | |
| 18 | /** |
| 19 | * An expression modified by a unary operator appearing before it, such as '!flag'. |
| 20 | */ |
| 21 | struct PrefixExpression : public Expression { |
| 22 | PrefixExpression(Token::Kind op, std::unique_ptr<Expression> operand) |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 23 | : INHERITED(operand->fOffset, kPrefix_Kind, operand->fType) |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 24 | , fOperand(std::move(operand)) |
| 25 | , fOperator(op) {} |
| 26 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 27 | bool isConstant() const override { |
| 28 | return fOperator == Token::MINUS && fOperand->isConstant(); |
| 29 | } |
| 30 | |
| 31 | bool hasSideEffects() const override { |
| 32 | return fOperator == Token::PLUSPLUS || fOperator == Token::MINUSMINUS || |
| 33 | fOperand->hasSideEffects(); |
| 34 | } |
| 35 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 36 | std::unique_ptr<Expression> constantPropagate(const IRGenerator& irGenerator, |
| 37 | const DefinitionMap& definitions) override { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 38 | if (fOperand->fKind == Expression::kFloatLiteral_Kind) { |
| 39 | return std::unique_ptr<Expression>(new FloatLiteral( |
| 40 | irGenerator.fContext, |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 41 | fOffset, |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 42 | -((FloatLiteral&) *fOperand).fValue)); |
| 43 | |
| 44 | } |
| 45 | return nullptr; |
| 46 | } |
| 47 | |
| 48 | String description() const override { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 49 | return Compiler::OperatorName(fOperator) + fOperand->description(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 52 | std::unique_ptr<Expression> fOperand; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 53 | const Token::Kind fOperator; |
| 54 | |
| 55 | typedef Expression INHERITED; |
| 56 | }; |
| 57 | |
| 58 | } // namespace |
| 59 | |
| 60 | #endif |