| 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_SWITCHSTATEMENT |
| 9 |
#define SKSL_SWITCHSTATEMENT |
| 10 |
|
| 11 |
#include "src/sksl/SkSLDefines.h" |
| 12 |
#include "src/sksl/SkSLPosition.h" |
| 13 |
#include "src/sksl/ir/SkSLBlock.h" |
| 14 |
#include "src/sksl/ir/SkSLExpression.h" |
| 15 |
#include "src/sksl/ir/SkSLIRNode.h" |
| 16 |
#include "src/sksl/ir/SkSLStatement.h" |
| 17 |
|
| 18 |
#include <memory> |
| 19 |
#include <string> |
| 20 |
#include <utility> |
| 21 |
|
| 22 |
namespace SkSL { |
| 23 |
|
| 24 |
class Context; |
| 25 |
class SymbolTable; |
| 26 |
|
| 27 |
/** |
| 28 |
* A 'switch' statement. |
| 29 |
*/ |
| 30 |
class SwitchStatement final : public Statement { |
| 31 |
public: |
| 32 |
inline static constexpr Kind kIRNodeKind = Kind::kSwitch; |
| 33 |
|
| 34 |
SwitchStatement(Position pos, |
| 35 |
std::unique_ptr<Expression> value, |
| 36 |
std::unique_ptr<Statement> caseBlock) |
| 37 |
: INHERITED(pos, kIRNodeKind) |
| 38 |
, fValue(std::move(value)) |
| 39 |
, fCaseBlock(std::move(caseBlock)) {} |
| 40 |
|
| 41 |
// Create a `switch` statement with an array of case-values and case-statements. |
| 42 |
// Coerces case values to the proper type and reports an error if cases are duplicated. |
| 43 |
// Reports errors via the ErrorReporter. |
| 44 |
static std::unique_ptr<Statement> Convert(const Context& context, |
| 45 |
Position pos, |
| 46 |
std::unique_ptr<Expression> value, |
| 47 |
ExpressionArray caseValues, |
| 48 |
StatementArray caseStatements, |
| 49 |
std::unique_ptr<SymbolTable> symbolTable); |
| 50 |
|
| 51 |
// Create a `switch` statement with a Block containing only SwitchCases. The SwitchCase block |
| 52 |
// must already contain non-overlapping, correctly-typed case values. Reports errors via ASSERT. |
| 53 |
static std::unique_ptr<Statement> Make(const Context& context, |
| 54 |
Position pos, |
| 55 |
std::unique_ptr<Expression> value, |
| 56 |
std::unique_ptr<Statement> caseBlock); |
| 57 |
|
| 58 |
std::unique_ptr<Expression>& value() { |
| 59 |
return fValue; |
| 60 |
} |
| 61 |
|
| 62 |
const std::unique_ptr<Expression>& value() const { |
| 63 |
return fValue; |
| 64 |
} |
| 65 |
|
| 66 |
std::unique_ptr<Statement>& caseBlock() { |
| 67 |
return fCaseBlock; |
| 68 |
} |
| 69 |
|
| 70 |
const std::unique_ptr<Statement>& caseBlock() const { |
| 71 |
return fCaseBlock; |
| 72 |
} |
| 73 |
|
| 74 |
StatementArray& cases() { |
| 75 |
return fCaseBlock->as<Block>().children(); |
| 76 |
} |
| 77 |
|
| 78 |
const StatementArray& cases() const { |
| 79 |
return fCaseBlock->as<Block>().children(); |
| 80 |
} |
| 81 |
|
| 82 |
std::string description() const override; |
| 83 |
|
| 84 |
private: |
| 85 |
std::unique_ptr<Expression> fValue; |
| 86 |
std::unique_ptr<Statement> fCaseBlock; // must be a Block containing only SwitchCase statements |
| 87 |
|
| 88 |
using INHERITED = Statement; |
| 89 |
}; |
| 90 |
|
| 91 |
} // namespace SkSL |
| 92 |
|
| 93 |
#endif |