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 | #include "src/tint/lang/wgsl/sem/value_expression.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 29 | |
| 30 | #include <utility> |
| 31 | |
dan sinclair | d3b1369 | 2023-07-20 01:14:15 +0000 | [diff] [blame] | 32 | #include "src/tint/lang/wgsl/sem/load.h" |
| 33 | #include "src/tint/lang/wgsl/sem/materialize.h" |
dan sinclair | 22b4dd2 | 2023-07-21 00:40:07 +0000 | [diff] [blame] | 34 | #include "src/tint/utils/rtti/switch.h" |
Ben Clayton | 2081ee4 | 2022-05-19 19:32:29 +0000 | [diff] [blame] | 35 | |
Ben Clayton | 3fb9a3f | 2023-02-04 21:20:26 +0000 | [diff] [blame] | 36 | TINT_INSTANTIATE_TYPEINFO(tint::sem::ValueExpression); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 37 | |
dan sinclair | c990b3c | 2022-04-07 16:04:35 +0000 | [diff] [blame] | 38 | namespace tint::sem { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 39 | |
Ben Clayton | 3fb9a3f | 2023-02-04 21:20:26 +0000 | [diff] [blame] | 40 | ValueExpression::ValueExpression(const ast::Expression* declaration, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 41 | const core::type::Type* type, |
Ben Clayton | 36c6155 | 2023-08-08 07:58:19 +0000 | [diff] [blame] | 42 | core::EvaluationStage stage, |
Ben Clayton | 3fb9a3f | 2023-02-04 21:20:26 +0000 | [diff] [blame] | 43 | const Statement* statement, |
dan sinclair | 464b3b8 | 2023-08-09 14:14:28 +0000 | [diff] [blame] | 44 | const core::constant::Value* constant, |
Ben Clayton | 3fb9a3f | 2023-02-04 21:20:26 +0000 | [diff] [blame] | 45 | bool has_side_effects, |
| 46 | const Variable* root_ident /* = nullptr */) |
Ben Clayton | 0b4a2f1 | 2023-02-05 22:59:40 +0000 | [diff] [blame] | 47 | : Base(declaration, statement), |
James Price | a7cd3ae | 2022-11-09 12:16:56 +0000 | [diff] [blame] | 48 | root_identifier_(root_ident), |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 49 | type_(type), |
Ben Clayton | 83bd738 | 2022-07-15 23:46:31 +0000 | [diff] [blame] | 50 | stage_(stage), |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 51 | constant_(std::move(constant)), |
| 52 | has_side_effects_(has_side_effects) { |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 53 | TINT_ASSERT(type_); |
Ben Clayton | 36c6155 | 2023-08-08 07:58:19 +0000 | [diff] [blame] | 54 | TINT_ASSERT((constant != nullptr) == (stage == core::EvaluationStage::kConstant)); |
Ben Clayton | f3f813e | 2023-01-04 12:30:47 +0000 | [diff] [blame] | 55 | if (constant != nullptr) { |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 56 | TINT_ASSERT(type_ == constant->Type()); |
Ben Clayton | f3f813e | 2023-01-04 12:30:47 +0000 | [diff] [blame] | 57 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Ben Clayton | 3fb9a3f | 2023-02-04 21:20:26 +0000 | [diff] [blame] | 60 | ValueExpression::~ValueExpression() = default; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 61 | |
Ben Clayton | 3fb9a3f | 2023-02-04 21:20:26 +0000 | [diff] [blame] | 62 | const ValueExpression* ValueExpression::UnwrapMaterialize() const { |
Ben Clayton | 2081ee4 | 2022-05-19 19:32:29 +0000 | [diff] [blame] | 63 | if (auto* m = As<Materialize>()) { |
| 64 | return m->Expr(); |
| 65 | } |
| 66 | return this; |
| 67 | } |
| 68 | |
Ben Clayton | 3fb9a3f | 2023-02-04 21:20:26 +0000 | [diff] [blame] | 69 | const ValueExpression* ValueExpression::UnwrapLoad() const { |
Ben Clayton | 2f9a988 | 2022-12-17 02:20:04 +0000 | [diff] [blame] | 70 | if (auto* l = As<Load>()) { |
| 71 | return l->Reference(); |
| 72 | } |
| 73 | return this; |
| 74 | } |
| 75 | |
Ben Clayton | 3fb9a3f | 2023-02-04 21:20:26 +0000 | [diff] [blame] | 76 | const ValueExpression* ValueExpression::Unwrap() const { |
Ben Clayton | 2f9a988 | 2022-12-17 02:20:04 +0000 | [diff] [blame] | 77 | return Switch( |
| 78 | this, // note: An expression can only be wrapped by a Load or Materialize, not both. |
| 79 | [&](const Load* load) { return load->Reference(); }, |
| 80 | [&](const Materialize* materialize) { return materialize->Expr(); }, |
| 81 | [&](Default) { return this; }); |
| 82 | } |
| 83 | |
dan sinclair | c990b3c | 2022-04-07 16:04:35 +0000 | [diff] [blame] | 84 | } // namespace tint::sem |