// 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/function.h" #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" #include "src/ast/variable_decoration.h" #include "src/semantic/variable.h" #include "src/type/multisampled_texture_type.h" #include "src/type/sampled_texture_type.h" #include "src/type/storage_texture_type.h" #include "src/type/texture_type.h" TINT_INSTANTIATE_CLASS_ID(tint::semantic::Function); namespace tint { namespace semantic { namespace { ParameterList GetParameters(ast::Function* ast) { ParameterList parameters; parameters.reserve(ast->params().size()); for (auto* param : ast->params()) { parameters.emplace_back(Parameter{param->type(), Parameter::Usage::kNone}); } return parameters; } } // namespace Function::Function(ast::Function* declaration, std::vector referenced_module_vars, std::vector local_referenced_module_vars, std::vector ancestor_entry_points) : Base(declaration->return_type(), GetParameters(declaration)), declaration_(declaration), 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)) {} Function::~Function() = default; const std::vector> Function::ReferencedLocationVariables() const { std::vector> ret; for (auto* var : ReferencedModuleVariables()) { for (auto* deco : var->Declaration()->decorations()) { if (auto* location = deco->As()) { ret.push_back({var, location}); break; } } } return ret; } const std::vector> Function::ReferencedUniformVariables() const { std::vector> ret; for (auto* var : ReferencedModuleVariables()) { if (var->StorageClass() != ast::StorageClass::kUniform) { continue; } ast::BindingDecoration* binding = nullptr; ast::GroupDecoration* group = nullptr; for (auto* deco : var->Declaration()->decorations()) { if (auto* b = deco->As()) { binding = b; } else if (auto* g = deco->As()) { group = g; } } if (binding == nullptr || group == nullptr) { continue; } ret.push_back({var, BindingInfo{binding, group}}); } return ret; } const std::vector> Function::ReferencedStorageBufferVariables() const { std::vector> ret; for (auto* var : ReferencedModuleVariables()) { if (var->StorageClass() != ast::StorageClass::kStorage) { continue; } ast::BindingDecoration* binding = nullptr; ast::GroupDecoration* group = nullptr; for (auto* deco : var->Declaration()->decorations()) { if (auto* b = deco->As()) { binding = b; } else if (auto* s = deco->As()) { group = s; } } if (binding == nullptr || group == nullptr) { continue; } ret.push_back({var, BindingInfo{binding, group}}); } return ret; } const std::vector> Function::ReferencedBuiltinVariables() const { std::vector> ret; for (auto* var : ReferencedModuleVariables()) { for (auto* deco : var->Declaration()->decorations()) { if (auto* builtin = deco->As()) { ret.push_back({var, builtin}); break; } } } return ret; } const std::vector> Function::ReferencedSamplerVariables() const { return ReferencedSamplerVariablesImpl(type::SamplerKind::kSampler); } const std::vector> Function::ReferencedComparisonSamplerVariables() const { return ReferencedSamplerVariablesImpl(type::SamplerKind::kComparisonSampler); } const std::vector> Function::ReferencedSampledTextureVariables() const { return ReferencedSampledTextureVariablesImpl(false); } const std::vector> Function::ReferencedMultisampledTextureVariables() const { return ReferencedSampledTextureVariablesImpl(true); } const std::vector> Function::ReferencedStorageTextureVariables() const { std::vector> ret; for (auto* var : ReferencedModuleVariables()) { auto* unwrapped_type = var->Declaration()->type()->UnwrapIfNeeded(); auto* storage_texture = unwrapped_type->As(); if (storage_texture == nullptr) { continue; } ast::BindingDecoration* binding = nullptr; ast::GroupDecoration* group = nullptr; for (auto* deco : var->Declaration()->decorations()) { if (auto* b = deco->As()) { binding = b; } else if (auto* s = deco->As()) { group = s; } } if (binding == nullptr || group == nullptr) { continue; } ret.push_back({var, BindingInfo{binding, group}}); } return ret; } const std::vector> Function::LocalReferencedBuiltinVariables() const { std::vector> ret; for (auto* var : LocalReferencedModuleVariables()) { for (auto* deco : var->Declaration()->decorations()) { if (auto* builtin = deco->As()) { ret.push_back({var, builtin}); break; } } } return ret; } bool Function::HasAncestorEntryPoint(Symbol symbol) const { for (const auto& point : ancestor_entry_points_) { if (point == symbol) { return true; } } return false; } const std::vector> Function::ReferencedSamplerVariablesImpl(type::SamplerKind kind) const { std::vector> ret; for (auto* var : ReferencedModuleVariables()) { auto* unwrapped_type = var->Declaration()->type()->UnwrapIfNeeded(); auto* sampler = unwrapped_type->As(); if (sampler == nullptr || sampler->kind() != kind) { continue; } ast::BindingDecoration* binding = nullptr; ast::GroupDecoration* group = nullptr; for (auto* deco : var->Declaration()->decorations()) { if (auto* b = deco->As()) { binding = b; } if (auto* s = deco->As()) { group = s; } } if (binding == nullptr || group == nullptr) { continue; } ret.push_back({var, BindingInfo{binding, group}}); } return ret; } const std::vector> Function::ReferencedSampledTextureVariablesImpl(bool multisampled) const { std::vector> ret; for (auto* var : ReferencedModuleVariables()) { auto* unwrapped_type = var->Declaration()->type()->UnwrapIfNeeded(); auto* texture = unwrapped_type->As(); if (texture == nullptr) { continue; } auto is_multisampled = texture->Is(); auto is_sampled = texture->Is(); if ((multisampled && !is_multisampled) || (!multisampled && !is_sampled)) { continue; } ast::BindingDecoration* binding = nullptr; ast::GroupDecoration* group = nullptr; for (auto* deco : var->Declaration()->decorations()) { if (auto* b = deco->As()) { binding = b; } else if (auto* s = deco->As()) { group = s; } } if (binding == nullptr || group == nullptr) { continue; } ret.push_back({var, BindingInfo{binding, group}}); } return ret; } } // namespace semantic } // namespace tint