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