Update rive-cpp to 2.0 version
[platform/core/uifw/rive-tizen.git] / submodule / skia / src / sksl / ir / SkSLIfStatement.cpp
1 /*
2  * Copyright 2021 Google LLC
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 #include "src/sksl/SkSLAnalysis.h"
9 #include "src/sksl/SkSLConstantFolder.h"
10 #include "src/sksl/SkSLContext.h"
11 #include "src/sksl/SkSLProgramSettings.h"
12 #include "src/sksl/ir/SkSLExpressionStatement.h"
13 #include "src/sksl/ir/SkSLIfStatement.h"
14 #include "src/sksl/ir/SkSLLiteral.h"
15 #include "src/sksl/ir/SkSLNop.h"
16 #include "src/sksl/ir/SkSLType.h"
17
18 namespace SkSL {
19
20 std::unique_ptr<Statement> IfStatement::clone() const {
21     return std::make_unique<IfStatement>(fPosition, this->isStatic(), this->test()->clone(),
22                                          this->ifTrue()->clone(),
23                                          this->ifFalse() ? this->ifFalse()->clone() : nullptr);
24 }
25
26 std::string IfStatement::description() const {
27     std::string result;
28     if (this->isStatic()) {
29         result += "@";
30     }
31     result += "if (" + this->test()->description() + ") " + this->ifTrue()->description();
32     if (this->ifFalse()) {
33         result += " else " + this->ifFalse()->description();
34     }
35     return result;
36 }
37
38 std::unique_ptr<Statement> IfStatement::Convert(const Context& context, Position pos,
39         bool isStatic, std::unique_ptr<Expression> test, std::unique_ptr<Statement> ifTrue,
40         std::unique_ptr<Statement> ifFalse) {
41     test = context.fTypes.fBool->coerceExpression(std::move(test), context);
42     if (!test) {
43         return nullptr;
44     }
45     SkASSERT(ifTrue);
46     if (Analysis::DetectVarDeclarationWithoutScope(*ifTrue, context.fErrors)) {
47         return nullptr;
48     }
49     if (ifFalse && Analysis::DetectVarDeclarationWithoutScope(*ifFalse, context.fErrors)) {
50         return nullptr;
51     }
52     return IfStatement::Make(context, pos, isStatic, std::move(test),
53                              std::move(ifTrue), std::move(ifFalse));
54 }
55
56 static std::unique_ptr<Statement> replace_empty_with_nop(std::unique_ptr<Statement> stmt,
57                                                          bool isEmpty) {
58     return (stmt && (!isEmpty || stmt->is<Nop>())) ? std::move(stmt)
59                                                    : Nop::Make();
60 }
61
62 std::unique_ptr<Statement> IfStatement::Make(const Context& context, Position pos, bool isStatic,
63         std::unique_ptr<Expression> test, std::unique_ptr<Statement> ifTrue,
64         std::unique_ptr<Statement> ifFalse) {
65     SkASSERT(test->type().matches(*context.fTypes.fBool));
66     SkASSERT(!Analysis::DetectVarDeclarationWithoutScope(*ifTrue));
67     SkASSERT(!ifFalse || !Analysis::DetectVarDeclarationWithoutScope(*ifFalse));
68
69     const bool optimize = context.fConfig->fSettings.fOptimize;
70     bool trueIsEmpty = false;
71     bool falseIsEmpty = false;
72
73     if (optimize) {
74         // If both sides are empty, the if statement can be reduced to its test expression.
75         trueIsEmpty = ifTrue->isEmpty();
76         falseIsEmpty = !ifFalse || ifFalse->isEmpty();
77         if (trueIsEmpty && falseIsEmpty) {
78             return ExpressionStatement::Make(context, std::move(test));
79         }
80     }
81
82     if (isStatic || optimize) {
83         // Static Boolean values can fold down to a single branch.
84         const Expression* testValue = ConstantFolder::GetConstantValueForVariable(*test);
85         if (testValue->isBoolLiteral()) {
86             if (testValue->as<Literal>().boolValue()) {
87                 return replace_empty_with_nop(std::move(ifTrue), trueIsEmpty);
88             } else {
89                 return replace_empty_with_nop(std::move(ifFalse), falseIsEmpty);
90             }
91         }
92     }
93
94     if (optimize) {
95         // Replace an empty if-true branches with Nop; eliminate empty if-false branches entirely.
96         ifTrue = replace_empty_with_nop(std::move(ifTrue), trueIsEmpty);
97         if (falseIsEmpty) {
98             ifFalse = nullptr;
99         }
100     }
101
102     return std::make_unique<IfStatement>(pos, isStatic, std::move(test),
103                                          std::move(ifTrue), std::move(ifFalse));
104 }
105
106 }  // namespace SkSL