Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 1 | // Copyright 2021 The Dawn & Tint Authors |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2 | // |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 3 | // Redistribution and use in source and binary forms, with or without |
| 4 | // modification, are permitted provided that the following conditions are met: |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 5 | // |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 6 | // 1. Redistributions of source code must retain the above copyright notice, this |
| 7 | // list of conditions and the following disclaimer. |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 8 | // |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 9 | // 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 Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 27 | |
dan sinclair | d3b1369 | 2023-07-20 01:14:15 +0000 | [diff] [blame] | 28 | #ifndef SRC_TINT_LANG_WGSL_SEM_SWITCH_STATEMENT_H_ |
| 29 | #define SRC_TINT_LANG_WGSL_SEM_SWITCH_STATEMENT_H_ |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 30 | |
Ben Clayton | 43581f1 | 2022-05-20 12:28:00 +0000 | [diff] [blame] | 31 | #include <vector> |
| 32 | |
dan sinclair | d3b1369 | 2023-07-20 01:14:15 +0000 | [diff] [blame] | 33 | #include "src/tint/lang/wgsl/sem/block_statement.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 34 | |
| 35 | // Forward declarations |
dan sinclair | c990b3c | 2022-04-07 16:04:35 +0000 | [diff] [blame] | 36 | namespace tint::ast { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 37 | class CaseStatement; |
dan sinclair | f148f08 | 2022-10-19 15:55:02 +0000 | [diff] [blame] | 38 | class CaseSelector; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 39 | class SwitchStatement; |
dan sinclair | c990b3c | 2022-04-07 16:04:35 +0000 | [diff] [blame] | 40 | } // namespace tint::ast |
dan sinclair | 464b3b8 | 2023-08-09 14:14:28 +0000 | [diff] [blame] | 41 | namespace tint::core::constant { |
dan sinclair | b53b8cf | 2022-12-15 16:25:31 +0000 | [diff] [blame] | 42 | class Value; |
dan sinclair | 464b3b8 | 2023-08-09 14:14:28 +0000 | [diff] [blame] | 43 | } // namespace tint::core::constant |
Ben Clayton | 43581f1 | 2022-05-20 12:28:00 +0000 | [diff] [blame] | 44 | namespace tint::sem { |
| 45 | class CaseStatement; |
dan sinclair | f148f08 | 2022-10-19 15:55:02 +0000 | [diff] [blame] | 46 | class CaseSelector; |
Ben Clayton | 3fb9a3f | 2023-02-04 21:20:26 +0000 | [diff] [blame] | 47 | class ValueExpression; |
Ben Clayton | 43581f1 | 2022-05-20 12:28:00 +0000 | [diff] [blame] | 48 | } // namespace tint::sem |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 49 | |
dan sinclair | c990b3c | 2022-04-07 16:04:35 +0000 | [diff] [blame] | 50 | namespace tint::sem { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 51 | |
| 52 | /// Holds semantic information about an switch statement |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 53 | class SwitchStatement final : public Castable<SwitchStatement, CompoundStatement> { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 54 | 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 Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 62 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 63 | /// Destructor |
| 64 | ~SwitchStatement() override; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 65 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 66 | /// @return the AST node for this statement |
| 67 | const ast::SwitchStatement* Declaration() const; |
Ben Clayton | 43581f1 | 2022-05-20 12:28:00 +0000 | [diff] [blame] | 68 | |
| 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 Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | /// Holds semantic information about a switch case statement |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 80 | class CaseStatement final : public Castable<CaseStatement, CompoundStatement> { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 81 | 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 Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 89 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 90 | /// Destructor |
| 91 | ~CaseStatement() override; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 92 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 93 | /// @return the AST node for this statement |
| 94 | const ast::CaseStatement* Declaration() const; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 95 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 96 | /// @param body the case body block statement |
| 97 | void SetBlock(const BlockStatement* body) { body_ = body; } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 98 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 99 | /// @returns the case body block statement |
| 100 | const BlockStatement* Body() const { return body_; } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 101 | |
Ben Clayton | 43581f1 | 2022-05-20 12:28:00 +0000 | [diff] [blame] | 102 | /// @returns the selectors for the case |
dan sinclair | f148f08 | 2022-10-19 15:55:02 +0000 | [diff] [blame] | 103 | std::vector<const CaseSelector*>& Selectors() { return selectors_; } |
Ben Clayton | 43581f1 | 2022-05-20 12:28:00 +0000 | [diff] [blame] | 104 | |
| 105 | /// @returns the selectors for the case |
dan sinclair | f148f08 | 2022-10-19 15:55:02 +0000 | [diff] [blame] | 106 | const std::vector<const CaseSelector*>& Selectors() const { return selectors_; } |
Ben Clayton | 43581f1 | 2022-05-20 12:28:00 +0000 | [diff] [blame] | 107 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 108 | private: |
| 109 | const BlockStatement* body_ = nullptr; |
dan sinclair | f148f08 | 2022-10-19 15:55:02 +0000 | [diff] [blame] | 110 | std::vector<const CaseSelector*> selectors_; |
| 111 | }; |
| 112 | |
| 113 | /// Holds semantic information about a switch case selector |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 114 | class CaseSelector final : public Castable<CaseSelector, Node> { |
dan sinclair | f148f08 | 2022-10-19 15:55:02 +0000 | [diff] [blame] | 115 | public: |
| 116 | /// Constructor |
| 117 | /// @param decl the selector declaration |
| 118 | /// @param val the case selector value, nullptr for a default selector |
dan sinclair | 464b3b8 | 2023-08-09 14:14:28 +0000 | [diff] [blame] | 119 | explicit CaseSelector(const ast::CaseSelector* decl, |
| 120 | const core::constant::Value* val = nullptr); |
dan sinclair | f148f08 | 2022-10-19 15:55:02 +0000 | [diff] [blame] | 121 | |
| 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 sinclair | 464b3b8 | 2023-08-09 14:14:28 +0000 | [diff] [blame] | 132 | const core::constant::Value* Value() const { return val_; } |
dan sinclair | f148f08 | 2022-10-19 15:55:02 +0000 | [diff] [blame] | 133 | |
| 134 | private: |
| 135 | const ast::CaseSelector* const decl_; |
dan sinclair | 464b3b8 | 2023-08-09 14:14:28 +0000 | [diff] [blame] | 136 | const core::constant::Value* const val_; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 137 | }; |
| 138 | |
dan sinclair | c990b3c | 2022-04-07 16:04:35 +0000 | [diff] [blame] | 139 | } // namespace tint::sem |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 140 | |
dan sinclair | d3b1369 | 2023-07-20 01:14:15 +0000 | [diff] [blame] | 141 | #endif // SRC_TINT_LANG_WGSL_SEM_SWITCH_STATEMENT_H_ |