• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/SkSLCompiler.h"
12 #include "src/sksl/SkSLIRGenerator.h"
13 #include "src/sksl/SkSLLexer.h"
14 #include "src/sksl/ir/SkSLExpression.h"
15 #include "src/sksl/ir/SkSLFloatLiteral.h"
16 
17 namespace SkSL {
18 
19 /**
20  * An expression modified by a unary operator appearing before it, such as '!flag'.
21  */
22 struct PrefixExpression : public Expression {
PrefixExpressionPrefixExpression23     PrefixExpression(Token::Kind op, std::unique_ptr<Expression> operand)
24     : INHERITED(operand->fOffset, kPrefix_Kind, operand->fType)
25     , fOperand(std::move(operand))
26     , fOperator(op) {}
27 
isConstantPrefixExpression28     bool isConstant() const override {
29         return fOperator == Token::MINUS && fOperand->isConstant();
30     }
31 
hasSideEffectsPrefixExpression32     bool hasSideEffects() const override {
33         return fOperator == Token::PLUSPLUS || fOperator == Token::MINUSMINUS ||
34                fOperand->hasSideEffects();
35     }
36 
constantPropagatePrefixExpression37     std::unique_ptr<Expression> constantPropagate(const IRGenerator& irGenerator,
38                                                   const DefinitionMap& definitions) override {
39         if (fOperand->fKind == Expression::kFloatLiteral_Kind) {
40             return std::unique_ptr<Expression>(new FloatLiteral(
41                                                               irGenerator.fContext,
42                                                               fOffset,
43                                                               -((FloatLiteral&) *fOperand).fValue));
44 
45         }
46         return nullptr;
47     }
48 
getFVecComponentPrefixExpression49     SKSL_FLOAT getFVecComponent(int index) const override {
50         SkASSERT(fOperator == Token::Kind::MINUS);
51         return -fOperand->getFVecComponent(index);
52     }
53 
getIVecComponentPrefixExpression54     SKSL_INT getIVecComponent(int index) const override {
55         SkASSERT(fOperator == Token::Kind::MINUS);
56         return -fOperand->getIVecComponent(index);
57     }
58 
getMatComponentPrefixExpression59     SKSL_FLOAT getMatComponent(int col, int row) const override {
60         SkASSERT(fOperator == Token::Kind::MINUS);
61         return -fOperand->getMatComponent(col, row);
62     }
63 
clonePrefixExpression64     std::unique_ptr<Expression> clone() const override {
65         return std::unique_ptr<Expression>(new PrefixExpression(fOperator, fOperand->clone()));
66     }
67 
descriptionPrefixExpression68     String description() const override {
69         return Compiler::OperatorName(fOperator) + fOperand->description();
70     }
71 
72     std::unique_ptr<Expression> fOperand;
73     const Token::Kind fOperator;
74 
75     typedef Expression INHERITED;
76 };
77 
78 } // namespace
79 
80 #endif
81