blob: f435330e9c64ecc433fd911b3352fb6257fb566a [file] [log] [blame]
Austin Engcc2516a2023-10-17 20:57:54 +00001// Copyright 2021 The Dawn & Tint Authors
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002//
Austin Engcc2516a2023-10-17 20:57:54 +00003// Redistribution and use in source and binary forms, with or without
4// modification, are permitted provided that the following conditions are met:
Ryan Harrisondbc13af2022-02-21 15:19:07 +00005//
Austin Engcc2516a2023-10-17 20:57:54 +00006// 1. Redistributions of source code must retain the above copyright notice, this
7// list of conditions and the following disclaimer.
Ryan Harrisondbc13af2022-02-21 15:19:07 +00008//
Austin Engcc2516a2023-10-17 20:57:54 +00009// 2. Redistributions in binary form must reproduce the above copyright notice,
10// this list of conditions and the following disclaimer in the documentation
11// and/or other materials provided with the distribution.
12//
13// 3. Neither the name of the copyright holder nor the names of its
14// contributors may be used to endorse or promote products derived from
15// this software without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Ryan Harrisondbc13af2022-02-21 15:19:07 +000027
dan sinclaird3b13692023-07-20 01:14:15 +000028#ifndef SRC_TINT_LANG_WGSL_SEM_SWITCH_STATEMENT_H_
29#define SRC_TINT_LANG_WGSL_SEM_SWITCH_STATEMENT_H_
Ryan Harrisondbc13af2022-02-21 15:19:07 +000030
Ben Clayton43581f12022-05-20 12:28:00 +000031#include <vector>
32
dan sinclaird3b13692023-07-20 01:14:15 +000033#include "src/tint/lang/wgsl/sem/block_statement.h"
Ryan Harrisondbc13af2022-02-21 15:19:07 +000034
35// Forward declarations
dan sinclairc990b3c2022-04-07 16:04:35 +000036namespace tint::ast {
Ryan Harrisondbc13af2022-02-21 15:19:07 +000037class CaseStatement;
dan sinclairf148f082022-10-19 15:55:02 +000038class CaseSelector;
Ryan Harrisondbc13af2022-02-21 15:19:07 +000039class SwitchStatement;
dan sinclairc990b3c2022-04-07 16:04:35 +000040} // namespace tint::ast
dan sinclair464b3b82023-08-09 14:14:28 +000041namespace tint::core::constant {
dan sinclairb53b8cf2022-12-15 16:25:31 +000042class Value;
dan sinclair464b3b82023-08-09 14:14:28 +000043} // namespace tint::core::constant
Ben Clayton43581f12022-05-20 12:28:00 +000044namespace tint::sem {
45class CaseStatement;
dan sinclairf148f082022-10-19 15:55:02 +000046class CaseSelector;
Ben Clayton3fb9a3f2023-02-04 21:20:26 +000047class ValueExpression;
Ben Clayton43581f12022-05-20 12:28:00 +000048} // namespace tint::sem
Ryan Harrisondbc13af2022-02-21 15:19:07 +000049
dan sinclairc990b3c2022-04-07 16:04:35 +000050namespace tint::sem {
Ryan Harrisondbc13af2022-02-21 15:19:07 +000051
52/// Holds semantic information about an switch statement
dan sinclairbae54e72023-07-28 15:01:54 +000053class SwitchStatement final : public Castable<SwitchStatement, CompoundStatement> {
dan sinclair41e4d9a2022-05-01 14:40:55 +000054 public:
55 /// Constructor
56 /// @param declaration the AST node for this switch statement
57 /// @param parent the owning statement
58 /// @param function the owning function
59 SwitchStatement(const ast::SwitchStatement* declaration,
60 const CompoundStatement* parent,
61 const sem::Function* function);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000062
dan sinclair41e4d9a2022-05-01 14:40:55 +000063 /// Destructor
64 ~SwitchStatement() override;
Ryan Harrisondbc13af2022-02-21 15:19:07 +000065
dan sinclair41e4d9a2022-05-01 14:40:55 +000066 /// @return the AST node for this statement
67 const ast::SwitchStatement* Declaration() const;
Ben Clayton43581f12022-05-20 12:28:00 +000068
69 /// @returns the case statements for this switch
70 std::vector<const CaseStatement*>& Cases() { return cases_; }
71
72 /// @returns the case statements for this switch
73 const std::vector<const CaseStatement*>& Cases() const { return cases_; }
74
75 private:
76 std::vector<const CaseStatement*> cases_;
Ryan Harrisondbc13af2022-02-21 15:19:07 +000077};
78
79/// Holds semantic information about a switch case statement
dan sinclairbae54e72023-07-28 15:01:54 +000080class CaseStatement final : public Castable<CaseStatement, CompoundStatement> {
dan sinclair41e4d9a2022-05-01 14:40:55 +000081 public:
82 /// Constructor
83 /// @param declaration the AST node for this case statement
84 /// @param parent the owning statement
85 /// @param function the owning function
86 CaseStatement(const ast::CaseStatement* declaration,
87 const CompoundStatement* parent,
88 const sem::Function* function);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000089
dan sinclair41e4d9a2022-05-01 14:40:55 +000090 /// Destructor
91 ~CaseStatement() override;
Ryan Harrisondbc13af2022-02-21 15:19:07 +000092
dan sinclair41e4d9a2022-05-01 14:40:55 +000093 /// @return the AST node for this statement
94 const ast::CaseStatement* Declaration() const;
Ryan Harrisondbc13af2022-02-21 15:19:07 +000095
dan sinclair41e4d9a2022-05-01 14:40:55 +000096 /// @param body the case body block statement
97 void SetBlock(const BlockStatement* body) { body_ = body; }
Ryan Harrisondbc13af2022-02-21 15:19:07 +000098
dan sinclair41e4d9a2022-05-01 14:40:55 +000099 /// @returns the case body block statement
100 const BlockStatement* Body() const { return body_; }
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000101
Ben Clayton43581f12022-05-20 12:28:00 +0000102 /// @returns the selectors for the case
dan sinclairf148f082022-10-19 15:55:02 +0000103 std::vector<const CaseSelector*>& Selectors() { return selectors_; }
Ben Clayton43581f12022-05-20 12:28:00 +0000104
105 /// @returns the selectors for the case
dan sinclairf148f082022-10-19 15:55:02 +0000106 const std::vector<const CaseSelector*>& Selectors() const { return selectors_; }
Ben Clayton43581f12022-05-20 12:28:00 +0000107
dan sinclair41e4d9a2022-05-01 14:40:55 +0000108 private:
109 const BlockStatement* body_ = nullptr;
dan sinclairf148f082022-10-19 15:55:02 +0000110 std::vector<const CaseSelector*> selectors_;
111};
112
113/// Holds semantic information about a switch case selector
dan sinclairbae54e72023-07-28 15:01:54 +0000114class CaseSelector final : public Castable<CaseSelector, Node> {
dan sinclairf148f082022-10-19 15:55:02 +0000115 public:
116 /// Constructor
117 /// @param decl the selector declaration
118 /// @param val the case selector value, nullptr for a default selector
dan sinclair464b3b82023-08-09 14:14:28 +0000119 explicit CaseSelector(const ast::CaseSelector* decl,
120 const core::constant::Value* val = nullptr);
dan sinclairf148f082022-10-19 15:55:02 +0000121
122 /// Destructor
123 ~CaseSelector() override;
124
125 /// @returns true if this is a default selector
126 bool IsDefault() const { return val_ == nullptr; }
127
128 /// @returns the case selector declaration
129 const ast::CaseSelector* Declaration() const;
130
131 /// @returns the selector constant value, or nullptr if this is the default selector
dan sinclair464b3b82023-08-09 14:14:28 +0000132 const core::constant::Value* Value() const { return val_; }
dan sinclairf148f082022-10-19 15:55:02 +0000133
134 private:
135 const ast::CaseSelector* const decl_;
dan sinclair464b3b82023-08-09 14:14:28 +0000136 const core::constant::Value* const val_;
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000137};
138
dan sinclairc990b3c2022-04-07 16:04:35 +0000139} // namespace tint::sem
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000140
dan sinclaird3b13692023-07-20 01:14:15 +0000141#endif // SRC_TINT_LANG_WGSL_SEM_SWITCH_STATEMENT_H_