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