blob: 274eeafd4befe8017cf119c98eabc57d22fb0604 [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#include "src/tint/lang/wgsl/sem/value_expression.h"
Ryan Harrisondbc13af2022-02-21 15:19:07 +000029
30#include <utility>
31
dan sinclaird3b13692023-07-20 01:14:15 +000032#include "src/tint/lang/wgsl/sem/load.h"
33#include "src/tint/lang/wgsl/sem/materialize.h"
dan sinclair22b4dd22023-07-21 00:40:07 +000034#include "src/tint/utils/rtti/switch.h"
Ben Clayton2081ee42022-05-19 19:32:29 +000035
Ben Clayton3fb9a3f2023-02-04 21:20:26 +000036TINT_INSTANTIATE_TYPEINFO(tint::sem::ValueExpression);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000037
dan sinclairc990b3c2022-04-07 16:04:35 +000038namespace tint::sem {
Ryan Harrisondbc13af2022-02-21 15:19:07 +000039
Ben Clayton3fb9a3f2023-02-04 21:20:26 +000040ValueExpression::ValueExpression(const ast::Expression* declaration,
dan sinclaircedcdf32023-08-10 02:39:48 +000041 const core::type::Type* type,
Ben Clayton36c61552023-08-08 07:58:19 +000042 core::EvaluationStage stage,
Ben Clayton3fb9a3f2023-02-04 21:20:26 +000043 const Statement* statement,
dan sinclair464b3b82023-08-09 14:14:28 +000044 const core::constant::Value* constant,
Ben Clayton3fb9a3f2023-02-04 21:20:26 +000045 bool has_side_effects,
46 const Variable* root_ident /* = nullptr */)
Ben Clayton0b4a2f12023-02-05 22:59:40 +000047 : Base(declaration, statement),
James Pricea7cd3ae2022-11-09 12:16:56 +000048 root_identifier_(root_ident),
Ryan Harrisondbc13af2022-02-21 15:19:07 +000049 type_(type),
Ben Clayton83bd7382022-07-15 23:46:31 +000050 stage_(stage),
Ryan Harrisondbc13af2022-02-21 15:19:07 +000051 constant_(std::move(constant)),
52 has_side_effects_(has_side_effects) {
Ben Claytonf848af22023-07-28 16:37:32 +000053 TINT_ASSERT(type_);
Ben Clayton36c61552023-08-08 07:58:19 +000054 TINT_ASSERT((constant != nullptr) == (stage == core::EvaluationStage::kConstant));
Ben Claytonf3f813e2023-01-04 12:30:47 +000055 if (constant != nullptr) {
Ben Claytonf848af22023-07-28 16:37:32 +000056 TINT_ASSERT(type_ == constant->Type());
Ben Claytonf3f813e2023-01-04 12:30:47 +000057 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +000058}
59
Ben Clayton3fb9a3f2023-02-04 21:20:26 +000060ValueExpression::~ValueExpression() = default;
Ryan Harrisondbc13af2022-02-21 15:19:07 +000061
Ben Clayton3fb9a3f2023-02-04 21:20:26 +000062const ValueExpression* ValueExpression::UnwrapMaterialize() const {
Ben Clayton2081ee42022-05-19 19:32:29 +000063 if (auto* m = As<Materialize>()) {
64 return m->Expr();
65 }
66 return this;
67}
68
Ben Clayton3fb9a3f2023-02-04 21:20:26 +000069const ValueExpression* ValueExpression::UnwrapLoad() const {
Ben Clayton2f9a9882022-12-17 02:20:04 +000070 if (auto* l = As<Load>()) {
71 return l->Reference();
72 }
73 return this;
74}
75
Ben Clayton3fb9a3f2023-02-04 21:20:26 +000076const ValueExpression* ValueExpression::Unwrap() const {
Ben Clayton2f9a9882022-12-17 02:20:04 +000077 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 sinclairc990b3c2022-04-07 16:04:35 +000084} // namespace tint::sem