• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017 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_SWITCHCASE
9 #define SKSL_SWITCHCASE
10 
11 #include "include/private/SkSLStatement.h"
12 #include "src/sksl/ir/SkSLExpression.h"
13 
14 namespace SkSL {
15 
16 /**
17  * A single case of a 'switch' statement.
18  */
19 class SwitchCase final : public Statement {
20 public:
21     inline static constexpr Kind kStatementKind = Kind::kSwitchCase;
22 
23     // null value implies "default" case
SwitchCase(int line,std::unique_ptr<Expression> value,std::unique_ptr<Statement> statement)24     SwitchCase(int line, std::unique_ptr<Expression> value, std::unique_ptr<Statement> statement)
25         : INHERITED(line, kStatementKind)
26         , fValue(std::move(value))
27         , fStatement(std::move(statement)) {}
28 
value()29     std::unique_ptr<Expression>& value() {
30         return fValue;
31     }
32 
value()33     const std::unique_ptr<Expression>& value() const {
34         return fValue;
35     }
36 
statement()37     std::unique_ptr<Statement>& statement() {
38         return fStatement;
39     }
40 
statement()41     const std::unique_ptr<Statement>& statement() const {
42         return fStatement;
43     }
44 
clone()45     std::unique_ptr<Statement> clone() const override {
46         return std::make_unique<SwitchCase>(fLine,
47                                             this->value() ? this->value()->clone() : nullptr,
48                                             this->statement()->clone());
49     }
50 
description()51     String description() const override {
52         if (this->value()) {
53             return String::printf("case %s:\n%s",
54                                   this->value()->description().c_str(),
55                                   fStatement->description().c_str());
56         } else {
57             return String::printf("default:\n%s", fStatement->description().c_str());
58         }
59     }
60 
61 private:
62     std::unique_ptr<Expression> fValue;
63     std::unique_ptr<Statement> fStatement;
64 
65     using INHERITED = Statement;
66 };
67 
68 }  // namespace SkSL
69 
70 #endif
71