From 2ddb1783c56bedb41799c906b8585f187982749c Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Mon, 8 Feb 2021 19:13:19 +0000 Subject: [PATCH] Add semantic::CallTarget, have Function derive from it CallTarget holds parameter information. This is simple to extract from an ast::Function. CallTarget will also be used for intrinsics, which can be overloaded. CallTarget will hold the resolved overload parameter signature. Bug: tint:361 Change-Id: I4dadc4a99293f12ede9e9cbd9132ba5f9b9830ed Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/40284 Reviewed-by: dan sinclair Commit-Queue: Ben Clayton --- BUILD.gn | 1 + src/CMakeLists.txt | 1 + src/semantic/call_target.h | 61 +++++++++++++++++++++++++++++++++ src/semantic/function.h | 19 +++++----- src/semantic/sem_call.cc | 1 - src/semantic/sem_call_target.cc | 30 ++++++++++++++++ src/semantic/sem_function.cc | 20 +++++++++-- src/semantic/variable.h | 1 - src/type_determiner.cc | 9 ++--- src/writer/spirv/test_helper.h | 1 + 10 files changed, 126 insertions(+), 18 deletions(-) create mode 100644 src/semantic/call_target.h create mode 100644 src/semantic/sem_call_target.cc diff --git a/BUILD.gn b/BUILD.gn index 5e3dbff0fb..6b49645700 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -383,6 +383,7 @@ source_set("libtint_core_src") { "src/semantic/intrinsic.h", "src/semantic/node.h", "src/semantic/sem_call.cc", + "src/semantic/sem_call_target.cc", "src/semantic/sem_expression.cc", "src/semantic/sem_function.cc", "src/semantic/sem_info.cc", diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 3b417f0b36..556e803df9 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -197,6 +197,7 @@ set(TINT_LIB_SRCS semantic/intrinsic.h semantic/node.h semantic/sem_call.cc + semantic/sem_call_target.cc semantic/sem_expression.cc semantic/sem_member_accessor_expression.cc semantic/sem_function.cc diff --git a/src/semantic/call_target.h b/src/semantic/call_target.h new file mode 100644 index 0000000000..ddb6aacb4f --- /dev/null +++ b/src/semantic/call_target.h @@ -0,0 +1,61 @@ +// Copyright 2021 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_SEMANTIC_CALL_TARGET_H_ +#define SRC_SEMANTIC_CALL_TARGET_H_ + +#include +#include + +#include "src/semantic/node.h" +#include "src/type/sampler_type.h" + +namespace tint { + +// Forward declarations +namespace type { +class Type; +} // namespace type + +namespace semantic { + +/// Parameter describes a single parameter of a call target +struct Parameter { + /// Parameter type + type::Type* type; +}; + +using Parameters = std::vector; + +/// CallTarget is the base for callable functions +class CallTarget : public Castable { + public: + /// Constructor + /// @param parameters the parameters for the call target + explicit CallTarget(const semantic::Parameters& parameters); + + /// Destructor + ~CallTarget() override; + + /// @return the parameters of the call target + const Parameters& Parameters() const { return parameters_; } + + private: + semantic::Parameters parameters_; +}; + +} // namespace semantic +} // namespace tint + +#endif // SRC_SEMANTIC_CALL_TARGET_H_ diff --git a/src/semantic/function.h b/src/semantic/function.h index 70708d986c..652e14d162 100644 --- a/src/semantic/function.h +++ b/src/semantic/function.h @@ -1,7 +1,6 @@ // Copyright 2021 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 // @@ -19,7 +18,7 @@ #include #include -#include "src/semantic/node.h" +#include "src/semantic/call_target.h" #include "src/type/sampler_type.h" namespace tint { @@ -27,20 +26,18 @@ namespace tint { // Forward declarations namespace ast { class BindingDecoration; +class BuiltinDecoration; +class Function; class GroupDecoration; class LocationDecoration; -class BuiltinDecoration; } // namespace ast -namespace type { -class Type; -} // namespace type namespace semantic { class Variable; /// Function holds the semantic information for function nodes. -class Function : public Castable { +class Function : public Castable { public: /// Information about a binding struct BindingInfo { @@ -51,13 +48,15 @@ class Function : public Castable { }; /// Constructor + /// @param ast the ast::Function /// @param referenced_module_vars the referenced module variables /// @param local_referenced_module_vars the locally referenced module /// variables /// @param ancestor_entry_points the ancestor entry points - explicit Function(std::vector referenced_module_vars, - std::vector local_referenced_module_vars, - std::vector ancestor_entry_points); + Function(ast::Function* ast, + std::vector referenced_module_vars, + std::vector local_referenced_module_vars, + std::vector ancestor_entry_points); /// Destructor ~Function() override; diff --git a/src/semantic/sem_call.cc b/src/semantic/sem_call.cc index fa6de41ae3..72be60a57c 100644 --- a/src/semantic/sem_call.cc +++ b/src/semantic/sem_call.cc @@ -1,7 +1,6 @@ // Copyright 2021 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 // diff --git a/src/semantic/sem_call_target.cc b/src/semantic/sem_call_target.cc new file mode 100644 index 0000000000..3b79a555cb --- /dev/null +++ b/src/semantic/sem_call_target.cc @@ -0,0 +1,30 @@ +// Copyright 2021 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/semantic/call_target.h" + +#include "src/type/type.h" + +TINT_INSTANTIATE_CLASS_ID(tint::semantic::CallTarget); + +namespace tint { +namespace semantic { + +CallTarget::CallTarget(const semantic::Parameters& parameters) + : parameters_(parameters) {} + +CallTarget::~CallTarget() = default; + +} // namespace semantic +} // namespace tint diff --git a/src/semantic/sem_function.cc b/src/semantic/sem_function.cc index 26768ec9fc..8c8d16c0eb 100644 --- a/src/semantic/sem_function.cc +++ b/src/semantic/sem_function.cc @@ -16,6 +16,7 @@ #include "src/ast/binding_decoration.h" #include "src/ast/builtin_decoration.h" +#include "src/ast/function.h" #include "src/ast/group_decoration.h" #include "src/ast/location_decoration.h" #include "src/ast/variable.h" @@ -30,10 +31,25 @@ TINT_INSTANTIATE_CLASS_ID(tint::semantic::Function); namespace tint { namespace semantic { -Function::Function(std::vector referenced_module_vars, +namespace { + +Parameters GetParameters(ast::Function* ast) { + semantic::Parameters parameters; + parameters.reserve(ast->params().size()); + for (auto* param : ast->params()) { + parameters.emplace_back(Parameter{param->type()}); + } + return parameters; +} + +} // namespace + +Function::Function(ast::Function* ast, + std::vector referenced_module_vars, std::vector local_referenced_module_vars, std::vector ancestor_entry_points) - : referenced_module_vars_(std::move(referenced_module_vars)), + : Base(GetParameters(ast)), + referenced_module_vars_(std::move(referenced_module_vars)), local_referenced_module_vars_(std::move(local_referenced_module_vars)), ancestor_entry_points_(std::move(ancestor_entry_points)) {} diff --git a/src/semantic/variable.h b/src/semantic/variable.h index 6f15dc7506..9032fe9355 100644 --- a/src/semantic/variable.h +++ b/src/semantic/variable.h @@ -1,7 +1,6 @@ // Copyright 2021 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 // diff --git a/src/type_determiner.cc b/src/type_determiner.cc index 8665519f2e..2e72f4af5b 100644 --- a/src/type_determiner.cc +++ b/src/type_determiner.cc @@ -1201,10 +1201,11 @@ void TypeDeterminer::CreateSemanticNodes() const { for (auto it : function_to_info_) { auto* func = it.first; auto* info = it.second; - sem.Add(func, builder_->create( - remap_vars(info->referenced_module_vars), - remap_vars(info->local_referenced_module_vars), - info->ancestor_entry_points)); + sem.Add(func, + builder_->create( + info->declaration, remap_vars(info->referenced_module_vars), + remap_vars(info->local_referenced_module_vars), + info->ancestor_entry_points)); } } diff --git a/src/writer/spirv/test_helper.h b/src/writer/spirv/test_helper.h index ec017ca4e5..aafc15d90c 100644 --- a/src/writer/spirv/test_helper.h +++ b/src/writer/spirv/test_helper.h @@ -16,6 +16,7 @@ #define SRC_WRITER_SPIRV_TEST_HELPER_H_ #include +#include #include #include "gtest/gtest.h"