dawn-cmake/src/tint/ast/function.cc
Ben Clayton 971318f7a2 tint: Replace all remaining AST types with ast::Type
This CL removes the following AST nodes:
* ast::Array
* ast::Atomic
* ast::Matrix
* ast::MultisampledTexture
* ast::Pointer
* ast::SampledTexture
* ast::Texture
* ast::TypeName
* ast::Vector

ast::Type, which used to be the base class for all AST types, is now a
thin wrapper around ast::IdentifierExpression. All types are now
referred to using their type name.

The resolver now handles type resolution and validation of the types
listed above based on the TemplateIdentifier arguments.

Other changes:
* ProgramBuilder has undergone substantial refactoring.
* ProgramBuilder helpers for type inferencing is now more explicit.
  Instead of passing 'nullptr', a new 'Infer' template argument is
  passed.
* ast::CheckIdentifier() is used for more tests that check identifiers,
  including types.

Bug: tint:1810
Change-Id: I8e739ef49435dc1c20a462f3ec5ba265661a7edb
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/118723
Reviewed-by: Dan Sinclair <dsinclair@chromium.org>
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: James Price <jrprice@google.com>
2023-02-14 13:52:43 +00:00

111 lines
3.5 KiB
C++

// Copyright 2020 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/ast/function.h"
#include "src/tint/ast/stage_attribute.h"
#include "src/tint/ast/workgroup_attribute.h"
#include "src/tint/program_builder.h"
TINT_INSTANTIATE_TYPEINFO(tint::ast::Function);
namespace tint::ast {
Function::Function(ProgramID pid,
NodeID nid,
const Source& src,
const Identifier* n,
utils::VectorRef<const Parameter*> parameters,
Type return_ty,
const BlockStatement* b,
utils::VectorRef<const Attribute*> attrs,
utils::VectorRef<const Attribute*> return_type_attrs)
: Base(pid, nid, src),
name(n),
params(std::move(parameters)),
return_type(return_ty),
body(b),
attributes(std::move(attrs)),
return_type_attributes(std::move(return_type_attrs)) {
TINT_ASSERT(AST, name);
if (name) {
TINT_ASSERT(AST, !name->Is<TemplatedIdentifier>());
}
TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, name, program_id);
TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, return_ty, program_id);
TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, body, program_id);
for (auto* param : params) {
TINT_ASSERT(AST, tint::Is<Parameter>(param));
TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, param, program_id);
}
for (auto* attr : attributes) {
TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, attr, program_id);
}
for (auto* attr : return_type_attributes) {
TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, attr, program_id);
}
}
Function::Function(Function&&) = default;
Function::~Function() = default;
PipelineStage Function::PipelineStage() const {
if (auto* stage = GetAttribute<StageAttribute>(attributes)) {
return stage->stage;
}
return PipelineStage::kNone;
}
const Function* Function::Clone(CloneContext* ctx) const {
// Clone arguments outside of create() call to have deterministic ordering
auto src = ctx->Clone(source);
auto n = ctx->Clone(name);
auto p = ctx->Clone(params);
auto ret = ctx->Clone(return_type);
auto* b = ctx->Clone(body);
auto attrs = ctx->Clone(attributes);
auto ret_attrs = ctx->Clone(return_type_attributes);
return ctx->dst->create<Function>(src, n, p, ret, b, attrs, ret_attrs);
}
const Function* FunctionList::Find(Symbol sym) const {
for (auto* func : *this) {
if (func->name->symbol == sym) {
return func;
}
}
return nullptr;
}
const Function* FunctionList::Find(Symbol sym, PipelineStage stage) const {
for (auto* func : *this) {
if (func->name->symbol == sym && func->PipelineStage() == stage) {
return func;
}
}
return nullptr;
}
bool FunctionList::HasStage(ast::PipelineStage stage) const {
for (auto* func : *this) {
if (func->PipelineStage() == stage) {
return true;
}
}
return false;
}
} // namespace tint::ast