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