• 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 "include/private/SkSLString.h"
13 #include "src/sksl/ir/SkSLExpression.h"
14 
15 #include <cinttypes>
16 
17 namespace SkSL {
18 
19 /**
20  * A single case of a 'switch' statement.
21  */
22 class SwitchCase final : public Statement {
23 public:
24     inline static constexpr Kind kIRNodeKind = Kind::kSwitchCase;
25 
Make(Position pos,SKSL_INT value,std::unique_ptr<Statement> statement)26     static std::unique_ptr<SwitchCase> Make(Position pos, SKSL_INT value,
27             std::unique_ptr<Statement> statement) {
28         return std::unique_ptr<SwitchCase>(new SwitchCase(pos, /*isDefault=*/false, value,
29                 std::move(statement)));
30     }
31 
MakeDefault(Position pos,std::unique_ptr<Statement> statement)32     static std::unique_ptr<SwitchCase> MakeDefault(Position pos,
33             std::unique_ptr<Statement> statement) {
34         return std::unique_ptr<SwitchCase>(new SwitchCase(pos, /*isDefault=*/true, -1,
35                 std::move(statement)));
36     }
37 
isDefault()38     bool isDefault() const {
39         return fDefault;
40     }
41 
value()42     SKSL_INT value() const {
43         SkASSERT(!this->isDefault());
44         return fValue;
45     }
46 
statement()47     std::unique_ptr<Statement>& statement() {
48         return fStatement;
49     }
50 
statement()51     const std::unique_ptr<Statement>& statement() const {
52         return fStatement;
53     }
54 
clone()55     std::unique_ptr<Statement> clone() const override {
56         return fDefault ? SwitchCase::MakeDefault(fPosition, this->statement()->clone())
57                         : SwitchCase::Make(fPosition, this->value(), this->statement()->clone());
58     }
59 
description()60     std::string description() const override {
61         if (this->isDefault()) {
62             return String::printf("default:\n%s", fStatement->description().c_str());
63         } else {
64             return String::printf("case %" PRId64 ":\n%s",
65                                   (int64_t) this->value(),
66                                   fStatement->description().c_str());
67         }
68     }
69 
70 private:
SwitchCase(Position pos,bool isDefault,SKSL_INT value,std::unique_ptr<Statement> statement)71     SwitchCase(Position pos, bool isDefault, SKSL_INT value, std::unique_ptr<Statement> statement)
72         : INHERITED(pos, kIRNodeKind)
73         , fDefault(isDefault)
74         , fValue(std::move(value))
75         , fStatement(std::move(statement)) {}
76 
77     bool fDefault;
78     SKSL_INT fValue;
79     std::unique_ptr<Statement> fStatement;
80 
81     using INHERITED = Statement;
82 };
83 
84 }  // namespace SkSL
85 
86 #endif
87