diff --git a/src/tint/BUILD.gn b/src/tint/BUILD.gn index d74eaac120..45a965d8fe 100644 --- a/src/tint/BUILD.gn +++ b/src/tint/BUILD.gn @@ -429,6 +429,7 @@ libtint_source_set("libtint_core_all_src") { "sem/if_statement.h", "sem/index_accessor_expression.h", "sem/info.h", + "sem/load.h", "sem/loop_statement.h", "sem/materialize.h", "sem/module.h", @@ -662,6 +663,8 @@ libtint_source_set("libtint_sem_src") { "sem/index_accessor_expression.h", "sem/info.cc", "sem/info.h", + "sem/load.cc", + "sem/load.h", "sem/loop_statement.cc", "sem/loop_statement.h", "sem/materialize.cc", diff --git a/src/tint/CMakeLists.txt b/src/tint/CMakeLists.txt index d32da21856..630f9988d2 100644 --- a/src/tint/CMakeLists.txt +++ b/src/tint/CMakeLists.txt @@ -322,6 +322,8 @@ list(APPEND TINT_LIB_SRCS sem/index_accessor_expression.h sem/info.cc sem/info.h + sem/load.cc + sem/load.h sem/loop_statement.cc sem/loop_statement.h sem/materialize.cc diff --git a/src/tint/sem/load.cc b/src/tint/sem/load.cc new file mode 100644 index 0000000000..58f70683c0 --- /dev/null +++ b/src/tint/sem/load.cc @@ -0,0 +1,37 @@ +// Copyright 2022 The Tint Authors. +// +// Licensed under the Apache License, Version 2.0(the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "src/tint/sem/load.h" + +#include "src/tint/debug.h" +#include "src/tint/type/reference.h" + +TINT_INSTANTIATE_TYPEINFO(tint::sem::Load); + +namespace tint::sem { +Load::Load(const Expression* ref, const Statement* statement) + : Base(/* declaration */ ref->Declaration(), + /* type */ ref->Type()->UnwrapRef(), + /* stage */ EvaluationStage::kRuntime, // Loads can only be runtime + /* statement */ statement, + /* constant */ nullptr, + /* has_side_effects */ ref->HasSideEffects(), + /* root_ident */ ref->RootIdentifier()), + reference_(ref) { + TINT_ASSERT(Semantic, ref->Type()->Is()); +} + +Load::~Load() = default; + +} // namespace tint::sem diff --git a/src/tint/sem/load.h b/src/tint/sem/load.h new file mode 100644 index 0000000000..1a63266621 --- /dev/null +++ b/src/tint/sem/load.h @@ -0,0 +1,50 @@ +// Copyright 2022 The Tint Authors. +// +// Licensed under the Apache License, Version 2.0(the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef SRC_TINT_SEM_LOAD_H_ +#define SRC_TINT_SEM_LOAD_H_ + +#include "src/tint/sem/expression.h" +#include "src/tint/type/reference.h" + +namespace tint::sem { + +/// Load is a semantic expression which represents the load of a reference to a non-reference value. +/// Loads from reference types are implicit in WGSL, so the Load semantic node shares the same AST +/// node as the inner semantic node. +class Load final : public Castable { + public: + /// Constructor + /// @param reference the reference expression being loaded + /// @param statement the statement that owns this expression + Load(const Expression* reference, const Statement* statement); + + /// Destructor + ~Load() override; + + /// @return the reference being loaded + const Expression* Reference() const { return reference_; } + + /// @returns the type of the loaded reference. + const type::Reference* ReferenceType() const { + return static_cast(reference_->Type()); + } + + private: + Expression const* const reference_; +}; + +} // namespace tint::sem + +#endif // SRC_TINT_SEM_LOAD_H_ diff --git a/src/tint/sem/materialize.h b/src/tint/sem/materialize.h index 6fa77a4e44..8a65a03711 100644 --- a/src/tint/sem/materialize.h +++ b/src/tint/sem/materialize.h @@ -40,7 +40,7 @@ class Materialize final : public Castable { /// Destructor ~Materialize() override; - /// @return the target of the call + /// @return the expression being materialized const Expression* Expr() const { return expr_; } private: